| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "modules/webaudio/AudioParam.h" | 28 #include "modules/webaudio/AudioParam.h" |
| 29 | 29 |
| 30 #include "modules/webaudio/AudioNode.h" | 30 #include "modules/webaudio/AudioNode.h" |
| 31 #include "modules/webaudio/AudioNodeOutput.h" | 31 #include "modules/webaudio/AudioNodeOutput.h" |
| 32 #include "platform/FloatConversion.h" | 32 #include "platform/FloatConversion.h" |
| 33 #include "platform/audio/AudioUtilities.h" | 33 #include "platform/audio/AudioUtilities.h" |
| 34 #include "wtf/MathExtras.h" | 34 #include "wtf/MathExtras.h" |
| 35 | 35 |
| 36 namespace blink { | 36 namespace blink { |
| 37 | 37 |
| 38 const double AudioParam::DefaultSmoothingConstant = 0.05; | 38 const double AudioParamHandler::DefaultSmoothingConstant = 0.05; |
| 39 const double AudioParam::SnapThreshold = 0.001; | 39 const double AudioParamHandler::SnapThreshold = 0.001; |
| 40 | 40 |
| 41 float AudioParam::value() | 41 float AudioParamHandler::value() |
| 42 { | 42 { |
| 43 // Update value for timeline. | 43 // Update value for timeline. |
| 44 if (context() && context()->isAudioThread()) { | 44 if (context() && context()->isAudioThread()) { |
| 45 bool hasValue; | 45 bool hasValue; |
| 46 float timelineValue = m_timeline.valueForContextTime(context(), narrowPr
ecisionToFloat(m_value), hasValue); | 46 float timelineValue = m_timeline.valueForContextTime(context(), narrowPr
ecisionToFloat(m_value), hasValue); |
| 47 | 47 |
| 48 if (hasValue) | 48 if (hasValue) |
| 49 m_value = timelineValue; | 49 m_value = timelineValue; |
| 50 } | 50 } |
| 51 | 51 |
| 52 return narrowPrecisionToFloat(m_value); | 52 return narrowPrecisionToFloat(m_value); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void AudioParam::setValue(float value) | 55 void AudioParamHandler::setValue(float value) |
| 56 { | 56 { |
| 57 m_value = value; | 57 m_value = value; |
| 58 } | 58 } |
| 59 | 59 |
| 60 float AudioParam::smoothedValue() | 60 float AudioParamHandler::smoothedValue() |
| 61 { | 61 { |
| 62 return narrowPrecisionToFloat(m_smoothedValue); | 62 return narrowPrecisionToFloat(m_smoothedValue); |
| 63 } | 63 } |
| 64 | 64 |
| 65 bool AudioParam::smooth() | 65 bool AudioParamHandler::smooth() |
| 66 { | 66 { |
| 67 // If values have been explicitly scheduled on the timeline, then use the ex
act value. | 67 // If values have been explicitly scheduled on the timeline, then use the ex
act value. |
| 68 // Smoothing effectively is performed by the timeline. | 68 // Smoothing effectively is performed by the timeline. |
| 69 bool useTimelineValue = false; | 69 bool useTimelineValue = false; |
| 70 if (context()) | 70 if (context()) |
| 71 m_value = m_timeline.valueForContextTime(context(), narrowPrecisionToFlo
at(m_value), useTimelineValue); | 71 m_value = m_timeline.valueForContextTime(context(), narrowPrecisionToFlo
at(m_value), useTimelineValue); |
| 72 | 72 |
| 73 if (m_smoothedValue == m_value) { | 73 if (m_smoothedValue == m_value) { |
| 74 // Smoothed value has already approached and snapped to value. | 74 // Smoothed value has already approached and snapped to value. |
| 75 return true; | 75 return true; |
| 76 } | 76 } |
| 77 | 77 |
| 78 if (useTimelineValue) { | 78 if (useTimelineValue) { |
| 79 m_smoothedValue = m_value; | 79 m_smoothedValue = m_value; |
| 80 } else { | 80 } else { |
| 81 // Dezipper - exponential approach. | 81 // Dezipper - exponential approach. |
| 82 m_smoothedValue += (m_value - m_smoothedValue) * DefaultSmoothingConstan
t; | 82 m_smoothedValue += (m_value - m_smoothedValue) * DefaultSmoothingConstan
t; |
| 83 | 83 |
| 84 // If we get close enough then snap to actual value. | 84 // If we get close enough then snap to actual value. |
| 85 // FIXME: the threshold needs to be adjustable depending on range - but | 85 // FIXME: the threshold needs to be adjustable depending on range - but |
| 86 // this is OK general purpose value. | 86 // this is OK general purpose value. |
| 87 if (fabs(m_smoothedValue - m_value) < SnapThreshold) | 87 if (fabs(m_smoothedValue - m_value) < SnapThreshold) |
| 88 m_smoothedValue = m_value; | 88 m_smoothedValue = m_value; |
| 89 } | 89 } |
| 90 | 90 |
| 91 return false; | 91 return false; |
| 92 } | 92 } |
| 93 | 93 |
| 94 float AudioParam::finalValue() | 94 float AudioParamHandler::finalValue() |
| 95 { | 95 { |
| 96 float value = m_value; | 96 float value = m_value; |
| 97 calculateFinalValues(&value, 1, false); | 97 calculateFinalValues(&value, 1, false); |
| 98 return value; | 98 return value; |
| 99 } | 99 } |
| 100 | 100 |
| 101 void AudioParam::calculateSampleAccurateValues(float* values, unsigned numberOfV
alues) | 101 void AudioParamHandler::calculateSampleAccurateValues(float* values, unsigned nu
mberOfValues) |
| 102 { | 102 { |
| 103 bool isSafe = context() && context()->isAudioThread() && values && numberOfV
alues; | 103 bool isSafe = context() && context()->isAudioThread() && values && numberOfV
alues; |
| 104 ASSERT(isSafe); | 104 ASSERT(isSafe); |
| 105 if (!isSafe) | 105 if (!isSafe) |
| 106 return; | 106 return; |
| 107 | 107 |
| 108 calculateFinalValues(values, numberOfValues, true); | 108 calculateFinalValues(values, numberOfValues, true); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void AudioParam::calculateFinalValues(float* values, unsigned numberOfValues, bo
ol sampleAccurate) | 111 void AudioParamHandler::calculateFinalValues(float* values, unsigned numberOfVal
ues, bool sampleAccurate) |
| 112 { | 112 { |
| 113 bool isGood = context() && context()->isAudioThread() && values && numberOfV
alues; | 113 bool isGood = context() && context()->isAudioThread() && values && numberOfV
alues; |
| 114 ASSERT(isGood); | 114 ASSERT(isGood); |
| 115 if (!isGood) | 115 if (!isGood) |
| 116 return; | 116 return; |
| 117 | 117 |
| 118 // The calculated result will be the "intrinsic" value summed with all audio
-rate connections. | 118 // The calculated result will be the "intrinsic" value summed with all audio
-rate connections. |
| 119 | 119 |
| 120 if (sampleAccurate) { | 120 if (sampleAccurate) { |
| 121 // Calculate sample-accurate (a-rate) intrinsic values. | 121 // Calculate sample-accurate (a-rate) intrinsic values. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 141 ASSERT(output); | 141 ASSERT(output); |
| 142 | 142 |
| 143 // Render audio from this output. | 143 // Render audio from this output. |
| 144 AudioBus* connectionBus = output->pull(0, AudioNode::ProcessingSizeInFra
mes); | 144 AudioBus* connectionBus = output->pull(0, AudioNode::ProcessingSizeInFra
mes); |
| 145 | 145 |
| 146 // Sum, with unity-gain. | 146 // Sum, with unity-gain. |
| 147 summingBus->sumFrom(*connectionBus); | 147 summingBus->sumFrom(*connectionBus); |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 | 150 |
| 151 void AudioParam::calculateTimelineValues(float* values, unsigned numberOfValues) | 151 void AudioParamHandler::calculateTimelineValues(float* values, unsigned numberOf
Values) |
| 152 { | 152 { |
| 153 // Calculate values for this render quantum. | 153 // Calculate values for this render quantum. |
| 154 // Normally numberOfValues will equal AudioNode::ProcessingSizeInFrames (the
render quantum size). | 154 // Normally numberOfValues will equal AudioNode::ProcessingSizeInFrames (the
render quantum size). |
| 155 double sampleRate = context()->sampleRate(); | 155 double sampleRate = context()->sampleRate(); |
| 156 double startTime = context()->currentTime(); | 156 double startTime = context()->currentTime(); |
| 157 double endTime = startTime + numberOfValues / sampleRate; | 157 double endTime = startTime + numberOfValues / sampleRate; |
| 158 | 158 |
| 159 // Note we're running control rate at the sample-rate. | 159 // Note we're running control rate at the sample-rate. |
| 160 // Pass in the current value as default value. | 160 // Pass in the current value as default value. |
| 161 m_value = m_timeline.valuesForTimeRange(startTime, endTime, narrowPrecisionT
oFloat(m_value), values, numberOfValues, sampleRate, sampleRate); | 161 m_value = m_timeline.valuesForTimeRange(startTime, endTime, narrowPrecisionT
oFloat(m_value), values, numberOfValues, sampleRate, sampleRate); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void AudioParam::connect(AudioNodeOutput& output) | 164 void AudioParamHandler::connect(AudioNodeOutput& output) |
| 165 { | 165 { |
| 166 ASSERT(context()->isGraphOwner()); | 166 ASSERT(context()->isGraphOwner()); |
| 167 | 167 |
| 168 if (m_outputs.contains(&output)) | 168 if (m_outputs.contains(&output)) |
| 169 return; | 169 return; |
| 170 | 170 |
| 171 output.addParam(*this); | 171 output.addParam(*this); |
| 172 m_outputs.add(&output); | 172 m_outputs.add(&output); |
| 173 changedOutputs(); | 173 changedOutputs(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 void AudioParam::disconnect(AudioNodeOutput& output) | 176 void AudioParamHandler::disconnect(AudioNodeOutput& output) |
| 177 { | 177 { |
| 178 ASSERT(context()->isGraphOwner()); | 178 ASSERT(context()->isGraphOwner()); |
| 179 | 179 |
| 180 if (m_outputs.contains(&output)) { | 180 if (m_outputs.contains(&output)) { |
| 181 m_outputs.remove(&output); | 181 m_outputs.remove(&output); |
| 182 changedOutputs(); | 182 changedOutputs(); |
| 183 output.removeParam(*this); | 183 output.removeParam(*this); |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 | 186 |
| 187 } // namespace blink | 187 } // namespace blink |
| 188 | 188 |
| 189 #endif // ENABLE(WEB_AUDIO) | 189 #endif // ENABLE(WEB_AUDIO) |
| OLD | NEW |