Class: Muse::Song::Bar

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ Bar

Returns a new instance of Bar.



64
65
66
67
68
69
70
71
# File 'lib/muse.rb', line 64

def initialize(id, options={})
  @bpm = options[:bpm] || 120
  @beats = (options[:b] || 1).to_f
  @envelope = options[:e] || 'default'
  @harmonic = options[:h] || 'default'
  @volume = options[:v] || 5
  @stream = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/muse.rb', line 130

def method_missing(name, *args, &block)
  name = name.to_s
  if name.start_with? *NOTES   
    if name.split('_').length > 1
      notes = name.split('_')
      add_to_stream chord(notes, args[0])
    else
      octave = name[name.length-1].to_i
      note = octave > 0 ? name.chop : name
      octave = 3 if octave == 0
      add_to_stream note_data(note, octave, args[0])
    end
  end
end

Instance Attribute Details

#beatsObject (readonly)

Returns the value of attribute beats.



60
61
62
# File 'lib/muse.rb', line 60

def beats
  @beats
end

#bpmObject (readonly)

Returns the value of attribute bpm.



60
61
62
# File 'lib/muse.rb', line 60

def bpm
  @bpm
end

#envelopeObject (readonly)

Returns the value of attribute envelope.



60
61
62
# File 'lib/muse.rb', line 60

def envelope
  @envelope
end

#harmonicObject (readonly)

Returns the value of attribute harmonic.



60
61
62
# File 'lib/muse.rb', line 60

def harmonic
  @harmonic
end

#streamObject

Returns the value of attribute stream.



61
62
63
# File 'lib/muse.rb', line 61

def stream
  @stream
end

Instance Method Details

#add_to_stream(str) ⇒ Object



126
127
128
# File 'lib/muse.rb', line 126

def add_to_stream(str)
  @stream += str
end

#chord(notes, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/muse.rb', line 81

def chord(notes,options={})
  puts "chord with #{notes}"
  triad =[]
  notes.each do |name|
    if name.start_with? *NOTES
      octave = name[name.length-1].to_i
      note = octave > 0 ? name.chop : name
      octave = 3 if octave == 0
      triad << note_data(note, octave, options)
    end
  end
  triad.transpose.map {|x| x.transpose.map {|y| y.reduce(:+)}}   
end

#frequency_of(step) ⇒ Object



77
78
79
# File 'lib/muse.rb', line 77

def frequency_of(step)
  440.0*(2**(step.to_f/12.0))
end

#note_data(note, octave = 3, options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/muse.rb', line 95

def note_data(note, octave=3, options={})
  stream = []
  if options
    beats  = options[:b].nil?  ? (@beats || 1) : options[:b].to_f
    volume = (options[:v].nil? ? (@volume.to_i || 5) : options[:v].to_i) * 1000
    envelope = options[:a].nil? ? @envelope : 'default'
    harmonic = options[:h].nil? ? @harmonic : 'default'
  else
    beats, volume, envelope, harmonic = (@beats || 1), 5000, @envelope || 'default', @harmonic || 'default'
  end
  puts "[#{note}] -> b:#{beats}, o:#{octave}, e:#{envelope}, h:#{harmonic}"
  duration = ((60 * WavHeader::SAMPLE_RATE * beats)/@bpm)/WavHeader::SAMPLE_RATE.to_f
  note_frequency = note + octave.to_s
  unless note == '_'
    freq = frequency_of(FREQUENCIES[note_frequency.to_sym])
  else
    freq = 0
  end      
  (0.0..duration.to_f).step(1.0/WavHeader::SAMPLE_RATE) do |i|
    env = Envelope.send(envelope.to_sym,i, duration)
    har = Harmonic.send(harmonic.to_sym, freq * i)
    x = (env * volume * har).to_i
    stream << [x,x]
  end  
  return stream           
end

#notes(&block) ⇒ Object



73
74
75
# File 'lib/muse.rb', line 73

def notes(&block)
  instance_eval &block
end

#truncate_stream_by(num) ⇒ Object



122
123
124
# File 'lib/muse.rb', line 122

def truncate_stream_by(num)
  num.times {@stream.pop}
end