Skip to content

Commit 52988de

Browse files
Merge pull request #166 from mahal/sfz-sampler
SFZ Instrument: show all Parameters, improve layout
2 parents 7148d40 + ca7d7a4 commit 52988de

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/MiniApps/InstrumentSFZ.swift

+47-21
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,34 @@ class InstrumentSFZConductor: ObservableObject, HasAudioEngine {
3535
struct InstrumentSFZView: View {
3636
@StateObject var conductor = InstrumentSFZConductor()
3737
@Environment(\.colorScheme) var colorScheme
38+
@Environment(\.horizontalSizeClass) var horizontalSizeClass
3839

3940
var body: some View {
40-
HStack {
41-
ForEach(0...7, id: \.self) {
42-
ParameterRow(param: conductor.instrument.parameters[$0])
43-
}
44-
}.padding(5)
45-
HStack {
46-
ForEach(8...15, id: \.self) {
47-
ParameterRow(param: conductor.instrument.parameters[$0])
48-
}
49-
}.padding(5)
50-
HStack {
51-
ForEach(16...23, id: \.self) {
52-
ParameterRow(param: conductor.instrument.parameters[$0])
53-
}
54-
}.padding(5)
55-
HStack {
56-
ForEach(24...30, id: \.self) {
57-
ParameterRow(param: conductor.instrument.parameters[$0])
41+
let instrumentParams = conductor.instrument.parameters
42+
let paramsPerLine = horizontalSizeClass == .compact ? 6 : 8
43+
let instrumentParamsChunked = instrumentParams.chunked(into: paramsPerLine)
44+
45+
GeometryReader { geoProxy in
46+
VStack {
47+
let paramRows = ForEach(0..<instrumentParamsChunked.count, id: \.self) { chunkIndex in
48+
HStack {
49+
ForEach(instrumentParamsChunked[chunkIndex], id: \.self) { param in
50+
ParameterRow(param: param)
51+
}
52+
}.padding(5)
53+
}
54+
// i wanted to do it with verticalSizeClass, but couldn't work it out
55+
if horizontalSizeClass == .compact {
56+
ScrollView {
57+
paramRows
58+
}
59+
} else {
60+
paramRows
61+
}
62+
CookbookKeyboard(noteOn: conductor.noteOn,
63+
noteOff: conductor.noteOff).frame(height: geoProxy.size.height / 5)
5864
}
59-
}.padding(5)
60-
CookbookKeyboard(noteOn: conductor.noteOn,
61-
noteOff: conductor.noteOff)
65+
}
6266
.cookbookNavBarTitle("Instrument SFZ")
6367
.onAppear {
6468
conductor.start()
@@ -70,3 +74,25 @@ struct InstrumentSFZView: View {
7074
Color.clear : Color(red: 0.9, green: 0.9, blue: 0.9))
7175
}
7276
}
77+
78+
extension Array {
79+
func chunked(into size: Int) -> [[Element]] {
80+
return stride(from: 0, to: count, by: size).map {
81+
Array(self[$0 ..< Swift.min($0 + size, count)])
82+
}
83+
}
84+
}
85+
86+
extension NodeParameter: Hashable {
87+
public func hash(into hasher: inout Hasher) {
88+
hasher.combine(def.identifier)
89+
}
90+
}
91+
92+
extension NodeParameter: Equatable {
93+
public static func == (lhs: NodeParameter, rhs: NodeParameter) -> Bool {
94+
// NodeParameter wraps AUParameter which should
95+
// conform to equtable as they are NSObjects
96+
return lhs.parameter == rhs.parameter
97+
}
98+
}

Cookbook/CookbookCommon/Sources/CookbookCommon/Reusable Components/ParameterRow.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public struct ParameterRow: View {
5656
.frame(height: 50)
5757
switch param.def.unit {
5858
case .boolean:
59-
Toggle(isOn: Binding(get: { param.value == 1.0 }, set: {
59+
Toggle("", isOn: Binding(get: { param.value == 1.0 }, set: {
6060
param.value = $0 ? 1.0 : 0.0; refresher.version += 1
61-
}), label: { Text(param.def.name) })
61+
}))
6262
case .indexed:
6363
if param.range.upperBound - param.range.lowerBound < 5 {
6464
Picker(param.def.name, selection: getIntBinding()) {

0 commit comments

Comments
 (0)