Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webaudio / AudioParam.cpp
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  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if ENABLE(WEB_AUDIO)
29
30 #include "modules/webaudio/AudioParam.h"
31
32 #include "platform/audio/AudioUtilities.h"
33 #include "modules/webaudio/AudioNode.h"
34 #include "modules/webaudio/AudioNodeOutput.h"
35 #include "platform/FloatConversion.h"
36 #include "wtf/MathExtras.h"
37
38 namespace blink {
39
40 const double AudioParam::DefaultSmoothingConstant = 0.05;
41 const double AudioParam::SnapThreshold = 0.001;
42
43 float AudioParam::value()
44 {
45     // Update value for timeline.
46     if (context() && context()->isAudioThread()) {
47         bool hasValue;
48         float timelineValue = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), hasValue);
49
50         if (hasValue)
51             m_value = timelineValue;
52     }
53
54     return narrowPrecisionToFloat(m_value);
55 }
56
57 void AudioParam::setValue(float value)
58 {
59     // Check against JavaScript giving us bogus floating-point values.
60     // Don't ASSERT, since this can happen if somebody writes bad JS.
61     if (!std::isnan(value) && !std::isinf(value))
62         m_value = value;
63 }
64
65 float AudioParam::smoothedValue()
66 {
67     return narrowPrecisionToFloat(m_smoothedValue);
68 }
69
70 bool AudioParam::smooth()
71 {
72     // If values have been explicitly scheduled on the timeline, then use the exact value.
73     // Smoothing effectively is performed by the timeline.
74     bool useTimelineValue = false;
75     if (context())
76         m_value = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), useTimelineValue);
77
78     if (m_smoothedValue == m_value) {
79         // Smoothed value has already approached and snapped to value.
80         return true;
81     }
82
83     if (useTimelineValue)
84         m_smoothedValue = m_value;
85     else {
86         // Dezipper - exponential approach.
87         m_smoothedValue += (m_value - m_smoothedValue) * DefaultSmoothingConstant;
88
89         // If we get close enough then snap to actual value.
90         if (fabs(m_smoothedValue - m_value) < SnapThreshold) // FIXME: the threshold needs to be adjustable depending on range - but this is OK general purpose value.
91             m_smoothedValue = m_value;
92     }
93
94     return false;
95 }
96
97 float AudioParam::finalValue()
98 {
99     float value = m_value;
100     calculateFinalValues(&value, 1, false);
101     return value;
102 }
103
104 void AudioParam::calculateSampleAccurateValues(float* values, unsigned numberOfValues)
105 {
106     bool isSafe = context() && context()->isAudioThread() && values && numberOfValues;
107     ASSERT(isSafe);
108     if (!isSafe)
109         return;
110
111     calculateFinalValues(values, numberOfValues, true);
112 }
113
114 void AudioParam::calculateFinalValues(float* values, unsigned numberOfValues, bool sampleAccurate)
115 {
116     bool isGood = context() && context()->isAudioThread() && values && numberOfValues;
117     ASSERT(isGood);
118     if (!isGood)
119         return;
120
121     // The calculated result will be the "intrinsic" value summed with all audio-rate connections.
122
123     if (sampleAccurate) {
124         // Calculate sample-accurate (a-rate) intrinsic values.
125         calculateTimelineValues(values, numberOfValues);
126     } else {
127         // Calculate control-rate (k-rate) intrinsic value.
128         bool hasValue;
129         float timelineValue = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), hasValue);
130
131         if (hasValue)
132             m_value = timelineValue;
133
134         values[0] = narrowPrecisionToFloat(m_value);
135     }
136
137     // Now sum all of the audio-rate connections together (unity-gain summing junction).
138     // Note that connections would normally be mono, but we mix down to mono if necessary.
139     RefPtr<AudioBus> summingBus = AudioBus::create(1, numberOfValues, false);
140     summingBus->setChannelMemory(0, values, numberOfValues);
141
142     for (unsigned i = 0; i < numberOfRenderingConnections(); ++i) {
143         AudioNodeOutput* output = renderingOutput(i);
144         ASSERT(output);
145
146         // Render audio from this output.
147         AudioBus* connectionBus = output->pull(0, AudioNode::ProcessingSizeInFrames);
148
149         // Sum, with unity-gain.
150         summingBus->sumFrom(*connectionBus);
151     }
152 }
153
154 void AudioParam::calculateTimelineValues(float* values, unsigned numberOfValues)
155 {
156     // Calculate values for this render quantum.
157     // Normally numberOfValues will equal AudioNode::ProcessingSizeInFrames (the render quantum size).
158     double sampleRate = context()->sampleRate();
159     double startTime = context()->currentTime();
160     double endTime = startTime + numberOfValues / sampleRate;
161
162     // Note we're running control rate at the sample-rate.
163     // Pass in the current value as default value.
164     m_value = m_timeline.valuesForTimeRange(startTime, endTime, narrowPrecisionToFloat(m_value), values, numberOfValues, sampleRate, sampleRate);
165 }
166
167 void AudioParam::connect(AudioNodeOutput& output)
168 {
169     ASSERT(context()->isGraphOwner());
170
171     if (m_outputs.contains(&output))
172         return;
173
174     output.addParam(*this);
175     m_outputs.add(&output);
176     changedOutputs();
177 }
178
179 void AudioParam::disconnect(AudioNodeOutput& output)
180 {
181     ASSERT(context()->isGraphOwner());
182
183     if (m_outputs.contains(&output)) {
184         m_outputs.remove(&output);
185         changedOutputs();
186         output.removeParam(*this);
187     }
188 }
189
190 } // namespace blink
191
192 #endif // ENABLE(WEB_AUDIO)