Class: Topaz::Tempo

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/topaz/tempo.rb

Overview

main tempo class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tempo_or_input, options = {}, &event) ⇒ Tempo

Returns a new instance of Tempo.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/topaz/tempo.rb', line 13

def initialize(tempo_or_input, options = {}, &event)
  @paused = false
  @destinations = []      
  @actions = { 
    :start => nil,
    :stop => nil,
    :tick => nil,        
    :midi_clock => Proc.new { @destinations.each { |d| d.send(:midi_clock) if d.respond_to?(:midi_clock) } },
    :stop_when => nil
  }
  
  on_tick(&event)

  if tempo_or_input.kind_of?(Numeric)
    @source = InternalTempo.new(@actions, tempo_or_input)
  else      
    midi_clock_source = tempo_or_input
  end
   
  initialize_midi_io(options[:midi], midi_clock_source)
  raise "You must specify an internal tempo rate or an external tempo source" if @source.nil?
          
  @source.interval = options[:interval] unless options[:interval].nil?      
  
end

Instance Attribute Details

#destinationsObject (readonly)

Returns the value of attribute destinations.



9
10
11
# File 'lib/topaz/tempo.rb', line 9

def destinations
  @destinations
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/topaz/tempo.rb', line 9

def source
  @source
end

Instance Method Details

#add_destination(dest) ⇒ Object

add a destination accepts UniMIDI::Output or array of



133
134
135
136
137
138
# File 'lib/topaz/tempo.rb', line 133

def add_destination(dest)
  dest = [dest].flatten.compact
  dest.each do |d|
    @destinations << MIDISyncOutput.new(d)
  end
end

#on_start(&block) ⇒ Object

pass in a callback that is called when start is called



72
73
74
# File 'lib/topaz/tempo.rb', line 72

def on_start(&block)
  @actions[:start] = block
end

#on_stop(&block) ⇒ Object

pass in a callback that is called when stop is called



77
78
79
# File 'lib/topaz/tempo.rb', line 77

def on_stop(&block)
  @actions[:stop] = block
end

#on_tick(&block) ⇒ Object

pass in a callback which will be fired on each tick



95
96
97
98
99
100
101
102
103
# File 'lib/topaz/tempo.rb', line 95

def on_tick(&block)
  proc = Proc.new do
    unless paused?
      @destinations.each { |d| d.send(:tick) if d.respond_to?(:tick) }
      yield
    end
  end
  @actions[:tick] = proc
end

#pauseObject

pause the clock



53
54
55
# File 'lib/topaz/tempo.rb', line 53

def pause
  @pause = true
end

#paused?Boolean

is this clock paused?

Returns:

  • (Boolean)


63
64
65
# File 'lib/topaz/tempo.rb', line 63

def paused?
  @pause
end

#remove_destination(dest) ⇒ Object

remove a destination accepts UniMIDI::Output or array of



142
143
144
145
146
147
148
149
# File 'lib/topaz/tempo.rb', line 142

def remove_destination(dest)
  dest = [dest].flatten.compact
  dest.each do |output|
    @destinations.each do |sync_output|
      @destinations.delete(sync_output) if sync_output.output == output
    end
  end
end

#start(options = {}) ⇒ Object

this will start the generator

in the case that external midi tempo is being used, this will wait for a start or clock message



110
111
112
113
114
115
# File 'lib/topaz/tempo.rb', line 110

def start(options = {})
  @start_time = Time.now
  @destinations.each { |dest| dest.start }
  @source.start(options)
  @actions[:start].call unless @actions[:start].nil?
end

#stop(options = {}) ⇒ Object

this will stop tempo



118
119
120
121
122
123
# File 'lib/topaz/tempo.rb', line 118

def stop(options = {})
  @destinations.each { |dest| dest.stop }
  @source.stop(options)
  @actions[:stop].call unless @actions[:stop].nil?
  @start_time = nil
end

#stop_when(&block) ⇒ Object

pass in a callback which will stop the clock if it evaluates to true



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/topaz/tempo.rb', line 82

def stop_when(&block)
  proc = Proc.new do
    if yield
      stop
      true
    else
      false
    end 
  end
  @actions[:stop_when] = proc
end

#tempo=(val) ⇒ Object

this will change the tempo

be warned, in the case that external midi tempo is being used, this will switch to internal tempo at the desired rate



44
45
46
47
48
49
50
# File 'lib/topaz/tempo.rb', line 44

def tempo=(val)
  if @source.respond_to?(:tempo=)
    @source.tempo = val
  else
    @source = InternalTempo.new(tempo, @action)
  end 
end

#timeObject Also known as: time_since_start

seconds since start was called



126
127
128
# File 'lib/topaz/tempo.rb', line 126

def time
  @start_time.nil? ? nil : (Time.now - @start_time).to_f
end

#toggle_pauseObject



67
68
69
# File 'lib/topaz/tempo.rb', line 67

def toggle_pause
  paused? ? unpause : pause      
end

#unpauseObject

unpause the clock



58
59
60
# File 'lib/topaz/tempo.rb', line 58

def unpause
  @pause = false
end