OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012, Google Inc. All rights reserved. | 2 * Copyright (C) 2012, 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 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 periodicWave = context()->periodicWave(SQUARE); | 119 periodicWave = context()->periodicWave(SQUARE); |
120 break; | 120 break; |
121 case SAWTOOTH: | 121 case SAWTOOTH: |
122 periodicWave = context()->periodicWave(SAWTOOTH); | 122 periodicWave = context()->periodicWave(SAWTOOTH); |
123 break; | 123 break; |
124 case TRIANGLE: | 124 case TRIANGLE: |
125 periodicWave = context()->periodicWave(TRIANGLE); | 125 periodicWave = context()->periodicWave(TRIANGLE); |
126 break; | 126 break; |
127 case CUSTOM: | 127 case CUSTOM: |
128 default: | 128 default: |
129 // Return false for invalid types, including CUSTOM since setPeriodicWave(
) method must be | 129 // Return false for invalid types, including CUSTOM since |
130 // called explicitly. | 130 // setPeriodicWave() method must be called explicitly. |
131 ASSERT_NOT_REACHED(); | 131 ASSERT_NOT_REACHED(); |
132 return false; | 132 return false; |
133 } | 133 } |
134 | 134 |
135 setPeriodicWave(periodicWave); | 135 setPeriodicWave(periodicWave); |
136 m_type = type; | 136 m_type = type; |
137 return true; | 137 return true; |
138 } | 138 } |
139 | 139 |
140 bool OscillatorHandler::calculateSampleAccuratePhaseIncrements( | 140 bool OscillatorHandler::calculateSampleAccuratePhaseIncrements( |
(...skipping 18 matching lines...) Expand all Loading... |
159 | 159 |
160 if (m_frequency->hasSampleAccurateValues()) { | 160 if (m_frequency->hasSampleAccurateValues()) { |
161 hasSampleAccurateValues = true; | 161 hasSampleAccurateValues = true; |
162 hasFrequencyChanges = true; | 162 hasFrequencyChanges = true; |
163 | 163 |
164 // Get the sample-accurate frequency values and convert to phase increments. | 164 // Get the sample-accurate frequency values and convert to phase increments. |
165 // They will be converted to phase increments below. | 165 // They will be converted to phase increments below. |
166 m_frequency->calculateSampleAccurateValues(phaseIncrements, | 166 m_frequency->calculateSampleAccurateValues(phaseIncrements, |
167 framesToProcess); | 167 framesToProcess); |
168 } else { | 168 } else { |
169 // Handle ordinary parameter smoothing/de-zippering if there are no schedule
d changes. | 169 // Handle ordinary parameter smoothing/de-zippering if there are no |
| 170 // scheduled changes. |
170 m_frequency->smooth(); | 171 m_frequency->smooth(); |
171 float frequency = m_frequency->smoothedValue(); | 172 float frequency = m_frequency->smoothedValue(); |
172 finalScale *= frequency; | 173 finalScale *= frequency; |
173 } | 174 } |
174 | 175 |
175 if (m_detune->hasSampleAccurateValues()) { | 176 if (m_detune->hasSampleAccurateValues()) { |
176 hasSampleAccurateValues = true; | 177 hasSampleAccurateValues = true; |
177 | 178 |
178 // Get the sample-accurate detune values. | 179 // Get the sample-accurate detune values. |
179 float* detuneValues = | 180 float* detuneValues = |
180 hasFrequencyChanges ? m_detuneValues.data() : phaseIncrements; | 181 hasFrequencyChanges ? m_detuneValues.data() : phaseIncrements; |
181 m_detune->calculateSampleAccurateValues(detuneValues, framesToProcess); | 182 m_detune->calculateSampleAccurateValues(detuneValues, framesToProcess); |
182 | 183 |
183 // Convert from cents to rate scalar. | 184 // Convert from cents to rate scalar. |
184 float k = 1.0 / 1200; | 185 float k = 1.0 / 1200; |
185 vsmul(detuneValues, 1, &k, detuneValues, 1, framesToProcess); | 186 vsmul(detuneValues, 1, &k, detuneValues, 1, framesToProcess); |
186 for (unsigned i = 0; i < framesToProcess; ++i) | 187 for (unsigned i = 0; i < framesToProcess; ++i) |
187 detuneValues[i] = powf( | 188 detuneValues[i] = powf( |
188 2, detuneValues[i]); // FIXME: converting to expf() will be faster. | 189 2, detuneValues[i]); // FIXME: converting to expf() will be faster. |
189 | 190 |
190 if (hasFrequencyChanges) { | 191 if (hasFrequencyChanges) { |
191 // Multiply frequencies by detune scalings. | 192 // Multiply frequencies by detune scalings. |
192 vmul(detuneValues, 1, phaseIncrements, 1, phaseIncrements, 1, | 193 vmul(detuneValues, 1, phaseIncrements, 1, phaseIncrements, 1, |
193 framesToProcess); | 194 framesToProcess); |
194 } | 195 } |
195 } else { | 196 } else { |
196 // Handle ordinary parameter smoothing/de-zippering if there are no schedule
d changes. | 197 // Handle ordinary parameter smoothing/de-zippering if there are no |
| 198 // scheduled changes. |
197 m_detune->smooth(); | 199 m_detune->smooth(); |
198 float detune = m_detune->smoothedValue(); | 200 float detune = m_detune->smoothedValue(); |
199 float detuneScale = powf(2, detune / 1200); | 201 float detuneScale = powf(2, detune / 1200); |
200 finalScale *= detuneScale; | 202 finalScale *= detuneScale; |
201 } | 203 } |
202 | 204 |
203 if (hasSampleAccurateValues) { | 205 if (hasSampleAccurateValues) { |
204 // Convert from frequency to wavetable increment. | 206 // Convert from frequency to wavetable increment. |
205 vsmul(phaseIncrements, 1, &finalScale, phaseIncrements, 1, framesToProcess); | 207 vsmul(phaseIncrements, 1, &finalScale, phaseIncrements, 1, framesToProcess); |
206 } | 208 } |
207 | 209 |
208 return hasSampleAccurateValues; | 210 return hasSampleAccurateValues; |
209 } | 211 } |
210 | 212 |
211 void OscillatorHandler::process(size_t framesToProcess) { | 213 void OscillatorHandler::process(size_t framesToProcess) { |
212 AudioBus* outputBus = output(0).bus(); | 214 AudioBus* outputBus = output(0).bus(); |
213 | 215 |
214 if (!isInitialized() || !outputBus->numberOfChannels()) { | 216 if (!isInitialized() || !outputBus->numberOfChannels()) { |
215 outputBus->zero(); | 217 outputBus->zero(); |
216 return; | 218 return; |
217 } | 219 } |
218 | 220 |
219 DCHECK_LE(framesToProcess, m_phaseIncrements.size()); | 221 DCHECK_LE(framesToProcess, m_phaseIncrements.size()); |
220 if (framesToProcess > m_phaseIncrements.size()) | 222 if (framesToProcess > m_phaseIncrements.size()) |
221 return; | 223 return; |
222 | 224 |
223 // The audio thread can't block on this lock, so we call tryLock() instead. | 225 // The audio thread can't block on this lock, so we call tryLock() instead. |
224 MutexTryLocker tryLocker(m_processLock); | 226 MutexTryLocker tryLocker(m_processLock); |
225 if (!tryLocker.locked()) { | 227 if (!tryLocker.locked()) { |
226 // Too bad - the tryLock() failed. We must be in the middle of changing wave
-tables. | 228 // Too bad - the tryLock() failed. We must be in the middle of changing |
| 229 // wave-tables. |
227 outputBus->zero(); | 230 outputBus->zero(); |
228 return; | 231 return; |
229 } | 232 } |
230 | 233 |
231 // We must access m_periodicWave only inside the lock. | 234 // We must access m_periodicWave only inside the lock. |
232 if (!m_periodicWave.get()) { | 235 if (!m_periodicWave.get()) { |
233 outputBus->zero(); | 236 outputBus->zero(); |
234 return; | 237 return; |
235 } | 238 } |
236 | 239 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 interpolationFactor * sample2Higher; | 314 interpolationFactor * sample2Higher; |
312 float sampleLower = (1 - interpolationFactor) * sample1Lower + | 315 float sampleLower = (1 - interpolationFactor) * sample1Lower + |
313 interpolationFactor * sample2Lower; | 316 interpolationFactor * sample2Lower; |
314 | 317 |
315 // Then interpolate between the two tables. | 318 // Then interpolate between the two tables. |
316 float sample = (1 - tableInterpolationFactor) * sampleHigher + | 319 float sample = (1 - tableInterpolationFactor) * sampleHigher + |
317 tableInterpolationFactor * sampleLower; | 320 tableInterpolationFactor * sampleLower; |
318 | 321 |
319 *destP++ = sample; | 322 *destP++ = sample; |
320 | 323 |
321 // Increment virtual read index and wrap virtualReadIndex into the range 0 -
> periodicWaveSize. | 324 // Increment virtual read index and wrap virtualReadIndex into the range |
| 325 // 0 -> periodicWaveSize. |
322 virtualReadIndex += incr; | 326 virtualReadIndex += incr; |
323 virtualReadIndex -= | 327 virtualReadIndex -= |
324 floor(virtualReadIndex * invPeriodicWaveSize) * periodicWaveSize; | 328 floor(virtualReadIndex * invPeriodicWaveSize) * periodicWaveSize; |
325 } | 329 } |
326 | 330 |
327 m_virtualReadIndex = virtualReadIndex; | 331 m_virtualReadIndex = virtualReadIndex; |
328 | 332 |
329 outputBus->clearSilentFlag(); | 333 outputBus->clearSilentFlag(); |
330 } | 334 } |
331 | 335 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 | 446 |
443 AudioParam* OscillatorNode::detune() { | 447 AudioParam* OscillatorNode::detune() { |
444 return m_detune; | 448 return m_detune; |
445 } | 449 } |
446 | 450 |
447 void OscillatorNode::setPeriodicWave(PeriodicWave* wave) { | 451 void OscillatorNode::setPeriodicWave(PeriodicWave* wave) { |
448 oscillatorHandler().setPeriodicWave(wave); | 452 oscillatorHandler().setPeriodicWave(wave); |
449 } | 453 } |
450 | 454 |
451 } // namespace blink | 455 } // namespace blink |
OLD | NEW |