content/media/webaudio/OscillatorNode.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     7 #ifndef OscillatorNode_h_
     8 #define OscillatorNode_h_
    10 #include "AudioNode.h"
    11 #include "AudioParam.h"
    12 #include "PeriodicWave.h"
    13 #include "mozilla/dom/OscillatorNodeBinding.h"
    14 #include "mozilla/Preferences.h"
    16 namespace mozilla {
    17 namespace dom {
    19 class AudioContext;
    21 class OscillatorNode : public AudioNode,
    22                        public MainThreadMediaStreamListener
    23 {
    24 public:
    25   explicit OscillatorNode(AudioContext* aContext);
    26   virtual ~OscillatorNode();
    28   NS_DECL_ISUPPORTS_INHERITED
    29   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OscillatorNode, AudioNode)
    31   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    33   virtual void DestroyMediaStream() MOZ_OVERRIDE
    34   {
    35     if (mStream) {
    36       mStream->RemoveMainThreadListener(this);
    37     }
    38     AudioNode::DestroyMediaStream();
    39   }
    40   virtual uint16_t NumberOfInputs() const MOZ_FINAL MOZ_OVERRIDE
    41   {
    42     return 0;
    43   }
    45   OscillatorType Type() const
    46   {
    47     return mType;
    48   }
    49   void SetType(OscillatorType aType, ErrorResult& aRv)
    50   {
    51     if (!Preferences::GetBool("media.webaudio.legacy.OscillatorNode")) {
    52       // Do not accept the alternate enum values unless the legacy pref
    53       // has been turned on.
    54       switch (aType) {
    55       case OscillatorType::_0:
    56       case OscillatorType::_1:
    57       case OscillatorType::_2:
    58       case OscillatorType::_3:
    59       case OscillatorType::_4:
    60         // Do nothing in order to emulate setting an invalid enum value.
    61         return;
    62       default:
    63         // Shut up the compiler warning
    64         break;
    65       }
    66     }
    68     // Handle the alternate enum values
    69     switch (aType) {
    70     case OscillatorType::_0: aType = OscillatorType::Sine; break;
    71     case OscillatorType::_1: aType = OscillatorType::Square; break;
    72     case OscillatorType::_2: aType = OscillatorType::Sawtooth; break;
    73     case OscillatorType::_3: aType = OscillatorType::Triangle; break;
    74     case OscillatorType::_4: aType = OscillatorType::Custom; break;
    75     default:
    76       // Shut up the compiler warning
    77       break;
    78     }
    79     if (aType == OscillatorType::Custom) {
    80       // ::Custom can only be set by setPeriodicWave().
    81       // https://github.com/WebAudio/web-audio-api/issues/105 for exception.
    82       aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
    83       return;
    84     }
    85     mType = aType;
    86     SendTypeToStream();
    87   }
    89   AudioParam* Frequency() const
    90   {
    91     return mFrequency;
    92   }
    93   AudioParam* Detune() const
    94   {
    95     return mDetune;
    96   }
    98   void Start(double aWhen, ErrorResult& aRv);
    99   void NoteOn(double aWhen, ErrorResult& aRv)
   100   {
   101     Start(aWhen, aRv);
   102   }
   103   void Stop(double aWhen, ErrorResult& aRv);
   104   void NoteOff(double aWhen, ErrorResult& aRv)
   105   {
   106     Stop(aWhen, aRv);
   107   }
   108   void SetPeriodicWave(PeriodicWave& aPeriodicWave)
   109   {
   110     mPeriodicWave = &aPeriodicWave;
   111     // SendTypeToStream will call SendPeriodicWaveToStream for us.
   112     mType = OscillatorType::Custom;
   113     SendTypeToStream();
   114   }
   116   IMPL_EVENT_HANDLER(ended)
   118   virtual void NotifyMainThreadStateChanged() MOZ_OVERRIDE;
   120   virtual const char* NodeType() const
   121   {
   122     return "OscillatorNode";
   123   }
   125   virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
   126   virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
   128 private:
   129   static void SendFrequencyToStream(AudioNode* aNode);
   130   static void SendDetuneToStream(AudioNode* aNode);
   131   void SendTypeToStream();
   132   void SendPeriodicWaveToStream();
   134 private:
   135   OscillatorType mType;
   136   nsRefPtr<PeriodicWave> mPeriodicWave;
   137   nsRefPtr<AudioParam> mFrequency;
   138   nsRefPtr<AudioParam> mDetune;
   139   bool mStartCalled;
   140   bool mStopped;
   141 };
   143 }
   144 }
   146 #endif

mercurial