/
music_player.rb
216 lines (174 loc) · 4.32 KB
/
music_player.rb
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
$:.unshift File.join(File.dirname(__FILE__), '../ext')
require 'thread'
require 'music_player.bundle'
module AudioToolbox
class MusicSequence
attr :tracks
def load(path)
@tracks.lock.synchronize do
load_internal(path)
end
end
end
class MusicTrackCollection
include Enumerable
attr :lock # :nodoc:
def initialize(sequence)
@sequence = sequence
@tracks = []
@lock = Mutex.new
end
def each
0.upto(size-1) { |i| yield self[i] }
end
def new(options=nil)
@lock.synchronize do
track = MusicTrack.send(:new, @sequence, options)
@tracks << track
track
end
end
def [](index)
@lock.synchronize do
@tracks[index] ||= ind_internal(index)
end
end
def delete(track)
@lock.synchronize do
delete_internal(track)
@tracks.delete(track)
track.freeze
end
nil
end
def tempo
@tempo ||= tempo_internal
end
end
class MusicTrack
class << self
private :new
end
def add(time, message)
message.add(time, self)
end
def iterator
MusicEventIterator.new(self)
end
include Enumerable
def each
i = iterator
while i.current?
yield i.event
i.next
end
nil
end
def each_with_time
i = iterator
while i.current?
yield [i.event, i.time]
i.next
end
nil
end
end
class MIDINoteMessage
def ==(msg)
self.class == msg.class &&
channel == msg.channel &&
note == msg.note &&
velocity == msg.velocity &&
release_velocity == msg.release_velocity &&
duration == msg.duration
end
def add(time, track)
track.add_midi_note_message(time, self)
end
end
class MIDIChannelMessage
def ==(msg)
self.class == msg.class &&
status == msg.status &&
data1 == msg.data1 &&
data2 == msg.data2
end
def channel
status ^ mask
end
def mask
raise NotImplementedError, "Subclass responsibility."
end
def add(time, track)
track.add_midi_channel_message(time, self)
end
protected
def required_opts(opts, *keys)
keys.map do |key|
opts[key] or raise ArgumentError, "%s is required." % key.inspect
end
end
end
class MIDIKeyPressureMessage < MIDIChannelMessage
alias :note :data1
alias :pressure :data2
def mask; 0xA0 end
def initialize(opts)
channel, note, pressure = required_opts(opts, :channel, :note, :pressure)
super(:status => mask | channel,
:data1 => note,
:data2 => pressure)
end
end
class MIDIControlChangeMessage < MIDIChannelMessage
alias :number :data1
alias :value :data2
def mask; 0xB0 end
def initialize(opts)
channel, number, value = required_opts(opts, :channel, :number, :value)
super(:status => mask | channel,
:data1 => number,
:data2 => value)
end
end
class MIDIProgramChangeMessage < MIDIChannelMessage
alias :program :data1
def mask; 0xC0 end
def initialize(opts)
channel, program = required_opts(opts, :channel, :program)
super(:status => mask | channel,
:data1 => program)
end
end
class MIDIChannelPressureMessage < MIDIChannelMessage
alias :pressure :data1
def mask; 0xD0 end
def initialize(opts)
channel, pressure = required_opts(opts, :channel, :pressure)
super(:status => mask | channel,
:data1 => pressure)
end
end
class MIDIPitchBendMessage < MIDIChannelMessage
alias :value :data1
def mask; 0xE0 end
def initialize(opts)
channel, value = required_opts(opts, :channel, :value)
super(:status => mask | channel,
:data1 => value)
end
end
class ExtendedTempoEvent
attr :bpm
def add(time, track)
track.add_extended_tempo_event(time, @bpm)
end
def initialize(opts)
@bpm = opts[:bpm]
end
def ==(other)
self.class == other.class &&
bpm == other.bpm
end
end
end