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 #ifndef AudioBufferSourceNode_h_
8 #define AudioBufferSourceNode_h_
10 #include "AudioScheduledSourceNode.h"
11 #include "AudioBuffer.h"
13 namespace mozilla::dom
{
15 struct AudioBufferSourceOptions
;
18 class AudioBufferSourceNode final
: public AudioScheduledSourceNode
,
19 public MainThreadMediaTrackListener
{
21 static already_AddRefed
<AudioBufferSourceNode
> Create(
22 JSContext
* aCx
, AudioContext
& aAudioContext
,
23 const AudioBufferSourceOptions
& aOptions
);
25 void DestroyMediaTrack() override
;
27 uint16_t NumberOfInputs() const final
{ return 0; }
28 AudioBufferSourceNode
* AsAudioBufferSourceNode() override
{ return this; }
29 NS_DECL_ISUPPORTS_INHERITED
30 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioBufferSourceNode
,
31 AudioScheduledSourceNode
)
33 static already_AddRefed
<AudioBufferSourceNode
> Constructor(
34 const GlobalObject
& aGlobal
, AudioContext
& aAudioContext
,
35 const AudioBufferSourceOptions
& aOptions
) {
36 return Create(aGlobal
.Context(), aAudioContext
, aOptions
);
39 JSObject
* WrapObject(JSContext
* aCx
,
40 JS::Handle
<JSObject
*> aGivenProto
) override
;
42 void Start(double aWhen
, double aOffset
, const Optional
<double>& aDuration
,
45 void Start(double aWhen
, ErrorResult
& aRv
) override
;
46 void Stop(double aWhen
, ErrorResult
& aRv
) override
;
48 AudioBuffer
* GetBuffer(JSContext
* aCx
) const { return mBuffer
; }
49 void SetBuffer(JSContext
* aCx
, AudioBuffer
* aBuffer
, ErrorResult
& aRv
) {
50 if (aBuffer
&& mBufferSet
) {
51 aRv
.ThrowInvalidStateError(
52 "Cannot set the buffer attribute of an AudioBufferSourceNode "
53 "with an AudioBuffer more than once");
60 SendBufferParameterToTrack(aCx
);
61 SendLoopParametersToTrack();
63 AudioParam
* PlaybackRate() const { return mPlaybackRate
; }
64 AudioParam
* Detune() const { return mDetune
; }
65 bool Loop() const { return mLoop
; }
66 void SetLoop(bool aLoop
) {
68 SendLoopParametersToTrack();
70 double LoopStart() const { return mLoopStart
; }
71 void SetLoopStart(double aStart
) {
73 SendLoopParametersToTrack();
75 double LoopEnd() const { return mLoopEnd
; }
76 void SetLoopEnd(double aEnd
) {
78 SendLoopParametersToTrack();
80 void NotifyMainThreadTrackEnded() override
;
82 const char* NodeType() const override
{ return "AudioBufferSourceNode"; }
84 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const override
;
85 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const override
;
88 explicit AudioBufferSourceNode(AudioContext
* aContext
);
89 ~AudioBufferSourceNode() = default;
91 friend class AudioBufferSourceNodeEngine
;
92 // START is sent during Start().
93 // STOP is sent during Stop().
94 // BUFFERSTART and DURATION are sent when SetBuffer() and Start() have
95 // been called (along with sending the buffer).
96 enum EngineParameters
{
100 // BUFFERSTART is the "offset" passed to start(), multiplied by
101 // buffer.sampleRate.
111 void SendLoopParametersToTrack();
112 void SendBufferParameterToTrack(JSContext
* aCx
);
113 void SendOffsetAndDurationParametersToTrack(AudioNodeTrack
* aTrack
);
119 RefPtr
<AudioBuffer
> mBuffer
;
120 RefPtr
<AudioParam
> mPlaybackRate
;
121 RefPtr
<AudioParam
> mDetune
;
127 } // namespace mozilla::dom