2013-08-08 02:37:36 -07:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY APPLE INC. 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 INC. 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.
|
|
|
|
*/
|
|
|
|
|
2013-08-08 15:07:49 -07:00
|
|
|
#include "HRTFPanner.h"
|
2013-08-15 12:44:14 -07:00
|
|
|
#include "HRTFDatabaseLoader.h"
|
2013-08-08 02:37:36 -07:00
|
|
|
|
2013-08-08 15:07:49 -07:00
|
|
|
#include "FFTConvolver.h"
|
|
|
|
#include "HRTFDatabase.h"
|
2013-08-08 02:37:36 -07:00
|
|
|
|
|
|
|
using namespace std;
|
2013-08-08 15:07:49 -07:00
|
|
|
using namespace mozilla;
|
2014-03-04 13:06:57 -08:00
|
|
|
using dom::ChannelInterpretation;
|
2013-08-08 02:37:36 -07:00
|
|
|
|
|
|
|
namespace WebCore {
|
|
|
|
|
|
|
|
// The value of 2 milliseconds is larger than the largest delay which exists in any HRTFKernel from the default HRTFDatabase (0.0136 seconds).
|
|
|
|
// We ASSERT the delay values used in process() with this value.
|
|
|
|
const double MaxDelayTimeSeconds = 0.002;
|
|
|
|
|
|
|
|
const int UninitializedAzimuth = -1;
|
2014-03-02 15:49:45 -08:00
|
|
|
const unsigned RenderingQuantum = WEBAUDIO_BLOCK_SIZE;
|
2013-08-08 02:37:36 -07:00
|
|
|
|
2013-08-08 15:08:06 -07:00
|
|
|
HRTFPanner::HRTFPanner(float sampleRate, mozilla::TemporaryRef<HRTFDatabaseLoader> databaseLoader)
|
2013-08-08 15:07:49 -07:00
|
|
|
: m_databaseLoader(databaseLoader)
|
2013-08-08 02:37:36 -07:00
|
|
|
, m_sampleRate(sampleRate)
|
|
|
|
, m_crossfadeSelection(CrossfadeSelection1)
|
|
|
|
, m_azimuthIndex1(UninitializedAzimuth)
|
|
|
|
, m_azimuthIndex2(UninitializedAzimuth)
|
2013-10-24 18:05:42 -07:00
|
|
|
// m_elevation1 and m_elevation2 are initialized in pan()
|
2013-08-08 02:37:36 -07:00
|
|
|
, m_crossfadeX(0)
|
|
|
|
, m_crossfadeIncr(0)
|
2013-08-08 02:38:30 -07:00
|
|
|
, m_convolverL1(HRTFElevation::fftSizeForSampleRate(sampleRate))
|
2013-08-08 15:07:49 -07:00
|
|
|
, m_convolverR1(m_convolverL1.fftSize())
|
|
|
|
, m_convolverL2(m_convolverL1.fftSize())
|
|
|
|
, m_convolverR2(m_convolverL1.fftSize())
|
2014-03-30 22:32:34 -07:00
|
|
|
, m_delayLine(MaxDelayTimeSeconds * sampleRate, 1.0)
|
2013-08-08 02:37:36 -07:00
|
|
|
{
|
2013-08-08 15:08:06 -07:00
|
|
|
MOZ_ASSERT(m_databaseLoader);
|
|
|
|
MOZ_COUNT_CTOR(HRTFPanner);
|
|
|
|
|
2013-08-08 15:07:49 -07:00
|
|
|
m_tempL1.SetLength(RenderingQuantum);
|
|
|
|
m_tempR1.SetLength(RenderingQuantum);
|
|
|
|
m_tempL2.SetLength(RenderingQuantum);
|
|
|
|
m_tempR2.SetLength(RenderingQuantum);
|
2013-08-08 02:37:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
HRTFPanner::~HRTFPanner()
|
|
|
|
{
|
2013-08-08 15:08:06 -07:00
|
|
|
MOZ_COUNT_DTOR(HRTFPanner);
|
2013-08-08 02:37:36 -07:00
|
|
|
}
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
size_t HRTFPanner::sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = aMallocSizeOf(this);
|
|
|
|
|
|
|
|
if (m_databaseLoader) {
|
|
|
|
m_databaseLoader->sizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
amount += m_convolverL1.sizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += m_convolverR1.sizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += m_convolverL2.sizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += m_convolverR2.sizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += m_delayLine.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += m_tempL1.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += m_tempL2.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += m_tempR1.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += m_tempR2.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2013-08-08 02:37:36 -07:00
|
|
|
void HRTFPanner::reset()
|
|
|
|
{
|
2013-10-24 18:05:42 -07:00
|
|
|
m_azimuthIndex1 = UninitializedAzimuth;
|
|
|
|
m_azimuthIndex2 = UninitializedAzimuth;
|
|
|
|
// m_elevation1 and m_elevation2 are initialized in pan()
|
|
|
|
m_crossfadeSelection = CrossfadeSelection1;
|
|
|
|
m_crossfadeX = 0.0f;
|
|
|
|
m_crossfadeIncr = 0.0f;
|
2013-08-08 02:37:36 -07:00
|
|
|
m_convolverL1.reset();
|
|
|
|
m_convolverR1.reset();
|
|
|
|
m_convolverL2.reset();
|
|
|
|
m_convolverR2.reset();
|
2014-03-04 13:06:57 -08:00
|
|
|
m_delayLine.Reset();
|
2013-08-08 02:37:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int HRTFPanner::calculateDesiredAzimuthIndexAndBlend(double azimuth, double& azimuthBlend)
|
|
|
|
{
|
|
|
|
// Convert the azimuth angle from the range -180 -> +180 into the range 0 -> 360.
|
|
|
|
// The azimuth index may then be calculated from this positive value.
|
|
|
|
if (azimuth < 0)
|
|
|
|
azimuth += 360.0;
|
|
|
|
|
|
|
|
HRTFDatabase* database = m_databaseLoader->database();
|
2013-08-08 15:07:49 -07:00
|
|
|
MOZ_ASSERT(database);
|
2013-08-08 02:37:36 -07:00
|
|
|
|
|
|
|
int numberOfAzimuths = database->numberOfAzimuths();
|
|
|
|
const double angleBetweenAzimuths = 360.0 / numberOfAzimuths;
|
|
|
|
|
|
|
|
// Calculate the azimuth index and the blend (0 -> 1) for interpolation.
|
|
|
|
double desiredAzimuthIndexFloat = azimuth / angleBetweenAzimuths;
|
|
|
|
int desiredAzimuthIndex = static_cast<int>(desiredAzimuthIndexFloat);
|
|
|
|
azimuthBlend = desiredAzimuthIndexFloat - static_cast<double>(desiredAzimuthIndex);
|
|
|
|
|
|
|
|
// We don't immediately start using this azimuth index, but instead approach this index from the last index we rendered at.
|
|
|
|
// This minimizes the clicks and graininess for moving sources which occur otherwise.
|
|
|
|
desiredAzimuthIndex = max(0, desiredAzimuthIndex);
|
|
|
|
desiredAzimuthIndex = min(numberOfAzimuths - 1, desiredAzimuthIndex);
|
|
|
|
return desiredAzimuthIndex;
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:49:45 -08:00
|
|
|
void HRTFPanner::pan(double desiredAzimuth, double elevation, const AudioChunk* inputBus, AudioChunk* outputBus)
|
2013-08-08 02:37:36 -07:00
|
|
|
{
|
2014-03-17 20:11:24 -07:00
|
|
|
#ifdef DEBUG
|
2013-08-08 15:07:49 -07:00
|
|
|
unsigned numInputChannels =
|
|
|
|
inputBus->IsNull() ? 0 : inputBus->mChannelData.Length();
|
2013-08-08 02:37:36 -07:00
|
|
|
|
2013-09-04 12:44:35 -07:00
|
|
|
MOZ_ASSERT(numInputChannels <= 2);
|
2014-03-02 15:49:45 -08:00
|
|
|
MOZ_ASSERT(inputBus->mDuration == WEBAUDIO_BLOCK_SIZE);
|
2014-03-17 20:11:24 -07:00
|
|
|
#endif
|
2013-08-08 02:37:36 -07:00
|
|
|
|
2014-03-02 15:49:45 -08:00
|
|
|
bool isOutputGood = outputBus && outputBus->mChannelData.Length() == 2 && outputBus->mDuration == WEBAUDIO_BLOCK_SIZE;
|
2013-08-08 15:07:49 -07:00
|
|
|
MOZ_ASSERT(isOutputGood);
|
2013-08-08 02:37:36 -07:00
|
|
|
|
2013-09-04 12:44:35 -07:00
|
|
|
if (!isOutputGood) {
|
2013-08-08 02:37:36 -07:00
|
|
|
if (outputBus)
|
2013-08-08 15:07:49 -07:00
|
|
|
outputBus->SetNull(outputBus->mDuration);
|
2013-08-08 02:37:36 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRTFDatabase* database = m_databaseLoader->database();
|
2013-08-08 15:08:06 -07:00
|
|
|
if (!database) { // not yet loaded
|
2013-08-08 15:07:49 -07:00
|
|
|
outputBus->SetNull(outputBus->mDuration);
|
2013-08-08 02:37:36 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IRCAM HRTF azimuths values from the loaded database is reversed from the panner's notion of azimuth.
|
|
|
|
double azimuth = -desiredAzimuth;
|
|
|
|
|
|
|
|
bool isAzimuthGood = azimuth >= -180.0 && azimuth <= 180.0;
|
2013-08-08 15:07:49 -07:00
|
|
|
MOZ_ASSERT(isAzimuthGood);
|
2013-08-08 02:37:36 -07:00
|
|
|
if (!isAzimuthGood) {
|
2013-08-08 15:07:49 -07:00
|
|
|
outputBus->SetNull(outputBus->mDuration);
|
2013-08-08 02:37:36 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normally, we'll just be dealing with mono sources.
|
|
|
|
// If we have a stereo input, implement stereo panning with left source processed by left HRTF, and right source by right HRTF.
|
|
|
|
|
2014-03-04 13:06:57 -08:00
|
|
|
// Get destination pointers.
|
2013-08-08 15:07:49 -07:00
|
|
|
float* destinationL =
|
|
|
|
static_cast<float*>(const_cast<void*>(outputBus->mChannelData[0]));
|
|
|
|
float* destinationR =
|
|
|
|
static_cast<float*>(const_cast<void*>(outputBus->mChannelData[1]));
|
2013-08-08 02:37:36 -07:00
|
|
|
|
|
|
|
double azimuthBlend;
|
|
|
|
int desiredAzimuthIndex = calculateDesiredAzimuthIndexAndBlend(azimuth, azimuthBlend);
|
|
|
|
|
|
|
|
// Initially snap azimuth and elevation values to first values encountered.
|
|
|
|
if (m_azimuthIndex1 == UninitializedAzimuth) {
|
|
|
|
m_azimuthIndex1 = desiredAzimuthIndex;
|
|
|
|
m_elevation1 = elevation;
|
|
|
|
}
|
|
|
|
if (m_azimuthIndex2 == UninitializedAzimuth) {
|
|
|
|
m_azimuthIndex2 = desiredAzimuthIndex;
|
|
|
|
m_elevation2 = elevation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cross-fade / transition over a period of around 45 milliseconds.
|
|
|
|
// This is an empirical value tuned to be a reasonable trade-off between
|
|
|
|
// smoothness and speed.
|
|
|
|
const double fadeFrames = sampleRate() <= 48000 ? 2048 : 4096;
|
|
|
|
|
|
|
|
// Check for azimuth and elevation changes, initiating a cross-fade if needed.
|
|
|
|
if (!m_crossfadeX && m_crossfadeSelection == CrossfadeSelection1) {
|
|
|
|
if (desiredAzimuthIndex != m_azimuthIndex1 || elevation != m_elevation1) {
|
|
|
|
// Cross-fade from 1 -> 2
|
|
|
|
m_crossfadeIncr = 1 / fadeFrames;
|
|
|
|
m_azimuthIndex2 = desiredAzimuthIndex;
|
|
|
|
m_elevation2 = elevation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m_crossfadeX == 1 && m_crossfadeSelection == CrossfadeSelection2) {
|
|
|
|
if (desiredAzimuthIndex != m_azimuthIndex2 || elevation != m_elevation2) {
|
|
|
|
// Cross-fade from 2 -> 1
|
|
|
|
m_crossfadeIncr = -1 / fadeFrames;
|
|
|
|
m_azimuthIndex1 = desiredAzimuthIndex;
|
|
|
|
m_elevation1 = elevation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:49:45 -08:00
|
|
|
// Get the HRTFKernels and interpolated delays.
|
|
|
|
HRTFKernel* kernelL1;
|
|
|
|
HRTFKernel* kernelR1;
|
|
|
|
HRTFKernel* kernelL2;
|
|
|
|
HRTFKernel* kernelR2;
|
|
|
|
double frameDelayL1;
|
|
|
|
double frameDelayR1;
|
|
|
|
double frameDelayL2;
|
|
|
|
double frameDelayR2;
|
|
|
|
database->getKernelsFromAzimuthElevation(azimuthBlend, m_azimuthIndex1, m_elevation1, kernelL1, kernelR1, frameDelayL1, frameDelayR1);
|
|
|
|
database->getKernelsFromAzimuthElevation(azimuthBlend, m_azimuthIndex2, m_elevation2, kernelL2, kernelR2, frameDelayL2, frameDelayR2);
|
|
|
|
|
|
|
|
bool areKernelsGood = kernelL1 && kernelR1 && kernelL2 && kernelR2;
|
|
|
|
MOZ_ASSERT(areKernelsGood);
|
|
|
|
if (!areKernelsGood) {
|
|
|
|
outputBus->SetNull(outputBus->mDuration);
|
|
|
|
return;
|
|
|
|
}
|
2013-08-08 02:37:36 -07:00
|
|
|
|
2014-03-02 15:49:45 -08:00
|
|
|
MOZ_ASSERT(frameDelayL1 / sampleRate() < MaxDelayTimeSeconds && frameDelayR1 / sampleRate() < MaxDelayTimeSeconds);
|
|
|
|
MOZ_ASSERT(frameDelayL2 / sampleRate() < MaxDelayTimeSeconds && frameDelayR2 / sampleRate() < MaxDelayTimeSeconds);
|
|
|
|
|
|
|
|
// Crossfade inter-aural delays based on transitions.
|
2014-03-04 13:06:57 -08:00
|
|
|
double frameDelaysL[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
double frameDelaysR[WEBAUDIO_BLOCK_SIZE];
|
|
|
|
{
|
|
|
|
float x = m_crossfadeX;
|
|
|
|
float incr = m_crossfadeIncr;
|
|
|
|
for (unsigned i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
|
|
|
|
frameDelaysL[i] = (1 - x) * frameDelayL1 + x * frameDelayL2;
|
|
|
|
frameDelaysR[i] = (1 - x) * frameDelayR1 + x * frameDelayR2;
|
|
|
|
x += incr;
|
|
|
|
}
|
|
|
|
}
|
2014-03-02 15:49:45 -08:00
|
|
|
|
|
|
|
// First run through delay lines for inter-aural time difference.
|
2014-03-04 13:06:57 -08:00
|
|
|
m_delayLine.Write(*inputBus);
|
|
|
|
// "Speakers" means a mono input is read into both outputs (with possibly
|
|
|
|
// different delays).
|
|
|
|
m_delayLine.ReadChannel(frameDelaysL, outputBus, 0,
|
|
|
|
ChannelInterpretation::Speakers);
|
|
|
|
m_delayLine.ReadChannel(frameDelaysR, outputBus, 1,
|
|
|
|
ChannelInterpretation::Speakers);
|
|
|
|
m_delayLine.NextBlock();
|
2014-03-02 15:49:45 -08:00
|
|
|
|
|
|
|
bool needsCrossfading = m_crossfadeIncr;
|
|
|
|
|
|
|
|
// Have the convolvers render directly to the final destination if we're not cross-fading.
|
|
|
|
float* convolutionDestinationL1 = needsCrossfading ? m_tempL1.Elements() : destinationL;
|
|
|
|
float* convolutionDestinationR1 = needsCrossfading ? m_tempR1.Elements() : destinationR;
|
|
|
|
float* convolutionDestinationL2 = needsCrossfading ? m_tempL2.Elements() : destinationL;
|
|
|
|
float* convolutionDestinationR2 = needsCrossfading ? m_tempR2.Elements() : destinationR;
|
|
|
|
|
|
|
|
// Now do the convolutions.
|
|
|
|
// Note that we avoid doing convolutions on both sets of convolvers if we're not currently cross-fading.
|
|
|
|
|
|
|
|
if (m_crossfadeSelection == CrossfadeSelection1 || needsCrossfading) {
|
|
|
|
m_convolverL1.process(kernelL1->fftFrame(), destinationL, convolutionDestinationL1, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
m_convolverR1.process(kernelR1->fftFrame(), destinationR, convolutionDestinationR1, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_crossfadeSelection == CrossfadeSelection2 || needsCrossfading) {
|
|
|
|
m_convolverL2.process(kernelL2->fftFrame(), destinationL, convolutionDestinationL2, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
m_convolverR2.process(kernelR2->fftFrame(), destinationR, convolutionDestinationR2, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
}
|
2013-08-08 02:37:36 -07:00
|
|
|
|
2014-03-02 15:49:45 -08:00
|
|
|
if (needsCrossfading) {
|
|
|
|
// Apply linear cross-fade.
|
|
|
|
float x = m_crossfadeX;
|
|
|
|
float incr = m_crossfadeIncr;
|
|
|
|
for (unsigned i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
|
|
|
|
destinationL[i] = (1 - x) * convolutionDestinationL1[i] + x * convolutionDestinationL2[i];
|
|
|
|
destinationR[i] = (1 - x) * convolutionDestinationR1[i] + x * convolutionDestinationR2[i];
|
|
|
|
x += incr;
|
2013-08-08 02:37:36 -07:00
|
|
|
}
|
2014-03-02 15:49:45 -08:00
|
|
|
// Update cross-fade value from local.
|
|
|
|
m_crossfadeX = x;
|
|
|
|
|
|
|
|
if (m_crossfadeIncr > 0 && fabs(m_crossfadeX - 1) < m_crossfadeIncr) {
|
|
|
|
// We've fully made the crossfade transition from 1 -> 2.
|
|
|
|
m_crossfadeSelection = CrossfadeSelection2;
|
|
|
|
m_crossfadeX = 1;
|
|
|
|
m_crossfadeIncr = 0;
|
|
|
|
} else if (m_crossfadeIncr < 0 && fabs(m_crossfadeX) < -m_crossfadeIncr) {
|
|
|
|
// We've fully made the crossfade transition from 2 -> 1.
|
|
|
|
m_crossfadeSelection = CrossfadeSelection1;
|
|
|
|
m_crossfadeX = 0;
|
|
|
|
m_crossfadeIncr = 0;
|
2013-08-08 02:37:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 18:05:43 -07:00
|
|
|
int HRTFPanner::maxTailFrames() const
|
2013-08-08 02:37:36 -07:00
|
|
|
{
|
2013-10-24 18:05:43 -07:00
|
|
|
// Although the ideal tail time would be the length of the impulse
|
|
|
|
// response, there is additional tail time from the approximations in the
|
|
|
|
// implementation. Because HRTFPanner is implemented with a DelayKernel
|
|
|
|
// and a FFTConvolver, the tailTime of the HRTFPanner is the sum of the
|
|
|
|
// tailTime of the DelayKernel and the tailTime of the FFTConvolver.
|
|
|
|
// The FFTConvolver has a tail time of fftSize(), including latency of
|
|
|
|
// fftSize()/2.
|
2014-03-04 13:06:57 -08:00
|
|
|
return m_delayLine.MaxDelayTicks() + fftSize();
|
2013-08-08 02:37:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WebCore
|