Class: Deftones::Source::FMOscillator

Inherits:
Core::Source show all
Defined in:
lib/deftones/source/fm_oscillator.rb

Instance Attribute Summary collapse

Attributes inherited from Core::Source

#mute, #onstop, #volume

Instance Method Summary collapse

Methods inherited from Core::Source

#active_at?, #apply_volume!, #cancel_stop, #clear_transport_event, #dispose, #mute?, #notify_stop_in_window, #number_of_inputs, #render, #render_block, #resolve_time, #resolve_transport_time, #restart, #schedule_transport_event, #source_type, #start, #state, #stop, #sync, #synced?, #unsync, #uses_legacy_render_for_block?

Constructor Details

#initialize(frequency: 440.0, harmonicity: 2.0, modulation_index: 5.0, detune: 0.0, phase: 0.0, context: Deftones.context) ⇒ FMOscillator

Returns a new instance of FMOscillator.



8
9
10
11
12
13
14
15
16
17
# File 'lib/deftones/source/fm_oscillator.rb', line 8

def initialize(frequency: 440.0, harmonicity: 2.0, modulation_index: 5.0,
               detune: 0.0, phase: 0.0, context: Deftones.context)
  super(context: context)
  @frequency = Core::Signal.new(value: frequency, units: :frequency, context: context)
  @harmonicity = Core::Signal.new(value: harmonicity, units: :number, context: context)
  @modulation_index = Core::Signal.new(value: modulation_index, units: :number, context: context)
  @detune = Core::Signal.new(value: detune, units: :number, context: context)
  @carrier_phase = phase.to_f % 1.0
  @modulator_phase = phase.to_f % 1.0
end

Instance Attribute Details

#detuneObject

Returns the value of attribute detune.



6
7
8
# File 'lib/deftones/source/fm_oscillator.rb', line 6

def detune
  @detune
end

#frequencyObject (readonly)

Returns the value of attribute frequency.



6
7
8
# File 'lib/deftones/source/fm_oscillator.rb', line 6

def frequency
  @frequency
end

#harmonicityObject (readonly)

Returns the value of attribute harmonicity.



6
7
8
# File 'lib/deftones/source/fm_oscillator.rb', line 6

def harmonicity
  @harmonicity
end

#modulation_indexObject (readonly) Also known as: modulationIndex

Returns the value of attribute modulation_index.



6
7
8
# File 'lib/deftones/source/fm_oscillator.rb', line 6

def modulation_index
  @modulation_index
end

Instance Method Details

#process(_input_buffer, num_frames, start_frame, _cache) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/deftones/source/fm_oscillator.rb', line 23

def process(_input_buffer, num_frames, start_frame, _cache)
  frequencies = @frequency.process(num_frames, start_frame)
  harmonicities = @harmonicity.process(num_frames, start_frame)
  modulation_indices = @modulation_index.process(num_frames, start_frame)
  detunes = @detune.process(num_frames, start_frame)

  Array.new(num_frames) do |index|
    current_time = (start_frame + index).to_f / context.sample_rate
    next 0.0 unless active_at?(current_time)

    carrier_frequency = frequencies[index] * (2.0**(detunes[index].to_f / 1200.0))
    modulator_frequency = carrier_frequency * harmonicities[index]
    modulation = Math.sin(2.0 * Math::PI * @modulator_phase) * modulation_indices[index]
    sample = Math.sin((2.0 * Math::PI * @carrier_phase) + modulation)

    @carrier_phase = (@carrier_phase + (carrier_frequency / context.sample_rate)) % 1.0
    @modulator_phase = (@modulator_phase + (modulator_frequency / context.sample_rate)) % 1.0
    sample
  end
end