f52ed1f176f5df6161d9b59a710f952af88ba896
[platform/framework/web/crosswalk-tizen.git] /
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
16  *
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.
27  */
28
29 #ifndef AudioParam_h
30 #define AudioParam_h
31
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>
41
42 namespace blink {
43
44 class AudioNodeOutput;
45
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.
50 enum AudioParamType {
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,
65   ParamTypeGainGain,
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,
85 };
86
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
93 // dies.
94 class AudioParamHandler final : public ThreadSafeRefCounted<AudioParamHandler>,
95                                 public AudioSummingJunction {
96  public:
97   AudioParamType getParamType() const { return m_paramType; }
98   void setParamType(AudioParamType);
99   // Return a nice name for the AudioParam.
100   String getParamName() const;
101
102   static const double DefaultSmoothingConstant;
103   static const double SnapThreshold;
104
105   static PassRefPtr<AudioParamHandler> create(BaseAudioContext& context,
106                                               AudioParamType paramType,
107                                               double defaultValue,
108                                               float minValue,
109                                               float maxValue) {
110     return adoptRef(new AudioParamHandler(context, paramType, defaultValue,
111                                           minValue, maxValue));
112   }
113
114   // This should be used only in audio rendering thread.
115   AudioDestinationHandler& destinationHandler() const;
116
117   // AudioSummingJunction
118   void didUpdate() override {}
119
120   AudioParamTimeline& timeline() { return m_timeline; }
121
122   // Intrinsic value.
123   float value();
124   void setValue(float);
125
126   // Final value for k-rate parameters, otherwise use
127   // calculateSampleAccurateValues() for a-rate.
128   // Must be called in the audio thread.
129   float finalValue();
130
131   float defaultValue() const { return static_cast<float>(m_defaultValue); }
132   float minValue() const { return m_minValue; }
133   float maxValue() const { return m_maxValue; }
134
135   // Value smoothing:
136
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();
141
142   // Smoothly exponentially approaches to (de-zippers) the desired value.
143   // Returns true if smoothed value has already snapped exactly to value.
144   bool smooth();
145
146   void resetSmoothedValue() { m_timeline.setSmoothedValue(intrinsicValue()); }
147
148   bool hasSampleAccurateValues() {
149     return m_timeline.hasValues() || numberOfRenderingConnections();
150   }
151
152   // Calculates numberOfValues parameter values starting at the context's
153   // current time.
154   // Must be called in the context's render thread.
155   void calculateSampleAccurateValues(float* values, unsigned numberOfValues);
156
157   // Connect an audio-rate signal to control this parameter.
158   void connect(AudioNodeOutput&);
159   void disconnect(AudioNodeOutput&);
160
161   float intrinsicValue() const { return noBarrierLoad(&m_intrinsicValue); }
162
163   // Update any histograms with the given value.
164   void updateHistograms(float newValue);
165
166  private:
167   AudioParamHandler(BaseAudioContext&,
168                     AudioParamType,
169                     double defaultValue,
170                     float min,
171                     float max);
172
173   void warnIfOutsideRange(float value, float minValue, float maxValue);
174
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);
181
182   int computeQHistogramValue(float) const;
183
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
186   // implementation.
187   AudioParamType m_paramType;
188
189   // Intrinsic value
190   float m_intrinsicValue;
191   void setIntrinsicValue(float newValue);
192
193   float m_defaultValue;
194
195   // Nominal range for the value
196   float m_minValue;
197   float m_maxValue;
198
199   AudioParamTimeline m_timeline;
200
201   // The destination node used to get necessary information like the smaple rate
202   // and context time.
203   RefPtr<AudioDestinationHandler> m_destinationHandler;
204 };
205
206 // AudioParam class represents web-exposed AudioParam interface.
207 class AudioParam final : public GarbageCollectedFinalized<AudioParam>,
208                          public ScriptWrappable {
209   DEFINE_WRAPPERTYPEINFO();
210
211  public:
212   static AudioParam* create(BaseAudioContext&,
213                             AudioParamType,
214                             double defaultValue);
215   static AudioParam* create(BaseAudioContext&,
216                             AudioParamType,
217                             double defaultValue,
218                             float minValue,
219                             float maxValue);
220
221   DECLARE_TRACE();
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; }
226
227   AudioParamType getParamType() const { return handler().getParamType(); }
228   void setParamType(AudioParamType);
229   String getParamName() const;
230
231   float value() const;
232   void setValue(float);
233   float defaultValue() const;
234
235   float minValue() const;
236   float maxValue() const;
237
238   AudioParam* setValueAtTime(float value, double time, ExceptionState&);
239   AudioParam* linearRampToValueAtTime(float value,
240                                       double time,
241                                       ExceptionState&);
242   AudioParam* exponentialRampToValueAtTime(float value,
243                                            double time,
244                                            ExceptionState&);
245   AudioParam* setTargetAtTime(float target,
246                               double time,
247                               double timeConstant,
248                               ExceptionState&);
249   AudioParam* setValueCurveAtTime(DOMFloat32Array* curve,
250                                   double time,
251                                   double duration,
252                                   ExceptionState&);
253   AudioParam* cancelScheduledValues(double startTime, ExceptionState&);
254
255  private:
256   AudioParam(BaseAudioContext&,
257              AudioParamType,
258              double defaultValue,
259              float min,
260              float max);
261
262   void warnIfOutsideRange(const String& paramMethd, float value);
263
264   RefPtr<AudioParamHandler> m_handler;
265   Member<BaseAudioContext> m_context;
266 };
267
268 }  // namespace blink
269
270 #endif  // AudioParam_h