forked from AudioKit/AudioKitEX
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSequence.swift
More file actions
152 lines (133 loc) · 5.27 KB
/
Sequence.swift
File metadata and controls
152 lines (133 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright AudioKit. All Rights Reserved. Revision History at http://github.com/AudioKit/AudioKit/
import CAudioKitEX
import Foundation
import AudioKit
#if !os(tvOS)
extension SequenceNote: Equatable {
/// Equality check
/// - Parameters:
/// - lhs: Left hand side
/// - rhs: Right hand side
/// - Returns: True if equal
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.noteOn == rhs.noteOn
&& lhs.noteOff == rhs.noteOff
}
}
extension SequenceEvent: Equatable {
/// Equality check
/// - Parameters:
/// - lhs: Left hand side
/// - rhs: Right hand side
/// - Returns: True if equal
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.status == rhs.status
&& lhs.data1 == rhs.data1
&& lhs.data2 == rhs.data2
&& lhs.beat == rhs.beat
}
}
extension Array where Element == SequenceEvent {
/// Sort an array of SequenceEvents by earliest beat time (Double)
/// - Parameters: none
/// - Returns: [SequenceEvent]
public func beatTimeOrdered() -> [SequenceEvent] {
return self.sorted(by: { (event1:SequenceEvent, event2:SequenceEvent) -> Bool in
let event1Beat = event1.beat
let event2Beat = event2.beat
if event1Beat == event2Beat {
if isNoteOn(event1.status) && isNoteOff(event2.status) { return false }
if isNoteOff(event1.status) && isNoteOn(event2.status) { return true }
}
return event1Beat < event2Beat
})
}
private func isNoteOn(_ statusByte: UInt8) -> Bool {
return statusByte & noteOnByte == noteOnByte
}
private func isNoteOff(_ statusByte: UInt8) -> Bool {
return statusByte & noteOffByte == noteOffByte
}
}
/// A value type for sequences.
public struct NoteEventSequence: Equatable {
/// Array of sequence notes
public var notes: [SequenceNote]
/// Array of sequence events
public var events: [SequenceEvent]
private(set) var totalDuration: Double = 0.0
/// Initialize with notes and events
/// - Parameters:
/// - notes: Array of sequence notes
/// - events: Array of sequence events
public init(notes: [SequenceNote] = [], events: [SequenceEvent] = [], totalDuration: Double = 0.0) {
self.notes = notes
self.events = events
self.totalDuration = totalDuration
}
/// Add a note
/// - Parameters:
/// - noteNumber: MIDI Note Number
/// - velocity: MIDI Velocity
/// - channel: MIDI Channel
/// - position: Position or time to start
/// - duration: Length of time until note is stopped
public mutating func add(noteNumber: MIDINoteNumber,
velocity: MIDIVelocity = 127,
channel: MIDIChannel = 0,
position: Double,
duration: Double) {
totalDuration = max(totalDuration, position + duration) // Compare latest note position and duration to current totalDuration.
var newNote = SequenceNote()
newNote.noteOn.status = noteOnByte
newNote.noteOn.data1 = noteNumber
newNote.noteOn.data2 = velocity
newNote.noteOn.beat = position
newNote.noteOff.status = noteOffByte
newNote.noteOff.data1 = noteNumber
newNote.noteOff.data2 = velocity
newNote.noteOff.beat = position + duration
notes.append(newNote)
}
/// Remove event that occurs at a specific time
/// - Parameter position: Time of event
public mutating func removeEvent(at position: Double) {
events.removeAll { $0.beat == position }
}
/// Remove note that occurs at a specific time
/// - Parameter position: Time of the note
public mutating func removeNote(at position: Double) {
notes.removeAll { $0.noteOn.beat == position }
}
/// Remove all occurrences of a certain MIDI Note nUmber
/// - Parameter noteNumber: Note to remove
public mutating func removeAllInstancesOf(noteNumber: MIDINoteNumber) {
notes.removeAll { $0.noteOn.data1 == noteNumber }
}
/// Add MIDI data to the track as an event
public mutating func add(status: MIDIStatus, data1: MIDIByte, data2: MIDIByte, position: Double) {
events.append(SequenceEvent(status: status.byte, data1: data1, data2: data2, beat: position))
}
/// Add a MIDI event to the track at a specific position
public mutating func add(event: MIDIEvent, position: Double) {
if let status = event.status, event.data.count > 2 {
add(status: status, data1: event.data[1], data2: event.data[2], position: position)
}
}
/// All MIDI events ordered by earliest beat time
/// - Returns: Array of SequenceEvents
public func beatTimeOrderedEvents() -> [SequenceEvent] {
/// Get all SequenceEvents
var allEvents: [SequenceEvent] = []
allEvents.append(contentsOf: events)
/// Get all SequenceEvents from Notes
var noteEvents: [SequenceEvent] = []
for note in notes {
noteEvents.append(note.noteOn)
noteEvents.append(note.noteOff)
}
allEvents.append(contentsOf: noteEvents)
return allEvents.beatTimeOrdered()
}
}
#endif