Class: Wavify::DSP::Effects::SoftLimiter

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

Overview

Soft-knee peak limiter for taming overs without a hard flat top.

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(threshold: 0.8, ceiling: 1.0, drive: 1.0) ⇒ SoftLimiter

Returns a new instance of SoftLimiter.

Parameters:

  • threshold: (Numeric) (defaults to: 0.8)
  • ceiling: (Numeric) (defaults to: 1.0)
  • drive: (Numeric) (defaults to: 1.0)

Raises:



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

def initialize(threshold: 0.8, ceiling: 1.0, drive: 1.0)
  super()
  @threshold = validate_unit!(threshold, :threshold)
  @ceiling = validate_unit!(ceiling, :ceiling)
  raise InvalidParameterError, "ceiling must be greater than threshold" unless @ceiling > @threshold

  @drive = validate_positive!(drive, :drive)
  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)


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

def process_sample(sample, channel:, sample_rate:)
  value = sample.to_f
  magnitude = value.abs
  return value if magnitude <= @threshold

  range = @ceiling - @threshold
  sign = value.negative? ? -1.0 : 1.0
  limited = @threshold + (range * (1.0 - Math.exp(-((magnitude - @threshold) * @drive) / range)))
  sign * limited.clamp(@threshold, @ceiling)
end