Class: Vizcore::Audio::MidiInput

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device: nil, backend: nil, poll_interval: DEFAULT_POLL_INTERVAL) ⇒ MidiInput

Returns a new instance of MidiInput.

Parameters:

  • device (Integer, String, Symbol, nil) (defaults to: nil)
  • backend (Module, nil) (defaults to: nil)
  • poll_interval (Float) (defaults to: DEFAULT_POLL_INTERVAL)


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_errorObject (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.

Parameters:

  • backend (Module, nil) (defaults to: nil)

Returns:

  • (Array<Hash>)

    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>

Parameters:

  • max (Integer, nil) (defaults to: nil)

Returns:



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

Returns:

  • (Boolean)


92
93
94
# File 'lib/vizcore/audio/midi_input.rb', line 92

def running?
  @running
end

#start {|event| ... } ⇒ Vizcore::Audio::MidiInput

Yield Parameters:

Returns:



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

#stopVizcore::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