content/media/webaudio/OscillatorNode.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef OscillatorNode_h_
michael@0 8 #define OscillatorNode_h_
michael@0 9
michael@0 10 #include "AudioNode.h"
michael@0 11 #include "AudioParam.h"
michael@0 12 #include "PeriodicWave.h"
michael@0 13 #include "mozilla/dom/OscillatorNodeBinding.h"
michael@0 14 #include "mozilla/Preferences.h"
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17 namespace dom {
michael@0 18
michael@0 19 class AudioContext;
michael@0 20
michael@0 21 class OscillatorNode : public AudioNode,
michael@0 22 public MainThreadMediaStreamListener
michael@0 23 {
michael@0 24 public:
michael@0 25 explicit OscillatorNode(AudioContext* aContext);
michael@0 26 virtual ~OscillatorNode();
michael@0 27
michael@0 28 NS_DECL_ISUPPORTS_INHERITED
michael@0 29 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OscillatorNode, AudioNode)
michael@0 30
michael@0 31 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
michael@0 32
michael@0 33 virtual void DestroyMediaStream() MOZ_OVERRIDE
michael@0 34 {
michael@0 35 if (mStream) {
michael@0 36 mStream->RemoveMainThreadListener(this);
michael@0 37 }
michael@0 38 AudioNode::DestroyMediaStream();
michael@0 39 }
michael@0 40 virtual uint16_t NumberOfInputs() const MOZ_FINAL MOZ_OVERRIDE
michael@0 41 {
michael@0 42 return 0;
michael@0 43 }
michael@0 44
michael@0 45 OscillatorType Type() const
michael@0 46 {
michael@0 47 return mType;
michael@0 48 }
michael@0 49 void SetType(OscillatorType aType, ErrorResult& aRv)
michael@0 50 {
michael@0 51 if (!Preferences::GetBool("media.webaudio.legacy.OscillatorNode")) {
michael@0 52 // Do not accept the alternate enum values unless the legacy pref
michael@0 53 // has been turned on.
michael@0 54 switch (aType) {
michael@0 55 case OscillatorType::_0:
michael@0 56 case OscillatorType::_1:
michael@0 57 case OscillatorType::_2:
michael@0 58 case OscillatorType::_3:
michael@0 59 case OscillatorType::_4:
michael@0 60 // Do nothing in order to emulate setting an invalid enum value.
michael@0 61 return;
michael@0 62 default:
michael@0 63 // Shut up the compiler warning
michael@0 64 break;
michael@0 65 }
michael@0 66 }
michael@0 67
michael@0 68 // Handle the alternate enum values
michael@0 69 switch (aType) {
michael@0 70 case OscillatorType::_0: aType = OscillatorType::Sine; break;
michael@0 71 case OscillatorType::_1: aType = OscillatorType::Square; break;
michael@0 72 case OscillatorType::_2: aType = OscillatorType::Sawtooth; break;
michael@0 73 case OscillatorType::_3: aType = OscillatorType::Triangle; break;
michael@0 74 case OscillatorType::_4: aType = OscillatorType::Custom; break;
michael@0 75 default:
michael@0 76 // Shut up the compiler warning
michael@0 77 break;
michael@0 78 }
michael@0 79 if (aType == OscillatorType::Custom) {
michael@0 80 // ::Custom can only be set by setPeriodicWave().
michael@0 81 // https://github.com/WebAudio/web-audio-api/issues/105 for exception.
michael@0 82 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
michael@0 83 return;
michael@0 84 }
michael@0 85 mType = aType;
michael@0 86 SendTypeToStream();
michael@0 87 }
michael@0 88
michael@0 89 AudioParam* Frequency() const
michael@0 90 {
michael@0 91 return mFrequency;
michael@0 92 }
michael@0 93 AudioParam* Detune() const
michael@0 94 {
michael@0 95 return mDetune;
michael@0 96 }
michael@0 97
michael@0 98 void Start(double aWhen, ErrorResult& aRv);
michael@0 99 void NoteOn(double aWhen, ErrorResult& aRv)
michael@0 100 {
michael@0 101 Start(aWhen, aRv);
michael@0 102 }
michael@0 103 void Stop(double aWhen, ErrorResult& aRv);
michael@0 104 void NoteOff(double aWhen, ErrorResult& aRv)
michael@0 105 {
michael@0 106 Stop(aWhen, aRv);
michael@0 107 }
michael@0 108 void SetPeriodicWave(PeriodicWave& aPeriodicWave)
michael@0 109 {
michael@0 110 mPeriodicWave = &aPeriodicWave;
michael@0 111 // SendTypeToStream will call SendPeriodicWaveToStream for us.
michael@0 112 mType = OscillatorType::Custom;
michael@0 113 SendTypeToStream();
michael@0 114 }
michael@0 115
michael@0 116 IMPL_EVENT_HANDLER(ended)
michael@0 117
michael@0 118 virtual void NotifyMainThreadStateChanged() MOZ_OVERRIDE;
michael@0 119
michael@0 120 virtual const char* NodeType() const
michael@0 121 {
michael@0 122 return "OscillatorNode";
michael@0 123 }
michael@0 124
michael@0 125 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
michael@0 126 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
michael@0 127
michael@0 128 private:
michael@0 129 static void SendFrequencyToStream(AudioNode* aNode);
michael@0 130 static void SendDetuneToStream(AudioNode* aNode);
michael@0 131 void SendTypeToStream();
michael@0 132 void SendPeriodicWaveToStream();
michael@0 133
michael@0 134 private:
michael@0 135 OscillatorType mType;
michael@0 136 nsRefPtr<PeriodicWave> mPeriodicWave;
michael@0 137 nsRefPtr<AudioParam> mFrequency;
michael@0 138 nsRefPtr<AudioParam> mDetune;
michael@0 139 bool mStartCalled;
michael@0 140 bool mStopped;
michael@0 141 };
michael@0 142
michael@0 143 }
michael@0 144 }
michael@0 145
michael@0 146 #endif
michael@0 147

mercurial