lib/xi/midi/stream.rb in xi-midi-0.1.0 vs lib/xi/midi/stream.rb in xi-midi-0.1.3
- old
+ new
@@ -1,26 +1,25 @@
require 'xi/stream'
require 'xi/midi/proxy'
module Xi::MIDI
- class Stream < ::Stream
+ class Stream < Xi::Stream
attr_accessor :device, :channel
- def initialize(clock, device: 0, channel: 0)
- super(clock)
+ def initialize(name, clock, device: 0, channel: 0)
+ super
@device = device
@channel = channel
@playing_notes = {}
- midi.open(device)
+ midi.open(device) unless midi.open?(device)
at_exit { kill_playing_notes }
end
def set(**params)
- params[:gate] = :midinote
- super(params)
+ super(gate: :midinote, **params)
end
def stop
kill_playing_notes
super
@@ -35,50 +34,63 @@
end
@playing_notes.clear
end
end
- def do_gate_on_change(so_ids)
+ def do_gate_on_change(changes)
+ debug "Gate on change: #{changes}"
+
channel = Array(@state[:channel] || 0)
midinote = Array(@state[:midinote] || 60)
velocity = Array(@state[:velocity] || 127)
- so_ids.each.with_index do |so_id, i|
- channel_i = channel[i % channel.size]
- midinote_i = midinote[i % midinote.size]
- velocity_i = velocity[i % velocity.size]
+ changes.each do |change|
+ change.fetch(:so_ids).each.with_index do |so_id, i|
+ channel_i = channel[i % channel.size]
+ midinote_i = midinote[i % midinote.size]
+ velocity_i = velocity[i % velocity.size]
- logger.info "MIDI Note on: #{[channel_i, midinote_i, velocity_i]}"
- midi.note_on(@device, channel_i, midinote_i, velocity_i)
+ debug "MIDI Note on: #{[channel_i, midinote_i, velocity_i]}"
+ midi.note_on(@device, channel_i, midinote_i, velocity_i)
- @playing_notes[so_id] = {channel: channel_i, midinote: midinote_i}
+ @playing_notes[so_id] = {channel: channel_i, midinote: midinote_i}
+ end
end
end
- def do_gate_off_change(so_ids)
- so_ids.each do |so_id|
- note = @playing_notes.delete(so_id)
- if note
- logger.info "MIDI Note off: #{[note[:channel], note[:midinote]]}"
- midi.note_off(@device, note[:channel], note[:midinote])
+ def do_gate_off_change(changes)
+ debug "Gate off change: #{changes}"
+
+ changes.each do |change|
+ change.fetch(:so_ids).each do |so_id|
+ note = @playing_notes.delete(so_id)
+ if note
+ debug "MIDI Note off: #{[note[:channel], note[:midinote]]}"
+ midi.note_off(@device, note[:channel], note[:midinote])
+ end
end
end
end
def do_state_change
- logger.info changed_state
- changed_state.each do |p, v|
+ debug "State change: #{changed_state}"
+
+ changed_state.each do |p, vs|
cc_id = cc_parameters[p]
- midi.cc(@device, channel, cc_id, v) if cc_id
+ Array(vs).each { |v| midi.cc(@device, channel, cc_id, v.to_i) } if cc_id
end
end
# @override
def cc_parameters
{}
end
def midi
Proxy.instance
+ end
+
+ def latency_sec
+ 0.01
end
end
end