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_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.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vizcore/audio/midi_input.rb', line 55 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.
67 68 69 |
# File 'lib/vizcore/audio/midi_input.rb', line 67 def last_error @last_error end |
Class Method Details
.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>
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/vizcore/audio/midi_input.rb', line 98 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
92 93 94 |
# File 'lib/vizcore/audio/midi_input.rb', line 92 def running? @running end |
#start {|event| ... } ⇒ Vizcore::Audio::MidiInput
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vizcore/audio/midi_input.rb', line 71 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
84 85 86 87 88 89 |
# File 'lib/vizcore/audio/midi_input.rb', line 84 def stop @running = false join_thread close_input self end |