2013-01-14 02:46:57 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "AudioNodeEngine.h"
|
2013-08-13 13:30:18 +04:00
|
|
|
#ifdef BUILD_ARM_NEON
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "mozilla/arm.h"
|
2013-08-13 13:30:18 +04:00
|
|
|
#include "AudioNodeEngineNEON.h"
|
|
|
|
#endif
|
2013-01-14 02:46:57 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
void
|
|
|
|
AllocateAudioBlock(uint32_t aChannelCount, AudioChunk* aChunk)
|
|
|
|
{
|
|
|
|
// XXX for SIMD purposes we should do something here to make sure the
|
|
|
|
// channel buffers are 16-byte aligned.
|
|
|
|
nsRefPtr<SharedBuffer> buffer =
|
|
|
|
SharedBuffer::Create(WEBAUDIO_BLOCK_SIZE*aChannelCount*sizeof(float));
|
|
|
|
aChunk->mDuration = WEBAUDIO_BLOCK_SIZE;
|
|
|
|
aChunk->mChannelData.SetLength(aChannelCount);
|
|
|
|
float* data = static_cast<float*>(buffer->Data());
|
|
|
|
for (uint32_t i = 0; i < aChannelCount; ++i) {
|
|
|
|
aChunk->mChannelData[i] = data + i*WEBAUDIO_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
aChunk->mBuffer = buffer.forget();
|
|
|
|
aChunk->mVolume = 1.0f;
|
|
|
|
aChunk->mBufferFormat = AUDIO_FORMAT_FLOAT32;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WriteZeroesToAudioBlock(AudioChunk* aChunk, uint32_t aStart, uint32_t aLength)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStart + aLength <= WEBAUDIO_BLOCK_SIZE);
|
2013-04-08 19:20:46 +04:00
|
|
|
MOZ_ASSERT(!aChunk->IsNull(), "You should pass a non-null chunk");
|
2013-01-14 02:46:57 +04:00
|
|
|
if (aLength == 0)
|
|
|
|
return;
|
|
|
|
for (uint32_t i = 0; i < aChunk->mChannelData.Length(); ++i) {
|
|
|
|
memset(static_cast<float*>(const_cast<void*>(aChunk->mChannelData[i])) + aStart,
|
|
|
|
0, aLength*sizeof(float));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 05:35:22 +04:00
|
|
|
void AudioBufferCopyWithScale(const float* aInput,
|
|
|
|
float aScale,
|
|
|
|
float* aOutput,
|
|
|
|
uint32_t aSize)
|
|
|
|
{
|
|
|
|
if (aScale == 1.0f) {
|
|
|
|
PodCopy(aOutput, aInput, aSize);
|
|
|
|
} else {
|
|
|
|
for (uint32_t i = 0; i < aSize; ++i) {
|
|
|
|
aOutput[i] = aInput[i]*aScale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 00:09:01 +04:00
|
|
|
void AudioBufferAddWithScale(const float* aInput,
|
|
|
|
float aScale,
|
|
|
|
float* aOutput,
|
|
|
|
uint32_t aSize)
|
2013-01-14 02:46:57 +04:00
|
|
|
{
|
2013-08-13 13:30:18 +04:00
|
|
|
#ifdef BUILD_ARM_NEON
|
|
|
|
if (mozilla::supports_neon()) {
|
|
|
|
AudioBufferAddWithScale_NEON(aInput, aScale, aOutput, aSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-14 02:46:57 +04:00
|
|
|
if (aScale == 1.0f) {
|
2013-06-11 00:09:01 +04:00
|
|
|
for (uint32_t i = 0; i < aSize; ++i) {
|
2013-01-14 02:46:57 +04:00
|
|
|
aOutput[i] += aInput[i];
|
|
|
|
}
|
|
|
|
} else {
|
2013-06-11 00:09:01 +04:00
|
|
|
for (uint32_t i = 0; i < aSize; ++i) {
|
2013-01-14 02:46:57 +04:00
|
|
|
aOutput[i] += aInput[i]*aScale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 00:09:01 +04:00
|
|
|
void
|
|
|
|
AudioBlockAddChannelWithScale(const float aInput[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aScale,
|
|
|
|
float aOutput[WEBAUDIO_BLOCK_SIZE])
|
|
|
|
{
|
|
|
|
AudioBufferAddWithScale(aInput, aScale, aOutput, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2013-01-14 02:46:57 +04:00
|
|
|
void
|
2013-04-23 05:59:51 +04:00
|
|
|
AudioBlockCopyChannelWithScale(const float* aInput,
|
2013-01-14 02:46:57 +04:00
|
|
|
float aScale,
|
2013-04-23 05:59:51 +04:00
|
|
|
float* aOutput)
|
2013-01-14 02:46:57 +04:00
|
|
|
{
|
|
|
|
if (aScale == 1.0f) {
|
|
|
|
memcpy(aOutput, aInput, WEBAUDIO_BLOCK_SIZE*sizeof(float));
|
|
|
|
} else {
|
2013-08-13 13:30:18 +04:00
|
|
|
#ifdef BUILD_ARM_NEON
|
|
|
|
if (mozilla::supports_neon()) {
|
|
|
|
AudioBlockCopyChannelWithScale_NEON(aInput, aScale, aOutput);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-14 02:46:57 +04:00
|
|
|
for (uint32_t i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
|
|
|
|
aOutput[i] = aInput[i]*aScale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 00:08:21 +04:00
|
|
|
void
|
|
|
|
BufferComplexMultiply(const float* aInput,
|
|
|
|
const float* aScale,
|
|
|
|
float* aOutput,
|
|
|
|
uint32_t aSize)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < aSize * 2; i += 2) {
|
|
|
|
float real1 = aInput[i];
|
|
|
|
float imag1 = aInput[i + 1];
|
|
|
|
float real2 = aScale[i];
|
|
|
|
float imag2 = aScale[i + 1];
|
|
|
|
float realResult = real1 * real2 - imag1 * imag2;
|
|
|
|
float imagResult = real1 * imag2 + imag1 * real2;
|
|
|
|
aOutput[i] = realResult;
|
|
|
|
aOutput[i + 1] = imagResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 21:54:17 +04:00
|
|
|
float
|
|
|
|
AudioBufferPeakValue(const float *aInput, uint32_t aSize)
|
|
|
|
{
|
|
|
|
float max = 0.0f;
|
|
|
|
for (uint32_t i = 0; i < aSize; i++) {
|
|
|
|
float mag = fabs(aInput[i]);
|
|
|
|
if (mag > max) {
|
|
|
|
max = mag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2013-01-30 03:37:51 +04:00
|
|
|
void
|
|
|
|
AudioBlockCopyChannelWithScale(const float aInput[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
const float aScale[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aOutput[WEBAUDIO_BLOCK_SIZE])
|
|
|
|
{
|
2013-08-13 13:30:18 +04:00
|
|
|
#ifdef BUILD_ARM_NEON
|
|
|
|
if (mozilla::supports_neon()) {
|
|
|
|
AudioBlockCopyChannelWithScale_NEON(aInput, aScale, aOutput);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-30 03:37:51 +04:00
|
|
|
for (uint32_t i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
|
|
|
|
aOutput[i] = aInput[i]*aScale[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 20:45:53 +04:00
|
|
|
void
|
2014-01-08 07:57:26 +04:00
|
|
|
AudioBlockInPlaceScale(float aBlock[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aScale)
|
2013-06-11 00:09:01 +04:00
|
|
|
{
|
2014-01-28 09:14:25 +04:00
|
|
|
AudioBufferInPlaceScale(aBlock, aScale, WEBAUDIO_BLOCK_SIZE);
|
2013-06-11 00:09:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBufferInPlaceScale(float* aBlock,
|
|
|
|
float aScale,
|
|
|
|
uint32_t aSize)
|
2013-03-21 20:45:53 +04:00
|
|
|
{
|
|
|
|
if (aScale == 1.0f) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-13 13:30:18 +04:00
|
|
|
#ifdef BUILD_ARM_NEON
|
|
|
|
if (mozilla::supports_neon()) {
|
2014-01-08 07:57:53 +04:00
|
|
|
AudioBufferInPlaceScale_NEON(aBlock, aScale, aSize);
|
2013-08-13 13:30:18 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-08 07:57:53 +04:00
|
|
|
for (uint32_t i = 0; i < aSize; ++i) {
|
2013-03-21 20:45:53 +04:00
|
|
|
*aBlock++ *= aScale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBlockPanMonoToStereo(const float aInput[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aGainL, float aGainR,
|
|
|
|
float aOutputL[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aOutputR[WEBAUDIO_BLOCK_SIZE])
|
|
|
|
{
|
|
|
|
AudioBlockCopyChannelWithScale(aInput, aGainL, aOutputL);
|
|
|
|
AudioBlockCopyChannelWithScale(aInput, aGainR, aOutputR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBlockPanStereoToStereo(const float aInputL[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
const float aInputR[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aGainL, float aGainR, bool aIsOnTheLeft,
|
|
|
|
float aOutputL[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aOutputR[WEBAUDIO_BLOCK_SIZE])
|
|
|
|
{
|
2013-08-13 13:30:18 +04:00
|
|
|
#ifdef BUILD_ARM_NEON
|
|
|
|
if (mozilla::supports_neon()) {
|
|
|
|
AudioBlockPanStereoToStereo_NEON(aInputL, aInputR,
|
|
|
|
aGainL, aGainR, aIsOnTheLeft,
|
|
|
|
aOutputL, aOutputR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-21 20:45:53 +04:00
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
if (aIsOnTheLeft) {
|
|
|
|
for (i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
|
2013-06-06 20:36:57 +04:00
|
|
|
*aOutputL++ = *aInputL++ + *aInputR * aGainL;
|
2013-03-21 20:45:53 +04:00
|
|
|
*aOutputR++ = *aInputR++ * aGainR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
|
2013-06-06 20:36:57 +04:00
|
|
|
*aOutputL++ = *aInputL * aGainL;
|
2013-03-21 20:45:53 +04:00
|
|
|
*aOutputR++ = *aInputR++ + *aInputL++ * aGainR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 00:09:01 +04:00
|
|
|
|
|
|
|
float
|
|
|
|
AudioBufferSumOfSquares(const float* aInput, uint32_t aLength)
|
|
|
|
{
|
|
|
|
float sum = 0.0f;
|
|
|
|
while (aLength--) {
|
|
|
|
sum += *aInput * *aInput;
|
|
|
|
++aInput;
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2013-01-14 02:46:57 +04:00
|
|
|
}
|