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 "AudioNodeOutput.h" |
34 | #include "AudioUtilities.h" |
35 | #include "FloatConversion.h" |
36 | #include "Logging.h" |
37 | #include <wtf/MathExtras.h> |
38 | |
39 | namespace WebCore { |
40 | |
41 | const double AudioParam::DefaultSmoothingConstant = 0.05; |
42 | const double AudioParam::SnapThreshold = 0.001; |
43 | |
44 | AudioParam::AudioParam(AudioContext& context, const String& name, double defaultValue, double minValue, double maxValue, unsigned units) |
45 | : AudioSummingJunction(context) |
46 | , m_name(name) |
47 | , m_value(defaultValue) |
48 | , m_defaultValue(defaultValue) |
49 | , m_minValue(minValue) |
50 | , m_maxValue(maxValue) |
51 | , m_units(units) |
52 | , m_smoothedValue(defaultValue) |
53 | , m_smoothingConstant(DefaultSmoothingConstant) |
54 | #if !RELEASE_LOG_DISABLED |
55 | , m_logger(context.logger()) |
56 | , m_logIdentifier(context.nextAudioParameterLogIdentifier()) |
57 | #endif |
58 | { |
59 | ALWAYS_LOG(LOGIDENTIFIER, "name = " , m_name, ", value = " , m_value, ", default = " , m_defaultValue, ", min = " , m_minValue, ", max = " , m_maxValue, ", units = " , m_units); |
60 | } |
61 | |
62 | float AudioParam::value() |
63 | { |
64 | // Update value for timeline. |
65 | if (context().isAudioThread()) { |
66 | bool hasValue; |
67 | float timelineValue = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), hasValue); |
68 | |
69 | if (hasValue) |
70 | m_value = timelineValue; |
71 | } |
72 | |
73 | return narrowPrecisionToFloat(m_value); |
74 | } |
75 | |
76 | void AudioParam::setValue(float value) |
77 | { |
78 | DEBUG_LOG(LOGIDENTIFIER, value); |
79 | |
80 | // Check against JavaScript giving us bogus floating-point values. |
81 | // Don't ASSERT, since this can happen if somebody writes bad JS. |
82 | if (!std::isnan(value) && !std::isinf(value)) |
83 | m_value = value; |
84 | } |
85 | |
86 | float AudioParam::smoothedValue() |
87 | { |
88 | return narrowPrecisionToFloat(m_smoothedValue); |
89 | } |
90 | |
91 | bool AudioParam::smooth() |
92 | { |
93 | // If values have been explicitly scheduled on the timeline, then use the exact value. |
94 | // Smoothing effectively is performed by the timeline. |
95 | bool useTimelineValue = false; |
96 | m_value = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), useTimelineValue); |
97 | |
98 | if (m_smoothedValue == m_value) { |
99 | // Smoothed value has already approached and snapped to value. |
100 | return true; |
101 | } |
102 | |
103 | if (useTimelineValue) |
104 | m_smoothedValue = m_value; |
105 | else { |
106 | // Dezipper - exponential approach. |
107 | m_smoothedValue += (m_value - m_smoothedValue) * m_smoothingConstant; |
108 | |
109 | // If we get close enough then snap to actual value. |
110 | if (fabs(m_smoothedValue - m_value) < SnapThreshold) // FIXME: the threshold needs to be adjustable depending on range - but this is OK general purpose value. |
111 | m_smoothedValue = m_value; |
112 | } |
113 | |
114 | return false; |
115 | } |
116 | |
117 | float AudioParam::finalValue() |
118 | { |
119 | float value; |
120 | calculateFinalValues(&value, 1, false); |
121 | return value; |
122 | } |
123 | |
124 | void AudioParam::calculateSampleAccurateValues(float* values, unsigned numberOfValues) |
125 | { |
126 | bool isSafe = context().isAudioThread() && values && numberOfValues; |
127 | ASSERT(isSafe); |
128 | if (!isSafe) |
129 | return; |
130 | |
131 | calculateFinalValues(values, numberOfValues, true); |
132 | } |
133 | |
134 | void AudioParam::calculateFinalValues(float* values, unsigned numberOfValues, bool sampleAccurate) |
135 | { |
136 | bool isGood = context().isAudioThread() && values && numberOfValues; |
137 | ASSERT(isGood); |
138 | if (!isGood) |
139 | return; |
140 | |
141 | // The calculated result will be the "intrinsic" value summed with all audio-rate connections. |
142 | |
143 | if (sampleAccurate) { |
144 | // Calculate sample-accurate (a-rate) intrinsic values. |
145 | calculateTimelineValues(values, numberOfValues); |
146 | } else { |
147 | // Calculate control-rate (k-rate) intrinsic value. |
148 | bool hasValue; |
149 | float timelineValue = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), hasValue); |
150 | |
151 | if (hasValue) |
152 | m_value = timelineValue; |
153 | |
154 | values[0] = narrowPrecisionToFloat(m_value); |
155 | } |
156 | |
157 | // Now sum all of the audio-rate connections together (unity-gain summing junction). |
158 | // Note that connections would normally be mono, but we mix down to mono if necessary. |
159 | auto summingBus = AudioBus::create(1, numberOfValues, false); |
160 | summingBus->setChannelMemory(0, values, numberOfValues); |
161 | |
162 | for (auto& output : m_renderingOutputs) { |
163 | ASSERT(output); |
164 | |
165 | // Render audio from this output. |
166 | AudioBus* connectionBus = output->pull(0, AudioNode::ProcessingSizeInFrames); |
167 | |
168 | // Sum, with unity-gain. |
169 | summingBus->sumFrom(*connectionBus); |
170 | } |
171 | } |
172 | |
173 | void AudioParam::calculateTimelineValues(float* values, unsigned numberOfValues) |
174 | { |
175 | // Calculate values for this render quantum. |
176 | // Normally numberOfValues will equal AudioNode::ProcessingSizeInFrames (the render quantum size). |
177 | double sampleRate = context().sampleRate(); |
178 | double startTime = context().currentTime(); |
179 | double endTime = startTime + numberOfValues / sampleRate; |
180 | |
181 | // Note we're running control rate at the sample-rate. |
182 | // Pass in the current value as default value. |
183 | m_value = m_timeline.valuesForTimeRange(startTime, endTime, narrowPrecisionToFloat(m_value), values, numberOfValues, sampleRate, sampleRate); |
184 | } |
185 | |
186 | void AudioParam::connect(AudioNodeOutput* output) |
187 | { |
188 | ASSERT(context().isGraphOwner()); |
189 | |
190 | ASSERT(output); |
191 | if (!output) |
192 | return; |
193 | |
194 | if (!m_outputs.add(output).isNewEntry) |
195 | return; |
196 | |
197 | INFO_LOG(LOGIDENTIFIER, output->node()->nodeType()); |
198 | |
199 | output->addParam(this); |
200 | changedOutputs(); |
201 | } |
202 | |
203 | void AudioParam::disconnect(AudioNodeOutput* output) |
204 | { |
205 | ASSERT(context().isGraphOwner()); |
206 | |
207 | ASSERT(output); |
208 | if (!output) |
209 | return; |
210 | |
211 | INFO_LOG(LOGIDENTIFIER, output->node()->nodeType()); |
212 | |
213 | if (m_outputs.remove(output)) { |
214 | changedOutputs(); |
215 | output->removeParam(this); |
216 | } |
217 | } |
218 | |
219 | #if !RELEASE_LOG_DISABLED |
220 | WTFLogChannel& AudioParam::logChannel() const |
221 | { |
222 | return LogMedia; |
223 | } |
224 | #endif |
225 | |
226 | |
227 | } // namespace WebCore |
228 | |
229 | #endif // ENABLE(WEB_AUDIO) |
230 | |