initial import
[vuplus_webkit] / Source / WebCore / 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 "AudioParam.h"
31
32 #include "AudioNode.h"
33 #include "AudioUtilities.h"
34 #include <wtf/MathExtras.h>
35
36 namespace WebCore {
37
38 const double AudioParam::DefaultSmoothingConstant = 0.05;
39 const double AudioParam::SnapThreshold = 0.001;
40
41 float AudioParam::value()
42 {
43     // Update value for timeline.
44     if (context() && context()->isAudioThread()) {
45         bool hasValue;
46         float timelineValue = m_timeline.valueForContextTime(context(), m_value, hasValue);
47
48         if (hasValue)
49             m_value = timelineValue;
50     }
51
52     return static_cast<float>(m_value);
53 }
54
55 void AudioParam::setValue(float value)
56 {
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))
60         m_value = value;
61 }
62
63 float AudioParam::smoothedValue()
64 {
65     return static_cast<float>(m_smoothedValue);
66 }
67
68 bool AudioParam::smooth()
69 {
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;
73     if (context())
74         m_value = m_timeline.valueForContextTime(context(), m_value, useTimelineValue);
75     
76     if (m_smoothedValue == m_value) {
77         // Smoothed value has already approached and snapped to value.
78         return true;
79     }
80     
81     if (useTimelineValue)
82         m_smoothedValue = m_value;
83     else {
84         // Dezipper - exponential approach.
85         m_smoothedValue += (m_value - m_smoothedValue) * m_smoothingConstant;
86
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;
90     }
91
92     return false;
93 }
94
95 void AudioParam::calculateSampleAccurateValues(float* values, unsigned numberOfValues)
96 {
97     bool isSafe = context() && context()->isAudioThread() && values;
98     ASSERT(isSafe);
99     if (!isSafe)
100         return;
101
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;
107
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);
111 }
112
113 } // namespace WebCore
114
115 #endif // ENABLE(WEB_AUDIO)