Sha256: 4cb56625b31d6dacfb3b5ed1054b54c3bd1ea5a1ae27a1577a6c0395515ef509

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Banjo
  class Channel
    include Banjo::Keys

    attr_accessor :output

    DEFAULT_DURATION = 0.5

    def channel
      0
    end

    def play(note)
      Note.new(self, note)
    end

    def midi_messages
      [0x90, 0x80]
    end

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

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

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

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

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

    def sustain(value = 0)
      output.puts 0xB0, 0x40, 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 hush
      output.puts 0xB0, 0x7B, 0
    end

    def play_note!(note, velocity = 100, duration = DEFAULT_DURATION)
      output.puts(midi_messages[0], note, velocity)
      EventMachine.add_timer(duration, proc { output.puts(midi_messages[1], note, 100) })
    end

    def within(lower, upper, &block)
      if(Banjo.tick >= lower && Banjo.tick <= upper)
        block.call
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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