Method: SynthBlocks::Core::Oscillator#run

Defined in:
lib/synth_blocks/core/oscillator.rb

#run(frequency, pulse_width: 0.5, waveform: :square) ⇒ Object

frequency

Oscillator frequency in Hz (can be altered at any time)

pulse_width

pulse width, only in effect when creating a square wave

waveform

can be: :square (default), :sawtooth, :sine



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/synth_blocks/core/oscillator.rb', line 18

def run(frequency, pulse_width: 0.5, waveform: :square)
  period = @sampling_frequency / frequency.to_f
  output = 0
  if waveform == :square
    output = @in_cycle > pulse_width ? -1.0 : 1.0
  end
  if waveform == :sawtooth
    output = (@in_cycle * 2) - 1.0
  end
  if waveform == :sine
    phase = @in_cycle * 2 * Math::PI
    output = Math.sin(phase)
  end
  @in_cycle = (@in_cycle + (1.0 / period)) % 1.0
  output
end