Module: Banjo

Defined in:
lib/banjo.rb,
lib/banjo/channel.rb,
lib/banjo/version.rb

Defined Under Namespace

Classes: Channel

Constant Summary collapse

VERSION =
"0.0.4"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.beats_per_measureObject

Returns the value of attribute beats_per_measure.



12
13
14
# File 'lib/banjo.rb', line 12

def beats_per_measure
  @beats_per_measure
end

.loop_countObject

Returns the value of attribute loop_count.



17
18
19
# File 'lib/banjo.rb', line 17

def loop_count
  @loop_count
end

.measures_per_loopObject

Returns the value of attribute measures_per_loop.



13
14
15
# File 'lib/banjo.rb', line 13

def measures_per_loop
  @measures_per_loop
end

.tempoObject

Returns the value of attribute tempo.



14
15
16
# File 'lib/banjo.rb', line 14

def tempo
  @tempo
end

.ticks_per_beatObject

Returns the value of attribute ticks_per_beat.



15
16
17
# File 'lib/banjo.rb', line 15

def ticks_per_beat
  @ticks_per_beat
end

.ticks_per_periodObject

Returns the value of attribute ticks_per_period.



16
17
18
# File 'lib/banjo.rb', line 16

def ticks_per_period
  @ticks_per_period
end

Class Method Details

.load_channelsObject



20
21
22
23
# File 'lib/banjo.rb', line 20

def self.load_channels
  load_channels!
rescue Exception
end

.load_channels!Object



25
26
27
28
29
# File 'lib/banjo.rb', line 25

def self.load_channels!
  Dir['./channels/*.rb'].each do |file|
    load file
  end
end

.playObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/banjo.rb', line 31

def self.play
  Banjo.measures_per_loop ||= 1
  Banjo.ticks_per_period = Banjo.ticks_per_beat * Banjo.beats_per_measure * Banjo.measures_per_loop
  tempo_in_ms = 60.0 / Banjo.tempo / Banjo.ticks_per_beat
  puts "Beat every: #{tempo_in_ms}"

  EventMachine.run do
    tick = 0
    self.loop_count = 0

    EM.add_periodic_timer(tempo_in_ms) do
      Banjo.load_channels if tick == 0

      Banjo::Channel.channels.each do |klass|
        channel = klass.new(tick)
        channel.perform
      end

      tick = update_counters(tick)
    end

    puts "Banjo Reactor started..."
  end
end

.update_counters(tick) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/banjo.rb', line 56

def self.update_counters(tick)
  if tick < (ticks_per_period - 1)
    tick += 1
  else
    puts self.loop_count += 1
    tick = 0
  end
  tick
end