Class: AudioStream::Fx::GraphicEqualizer

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

Instance Method Summary collapse

Constructor Details

#initialize(soundinfo) ⇒ GraphicEqualizer

Returns a new instance of GraphicEqualizer.



4
5
6
7
# File 'lib/audio_stream/fx/graphic_equalizer.rb', line 4

def initialize(soundinfo)
  @soundinfo = soundinfo
  @filters = []
end

Instance Method Details

#add(freq:, bandwidth: 1.0, gain:) ⇒ Object



9
10
11
12
# File 'lib/audio_stream/fx/graphic_equalizer.rb', line 9

def add(freq:, bandwidth: 1.0, gain:)
  @filters << PeakingFilter.create(@soundinfo, freq: freq, bandwidth: bandwidth, gain: gain)
  self
end

#plot(width = 500) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/audio_stream/fx/graphic_equalizer.rb', line 21

def plot(width=500)
  data_arr = @filters.map{|filter| filter.plot_data(width)}

  data = {
    x: data_arr[0][:x],
    magnitude: data_arr.map{|d| d[:magnitude]}.transpose.map {|a| a.sum},
    phase: data_arr.map{|d| d[:phase]}.transpose.map {|a| a.sum},
  }

  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)', showgrid: false},
      yaxis2: {side: 'right', title: 'Phase (deg)', showgrid: false, overlaying: 'y'}
    }
  )
end

#process(input) ⇒ Object



14
15
16
17
18
19
# File 'lib/audio_stream/fx/graphic_equalizer.rb', line 14

def process(input)
  @filters.each {|filter|
    input = filter.process(input)
  }
  input
end