Class: AudioStream::Fx::Compressor

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

Instance Method Summary collapse

Constructor Details

#initialize(threshold: 0.5, ratio: 0.5) ⇒ Compressor

Returns a new instance of Compressor.



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

def initialize(threshold: 0.5, ratio: 0.5)
  @threshold = threshold
  @ratio = ratio
  @zoom = 1.0 / (@ratio * (1.0 - @threshold) + @threshold)
end

Instance Method Details

#process(input) ⇒ Object



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

def process(input)
  streams = input.streams.map {|stream|
    sign = Vdsp::DoubleArray.new(input.window_size)
    Vdsp::UnsafeDouble.vlim(stream, 0, 1, 0.0, @zoom, sign, 0, 1, input.window_size)

    abs = stream.abs

    under = Vdsp::DoubleArray.new(input.window_size)
    Vdsp::UnsafeDouble.vclip(abs, 0, 1, 0.0, @threshold, under, 0, 1, input.window_size)

    over = Vdsp::DoubleArray.new(input.window_size)
    Vdsp::UnsafeDouble.vthr(abs, 0, 1, @threshold, over, 0, 1, input.window_size)
    over = (over - @threshold) * @ratio

    (under + over) * sign
  }
  Buffer.new(*streams)
end