1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/OscillatorNode.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,147 @@ 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 OscillatorNode_h_ 1.11 +#define OscillatorNode_h_ 1.12 + 1.13 +#include "AudioNode.h" 1.14 +#include "AudioParam.h" 1.15 +#include "PeriodicWave.h" 1.16 +#include "mozilla/dom/OscillatorNodeBinding.h" 1.17 +#include "mozilla/Preferences.h" 1.18 + 1.19 +namespace mozilla { 1.20 +namespace dom { 1.21 + 1.22 +class AudioContext; 1.23 + 1.24 +class OscillatorNode : public AudioNode, 1.25 + public MainThreadMediaStreamListener 1.26 +{ 1.27 +public: 1.28 + explicit OscillatorNode(AudioContext* aContext); 1.29 + virtual ~OscillatorNode(); 1.30 + 1.31 + NS_DECL_ISUPPORTS_INHERITED 1.32 + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OscillatorNode, AudioNode) 1.33 + 1.34 + virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.35 + 1.36 + virtual void DestroyMediaStream() MOZ_OVERRIDE 1.37 + { 1.38 + if (mStream) { 1.39 + mStream->RemoveMainThreadListener(this); 1.40 + } 1.41 + AudioNode::DestroyMediaStream(); 1.42 + } 1.43 + virtual uint16_t NumberOfInputs() const MOZ_FINAL MOZ_OVERRIDE 1.44 + { 1.45 + return 0; 1.46 + } 1.47 + 1.48 + OscillatorType Type() const 1.49 + { 1.50 + return mType; 1.51 + } 1.52 + void SetType(OscillatorType aType, ErrorResult& aRv) 1.53 + { 1.54 + if (!Preferences::GetBool("media.webaudio.legacy.OscillatorNode")) { 1.55 + // Do not accept the alternate enum values unless the legacy pref 1.56 + // has been turned on. 1.57 + switch (aType) { 1.58 + case OscillatorType::_0: 1.59 + case OscillatorType::_1: 1.60 + case OscillatorType::_2: 1.61 + case OscillatorType::_3: 1.62 + case OscillatorType::_4: 1.63 + // Do nothing in order to emulate setting an invalid enum value. 1.64 + return; 1.65 + default: 1.66 + // Shut up the compiler warning 1.67 + break; 1.68 + } 1.69 + } 1.70 + 1.71 + // Handle the alternate enum values 1.72 + switch (aType) { 1.73 + case OscillatorType::_0: aType = OscillatorType::Sine; break; 1.74 + case OscillatorType::_1: aType = OscillatorType::Square; break; 1.75 + case OscillatorType::_2: aType = OscillatorType::Sawtooth; break; 1.76 + case OscillatorType::_3: aType = OscillatorType::Triangle; break; 1.77 + case OscillatorType::_4: aType = OscillatorType::Custom; break; 1.78 + default: 1.79 + // Shut up the compiler warning 1.80 + break; 1.81 + } 1.82 + if (aType == OscillatorType::Custom) { 1.83 + // ::Custom can only be set by setPeriodicWave(). 1.84 + // https://github.com/WebAudio/web-audio-api/issues/105 for exception. 1.85 + aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); 1.86 + return; 1.87 + } 1.88 + mType = aType; 1.89 + SendTypeToStream(); 1.90 + } 1.91 + 1.92 + AudioParam* Frequency() const 1.93 + { 1.94 + return mFrequency; 1.95 + } 1.96 + AudioParam* Detune() const 1.97 + { 1.98 + return mDetune; 1.99 + } 1.100 + 1.101 + void Start(double aWhen, ErrorResult& aRv); 1.102 + void NoteOn(double aWhen, ErrorResult& aRv) 1.103 + { 1.104 + Start(aWhen, aRv); 1.105 + } 1.106 + void Stop(double aWhen, ErrorResult& aRv); 1.107 + void NoteOff(double aWhen, ErrorResult& aRv) 1.108 + { 1.109 + Stop(aWhen, aRv); 1.110 + } 1.111 + void SetPeriodicWave(PeriodicWave& aPeriodicWave) 1.112 + { 1.113 + mPeriodicWave = &aPeriodicWave; 1.114 + // SendTypeToStream will call SendPeriodicWaveToStream for us. 1.115 + mType = OscillatorType::Custom; 1.116 + SendTypeToStream(); 1.117 + } 1.118 + 1.119 + IMPL_EVENT_HANDLER(ended) 1.120 + 1.121 + virtual void NotifyMainThreadStateChanged() MOZ_OVERRIDE; 1.122 + 1.123 + virtual const char* NodeType() const 1.124 + { 1.125 + return "OscillatorNode"; 1.126 + } 1.127 + 1.128 + virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE; 1.129 + virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE; 1.130 + 1.131 +private: 1.132 + static void SendFrequencyToStream(AudioNode* aNode); 1.133 + static void SendDetuneToStream(AudioNode* aNode); 1.134 + void SendTypeToStream(); 1.135 + void SendPeriodicWaveToStream(); 1.136 + 1.137 +private: 1.138 + OscillatorType mType; 1.139 + nsRefPtr<PeriodicWave> mPeriodicWave; 1.140 + nsRefPtr<AudioParam> mFrequency; 1.141 + nsRefPtr<AudioParam> mDetune; 1.142 + bool mStartCalled; 1.143 + bool mStopped; 1.144 +}; 1.145 + 1.146 +} 1.147 +} 1.148 + 1.149 +#endif 1.150 +