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/AudioContext.h"
35 #include "modules/webaudio/AudioParamTimeline.h"
36 #include "modules/webaudio/AudioSummingJunction.h"
37 #include "wtf/PassRefPtr.h"
38 #include "wtf/text/WTFString.h"
39 #include <sys/types.h>
43 class AudioNodeOutput;
45 class AudioParam final : public AudioSummingJunction, public ScriptWrappable {
46 DEFINE_WRAPPERTYPEINFO();
48 static const double DefaultSmoothingConstant;
49 static const double SnapThreshold;
51 static AudioParam* create(AudioContext* context, double defaultValue)
53 return new AudioParam(context, defaultValue);
56 // AudioSummingJunction
57 virtual void didUpdate() override { }
63 // Final value for k-rate parameters, otherwise use calculateSampleAccurateValues() for a-rate.
64 // Must be called in the audio thread.
67 float defaultValue() const { return static_cast<float>(m_defaultValue); }
71 // When a new value is set with setValue(), in our internal use of the parameter we don't immediately jump to it.
72 // Instead we smoothly approach this value to avoid glitching.
73 float smoothedValue();
75 // Smoothly exponentially approaches to (de-zippers) the desired value.
76 // Returns true if smoothed value has already snapped exactly to value.
79 void resetSmoothedValue() { m_smoothedValue = m_value; }
81 // Parameter automation.
82 void setValueAtTime(float value, double time, ExceptionState& exceptionState)
84 m_timeline.setValueAtTime(value, time, exceptionState);
86 void linearRampToValueAtTime(float value, double time, ExceptionState& exceptionState)
88 m_timeline.linearRampToValueAtTime(value, time, exceptionState);
90 void exponentialRampToValueAtTime(float value, double time, ExceptionState& es)
92 m_timeline.exponentialRampToValueAtTime(value, time, es);
94 void setTargetAtTime(float target, double time, double timeConstant, ExceptionState& exceptionState)
96 m_timeline.setTargetAtTime(target, time, timeConstant, exceptionState);
98 void setValueCurveAtTime(DOMFloat32Array* curve, double time, double duration, ExceptionState& exceptionState)
100 m_timeline.setValueCurveAtTime(curve->view(), time, duration, exceptionState);
102 void cancelScheduledValues(double startTime, ExceptionState& exceptionState)
104 m_timeline.cancelScheduledValues(startTime, exceptionState);
107 bool hasSampleAccurateValues() { return m_timeline.hasValues() || numberOfRenderingConnections(); }
109 // Calculates numberOfValues parameter values starting at the context's current time.
110 // Must be called in the context's render thread.
111 void calculateSampleAccurateValues(float* values, unsigned numberOfValues);
113 // Connect an audio-rate signal to control this parameter.
114 void connect(AudioNodeOutput&);
115 void disconnect(AudioNodeOutput&);
118 AudioParam(AudioContext* context, double defaultValue)
119 : AudioSummingJunction(context)
120 , m_value(defaultValue)
121 , m_defaultValue(defaultValue)
122 , m_smoothedValue(defaultValue) { }
124 // sampleAccurate corresponds to a-rate (audio rate) vs. k-rate in the Web Audio specification.
125 void calculateFinalValues(float* values, unsigned numberOfValues, bool sampleAccurate);
126 void calculateTimelineValues(float* values, unsigned numberOfValues);
129 double m_defaultValue;
131 // Smoothing (de-zippering)
132 double m_smoothedValue;
134 AudioParamTimeline m_timeline;
139 #endif // AudioParam_h