Sha256: 042b78bade7aba87d1803c2801bc45301841fa759762242e706213a02f686325

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Banjo
  class Channel
    attr_accessor :output, :tick
    DEFAULT_DURATION = 0.5

    def channel
      0
    end

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

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

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

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

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

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

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

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

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

    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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
banjo-0.0.3 lib/banjo/channel.rb