Class: InevitableCacophony::ToneGenerator

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

Constant Summary collapse

SAMPLE_RATE =

Hertz

44100
TAU =

One full revolution of a circle (or one full cycle of a sine wave)

Math::PI * 2

Instance Method Summary collapse

Constructor Details

#initialize(tonic) ⇒ ToneGenerator

Returns a new instance of ToneGenerator.

Parameters:

  • tonic (Numeric)

    The tonic frequency, in Hertz



37
38
39
40
# File 'lib/inevitable_cacophony/tone_generator.rb', line 37

def initialize(tonic)
        @tonic = tonic
        @phrases = []
end

Instance Method Details

#add_phrase(phrase) ⇒ Object



24
25
26
# File 'lib/inevitable_cacophony/tone_generator.rb', line 24

def add_phrase(phrase)
        		@phrases << phrase_buffer(phrase)
end

#phrase_buffer(phrase) ⇒ Object

Create a buffer representing a given phrase as an audio sample

Parameters:



19
20
21
22
# File 'lib/inevitable_cacophony/tone_generator.rb', line 19

def phrase_buffer(phrase)
	samples = phrase.notes.map { |note| note_samples(note, phrase.tempo) }
               WaveFile::Buffer.new(samples.flatten, WaveFile::Format.new(:mono, :float, SAMPLE_RATE))
end

#write(io) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/inevitable_cacophony/tone_generator.rb', line 28

def write(io)
        WaveFile::Writer.new(io, WaveFile::Format.new(:mono, :pcm_16, SAMPLE_RATE)) do |writer|
                @phrases.each do |phrase|
                        writer.write(phrase)
                end
        end
end