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
|
2016-03-18 23:24:02 +03:00
|
|
|
#ifdef USE_SSE2
|
|
|
|
#include "AudioNodeEngineSSE2.h"
|
|
|
|
#endif
|
2013-01-14 02:46:57 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-08-27 11:35:43 +03:00
|
|
|
already_AddRefed<ThreadSharedFloatArrayBufferList>
|
|
|
|
ThreadSharedFloatArrayBufferList::Create(uint32_t aChannelCount,
|
|
|
|
size_t aLength,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ThreadSharedFloatArrayBufferList> buffer =
|
2015-08-27 11:35:43 +03:00
|
|
|
new ThreadSharedFloatArrayBufferList(aChannelCount);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aChannelCount; ++i) {
|
|
|
|
float* channelData = js_pod_malloc<float>(aLength);
|
|
|
|
if (!channelData) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->SetData(i, channelData, js_free, channelData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.forget();
|
|
|
|
}
|
|
|
|
|
2013-01-14 02:46:57 +04:00
|
|
|
void
|
2015-09-03 10:01:50 +03:00
|
|
|
WriteZeroesToAudioBlock(AudioBlock* aChunk,
|
|
|
|
uint32_t aStart, uint32_t aLength)
|
2013-01-14 02:46:57 +04:00
|
|
|
{
|
|
|
|
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;
|
2015-07-22 08:59:21 +03:00
|
|
|
|
2015-09-03 08:30:16 +03:00
|
|
|
for (uint32_t i = 0; i < aChunk->ChannelCount(); ++i) {
|
2015-07-22 08:59:21 +03:00
|
|
|
PodZero(aChunk->ChannelFloatsForWrite(i) + aStart, aLength);
|
2013-01-14 02:46:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2016-03-18 23:24:02 +03:00
|
|
|
|
|
|
|
#ifdef USE_SSE2
|
|
|
|
if (mozilla::supports_sse2()) {
|
|
|
|
AudioBufferAddWithScale_SSE(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
|
2016-03-18 23:24:02 +03:00
|
|
|
|
|
|
|
#ifdef USE_SSE2
|
|
|
|
if (mozilla::supports_sse2()) {
|
|
|
|
AudioBlockCopyChannelWithScale_SSE(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)
|
|
|
|
{
|
2016-04-14 15:57:21 +03:00
|
|
|
|
|
|
|
#ifdef USE_SSE2
|
|
|
|
if (mozilla::supports_sse()) {
|
|
|
|
BufferComplexMultiply_SSE(aInput, aScale, aOutput, aSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-11 00:08:21 +04:00
|
|
|
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
|
2016-03-18 23:24:02 +03:00
|
|
|
|
|
|
|
#ifdef USE_SSE2
|
|
|
|
if (mozilla::supports_sse2()) {
|
|
|
|
AudioBlockCopyChannelWithScale_SSE(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
|
2016-03-18 23:24:02 +03:00
|
|
|
|
|
|
|
#ifdef USE_SSE2
|
|
|
|
if (mozilla::supports_sse2()) {
|
|
|
|
AudioBufferInPlaceScale_SSE(aBlock, aScale, aSize);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 20:15:13 +03:00
|
|
|
void
|
|
|
|
AudioBlockPanMonoToStereo(const float aInput[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aGainL[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aGainR[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aOutputL[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aOutputR[WEBAUDIO_BLOCK_SIZE])
|
|
|
|
{
|
|
|
|
AudioBlockCopyChannelWithScale(aInput, aGainL, aOutputL);
|
|
|
|
AudioBlockCopyChannelWithScale(aInput, aGainR, aOutputR);
|
|
|
|
}
|
|
|
|
|
2013-03-21 20:45:53 +04:00
|
|
|
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
|
|
|
|
|
2016-03-18 23:24:02 +03:00
|
|
|
#ifdef USE_SSE2
|
|
|
|
if (mozilla::supports_sse2()) {
|
|
|
|
AudioBlockPanStereoToStereo_SSE(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) {
|
2014-11-19 20:15:13 +03:00
|
|
|
aOutputL[i] = aInputL[i] + aInputR[i] * aGainL;
|
|
|
|
aOutputR[i] = aInputR[i] * aGainR;
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
|
2014-11-19 20:15:13 +03:00
|
|
|
aOutputL[i] = aInputL[i] * aGainL;
|
|
|
|
aOutputR[i] = aInputR[i] + aInputL[i] * aGainR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBlockPanStereoToStereo(const float aInputL[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
const float aInputR[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aGainL[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aGainR[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
bool aIsOnTheLeft[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aOutputL[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
float aOutputR[WEBAUDIO_BLOCK_SIZE])
|
|
|
|
{
|
|
|
|
#ifdef BUILD_ARM_NEON
|
2015-12-17 21:15:54 +03:00
|
|
|
if (mozilla::supports_neon()) {
|
|
|
|
AudioBlockPanStereoToStereo_NEON(aInputL, aInputR,
|
|
|
|
aGainL, aGainR, aIsOnTheLeft,
|
|
|
|
aOutputL, aOutputR);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-19 20:15:13 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < WEBAUDIO_BLOCK_SIZE; i++) {
|
|
|
|
if (aIsOnTheLeft[i]) {
|
|
|
|
aOutputL[i] = aInputL[i] + aInputR[i] * aGainL[i];
|
|
|
|
aOutputR[i] = aInputR[i] * aGainR[i];
|
|
|
|
} else {
|
|
|
|
aOutputL[i] = aInputL[i] * aGainL[i];
|
|
|
|
aOutputR[i] = aInputR[i] + aInputL[i] * aGainR[i];
|
2013-03-21 20:45:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 00:09:01 +04:00
|
|
|
|
|
|
|
float
|
|
|
|
AudioBufferSumOfSquares(const float* aInput, uint32_t aLength)
|
|
|
|
{
|
|
|
|
float sum = 0.0f;
|
2016-04-14 15:57:21 +03:00
|
|
|
|
|
|
|
#ifdef USE_SSE2
|
|
|
|
if (mozilla::supports_sse()) {
|
|
|
|
const float* alignedInput = ALIGNED16(aInput);
|
|
|
|
float vLength = (aLength >> 4) << 4;
|
|
|
|
|
|
|
|
// use scalar operations for any unaligned data at the beginning
|
|
|
|
while (aInput != alignedInput) {
|
|
|
|
sum += *aInput * *aInput;
|
|
|
|
++aInput;
|
|
|
|
}
|
|
|
|
|
|
|
|
sum += AudioBufferSumOfSquares_SSE(alignedInput, vLength);
|
|
|
|
|
|
|
|
// adjust aInput and aLength to use scalar operations for any
|
|
|
|
// remaining values
|
|
|
|
aInput = alignedInput + 1;
|
|
|
|
aLength -= vLength;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-11 00:09:01 +04:00
|
|
|
while (aLength--) {
|
|
|
|
sum += *aInput * *aInput;
|
|
|
|
++aInput;
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2015-09-03 10:01:50 +03:00
|
|
|
void
|
|
|
|
AudioNodeEngine::ProcessBlock(AudioNodeStream* aStream,
|
2015-09-18 08:05:25 +03:00
|
|
|
GraphTime aFrom,
|
2015-09-03 10:01:50 +03:00
|
|
|
const AudioBlock& aInput,
|
|
|
|
AudioBlock* aOutput,
|
|
|
|
bool* aFinished)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInputCount <= 1 && mOutputCount <= 1);
|
|
|
|
*aOutput = aInput;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeEngine::ProcessBlocksOnPorts(AudioNodeStream* aStream,
|
|
|
|
const OutputChunks& aInput,
|
|
|
|
OutputChunks& aOutput,
|
|
|
|
bool* aFinished)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInputCount > 1 || mOutputCount > 1);
|
|
|
|
// Only produce one output port, and drop all other input ports.
|
|
|
|
aOutput[0] = aInput[0];
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|