Class: WSK::Actions::NoiseGate
- Inherits:
-
Object
- Object
- WSK::Actions::NoiseGate
- Defined in:
- lib/WSK/Actions/NoiseGate.rb
Constant Summary
Constants included from FFT
FFT::FFTDISTANCE_AVERAGE_HISTORY_TOLERANCE_PC, FFT::FFTDISTANCE_MAX_HISTORY_TOLERANCE_PC, FFT::FFTDIST_MAX, FFT::FFTNBRSAMPLES_HISTORY, FFT::FFTSAMPLE_FREQ, FFT::FFT_SAMPLES_PREFETCH, FFT::FREQINDEX_FIRST, FFT::FREQINDEX_LAST
Instance Method Summary collapse
-
#execute(iInputData, oOutputData) ⇒ Object
Execute.
-
#get_nbr_samples(iInputData) ⇒ Object
Get the number of samples that will be written.
Methods included from FFT
#getNextFFTSample, #getNextNonSilentSample, #getNextSilentSample, #getSampleBeyondThresholds
Methods included from Common
#accessInputWaveFile, #accessOutputWaveFile, #getWaveFileAccesses, #parsePlugins, #readDuration, #readFFTProfile, #readThresholds, #val2db
Instance Method Details
#execute(iInputData, oOutputData) ⇒ Object
Execute
- Parameters
-
iInputData (WSK::Model::InputData): The input data
-
oOutputData (Object): The output data to fill
- Return
-
Exception: An error, or nil if success
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 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/WSK/Actions/NoiseGate.rb', line 34 def execute(iInputData, oOutputData) lSilenceThresholds = readThresholds(@SilenceThreshold, iInputData.Header.NbrChannels) lAttackDuration = readDuration(@Attack, iInputData.Header.SampleRate) lReleaseDuration = readDuration(@Release, iInputData.Header.SampleRate) lSilenceDuration = readDuration(@SilenceMin, iInputData.Header.SampleRate) lNoiseFFTMaxDistance, lNoiseFFTProfile = readFFTProfile(@NoiseFFTFileName) # Create a map of the non silent parts # list< [ Integer, Integer ] > # list< [ IdxBeginNonSilentSample, IdxEndNonSilentSample ] > lNonSilentParts = [] lIdxSample = 0 while (lIdxSample != nil) lIdxNextSilence, lSilenceLength, lIdxNextBeyondThresholds = getNextSilentSample(iInputData, lIdxSample, lSilenceThresholds, lSilenceDuration, lNoiseFFTProfile, lNoiseFFTMaxDistance, false) if (lIdxNextSilence == nil) lNonSilentParts << [lIdxSample, iInputData.NbrSamples-1] else lNonSilentParts << [lIdxSample, lIdxNextSilence-1] end lIdxSample = lIdxNextBeyondThresholds end lStrNonSilentParts = lNonSilentParts.map { |iNonSilentInfo| "[#{iNonSilentInfo[0]/iInputData.Header.SampleRate}s - #{iNonSilentInfo[1]/iInputData.Header.SampleRate}s]" } log_info "#{lNonSilentParts.size} non silent parts: #{lStrNonSilentParts[0..9].join(', ')}" lStrDbgNonSilentParts = lNonSilentParts.map { |iNonSilentInfo| "[#{iNonSilentInfo[0]} - #{iNonSilentInfo[1]}]" } log_debug "#{lNonSilentParts.size} non silent parts: #{lStrDbgNonSilentParts[0..9].join(', ')}" # Now we write the non-silent parts, spaced with nulled parts, with fadeins and fadeouts around. lNextSampleToWrite = 0 lNonSilentParts.each do |iNonSilentInfo| iIdxBegin, iIdxEnd = iNonSilentInfo # Compute the fadein buffer lIdxBeginFadeIn = iIdxBegin - lAttackDuration if (lIdxBeginFadeIn < 0) lIdxBeginFadeIn = 0 end # Write a blank buffer if needed if (lIdxBeginFadeIn > lNextSampleToWrite) log_debug "Write #{lIdxBeginFadeIn - lNextSampleToWrite} samples of silence" oOutputData.pushRawBuffer("\000" * (((lIdxBeginFadeIn - lNextSampleToWrite)*iInputData.Header.NbrChannels*iInputData.Header.NbrBitsPerSample)/8)) end lFadeInSize = iIdxBegin-lIdxBeginFadeIn if (lFadeInSize > 0) lBuffer = [] lIdxFadeSample = 0 iInputData.each(lIdxBeginFadeIn) do |iChannelValues| if (lIdxFadeSample == lFadeInSize) break end lBuffer.concat(iChannelValues.map { |iValue| (iValue*lIdxFadeSample)/lFadeInSize }) lIdxFadeSample += 1 end log_debug "Write #{lBuffer.size/iInputData.Header.NbrChannels} samples of fadein." oOutputData.pushBuffer(lBuffer) else log_debug 'Ignore empty fadein.' end # Write the file log_debug "Write #{iIdxEnd-iIdxBegin+1} samples of audio." iInputData.each_raw_buffer(iIdxBegin, iIdxEnd) do |iInputRawBuffer, iNbrSamples, iNbrChannels| oOutputData.pushRawBuffer(iInputRawBuffer) end # Write the fadeout buffer lIdxEndFadeOut = iIdxEnd + lReleaseDuration if (lIdxEndFadeOut >= iInputData.NbrSamples) lIdxEndFadeOut = iInputData.NbrSamples - 1 end lFadeOutSize = lIdxEndFadeOut-iIdxEnd if (lFadeOutSize > 0) lBuffer = [] lIdxFadeSample = 0 iInputData.each(iIdxEnd+1) do |iChannelValues| if (lIdxFadeSample == lFadeOutSize) break end lBuffer.concat(iChannelValues.map { |iValue| (iValue*(lFadeOutSize-lIdxFadeSample))/lFadeOutSize }) lIdxFadeSample += 1 end log_debug "Write #{lBuffer.size/iInputData.Header.NbrChannels} samples of fadeout." oOutputData.pushBuffer(lBuffer) else log_debug 'Ignore empty fadeout.' end lNextSampleToWrite = lIdxEndFadeOut + 1 end # If there is remaining silence, write it if (lNextSampleToWrite < iInputData.NbrSamples) log_debug "Write #{iInputData.NbrSamples - lNextSampleToWrite} samples of last silence" oOutputData.pushRawBuffer("\000" * (((iInputData.NbrSamples - lNextSampleToWrite)*iInputData.Header.NbrChannels*iInputData.Header.NbrBitsPerSample)/8)) end return nil end |
#get_nbr_samples(iInputData) ⇒ Object
Get the number of samples that will be written. This is called before execute, as it is needed to write the output file. It is possible to give a majoration: it will be padded with silence.
- Parameters
-
iInputData (WSK::Model::InputData): The input data
- Return
-
Integer: The number of samples to be written
23 24 25 |
# File 'lib/WSK/Actions/NoiseGate.rb', line 23 def get_nbr_samples(iInputData) return iInputData.NbrSamples end |