Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFZ Instrument: show all Parameters, improve layout #166

Merged
merged 9 commits into from
Oct 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,34 @@ class InstrumentSFZConductor: ObservableObject, HasAudioEngine {
struct InstrumentSFZView: View {
@StateObject var conductor = InstrumentSFZConductor()
@Environment(\.colorScheme) var colorScheme
@Environment(\.horizontalSizeClass) var horizontalSizeClass

var body: some View {
HStack {
ForEach(0...7, id: \.self) {
ParameterRow(param: conductor.instrument.parameters[$0])
}
}.padding(5)
HStack {
ForEach(8...15, id: \.self) {
ParameterRow(param: conductor.instrument.parameters[$0])
}
}.padding(5)
HStack {
ForEach(16...23, id: \.self) {
ParameterRow(param: conductor.instrument.parameters[$0])
}
}.padding(5)
HStack {
ForEach(24...30, id: \.self) {
ParameterRow(param: conductor.instrument.parameters[$0])
let instrumentParams = conductor.instrument.parameters
let paramsPerLine = horizontalSizeClass == .compact ? 6 : 8
let instrumentParamsChunked = instrumentParams.chunked(into: paramsPerLine)

GeometryReader { geoProxy in
VStack {
let paramRows = ForEach(0..<instrumentParamsChunked.count, id: \.self) { chunkIndex in
HStack {
ForEach(instrumentParamsChunked[chunkIndex], id: \.self) { param in
ParameterRow(param: param)
}
}.padding(5)
}
// i wanted to do it with verticalSizeClass, but couldn't work it out
if horizontalSizeClass == .compact {
ScrollView {
paramRows
}
} else {
paramRows
}
CookbookKeyboard(noteOn: conductor.noteOn,
noteOff: conductor.noteOff).frame(height: geoProxy.size.height / 5)
}
}.padding(5)
CookbookKeyboard(noteOn: conductor.noteOn,
noteOff: conductor.noteOff)
}
.cookbookNavBarTitle("Instrument SFZ")
.onAppear {
conductor.start()
Expand All @@ -70,3 +74,25 @@ struct InstrumentSFZView: View {
Color.clear : Color(red: 0.9, green: 0.9, blue: 0.9))
}
}

extension Array {
func chunked(into size: Int) -> [[Element]] {
return stride(from: 0, to: count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, count)])
}
}
}

extension NodeParameter: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(def.identifier)
}
}

extension NodeParameter: Equatable {
public static func == (lhs: NodeParameter, rhs: NodeParameter) -> Bool {
// NodeParameter wraps AUParameter which should
// conform to equtable as they are NSObjects
return lhs.parameter == rhs.parameter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public struct ParameterRow: View {
.frame(height: 50)
switch param.def.unit {
case .boolean:
Toggle(isOn: Binding(get: { param.value == 1.0 }, set: {
Toggle("", isOn: Binding(get: { param.value == 1.0 }, set: {
param.value = $0 ? 1.0 : 0.0; refresher.version += 1
}), label: { Text(param.def.name) })
}))
case .indexed:
if param.range.upperBound - param.range.lowerBound < 5 {
Picker(param.def.name, selection: getIntBinding()) {
Expand Down
Loading