| 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 * | 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 14 matching lines...) Expand all Loading... |
| 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 */ | 27 */ |
| 28 | 28 |
| 29 #include "bindings/core/v8/ExceptionMessages.h" | 29 #include "bindings/core/v8/ExceptionMessages.h" |
| 30 #include "bindings/core/v8/ExceptionState.h" | 30 #include "bindings/core/v8/ExceptionState.h" |
| 31 #include "core/dom/ExceptionCode.h" | 31 #include "core/dom/ExceptionCode.h" |
| 32 #include "modules/webaudio/BaseAudioContext.h" | 32 #include "modules/webaudio/BaseAudioContext.h" |
| 33 #include "modules/webaudio/OscillatorNode.h" | 33 #include "modules/webaudio/OscillatorNode.h" |
| 34 #include "modules/webaudio/PeriodicWave.h" | 34 #include "modules/webaudio/PeriodicWave.h" |
| 35 #include "modules/webaudio/PeriodicWaveOptions.h" |
| 35 #include "platform/audio/FFTFrame.h" | 36 #include "platform/audio/FFTFrame.h" |
| 36 #include "platform/audio/VectorMath.h" | 37 #include "platform/audio/VectorMath.h" |
| 37 #include "wtf/PtrUtil.h" | 38 #include "wtf/PtrUtil.h" |
| 38 #include <algorithm> | 39 #include <algorithm> |
| 39 #include <memory> | 40 #include <memory> |
| 40 | 41 |
| 41 namespace blink { | 42 namespace blink { |
| 42 | 43 |
| 43 // The number of bands per octave. Each octave will have this many entries in t
he wave tables. | 44 // The number of bands per octave. Each octave will have this many entries in t
he wave tables. |
| 44 const unsigned kNumberOfOctaveBands = 3; | 45 const unsigned kNumberOfOctaveBands = 3; |
| 45 | 46 |
| 46 // The max length of a periodic wave. This must be a power of two greater than o
r equal to 2048 and | 47 // The max length of a periodic wave. This must be a power of two greater than o
r equal to 2048 and |
| 47 // must be supported by the FFT routines. | 48 // must be supported by the FFT routines. |
| 48 const unsigned kMaxPeriodicWaveSize = 16384; | 49 const unsigned kMaxPeriodicWaveSize = 16384; |
| 49 | 50 |
| 50 const float CentsPerRange = 1200 / kNumberOfOctaveBands; | 51 const float CentsPerRange = 1200 / kNumberOfOctaveBands; |
| 51 | 52 |
| 52 using namespace VectorMath; | 53 using namespace VectorMath; |
| 53 | 54 |
| 54 PeriodicWave* PeriodicWave::create( | 55 PeriodicWave* PeriodicWave::create( |
| 55 BaseAudioContext& context, | 56 BaseAudioContext& context, |
| 56 DOMFloat32Array* real, | 57 size_t realLength, |
| 57 DOMFloat32Array* imag, | 58 const float* real, |
| 59 size_t imagLength, |
| 60 const float* imag, |
| 58 bool disableNormalization, | 61 bool disableNormalization, |
| 59 ExceptionState& exceptionState) | 62 ExceptionState& exceptionState) |
| 60 { | 63 { |
| 61 DCHECK(isMainThread()); | 64 DCHECK(isMainThread()); |
| 62 | 65 |
| 63 if (context.isContextClosed()) { | 66 if (context.isContextClosed()) { |
| 64 context.throwExceptionForClosedState(exceptionState); | 67 context.throwExceptionForClosedState(exceptionState); |
| 65 return nullptr; | 68 return nullptr; |
| 66 } | 69 } |
| 67 | 70 |
| 68 if (real->length() != imag->length()) { | 71 if (realLength != imagLength) { |
| 69 exceptionState.throwDOMException( | 72 exceptionState.throwDOMException( |
| 70 IndexSizeError, | 73 IndexSizeError, |
| 71 "length of real array (" + String::number(real->length()) | 74 "length of real array (" + String::number(realLength) |
| 72 + ") and length of imaginary array (" + String::number(imag->length
()) | 75 + ") and length of imaginary array (" + String::number(imagLength) |
| 73 + ") must match."); | 76 + ") must match."); |
| 74 return nullptr; | 77 return nullptr; |
| 75 } | 78 } |
| 76 | 79 |
| 77 PeriodicWave* periodicWave = new PeriodicWave(context.sampleRate()); | 80 PeriodicWave* periodicWave = new PeriodicWave(context.sampleRate()); |
| 78 size_t numberOfComponents = real->length(); | 81 periodicWave->createBandLimitedTables(real, imag, realLength, disableNormali
zation); |
| 79 periodicWave->createBandLimitedTables(real->data(), imag->data(), numberOfCo
mponents, disableNormalization); | |
| 80 return periodicWave; | 82 return periodicWave; |
| 81 } | 83 } |
| 82 | 84 |
| 85 PeriodicWave* PeriodicWave::create( |
| 86 BaseAudioContext& context, |
| 87 DOMFloat32Array* real, |
| 88 DOMFloat32Array* imag, |
| 89 bool disableNormalization, |
| 90 ExceptionState& exceptionState) |
| 91 { |
| 92 DCHECK(isMainThread()); |
| 93 |
| 94 return create( |
| 95 context, |
| 96 real->length(), |
| 97 real->data(), |
| 98 imag->length(), |
| 99 imag->data(), |
| 100 disableNormalization, |
| 101 exceptionState); |
| 102 } |
| 103 |
| 104 PeriodicWave* PeriodicWave::create( |
| 105 BaseAudioContext* context, |
| 106 const PeriodicWaveOptions& options, |
| 107 ExceptionState& exceptionState) |
| 108 { |
| 109 bool normalize = options.hasDisableNormalization() ? options.disableNormaliz
ation() : false; |
| 110 |
| 111 |
| 112 if (!options.hasReal() && !options.hasImag()) { |
| 113 exceptionState.throwDOMException( |
| 114 InvalidStateError, |
| 115 "At least one of real and imag members must be specified."); |
| 116 return nullptr; |
| 117 } |
| 118 |
| 119 Vector<float> realCoef; |
| 120 Vector<float> imagCoef; |
| 121 |
| 122 if (options.hasReal()) { |
| 123 realCoef = options.real(); |
| 124 if (options.hasImag()) |
| 125 imagCoef = options.imag(); |
| 126 else |
| 127 imagCoef.resize(realCoef.size()); |
| 128 } else { |
| 129 // We know real is not given, so imag must exist (because we checked for |
| 130 // this above). |
| 131 imagCoef = options.imag(); |
| 132 realCoef.resize(imagCoef.size()); |
| 133 } |
| 134 |
| 135 return create( |
| 136 *context, |
| 137 realCoef.size(), |
| 138 realCoef.data(), |
| 139 imagCoef.size(), |
| 140 imagCoef.data(), |
| 141 normalize, |
| 142 exceptionState); |
| 143 } |
| 144 |
| 83 PeriodicWave* PeriodicWave::createSine(float sampleRate) | 145 PeriodicWave* PeriodicWave::createSine(float sampleRate) |
| 84 { | 146 { |
| 85 PeriodicWave* periodicWave = new PeriodicWave(sampleRate); | 147 PeriodicWave* periodicWave = new PeriodicWave(sampleRate); |
| 86 periodicWave->generateBasicWaveform(OscillatorHandler::SINE); | 148 periodicWave->generateBasicWaveform(OscillatorHandler::SINE); |
| 87 return periodicWave; | 149 return periodicWave; |
| 88 } | 150 } |
| 89 | 151 |
| 90 PeriodicWave* PeriodicWave::createSquare(float sampleRate) | 152 PeriodicWave* PeriodicWave::createSquare(float sampleRate) |
| 91 { | 153 { |
| 92 PeriodicWave* periodicWave = new PeriodicWave(sampleRate); | 154 PeriodicWave* periodicWave = new PeriodicWave(sampleRate); |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 | 406 |
| 345 realP[n] = 0; | 407 realP[n] = 0; |
| 346 imagP[n] = b; | 408 imagP[n] = b; |
| 347 } | 409 } |
| 348 | 410 |
| 349 createBandLimitedTables(realP, imagP, halfSize, false); | 411 createBandLimitedTables(realP, imagP, halfSize, false); |
| 350 } | 412 } |
| 351 | 413 |
| 352 } // namespace blink | 414 } // namespace blink |
| 353 | 415 |
| OLD | NEW |