/
recorder.rb
62 lines (50 loc) · 1.31 KB
/
recorder.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
module JSound
module Midi
module Devices
# A Device that records incoming messages, and the timestamp at which they were received.
class Recorder < Device
# The recorded [message,timestamp] pairs.
#
# Timestamps are in floating point seconds.
attr_reader :messages_with_timestamps
# The recorded messages without timestamps
def messages
@messages_with_timestamps.map{|m,t| m }
end
def initialize(autostart=true)
clear
if autostart
start
else
stop
end
end
# clear any recorded messages
def clear
@messages_with_timestamps = []
end
# start recording
def open
@recording = true
end
alias start open
# stop recording
def close
@recording = false
end
alias stop close
# true if this object is currently recording
def open?
@recording
end
alias recording? open?
def output= device
raise "#{self.class} cannot be assigned an output"
end
def message(message)
@messages_with_timestamps << [message, Time.now.to_f] if recording?
end
end
end
end
end