AudioBufferSourceNode.h (4076B)
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 "AudioBuffer.h" 11 #include "AudioScheduledSourceNode.h" 12 13 namespace mozilla::dom { 14 15 struct AudioBufferSourceOptions; 16 class AudioParam; 17 18 class AudioBufferSourceNode final : public AudioScheduledSourceNode, 19 public MainThreadMediaTrackListener { 20 public: 21 static already_AddRefed<AudioBufferSourceNode> Create( 22 JSContext* aCx, AudioContext& aAudioContext, 23 const AudioBufferSourceOptions& aOptions); 24 25 void DestroyMediaTrack() override; 26 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) 32 33 static already_AddRefed<AudioBufferSourceNode> Constructor( 34 const GlobalObject& aGlobal, AudioContext& aAudioContext, 35 const AudioBufferSourceOptions& aOptions) { 36 return Create(aGlobal.Context(), aAudioContext, aOptions); 37 } 38 39 JSObject* WrapObject(JSContext* aCx, 40 JS::Handle<JSObject*> aGivenProto) override; 41 42 void Start(double aWhen, double aOffset, const Optional<double>& aDuration, 43 ErrorResult& aRv); 44 45 void Start(double aWhen, ErrorResult& aRv) override; 46 void Stop(double aWhen, ErrorResult& aRv) override; 47 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"); 54 return; 55 } 56 if (aBuffer) { 57 mBufferSet = true; 58 } 59 mBuffer = aBuffer; 60 SendBufferParameterToTrack(aCx); 61 SendLoopParametersToTrack(); 62 } 63 AudioParam* PlaybackRate() const { return mPlaybackRate; } 64 AudioParam* Detune() const { return mDetune; } 65 bool Loop() const { return mLoop; } 66 void SetLoop(bool aLoop) { 67 mLoop = aLoop; 68 SendLoopParametersToTrack(); 69 } 70 double LoopStart() const { return mLoopStart; } 71 void SetLoopStart(double aStart) { 72 mLoopStart = aStart; 73 SendLoopParametersToTrack(); 74 } 75 double LoopEnd() const { return mLoopEnd; } 76 void SetLoopEnd(double aEnd) { 77 mLoopEnd = aEnd; 78 SendLoopParametersToTrack(); 79 } 80 void NotifyMainThreadTrackEnded() override; 81 82 const char* NodeType() const override { return "AudioBufferSourceNode"; } 83 84 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override; 85 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override; 86 87 private: 88 explicit AudioBufferSourceNode(AudioContext* aContext); 89 ~AudioBufferSourceNode() = default; 90 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 { 97 SAMPLE_RATE, 98 START, 99 STOP, 100 // BUFFERSTART is the "offset" passed to start(), multiplied by 101 // buffer.sampleRate. 102 BUFFERSTART, 103 DURATION, 104 LOOP, 105 LOOPSTART, 106 LOOPEND, 107 PLAYBACKRATE, 108 DETUNE 109 }; 110 111 void SendLoopParametersToTrack(); 112 void SendBufferParameterToTrack(JSContext* aCx); 113 void SendOffsetAndDurationParametersToTrack(AudioNodeTrack* aTrack); 114 115 double mLoopStart; 116 double mLoopEnd; 117 double mOffset; 118 double mDuration; 119 RefPtr<AudioBuffer> mBuffer; 120 RefPtr<AudioParam> mPlaybackRate; 121 RefPtr<AudioParam> mDetune; 122 bool mLoop; 123 bool mStartCalled; 124 bool mBufferSet; 125 }; 126 127 } // namespace mozilla::dom 128 129 #endif