Class: Deftones::Source::PWMOscillator
- Inherits:
-
Core::Source
- Object
- Core::Source
- Deftones::Source::PWMOscillator
- Defined in:
- lib/deftones/source/pwm_oscillator.rb
Instance Attribute Summary collapse
-
#detune ⇒ Object
Returns the value of attribute detune.
-
#frequency ⇒ Object
readonly
Returns the value of attribute frequency.
-
#modulation_depth ⇒ Object
(also: #modulationDepth)
readonly
Returns the value of attribute modulation_depth.
-
#modulation_frequency ⇒ Object
(also: #modulationFrequency)
readonly
Returns the value of attribute modulation_frequency.
Attributes inherited from Core::Source
Instance Method Summary collapse
-
#initialize(frequency: 440.0, modulation_frequency: 0.5, modulation_depth: 0.4, pulse_width: 0.5, detune: 0.0, context: Deftones.context) ⇒ PWMOscillator
constructor
A new instance of PWMOscillator.
- #process(_input_buffer, num_frames, start_frame, _cache) ⇒ Object
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, modulation_frequency: 0.5, modulation_depth: 0.4, pulse_width: 0.5, detune: 0.0, context: Deftones.context) ⇒ PWMOscillator
Returns a new instance of PWMOscillator.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/deftones/source/pwm_oscillator.rb', line 8 def initialize(frequency: 440.0, modulation_frequency: 0.5, modulation_depth: 0.4, pulse_width: 0.5, detune: 0.0, context: Deftones.context) super(context: context) @frequency = Core::Signal.new(value: frequency, units: :frequency, context: context) @modulation_frequency = Core::Signal.new(value: modulation_frequency, units: :frequency, context: context) @modulation_depth = Core::Signal.new(value: modulation_depth, units: :number, context: context) @detune = Core::Signal.new(value: detune, units: :number, context: context) @base_width = pulse_width.to_f @phase = 0.0 @modulation_phase = 0.0 end |
Instance Attribute Details
#detune ⇒ Object
Returns the value of attribute detune.
6 7 8 |
# File 'lib/deftones/source/pwm_oscillator.rb', line 6 def detune @detune end |
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
6 7 8 |
# File 'lib/deftones/source/pwm_oscillator.rb', line 6 def frequency @frequency end |
#modulation_depth ⇒ Object (readonly) Also known as: modulationDepth
Returns the value of attribute modulation_depth.
6 7 8 |
# File 'lib/deftones/source/pwm_oscillator.rb', line 6 def modulation_depth @modulation_depth end |
#modulation_frequency ⇒ Object (readonly) Also known as: modulationFrequency
Returns the value of attribute modulation_frequency.
6 7 8 |
# File 'lib/deftones/source/pwm_oscillator.rb', line 6 def modulation_frequency @modulation_frequency end |
Instance Method Details
#process(_input_buffer, num_frames, start_frame, _cache) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/deftones/source/pwm_oscillator.rb', line 24 def process(_input_buffer, num_frames, start_frame, _cache) frequencies = @frequency.process(num_frames, start_frame) mod_frequencies = @modulation_frequency.process(num_frames, start_frame) mod_depths = @modulation_depth.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) width = @base_width + (Math.sin(2.0 * Math::PI * @modulation_phase) * mod_depths[index] * 0.5) duty = Deftones::DSP::Helpers.clamp(width, 0.05, 0.95) sample = @phase < duty ? 1.0 : -1.0 frequency = frequencies[index] * (2.0**(detunes[index].to_f / 1200.0)) @phase = (@phase + (frequency / context.sample_rate)) % 1.0 @modulation_phase = (@modulation_phase + (mod_frequencies[index] / context.sample_rate)) % 1.0 sample end end |