Skip to content

Commit 4e3e9a0

Browse files
authored
Merge pull request #4 from eljeff/develop
Sequencer cleanup
2 parents 981c703 + 8496f5f commit 4e3e9a0

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

Sources/AudioKitEX/Sequencing/Sequencer.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ open class Sequencer {
1515
/// Overall playback speed
1616
open var tempo: BPM {
1717
get { return tracks.first?.tempo ?? 0 }
18-
set { for track in tracks { track.tempo = newValue } }
18+
set { tracks.forEach { $0.tempo = newValue } }
1919
}
2020

2121
/// Length in beats
2222
open var length: Double {
2323
get { return tracks.max(by: { $0.length > $1.length })?.length ?? 0 }
24-
set { for track in tracks { track.length = newValue } }
24+
set { tracks.forEach { $0.length = newValue } }
2525
}
2626

2727
/// Whether or not looping is enabled
2828
open var loopEnabled: Bool {
2929
get { return tracks.first?.loopEnabled ?? false }
30-
set { for track in tracks { track.loopEnabled = newValue } }
30+
set { tracks.forEach { $0.loopEnabled = newValue } }
3131
}
3232

3333
/// Is the sequencer currently playing
@@ -63,27 +63,27 @@ open class Sequencer {
6363

6464
/// Start playback of the track from the current position (like unpause)
6565
public func play() {
66-
for track in tracks { track.play() }
66+
tracks.forEach { $0.play() }
6767
}
6868

6969
/// Start the playback of the track from the beginning
7070
public func playFromStart() {
71-
for track in tracks { track.playFromStart() }
71+
tracks.forEach { $0.playFromStart() }
7272
}
7373

7474
/// Start playback after a certain number of beats
7575
public func playAfterDelay(beats: Double) {
76-
for track in tracks { track.playAfterDelay(beats: beats) }
76+
tracks.forEach { $0.playAfterDelay(beats: beats) }
7777
}
7878

7979
/// Stop playback
8080
public func stop() {
81-
for track in tracks { track.stop() }
81+
tracks.forEach { $0.stop() }
8282
}
8383

8484
/// Rewind playback
8585
public func rewind() {
86-
for track in tracks { track.rewind() }
86+
tracks.forEach { $0.rewind() }
8787
}
8888

8989
/// Load MIDI data from a file URL
@@ -117,14 +117,15 @@ open class Sequencer {
117117
length = tracks.max(by: { $0.length > $1.length })?.length ?? 0
118118
}
119119

120-
/// Add a MIDI note to the track
120+
/// Add a MIDI noteOn and noteOff to the track
121121
/// - Parameters:
122122
/// - noteNumber: MIDI Note number to add
123123
/// - velocity: Velocity of the note
124124
/// - channel: Channel to place the note on
125125
/// - position: Location in beats of the new note
126126
/// - duration: Duration in beats of the new note
127127
/// - trackIndex: Which track to add the note to
128+
@available(*, deprecated, message: "Access track directly for editing - sequencer.tracks[i].add(...)")
128129
public func add(noteNumber: MIDINoteNumber,
129130
velocity: MIDIVelocity = 127,
130131
channel: MIDIChannel = 0,
@@ -147,6 +148,7 @@ open class Sequencer {
147148
/// - event: Event to add
148149
/// - position: Location in time in beats to add the event at
149150
/// - trackIndex: Which track to add the event
151+
@available(*, deprecated, message: "Access track directly for editing - sequencer.tracks[i].add(...)")
150152
public func add(event: MIDIEvent, position: Double, trackIndex: Int = 0) {
151153
guard tracks.count > trackIndex, trackIndex >= 0 else {
152154
Log("Track index \(trackIndex) out of range (sequencer has \(tracks.count) tracks)")
@@ -157,9 +159,7 @@ open class Sequencer {
157159

158160
/// Remove all notes
159161
public func clear() {
160-
for track in tracks {
161-
track.clear()
162-
}
162+
tracks.forEach { $0.clear() }
163163
}
164164

165165
/// Move to a new time in the playback
@@ -183,7 +183,7 @@ open class Sequencer {
183183
/// Add track associated with a node
184184
/// - Parameter node: Node to create the track for
185185
/// - Returns: Track associated with the given node
186-
public func addTrack(for node: Node) -> SequencerTrack {
186+
@discardableResult public func addTrack(for node: Node) -> SequencerTrack {
187187
let track = SequencerTrack(targetNode: node)
188188
tracks.append(track)
189189
return track

Sources/AudioKitEX/Sequencing/SequencerTrack.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,33 @@ open class SequencerTrack {
8585
/// Sequence on this track
8686
public var sequence = NoteEventSequence() { didSet { updateSequence() } }
8787

88+
/// Add a MIDI noteOn and noteOff to the track
89+
/// - Parameters:
90+
/// - noteNumber: MIDI Note number to add
91+
/// - velocity: Velocity of the note
92+
/// - channel: Channel to place the note on
93+
/// - position: Location in beats of the new note
94+
/// - duration: Duration in beats of the new note
95+
public func add(noteNumber: MIDINoteNumber,
96+
velocity: MIDIVelocity = 127,
97+
channel: MIDIChannel = 0,
98+
position: Double,
99+
duration: Double) {
100+
sequence.add(noteNumber: noteNumber,
101+
velocity: velocity,
102+
channel: channel,
103+
position: position,
104+
duration: duration)
105+
}
106+
107+
/// Add a MIDI event to the track
108+
/// - Parameters:
109+
/// - event: Event to add
110+
/// - position: Location in time in beats to add the event at
111+
public func add(event: MIDIEvent, position: Double) {
112+
sequence.add(event: event, position: position)
113+
}
114+
88115
/// Remove the notes in the track
89116
public func clear() {
90117
sequence = NoteEventSequence()

0 commit comments

Comments
 (0)