Class: MusicTheory::Octave

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Output

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

Constructor Details

#initialize(options = {}) ⇒ Octave

Returns a new instance of Octave.



8
9
10
11
12
13
14
# File 'lib/music_theory/octave.rb', line 8

def initialize(options = {})
  @starting_note    = options[:starting_note] || MusicTheory::Note.new  # Note to start on
  @amount           = options[:amount] || 2                             # Number of octaves to repeat
  @direction        = options[:direction] ||  'asc'                     # Number of seconds per note
  @output_file_name = options[:output_file_name] || 'octave'            # File name to write (without extension)
  build_octave
end

Instance Attribute Details

#all_notesObject

Returns the value of attribute all_notes.



6
7
8
# File 'lib/music_theory/octave.rb', line 6

def all_notes
  @all_notes
end

#amountObject

Returns the value of attribute amount.



6
7
8
# File 'lib/music_theory/octave.rb', line 6

def amount
  @amount
end

#directionObject

Returns the value of attribute direction.



6
7
8
# File 'lib/music_theory/octave.rb', line 6

def direction
  @direction
end

#output_file_nameObject

Returns the value of attribute output_file_name.



6
7
8
# File 'lib/music_theory/octave.rb', line 6

def output_file_name
  @output_file_name
end

#starting_noteObject

Returns the value of attribute starting_note.



6
7
8
# File 'lib/music_theory/octave.rb', line 6

def starting_note
  @starting_note
end

Instance Method Details

#build_octaveObject



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

def build_octave
  @all_notes = [ starting_note ]
  amount.to_i.times do
    new_note = all_notes.last.dup
    if direction == 'asc'
      new_note.frequency = all_notes.last.frequency * 2
    elsif direction == 'desc'
      new_note.frequency = all_notes.last.frequency / 2
    end
    all_notes << new_note unless new_note.frequency > 20000
  end
end

#samplesObject



29
30
31
# File 'lib/music_theory/octave.rb', line 29

def samples
  all_notes.map(&:samples).flatten
end