Class: MTK::IO::UniMIDIInput
- Defined in:
- lib/mtk/io/unimidi_input.rb
Overview
Note:
This class is optional and only available if you require ‘mtk/midi/unimidi_input’. It depends on the ‘unimidi’ gem.
Provides realtime MIDI input for MRI/YARV Ruby via the unimidi gem.
Instance Attribute Summary collapse
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#recording ⇒ Object
readonly
Returns the value of attribute recording.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(input_device, options = {}) ⇒ UniMIDIInput
constructor
A new instance of UniMIDIInput.
- #record(options = {}) ⇒ Object
- #stop ⇒ Object
- #to_timeline(options = {}) ⇒ Object
Methods inherited from MIDIInput
available_input_types, find_by_name, input_types_by_device, #name, open
Constructor Details
#initialize(input_device, options = {}) ⇒ UniMIDIInput
Returns a new instance of UniMIDIInput.
25 26 27 28 |
# File 'lib/mtk/io/unimidi_input.rb', line 25 def initialize(input_device, ={}) super @open_time = Time.now.to_f end |
Instance Attribute Details
#device ⇒ Object (readonly)
Returns the value of attribute device.
23 24 25 |
# File 'lib/mtk/io/unimidi_input.rb', line 23 def device @device end |
#recording ⇒ Object (readonly)
Returns the value of attribute recording.
23 24 25 |
# File 'lib/mtk/io/unimidi_input.rb', line 23 def recording @recording end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
23 24 25 |
# File 'lib/mtk/io/unimidi_input.rb', line 23 def thread @thread end |
Class Method Details
.devices ⇒ Object
14 15 16 |
# File 'lib/mtk/io/unimidi_input.rb', line 14 def self.devices @devices ||= ::UniMIDI::Input.all.reject{|d| d.name.strip.empty? } end |
.devices_by_name ⇒ Object
18 19 20 |
# File 'lib/mtk/io/unimidi_input.rb', line 18 def self.devices_by_name @devices_by_name ||= devices.each_with_object( Hash.new ){|device,hash| hash[device.name] = device } end |
Instance Method Details
#record(options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mtk/io/unimidi_input.rb', line 30 def record(={}) @recording = [] unless [:append] and @recording monitor = [:monitor] stop @thread = Thread.new do @start_time = Time.now.to_f loop do @device.gets.each do |data| puts data if monitor record_raw_data data end sleep 0.001 end end time_limit = [:time_limit] if time_limit puts "Blocking current thread for #{time_limit} seconds to record MIDI input." @thread.join(time_limit) end end |
#stop ⇒ Object
53 54 55 |
# File 'lib/mtk/io/unimidi_input.rb', line 53 def stop Thread.kill @thread if @thread end |
#to_timeline(options = {}) ⇒ Object
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 |
# File 'lib/mtk/io/unimidi_input.rb', line 57 def to_timeline(={}) return nil if not @recording bpm = .fetch :bmp, 120 beats_per_second = bpm.to_f/60 timeline = MTK::Events::Timeline.new note_ons = {} start = nil @recording.each do |, time| start ||= time time -= start time /= beats_per_second if .is_a? MTK::Events::Event timeline.add time, unless .type == :unknown else = .type = :note_off if == :note_on and .velocity == 0 # TODO: this will need to be made more robust when we support off velocities case when :note_on pitch = .pitch note_ons[pitch] = [,time] when :note_off pitch = .pitch if note_ons.has_key? pitch note_on, start_time = note_ons.delete(pitch) duration = time - start_time note = MTK::Events::Note.from_midi pitch, note_on.velocity, duration timeline.add time,note end end end end timeline.quantize! [:quantize] if .key? :quantize timeline.shift_to! [:shift_to] if .key? :shift_to timeline end |