Class: Wavify::DSP::Effects::Bitcrusher

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

Overview

Bit-depth reduction and sample-rate hold effect.

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(bit_depth: 8, downsample: 1, mix: 1.0) ⇒ Bitcrusher

Returns a new instance of Bitcrusher.

Parameters:

  • bits: (Integer)
  • sample_rate_reduction: (Integer)
  • mix: (Numeric) (defaults to: 1.0)


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

def initialize(bit_depth: 8, downsample: 1, mix: 1.0)
  super()
  @bit_depth = validate_bit_depth!(bit_depth)
  @downsample = validate_downsample!(downsample)
  @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
# File 'lib/wavify/dsp/effects/bitcrusher.rb', line 22

def process_sample(sample, channel:, sample_rate:)
  dry = sample.to_f
  if (@counters.fetch(channel) % @downsample).zero?
    @held_samples[channel] = quantize(dry)
  end
  @counters[channel] += 1

  wet = @held_samples.fetch(channel)
  (dry * (1.0 - @mix)) + (wet * @mix)
end