Class: Wavify::DSP::Effects::Distortion

Inherits:
EffectBase
  • Object
show all
Defined in:
lib/wavify/dsp/effects/distortion.rb,
sig/effects.rbs

Overview

Soft-clipping distortion with tone shaping and dry/wet mix.

Constant Summary

Constants inherited from EffectBase

EffectBase::TAIL_CHUNK_FRAMES

Instance Method Summary collapse

Methods inherited from EffectBase

#apply, #build_runtime, #flush, #latency, #lookahead, #process, #reset, #tail_duration

Constructor Details

#initialize(drive: 0.5, tone: 0.5, mix: 1.0) ⇒ Distortion

Returns a new instance of Distortion.

Parameters:

  • drive: (Numeric) (defaults to: 0.5)
  • mix: (Numeric) (defaults to: 1.0)
  • tone: (Numeric) (defaults to: 0.5)


8
9
10
11
12
13
14
# File 'lib/wavify/dsp/effects/distortion.rb', line 8

def initialize(drive: 0.5, tone: 0.5, mix: 1.0)
  super()
  @drive = validate_unit!(drive, :drive)
  @tone = validate_unit!(tone, :tone)
  @mix = validate_unit!(mix, :mix)
  reset
end

Instance Method Details

#process_sample(sample, channel:, sample_rate:) ⇒ Float

Processes a single sample for one channel.

Parameters:

  • sample (Numeric)
  • channel (Integer)
  • sample_rate (Integer)

Returns:

  • (Float)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wavify/dsp/effects/distortion.rb', line 22

def process_sample(sample, channel:, sample_rate:)
  dry = sample.to_f
  wet = Math.tanh(dry * pre_gain)

  # One-pole low-pass on the distorted signal for tone shaping.
  previous = @tone_state.fetch(channel)
  filtered = previous + (@tone_coefficient * (wet - previous))
  @tone_state[channel] = filtered

  (dry * (1.0 - @mix)) + (filtered * @mix)
end