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.
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.
30 #include "modules/webaudio/AudioParam.h"
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"
40 const double AudioParam::DefaultSmoothingConstant = 0.05;
41 const double AudioParam::SnapThreshold = 0.001;
43 float AudioParam::value()
45 // Update value for timeline.
46 if (context() && context()->isAudioThread()) {
48 float timelineValue = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), hasValue);
51 m_value = timelineValue;
54 return narrowPrecisionToFloat(m_value);
57 void AudioParam::setValue(float value)
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))
65 float AudioParam::smoothedValue()
67 return narrowPrecisionToFloat(m_smoothedValue);
70 bool AudioParam::smooth()
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;
76 m_value = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), useTimelineValue);
78 if (m_smoothedValue == m_value) {
79 // Smoothed value has already approached and snapped to value.
84 m_smoothedValue = m_value;
86 // Dezipper - exponential approach.
87 m_smoothedValue += (m_value - m_smoothedValue) * DefaultSmoothingConstant;
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;
97 float AudioParam::finalValue()
99 float value = m_value;
100 calculateFinalValues(&value, 1, false);
104 void AudioParam::calculateSampleAccurateValues(float* values, unsigned numberOfValues)
106 bool isSafe = context() && context()->isAudioThread() && values && numberOfValues;
111 calculateFinalValues(values, numberOfValues, true);
114 void AudioParam::calculateFinalValues(float* values, unsigned numberOfValues, bool sampleAccurate)
116 bool isGood = context() && context()->isAudioThread() && values && numberOfValues;
121 // The calculated result will be the "intrinsic" value summed with all audio-rate connections.
123 if (sampleAccurate) {
124 // Calculate sample-accurate (a-rate) intrinsic values.
125 calculateTimelineValues(values, numberOfValues);
127 // Calculate control-rate (k-rate) intrinsic value.
129 float timelineValue = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), hasValue);
132 m_value = timelineValue;
134 values[0] = narrowPrecisionToFloat(m_value);
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);
142 for (unsigned i = 0; i < numberOfRenderingConnections(); ++i) {
143 AudioNodeOutput* output = renderingOutput(i);
146 // Render audio from this output.
147 AudioBus* connectionBus = output->pull(0, AudioNode::ProcessingSizeInFrames);
149 // Sum, with unity-gain.
150 summingBus->sumFrom(*connectionBus);
154 void AudioParam::calculateTimelineValues(float* values, unsigned numberOfValues)
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;
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);
167 void AudioParam::connect(AudioNodeOutput& output)
169 ASSERT(context()->isGraphOwner());
171 if (m_outputs.contains(&output))
174 output.addParam(*this);
175 m_outputs.add(&output);
179 void AudioParam::disconnect(AudioNodeOutput& output)
181 ASSERT(context()->isGraphOwner());
183 if (m_outputs.contains(&output)) {
184 m_outputs.remove(&output);
186 output.removeParam(*this);
192 #endif // ENABLE(WEB_AUDIO)