michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef OscillatorNode_h_ michael@0: #define OscillatorNode_h_ michael@0: michael@0: #include "AudioNode.h" michael@0: #include "AudioParam.h" michael@0: #include "PeriodicWave.h" michael@0: #include "mozilla/dom/OscillatorNodeBinding.h" michael@0: #include "mozilla/Preferences.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: class AudioContext; michael@0: michael@0: class OscillatorNode : public AudioNode, michael@0: public MainThreadMediaStreamListener michael@0: { michael@0: public: michael@0: explicit OscillatorNode(AudioContext* aContext); michael@0: virtual ~OscillatorNode(); michael@0: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OscillatorNode, AudioNode) michael@0: michael@0: virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: virtual void DestroyMediaStream() MOZ_OVERRIDE michael@0: { michael@0: if (mStream) { michael@0: mStream->RemoveMainThreadListener(this); michael@0: } michael@0: AudioNode::DestroyMediaStream(); michael@0: } michael@0: virtual uint16_t NumberOfInputs() const MOZ_FINAL MOZ_OVERRIDE michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: OscillatorType Type() const michael@0: { michael@0: return mType; michael@0: } michael@0: void SetType(OscillatorType aType, ErrorResult& aRv) michael@0: { michael@0: if (!Preferences::GetBool("media.webaudio.legacy.OscillatorNode")) { michael@0: // Do not accept the alternate enum values unless the legacy pref michael@0: // has been turned on. michael@0: switch (aType) { michael@0: case OscillatorType::_0: michael@0: case OscillatorType::_1: michael@0: case OscillatorType::_2: michael@0: case OscillatorType::_3: michael@0: case OscillatorType::_4: michael@0: // Do nothing in order to emulate setting an invalid enum value. michael@0: return; michael@0: default: michael@0: // Shut up the compiler warning michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // Handle the alternate enum values michael@0: switch (aType) { michael@0: case OscillatorType::_0: aType = OscillatorType::Sine; break; michael@0: case OscillatorType::_1: aType = OscillatorType::Square; break; michael@0: case OscillatorType::_2: aType = OscillatorType::Sawtooth; break; michael@0: case OscillatorType::_3: aType = OscillatorType::Triangle; break; michael@0: case OscillatorType::_4: aType = OscillatorType::Custom; break; michael@0: default: michael@0: // Shut up the compiler warning michael@0: break; michael@0: } michael@0: if (aType == OscillatorType::Custom) { michael@0: // ::Custom can only be set by setPeriodicWave(). michael@0: // https://github.com/WebAudio/web-audio-api/issues/105 for exception. michael@0: aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); michael@0: return; michael@0: } michael@0: mType = aType; michael@0: SendTypeToStream(); michael@0: } michael@0: michael@0: AudioParam* Frequency() const michael@0: { michael@0: return mFrequency; michael@0: } michael@0: AudioParam* Detune() const michael@0: { michael@0: return mDetune; michael@0: } michael@0: michael@0: void Start(double aWhen, ErrorResult& aRv); michael@0: void NoteOn(double aWhen, ErrorResult& aRv) michael@0: { michael@0: Start(aWhen, aRv); michael@0: } michael@0: void Stop(double aWhen, ErrorResult& aRv); michael@0: void NoteOff(double aWhen, ErrorResult& aRv) michael@0: { michael@0: Stop(aWhen, aRv); michael@0: } michael@0: void SetPeriodicWave(PeriodicWave& aPeriodicWave) michael@0: { michael@0: mPeriodicWave = &aPeriodicWave; michael@0: // SendTypeToStream will call SendPeriodicWaveToStream for us. michael@0: mType = OscillatorType::Custom; michael@0: SendTypeToStream(); michael@0: } michael@0: michael@0: IMPL_EVENT_HANDLER(ended) michael@0: michael@0: virtual void NotifyMainThreadStateChanged() MOZ_OVERRIDE; michael@0: michael@0: virtual const char* NodeType() const michael@0: { michael@0: return "OscillatorNode"; michael@0: } michael@0: michael@0: virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE; michael@0: virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE; michael@0: michael@0: private: michael@0: static void SendFrequencyToStream(AudioNode* aNode); michael@0: static void SendDetuneToStream(AudioNode* aNode); michael@0: void SendTypeToStream(); michael@0: void SendPeriodicWaveToStream(); michael@0: michael@0: private: michael@0: OscillatorType mType; michael@0: nsRefPtr mPeriodicWave; michael@0: nsRefPtr mFrequency; michael@0: nsRefPtr mDetune; michael@0: bool mStartCalled; michael@0: bool mStopped; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: