Class: ColtraneSynth::Synth

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer, freq, nominal_rate) ⇒ Synth

Returns a new instance of Synth.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/coltrane/synth/synth.rb', line 7

def initialize(buffer, freq, nominal_rate)
  @freq = freq.to_f
  @nominal_rate = nominal_rate

  i = -1
  wav = NArray.sint(1024)

  while i += 1
    1024.times do |j|
      wav[j] = (0.4 * Math.sin(phase(freq) * (i * 1024 + j)) * 0x7FFF).round
    end
    buffer << wav
  end
end

Instance Attribute Details

#freqObject (readonly)

Returns the value of attribute freq.



5
6
7
# File 'lib/coltrane/synth/synth.rb', line 5

def freq
  @freq
end

#nominal_rateObject (readonly)

Returns the value of attribute nominal_rate.



5
6
7
# File 'lib/coltrane/synth/synth.rb', line 5

def nominal_rate
  @nominal_rate
end

Instance Method Details

#phase(freq) ⇒ Object



22
23
24
# File 'lib/coltrane/synth/synth.rb', line 22

def phase(freq)
  Math::PI * 2.0 * freq / nominal_rate
end