Class: MusicTheory::Chord

Inherits:
Object
  • Object
show all
Includes:
Output
Defined in:
lib/music_theory/chord.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Output

#buffer_format, #format, #output_track, #play, #sample_rate

Constructor Details

#initialize(third, options = {}) ⇒ Chord

Returns a new instance of Chord.



9
10
11
12
13
# File 'lib/music_theory/chord.rb', line 9

def initialize(third, options = {})
  @duration         = options[:duration] || 2.0
  @third            = third
  @output_file_name = options[:output_file_name] || 'chord' # File name to write (without extension)
end

Instance Attribute Details

#all_notesObject

Returns the value of attribute all_notes.



7
8
9
# File 'lib/music_theory/chord.rb', line 7

def all_notes
  @all_notes
end

#durationObject

Returns the value of attribute duration.



7
8
9
# File 'lib/music_theory/chord.rb', line 7

def duration
  @duration
end

#output_file_nameObject

Returns the value of attribute output_file_name.



7
8
9
# File 'lib/music_theory/chord.rb', line 7

def output_file_name
  @output_file_name
end

#thirdObject

Returns the value of attribute third.



7
8
9
# File 'lib/music_theory/chord.rb', line 7

def third
  @third
end

Instance Method Details

#flatten_thirdObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/music_theory/chord.rb', line 15

def flatten_third
   third.all_notes.each {|note| note.duration = duration}
   new_samples = []
   sample_count = third.all_notes.first.samples.count
   third.samples.in_groups_of(sample_count).each do |group|
     group.each_with_index do |value, i|
       new_samples[i] ||= 0
       new_samples[i] +=  value
     end
   end
   normalize_samples(new_samples)
end

#normalize_samples(new_samples) ⇒ Object



28
29
30
31
32
# File 'lib/music_theory/chord.rb', line 28

def normalize_samples(new_samples)
  max = new_samples.map {|s| s.abs }.max
  multiplier = 1.0 / max
  new_samples.map!{ |s| multiplier * s }
end

#samplesObject



34
35
36
# File 'lib/music_theory/chord.rb', line 34

def samples
  flatten_third
end