Class: AudioStream::Fx::ConvolutionReverb
- Inherits:
-
Object
- Object
- AudioStream::Fx::ConvolutionReverb
- Defined in:
- lib/audio_stream/fx/convolution_reverb.rb
Instance Method Summary collapse
-
#initialize(impulse, dry: -6,, wet: -6)) ⇒ ConvolutionReverb
constructor
A new instance of ConvolutionReverb.
- #process(input) ⇒ Object
Constructor Details
#initialize(impulse, dry: -6,, wet: -6)) ⇒ ConvolutionReverb
Returns a new instance of ConvolutionReverb.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/audio_stream/fx/convolution_reverb.rb', line 7 def initialize(impulse, dry: -6, wet: -6) impulse_bufs = impulse.to_a @impulse_size = impulse_bufs.size @channels = impulse_bufs[0].channels @window_size = impulse_bufs[0].window_size @dry_gain = Decibel.db(dry).mag @wet_gain = Decibel.db(wet).mag zero_buf = Buffer.create(@window_size, @channels) impulse_bufs = [zero_buf.clone] + impulse_bufs @impulse_ffts = [] @impulse_size.times {|i| na = NArray.float(@channels, @window_size*2) impulse_bufs[i].to_float_na(na, 0) impulse_bufs[i+1].to_float_na(na, @window_size*@channels) @impulse_ffts << FFTW3.fft(na, FFTW3::FORWARD) / na.length } @impulse_max_gain = @impulse_ffts.map{|c| c.real**2 + c.imag**2}.map(&:sum).max / @channels @wet_ffts = RingBuffer.new(@impulse_size) { Array.new(@impulse_size, NArray.float(@channels, @window_size*2)) } @prev_input = zero_buf.clone end |
Instance Method Details
#process(input) ⇒ Object
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/audio_stream/fx/convolution_reverb.rb', line 35 def process(input) if @window_size!=input.window_size raise "window size is not match: impulse.size=#{@window_size} input.size=#{input.window_size}" end if @channels!=input.channels raise "channels is not match: impulse.channels=#{@channels} input.channels=#{input.channels}" end # current dry to wet na = NArray.float(@channels, @window_size*2) @prev_input.to_float_na(na, 0) input.to_float_na(na, @window_size*@channels) input_fft = FFTW3.fft(na, FFTW3::FORWARD) / na.length @wet_ffts.current = @impulse_ffts.map {|impulse_fft| input_fft * impulse_fft } @wet_ffts.rotate @prev_input = input.clone # calc wet matrix sum wet_fft = NArray.complex(@channels, @window_size*2) @wet_ffts.each_with_index {|wet, i| wet_fft += wet[@impulse_size-i-1] } wet_na = FFTW3.fft(wet_fft, FFTW3::BACKWARD)[(@channels*@window_size)...(@channels*@window_size*2)] * (@wet_gain / @impulse_max_gain) # current dry + wet matrix sum src0 = input.streams[0] src1 = input.streams[1] case @channels when 1 output = Buffer.create_mono(@window_size) dst0 = output.streams[0] @window_size.times {|i| dry = src0[i] * @dry_gain wet = wet_na[i].real dst0[i] = dry + wet } when 2 output = Buffer.create_stereo(@window_size) dst0 = output.streams[0] dst1 = output.streams[1] @window_size.times {|i| # dry dry0 = src0[i] * @dry_gain dry1 = src1[i] * @dry_gain # wet wet0 = wet_na[i*2].real wet1 = wet_na[(i*2)+1].real dst0[i] = dry0 + wet0 dst1[i] = dry1 + wet1 } end output end |