Class: AudioStream::Fx::NoiseGate

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

Instance Method Summary collapse

Constructor Details

#initialize(threshold: 0.01) ⇒ NoiseGate

Returns a new instance of NoiseGate.



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

def initialize(threshold: 0.01)
  @threshold = threshold
  @window = HanningWindow.instance
end

Instance Method Details

#process(input) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/audio_stream/fx/noise_gate.rb', line 9

def process(input)
  window_size = input.window_size
  channels = input.channels

  # fft
  na = @window.process(input).to_float_na
  fft = FFTW3.fft(na, FFTW3::FORWARD) / na.length

  # noise gate
  fft.size.times {|i|
    if @threshold <= fft[i].abs
      fft[i] = 0i
    end
  }
  wet_na = FFTW3.fft(fft, FFTW3::BACKWARD)
  noise = Buffer.from_na(wet_na)

  input - noise
end