Class: AudioStream::Fx::BiquadFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/audio_stream/fx/biquad_filter.rb

Constant Summary collapse

DEFAULT_Q =
1.0 / Math.sqrt(2.0)

Instance Method Summary collapse

Constructor Details

#initialize(soundinfo) ⇒ BiquadFilter

Returns a new instance of BiquadFilter.



6
7
8
9
10
11
12
# File 'lib/audio_stream/fx/biquad_filter.rb', line 6

def initialize(soundinfo)
  @soundinfo = soundinfo
  @biquads = [
    Vdsp::DoubleBiquad.new(1),
    Vdsp::DoubleBiquad.new(1),
  ]
end

Instance Method Details

#plot(width = 500) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/audio_stream/fx/biquad_filter.rb', line 67

def plot(width=500)
  data = plot_data(width)

  mag_range = nil
  if -1.0<data[:magnitude].min && data[:magnitude].max<1.0
    mag_range = [-1.0, 1.0]
  end

  Plotly::Plot.new(
    data: [{x: data[:x], y: data[:magnitude], name: 'Magnitude', yaxis: 'y1'}, {x: data[:x], y: data[:phase], name: 'Phase', yaxis: 'y2'}],
    layout: {
      xaxis: {title: 'Frequency (Hz)', type: 'log'},
      yaxis: {side: 'left', title: 'Magnitude (dB)', range: mag_range, showgrid: false},
      yaxis2: {side: 'right', title: 'Phase (deg)', showgrid: false, overlaying: 'y'}
    }
  )
end

#plot_data(width = 500) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/audio_stream/fx/biquad_filter.rb', line 32

def plot_data(width=500)
  b0 = @coef.b0
  b1 = @coef.b1
  b2 = @coef.b2
  a1 = @coef.a1
  a2 = @coef.a2

  noctaves = 10
  nyquist = @soundinfo.samplerate * 0.5

  freq = []
  x = []
  width.times {|i|
    f = i.to_f / width
    f = 2.0 ** (noctaves * (f - 1.0))
    freq << f
    x << (f * nyquist)
  }

  mag_res = []
  phase_res = []
  width.times {|i|
    omega = -Math::PI * freq[i]
    z = Complex(Math.cos(omega), Math.sin(omega))
    num = b0 + (b1 + b2 * z) * z
    den = 1 + (a1 + a2 * z) * z
    res = num / den

    mag_res << Decibel.mag(res.abs).db
    phase_res << 180 / Math::PI * Math.atan2(res.imag, res.real)
  }

  {x: x, magnitude: mag_res, phase: phase_res}
end

#process(input) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/audio_stream/fx/biquad_filter.rb', line 18

def process(input)
  channels = input.channels

  case channels
  when 1
    dst0 = @biquads[0].apply(input.streams[0])
    Buffer.new(dst0)
  when 2
    dst0 = @biquads[0].apply(input.streams[0])
    dst1 = @biquads[1].apply(input.streams[1])
    Buffer.new(dst0, dst1)
  end
end

#update_coef(*args, **kwargs) ⇒ Object

Raises:



14
15
16
# File 'lib/audio_stream/fx/biquad_filter.rb', line 14

def update_coef(*args, **kwargs)
  raise Error, "#{self.class.name}.update_coef is not implemented"
end