1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/AudioBufferSourceNode.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,148 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef AudioBufferSourceNode_h_ 1.11 +#define AudioBufferSourceNode_h_ 1.12 + 1.13 +#include "AudioNode.h" 1.14 +#include "AudioBuffer.h" 1.15 + 1.16 +namespace mozilla { 1.17 +namespace dom { 1.18 + 1.19 +class AudioParam; 1.20 + 1.21 +class AudioBufferSourceNode : public AudioNode, 1.22 + public MainThreadMediaStreamListener 1.23 +{ 1.24 +public: 1.25 + explicit AudioBufferSourceNode(AudioContext* aContext); 1.26 + virtual ~AudioBufferSourceNode(); 1.27 + 1.28 + virtual void DestroyMediaStream() MOZ_OVERRIDE 1.29 + { 1.30 + if (mStream) { 1.31 + mStream->RemoveMainThreadListener(this); 1.32 + } 1.33 + AudioNode::DestroyMediaStream(); 1.34 + } 1.35 + virtual uint16_t NumberOfInputs() const MOZ_FINAL MOZ_OVERRIDE 1.36 + { 1.37 + return 0; 1.38 + } 1.39 + virtual AudioBufferSourceNode* AsAudioBufferSourceNode() MOZ_OVERRIDE 1.40 + { 1.41 + return this; 1.42 + } 1.43 + NS_DECL_ISUPPORTS_INHERITED 1.44 + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioBufferSourceNode, AudioNode) 1.45 + 1.46 + virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.47 + 1.48 + void Start(double aWhen, double aOffset, 1.49 + const Optional<double>& aDuration, ErrorResult& aRv); 1.50 + void Stop(double aWhen, ErrorResult& aRv); 1.51 + 1.52 + AudioBuffer* GetBuffer(JSContext* aCx) const 1.53 + { 1.54 + return mBuffer; 1.55 + } 1.56 + void SetBuffer(JSContext* aCx, AudioBuffer* aBuffer) 1.57 + { 1.58 + mBuffer = aBuffer; 1.59 + SendBufferParameterToStream(aCx); 1.60 + SendLoopParametersToStream(); 1.61 + } 1.62 + AudioParam* PlaybackRate() const 1.63 + { 1.64 + return mPlaybackRate; 1.65 + } 1.66 + bool Loop() const 1.67 + { 1.68 + return mLoop; 1.69 + } 1.70 + void SetLoop(bool aLoop) 1.71 + { 1.72 + mLoop = aLoop; 1.73 + SendLoopParametersToStream(); 1.74 + } 1.75 + double LoopStart() const 1.76 + { 1.77 + return mLoopStart; 1.78 + } 1.79 + void SetLoopStart(double aStart) 1.80 + { 1.81 + mLoopStart = aStart; 1.82 + SendLoopParametersToStream(); 1.83 + } 1.84 + double LoopEnd() const 1.85 + { 1.86 + return mLoopEnd; 1.87 + } 1.88 + void SetLoopEnd(double aEnd) 1.89 + { 1.90 + mLoopEnd = aEnd; 1.91 + SendLoopParametersToStream(); 1.92 + } 1.93 + void SendDopplerShiftToStream(double aDopplerShift); 1.94 + 1.95 + IMPL_EVENT_HANDLER(ended) 1.96 + 1.97 + virtual void NotifyMainThreadStateChanged() MOZ_OVERRIDE; 1.98 + 1.99 + virtual const char* NodeType() const 1.100 + { 1.101 + return "AudioBufferSourceNode"; 1.102 + } 1.103 + 1.104 + virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE; 1.105 + virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE; 1.106 + 1.107 +private: 1.108 + friend class AudioBufferSourceNodeEngine; 1.109 + // START is sent during Start(). 1.110 + // STOP is sent during Stop(). 1.111 + // BUFFERSTART and BUFFEREND are sent when SetBuffer() and Start() have 1.112 + // been called (along with sending the buffer). 1.113 + enum EngineParameters { 1.114 + SAMPLE_RATE, 1.115 + START, 1.116 + STOP, 1.117 + // BUFFERSTART is the "offset" passed to start(), multiplied by 1.118 + // buffer.sampleRate. 1.119 + BUFFERSTART, 1.120 + // BUFFEREND is the sum of "offset" and "duration" passed to start(), 1.121 + // multiplied by buffer.sampleRate, or the size of the buffer, if smaller. 1.122 + BUFFEREND, 1.123 + LOOP, 1.124 + LOOPSTART, 1.125 + LOOPEND, 1.126 + PLAYBACKRATE, 1.127 + DOPPLERSHIFT 1.128 + }; 1.129 + 1.130 + void SendLoopParametersToStream(); 1.131 + void SendBufferParameterToStream(JSContext* aCx); 1.132 + void SendOffsetAndDurationParametersToStream(AudioNodeStream* aStream); 1.133 + static void SendPlaybackRateToStream(AudioNode* aNode); 1.134 + 1.135 +private: 1.136 + double mLoopStart; 1.137 + double mLoopEnd; 1.138 + double mOffset; 1.139 + double mDuration; 1.140 + nsRefPtr<AudioBuffer> mBuffer; 1.141 + nsRefPtr<AudioParam> mPlaybackRate; 1.142 + bool mLoop; 1.143 + bool mStartCalled; 1.144 + bool mStopped; 1.145 +}; 1.146 + 1.147 +} 1.148 +} 1.149 + 1.150 +#endif 1.151 +