Class: Niki::Instrument

Inherits:
Object
  • Object
show all
Includes:
Chords
Defined in:
lib/niki/instrument.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Chords

#silence

Constructor Details

#initialize(name, song, &block) ⇒ Instrument

Returns a new instance of Instrument.



7
8
9
10
11
12
13
14
15
16
# File 'lib/niki/instrument.rb', line 7

def initialize(name, song, &block)
  @name = name
  @song = song
  @channel_number = nil
  @macros = {}

  self.instance_exec(&block)

  Part.send :include, self.to_module
end

Instance Attribute Details

#channel_numberObject (readonly)

Returns the value of attribute channel_number.



3
4
5
# File 'lib/niki/instrument.rb', line 3

def channel_number
  @channel_number
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/niki/instrument.rb', line 3

def name
  @name
end

Instance Method Details

#channel(number) ⇒ Object



18
19
20
# File 'lib/niki/instrument.rb', line 18

def channel(number)
  @channel_number = number - 1
end

#noteObject



22
23
24
# File 'lib/niki/instrument.rb', line 22

def note
  @macros
end

#to_moduleObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/niki/instrument.rb', line 26

def to_module
  instrument_name = @name
  macros = @macros

  Module.new do
    define_method instrument_name do |*args|
      note     = args.shift
      duration = args.shift || 1
      options  = args.shift || {}

      velocity = options[:v] || 100

      if note.is_a?(Hash) && note[:from]
        copy_from_part(note[:from], instrument_name)
        return
      end

      note = [note].flatten.map do |n|
        if n.is_a?(Fixnum)
          n
        else
          # Try to fetch the note from the symbol
          macros[n]
        end
      end

      note.push options[:base] if options[:base]

      register_note(instrument_name, note, duration, velocity)
    end
  end
end