content/media/webaudio/AudioParam.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/AudioParam.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,193 @@
     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 AudioParam_h_
    1.11 +#define AudioParam_h_
    1.12 +
    1.13 +#include "AudioParamTimeline.h"
    1.14 +#include "nsWrapperCache.h"
    1.15 +#include "nsCycleCollectionParticipant.h"
    1.16 +#include "nsAutoPtr.h"
    1.17 +#include "AudioNode.h"
    1.18 +#include "mozilla/dom/TypedArray.h"
    1.19 +#include "WebAudioUtils.h"
    1.20 +#include "js/TypeDecls.h"
    1.21 +
    1.22 +namespace mozilla {
    1.23 +
    1.24 +namespace dom {
    1.25 +
    1.26 +class AudioParam MOZ_FINAL : public nsWrapperCache,
    1.27 +                             public AudioParamTimeline
    1.28 +{
    1.29 +public:
    1.30 +  typedef void (*CallbackType)(AudioNode*);
    1.31 +
    1.32 +  AudioParam(AudioNode* aNode,
    1.33 +             CallbackType aCallback,
    1.34 +             float aDefaultValue);
    1.35 +  virtual ~AudioParam();
    1.36 +
    1.37 +  NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
    1.38 +  NS_IMETHOD_(MozExternalRefCountType) Release(void);
    1.39 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioParam)
    1.40 +
    1.41 +  AudioContext* GetParentObject() const
    1.42 +  {
    1.43 +    return mNode->Context();
    1.44 +  }
    1.45 +
    1.46 +  double DOMTimeToStreamTime(double aTime) const
    1.47 +  {
    1.48 +    return mNode->Context()->DOMTimeToStreamTime(aTime);
    1.49 +  }
    1.50 +
    1.51 +  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    1.52 +
    1.53 +  // We override SetValueCurveAtTime to convert the Float32Array to the wrapper
    1.54 +  // object.
    1.55 +  void SetValueCurveAtTime(const Float32Array& aValues, double aStartTime, double aDuration, ErrorResult& aRv)
    1.56 +  {
    1.57 +    if (!WebAudioUtils::IsTimeValid(aStartTime)) {
    1.58 +      aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
    1.59 +      return;
    1.60 +    }
    1.61 +    aValues.ComputeLengthAndData();
    1.62 +    AudioParamTimeline::SetValueCurveAtTime(aValues.Data(), aValues.Length(),
    1.63 +                                            DOMTimeToStreamTime(aStartTime), aDuration, aRv);
    1.64 +    mCallback(mNode);
    1.65 +  }
    1.66 +
    1.67 +  // We override the rest of the mutating AudioParamTimeline methods in order to make
    1.68 +  // sure that the callback is called every time that this object gets mutated.
    1.69 +  void SetValue(float aValue)
    1.70 +  {
    1.71 +    // Optimize away setting the same value on an AudioParam
    1.72 +    if (HasSimpleValue() &&
    1.73 +        WebAudioUtils::FuzzyEqual(GetValue(), aValue)) {
    1.74 +      return;
    1.75 +    }
    1.76 +    AudioParamTimeline::SetValue(aValue);
    1.77 +    mCallback(mNode);
    1.78 +  }
    1.79 +  void SetValueAtTime(float aValue, double aStartTime, ErrorResult& aRv)
    1.80 +  {
    1.81 +    if (!WebAudioUtils::IsTimeValid(aStartTime)) {
    1.82 +      aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
    1.83 +      return;
    1.84 +    }
    1.85 +    AudioParamTimeline::SetValueAtTime(aValue, DOMTimeToStreamTime(aStartTime), aRv);
    1.86 +    mCallback(mNode);
    1.87 +  }
    1.88 +  void LinearRampToValueAtTime(float aValue, double aEndTime, ErrorResult& aRv)
    1.89 +  {
    1.90 +    if (!WebAudioUtils::IsTimeValid(aEndTime)) {
    1.91 +      aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
    1.92 +      return;
    1.93 +    }
    1.94 +    AudioParamTimeline::LinearRampToValueAtTime(aValue, DOMTimeToStreamTime(aEndTime), aRv);
    1.95 +    mCallback(mNode);
    1.96 +  }
    1.97 +  void ExponentialRampToValueAtTime(float aValue, double aEndTime, ErrorResult& aRv)
    1.98 +  {
    1.99 +    if (!WebAudioUtils::IsTimeValid(aEndTime)) {
   1.100 +      aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
   1.101 +      return;
   1.102 +    }
   1.103 +    AudioParamTimeline::ExponentialRampToValueAtTime(aValue, DOMTimeToStreamTime(aEndTime), aRv);
   1.104 +    mCallback(mNode);
   1.105 +  }
   1.106 +  void SetTargetAtTime(float aTarget, double aStartTime, double aTimeConstant, ErrorResult& aRv)
   1.107 +  {
   1.108 +    if (!WebAudioUtils::IsTimeValid(aStartTime) ||
   1.109 +        !WebAudioUtils::IsTimeValid(aTimeConstant)) {
   1.110 +      aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
   1.111 +      return;
   1.112 +    }
   1.113 +    AudioParamTimeline::SetTargetAtTime(aTarget, DOMTimeToStreamTime(aStartTime), aTimeConstant, aRv);
   1.114 +    mCallback(mNode);
   1.115 +  }
   1.116 +  void CancelScheduledValues(double aStartTime, ErrorResult& aRv)
   1.117 +  {
   1.118 +    if (!WebAudioUtils::IsTimeValid(aStartTime)) {
   1.119 +      aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
   1.120 +      return;
   1.121 +    }
   1.122 +    AudioParamTimeline::CancelScheduledValues(DOMTimeToStreamTime(aStartTime));
   1.123 +    mCallback(mNode);
   1.124 +  }
   1.125 +
   1.126 +  float DefaultValue() const
   1.127 +  {
   1.128 +    return mDefaultValue;
   1.129 +  }
   1.130 +
   1.131 +  AudioNode* Node() const
   1.132 +  {
   1.133 +    return mNode;
   1.134 +  }
   1.135 +
   1.136 +  const nsTArray<AudioNode::InputNode>& InputNodes() const
   1.137 +  {
   1.138 +    return mInputNodes;
   1.139 +  }
   1.140 +
   1.141 +  void RemoveInputNode(uint32_t aIndex)
   1.142 +  {
   1.143 +    mInputNodes.RemoveElementAt(aIndex);
   1.144 +  }
   1.145 +
   1.146 +  AudioNode::InputNode* AppendInputNode()
   1.147 +  {
   1.148 +    return mInputNodes.AppendElement();
   1.149 +  }
   1.150 +
   1.151 +  void DisconnectFromGraphAndDestroyStream();
   1.152 +
   1.153 +  // May create the stream if it doesn't exist
   1.154 +  MediaStream* Stream();
   1.155 +
   1.156 +  virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
   1.157 +  {
   1.158 +    size_t amount = AudioParamTimeline::SizeOfExcludingThis(aMallocSizeOf);
   1.159 +    // Not owned:
   1.160 +    // - mNode
   1.161 +
   1.162 +    // Just count the array, actual nodes are counted in mNode.
   1.163 +    amount += mInputNodes.SizeOfExcludingThis(aMallocSizeOf);
   1.164 +
   1.165 +    if (mNodeStreamPort) {
   1.166 +      amount += mNodeStreamPort->SizeOfIncludingThis(aMallocSizeOf);
   1.167 +    }
   1.168 +
   1.169 +    return amount;
   1.170 +  }
   1.171 +
   1.172 +  virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
   1.173 +  {
   1.174 +    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
   1.175 +  }
   1.176 +
   1.177 +protected:
   1.178 +  nsCycleCollectingAutoRefCnt mRefCnt;
   1.179 +  NS_DECL_OWNINGTHREAD
   1.180 +
   1.181 +private:
   1.182 +  nsRefPtr<AudioNode> mNode;
   1.183 +  // For every InputNode, there is a corresponding entry in mOutputParams of the
   1.184 +  // InputNode's mInputNode.
   1.185 +  nsTArray<AudioNode::InputNode> mInputNodes;
   1.186 +  CallbackType mCallback;
   1.187 +  const float mDefaultValue;
   1.188 +  // The input port used to connect the AudioParam's stream to its node's stream
   1.189 +  nsRefPtr<MediaInputPort> mNodeStreamPort;
   1.190 +};
   1.191 +
   1.192 +}
   1.193 +}
   1.194 +
   1.195 +#endif
   1.196 +

mercurial