/* * Copyright (C) 2010 Google Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Empirical gain calibration tested across many impulse responses to ensure // perceived volume is same as dry (unprocessed) signal constfloat GainCalibration = 0.00125f; constfloat GainCalibrationSampleRate = 44100;
// A minimum power value to when normalizing a silent (or very quiet) impulse // response constfloat MinPower = 0.000125f;
staticfloat calculateNormalizationScale(const nsTArray<constfloat*>& response,
size_t aLength, float sampleRate) { // Normalize by RMS power
size_t numberOfChannels = response.Length();
float power = 0;
for (size_t i = 0; i < numberOfChannels; ++i) { float channelPower = AudioBufferSumOfSquares(response[i], aLength);
power += channelPower;
}
power = sqrt(power / (numberOfChannels * aLength));
// Protect against accidental overload if (!std::isfinite(power) || std::isnan(power) || power < MinPower)
power = MinPower;
float scale = 1 / power;
scale *= GainCalibration; // calibrate to make perceived volume same as // unprocessed
// Scale depends on sample-rate. if (sampleRate) scale *= GainCalibrationSampleRate / sampleRate;
// True-stereo compensation if (numberOfChannels == 4) scale *= 0.5f;
// The reverb can handle a mono impulse response and still do stereo // processing
size_t numResponseChannels = impulseResponseBuffer.Length();
MOZ_ASSERT(numResponseChannels > 0); // The number of convolvers required is at least the number of audio // channels. Even if there is initially only one audio channel, another // may be added later, and so a second convolver is created now while the // impulse response is available.
size_t numConvolvers = std::max<size_t>(numResponseChannels, 2);
m_convolvers.SetCapacity(numConvolvers);
int convolverRenderPhase = 0; for (size_t i = 0; i < numConvolvers; ++i) {
size_t channelIndex = i < numResponseChannels ? i : 0; constfloat* channel = impulseResponseBuffer[channelIndex];
size_t length = impulseResponseBufferLength;
bool allocationFailure;
UniquePtr<ReverbConvolver> convolver( new ReverbConvolver(channel, length, maxFFTSize, convolverRenderPhase,
useBackgroundThreads, &allocationFailure)); if (allocationFailure) { returnfalse;
}
m_convolvers.AppendElement(std::move(convolver));
convolverRenderPhase += WEBAUDIO_BLOCK_SIZE;
}
// For "True" stereo processing we allocate a temporary buffer to avoid // repeatedly allocating it in the process() method. It can be bad to allocate // memory in a real-time thread. if (numResponseChannels == 4) {
m_tempBuffer.AllocateChannels(2);
WriteZeroesToAudioBlock(&m_tempBuffer, 0, WEBAUDIO_BLOCK_SIZE);
} returntrue;
}
void Reverb::process(const AudioBlock* sourceBus, AudioBlock* destinationBus) { // Do a fairly comprehensive sanity check. // If these conditions are satisfied, all of the source and destination // pointers will be valid for the various matrixing cases. bool isSafeToProcess =
sourceBus && destinationBus && sourceBus->ChannelCount() > 0 &&
destinationBus->mChannelData.Length() > 0 &&
WEBAUDIO_BLOCK_SIZE <= MaxFrameSize &&
WEBAUDIO_BLOCK_SIZE <= size_t(sourceBus->GetDuration()) &&
WEBAUDIO_BLOCK_SIZE <= size_t(destinationBus->GetDuration());
MOZ_ASSERT(isSafeToProcess); if (!isSafeToProcess) return;
// For now only handle mono or stereo output
MOZ_ASSERT(destinationBus->ChannelCount() <= 2);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.