Skip to content

Commit 997b855

Browse files
committed
Made pitch range an associated value for keyboard layouts that use that
1 parent 664ffdb commit 997b855

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

Demo/Shared/ContentView.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ struct ContentView: View {
2121

2222
var body: some View {
2323
HStack {
24-
Keyboard(pitchRange: Pitch(48)...Pitch(77),
25-
layout: .pianoRoll).frame(width: 200)
24+
Keyboard(layout: .pianoRoll(pitchRange: Pitch(48)...Pitch(77))).frame(width: 200)
2625
VStack {
2726
HStack {
2827
Stepper("Lowest Note: \(Pitch(intValue: lowNote).note(in: .C).description)",
@@ -48,10 +47,9 @@ struct ContentView: View {
4847

4948
})
5049
}
51-
Keyboard(pitchRange: Pitch(intValue: lowNote)...Pitch(intValue: highNote),
50+
Keyboard(layout: .piano(pitchRange: Pitch(intValue: lowNote)...Pitch(intValue: highNote)),
5251
noteOn: noteOn, noteOff: noteOff)
53-
Keyboard(pitchRange: Pitch(12)...Pitch(84),
54-
layout: .isomorphic,
52+
Keyboard(layout: .isomorphic(pitchRange: Pitch(12)...Pitch(84)),
5553
noteOn: noteOn, noteOff: noteOff)
5654
Keyboard(layout: .guitar(openPitches: [Pitch(64), Pitch(59), Pitch(55), Pitch(50), Pitch(45), Pitch(40)], fretcount: 22),
5755
noteOn: noteOn, noteOff: noteOff) { pitch, isActivated in
@@ -61,8 +59,7 @@ struct ContentView: View {
6159
pressedColor: Color(PitchColor.newtonian[Int(pitch.pitchClass)]),
6260
alignment: .center)
6361
}
64-
Keyboard(pitchRange: Pitch(48)...Pitch(65),
65-
layout: .isomorphic) { pitch, isActivated in
62+
Keyboard(layout: .isomorphic(pitchRange: Pitch(48)...Pitch(65))) { pitch, isActivated in
6663
KeyboardKey(pitch: pitch,
6764
isActivated: isActivated,
6865
text: pitch.note(in: .F).description,

Sources/Keyboard/Keyboard.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ public struct Keyboard<Content>: View where Content: View {
66

77
@StateObject var model: KeyboardModel = KeyboardModel()
88

9-
var pitchRange: ClosedRange<Pitch>
109
var latching: Bool
1110
var noteOn: (Pitch) -> Void
1211
var noteOff: (Pitch) -> Void
1312
var layout: KeyboardLayout
1413

15-
public init(pitchRange: ClosedRange<Pitch> = (Pitch(60)...Pitch(72)),
14+
public init(layout: KeyboardLayout = .piano(pitchRange: (Pitch(60)...Pitch(72))),
1615
latching: Bool = false,
17-
layout: KeyboardLayout = .piano,
16+
1817
noteOn: @escaping (Pitch) -> Void = { _ in },
1918
noteOff: @escaping (Pitch) -> Void = { _ in },
2019
@ViewBuilder content: @escaping (Pitch, Bool)->Content) {
21-
self.pitchRange = pitchRange
2220
self.latching = latching
2321
self.layout = layout
2422
self.noteOn = noteOn
@@ -29,13 +27,13 @@ public struct Keyboard<Content>: View where Content: View {
2927
public var body: some View {
3028
Group {
3129
switch layout {
32-
case .piano:
30+
case .piano(let pitchRange):
3331
Piano(content: content, model: model, pitchRange: pitchRange, latching: latching)
34-
case .isomorphic:
32+
case .isomorphic(let pitchRange):
3533
Isomorphic(content: content, model: model, pitchRange: pitchRange, latching: latching)
3634
case .guitar(let openPitches, let fretCount):
3735
Guitar(content: content, model: model, openPitches: openPitches, fretCount: fretCount, latching: latching)
38-
case .pianoRoll:
36+
case .pianoRoll(let pitchRange):
3937
PianoRoll(content: content, model: model, pitchRange: pitchRange, latching: latching)
4038
}
4139

@@ -52,26 +50,28 @@ public struct Keyboard<Content>: View where Content: View {
5250

5351
extension Keyboard where Content == KeyboardKey {
5452

55-
public init(pitchRange: ClosedRange<Pitch> = (Pitch(60)...Pitch(72)),
53+
public init(layout: KeyboardLayout = .piano(pitchRange: (Pitch(60)...Pitch(72))),
5654
latching: Bool = false,
57-
layout: KeyboardLayout = .piano,
5855
noteOn: @escaping (Pitch) -> Void = { _ in },
5956
noteOff: @escaping (Pitch) -> Void = { _ in }){
60-
self.pitchRange = pitchRange
61-
self.latching = latching
6257
self.layout = layout
58+
self.latching = latching
6359
self.noteOn = noteOn
6460
self.noteOff = noteOff
61+
6562
var alignment: Alignment = .bottom
63+
64+
var flatTop = false
6665
switch layout {
6766
case .guitar(_, _):
6867
alignment = .center
69-
case .pianoRoll:
70-
alignment = .trailing
71-
default:
68+
case .isomorphic(_):
7269
alignment = .bottom
73-
70+
case .piano(_):
71+
flatTop = true
72+
case .pianoRoll(_):
73+
alignment = .trailing
7474
}
75-
self.content = { KeyboardKey(pitch: $0, isActivated: $1, flatTop: layout == .piano, alignment: alignment) }
75+
self.content = { KeyboardKey(pitch: $0, isActivated: $1, flatTop: flatTop, alignment: alignment) }
7676
}
7777
}

Sources/Keyboard/Layouts/KeyboardLayout.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import Tonic
33

44
/// Types of keyboards we can generate
55
public enum KeyboardLayout: Equatable, Hashable {
6-
/// Traditional Piano layout with raised black keys over white keys
7-
case piano
8-
9-
/// All notes linearly right after one another
10-
case isomorphic
11-
126
/// Guitar in arbitrary tuning, from first string (highest) to loweset string
137
case guitar(openPitches: [Pitch], fretcount: Int)
148

9+
/// All notes linearly right after one another
10+
case isomorphic(pitchRange: ClosedRange<Pitch>)
11+
12+
/// Traditional Piano layout with raised black keys over white keys
13+
case piano(pitchRange: ClosedRange<Pitch>)
14+
1515
/// Vertical isomorphic
16-
case pianoRoll
16+
case pianoRoll(pitchRange: ClosedRange<Pitch>)
1717
}

0 commit comments

Comments
 (0)