Skip to content

Commit

Permalink
Merge pull request #166 from mahal/sfz-sampler
Browse files Browse the repository at this point in the history
SFZ Instrument: show all Parameters, improve layout
  • Loading branch information
NickCulbertson authored Oct 10, 2024
2 parents 7148d40 + ca7d7a4 commit 52988de
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
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

0 comments on commit 52988de

Please sign in to comment.