Class: SynthBlocks::Core::Oscillator

Inherits:
Object
  • Object
show all
Defined in:
lib/synth_blocks/core/oscillator.rb

Overview

simple oscillator can currently do squarewave, sawtooth and sine this oscillator is not bandwidth limited and will thus alias like there’s no tomorrow

Instance Method Summary collapse

Constructor Details

#initialize(sampling_frequency) ⇒ Oscillator

Create new oscillator



10
11
12
13
# File 'lib/synth_blocks/core/oscillator.rb', line 10

def initialize(sampling_frequency)
  @sampling_frequency = sampling_frequency.to_f
  @in_cycle = 0
end

Instance Method Details

#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