Class: Feep::Scale

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

Instance Method Summary collapse

Instance Method Details

#play_scale(options) ⇒ Object

plays the musical scale with Feep



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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/feep/scale.rb', line 11

def play_scale(options)
  unless SCALES.include?(options[:scale].to_sym)
    Utils.print_error(:invalid_scale)
  end

  valid_notes = NOTES | NOTES_ALT | FREQS
  freq = Utils::convert_note_to_freq(options[:freq_or_note]).to_f

  if valid_notes.include?(freq)
    steps = SCALES[options[:scale].to_sym].split(',')

    freq_index = FREQS.index(freq)

    feep_options = {
      :freq_or_note => freq.to_s, 
      :waveform => options[:waveform], 
      :volume => options[:volume], 
      :duration => options[:duration], 
      :save => options[:save], 
      :verbose => options[:verbose]
    }

    # play number of degrees of scale supplied or one octave by default
    degrees = options[:degrees] || steps.length

    if options[:verbose]
      puts "Playing a #{options[:scale]} scale..."
    end

    1.upto(degrees.to_i) {|deg|
      if options[:verbose]
        puts "note: #{NOTE_FREQ.key(freq)}, freq: #{freq}"
      end
      
      # play note
      Feep::Base.new(feep_options)
  
      # go to the next note in the scale
      freq_index += steps[deg].to_i

      # set new note to play next time around
      freq = feep_options[:freq_or_note] = FREQS[freq_index].to_s
    }
  else
    Utils.print_error(:invalid_scale_root_note)
  end
end