2012-09-18 16:07:33 -07: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 "AudioBufferSourceNode.h"
|
|
|
|
#include "mozilla/dom/AudioBufferSourceNodeBinding.h"
|
2013-09-05 13:25:17 -07:00
|
|
|
#include "mozilla/dom/AudioParam.h"
|
2013-02-04 15:07:25 -08:00
|
|
|
#include "nsMathUtils.h"
|
|
|
|
#include "AudioNodeEngine.h"
|
2013-06-07 12:25:04 -07:00
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
#include "AudioDestinationNode.h"
|
2013-08-15 12:44:14 -07:00
|
|
|
#include "AudioParamTimeline.h"
|
2013-03-18 17:54:32 -07:00
|
|
|
#include "speex/speex_resampler.h"
|
2013-04-22 14:01:22 -07:00
|
|
|
#include <limits>
|
2012-09-18 16:07:33 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AudioBufferSourceNode)
|
|
|
|
|
2013-04-25 13:52:52 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioBufferSourceNode)
|
2013-04-11 05:47:57 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBuffer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPlaybackRate)
|
|
|
|
if (tmp->Context()) {
|
2013-04-30 16:20:55 -07:00
|
|
|
// AudioNode's Unlink implementation disconnects us from the graph
|
|
|
|
// too, but we need to do this right here to make sure that
|
|
|
|
// UnregisterAudioBufferSourceNode can properly untangle us from
|
|
|
|
// the possibly connected PannerNodes.
|
|
|
|
tmp->DisconnectFromGraph();
|
2013-04-11 05:47:57 -07:00
|
|
|
tmp->Context()->UnregisterAudioBufferSourceNode(tmp);
|
|
|
|
}
|
2013-04-25 13:52:52 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(AudioNode)
|
2013-04-11 05:47:57 -07:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AudioBufferSourceNode, AudioNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBuffer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPlaybackRate)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2012-09-24 20:31:58 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AudioBufferSourceNode)
|
2013-04-08 19:45:02 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
|
2012-09-24 20:31:58 -07:00
|
|
|
|
2013-04-08 19:45:02 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(AudioBufferSourceNode, AudioNode)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(AudioBufferSourceNode, AudioNode)
|
2012-09-18 16:07:33 -07:00
|
|
|
|
2013-06-25 09:00:42 -07:00
|
|
|
/**
|
|
|
|
* Media-thread playback engine for AudioBufferSourceNode.
|
|
|
|
* Nothing is played until a non-null buffer has been set (via
|
2014-02-05 11:27:45 -08:00
|
|
|
* AudioNodeStream::SetBuffer) and a non-zero mBufferEnd has been set (via
|
2013-06-25 09:00:42 -07:00
|
|
|
* AudioNodeStream::SetInt32Parameter).
|
|
|
|
*/
|
2013-06-07 12:25:04 -07:00
|
|
|
class AudioBufferSourceNodeEngine : public AudioNodeEngine
|
2013-02-04 15:07:25 -08:00
|
|
|
{
|
|
|
|
public:
|
2013-04-20 09:16:28 -07:00
|
|
|
explicit AudioBufferSourceNodeEngine(AudioNode* aNode,
|
|
|
|
AudioDestinationNode* aDestination) :
|
|
|
|
AudioNodeEngine(aNode),
|
2014-02-26 14:45:04 -08:00
|
|
|
mStart(0.0), mBeginProcessing(0),
|
2014-02-26 14:45:03 -08:00
|
|
|
mStop(TRACK_TICKS_MAX),
|
2013-12-03 17:20:12 -08:00
|
|
|
mResampler(nullptr), mRemainingResamplerTail(0),
|
2014-02-05 11:28:42 -08:00
|
|
|
mBufferEnd(0),
|
2013-03-18 17:54:32 -07:00
|
|
|
mLoopStart(0), mLoopEnd(0),
|
2014-02-26 14:45:03 -08:00
|
|
|
mBufferSampleRate(0), mBufferPosition(0), mChannels(0),
|
2013-04-11 05:47:57 -07:00
|
|
|
mDopplerShift(1.0f),
|
2013-06-07 12:25:04 -07:00
|
|
|
mDestination(static_cast<AudioNodeStream*>(aDestination->Stream())),
|
2013-04-09 05:47:42 -07:00
|
|
|
mPlaybackRateTimeline(1.0f), mLoop(false)
|
2013-03-10 10:59:41 -07:00
|
|
|
{}
|
2013-02-04 15:07:25 -08:00
|
|
|
|
2013-03-18 17:54:32 -07:00
|
|
|
~AudioBufferSourceNodeEngine()
|
|
|
|
{
|
|
|
|
if (mResampler) {
|
|
|
|
speex_resampler_destroy(mResampler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 02:23:21 -07:00
|
|
|
void SetSourceStream(AudioNodeStream* aSource)
|
|
|
|
{
|
|
|
|
mSource = aSource;
|
|
|
|
}
|
|
|
|
|
2013-05-24 10:10:08 -07:00
|
|
|
virtual void SetTimelineParameter(uint32_t aIndex,
|
|
|
|
const dom::AudioParamTimeline& aValue,
|
|
|
|
TrackRate aSampleRate) MOZ_OVERRIDE
|
2013-04-09 05:47:42 -07:00
|
|
|
{
|
|
|
|
switch (aIndex) {
|
2013-04-21 21:22:33 -07:00
|
|
|
case AudioBufferSourceNode::PLAYBACKRATE:
|
2013-04-09 05:47:42 -07:00
|
|
|
mPlaybackRateTimeline = aValue;
|
2013-07-12 02:23:21 -07:00
|
|
|
WebAudioUtils::ConvertAudioParamToTicks(mPlaybackRateTimeline, mSource, mDestination);
|
2013-04-09 05:47:42 -07:00
|
|
|
break;
|
|
|
|
default:
|
2013-05-12 21:17:55 -07:00
|
|
|
NS_ERROR("Bad AudioBufferSourceNodeEngine TimelineParameter");
|
2013-04-09 05:47:42 -07:00
|
|
|
}
|
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
virtual void SetStreamTimeParameter(uint32_t aIndex, TrackTicks aParam)
|
|
|
|
{
|
|
|
|
switch (aIndex) {
|
2013-04-21 21:22:33 -07:00
|
|
|
case AudioBufferSourceNode::STOP: mStop = aParam; break;
|
2013-02-04 15:07:25 -08:00
|
|
|
default:
|
|
|
|
NS_ERROR("Bad AudioBufferSourceNodeEngine StreamTimeParameter");
|
|
|
|
}
|
|
|
|
}
|
2013-04-11 05:47:57 -07:00
|
|
|
virtual void SetDoubleParameter(uint32_t aIndex, double aParam)
|
|
|
|
{
|
|
|
|
switch (aIndex) {
|
2014-02-26 14:45:04 -08:00
|
|
|
case AudioBufferSourceNode::START:
|
|
|
|
MOZ_ASSERT(!mStart, "Another START?");
|
|
|
|
mStart = mSource->TimeFromDestinationTime(mDestination, aParam) *
|
|
|
|
mSource->SampleRate();
|
|
|
|
// Round to nearest
|
|
|
|
mBeginProcessing = mStart + 0.5;
|
|
|
|
break;
|
|
|
|
case AudioBufferSourceNode::DOPPLERSHIFT:
|
|
|
|
mDopplerShift = aParam > 0 && aParam == aParam ? aParam : 1.0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Bad AudioBufferSourceNodeEngine double parameter.");
|
2013-04-11 05:47:57 -07:00
|
|
|
};
|
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
virtual void SetInt32Parameter(uint32_t aIndex, int32_t aParam)
|
|
|
|
{
|
|
|
|
switch (aIndex) {
|
2013-05-24 10:09:51 -07:00
|
|
|
case AudioBufferSourceNode::SAMPLE_RATE: mBufferSampleRate = aParam; break;
|
2014-02-05 11:28:42 -08:00
|
|
|
case AudioBufferSourceNode::BUFFERSTART:
|
|
|
|
if (mBufferPosition == 0) {
|
|
|
|
mBufferPosition = aParam;
|
|
|
|
}
|
|
|
|
break;
|
2014-02-05 11:27:45 -08:00
|
|
|
case AudioBufferSourceNode::BUFFEREND: mBufferEnd = aParam; break;
|
2013-04-21 21:22:33 -07:00
|
|
|
case AudioBufferSourceNode::LOOP: mLoop = !!aParam; break;
|
|
|
|
case AudioBufferSourceNode::LOOPSTART: mLoopStart = aParam; break;
|
|
|
|
case AudioBufferSourceNode::LOOPEND: mLoopEnd = aParam; break;
|
2013-02-04 15:07:25 -08:00
|
|
|
default:
|
|
|
|
NS_ERROR("Bad AudioBufferSourceNodeEngine Int32Parameter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual void SetBuffer(already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer)
|
|
|
|
{
|
|
|
|
mBuffer = aBuffer;
|
|
|
|
}
|
|
|
|
|
2014-02-26 14:45:03 -08:00
|
|
|
bool BegunResampling()
|
|
|
|
{
|
|
|
|
return mBeginProcessing == -TRACK_TICKS_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateResampler(int32_t aOutRate, uint32_t aChannels)
|
2013-03-18 17:54:32 -07:00
|
|
|
{
|
2014-02-26 14:45:03 -08:00
|
|
|
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()))) {
|
2013-03-18 17:54:32 -07:00
|
|
|
speex_resampler_destroy(mResampler);
|
|
|
|
mResampler = nullptr;
|
2014-02-26 14:45:04 -08:00
|
|
|
mBeginProcessing = mStart + 0.5;
|
2014-02-26 14:45:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aOutRate == mBufferSampleRate && !mResampler) {
|
|
|
|
return;
|
2013-03-18 17:54:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mResampler) {
|
|
|
|
mChannels = aChannels;
|
2014-02-26 14:45:03 -08:00
|
|
|
mResampler = speex_resampler_init(mChannels, mBufferSampleRate, aOutRate,
|
2013-03-18 17:54:32 -07:00
|
|
|
SPEEX_RESAMPLER_QUALITY_DEFAULT,
|
|
|
|
nullptr);
|
2014-02-26 14:45:03 -08:00
|
|
|
} else {
|
|
|
|
uint32_t currentOutSampleRate, currentInSampleRate;
|
|
|
|
speex_resampler_get_rate(mResampler, ¤tInSampleRate,
|
|
|
|
¤tOutSampleRate);
|
|
|
|
if (currentOutSampleRate == static_cast<uint32_t>(aOutRate)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
speex_resampler_set_rate(mResampler, currentInSampleRate, 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);
|
2014-02-26 14:45:04 -08:00
|
|
|
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;
|
2013-03-18 17:54:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-10 18:02:22 -07:00
|
|
|
// Borrow a full buffer of size WEBAUDIO_BLOCK_SIZE from the source buffer
|
|
|
|
// at offset aSourceOffset. This avoids copying memory.
|
2013-03-10 15:38:57 -07:00
|
|
|
void BorrowFromInputBuffer(AudioChunk* aOutput,
|
2014-02-10 14:19:26 -08:00
|
|
|
uint32_t aChannels)
|
2013-03-10 15:38:57 -07:00
|
|
|
{
|
|
|
|
aOutput->mDuration = WEBAUDIO_BLOCK_SIZE;
|
|
|
|
aOutput->mBuffer = mBuffer;
|
|
|
|
aOutput->mChannelData.SetLength(aChannels);
|
|
|
|
for (uint32_t i = 0; i < aChannels; ++i) {
|
2014-02-10 14:19:26 -08:00
|
|
|
aOutput->mChannelData[i] = mBuffer->GetData(i) + mBufferPosition;
|
2013-03-10 15:38:57 -07:00
|
|
|
}
|
|
|
|
aOutput->mVolume = 1.0f;
|
|
|
|
aOutput->mBufferFormat = AUDIO_FORMAT_FLOAT32;
|
|
|
|
}
|
|
|
|
|
2013-03-10 18:02:22 -07:00
|
|
|
// Copy aNumberOfFrames frames from the source buffer at offset aSourceOffset
|
|
|
|
// and put it at offset aBufferOffset in the destination buffer.
|
|
|
|
void CopyFromInputBuffer(AudioChunk* aOutput,
|
|
|
|
uint32_t aChannels,
|
2014-02-10 14:19:26 -08:00
|
|
|
uintptr_t aOffsetWithinBlock,
|
2013-03-10 18:02:22 -07:00
|
|
|
uint32_t aNumberOfFrames) {
|
|
|
|
for (uint32_t i = 0; i < aChannels; ++i) {
|
|
|
|
float* baseChannelData = static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i]));
|
2014-02-10 14:19:26 -08:00
|
|
|
memcpy(baseChannelData + aOffsetWithinBlock,
|
|
|
|
mBuffer->GetData(i) + mBufferPosition,
|
2013-03-10 18:02:22 -07:00
|
|
|
aNumberOfFrames * sizeof(float));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 10:09:51 -07:00
|
|
|
// Resamples input data to an output buffer, according to |mBufferSampleRate| and
|
2013-04-09 05:47:42 -07:00
|
|
|
// the playbackRate.
|
2013-03-18 17:54:32 -07:00
|
|
|
// 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).
|
2013-05-24 10:13:31 -07:00
|
|
|
void CopyFromInputBufferWithResampling(AudioNodeStream* aStream,
|
|
|
|
AudioChunk* aOutput,
|
2013-03-18 17:54:32 -07:00
|
|
|
uint32_t aChannels,
|
2014-02-26 14:45:03 -08:00
|
|
|
uint32_t* aOffsetWithinBlock,
|
|
|
|
TrackTicks* aCurrentPosition,
|
2014-02-10 14:19:26 -08:00
|
|
|
int32_t aBufferMax) {
|
2013-12-03 17:20:12 -08:00
|
|
|
// TODO: adjust for mStop (see bug 913854 comment 9).
|
2014-02-26 14:45:03 -08:00
|
|
|
uint32_t availableInOutputBuffer =
|
|
|
|
WEBAUDIO_BLOCK_SIZE - *aOffsetWithinBlock;
|
|
|
|
SpeexResamplerState* resampler = mResampler;
|
2013-12-03 17:20:12 -08:00
|
|
|
MOZ_ASSERT(aChannels > 0);
|
|
|
|
|
2014-02-10 14:19:26 -08:00
|
|
|
if (mBufferPosition < aBufferMax) {
|
|
|
|
uint32_t availableInInputBuffer = aBufferMax - mBufferPosition;
|
2014-02-26 14:45:03 -08:00
|
|
|
uint32_t ratioNum, ratioDen;
|
|
|
|
speex_resampler_get_ratio(resampler, &ratioNum, &ratioDen);
|
2013-12-03 17:20:12 -08:00
|
|
|
// Limit the number of input samples copied and possibly
|
|
|
|
// format-converted for resampling by estimating how many will be used.
|
2014-02-26 14:45:03 -08:00
|
|
|
// 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 = availableInOutputBuffer * 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.
|
|
|
|
uint32_t skipFracNum = inputLatency * ratioDen;
|
2014-02-26 14:45:04 -08:00
|
|
|
double leadTicks = mStart - *aCurrentPosition;
|
|
|
|
if (leadTicks > 0.0) {
|
|
|
|
// Round to nearest output subsample supported by the resampler at
|
|
|
|
// these rates.
|
|
|
|
skipFracNum -= leadTicks * ratioNum + 0.5;
|
2014-02-26 14:45:03 -08:00
|
|
|
MOZ_ASSERT(skipFracNum < INT32_MAX, "mBeginProcessing is wrong?");
|
|
|
|
}
|
|
|
|
speex_resampler_set_skip_frac_num(resampler, skipFracNum);
|
|
|
|
|
|
|
|
mBeginProcessing = -TRACK_TICKS_MAX;
|
|
|
|
}
|
|
|
|
inputLimit = std::min(inputLimit, availableInInputBuffer);
|
|
|
|
|
2013-12-03 17:20:12 -08:00
|
|
|
for (uint32_t i = 0; true; ) {
|
|
|
|
uint32_t inSamples = inputLimit;
|
2014-02-10 14:19:26 -08:00
|
|
|
const float* inputData = mBuffer->GetData(i) + mBufferPosition;
|
2013-12-03 17:20:12 -08:00
|
|
|
|
|
|
|
uint32_t outSamples = availableInOutputBuffer;
|
|
|
|
float* outputData =
|
|
|
|
static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i])) +
|
2014-02-26 14:45:03 -08:00
|
|
|
*aOffsetWithinBlock;
|
2013-12-03 17:20:12 -08:00
|
|
|
|
|
|
|
WebAudioUtils::SpeexResamplerProcess(resampler, i,
|
|
|
|
inputData, &inSamples,
|
|
|
|
outputData, &outSamples);
|
|
|
|
if (++i == aChannels) {
|
2014-02-05 11:28:42 -08:00
|
|
|
mBufferPosition += inSamples;
|
|
|
|
MOZ_ASSERT(mBufferPosition <= mBufferEnd || mLoop);
|
2014-02-26 14:45:03 -08:00
|
|
|
*aOffsetWithinBlock += outSamples;
|
|
|
|
*aCurrentPosition += outSamples;
|
2014-02-04 17:42:18 -08:00
|
|
|
if (inSamples == availableInInputBuffer && !mLoop) {
|
2014-02-26 14:45:03 -08:00
|
|
|
// 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.
|
2013-12-03 17:20:12 -08:00
|
|
|
mRemainingResamplerTail =
|
2014-02-26 14:45:03 -08:00
|
|
|
2 * speex_resampler_get_input_latency(resampler) - 1;
|
2013-12-03 17:20:12 -08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (uint32_t i = 0; true; ) {
|
|
|
|
uint32_t inSamples = mRemainingResamplerTail;
|
|
|
|
uint32_t outSamples = availableInOutputBuffer;
|
|
|
|
float* outputData =
|
|
|
|
static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i])) +
|
2014-02-26 14:45:03 -08:00
|
|
|
*aOffsetWithinBlock;
|
2013-12-03 17:20:12 -08:00
|
|
|
|
|
|
|
// 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) {
|
|
|
|
mRemainingResamplerTail -= inSamples;
|
|
|
|
MOZ_ASSERT(mRemainingResamplerTail >= 0);
|
2014-02-26 14:45:03 -08:00
|
|
|
*aOffsetWithinBlock += outSamples;
|
|
|
|
*aCurrentPosition += outSamples;
|
2013-12-03 17:20:12 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-18 17:54:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-10 18:02:22 -07:00
|
|
|
/**
|
|
|
|
* 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(AudioChunk* aOutput,
|
|
|
|
uint32_t aChannels,
|
|
|
|
uint32_t* aOffsetWithinBlock,
|
|
|
|
TrackTicks* aCurrentPosition,
|
|
|
|
TrackTicks aMaxPos)
|
|
|
|
{
|
2013-10-22 22:36:59 -07:00
|
|
|
MOZ_ASSERT(*aCurrentPosition < aMaxPos);
|
|
|
|
uint32_t numFrames =
|
|
|
|
std::min<TrackTicks>(WEBAUDIO_BLOCK_SIZE - *aOffsetWithinBlock,
|
|
|
|
aMaxPos - *aCurrentPosition);
|
2013-03-10 18:02:22 -07:00
|
|
|
if (numFrames == WEBAUDIO_BLOCK_SIZE) {
|
|
|
|
aOutput->SetNull(numFrames);
|
|
|
|
} else {
|
2014-01-29 02:47:58 -08:00
|
|
|
if (*aOffsetWithinBlock == 0) {
|
2013-03-10 18:02:22 -07:00
|
|
|
AllocateAudioBlock(aChannels, aOutput);
|
|
|
|
}
|
|
|
|
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
|
2013-12-02 15:07:17 -08:00
|
|
|
* we write. This will never advance aOffsetWithinBlock past
|
2013-03-10 18:02:22 -07:00
|
|
|
* 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.
|
|
|
|
*/
|
2013-05-24 10:13:31 -07:00
|
|
|
void CopyFromBuffer(AudioNodeStream* aStream,
|
|
|
|
AudioChunk* aOutput,
|
2013-03-10 18:02:22 -07:00
|
|
|
uint32_t aChannels,
|
|
|
|
uint32_t* aOffsetWithinBlock,
|
|
|
|
TrackTicks* aCurrentPosition,
|
2014-02-10 14:19:26 -08:00
|
|
|
int32_t aBufferMax)
|
2013-03-10 18:02:22 -07:00
|
|
|
{
|
2013-10-22 22:36:59 -07:00
|
|
|
MOZ_ASSERT(*aCurrentPosition < mStop);
|
|
|
|
uint32_t numFrames =
|
2014-02-10 14:19:26 -08:00
|
|
|
std::min(std::min<TrackTicks>(WEBAUDIO_BLOCK_SIZE - *aOffsetWithinBlock,
|
|
|
|
aBufferMax - mBufferPosition),
|
|
|
|
mStop - *aCurrentPosition);
|
2014-02-26 14:45:03 -08:00
|
|
|
if (numFrames == WEBAUDIO_BLOCK_SIZE && !mResampler) {
|
2014-02-10 14:19:26 -08:00
|
|
|
MOZ_ASSERT(mBufferPosition < aBufferMax);
|
|
|
|
BorrowFromInputBuffer(aOutput, aChannels);
|
2013-03-18 17:54:32 -07:00
|
|
|
*aOffsetWithinBlock += numFrames;
|
|
|
|
*aCurrentPosition += numFrames;
|
2014-02-05 11:28:42 -08:00
|
|
|
mBufferPosition += numFrames;
|
2013-03-10 18:02:22 -07:00
|
|
|
} else {
|
2014-01-29 02:47:58 -08:00
|
|
|
if (*aOffsetWithinBlock == 0) {
|
2013-03-10 18:02:22 -07:00
|
|
|
AllocateAudioBlock(aChannels, aOutput);
|
|
|
|
}
|
2014-02-26 14:45:03 -08:00
|
|
|
if (!mResampler) {
|
2014-02-10 14:19:26 -08:00
|
|
|
MOZ_ASSERT(mBufferPosition < aBufferMax);
|
|
|
|
CopyFromInputBuffer(aOutput, aChannels, *aOffsetWithinBlock, numFrames);
|
2013-03-18 17:54:32 -07:00
|
|
|
*aOffsetWithinBlock += numFrames;
|
|
|
|
*aCurrentPosition += numFrames;
|
2014-02-05 11:28:42 -08:00
|
|
|
mBufferPosition += numFrames;
|
2013-03-18 17:54:32 -07:00
|
|
|
} else {
|
2014-02-26 14:45:03 -08:00
|
|
|
CopyFromInputBufferWithResampling(aStream, aOutput, aChannels, aOffsetWithinBlock, aCurrentPosition, aBufferMax);
|
2013-03-18 17:54:32 -07:00
|
|
|
}
|
2013-03-10 18:02:22 -07:00
|
|
|
}
|
2013-03-18 17:54:32 -07:00
|
|
|
}
|
|
|
|
|
2014-02-26 14:45:03 -08:00
|
|
|
int32_t ComputeFinalOutSampleRate(float aPlaybackRate)
|
2013-04-11 05:47:57 -07:00
|
|
|
{
|
2014-02-26 14:45:03 -08:00
|
|
|
// 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() /
|
|
|
|
(aPlaybackRate * mDopplerShift));
|
|
|
|
return rate ? rate : mBufferSampleRate;
|
2013-04-11 05:47:57 -07:00
|
|
|
}
|
|
|
|
|
2014-02-26 14:45:03 -08:00
|
|
|
void UpdateSampleRateIfNeeded(uint32_t aChannels)
|
2013-04-11 05:47:57 -07:00
|
|
|
{
|
2014-02-26 14:45:03 -08:00
|
|
|
float playbackRate;
|
2013-04-11 05:47:57 -07:00
|
|
|
|
|
|
|
if (mPlaybackRateTimeline.HasSimpleValue()) {
|
2014-02-26 14:45:03 -08:00
|
|
|
playbackRate = mPlaybackRateTimeline.GetValue();
|
2013-04-11 05:47:57 -07:00
|
|
|
} else {
|
2014-02-26 14:45:03 -08:00
|
|
|
playbackRate = mPlaybackRateTimeline.GetValueAtTime(mSource->GetCurrentPosition());
|
2013-04-11 05:47:57 -07:00
|
|
|
}
|
2014-02-26 14:45:03 -08:00
|
|
|
if (playbackRate <= 0 || playbackRate != playbackRate) {
|
|
|
|
playbackRate = 1.0f;
|
2013-04-30 07:04:05 -07:00
|
|
|
}
|
|
|
|
|
2014-02-26 14:45:03 -08:00
|
|
|
int32_t outRate = ComputeFinalOutSampleRate(playbackRate);
|
|
|
|
UpdateResampler(outRate, aChannels);
|
2013-04-11 05:47:57 -07:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:09:49 -08:00
|
|
|
virtual void ProcessBlock(AudioNodeStream* aStream,
|
|
|
|
const AudioChunk& aInput,
|
|
|
|
AudioChunk* aOutput,
|
|
|
|
bool* aFinished)
|
2013-02-04 15:07:25 -08:00
|
|
|
{
|
2014-02-05 11:27:45 -08:00
|
|
|
if (!mBuffer || !mBufferEnd) {
|
2013-12-12 04:33:01 -08:00
|
|
|
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
|
2013-02-04 15:07:25 -08:00
|
|
|
return;
|
2013-06-25 09:00:42 -07:00
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
|
|
|
|
uint32_t channels = mBuffer->GetChannels();
|
|
|
|
if (!channels) {
|
|
|
|
aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-09 05:47:42 -07:00
|
|
|
// WebKit treats the playbackRate as a k-rate parameter in their code,
|
|
|
|
// despite the spec saying that it should be an a-rate parameter. We treat
|
|
|
|
// it as k-rate. Spec bug: https://www.w3.org/Bugs/Public/show_bug.cgi?id=21592
|
2014-02-26 14:45:03 -08:00
|
|
|
UpdateSampleRateIfNeeded(channels);
|
2013-04-09 05:47:42 -07:00
|
|
|
|
2013-03-10 18:02:22 -07:00
|
|
|
uint32_t written = 0;
|
2013-12-02 15:07:17 -08:00
|
|
|
TrackTicks streamPosition = aStream->GetCurrentPosition();
|
2013-03-10 18:02:22 -07:00
|
|
|
while (written < WEBAUDIO_BLOCK_SIZE) {
|
|
|
|
if (mStop != TRACK_TICKS_MAX &&
|
2013-12-02 15:07:17 -08:00
|
|
|
streamPosition >= mStop) {
|
|
|
|
FillWithZeroes(aOutput, channels, &written, &streamPosition, TRACK_TICKS_MAX);
|
2013-03-10 18:02:22 -07:00
|
|
|
continue;
|
|
|
|
}
|
2014-02-26 14:45:03 -08:00
|
|
|
if (streamPosition < mBeginProcessing) {
|
|
|
|
FillWithZeroes(aOutput, channels, &written, &streamPosition,
|
|
|
|
mBeginProcessing);
|
2013-03-10 18:02:22 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (mLoop) {
|
2014-02-10 14:19:26 -08:00
|
|
|
// 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;
|
2013-03-10 18:02:22 -07:00
|
|
|
}
|
2014-02-10 14:19:26 -08:00
|
|
|
CopyFromBuffer(aStream, aOutput, channels, &written, &streamPosition, mLoopEnd);
|
2013-03-10 18:02:22 -07:00
|
|
|
} else {
|
2014-02-05 11:28:42 -08:00
|
|
|
if (mBufferPosition < mBufferEnd || mRemainingResamplerTail) {
|
2014-02-10 14:19:26 -08:00
|
|
|
CopyFromBuffer(aStream, aOutput, channels, &written, &streamPosition, mBufferEnd);
|
2013-03-10 18:02:22 -07:00
|
|
|
} else {
|
2013-12-02 15:07:17 -08:00
|
|
|
FillWithZeroes(aOutput, channels, &written, &streamPosition, TRACK_TICKS_MAX);
|
2013-03-10 18:02:22 -07:00
|
|
|
}
|
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
|
|
|
|
2013-03-10 18:02:22 -07:00
|
|
|
// We've finished if we've gone past mStop, or if we're past mDuration when
|
|
|
|
// looping is disabled.
|
2013-12-03 17:20:12 -08:00
|
|
|
if (streamPosition >= mStop ||
|
2014-02-05 11:28:42 -08:00
|
|
|
(!mLoop && mBufferPosition >= mBufferEnd && !mRemainingResamplerTail)) {
|
2013-03-10 18:02:22 -07:00
|
|
|
*aFinished = true;
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
// Not owned:
|
|
|
|
// - mBuffer - shared w/ AudioNode
|
|
|
|
// - mPlaybackRateTimeline - 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2014-02-26 14:45:04 -08:00
|
|
|
double mStart; // including the fractional position between ticks
|
2014-02-26 14:45:03 -08:00
|
|
|
// 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 -TRACK_TICKS_MAX
|
|
|
|
// indicates that the resampler has begun processing.
|
|
|
|
TrackTicks mBeginProcessing;
|
2013-02-04 15:07:25 -08:00
|
|
|
TrackTicks mStop;
|
|
|
|
nsRefPtr<ThreadSharedFloatArrayBufferList> mBuffer;
|
2013-03-18 17:54:32 -07:00
|
|
|
SpeexResamplerState* mResampler;
|
2014-02-05 11:28:42 -08:00
|
|
|
// mRemainingResamplerTail, like mBufferPosition, and
|
|
|
|
// mBufferEnd, is measured in input buffer samples.
|
2013-12-03 17:20:12 -08:00
|
|
|
int mRemainingResamplerTail;
|
2014-02-05 11:27:45 -08:00
|
|
|
int32_t mBufferEnd;
|
2013-03-10 10:59:41 -07:00
|
|
|
int32_t mLoopStart;
|
|
|
|
int32_t mLoopEnd;
|
2013-05-24 10:09:51 -07:00
|
|
|
int32_t mBufferSampleRate;
|
2014-02-05 11:28:42 -08:00
|
|
|
int32_t mBufferPosition;
|
2013-03-18 17:54:32 -07:00
|
|
|
uint32_t mChannels;
|
2013-04-11 05:47:57 -07:00
|
|
|
float mDopplerShift;
|
2013-06-07 12:25:04 -07:00
|
|
|
AudioNodeStream* mDestination;
|
2013-07-12 02:23:21 -07:00
|
|
|
AudioNodeStream* mSource;
|
2013-04-09 05:47:42 -07:00
|
|
|
AudioParamTimeline mPlaybackRateTimeline;
|
2013-03-18 17:54:32 -07:00
|
|
|
bool mLoop;
|
2013-02-04 15:07:25 -08:00
|
|
|
};
|
|
|
|
|
2012-09-18 16:07:33 -07:00
|
|
|
AudioBufferSourceNode::AudioBufferSourceNode(AudioContext* aContext)
|
2013-04-27 15:44:50 -07:00
|
|
|
: AudioNode(aContext,
|
|
|
|
2,
|
|
|
|
ChannelCountMode::Max,
|
|
|
|
ChannelInterpretation::Speakers)
|
2013-03-10 09:56:14 -07:00
|
|
|
, mLoopStart(0.0)
|
|
|
|
, mLoopEnd(0.0)
|
2014-01-06 15:53:47 -08:00
|
|
|
// mOffset and mDuration are initialized in Start().
|
2013-06-27 04:30:41 -07:00
|
|
|
, mPlaybackRate(new AudioParam(MOZ_THIS_IN_INITIALIZER_LIST(),
|
|
|
|
SendPlaybackRateToStream, 1.0f))
|
2013-04-14 18:52:55 -07:00
|
|
|
, mLoop(false)
|
|
|
|
, mStartCalled(false)
|
2013-05-06 11:22:01 -07:00
|
|
|
, mStopped(false)
|
2013-02-04 15:07:25 -08:00
|
|
|
{
|
2013-07-12 02:23:21 -07:00
|
|
|
AudioBufferSourceNodeEngine* engine = new AudioBufferSourceNodeEngine(this, aContext->Destination());
|
|
|
|
mStream = aContext->Graph()->CreateAudioNodeStream(engine, MediaStreamGraph::SOURCE_STREAM);
|
|
|
|
engine->SetSourceStream(static_cast<AudioNodeStream*>(mStream.get()));
|
2013-02-04 15:07:25 -08:00
|
|
|
mStream->AddMainThreadListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioBufferSourceNode::~AudioBufferSourceNode()
|
2012-09-18 16:07:33 -07:00
|
|
|
{
|
2013-04-11 05:47:57 -07:00
|
|
|
if (Context()) {
|
|
|
|
Context()->UnregisterAudioBufferSourceNode(this);
|
|
|
|
}
|
2012-09-18 16:07:33 -07:00
|
|
|
}
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
size_t
|
|
|
|
AudioBufferSourceNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
if (mBuffer) {
|
|
|
|
amount += mBuffer->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
amount += mPlaybackRate->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
AudioBufferSourceNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-09-18 16:07:33 -07:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
AudioBufferSourceNode::WrapObject(JSContext* aCx)
|
2012-09-18 16:07:33 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return AudioBufferSourceNodeBinding::Wrap(aCx, this);
|
2012-09-18 16:07:33 -07:00
|
|
|
}
|
|
|
|
|
2012-09-24 20:31:58 -07:00
|
|
|
void
|
2013-04-22 14:01:22 -07:00
|
|
|
AudioBufferSourceNode::Start(double aWhen, double aOffset,
|
2013-02-04 15:07:25 -08:00
|
|
|
const Optional<double>& aDuration, ErrorResult& aRv)
|
2012-09-24 20:31:58 -07:00
|
|
|
{
|
2013-05-30 17:53:15 -07:00
|
|
|
if (!WebAudioUtils::IsTimeValid(aWhen) ||
|
|
|
|
(aDuration.WasPassed() && !WebAudioUtils::IsTimeValid(aDuration.Value()))) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
if (mStartCalled) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mStartCalled = true;
|
|
|
|
|
|
|
|
AudioNodeStream* ns = static_cast<AudioNodeStream*>(mStream.get());
|
2013-04-22 14:01:22 -07:00
|
|
|
if (!ns) {
|
2013-02-04 15:07:25 -08:00
|
|
|
// Nothing to play, or we're already dead for some reason
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-06 15:53:47 -08:00
|
|
|
// 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.
|
2013-04-22 14:01:22 -07:00
|
|
|
if (mBuffer) {
|
2014-01-06 15:53:47 -08:00
|
|
|
SendOffsetAndDurationParametersToStream(ns);
|
2013-04-22 14:01:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't set parameter unnecessarily
|
|
|
|
if (aWhen > 0.0) {
|
2014-02-26 14:45:04 -08:00
|
|
|
ns->SetDoubleParameter(START, mContext->DOMTimeToStreamTime(aWhen));
|
2013-04-22 14:01:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBufferSourceNode::SendBufferParameterToStream(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AudioNodeStream* ns = static_cast<AudioNodeStream*>(mStream.get());
|
|
|
|
MOZ_ASSERT(ns, "Why don't we have a stream here?");
|
|
|
|
|
|
|
|
if (mBuffer) {
|
|
|
|
float rate = mBuffer->SampleRate();
|
|
|
|
nsRefPtr<ThreadSharedFloatArrayBufferList> data =
|
|
|
|
mBuffer->GetThreadSharedChannelsForRate(aCx);
|
|
|
|
ns->SetBuffer(data.forget());
|
|
|
|
ns->SetInt32Parameter(SAMPLE_RATE, rate);
|
2013-06-25 09:00:42 -07:00
|
|
|
|
|
|
|
if (mStartCalled) {
|
2014-01-06 15:53:47 -08:00
|
|
|
SendOffsetAndDurationParametersToStream(ns);
|
2013-06-25 09:00:42 -07:00
|
|
|
}
|
2013-04-22 14:01:22 -07:00
|
|
|
} else {
|
|
|
|
ns->SetBuffer(nullptr);
|
2014-02-05 11:33:16 -08:00
|
|
|
|
|
|
|
MarkInactive();
|
2013-04-22 14:01:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-06 15:53:47 -08:00
|
|
|
AudioBufferSourceNode::SendOffsetAndDurationParametersToStream(AudioNodeStream* aStream)
|
2013-04-22 14:01:22 -07:00
|
|
|
{
|
2013-06-25 09:00:42 -07:00
|
|
|
NS_ASSERTION(mBuffer && mStartCalled,
|
|
|
|
"Only call this when we have a buffer and start() has been called");
|
|
|
|
|
|
|
|
float rate = mBuffer->SampleRate();
|
2014-02-05 11:27:45 -08:00
|
|
|
int32_t bufferEnd = mBuffer->Length();
|
2014-01-06 15:53:47 -08:00
|
|
|
int32_t offsetSamples = std::max(0, NS_lround(mOffset * rate));
|
2013-03-18 17:54:32 -07:00
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
// Don't set parameter unnecessarily
|
2013-12-02 15:07:17 -08:00
|
|
|
if (offsetSamples > 0) {
|
2014-02-05 11:27:45 -08:00
|
|
|
aStream->SetInt32Parameter(BUFFERSTART, offsetSamples);
|
2013-12-02 15:07:17 -08:00
|
|
|
}
|
|
|
|
|
2014-01-06 15:53:47 -08:00
|
|
|
if (mDuration != std::numeric_limits<double>::min()) {
|
2014-02-05 11:27:45 -08:00
|
|
|
bufferEnd = std::min(bufferEnd,
|
|
|
|
offsetSamples + NS_lround(mDuration * rate));
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
2014-02-05 11:27:45 -08:00
|
|
|
aStream->SetInt32Parameter(BUFFEREND, bufferEnd);
|
2014-02-05 11:33:16 -08:00
|
|
|
|
|
|
|
MarkActive();
|
2012-09-24 20:31:58 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
void
|
2013-09-23 18:47:00 -07:00
|
|
|
AudioBufferSourceNode::Stop(double aWhen, ErrorResult& aRv)
|
2013-02-04 15:07:25 -08:00
|
|
|
{
|
2013-05-30 17:53:15 -07:00
|
|
|
if (!WebAudioUtils::IsTimeValid(aWhen)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
if (!mStartCalled) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioNodeStream* ns = static_cast<AudioNodeStream*>(mStream.get());
|
2013-04-24 19:24:25 -07:00
|
|
|
if (!ns || !Context()) {
|
2013-02-04 15:07:25 -08:00
|
|
|
// We've already stopped and had our stream shut down
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-15 03:08:20 -08:00
|
|
|
ns->SetStreamTimeParameter(STOP, Context(), std::max(0.0, aWhen));
|
2012-09-18 16:07:33 -07:00
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
AudioBufferSourceNode::NotifyMainThreadStateChanged()
|
|
|
|
{
|
|
|
|
if (mStream->IsFinished()) {
|
2013-05-06 11:22:01 -07:00
|
|
|
class EndedEventDispatcher : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit EndedEventDispatcher(AudioBufferSourceNode* aNode)
|
|
|
|
: mNode(aNode) {}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
// 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"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<AudioBufferSourceNode> mNode;
|
|
|
|
};
|
|
|
|
if (!mStopped) {
|
|
|
|
// Only dispatch the ended event once
|
|
|
|
NS_DispatchToMainThread(new EndedEventDispatcher(this));
|
|
|
|
mStopped = true;
|
|
|
|
}
|
|
|
|
|
2013-04-14 18:52:55 -07:00
|
|
|
// Drop the playing reference
|
|
|
|
// Warning: The below line might delete this.
|
2013-09-23 18:47:00 -07:00
|
|
|
MarkInactive();
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
2012-09-18 16:07:33 -07:00
|
|
|
}
|
|
|
|
|
2013-04-09 05:47:42 -07:00
|
|
|
void
|
|
|
|
AudioBufferSourceNode::SendPlaybackRateToStream(AudioNode* aNode)
|
|
|
|
{
|
|
|
|
AudioBufferSourceNode* This = static_cast<AudioBufferSourceNode*>(aNode);
|
2013-04-21 21:22:33 -07:00
|
|
|
SendTimelineParameterToStream(This, PLAYBACKRATE, *This->mPlaybackRate);
|
2013-04-09 05:47:42 -07:00
|
|
|
}
|
|
|
|
|
2013-04-11 05:47:57 -07:00
|
|
|
void
|
|
|
|
AudioBufferSourceNode::SendDopplerShiftToStream(double aDopplerShift)
|
|
|
|
{
|
2013-04-21 21:22:33 -07:00
|
|
|
SendDoubleParameterToStream(DOPPLERSHIFT, aDopplerShift);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioBufferSourceNode::SendLoopParametersToStream()
|
|
|
|
{
|
|
|
|
// 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;
|
2013-10-22 22:40:11 -07:00
|
|
|
if (mLoopStart >= 0.0 && mLoopEnd > 0.0 &&
|
2013-04-21 21:22:33 -07:00
|
|
|
mLoopStart < mLoopEnd) {
|
2013-10-22 22:40:11 -07:00
|
|
|
MOZ_ASSERT(mLoopStart != 0.0 || mLoopEnd != 0.0);
|
2013-04-21 21:22:33 -07:00
|
|
|
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);
|
2013-04-25 06:34:47 -07:00
|
|
|
if (loopStartTicks < loopEndTicks) {
|
|
|
|
SendInt32ParameterToStream(LOOPSTART, loopStartTicks);
|
|
|
|
SendInt32ParameterToStream(LOOPEND, loopEndTicks);
|
|
|
|
SendInt32ParameterToStream(LOOP, 1);
|
2013-06-02 06:26:26 -07:00
|
|
|
} else {
|
|
|
|
// Be explicit about looping not happening if the offsets make
|
|
|
|
// looping impossible.
|
|
|
|
SendInt32ParameterToStream(LOOP, 0);
|
2013-04-25 06:34:47 -07:00
|
|
|
}
|
|
|
|
} else if (!mLoop) {
|
|
|
|
SendInt32ParameterToStream(LOOP, 0);
|
2013-04-21 21:22:33 -07:00
|
|
|
}
|
2013-04-11 05:47:57 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
|
|
|
}
|