Class: Deftones::Source::FatOscillator

Inherits:
Core::Source show all
Defined in:
lib/deftones/source/fat_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(type: :sawtooth, frequency: 440.0, count: 3, spread: 20.0, context: Deftones.context) ⇒ FatOscillator

Returns a new instance of FatOscillator.



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

def initialize(type: :sawtooth, frequency: 440.0, count: 3, spread: 20.0, context: Deftones.context)
  super(context: context)
  @frequency = Core::Signal.new(value: frequency, units: :frequency, context: context)
  @phases = []
  self.type = type
  self.count = count
  self.spread = spread
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



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

def count
  @count
end

#frequencyObject (readonly)

Returns the value of attribute frequency.



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

def frequency
  @frequency
end

#spreadObject

Returns the value of attribute spread.



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

def spread
  @spread
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#detune_frequencies(base_frequency) ⇒ Object (private)



50
51
52
53
54
55
56
57
58
# File 'lib/deftones/source/fat_oscillator.rb', line 50

def detune_frequencies(base_frequency)
  offsets = Array.new(@count) do |index|
    position = @count == 1 ? 0.0 : (index.to_f / (@count - 1)) - 0.5
    cents = position * @spread
    base_frequency * (2.0**(cents / 1200.0))
  end
  @phases = Array.new(@count, 0.0) if @phases.length != @count
  offsets
end

#normalize_type(type) ⇒ Object (private)

Raises:

  • (ArgumentError)


60
61
62
63
64
65
# File 'lib/deftones/source/fat_oscillator.rb', line 60

def normalize_type(type)
  normalized = type.to_sym
  return normalized if Oscillator::TYPES.include?(normalized)

  raise ArgumentError, "Unsupported oscillator type: #{type}"
end

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/deftones/source/fat_oscillator.rb', line 30

def process(_input_buffer, num_frames, start_frame, _cache)
  frequencies = @frequency.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)

    detuned = detune_frequencies(frequencies[index])
    samples = @phases.each_with_index.map do |phase, voice_index|
      phase_increment = detuned[voice_index] / context.sample_rate
      sample = Oscillator.sample(@type, phase, phase_increment)
      @phases[voice_index] = (phase + phase_increment) % 1.0
      sample
    end
    samples.sum / samples.length.to_f
  end
end