Class: Zgomot::Midi::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/zgomot/midi/dispatcher.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clockObject (readonly)

Returns the value of attribute clock.



13
14
15
# File 'lib/zgomot/midi/dispatcher.rb', line 13

def clock
  @clock
end

.last_timeObject (readonly)

Returns the value of attribute last_time.



13
14
15
# File 'lib/zgomot/midi/dispatcher.rb', line 13

def last_time
  @last_time
end

.playingObject (readonly)

Returns the value of attribute playing.



13
14
15
# File 'lib/zgomot/midi/dispatcher.rb', line 13

def playing
  @playing
end

.qdispatchObject (readonly)

Returns the value of attribute qdispatch.



13
14
15
# File 'lib/zgomot/midi/dispatcher.rb', line 13

def qdispatch
  @qdispatch
end

.qmutexObject (readonly)

Returns the value of attribute qmutex.



13
14
15
# File 'lib/zgomot/midi/dispatcher.rb', line 13

def qmutex
  @qmutex
end

.queueObject (readonly)

Returns the value of attribute queue.



13
14
15
# File 'lib/zgomot/midi/dispatcher.rb', line 13

def queue
  @queue
end

.resolutionObject (readonly)

Returns the value of attribute resolution.



13
14
15
# File 'lib/zgomot/midi/dispatcher.rb', line 13

def resolution
  @resolution
end

.threadObject (readonly)

Returns the value of attribute thread.



13
14
15
# File 'lib/zgomot/midi/dispatcher.rb', line 13

def thread
  @thread
end

.tickObject (readonly)

Returns the value of attribute tick.



13
14
15
# File 'lib/zgomot/midi/dispatcher.rb', line 13

def tick
  @tick
end

Class Method Details

.clkObject



15
16
17
# File 'lib/zgomot/midi/dispatcher.rb', line 15

def clk
  clock.to_s
end

.dequeue(time) ⇒ Object



33
34
35
36
37
# File 'lib/zgomot/midi/dispatcher.rb', line 33

def dequeue(time)
  qmutex.synchronize do
    queue.partition{|n| n.play_at <= time}
  end
end

.dispatch(now) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/zgomot/midi/dispatcher.rb', line 39

def dispatch(now)
  qdispatch.synchronize do
    ready, @queue = dequeue(now)
    notes_off(now)
    notes_on(ready)
  end
end

.done?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/zgomot/midi/dispatcher.rb', line 23

def done?
  qdispatch.synchronize{queue.empty? and playing.empty?}
end

.enqueue(ch) ⇒ Object



27
28
29
30
31
# File 'lib/zgomot/midi/dispatcher.rb', line 27

def enqueue(ch)
  qmutex.synchronize do
    @queue += ch.pattern.map{|p| p.to_midi}.flatten.compact.select{|n| not n.pitch_class.eql?(:R)}
  end
end

.flushObject



19
20
21
# File 'lib/zgomot/midi/dispatcher.rb', line 19

def flush
  @queue.clear
end

.notes_off(time) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/zgomot/midi/dispatcher.rb', line 55

def notes_off(time)
  turn_off, @playing = playing.partition{|n| (n.play_at+n.length_to_sec) <= time}
  turn_off.each do |n|
    Zgomot.logger.info "NOTE OFF:#{n.channel} : #{n.to_s} : #{n.time.to_s} : #{clock.current_time.to_s}"
    Zgomot::Drivers::Mgr.note_off(n.midi, n.channel, (127*n.velocity).to_i)
  end
end

.notes_on(notes) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/zgomot/midi/dispatcher.rb', line 47

def notes_on(notes)
  notes.each do |n|
    Zgomot.logger.info "NOTE ON: #{n.channel} : #{n.to_s} : #{n.time.to_s} : #{clock.current_time.to_s}"
    Zgomot::Drivers::Mgr.note_on(n.midi, n.channel, (127*n.velocity).to_i)
  end
  @playing += notes
end