Class: AudioStream::Fx::PeakingFilter

Inherits:
BiquadFilter show all
Defined in:
lib/audio_stream/fx/peaking_filter.rb

Constant Summary

Constants inherited from BiquadFilter

BiquadFilter::DEFAULT_Q

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BiquadFilter

#initialize, #plot, #plot_data, #process

Constructor Details

This class inherits a constructor from AudioStream::Fx::BiquadFilter

Class Method Details

.create(soundinfo, freq:, bandwidth: 1.0, gain: 40.0) ⇒ Object

Parameters:

  • soundinfo (AudioStream::SoundInfo)
  • freq (Float)

    Cutoff frequency

  • bandwidth (Float) (defaults to: 1.0)

    bandwidth (octave)

  • gain (AudioStream::Decibel) (defaults to: 40.0)

    Amplification level at cutoff frequency



31
32
33
34
35
36
# File 'lib/audio_stream/fx/peaking_filter.rb', line 31

def self.create(soundinfo, freq:, bandwidth: 1.0, gain: 40.0)
  filter = new(soundinfo)
  filter.update_coef(freq: freq, bandwidth: bandwidth, gain: gain)

  filter
end

Instance Method Details

#update_coef(freq:, bandwidth:, gain:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/audio_stream/fx/peaking_filter.rb', line 5

def update_coef(freq:, bandwidth:, gain:)
  if Decibel===gain
    gain = gain.db
  end

  omega = 2.0 * Math::PI * freq / @samplerate
  alpha = Math.sin(omega) * Math.sinh(Math.log(2.0) / 2.0 * bandwidth * omega / Math.sin(omega))
  a = 10.0 ** (gain / 40.0)

  a0 = 1.0 + alpha / a
  a1 = -2.0 * Math.cos(omega)
  a2 = 1.0 - alpha / a
  b0 = 1.0 + alpha * a
  b1 = -2.0 * Math.cos(omega)
  b2 = 1.0 - alpha * a

  @coef = Vdsp::Biquad::Coefficient.new(b0/a0, b1/a0, b2/a0, a1/a0, a2/a0)
  @biquads.each {|biquad|
    biquad.coefficients = @coef
  }
end