Skip to content

Commit 8578658

Browse files
Merge pull request #173 from ayaysir/Refactor-Arpeggiator
Refactor MiniApps/Arpeggiator
2 parents 7579888 + 9dc9bde commit 8578658

1 file changed

Lines changed: 14 additions & 34 deletions

File tree

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

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ArpeggiatorConductor: ObservableObject, HasAudioEngine {
1414
var midiCallback: CallbackInstrument!
1515

1616
var heldNotes = [Int]()
17-
var arpUp = false
17+
var isArpDescending = false
1818
var currentNote = 0
1919
var sequencerNoteLength = 1.0
2020

@@ -45,46 +45,26 @@ class ArpeggiatorConductor: ObservableObject, HasAudioEngine {
4545
return
4646
}
4747

48-
//UP
49-
if !arpUp {
50-
let tempArray = heldNotes
51-
var arrayValue = 0
52-
if tempArray.max() != currentNote {
53-
arrayValue = tempArray.sorted().first(where: { $0 > currentNote }) ?? tempArray.min()!
54-
currentNote = arrayValue
55-
}else{
56-
arpUp = true
57-
arrayValue = tempArray.sorted().last(where: { $0 < currentNote }) ?? tempArray.max()!
58-
currentNote = arrayValue
48+
if !isArpDescending {
49+
if heldNotes.max() != currentNote {
50+
currentNote = heldNotes.filter { $0 > currentNote }.min() ?? heldNotes.min()!
51+
} else {
52+
isArpDescending = true
53+
currentNote = heldNotes.filter { $0 < currentNote }.max() ?? heldNotes.max()!
5954
}
60-
61-
}else{
62-
//DOWN
63-
let tempArray = heldNotes
64-
var arrayValue = 0
65-
if tempArray.min() != currentNote {
66-
arrayValue = tempArray.sorted().last(where: { $0 < currentNote }) ?? tempArray.max()!
67-
currentNote = arrayValue
68-
}else{
69-
arpUp = false
70-
arrayValue = tempArray.sorted().first(where: { $0 > currentNote }) ?? tempArray.min()!
71-
currentNote = arrayValue
55+
} else {
56+
if heldNotes.min() != currentNote {
57+
currentNote = heldNotes.filter { $0 < currentNote }.max() ?? heldNotes.max()!
58+
} else {
59+
isArpDescending = false
60+
currentNote = heldNotes.filter { $0 > currentNote }.min() ?? heldNotes.min()!
7261
}
7362
}
7463
instrument.play(noteNumber: MIDINoteNumber(currentNote), velocity: 120, channel: 0)
7564
}
7665

7766
func noteOff(pitch: Pitch) {
78-
let mynote = pitch.intValue
79-
80-
//remove notes from an array
81-
for i in heldNotes {
82-
if i == mynote {
83-
heldNotes = heldNotes.filter {
84-
$0 != mynote
85-
}
86-
}
87-
}
67+
heldNotes = heldNotes.filter { $0 != pitch.intValue }
8868
}
8969

9070
init() {

0 commit comments

Comments
 (0)