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 "AudioParam.h"
32 #include "AudioNode.h"
33 #include "AudioUtilities.h"
34 #include <wtf/MathExtras.h>
38 const double AudioParam::DefaultSmoothingConstant = 0.05;
39 const double AudioParam::SnapThreshold = 0.001;
41 float AudioParam::value()
43 // Update value for timeline.
44 if (context() && context()->isAudioThread()) {
46 float timelineValue = m_timeline.valueForContextTime(context(), m_value, hasValue);
49 m_value = timelineValue;
52 return static_cast<float>(m_value);
55 void AudioParam::setValue(float value)
57 // Check against JavaScript giving us bogus floating-point values.
58 // Don't ASSERT, since this can happen if somebody writes bad JS.
59 if (!isnan(value) && !isinf(value))
63 float AudioParam::smoothedValue()
65 return static_cast<float>(m_smoothedValue);
68 bool AudioParam::smooth()
70 // If values have been explicitly scheduled on the timeline, then use the exact value.
71 // Smoothing effectively is performed by the timeline.
72 bool useTimelineValue = false;
74 m_value = m_timeline.valueForContextTime(context(), m_value, useTimelineValue);
76 if (m_smoothedValue == m_value) {
77 // Smoothed value has already approached and snapped to value.
82 m_smoothedValue = m_value;
84 // Dezipper - exponential approach.
85 m_smoothedValue += (m_value - m_smoothedValue) * m_smoothingConstant;
87 // If we get close enough then snap to actual value.
88 if (fabs(m_smoothedValue - m_value) < SnapThreshold) // FIXME: the threshold needs to be adjustable depending on range - but this is OK general purpose value.
89 m_smoothedValue = m_value;
95 void AudioParam::calculateSampleAccurateValues(float* values, unsigned numberOfValues)
97 bool isSafe = context() && context()->isAudioThread() && values;
102 // Calculate values for this render quantum.
103 // Normally numberOfValues will equal AudioNode::ProcessingSizeInFrames (the render quantum size).
104 float sampleRate = context()->sampleRate();
105 float startTime = context()->currentTime();
106 float endTime = startTime + numberOfValues / sampleRate;
108 // Note we're running control rate at the sample-rate.
109 // Pass in the current value as default value.
110 m_value = m_timeline.valuesForTimeRange(startTime, endTime, m_value, values, numberOfValues, sampleRate, sampleRate);
113 } // namespace WebCore
115 #endif // ENABLE(WEB_AUDIO)