2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "bindings/core/v8/ScriptWrappable.h"
33 #include "core/dom/DOMTypedArray.h"
34 #include "modules/webaudio/AudioParamTimeline.h"
35 #include "modules/webaudio/AudioSummingJunction.h"
36 #include "modules/webaudio/BaseAudioContext.h"
37 #include "wtf/PassRefPtr.h"
38 #include "wtf/ThreadSafeRefCounted.h"
39 #include "wtf/text/WTFString.h"
40 #include <sys/types.h>
44 class AudioNodeOutput;
46 // Each AudioParam gets an identifier here. This is mostly for instrospection
47 // if warnings or other messages need to be printed. It's useful to know what
48 // the AudioParam represents. The name should include the node type and the
49 // name of the AudioParam.
51 ParamTypeAudioBufferSourcePlaybackRate,
52 ParamTypeAudioBufferSourceDetune,
53 ParamTypeBiquadFilterFrequency,
54 ParamTypeBiquadFilterQ,
55 ParamTypeBiquadFilterQLowpass,
56 ParamTypeBiquadFilterQHighpass,
57 ParamTypeBiquadFilterGain,
58 ParamTypeBiquadFilterDetune,
59 ParamTypeDelayDelayTime,
60 ParamTypeDynamicsCompressorThreshold,
61 ParamTypeDynamicsCompressorKnee,
62 ParamTypeDynamicsCompressorRatio,
63 ParamTypeDynamicsCompressorAttack,
64 ParamTypeDynamicsCompressorRelease,
66 ParamTypeOscillatorFrequency,
67 ParamTypeOscillatorDetune,
68 ParamTypeStereoPannerPan,
69 ParamTypePannerPositionX,
70 ParamTypePannerPositionY,
71 ParamTypePannerPositionZ,
72 ParamTypePannerOrientationX,
73 ParamTypePannerOrientationY,
74 ParamTypePannerOrientationZ,
75 ParamTypeAudioListenerPositionX,
76 ParamTypeAudioListenerPositionY,
77 ParamTypeAudioListenerPositionZ,
78 ParamTypeAudioListenerForwardX,
79 ParamTypeAudioListenerForwardY,
80 ParamTypeAudioListenerForwardZ,
81 ParamTypeAudioListenerUpX,
82 ParamTypeAudioListenerUpY,
83 ParamTypeAudioListenerUpZ,
84 ParamTypeConstantSourceValue,
87 // AudioParamHandler is an actual implementation of web-exposed AudioParam
88 // interface. Each of AudioParam object creates and owns an AudioParamHandler,
89 // and it is responsible for all of AudioParam tasks. An AudioParamHandler
90 // object is owned by the originator AudioParam object, and some audio
91 // processing classes have additional references. An AudioParamHandler can
92 // outlive the owner AudioParam, and it never dies before the owner AudioParam
94 class AudioParamHandler final : public ThreadSafeRefCounted<AudioParamHandler>,
95 public AudioSummingJunction {
97 AudioParamType getParamType() const { return m_paramType; }
98 void setParamType(AudioParamType);
99 // Return a nice name for the AudioParam.
100 String getParamName() const;
102 static const double DefaultSmoothingConstant;
103 static const double SnapThreshold;
105 static PassRefPtr<AudioParamHandler> create(BaseAudioContext& context,
106 AudioParamType paramType,
110 return adoptRef(new AudioParamHandler(context, paramType, defaultValue,
111 minValue, maxValue));
114 // This should be used only in audio rendering thread.
115 AudioDestinationHandler& destinationHandler() const;
117 // AudioSummingJunction
118 void didUpdate() override {}
120 AudioParamTimeline& timeline() { return m_timeline; }
124 void setValue(float);
126 // Final value for k-rate parameters, otherwise use
127 // calculateSampleAccurateValues() for a-rate.
128 // Must be called in the audio thread.
131 float defaultValue() const { return static_cast<float>(m_defaultValue); }
132 float minValue() const { return m_minValue; }
133 float maxValue() const { return m_maxValue; }
137 // When a new value is set with setValue(), in our internal use of the
138 // parameter we don't immediately jump to it. Instead we smoothly approach
139 // this value to avoid glitching.
140 float smoothedValue();
142 // Smoothly exponentially approaches to (de-zippers) the desired value.
143 // Returns true if smoothed value has already snapped exactly to value.
146 void resetSmoothedValue() { m_timeline.setSmoothedValue(intrinsicValue()); }
148 bool hasSampleAccurateValues() {
149 return m_timeline.hasValues() || numberOfRenderingConnections();
152 // Calculates numberOfValues parameter values starting at the context's
154 // Must be called in the context's render thread.
155 void calculateSampleAccurateValues(float* values, unsigned numberOfValues);
157 // Connect an audio-rate signal to control this parameter.
158 void connect(AudioNodeOutput&);
159 void disconnect(AudioNodeOutput&);
161 float intrinsicValue() const { return noBarrierLoad(&m_intrinsicValue); }
163 // Update any histograms with the given value.
164 void updateHistograms(float newValue);
167 AudioParamHandler(BaseAudioContext&,
173 void warnIfOutsideRange(float value, float minValue, float maxValue);
175 // sampleAccurate corresponds to a-rate (audio rate) vs. k-rate in the Web
176 // Audio specification.
177 void calculateFinalValues(float* values,
178 unsigned numberOfValues,
179 bool sampleAccurate);
180 void calculateTimelineValues(float* values, unsigned numberOfValues);
182 int computeQHistogramValue(float) const;
184 // The type of AudioParam, indicating what this AudioParam represents and what
185 // node it belongs to. Mostly for informational purposes and doesn't affect
187 AudioParamType m_paramType;
190 float m_intrinsicValue;
191 void setIntrinsicValue(float newValue);
193 float m_defaultValue;
195 // Nominal range for the value
199 AudioParamTimeline m_timeline;
201 // The destination node used to get necessary information like the smaple rate
203 RefPtr<AudioDestinationHandler> m_destinationHandler;
206 // AudioParam class represents web-exposed AudioParam interface.
207 class AudioParam final : public GarbageCollectedFinalized<AudioParam>,
208 public ScriptWrappable {
209 DEFINE_WRAPPERTYPEINFO();
212 static AudioParam* create(BaseAudioContext&,
214 double defaultValue);
215 static AudioParam* create(BaseAudioContext&,
222 // |handler| always returns a valid object.
223 AudioParamHandler& handler() const { return *m_handler; }
224 // |context| always returns a valid object.
225 BaseAudioContext* context() const { return m_context; }
227 AudioParamType getParamType() const { return handler().getParamType(); }
228 void setParamType(AudioParamType);
229 String getParamName() const;
232 void setValue(float);
233 float defaultValue() const;
235 float minValue() const;
236 float maxValue() const;
238 AudioParam* setValueAtTime(float value, double time, ExceptionState&);
239 AudioParam* linearRampToValueAtTime(float value,
242 AudioParam* exponentialRampToValueAtTime(float value,
245 AudioParam* setTargetAtTime(float target,
249 AudioParam* setValueCurveAtTime(DOMFloat32Array* curve,
253 AudioParam* cancelScheduledValues(double startTime, ExceptionState&);
256 AudioParam(BaseAudioContext&,
262 void warnIfOutsideRange(const String& paramMethd, float value);
264 RefPtr<AudioParamHandler> m_handler;
265 Member<BaseAudioContext> m_context;
270 #endif // AudioParam_h