Class: Banjo::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/banjo/channel.rb

Constant Summary collapse

DEFAULT_DURATION =
0.5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tick) ⇒ Channel

Returns a new instance of Channel.



18
19
20
21
# File 'lib/banjo/channel.rb', line 18

def initialize(tick)
  @output = UniMIDI::Output.all[channel]
  @tick   = tick
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



3
4
5
# File 'lib/banjo/channel.rb', line 3

def output
  @output
end

#tickObject

Returns the value of attribute tick.



3
4
5
# File 'lib/banjo/channel.rb', line 3

def tick
  @tick
end

Class Method Details

.channelsObject



10
11
12
# File 'lib/banjo/channel.rb', line 10

def self.channels
  @channels ||= []
end

.inherited(child) ⇒ Object



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

def self.inherited(child)
  channels << child
end

Instance Method Details

#arpeggio(notes, velocity = 50) ⇒ Object



49
50
51
52
53
54
# File 'lib/banjo/channel.rb', line 49

def arpeggio(notes, velocity = 50)
  step = (1.0 * Banjo.ticks_per_period / notes.size).round
  notes.each_with_index do |note, index|
    tick_note((index * step), note, velocity)
  end
end

#channelObject



6
7
8
# File 'lib/banjo/channel.rb', line 6

def channel
  0
end

#mod_note(mod, note, offset = 0, velocity = 50) ⇒ Object



33
34
35
# File 'lib/banjo/channel.rb', line 33

def mod_note(mod, note, offset = 0, velocity = 50)
  play_note(note, velocity, DEFAULT_DURATION) if ((tick + offset) % mod == 0)
end

#modulation(value = 0) ⇒ Object



41
42
43
# File 'lib/banjo/channel.rb', line 41

def modulation(value = 0)
  output.puts 0xB0, 0x01, value
end

#pitch(value = 63) ⇒ Object



45
46
47
# File 'lib/banjo/channel.rb', line 45

def pitch(value = 63)
  output.puts 0xE0, 0, value
end

#play_note(note, velocity = 50, duration = DEFAULT_DURATION) ⇒ Object



37
38
39
# File 'lib/banjo/channel.rb', line 37

def play_note(note, velocity = 50, duration = DEFAULT_DURATION)
  EM.defer { play_note!(note, velocity, duration) }
end

#play_note!(note, velocity = 50, duration = DEFAULT_DURATION) ⇒ Object



60
61
62
63
64
# File 'lib/banjo/channel.rb', line 60

def play_note!(note, velocity = 50, duration = DEFAULT_DURATION)
  output.puts(0x80, note, velocity)
  sleep(duration)
  output.puts(0x90, note, velocity)
end

#stfuObject



56
57
58
# File 'lib/banjo/channel.rb', line 56

def stfu
  output.puts 0xB0, 0x7B, 0
end

#tick_note(tick, note, velocity = 50, duration = DEFAULT_DURATION) ⇒ Object



23
24
25
# File 'lib/banjo/channel.rb', line 23

def tick_note(tick, note, velocity = 50, duration = DEFAULT_DURATION)
  play_note(note, velocity, duration) if tick == self.tick
end

#tick_notes(notes, velocity = 50, duration = DEFAULT_DURATION) ⇒ Object



27
28
29
30
31
# File 'lib/banjo/channel.rb', line 27

def tick_notes(notes, velocity = 50, duration = DEFAULT_DURATION)
  notes.each do |tick, note|
    tick_note(tick, note, velocity, duration)
  end
end