Class: Vizcore::Audio::MidiInput
- Inherits:
-
Object
- Object
- Vizcore::Audio::MidiInput
- Defined in:
- lib/vizcore/audio/midi_input.rb
Overview
Poll-based MIDI input wrapper using the ‘unimidi` backend.
Defined Under Namespace
Classes: Event
Constant Summary collapse
- DEFAULT_POLL_INTERVAL =
Poll interval for empty reads in seconds.
0.01
Instance Attribute Summary collapse
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
Class Method Summary collapse
-
.available? ⇒ Boolean
True when the optional UniMIDI backend can be loaded.
-
.available_devices(backend: nil) ⇒ Array<Hash>
Available MIDI devices.
Instance Method Summary collapse
-
#initialize(device: nil, backend: nil, poll_interval: DEFAULT_POLL_INTERVAL) ⇒ MidiInput
constructor
A new instance of MidiInput.
- #poll(max = nil) ⇒ Array<Vizcore::Audio::MidiInput::Event>
- #running? ⇒ Boolean
- #start {|event| ... } ⇒ Vizcore::Audio::MidiInput
- #stop ⇒ Vizcore::Audio::MidiInput
Constructor Details
#initialize(device: nil, backend: nil, poll_interval: DEFAULT_POLL_INTERVAL) ⇒ MidiInput
Returns a new instance of MidiInput.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vizcore/audio/midi_input.rb', line 60 def initialize(device: nil, backend: nil, poll_interval: DEFAULT_POLL_INTERVAL) @device = device @backend = backend || self.class.send(:load_backend) @poll_interval = Float(poll_interval) @running = false @thread = nil @input = nil @events = Queue.new @callback = nil @last_error = nil end |
Instance Attribute Details
#last_error ⇒ Object (readonly)
Returns the value of attribute last_error.
72 73 74 |
# File 'lib/vizcore/audio/midi_input.rb', line 72 def last_error @last_error end |
Class Method Details
.available? ⇒ Boolean
Returns true when the optional UniMIDI backend can be loaded.
33 34 35 |
# File 'lib/vizcore/audio/midi_input.rb', line 33 def available? !load_backend.nil? end |
.available_devices(backend: nil) ⇒ Array<Hash>
Returns available MIDI devices.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/vizcore/audio/midi_input.rb', line 18 def available_devices(backend: nil) midi_backend = backend || load_backend return [] unless midi_backend midi_backend::Input.all.each_with_index.map do |device, index| { id: extract_device_id(device, index), name: extract_device_name(device) } end rescue StandardError [] end |
Instance Method Details
#poll(max = nil) ⇒ Array<Vizcore::Audio::MidiInput::Event>
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/vizcore/audio/midi_input.rb', line 103 def poll(max = nil) limit = max ? Integer(max) : nil result = [] while limit.nil? || result.length < limit begin result << @events.pop(true) rescue ThreadError break end end result end |
#running? ⇒ Boolean
97 98 99 |
# File 'lib/vizcore/audio/midi_input.rb', line 97 def running? @running end |
#start {|event| ... } ⇒ Vizcore::Audio::MidiInput
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/vizcore/audio/midi_input.rb', line 76 def start(&callback) return self if running? @callback = callback if block_given? @input = open_input return self unless @input @running = true @thread = Thread.new { consume_loop } self end |
#stop ⇒ Vizcore::Audio::MidiInput
89 90 91 92 93 94 |
# File 'lib/vizcore/audio/midi_input.rb', line 89 def stop @running = false join_thread close_input self end |