Class: MusicTheory::Scale

Inherits:
Object
  • Object
show all
Includes:
Output, ScaleSteps
Defined in:
lib/music_theory/scale.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ScaleSteps

#i, #ii, #iii, #iv, #v, #vi, #vii, #viii

Methods included from Output

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

Constructor Details

#initialize(scale_type = :major, options = {}) ⇒ Scale

Returns a new instance of Scale.



10
11
12
13
14
15
16
17
18
19
# File 'lib/music_theory/scale.rb', line 10

def initialize(scale_type  = :major, options = {})
  @scale_type       = scale_type
  @distort          = options[:distort] || false
  @duration         = options[:duration] || 0.5
  @frequency        = options[:frequency] || 220.0
  @starting_note    = create_new_note(frequency)     # Note to start on
  @output_file_name = options[:output_file_name] || 'scale' # File name to write (without extension)
  set_all_notes
  set_scale_notes
end

Instance Attribute Details

#all_notesObject

Returns the value of attribute all_notes.



8
9
10
# File 'lib/music_theory/scale.rb', line 8

def all_notes
  @all_notes
end

#distortObject

Returns the value of attribute distort.



8
9
10
# File 'lib/music_theory/scale.rb', line 8

def distort
  @distort
end

#durationObject

Returns the value of attribute duration.



8
9
10
# File 'lib/music_theory/scale.rb', line 8

def duration
  @duration
end

#frequencyObject

Returns the value of attribute frequency.



8
9
10
# File 'lib/music_theory/scale.rb', line 8

def frequency
  @frequency
end

#output_file_nameObject

Returns the value of attribute output_file_name.



8
9
10
# File 'lib/music_theory/scale.rb', line 8

def output_file_name
  @output_file_name
end

#scale_notesObject

Returns the value of attribute scale_notes.



8
9
10
# File 'lib/music_theory/scale.rb', line 8

def scale_notes
  @scale_notes
end

#scale_typeObject

Returns the value of attribute scale_type.



8
9
10
# File 'lib/music_theory/scale.rb', line 8

def scale_type
  @scale_type
end

#starting_noteObject

Returns the value of attribute starting_note.



8
9
10
# File 'lib/music_theory/scale.rb', line 8

def starting_note
  @starting_note
end

Instance Method Details

#arpeggioObject



58
59
60
# File 'lib/music_theory/scale.rb', line 58

def arpeggio
  arpeggio ||= third.arpeggio
end

#chordObject



62
63
64
# File 'lib/music_theory/scale.rb', line 62

def chord
  chord ||= third.chord
end

#create_new_note(note_frequency) ⇒ Object



38
39
40
# File 'lib/music_theory/scale.rb', line 38

def create_new_note(note_frequency)
  MusicTheory::Note.new( frequency: note_frequency, duration: duration, distort: distort)
end

#modeObject



25
26
27
# File 'lib/music_theory/scale.rb', line 25

def mode
   MusicTheory::Modes.send(scale_type)
end

#samplesObject



50
51
52
# File 'lib/music_theory/scale.rb', line 50

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

#set_all_notesObject



42
43
44
45
46
47
48
# File 'lib/music_theory/scale.rb', line 42

def set_all_notes
  @all_notes = [@starting_note]
  12.times do
    new_note = create_new_note(all_notes.last.frequency * twelfth_root_of_two)
    all_notes << new_note
  end
end

#set_scale_notesObject



29
30
31
32
33
34
35
36
# File 'lib/music_theory/scale.rb', line 29

def set_scale_notes
  @scale_notes = [all_notes.first]
  note_index = 0
  mode.each do |interval|
    note_index +=  interval
    scale_notes << all_notes[note_index]
  end
end

#thirdObject



54
55
56
# File 'lib/music_theory/scale.rb', line 54

def third
  third ||= MusicTheory::Third.new self.dup
end

#twelfth_root_of_twoObject



21
22
23
# File 'lib/music_theory/scale.rb', line 21

def twelfth_root_of_two
  2 ** (1.0/12)
end