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