Files
palemoon27/dom/media/webaudio/AudioBufferSourceNode.cpp
T
roytam1 69d1f32ff7 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1268085 - Remove unused post barrier callbacks r=terrence (0ab13411c9)
- Bug 1267699 - Move some public types to the right namespace; r=sfink (3d5008e610)
- Bug 1267550 (part 1) - Rename MOZ_MUST_USE as MOZ_MUST_USE_TYPE. r=ehsan. (6f47375796)
- Bug 1259021 - Rename Vector::extractRawBuffer to extractOrCopyRawBuffer r=Waldo (97ca94495b)
- Bug 1259021 - Add Vector::extractRawBuffer method that doesn't copy the buffer r=Waldo (e58deec48f)
- Bug 1265892 - Change Vector to use Impl::new_ consistently. r=Waldo (7a52d21b29)
- Bug 1267912 - Rename nsNetUtil.inl as nsNetUtilInlines.h. r=valentin. (548a41b293)
- Bug 1265690 part 1 - Mark StringBuffer methods WARN_UNUSED_RESULT, fix OOM issues. r=jonco (0d7e6837e3)
- Bug 1265690 part 2 - Fix some more OOM issues in TypedObject code. r=jonco (b60902453e)
- Bug 1263490 - Part 2: Add GetFirstDollarIndex intrinsic and use it inRegExpReplace. r=till (4ba19db8c4)
- Bug 1263490 - Part 3: Inline GetFirstDollarIndex intrinsic. r=h4writer (e7d9b5d1cc)
- Bug 1263490 - Part 4: Fold GetFirstDollarIndex into a integer constant. r=h4writer (3479c7d1af)
- Bug 1267269 - Make MIRType an enum class. r=bbouvier (d580ef372a)
- Bug 1259295 - BaldrMonkey: Postorder (r=luke) (6ef7a77663)
- Bug 1254142: BaldrMonkey: make br_table yield (r=luke) (80e7635e58)
- Bug 1263202 - BaldrMonkey: switch to arities on branches, calls and return (r=bbouvier) (f5a0358634)
- Bug 1236358 - Improper reading of string16 in Pickle::ReadString16. r=jld (8370ba6a0b)
- Bug 1263205 - BaldrMonkey: Update section headers for proposed spec changes (r=luke) (0def2e6bc2)
- Bug 1263205 - BaldrMonkey: Update for proposed new section names (r=luke) (e57f0e3367)
- Bug 1263205 - BaldrMonkey: Add 'form' field to types section (r=bbouvier) (794edc890f)
- Bug 1259021 - Use in-place storage in AutoStableStringChars to avoid allocation for short strings r=jandem r=Waldo (ffb53cbcf4)
- Bug 1267550 (part 2) - Rename MOZ_WARN_UNUSED_RESULT as MOZ_MUST_USE. r=froydnj. (47bc674b86)
- Bug 1268518: Baldr: implement int32/int64 rotations; r=luke (0d5eedccce)
- Bug 1255008: IonMonkey - Add a by default disabled flow sensitive alias analysis pass, r=jandem (521c585d75)
- Bug 1266781: Baldr: implement proper checked truncations to integer types; r=sunfish (46078fb3d3)
- Bug 1266781: Rename MTruncateToInt64 into MWasmTruncateInt64; r=sunfish (c7d7d1ac11)
- Bug 1266781: Add new traps; r=luke (b7ed3d44e6)
- Bug 1268024: Pass the atomic attribute down to EmitHeapAccess; r=luke (6195f7d7a3)
- Bug 1268024: A few cleanups related to loads/stores; r=luke (88141e3a01)
- Bug 1258312 - Make Pickle::Resize infallible r=jld (241ee9b60d)
- Bug 1162772, part 1 - Allow CompartmentCreationOptions to store Secure Context state. r=jorendorff (ff666384cf)
- Bug 1162772, part 2 - Expose whether SEC_FORCE_INHERIT_PRINCIPAL was dropped from an nsILoadInfo. r=bz (ada46f86bf)
- Bug 1162772, part 3 - Add a getChannelResultPrincipalIfNotSandboxed method to nsIScriptSecurityManager. r=bz (5b1d9f6807)
- Bug 1162772, part 4 - Implement nsGlobalWindow::IsSecureContext. r=bz (f392f439c9)
- Bug 1162772, part 5 - Expose Window.isSecureContext to content. r=bz (e7296e2cf1)
- Bug 1267509 - Make nsContentSecurityManager::IsURIPotentiallyTrustworthy act on an nsIPrincipal. r=bz (83de80350a)
- Bug 1219098 - Use UniquePtr in UncompressedSourceCache, for it is good (r=jandem) (b68769c729)
- Bug 1244279 - Part 1: Take a bit in ObjectElements::Flags to indicate whether the object is in the whole cell store buffer. r=terrence (968cf373f9)
- Bug 1244279 - Part 0: Add a GC ubench for large arrays with both elements and properties. r=terrence (ec76b48323)
- Bug 1255925 - Give a name to getters/setters and integer-named methods. r=efaust (f978cc6916)
- Bug 888969 - Make the getPrototypeOf/setPrototypeOf traps scriptable. r=efaust, r=bholley (eb2325a9ea)
- Bug 1267557 part 0 - Move JS poison constants to jsutil.h. r=jonco (65afc690d2)
- Bug 1267557 part 1 - Also poison bytes allocated before the actual jitcode. r=nbp (70f0b327d3)
- Bug 1267557 part 2 - Use different jitcode poison values. r=nbp (08008ab9dc)
- Bug 1267557 part 3 - Define JS_SWEPT_CODE_PATTERN for mips. r=nbp (17e894d59d)
- Bug 1267449 - Do not infinite loop in js_fputs; r=jimb (67f961b6cd)
- Bug 1219098 - Reenable compression on large sources, but revert to uncompressed if decompression happens (r=jandem) (b44ee8d77d)
- Bug 1267551 (part 1) - Use MOZ_MUST_USE more in jsnum.h. r=jonco. (d2476bf8f4)
- Bug 1267551 (part 2) - Use MOZ_MUST_USE more in js/src/ds/. r=jonco. (4ff5d9aa88)
- Bug 1267412 - Use MutableHandleValue instead of pointer-to-AutoValueVector; r=sfink (3f6dd284bb)
- Bug 1266406 - Use EnumSet<AllocKind> to simplify GC sweeping phase information r=terrence (64811500e7)
- Bug 1266457 - Update pointers in GC things in two phases when compacting r=terrence (f6f5bc4e4d)
- Bug 1266457 - Simplify typed object trace hook r=terence (3b06c8d1e5)
- Bug 1268541 - Compact arenas containing base shapes r=terrence (b458b92eea)
- Bug 1268805 - Implement PrivateGCThingValue. (r=terrence) (deec9a83ae)
- Bug 1268415: Initialize members in UpdatePointerTasks; r=jonco (6cb219005a)
- Bug 1268501 - Release the GC lock periodically when releasing arenas on the backgound thread r=terrence (37f0997682)
- Bug 1263572 - Wait for background sweeping to finish before checking base shapes r=terrence (354801a411)
- Bug 1266887 - Store Rooted heads on the Zone; r=sfink (91c0101ee3)
- Bug 1266402 - Add iteration to EnumSet<T> so that it can be used in range-based for loops r=Waldo (e9507a2524)
- Bug 1266404 - Allow construction of an EnumSet<T> using an initializer list r=Waldo (1b6d340e99)
- Bug 1254020 - Always compute theme scaling factor when per-monitor dpi aware, even if only a single display is currently present. r=emk (a00cda21f4)
- Bug 1263525 - Add dedicated function for std_Array self-hosted intrinsic. r=efaust (449d8bb7eb)
- Bug 1255925 - Change JSFunction::name to return a JSAtom. r=efaust (5ab396ce83)
- Bug 888969 - Make our tree's sole implementation of nsIRemoteTagService.getRemoteObjectTag not depend upon the infallibility of [[GetPrototypeOf]] on the object provided to it. r=bz (f388f4bf1f)
- Bug 1264896 - Kill off nsIRemoteTagService and do what it does, in its sole caller, in far-faster C++. r=billm (5ed3fb103d)
- Bug 1268246 - Add a simple Poison class lifetime checker. r=froydnj (7b237bc70e)
- Bug 1249496 - Don't apply dpi-based scaling for window titlebar dimensions when on a secondary display, because windows doesn't scale it. r=emk (64dd706dbc)
- Bug 1164518 - Avoid unnecessary DB updates when caching Safe Browsing results. r=gcp (3cafd9a4df)
- Bug 1264472 - Use nsRunnables in FIDO U2F. r=keeler (3aa9570132)
- Bug 1236060 - Dispatch error should advance queue. r=smaug (74155b75dd)
- Bug 1251697 part 1. Thread an ErrorResult reference through the worker XHR WorkerThreadProxySyncRunnable implementations. r=khuey (77804cbb7c)
- Bug 1251697 part 2. Have WorkerThreadProxySyncRunnable hand the ErrorResult reference it holds to its ResponseRunnable so it can report exceptions on there instead of on a JSContext. r=khuey (355c9ee313)
- Bug 1251697 part 3. Remove the JSContext argument of StopSyncLoopRunnable::MaybeSetException. r=khuey (010f5b1058)
- Bug 1155328. r=smaug (e1f8dac304)
- Bug 1265927: Move nsRunnable to mozilla::Runnable, CancelableRunnable to mozilla::CancelableRunnable. r=froydnj (f83bfcae02)
- Bug 1239946 - Change test to return error on Speak. r=eeejay (1d402beb02)
- Bug 1254378 - Update synth tests and introduce no voiceschanged test. r=smaug (f5823bb70e)
- Bug 1251627. Fix XMLHttpRequest.send() to follow the spec better in terms of the exceptions it throws. r=khuey (cd0e321948)
- Bug 1268868: [MSE] P1. Re-enable gap detection within a media segment. r=gerald (b8b8df4bc2)
- Bug 1268868: [MSE] P2. Reset longest duration after keyframe is seen. r=gerald (2b1401465c)
- Bug 1268868: [MSE] P3. Prevent crash should gap be detected in content. r=gerald (063d9376fc)
- Bug 1254378 - Implement nsISynthVoiceRegistry.notifyVoicesChanged. r=smaug (4b63b1c360)
- Bug 1266804 - Un-inline js::Unbox(); r=jorendorff (0f288b6173)
- Bug 1268863 - Report ScriptSources that are only reachable via AsmJSModule (r=njn) (5ba40acb64)
- bump version to 45.1b1 (1414db0ca8)
- Bug 1262062 - remove old futex names. r=bbouvier (62662bdd2e)
- memory: build fix after renaming MOZ_WARN_UNUSED_RESULT (7254dc8d53)
- import from mozilla:
 - Bug 1268725 - BaldrMonkey: Refactor away the internal storage from ExprIter. r=luke (1931bd636f17)
 - Bug 1268725 - BaldrMonkey: Convert default arguments into explicit arguments. r=luke (c8a11b8b6bbd) (867ec715d6)
2024-08-21 10:45:07 +08:00

833 lines
28 KiB
C++

/* -*- 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 "AudioBufferSourceNode.h"
#include "mozilla/dom/AudioBufferSourceNodeBinding.h"
#include "mozilla/dom/AudioParam.h"
#include "mozilla/FloatingPoint.h"
#include "nsContentUtils.h"
#include "nsMathUtils.h"
#include "AudioNodeEngine.h"
#include "AudioNodeStream.h"
#include "AudioDestinationNode.h"
#include "AudioParamTimeline.h"
#include <limits>
#include <algorithm>
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(AudioBufferSourceNode, AudioNode, mBuffer, mPlaybackRate, mDetune)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AudioBufferSourceNode)
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
NS_IMPL_ADDREF_INHERITED(AudioBufferSourceNode, AudioNode)
NS_IMPL_RELEASE_INHERITED(AudioBufferSourceNode, AudioNode)
/**
* Media-thread playback engine for AudioBufferSourceNode.
* Nothing is played until a non-null buffer has been set (via
* AudioNodeStream::SetBuffer) and a non-zero mBufferEnd has been set (via
* AudioNodeStream::SetInt32Parameter).
*/
class AudioBufferSourceNodeEngine final : public AudioNodeEngine
{
public:
AudioBufferSourceNodeEngine(AudioNode* aNode,
AudioDestinationNode* aDestination) :
AudioNodeEngine(aNode),
mStart(0.0), mBeginProcessing(0),
mStop(STREAM_TIME_MAX),
mResampler(nullptr), mRemainingResamplerTail(0),
mBufferEnd(0),
mLoopStart(0), mLoopEnd(0),
mBufferPosition(0), mBufferSampleRate(0),
// mResamplerOutRate is initialized in UpdateResampler().
mChannels(0),
mDopplerShift(1.0f),
mDestination(aDestination->Stream()),
mPlaybackRateTimeline(1.0f),
mDetuneTimeline(0.0f),
mLoop(false)
{}
~AudioBufferSourceNodeEngine()
{
if (mResampler) {
speex_resampler_destroy(mResampler);
}
}
void SetSourceStream(AudioNodeStream* aSource)
{
mSource = aSource;
}
void RecvTimelineEvent(uint32_t aIndex,
dom::AudioTimelineEvent& aEvent) override
{
MOZ_ASSERT(mDestination);
WebAudioUtils::ConvertAudioTimelineEventToTicks(aEvent,
mDestination);
switch (aIndex) {
case AudioBufferSourceNode::PLAYBACKRATE:
mPlaybackRateTimeline.InsertEvent<int64_t>(aEvent);
break;
case AudioBufferSourceNode::DETUNE:
mDetuneTimeline.InsertEvent<int64_t>(aEvent);
break;
default:
NS_ERROR("Bad AudioBufferSourceNodeEngine TimelineParameter");
}
}
void SetStreamTimeParameter(uint32_t aIndex, StreamTime aParam) override
{
switch (aIndex) {
case AudioBufferSourceNode::STOP: mStop = aParam; break;
default:
NS_ERROR("Bad AudioBufferSourceNodeEngine StreamTimeParameter");
}
}
void SetDoubleParameter(uint32_t aIndex, double aParam) override
{
switch (aIndex) {
case AudioBufferSourceNode::START:
MOZ_ASSERT(!mStart, "Another START?");
mStart = aParam * mDestination->SampleRate();
// Round to nearest
mBeginProcessing = mStart + 0.5;
break;
case AudioBufferSourceNode::DOPPLERSHIFT:
mDopplerShift = (aParam <= 0 || mozilla::IsNaN(aParam)) ? 1.0 : aParam;
break;
default:
NS_ERROR("Bad AudioBufferSourceNodeEngine double parameter.");
};
}
void SetInt32Parameter(uint32_t aIndex, int32_t aParam) override
{
switch (aIndex) {
case AudioBufferSourceNode::SAMPLE_RATE:
MOZ_ASSERT(aParam > 0);
mBufferSampleRate = aParam;
mSource->SetActive();
break;
case AudioBufferSourceNode::BUFFERSTART:
MOZ_ASSERT(aParam >= 0);
if (mBufferPosition == 0) {
mBufferPosition = aParam;
}
break;
case AudioBufferSourceNode::BUFFEREND:
MOZ_ASSERT(aParam >= 0);
mBufferEnd = aParam;
break;
case AudioBufferSourceNode::LOOP: mLoop = !!aParam; break;
case AudioBufferSourceNode::LOOPSTART:
MOZ_ASSERT(aParam >= 0);
mLoopStart = aParam;
break;
case AudioBufferSourceNode::LOOPEND:
MOZ_ASSERT(aParam >= 0);
mLoopEnd = aParam;
break;
default:
NS_ERROR("Bad AudioBufferSourceNodeEngine Int32Parameter");
}
}
void SetBuffer(already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer) override
{
mBuffer = aBuffer;
}
bool BegunResampling()
{
return mBeginProcessing == -STREAM_TIME_MAX;
}
void UpdateResampler(int32_t aOutRate, uint32_t aChannels)
{
if (mResampler &&
(aChannels != mChannels ||
// If the resampler has begun, then it will have moved
// mBufferPosition to after the samples it has read, but it hasn't
// output its buffered samples. Keep using the resampler, even if
// the rates now match, so that this latent segment is output.
(aOutRate == mBufferSampleRate && !BegunResampling()))) {
speex_resampler_destroy(mResampler);
mResampler = nullptr;
mRemainingResamplerTail = 0;
mBeginProcessing = mStart + 0.5;
}
if (aChannels == 0 ||
(aOutRate == mBufferSampleRate && !mResampler)) {
mResamplerOutRate = aOutRate;
return;
}
if (!mResampler) {
mChannels = aChannels;
mResampler = speex_resampler_init(mChannels, mBufferSampleRate, aOutRate,
SPEEX_RESAMPLER_QUALITY_MIN,
nullptr);
} else {
if (mResamplerOutRate == aOutRate) {
return;
}
speex_resampler_set_rate(mResampler, mBufferSampleRate, aOutRate);
}
mResamplerOutRate = aOutRate;
if (!BegunResampling()) {
// Low pass filter effects from the resampler mean that samples before
// the start time are influenced by resampling the buffer. The input
// latency indicates half the filter width.
int64_t inputLatency = speex_resampler_get_input_latency(mResampler);
uint32_t ratioNum, ratioDen;
speex_resampler_get_ratio(mResampler, &ratioNum, &ratioDen);
// The output subsample resolution supported in aligning the resampler
// is ratioNum. First round the start time to the nearest subsample.
int64_t subsample = mStart * ratioNum + 0.5;
// Now include the leading effects of the filter, and round *up* to the
// next whole tick, because there is no effect on samples outside the
// filter width.
mBeginProcessing =
(subsample - inputLatency * ratioDen + ratioNum - 1) / ratioNum;
}
}
// Borrow a full buffer of size WEBAUDIO_BLOCK_SIZE from the source buffer
// at offset aSourceOffset. This avoids copying memory.
void BorrowFromInputBuffer(AudioBlock* aOutput,
uint32_t aChannels)
{
aOutput->SetBuffer(mBuffer);
aOutput->mChannelData.SetLength(aChannels);
for (uint32_t i = 0; i < aChannels; ++i) {
aOutput->mChannelData[i] = mBuffer->GetData(i) + mBufferPosition;
}
aOutput->mVolume = 1.0f;
aOutput->mBufferFormat = AUDIO_FORMAT_FLOAT32;
}
// Copy aNumberOfFrames frames from the source buffer at offset aSourceOffset
// and put it at offset aBufferOffset in the destination buffer.
void CopyFromInputBuffer(AudioBlock* aOutput,
uint32_t aChannels,
uintptr_t aOffsetWithinBlock,
uint32_t aNumberOfFrames) {
for (uint32_t i = 0; i < aChannels; ++i) {
float* baseChannelData = aOutput->ChannelFloatsForWrite(i);
memcpy(baseChannelData + aOffsetWithinBlock,
mBuffer->GetData(i) + mBufferPosition,
aNumberOfFrames * sizeof(float));
}
}
// Resamples input data to an output buffer, according to |mBufferSampleRate| and
// the playbackRate/detune.
// The number of frames consumed/produced depends on the amount of space
// remaining in both the input and output buffer, and the playback rate (that
// is, the ratio between the output samplerate and the input samplerate).
void CopyFromInputBufferWithResampling(AudioBlock* aOutput,
uint32_t aChannels,
uint32_t* aOffsetWithinBlock,
uint32_t aAvailableInOutput,
StreamTime* aCurrentPosition,
uint32_t aBufferMax)
{
if (*aOffsetWithinBlock == 0) {
aOutput->AllocateChannels(aChannels);
}
SpeexResamplerState* resampler = mResampler;
MOZ_ASSERT(aChannels > 0);
if (mBufferPosition < aBufferMax) {
uint32_t availableInInputBuffer = aBufferMax - mBufferPosition;
uint32_t ratioNum, ratioDen;
speex_resampler_get_ratio(resampler, &ratioNum, &ratioDen);
// Limit the number of input samples copied and possibly
// format-converted for resampling by estimating how many will be used.
// This may be a little small if still filling the resampler with
// initial data, but we'll get called again and it will work out.
uint32_t inputLimit = aAvailableInOutput * ratioNum / ratioDen + 10;
if (!BegunResampling()) {
// First time the resampler is used.
uint32_t inputLatency = speex_resampler_get_input_latency(resampler);
inputLimit += inputLatency;
// If starting after mStart, then play from the beginning of the
// buffer, but correct for input latency. If starting before mStart,
// then align the resampler so that the time corresponding to the
// first input sample is mStart.
int64_t skipFracNum = static_cast<int64_t>(inputLatency) * ratioDen;
double leadTicks = mStart - *aCurrentPosition;
if (leadTicks > 0.0) {
// Round to nearest output subsample supported by the resampler at
// these rates.
int64_t leadSubsamples = leadTicks * ratioNum + 0.5;
MOZ_ASSERT(leadSubsamples <= skipFracNum,
"mBeginProcessing is wrong?");
skipFracNum -= leadSubsamples;
}
speex_resampler_set_skip_frac_num(resampler,
std::min<int64_t>(skipFracNum, UINT32_MAX));
mBeginProcessing = -STREAM_TIME_MAX;
}
inputLimit = std::min(inputLimit, availableInInputBuffer);
for (uint32_t i = 0; true; ) {
uint32_t inSamples = inputLimit;
const float* inputData = mBuffer->GetData(i) + mBufferPosition;
uint32_t outSamples = aAvailableInOutput;
float* outputData =
aOutput->ChannelFloatsForWrite(i) + *aOffsetWithinBlock;
WebAudioUtils::SpeexResamplerProcess(resampler, i,
inputData, &inSamples,
outputData, &outSamples);
if (++i == aChannels) {
mBufferPosition += inSamples;
MOZ_ASSERT(mBufferPosition <= mBufferEnd || mLoop);
*aOffsetWithinBlock += outSamples;
*aCurrentPosition += outSamples;
if (inSamples == availableInInputBuffer && !mLoop) {
// We'll feed in enough zeros to empty out the resampler's memory.
// This handles the output latency as well as capturing the low
// pass effects of the resample filter.
mRemainingResamplerTail =
2 * speex_resampler_get_input_latency(resampler) - 1;
}
return;
}
}
} else {
for (uint32_t i = 0; true; ) {
uint32_t inSamples = mRemainingResamplerTail;
uint32_t outSamples = aAvailableInOutput;
float* outputData =
aOutput->ChannelFloatsForWrite(i) + *aOffsetWithinBlock;
// AudioDataValue* for aIn selects the function that does not try to
// copy and format-convert input data.
WebAudioUtils::SpeexResamplerProcess(resampler, i,
static_cast<AudioDataValue*>(nullptr), &inSamples,
outputData, &outSamples);
if (++i == aChannels) {
MOZ_ASSERT(inSamples <= mRemainingResamplerTail);
mRemainingResamplerTail -= inSamples;
*aOffsetWithinBlock += outSamples;
*aCurrentPosition += outSamples;
break;
}
}
}
}
/**
* Fill aOutput with as many zero frames as we can, and advance
* aOffsetWithinBlock and aCurrentPosition based on how many frames we write.
* This will never advance aOffsetWithinBlock past WEBAUDIO_BLOCK_SIZE or
* aCurrentPosition past aMaxPos. This function knows when it needs to
* allocate the output buffer, and also optimizes the case where it can avoid
* memory allocations.
*/
void FillWithZeroes(AudioBlock* aOutput,
uint32_t aChannels,
uint32_t* aOffsetWithinBlock,
StreamTime* aCurrentPosition,
StreamTime aMaxPos)
{
MOZ_ASSERT(*aCurrentPosition < aMaxPos);
uint32_t numFrames =
std::min<StreamTime>(WEBAUDIO_BLOCK_SIZE - *aOffsetWithinBlock,
aMaxPos - *aCurrentPosition);
if (numFrames == WEBAUDIO_BLOCK_SIZE || !aChannels) {
aOutput->SetNull(numFrames);
} else {
if (*aOffsetWithinBlock == 0) {
aOutput->AllocateChannels(aChannels);
}
WriteZeroesToAudioBlock(aOutput, *aOffsetWithinBlock, numFrames);
}
*aOffsetWithinBlock += numFrames;
*aCurrentPosition += numFrames;
}
/**
* Copy as many frames as possible from the source buffer to aOutput, and
* advance aOffsetWithinBlock and aCurrentPosition based on how many frames
* we write. This will never advance aOffsetWithinBlock past
* WEBAUDIO_BLOCK_SIZE, or aCurrentPosition past mStop. It takes data from
* the buffer at aBufferOffset, and never takes more data than aBufferMax.
* This function knows when it needs to allocate the output buffer, and also
* optimizes the case where it can avoid memory allocations.
*/
void CopyFromBuffer(AudioBlock* aOutput,
uint32_t aChannels,
uint32_t* aOffsetWithinBlock,
StreamTime* aCurrentPosition,
uint32_t aBufferMax)
{
MOZ_ASSERT(*aCurrentPosition < mStop);
uint32_t availableInOutput =
std::min<StreamTime>(WEBAUDIO_BLOCK_SIZE - *aOffsetWithinBlock,
mStop - *aCurrentPosition);
if (mResampler) {
CopyFromInputBufferWithResampling(aOutput, aChannels,
aOffsetWithinBlock, availableInOutput,
aCurrentPosition, aBufferMax);
return;
}
if (aChannels == 0) {
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
// There is no attempt here to limit advance so that mBufferPosition is
// limited to aBufferMax. The only observable affect of skipping the
// check would be in the precise timing of the ended event if the loop
// attribute is reset after playback has looped.
*aOffsetWithinBlock += availableInOutput;
*aCurrentPosition += availableInOutput;
// Rounding at the start and end of the period means that fractional
// increments essentially accumulate if outRate remains constant. If
// outRate is varying, then accumulation happens on average but not
// precisely.
TrackTicks start = *aCurrentPosition *
mBufferSampleRate / mResamplerOutRate;
TrackTicks end = (*aCurrentPosition + availableInOutput) *
mBufferSampleRate / mResamplerOutRate;
mBufferPosition += end - start;
return;
}
uint32_t numFrames = std::min(aBufferMax - mBufferPosition,
availableInOutput);
if (numFrames == WEBAUDIO_BLOCK_SIZE) {
MOZ_ASSERT(mBufferPosition < aBufferMax);
BorrowFromInputBuffer(aOutput, aChannels);
} else {
if (*aOffsetWithinBlock == 0) {
aOutput->AllocateChannels(aChannels);
}
MOZ_ASSERT(mBufferPosition < aBufferMax);
CopyFromInputBuffer(aOutput, aChannels, *aOffsetWithinBlock, numFrames);
}
*aOffsetWithinBlock += numFrames;
*aCurrentPosition += numFrames;
mBufferPosition += numFrames;
}
int32_t ComputeFinalOutSampleRate(float aPlaybackRate, float aDetune)
{
float computedPlaybackRate = aPlaybackRate * pow(2, aDetune / 1200.f);
// Make sure the playback rate and the doppler shift are something
// our resampler can work with.
int32_t rate = WebAudioUtils::
TruncateFloatToInt<int32_t>(mSource->SampleRate() /
(computedPlaybackRate * mDopplerShift));
return rate ? rate : mBufferSampleRate;
}
void UpdateSampleRateIfNeeded(uint32_t aChannels, StreamTime aStreamPosition)
{
float playbackRate;
float detune;
if (mPlaybackRateTimeline.HasSimpleValue()) {
playbackRate = mPlaybackRateTimeline.GetValue();
} else {
playbackRate = mPlaybackRateTimeline.GetValueAtTime(aStreamPosition);
}
if (mDetuneTimeline.HasSimpleValue()) {
detune = mDetuneTimeline.GetValue();
} else {
detune = mDetuneTimeline.GetValueAtTime(aStreamPosition);
}
if (playbackRate <= 0 || mozilla::IsNaN(playbackRate)) {
playbackRate = 1.0f;
}
detune = std::min(std::max(-1200.f, detune), 1200.f);
int32_t outRate = ComputeFinalOutSampleRate(playbackRate, detune);
UpdateResampler(outRate, aChannels);
}
void ProcessBlock(AudioNodeStream* aStream,
GraphTime aFrom,
const AudioBlock& aInput,
AudioBlock* aOutput,
bool* aFinished) override
{
if (mBufferSampleRate == 0) {
// start() has not yet been called or no buffer has yet been set
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
return;
}
StreamTime streamPosition = mDestination->GraphTimeToStreamTime(aFrom);
uint32_t channels = mBuffer ? mBuffer->GetChannels() : 0;
UpdateSampleRateIfNeeded(channels, streamPosition);
uint32_t written = 0;
while (written < WEBAUDIO_BLOCK_SIZE) {
if (mStop != STREAM_TIME_MAX &&
streamPosition >= mStop) {
FillWithZeroes(aOutput, channels, &written, &streamPosition, STREAM_TIME_MAX);
continue;
}
if (streamPosition < mBeginProcessing) {
FillWithZeroes(aOutput, channels, &written, &streamPosition,
mBeginProcessing);
continue;
}
if (mLoop) {
// mLoopEnd can become less than mBufferPosition when a LOOPEND engine
// parameter is received after "loopend" is changed on the node or a
// new buffer with lower samplerate is set.
if (mBufferPosition >= mLoopEnd) {
mBufferPosition = mLoopStart;
}
CopyFromBuffer(aOutput, channels, &written, &streamPosition, mLoopEnd);
} else {
if (mBufferPosition < mBufferEnd || mRemainingResamplerTail) {
CopyFromBuffer(aOutput, channels, &written, &streamPosition, mBufferEnd);
} else {
FillWithZeroes(aOutput, channels, &written, &streamPosition, STREAM_TIME_MAX);
}
}
}
// We've finished if we've gone past mStop, or if we're past mDuration when
// looping is disabled.
if (streamPosition >= mStop ||
(!mLoop && mBufferPosition >= mBufferEnd && !mRemainingResamplerTail)) {
*aFinished = true;
}
}
bool IsActive() const override
{
// Whether buffer has been set and start() has been called.
return mBufferSampleRate != 0;
}
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
{
// Not owned:
// - mBuffer - shared w/ AudioNode
// - mPlaybackRateTimeline - shared w/ AudioNode
// - mDetuneTimeline - shared w/ AudioNode
size_t amount = AudioNodeEngine::SizeOfExcludingThis(aMallocSizeOf);
// NB: We need to modify speex if we want the full memory picture, internal
// fields that need measuring noted below.
// - mResampler->mem
// - mResampler->sinc_table
// - mResampler->last_sample
// - mResampler->magic_samples
// - mResampler->samp_frac_num
amount += aMallocSizeOf(mResampler);
return amount;
}
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
{
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
double mStart; // including the fractional position between ticks
// Low pass filter effects from the resampler mean that samples before the
// start time are influenced by resampling the buffer. mBeginProcessing
// includes the extent of this filter. The special value of -STREAM_TIME_MAX
// indicates that the resampler has begun processing.
StreamTime mBeginProcessing;
StreamTime mStop;
RefPtr<ThreadSharedFloatArrayBufferList> mBuffer;
SpeexResamplerState* mResampler;
// mRemainingResamplerTail, like mBufferPosition, and
// mBufferEnd, is measured in input buffer samples.
uint32_t mRemainingResamplerTail;
uint32_t mBufferEnd;
uint32_t mLoopStart;
uint32_t mLoopEnd;
uint32_t mBufferPosition;
int32_t mBufferSampleRate;
int32_t mResamplerOutRate;
uint32_t mChannels;
float mDopplerShift;
AudioNodeStream* mDestination;
AudioNodeStream* mSource;
AudioParamTimeline mPlaybackRateTimeline;
AudioParamTimeline mDetuneTimeline;
bool mLoop;
};
AudioBufferSourceNode::AudioBufferSourceNode(AudioContext* aContext)
: AudioNode(aContext,
2,
ChannelCountMode::Max,
ChannelInterpretation::Speakers)
, mLoopStart(0.0)
, mLoopEnd(0.0)
// mOffset and mDuration are initialized in Start().
, mPlaybackRate(new AudioParam(this, PLAYBACKRATE, 1.0f, "playbackRate"))
, mDetune(new AudioParam(this, DETUNE, 0.0f, "detune"))
, mLoop(false)
, mStartCalled(false)
{
AudioBufferSourceNodeEngine* engine = new AudioBufferSourceNodeEngine(this, aContext->Destination());
mStream = AudioNodeStream::Create(aContext, engine,
AudioNodeStream::NEED_MAIN_THREAD_FINISHED);
engine->SetSourceStream(mStream);
mStream->AddMainThreadListener(this);
}
AudioBufferSourceNode::~AudioBufferSourceNode()
{
}
void
AudioBufferSourceNode::DestroyMediaStream()
{
bool hadStream = mStream;
if (hadStream) {
mStream->RemoveMainThreadListener(this);
}
AudioNode::DestroyMediaStream();
if (hadStream && Context()) {
Context()->UnregisterAudioBufferSourceNode(this);
}
}
size_t
AudioBufferSourceNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
{
size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
/* mBuffer can be shared and is accounted for separately. */
amount += mPlaybackRate->SizeOfIncludingThis(aMallocSizeOf);
amount += mDetune->SizeOfIncludingThis(aMallocSizeOf);
return amount;
}
size_t
AudioBufferSourceNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
{
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
JSObject*
AudioBufferSourceNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return AudioBufferSourceNodeBinding::Wrap(aCx, this, aGivenProto);
}
void
AudioBufferSourceNode::Start(double aWhen, double aOffset,
const Optional<double>& aDuration, ErrorResult& aRv)
{
if (!WebAudioUtils::IsTimeValid(aWhen) ||
(aDuration.WasPassed() && !WebAudioUtils::IsTimeValid(aDuration.Value()))) {
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return;
}
if (mStartCalled) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}
mStartCalled = true;
AudioNodeStream* ns = mStream;
if (!ns) {
// Nothing to play, or we're already dead for some reason
return;
}
// Remember our arguments so that we can use them when we get a new buffer.
mOffset = aOffset;
mDuration = aDuration.WasPassed() ? aDuration.Value()
: std::numeric_limits<double>::min();
// We can't send these parameters without a buffer because we don't know the
// buffer's sample rate or length.
if (mBuffer) {
SendOffsetAndDurationParametersToStream(ns);
}
// Don't set parameter unnecessarily
if (aWhen > 0.0) {
ns->SetDoubleParameter(START, aWhen);
}
}
void
AudioBufferSourceNode::SendBufferParameterToStream(JSContext* aCx)
{
AudioNodeStream* ns = mStream;
if (!ns) {
return;
}
if (mBuffer) {
RefPtr<ThreadSharedFloatArrayBufferList> data =
mBuffer->GetThreadSharedChannelsForRate(aCx);
ns->SetBuffer(data.forget());
if (mStartCalled) {
SendOffsetAndDurationParametersToStream(ns);
}
} else {
ns->SetInt32Parameter(BUFFEREND, 0);
ns->SetBuffer(nullptr);
MarkInactive();
}
}
void
AudioBufferSourceNode::SendOffsetAndDurationParametersToStream(AudioNodeStream* aStream)
{
NS_ASSERTION(mBuffer && mStartCalled,
"Only call this when we have a buffer and start() has been called");
float rate = mBuffer->SampleRate();
aStream->SetInt32Parameter(SAMPLE_RATE, rate);
int32_t bufferEnd = mBuffer->Length();
int32_t offsetSamples = std::max(0, NS_lround(mOffset * rate));
// Don't set parameter unnecessarily
if (offsetSamples > 0) {
aStream->SetInt32Parameter(BUFFERSTART, offsetSamples);
}
if (mDuration != std::numeric_limits<double>::min()) {
MOZ_ASSERT(mDuration >= 0.0); // provided by Start()
MOZ_ASSERT(rate >= 0.0f); // provided by AudioBuffer::Create()
static_assert(std::numeric_limits<double>::digits >=
std::numeric_limits<decltype(bufferEnd)>::digits,
"bufferEnd should be represented exactly by double");
// + 0.5 rounds mDuration to nearest sample when assigned to bufferEnd.
bufferEnd = std::min<double>(bufferEnd,
offsetSamples + mDuration * rate + 0.5);
}
aStream->SetInt32Parameter(BUFFEREND, bufferEnd);
MarkActive();
}
void
AudioBufferSourceNode::Stop(double aWhen, ErrorResult& aRv)
{
if (!WebAudioUtils::IsTimeValid(aWhen)) {
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return;
}
if (!mStartCalled) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}
AudioNodeStream* ns = mStream;
if (!ns || !Context()) {
// We've already stopped and had our stream shut down
return;
}
ns->SetStreamTimeParameter(STOP, Context(), std::max(0.0, aWhen));
}
void
AudioBufferSourceNode::NotifyMainThreadStreamFinished()
{
MOZ_ASSERT(mStream->IsFinished());
class EndedEventDispatcher final : public Runnable
{
public:
explicit EndedEventDispatcher(AudioBufferSourceNode* aNode)
: mNode(aNode) {}
NS_IMETHODIMP Run() override
{
// If it's not safe to run scripts right now, schedule this to run later
if (!nsContentUtils::IsSafeToRunScript()) {
nsContentUtils::AddScriptRunner(this);
return NS_OK;
}
mNode->DispatchTrustedEvent(NS_LITERAL_STRING("ended"));
// Release stream resources.
mNode->DestroyMediaStream();
return NS_OK;
}
private:
RefPtr<AudioBufferSourceNode> mNode;
};
NS_DispatchToMainThread(new EndedEventDispatcher(this));
// Drop the playing reference
// Warning: The below line might delete this.
MarkInactive();
}
void
AudioBufferSourceNode::SendDopplerShiftToStream(double aDopplerShift)
{
MOZ_ASSERT(mStream, "Should have disconnected panner if no stream");
SendDoubleParameterToStream(DOPPLERSHIFT, aDopplerShift);
}
void
AudioBufferSourceNode::SendLoopParametersToStream()
{
if (!mStream) {
return;
}
// Don't compute and set the loop parameters unnecessarily
if (mLoop && mBuffer) {
float rate = mBuffer->SampleRate();
double length = (double(mBuffer->Length()) / mBuffer->SampleRate());
double actualLoopStart, actualLoopEnd;
if (mLoopStart >= 0.0 && mLoopEnd > 0.0 &&
mLoopStart < mLoopEnd) {
MOZ_ASSERT(mLoopStart != 0.0 || mLoopEnd != 0.0);
actualLoopStart = (mLoopStart > length) ? 0.0 : mLoopStart;
actualLoopEnd = std::min(mLoopEnd, length);
} else {
actualLoopStart = 0.0;
actualLoopEnd = length;
}
int32_t loopStartTicks = NS_lround(actualLoopStart * rate);
int32_t loopEndTicks = NS_lround(actualLoopEnd * rate);
if (loopStartTicks < loopEndTicks) {
SendInt32ParameterToStream(LOOPSTART, loopStartTicks);
SendInt32ParameterToStream(LOOPEND, loopEndTicks);
SendInt32ParameterToStream(LOOP, 1);
} else {
// Be explicit about looping not happening if the offsets make
// looping impossible.
SendInt32ParameterToStream(LOOP, 0);
}
} else {
SendInt32ParameterToStream(LOOP, 0);
}
}
} // namespace dom
} // namespace mozilla