2013-04-01 07:41:14 +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 "mozilla/dom/AnalyserNode.h"
|
|
|
|
#include "mozilla/dom/AnalyserNodeBinding.h"
|
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeStream.h"
|
2013-04-02 01:35:18 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2013-06-07 15:33:00 +04:00
|
|
|
#include "mozilla/PodOperations.h"
|
2013-04-01 07:41:14 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2015-07-22 10:50:37 +03:00
|
|
|
|
|
|
|
static const uint32_t MAX_FFT_SIZE = 32768;
|
|
|
|
static const size_t CHUNK_COUNT = MAX_FFT_SIZE >> WEBAUDIO_BLOCK_SIZE_BITS;
|
|
|
|
static_assert(MAX_FFT_SIZE == CHUNK_COUNT * WEBAUDIO_BLOCK_SIZE,
|
|
|
|
"MAX_FFT_SIZE must be a multiple of WEBAUDIO_BLOCK_SIZE");
|
|
|
|
static_assert((CHUNK_COUNT & (CHUNK_COUNT - 1)) == 0,
|
|
|
|
"CHUNK_COUNT must be power of 2 for remainder behavior");
|
|
|
|
|
2013-04-01 07:41:14 +04:00
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(AnalyserNode, AudioNode)
|
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
class AnalyserNodeEngine final : public AudioNodeEngine
|
2013-04-01 07:41:14 +04:00
|
|
|
{
|
2015-04-28 09:42:00 +03:00
|
|
|
class TransferBuffer final : public nsRunnable
|
2013-04-03 00:18:32 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-04-20 20:16:28 +04:00
|
|
|
TransferBuffer(AudioNodeStream* aStream,
|
2013-04-03 00:18:32 +04:00
|
|
|
const AudioChunk& aChunk)
|
2013-04-20 20:16:28 +04:00
|
|
|
: mStream(aStream)
|
2013-04-03 00:18:32 +04:00
|
|
|
, mChunk(aChunk)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2015-07-14 12:57:01 +03:00
|
|
|
nsRefPtr<AnalyserNode> node =
|
|
|
|
static_cast<AnalyserNode*>(mStream->Engine()->NodeMainThread());
|
2013-04-20 20:16:28 +04:00
|
|
|
if (node) {
|
|
|
|
node->AppendChunk(mChunk);
|
|
|
|
}
|
2013-04-03 00:18:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-04-20 20:16:28 +04:00
|
|
|
nsRefPtr<AudioNodeStream> mStream;
|
2013-04-03 00:18:32 +04:00
|
|
|
AudioChunk mChunk;
|
|
|
|
};
|
|
|
|
|
2013-04-01 07:41:14 +04:00
|
|
|
public:
|
2013-04-20 20:16:28 +04:00
|
|
|
explicit AnalyserNodeEngine(AnalyserNode* aNode)
|
|
|
|
: AudioNodeEngine(aNode)
|
2013-04-02 01:35:18 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2014-03-05 01:09:49 +04:00
|
|
|
virtual void ProcessBlock(AudioNodeStream* aStream,
|
2015-09-03 10:01:50 +03:00
|
|
|
const AudioBlock& aInput,
|
|
|
|
AudioBlock* aOutput,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool* aFinished) override
|
2013-04-01 07:41:14 +04:00
|
|
|
{
|
|
|
|
*aOutput = aInput;
|
2013-04-02 01:35:18 +04:00
|
|
|
|
2015-09-08 23:52:39 +03:00
|
|
|
if (aInput.IsNull()) {
|
|
|
|
// If AnalyserNode::mChunks has only null chunks, then there is no need
|
|
|
|
// to send further null chunks.
|
|
|
|
if (mChunksToProcess == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
--mChunksToProcess;
|
|
|
|
} else {
|
|
|
|
// This many null chunks will be required to empty AnalyserNode::mChunks.
|
|
|
|
mChunksToProcess = CHUNK_COUNT;
|
|
|
|
}
|
|
|
|
|
2015-09-03 10:01:50 +03:00
|
|
|
nsRefPtr<TransferBuffer> transfer =
|
|
|
|
new TransferBuffer(aStream, aInput.AsAudioChunk());
|
2015-07-14 14:11:59 +03:00
|
|
|
NS_DispatchToMainThread(transfer);
|
2013-04-01 07:41:14 +04:00
|
|
|
}
|
2014-04-13 22:08:10 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
|
2014-04-13 22:08:10 +04:00
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
2015-09-08 23:52:39 +03:00
|
|
|
|
|
|
|
size_t mChunksToProcess = 0;
|
2013-04-01 07:41:14 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
AnalyserNode::AnalyserNode(AudioContext* aContext)
|
2013-04-28 02:44:50 +04:00
|
|
|
: AudioNode(aContext,
|
|
|
|
1,
|
2014-12-04 22:22:51 +03:00
|
|
|
ChannelCountMode::Max,
|
2013-04-28 02:44:50 +04:00
|
|
|
ChannelInterpretation::Speakers)
|
2013-06-11 00:08:21 +04:00
|
|
|
, mAnalysisBlock(2048)
|
2013-04-01 07:41:14 +04:00
|
|
|
, mMinDecibels(-100.)
|
|
|
|
, mMaxDecibels(-30.)
|
|
|
|
, mSmoothingTimeConstant(.8)
|
|
|
|
{
|
2015-09-08 16:22:16 +03:00
|
|
|
mStream = AudioNodeStream::Create(aContext,
|
2015-08-12 02:26:24 +03:00
|
|
|
new AnalyserNodeEngine(this),
|
2015-08-13 07:13:34 +03:00
|
|
|
AudioNodeStream::NO_STREAM_FLAGS);
|
2015-07-22 10:50:37 +03:00
|
|
|
|
|
|
|
// Enough chunks must be recorded to handle the case of fftSize being
|
|
|
|
// increased to maximum immediately before getFloatTimeDomainData() is
|
|
|
|
// called, for example.
|
2015-09-16 01:30:44 +03:00
|
|
|
unused << mChunks.SetLength(CHUNK_COUNT, fallible);
|
2015-07-22 10:50:37 +03:00
|
|
|
|
2013-04-02 01:35:18 +04:00
|
|
|
AllocateBuffer();
|
2013-04-01 07:41:14 +04:00
|
|
|
}
|
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
size_t
|
|
|
|
AnalyserNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mAnalysisBlock.SizeOfExcludingThis(aMallocSizeOf);
|
2015-07-29 09:24:24 +03:00
|
|
|
amount += mChunks.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mOutputBuffer.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-04-13 22:08:10 +04:00
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
AnalyserNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2013-04-01 07:41:14 +04:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
AnalyserNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-04-01 07:41:14 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return AnalyserNodeBinding::Wrap(aCx, this, aGivenProto);
|
2013-04-01 07:41:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AnalyserNode::SetFftSize(uint32_t aValue, ErrorResult& aRv)
|
|
|
|
{
|
2015-02-16 18:03:07 +03:00
|
|
|
// Disallow values that are not a power of 2 and outside the [32,32768] range
|
2013-04-09 05:26:08 +04:00
|
|
|
if (aValue < 32 ||
|
2015-07-22 10:50:37 +03:00
|
|
|
aValue > MAX_FFT_SIZE ||
|
2013-04-01 07:41:14 +04:00
|
|
|
(aValue & (aValue - 1)) != 0) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-11 00:08:21 +04:00
|
|
|
if (FftSize() != aValue) {
|
|
|
|
mAnalysisBlock.SetFFTSize(aValue);
|
2013-04-02 01:35:18 +04:00
|
|
|
AllocateBuffer();
|
|
|
|
}
|
2013-04-01 07:41:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AnalyserNode::SetMinDecibels(double aValue, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aValue >= mMaxDecibels) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mMinDecibels = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AnalyserNode::SetMaxDecibels(double aValue, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aValue <= mMinDecibels) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mMaxDecibels = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AnalyserNode::SetSmoothingTimeConstant(double aValue, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aValue < 0 || aValue > 1) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mSmoothingTimeConstant = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-05 21:40:01 +04:00
|
|
|
AnalyserNode::GetFloatFrequencyData(const Float32Array& aArray)
|
2013-04-01 07:41:14 +04:00
|
|
|
{
|
2013-04-02 03:26:17 +04:00
|
|
|
if (!FFTAnalysis()) {
|
|
|
|
// Might fail to allocate memory
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
aArray.ComputeLengthAndData();
|
|
|
|
|
2013-04-02 03:26:17 +04:00
|
|
|
float* buffer = aArray.Data();
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t length = std::min(size_t(aArray.Length()), mOutputBuffer.Length());
|
2013-04-02 03:26:17 +04:00
|
|
|
|
2014-05-09 05:03:35 +04:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2013-04-02 03:26:17 +04:00
|
|
|
buffer[i] = WebAudioUtils::ConvertLinearToDecibels(mOutputBuffer[i], mMinDecibels);
|
|
|
|
}
|
2013-04-01 07:41:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-05 21:40:01 +04:00
|
|
|
AnalyserNode::GetByteFrequencyData(const Uint8Array& aArray)
|
2013-04-01 07:41:14 +04:00
|
|
|
{
|
2013-04-02 03:26:17 +04:00
|
|
|
if (!FFTAnalysis()) {
|
|
|
|
// Might fail to allocate memory
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const double rangeScaleFactor = 1.0 / (mMaxDecibels - mMinDecibels);
|
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
aArray.ComputeLengthAndData();
|
|
|
|
|
2013-04-02 03:26:17 +04:00
|
|
|
unsigned char* buffer = aArray.Data();
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t length = std::min(size_t(aArray.Length()), mOutputBuffer.Length());
|
2013-04-02 03:26:17 +04:00
|
|
|
|
2014-05-09 05:03:35 +04:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2013-04-02 03:26:17 +04:00
|
|
|
const double decibels = WebAudioUtils::ConvertLinearToDecibels(mOutputBuffer[i], mMinDecibels);
|
|
|
|
// scale down the value to the range of [0, UCHAR_MAX]
|
|
|
|
const double scaled = std::max(0.0, std::min(double(UCHAR_MAX),
|
|
|
|
UCHAR_MAX * (decibels - mMinDecibels) * rangeScaleFactor));
|
|
|
|
buffer[i] = static_cast<unsigned char>(scaled);
|
|
|
|
}
|
2013-04-01 07:41:14 +04:00
|
|
|
}
|
|
|
|
|
2014-02-25 14:30:48 +04:00
|
|
|
void
|
|
|
|
AnalyserNode::GetFloatTimeDomainData(const Float32Array& aArray)
|
|
|
|
{
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
aArray.ComputeLengthAndData();
|
|
|
|
|
2014-02-25 14:30:48 +04:00
|
|
|
float* buffer = aArray.Data();
|
2015-07-22 10:50:37 +03:00
|
|
|
size_t length = std::min(aArray.Length(), FftSize());
|
2014-02-25 14:30:48 +04:00
|
|
|
|
2015-07-22 10:50:37 +03:00
|
|
|
GetTimeDomainData(buffer, length);
|
2014-02-25 14:30:48 +04:00
|
|
|
}
|
|
|
|
|
2013-04-01 07:41:14 +04:00
|
|
|
void
|
2013-08-05 21:40:01 +04:00
|
|
|
AnalyserNode::GetByteTimeDomainData(const Uint8Array& aArray)
|
2013-04-01 07:41:14 +04:00
|
|
|
{
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
aArray.ComputeLengthAndData();
|
|
|
|
|
2015-07-22 10:50:37 +03:00
|
|
|
size_t length = std::min(aArray.Length(), FftSize());
|
2013-04-02 03:26:17 +04:00
|
|
|
|
2015-07-22 10:50:37 +03:00
|
|
|
AlignedTArray<float> tmpBuffer;
|
|
|
|
if (!tmpBuffer.SetLength(length, fallible)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetTimeDomainData(tmpBuffer.Elements(), length);
|
|
|
|
|
|
|
|
unsigned char* buffer = aArray.Data();
|
2014-05-09 05:03:35 +04:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2015-07-22 10:50:37 +03:00
|
|
|
const float value = tmpBuffer[i];
|
2013-04-02 03:26:17 +04:00
|
|
|
// scale the value to the range of [0, UCHAR_MAX]
|
|
|
|
const float scaled = std::max(0.0f, std::min(float(UCHAR_MAX),
|
|
|
|
128.0f * (value + 1.0f)));
|
|
|
|
buffer[i] = static_cast<unsigned char>(scaled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AnalyserNode::FFTAnalysis()
|
|
|
|
{
|
2015-07-15 22:58:18 +03:00
|
|
|
AlignedTArray<float> tmpBuffer;
|
2015-07-22 10:50:37 +03:00
|
|
|
size_t fftSize = FftSize();
|
|
|
|
if (!tmpBuffer.SetLength(fftSize, fallible)) {
|
|
|
|
return false;
|
2013-04-02 03:26:17 +04:00
|
|
|
}
|
|
|
|
|
2015-07-22 10:50:37 +03:00
|
|
|
float* inputBuffer = tmpBuffer.Elements();
|
|
|
|
GetTimeDomainData(inputBuffer, fftSize);
|
|
|
|
ApplyBlackmanWindow(inputBuffer, fftSize);
|
2013-06-11 00:08:21 +04:00
|
|
|
mAnalysisBlock.PerformFFT(inputBuffer);
|
2013-04-02 03:26:17 +04:00
|
|
|
|
|
|
|
// Normalize so than an input sine wave at 0dBfs registers as 0dBfs (undo FFT scaling factor).
|
2015-07-22 10:50:37 +03:00
|
|
|
const double magnitudeScale = 1.0 / fftSize;
|
2013-04-02 03:26:17 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mOutputBuffer.Length(); ++i) {
|
2013-06-11 00:08:21 +04:00
|
|
|
double scalarMagnitude = NS_hypot(mAnalysisBlock.RealData(i),
|
|
|
|
mAnalysisBlock.ImagData(i)) *
|
2013-04-02 03:26:17 +04:00
|
|
|
magnitudeScale;
|
|
|
|
mOutputBuffer[i] = mSmoothingTimeConstant * mOutputBuffer[i] +
|
|
|
|
(1.0 - mSmoothingTimeConstant) * scalarMagnitude;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AnalyserNode::ApplyBlackmanWindow(float* aBuffer, uint32_t aSize)
|
|
|
|
{
|
|
|
|
double alpha = 0.16;
|
|
|
|
double a0 = 0.5 * (1.0 - alpha);
|
|
|
|
double a1 = 0.5;
|
|
|
|
double a2 = 0.5 * alpha;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aSize; ++i) {
|
|
|
|
double x = double(i) / aSize;
|
|
|
|
double window = a0 - a1 * cos(2 * M_PI * x) + a2 * cos(4 * M_PI * x);
|
|
|
|
aBuffer[i] *= window;
|
|
|
|
}
|
2013-04-01 07:41:14 +04:00
|
|
|
}
|
|
|
|
|
2013-04-02 01:35:18 +04:00
|
|
|
bool
|
|
|
|
AnalyserNode::AllocateBuffer()
|
|
|
|
{
|
|
|
|
bool result = true;
|
2015-07-22 10:50:37 +03:00
|
|
|
if (mOutputBuffer.Length() != FrequencyBinCount()) {
|
2015-07-15 22:58:15 +03:00
|
|
|
if (!mOutputBuffer.SetLength(FrequencyBinCount(), fallible)) {
|
2013-11-13 07:07:31 +04:00
|
|
|
return false;
|
2013-04-02 01:35:18 +04:00
|
|
|
}
|
2013-11-13 07:07:31 +04:00
|
|
|
memset(mOutputBuffer.Elements(), 0, sizeof(float) * FrequencyBinCount());
|
2013-04-02 01:35:18 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AnalyserNode::AppendChunk(const AudioChunk& aChunk)
|
|
|
|
{
|
2015-07-22 10:50:37 +03:00
|
|
|
if (mChunks.Length() == 0) {
|
|
|
|
return;
|
2013-06-07 15:33:00 +04:00
|
|
|
}
|
|
|
|
|
2015-07-22 10:50:37 +03:00
|
|
|
++mCurrentChunk;
|
|
|
|
mChunks[mCurrentChunk & (CHUNK_COUNT - 1)] = aChunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads into aData the oldest aLength samples of the fftSize most recent
|
|
|
|
// samples.
|
|
|
|
void
|
|
|
|
AnalyserNode::GetTimeDomainData(float* aData, size_t aLength)
|
|
|
|
{
|
|
|
|
size_t fftSize = FftSize();
|
|
|
|
MOZ_ASSERT(aLength <= fftSize);
|
|
|
|
|
|
|
|
if (mChunks.Length() == 0) {
|
|
|
|
PodZero(aData, aLength);
|
|
|
|
return;
|
2013-04-02 01:35:18 +04:00
|
|
|
}
|
2015-07-22 10:50:37 +03:00
|
|
|
|
|
|
|
size_t readChunk =
|
|
|
|
mCurrentChunk - ((fftSize - 1) >> WEBAUDIO_BLOCK_SIZE_BITS);
|
|
|
|
size_t readIndex = (0 - fftSize) & (WEBAUDIO_BLOCK_SIZE - 1);
|
|
|
|
MOZ_ASSERT(readIndex == 0 || readIndex + fftSize == WEBAUDIO_BLOCK_SIZE);
|
|
|
|
|
|
|
|
for (size_t writeIndex = 0; writeIndex < aLength; ) {
|
|
|
|
const AudioChunk& chunk = mChunks[readChunk & (CHUNK_COUNT - 1)];
|
2015-09-03 08:30:16 +03:00
|
|
|
const size_t channelCount = chunk.ChannelCount();
|
2015-07-22 10:50:37 +03:00
|
|
|
size_t copyLength =
|
|
|
|
std::min<size_t>(aLength - writeIndex, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
float* dataOut = &aData[writeIndex];
|
|
|
|
|
|
|
|
if (channelCount == 0) {
|
|
|
|
PodZero(dataOut, copyLength);
|
|
|
|
} else {
|
|
|
|
float scale = chunk.mVolume / channelCount;
|
|
|
|
{ // channel 0
|
|
|
|
auto channelData =
|
|
|
|
static_cast<const float*>(chunk.mChannelData[0]) + readIndex;
|
|
|
|
AudioBufferCopyWithScale(channelData, scale, dataOut, copyLength);
|
|
|
|
}
|
|
|
|
for (uint32_t i = 1; i < channelCount; ++i) {
|
|
|
|
auto channelData =
|
|
|
|
static_cast<const float*>(chunk.mChannelData[i]) + readIndex;
|
|
|
|
AudioBufferAddWithScale(channelData, scale, dataOut, copyLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
readChunk++;
|
|
|
|
writeIndex += copyLength;
|
2013-04-02 01:35:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-04-01 07:41:14 +04:00
|
|
|
|