Line data Source code
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/. */
6 :
7 : #ifndef AudioBufferSourceNode_h_
8 : #define AudioBufferSourceNode_h_
9 :
10 : #include "AudioScheduledSourceNode.h"
11 : #include "AudioBuffer.h"
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 : struct AudioBufferSourceOptions;
17 : class AudioParam;
18 :
19 : class AudioBufferSourceNode final : public AudioScheduledSourceNode
20 : , public MainThreadMediaStreamListener
21 : {
22 : public:
23 : static already_AddRefed<AudioBufferSourceNode>
24 : Create(JSContext* aCx, AudioContext& aAudioContext,
25 : const AudioBufferSourceOptions& aOptions, ErrorResult& aRv);
26 :
27 : void DestroyMediaStream() override;
28 :
29 0 : uint16_t NumberOfInputs() const final override
30 : {
31 0 : return 0;
32 : }
33 0 : AudioBufferSourceNode* AsAudioBufferSourceNode() override
34 : {
35 0 : return this;
36 : }
37 : NS_DECL_ISUPPORTS_INHERITED
38 0 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioBufferSourceNode,
39 : AudioScheduledSourceNode)
40 :
41 : static already_AddRefed<AudioBufferSourceNode>
42 0 : Constructor(const GlobalObject& aGlobal, AudioContext& aAudioContext,
43 : const AudioBufferSourceOptions& aOptions, ErrorResult& aRv)
44 : {
45 0 : return Create(aGlobal.Context(), aAudioContext, aOptions, aRv);
46 : }
47 :
48 : JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
49 :
50 : void Start(double aWhen, double aOffset,
51 : const Optional<double>& aDuration, ErrorResult& aRv);
52 :
53 : void Start(double aWhen, ErrorResult& aRv) override;
54 : void Stop(double aWhen, ErrorResult& aRv) override;
55 :
56 0 : AudioBuffer* GetBuffer(JSContext* aCx) const
57 : {
58 0 : return mBuffer;
59 : }
60 0 : void SetBuffer(JSContext* aCx, AudioBuffer* aBuffer)
61 : {
62 0 : mBuffer = aBuffer;
63 0 : SendBufferParameterToStream(aCx);
64 0 : SendLoopParametersToStream();
65 0 : }
66 0 : AudioParam* PlaybackRate() const
67 : {
68 0 : return mPlaybackRate;
69 : }
70 0 : AudioParam* Detune() const
71 : {
72 0 : return mDetune;
73 : }
74 0 : bool Loop() const
75 : {
76 0 : return mLoop;
77 : }
78 0 : void SetLoop(bool aLoop)
79 : {
80 0 : mLoop = aLoop;
81 0 : SendLoopParametersToStream();
82 0 : }
83 0 : double LoopStart() const
84 : {
85 0 : return mLoopStart;
86 : }
87 0 : void SetLoopStart(double aStart)
88 : {
89 0 : mLoopStart = aStart;
90 0 : SendLoopParametersToStream();
91 0 : }
92 0 : double LoopEnd() const
93 : {
94 0 : return mLoopEnd;
95 : }
96 0 : void SetLoopEnd(double aEnd)
97 : {
98 0 : mLoopEnd = aEnd;
99 0 : SendLoopParametersToStream();
100 0 : }
101 : void SendDopplerShiftToStream(double aDopplerShift);
102 :
103 : void NotifyMainThreadStreamFinished() override;
104 :
105 0 : const char* NodeType() const override
106 : {
107 0 : return "AudioBufferSourceNode";
108 : }
109 :
110 : size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
111 : size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
112 :
113 : private:
114 : explicit AudioBufferSourceNode(AudioContext* aContext);
115 0 : ~AudioBufferSourceNode() = default;
116 :
117 : friend class AudioBufferSourceNodeEngine;
118 : // START is sent during Start().
119 : // STOP is sent during Stop().
120 : // BUFFERSTART and BUFFEREND are sent when SetBuffer() and Start() have
121 : // been called (along with sending the buffer).
122 : enum EngineParameters {
123 : SAMPLE_RATE,
124 : START,
125 : STOP,
126 : // BUFFERSTART is the "offset" passed to start(), multiplied by
127 : // buffer.sampleRate.
128 : BUFFERSTART,
129 : // BUFFEREND is the sum of "offset" and "duration" passed to start(),
130 : // multiplied by buffer.sampleRate, or the size of the buffer, if smaller.
131 : BUFFEREND,
132 : LOOP,
133 : LOOPSTART,
134 : LOOPEND,
135 : PLAYBACKRATE,
136 : DETUNE,
137 : DOPPLERSHIFT
138 : };
139 :
140 : void SendLoopParametersToStream();
141 : void SendBufferParameterToStream(JSContext* aCx);
142 : void SendOffsetAndDurationParametersToStream(AudioNodeStream* aStream);
143 :
144 : double mLoopStart;
145 : double mLoopEnd;
146 : double mOffset;
147 : double mDuration;
148 : RefPtr<AudioBuffer> mBuffer;
149 : RefPtr<AudioParam> mPlaybackRate;
150 : RefPtr<AudioParam> mDetune;
151 : bool mLoop;
152 : bool mStartCalled;
153 : };
154 :
155 : } // namespace dom
156 : } // namespace mozilla
157 :
158 : #endif
|