Class: Feep::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

main entry point



13
14
15
16
# File 'lib/feep.rb', line 13

def initialize(options)
  @options = options
  configure_sound(options)
end

Instance Method Details

#configure_sound(options) ⇒ Object

takes CLI options, massages them, and passes them to the sound generation methods



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.rb', line 20

def configure_sound(options)
  ### A. Check non-essential options
  if !WAVE_TYPES.include?(options[:waveform])
    print_error(ERROR_MSG[:invalid_waveform])
  end

  # Convert ms to secs in order to multiply the sample rate by
  duration_s = (options[:duration].to_f / 1000)

  # Make the samples to write a nice integer
  samples_to_write = (SAMPLE_RATE * duration_s).to_i

  ### B. Set frequency/note, or group of frequencies/notes, to play

  # Is it a chord, scale, or single note?
  if options[:freq_or_note].include?(',')
    # yes, it's a chord, so create threads
    threads = []
    options[:freq_or_note].split(',').each do |note|
      sound_to_play = Utils.convert_note_to_freq(note)
      output_filename = "#{options[:waveform]}_#{sound_to_play}Hz_#{options[:volume].to_f}_#{options[:duration]}.wav"
      threads << Thread.new {
        SoundPlayer.new.play_note(sound_to_play, output_filename, samples_to_write, options) 
      }
    end
    threads.each { |th| th.join }
  else
    # it's a scale
    if options[:scale]
      Feep::Scale.new.play_scale(options)
    else
      # no, it's a single note
      sound_to_play = Utils.convert_note_to_freq(options[:freq_or_note])
      output_filename = "#{options[:waveform]}_#{sound_to_play}Hz_#{options[:volume].to_f}_#{options[:duration]}.wav"
      SoundPlayer.new.play_note(sound_to_play, output_filename, samples_to_write, options)
    end
  end
end