Bug 867203 - Part 2: Remove useless mPannerNode member in AudioBufferSourceNode....
[gecko.git] / content / media / webaudio / AudioBufferSourceNode.cpp
blob27ebbcad029c900d62e158f1fb9cd95f3acebc22
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "AudioBufferSourceNode.h"
8 #include "mozilla/dom/AudioBufferSourceNodeBinding.h"
9 #include "nsMathUtils.h"
10 #include "AudioNodeEngine.h"
11 #include "AudioNodeStream.h"
12 #include "AudioDestinationNode.h"
13 #include "PannerNode.h"
14 #include "speex/speex_resampler.h"
15 #include <limits>
17 namespace mozilla {
18 namespace dom {
20 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioBufferSourceNode)
21 NS_IMPL_CYCLE_COLLECTION_UNLINK(mBuffer)
22 NS_IMPL_CYCLE_COLLECTION_UNLINK(mPlaybackRate)
23 if (tmp->Context()) {
24 // AudioNode's Unlink implementation disconnects us from the graph
25 // too, but we need to do this right here to make sure that
26 // UnregisterAudioBufferSourceNode can properly untangle us from
27 // the possibly connected PannerNodes.
28 tmp->DisconnectFromGraph();
29 tmp->Context()->UnregisterAudioBufferSourceNode(tmp);
31 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(AudioNode)
33 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AudioBufferSourceNode, AudioNode)
34 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBuffer)
35 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPlaybackRate)
36 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
38 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AudioBufferSourceNode)
39 NS_INTERFACE_MAP_END_INHERITING(AudioNode)
41 NS_IMPL_ADDREF_INHERITED(AudioBufferSourceNode, AudioNode)
42 NS_IMPL_RELEASE_INHERITED(AudioBufferSourceNode, AudioNode)
44 class AudioBufferSourceNodeEngine : public AudioNodeEngine
46 public:
47 explicit AudioBufferSourceNodeEngine(AudioNode* aNode,
48 AudioDestinationNode* aDestination) :
49 AudioNodeEngine(aNode),
50 mStart(0), mStop(TRACK_TICKS_MAX),
51 mResampler(nullptr),
52 mOffset(0), mDuration(0),
53 mLoopStart(0), mLoopEnd(0),
54 mSampleRate(0), mPosition(0), mChannels(0), mPlaybackRate(1.0f),
55 mDopplerShift(1.0f),
56 mDestination(static_cast<AudioNodeStream*>(aDestination->Stream())),
57 mPlaybackRateTimeline(1.0f), mLoop(false)
60 ~AudioBufferSourceNodeEngine()
62 if (mResampler) {
63 speex_resampler_destroy(mResampler);
67 virtual void SetTimelineParameter(uint32_t aIndex, const dom::AudioParamTimeline& aValue)
69 switch (aIndex) {
70 case AudioBufferSourceNode::PLAYBACKRATE:
71 mPlaybackRateTimeline = aValue;
72 // If we have a simple value that is 1.0 (i.e. intrinsic speed), and our
73 // input buffer is already at the ideal audio rate, and we have a
74 // resampler, we can release it.
75 if (mResampler && mPlaybackRateTimeline.HasSimpleValue() &&
76 mPlaybackRateTimeline.GetValue() == 1.0 &&
77 mSampleRate == IdealAudioRate()) {
78 speex_resampler_destroy(mResampler);
79 mResampler = nullptr;
81 WebAudioUtils::ConvertAudioParamToTicks(mPlaybackRateTimeline, nullptr, mDestination);
82 break;
83 default:
84 NS_ERROR("Bad GainNodeEngine TimelineParameter");
87 virtual void SetStreamTimeParameter(uint32_t aIndex, TrackTicks aParam)
89 switch (aIndex) {
90 case AudioBufferSourceNode::START: mStart = aParam; break;
91 case AudioBufferSourceNode::STOP: mStop = aParam; break;
92 default:
93 NS_ERROR("Bad AudioBufferSourceNodeEngine StreamTimeParameter");
96 virtual void SetDoubleParameter(uint32_t aIndex, double aParam)
98 switch (aIndex) {
99 case AudioBufferSourceNode::DOPPLERSHIFT:
100 mDopplerShift = aParam;
101 break;
102 default:
103 NS_ERROR("Bad AudioBufferSourceNodeEngine double parameter.");
106 virtual void SetInt32Parameter(uint32_t aIndex, int32_t aParam)
108 switch (aIndex) {
109 case AudioBufferSourceNode::SAMPLE_RATE: mSampleRate = aParam; break;
110 case AudioBufferSourceNode::OFFSET: mOffset = aParam; break;
111 case AudioBufferSourceNode::DURATION: mDuration = aParam; break;
112 case AudioBufferSourceNode::LOOP: mLoop = !!aParam; break;
113 case AudioBufferSourceNode::LOOPSTART: mLoopStart = aParam; break;
114 case AudioBufferSourceNode::LOOPEND: mLoopEnd = aParam; break;
115 default:
116 NS_ERROR("Bad AudioBufferSourceNodeEngine Int32Parameter");
119 virtual void SetBuffer(already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer)
121 mBuffer = aBuffer;
124 SpeexResamplerState* Resampler(uint32_t aChannels)
126 if (aChannels != mChannels && mResampler) {
127 speex_resampler_destroy(mResampler);
128 mResampler = nullptr;
131 if (!mResampler) {
132 mChannels = aChannels;
133 mResampler = speex_resampler_init(mChannels, mSampleRate,
134 ComputeFinalOutSampleRate(),
135 SPEEX_RESAMPLER_QUALITY_DEFAULT,
136 nullptr);
138 return mResampler;
141 // Borrow a full buffer of size WEBAUDIO_BLOCK_SIZE from the source buffer
142 // at offset aSourceOffset. This avoids copying memory.
143 void BorrowFromInputBuffer(AudioChunk* aOutput,
144 uint32_t aChannels,
145 uintptr_t aSourceOffset)
147 aOutput->mDuration = WEBAUDIO_BLOCK_SIZE;
148 aOutput->mBuffer = mBuffer;
149 aOutput->mChannelData.SetLength(aChannels);
150 for (uint32_t i = 0; i < aChannels; ++i) {
151 aOutput->mChannelData[i] = mBuffer->GetData(i) + aSourceOffset;
153 aOutput->mVolume = 1.0f;
154 aOutput->mBufferFormat = AUDIO_FORMAT_FLOAT32;
157 // Copy aNumberOfFrames frames from the source buffer at offset aSourceOffset
158 // and put it at offset aBufferOffset in the destination buffer.
159 void CopyFromInputBuffer(AudioChunk* aOutput,
160 uint32_t aChannels,
161 uintptr_t aSourceOffset,
162 uintptr_t aBufferOffset,
163 uint32_t aNumberOfFrames) {
164 for (uint32_t i = 0; i < aChannels; ++i) {
165 float* baseChannelData = static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i]));
166 memcpy(baseChannelData + aBufferOffset,
167 mBuffer->GetData(i) + aSourceOffset,
168 aNumberOfFrames * sizeof(float));
172 // Resamples input data to an output buffer, according to |mSampleRate| and
173 // the playbackRate.
174 // The number of frames consumed/produced depends on the amount of space
175 // remaining in both the input and output buffer, and the playback rate (that
176 // is, the ratio between the output samplerate and the input samplerate).
177 void CopyFromInputBufferWithResampling(AudioChunk* aOutput,
178 uint32_t aChannels,
179 uintptr_t aSourceOffset,
180 uintptr_t aBufferOffset,
181 uint32_t aAvailableInInputBuffer,
182 uint32_t& aFramesRead,
183 uint32_t& aFramesWritten) {
184 double finalPlaybackRate = static_cast<double>(mSampleRate) / ComputeFinalOutSampleRate();
185 uint32_t availableInOuputBuffer = WEBAUDIO_BLOCK_SIZE - aBufferOffset;
186 uint32_t inputSamples, outputSamples;
188 // Check if we are short on input or output buffer.
189 if (aAvailableInInputBuffer < availableInOuputBuffer * finalPlaybackRate) {
190 outputSamples = ceil(aAvailableInInputBuffer / finalPlaybackRate);
191 inputSamples = aAvailableInInputBuffer;
192 } else {
193 inputSamples = ceil(availableInOuputBuffer * finalPlaybackRate);
194 outputSamples = availableInOuputBuffer;
197 SpeexResamplerState* resampler = Resampler(aChannels);
199 for (uint32_t i = 0; i < aChannels; ++i) {
200 uint32_t inSamples = inputSamples;
201 uint32_t outSamples = outputSamples;
203 const float* inputData = mBuffer->GetData(i) + aSourceOffset;
204 float* outputData =
205 static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i])) +
206 aBufferOffset;
208 speex_resampler_process_float(resampler, i,
209 inputData, &inSamples,
210 outputData, &outSamples);
212 aFramesRead = inSamples;
213 aFramesWritten = outSamples;
218 * Fill aOutput with as many zero frames as we can, and advance
219 * aOffsetWithinBlock and aCurrentPosition based on how many frames we write.
220 * This will never advance aOffsetWithinBlock past WEBAUDIO_BLOCK_SIZE or
221 * aCurrentPosition past aMaxPos. This function knows when it needs to
222 * allocate the output buffer, and also optimizes the case where it can avoid
223 * memory allocations.
225 void FillWithZeroes(AudioChunk* aOutput,
226 uint32_t aChannels,
227 uint32_t* aOffsetWithinBlock,
228 TrackTicks* aCurrentPosition,
229 TrackTicks aMaxPos)
231 uint32_t numFrames = std::min(WEBAUDIO_BLOCK_SIZE - *aOffsetWithinBlock,
232 uint32_t(aMaxPos - *aCurrentPosition));
233 if (numFrames == WEBAUDIO_BLOCK_SIZE) {
234 aOutput->SetNull(numFrames);
235 } else {
236 if (aOutput->IsNull()) {
237 AllocateAudioBlock(aChannels, aOutput);
239 WriteZeroesToAudioBlock(aOutput, *aOffsetWithinBlock, numFrames);
241 *aOffsetWithinBlock += numFrames;
242 *aCurrentPosition += numFrames;
246 * Copy as many frames as possible from the source buffer to aOutput, and
247 * advance aOffsetWithinBlock and aCurrentPosition based on how many frames
248 * we copy. This will never advance aOffsetWithinBlock past
249 * WEBAUDIO_BLOCK_SIZE, or aCurrentPosition past mStop. It takes data from
250 * the buffer at aBufferOffset, and never takes more data than aBufferMax.
251 * This function knows when it needs to allocate the output buffer, and also
252 * optimizes the case where it can avoid memory allocations.
254 void CopyFromBuffer(AudioChunk* aOutput,
255 uint32_t aChannels,
256 uint32_t* aOffsetWithinBlock,
257 TrackTicks* aCurrentPosition,
258 uint32_t aBufferOffset,
259 uint32_t aBufferMax)
261 uint32_t numFrames = std::min(std::min(WEBAUDIO_BLOCK_SIZE - *aOffsetWithinBlock,
262 aBufferMax - aBufferOffset),
263 uint32_t(mStop - *aCurrentPosition));
264 if (numFrames == WEBAUDIO_BLOCK_SIZE && !ShouldResample()) {
265 BorrowFromInputBuffer(aOutput, aChannels, aBufferOffset);
266 *aOffsetWithinBlock += numFrames;
267 *aCurrentPosition += numFrames;
268 mPosition += numFrames;
269 } else {
270 if (aOutput->IsNull()) {
271 MOZ_ASSERT(*aOffsetWithinBlock == 0);
272 AllocateAudioBlock(aChannels, aOutput);
274 if (!ShouldResample()) {
275 CopyFromInputBuffer(aOutput, aChannels, aBufferOffset, *aOffsetWithinBlock, numFrames);
276 *aOffsetWithinBlock += numFrames;
277 *aCurrentPosition += numFrames;
278 mPosition += numFrames;
279 } else {
280 uint32_t framesRead, framesWritten, availableInInputBuffer;
282 availableInInputBuffer = aBufferMax - aBufferOffset;
284 CopyFromInputBufferWithResampling(aOutput, aChannels, aBufferOffset, *aOffsetWithinBlock, availableInInputBuffer, framesRead, framesWritten);
285 *aOffsetWithinBlock += framesWritten;
286 *aCurrentPosition += framesRead;
287 mPosition += framesRead;
292 TrackTicks GetPosition(AudioNodeStream* aStream)
294 if (aStream->GetCurrentPosition() < mStart) {
295 return aStream->GetCurrentPosition();
297 return mStart + mPosition;
300 int32_t ComputeFinalOutSampleRate() const
302 return static_cast<uint32_t>(IdealAudioRate() / (mPlaybackRate * mDopplerShift));
305 bool ShouldResample() const
307 return !(mPlaybackRate == 1.0 &&
308 mDopplerShift == 1.0 &&
309 mSampleRate == IdealAudioRate());
312 void UpdateSampleRateIfNeeded(AudioNodeStream* aStream)
314 if (mPlaybackRateTimeline.HasSimpleValue()) {
315 mPlaybackRate = mPlaybackRateTimeline.GetValue();
316 } else {
317 mPlaybackRate = mPlaybackRateTimeline.GetValueAtTime(aStream->GetCurrentPosition());
320 // Make sure the playback rate if something our resampler can work with.
321 if (mPlaybackRate <= 0.0 || mPlaybackRate >= 1024) {
322 mPlaybackRate = 1.0;
325 uint32_t currentOutSampleRate, currentInSampleRate;
326 if (ShouldResample()) {
327 SpeexResamplerState* resampler = Resampler(mChannels);
328 speex_resampler_get_rate(resampler, &currentInSampleRate, &currentOutSampleRate);
329 uint32_t finalSampleRate = ComputeFinalOutSampleRate();
330 if (currentOutSampleRate != finalSampleRate) {
331 speex_resampler_set_rate(resampler, currentInSampleRate, finalSampleRate);
336 virtual void ProduceAudioBlock(AudioNodeStream* aStream,
337 const AudioChunk& aInput,
338 AudioChunk* aOutput,
339 bool* aFinished)
341 if (!mBuffer)
342 return;
344 uint32_t channels = mBuffer->GetChannels();
345 if (!channels) {
346 aOutput->SetNull(WEBAUDIO_BLOCK_SIZE);
347 return;
350 // WebKit treats the playbackRate as a k-rate parameter in their code,
351 // despite the spec saying that it should be an a-rate parameter. We treat
352 // it as k-rate. Spec bug: https://www.w3.org/Bugs/Public/show_bug.cgi?id=21592
353 UpdateSampleRateIfNeeded(aStream);
355 uint32_t written = 0;
356 TrackTicks currentPosition = GetPosition(aStream);
357 while (written < WEBAUDIO_BLOCK_SIZE) {
358 if (mStop != TRACK_TICKS_MAX &&
359 currentPosition >= mStop) {
360 FillWithZeroes(aOutput, channels, &written, &currentPosition, TRACK_TICKS_MAX);
361 continue;
363 if (currentPosition < mStart) {
364 FillWithZeroes(aOutput, channels, &written, &currentPosition, mStart);
365 continue;
367 TrackTicks t = currentPosition - mStart;
368 if (mLoop) {
369 if (mOffset + t < mLoopEnd) {
370 CopyFromBuffer(aOutput, channels, &written, &currentPosition, mOffset + t, mLoopEnd);
371 } else {
372 uint32_t offsetInLoop = (mOffset + t - mLoopEnd) % (mLoopEnd - mLoopStart);
373 CopyFromBuffer(aOutput, channels, &written, &currentPosition, mLoopStart + offsetInLoop, mLoopEnd);
375 } else {
376 if (mOffset + t < mDuration) {
377 CopyFromBuffer(aOutput, channels, &written, &currentPosition, mOffset + t, mDuration);
378 } else {
379 FillWithZeroes(aOutput, channels, &written, &currentPosition, TRACK_TICKS_MAX);
384 // We've finished if we've gone past mStop, or if we're past mDuration when
385 // looping is disabled.
386 if (currentPosition >= mStop ||
387 (!mLoop && currentPosition - mStart + mOffset > mDuration)) {
388 *aFinished = true;
392 TrackTicks mStart;
393 TrackTicks mStop;
394 nsRefPtr<ThreadSharedFloatArrayBufferList> mBuffer;
395 SpeexResamplerState* mResampler;
396 int32_t mOffset;
397 int32_t mDuration;
398 int32_t mLoopStart;
399 int32_t mLoopEnd;
400 int32_t mSampleRate;
401 uint32_t mPosition;
402 uint32_t mChannels;
403 float mPlaybackRate;
404 float mDopplerShift;
405 AudioNodeStream* mDestination;
406 AudioParamTimeline mPlaybackRateTimeline;
407 bool mLoop;
410 AudioBufferSourceNode::AudioBufferSourceNode(AudioContext* aContext)
411 : AudioNode(aContext,
413 ChannelCountMode::Max,
414 ChannelInterpretation::Speakers)
415 , mLoopStart(0.0)
416 , mLoopEnd(0.0)
417 , mOffset(0.0)
418 , mDuration(std::numeric_limits<double>::min())
419 , mPlaybackRate(new AudioParam(this, SendPlaybackRateToStream, 1.0f))
420 , mLoop(false)
421 , mStartCalled(false)
422 , mOffsetAndDurationRemembered(false)
424 mStream = aContext->Graph()->CreateAudioNodeStream(
425 new AudioBufferSourceNodeEngine(this, aContext->Destination()),
426 MediaStreamGraph::INTERNAL_STREAM);
427 mStream->AddMainThreadListener(this);
430 AudioBufferSourceNode::~AudioBufferSourceNode()
432 if (Context()) {
433 Context()->UnregisterAudioBufferSourceNode(this);
437 JSObject*
438 AudioBufferSourceNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
440 return AudioBufferSourceNodeBinding::Wrap(aCx, aScope, this);
443 void
444 AudioBufferSourceNode::Start(double aWhen, double aOffset,
445 const Optional<double>& aDuration, ErrorResult& aRv)
447 if (mStartCalled) {
448 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
449 return;
451 mStartCalled = true;
453 AudioNodeStream* ns = static_cast<AudioNodeStream*>(mStream.get());
454 if (!ns) {
455 // Nothing to play, or we're already dead for some reason
456 return;
459 if (mBuffer) {
460 double duration = aDuration.WasPassed() ?
461 aDuration.Value() :
462 std::numeric_limits<double>::min();
463 SendOffsetAndDurationParametersToStream(ns, aOffset, duration);
464 } else {
465 // Remember our argument so that we can use them once we have a buffer
466 mOffset = aOffset;
467 mDuration = aDuration.WasPassed() ?
468 aDuration.Value() :
469 std::numeric_limits<double>::min();
470 mOffsetAndDurationRemembered = true;
473 // Don't set parameter unnecessarily
474 if (aWhen > 0.0) {
475 ns->SetStreamTimeParameter(START, Context()->DestinationStream(), aWhen);
478 MOZ_ASSERT(!mPlayingRef, "We can only accept a successful start() call once");
479 mPlayingRef.Take(this);
482 void
483 AudioBufferSourceNode::SendBufferParameterToStream(JSContext* aCx)
485 AudioNodeStream* ns = static_cast<AudioNodeStream*>(mStream.get());
486 MOZ_ASSERT(ns, "Why don't we have a stream here?");
488 if (mBuffer) {
489 float rate = mBuffer->SampleRate();
490 nsRefPtr<ThreadSharedFloatArrayBufferList> data =
491 mBuffer->GetThreadSharedChannelsForRate(aCx);
492 ns->SetBuffer(data.forget());
493 ns->SetInt32Parameter(SAMPLE_RATE, rate);
494 } else {
495 ns->SetBuffer(nullptr);
498 if (mOffsetAndDurationRemembered) {
499 SendOffsetAndDurationParametersToStream(ns, mOffset, mDuration);
503 void
504 AudioBufferSourceNode::SendOffsetAndDurationParametersToStream(AudioNodeStream* aStream,
505 double aOffset,
506 double aDuration)
508 float rate = mBuffer ? mBuffer->SampleRate() : Context()->SampleRate();
509 int32_t lengthSamples = mBuffer ? mBuffer->Length() : 0;
510 double length = double(lengthSamples) / rate;
511 double offset = std::max(0.0, aOffset);
512 double endOffset = aDuration == std::numeric_limits<double>::min() ?
513 length : std::min(aOffset + aDuration, length);
515 if (offset >= endOffset) {
516 return;
519 int32_t offsetTicks = NS_lround(offset*rate);
520 // Don't set parameter unnecessarily
521 if (offsetTicks > 0) {
522 aStream->SetInt32Parameter(OFFSET, offsetTicks);
524 aStream->SetInt32Parameter(DURATION, NS_lround(endOffset*rate) - offsetTicks);
527 void
528 AudioBufferSourceNode::Stop(double aWhen, ErrorResult& aRv)
530 if (!mStartCalled) {
531 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
532 return;
535 if (!mBuffer) {
536 // We don't have a buffer, so the stream is never marked as finished.
537 // Therefore we need to drop our playing ref right now.
538 mPlayingRef.Drop(this);
541 AudioNodeStream* ns = static_cast<AudioNodeStream*>(mStream.get());
542 if (!ns || !Context()) {
543 // We've already stopped and had our stream shut down
544 return;
547 ns->SetStreamTimeParameter(STOP, Context()->DestinationStream(),
548 std::max(0.0, aWhen));
551 void
552 AudioBufferSourceNode::NotifyMainThreadStateChanged()
554 if (mStream->IsFinished()) {
555 // Drop the playing reference
556 // Warning: The below line might delete this.
557 mPlayingRef.Drop(this);
561 void
562 AudioBufferSourceNode::SendPlaybackRateToStream(AudioNode* aNode)
564 AudioBufferSourceNode* This = static_cast<AudioBufferSourceNode*>(aNode);
565 SendTimelineParameterToStream(This, PLAYBACKRATE, *This->mPlaybackRate);
568 void
569 AudioBufferSourceNode::SendDopplerShiftToStream(double aDopplerShift)
571 SendDoubleParameterToStream(DOPPLERSHIFT, aDopplerShift);
574 void
575 AudioBufferSourceNode::SendLoopParametersToStream()
577 // Don't compute and set the loop parameters unnecessarily
578 if (mLoop && mBuffer) {
579 float rate = mBuffer->SampleRate();
580 double length = (double(mBuffer->Length()) / mBuffer->SampleRate());
581 double actualLoopStart, actualLoopEnd;
582 if (((mLoopStart != 0.0) || (mLoopEnd != 0.0)) &&
583 mLoopStart >= 0.0 && mLoopEnd > 0.0 &&
584 mLoopStart < mLoopEnd) {
585 actualLoopStart = (mLoopStart > length) ? 0.0 : mLoopStart;
586 actualLoopEnd = std::min(mLoopEnd, length);
587 } else {
588 actualLoopStart = 0.0;
589 actualLoopEnd = length;
591 int32_t loopStartTicks = NS_lround(actualLoopStart * rate);
592 int32_t loopEndTicks = NS_lround(actualLoopEnd * rate);
593 if (loopStartTicks < loopEndTicks) {
594 SendInt32ParameterToStream(LOOPSTART, loopStartTicks);
595 SendInt32ParameterToStream(LOOPEND, loopEndTicks);
596 SendInt32ParameterToStream(LOOP, 1);
598 } else if (!mLoop) {
599 SendInt32ParameterToStream(LOOP, 0);