Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: Source/modules/webaudio/PeriodicWave.cpp

Issue 18182009: WaveTable name has changed to PeriodicWave (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: fix global constructors test Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/modules/webaudio/PeriodicWave.h ('k') | Source/modules/webaudio/PeriodicWave.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 12 matching lines...) Expand all
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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 "config.h" 29 #include "config.h"
30 30
31 #if ENABLE(WEB_AUDIO) 31 #if ENABLE(WEB_AUDIO)
32 32
33 #include "modules/webaudio/WaveTable.h" 33 #include "modules/webaudio/PeriodicWave.h"
34 34
35 #include "core/platform/audio/FFTFrame.h" 35 #include "core/platform/audio/FFTFrame.h"
36 #include "core/platform/audio/VectorMath.h" 36 #include "core/platform/audio/VectorMath.h"
37 #include "modules/webaudio/OscillatorNode.h" 37 #include "modules/webaudio/OscillatorNode.h"
38 #include "wtf/OwnPtr.h"
38 #include <algorithm> 39 #include <algorithm>
39 #include "wtf/OwnPtr.h"
40 40
41 const unsigned WaveTableSize = 4096; // This must be a power of two. 41 const unsigned PeriodicWaveSize = 4096; // This must be a power of two.
42 const unsigned NumberOfRanges = 36; // There should be 3 * log2(WaveTableSize) 1 /3 octave ranges. 42 const unsigned NumberOfRanges = 36; // There should be 3 * log2(PeriodicWaveSize ) 1/3 octave ranges.
43 const float CentsPerRange = 1200 / 3; // 1/3 Octave. 43 const float CentsPerRange = 1200 / 3; // 1/3 Octave.
44 44
45 namespace WebCore { 45 namespace WebCore {
46 46
47 using namespace VectorMath; 47 using namespace VectorMath;
48 48
49 PassRefPtr<WaveTable> WaveTable::create(float sampleRate, Float32Array* real, Fl oat32Array* imag) 49 PassRefPtr<PeriodicWave> PeriodicWave::create(float sampleRate, Float32Array* re al, Float32Array* imag)
50 { 50 {
51 bool isGood = real && imag && real->length() == imag->length(); 51 bool isGood = real && imag && real->length() == imag->length();
52 ASSERT(isGood); 52 ASSERT(isGood);
53 if (isGood) { 53 if (isGood) {
54 RefPtr<WaveTable> waveTable = adoptRef(new WaveTable(sampleRate)); 54 RefPtr<PeriodicWave> periodicWave = adoptRef(new PeriodicWave(sampleRate ));
55 size_t numberOfComponents = real->length(); 55 size_t numberOfComponents = real->length();
56 waveTable->createBandLimitedTables(real->data(), imag->data(), numberOfC omponents); 56 periodicWave->createBandLimitedTables(real->data(), imag->data(), number OfComponents);
57 return waveTable; 57 return periodicWave;
58 } 58 }
59 return 0; 59 return 0;
60 } 60 }
61 61
62 PassRefPtr<WaveTable> WaveTable::createSine(float sampleRate) 62 PassRefPtr<PeriodicWave> PeriodicWave::createSine(float sampleRate)
63 { 63 {
64 RefPtr<WaveTable> waveTable = adoptRef(new WaveTable(sampleRate)); 64 RefPtr<PeriodicWave> periodicWave = adoptRef(new PeriodicWave(sampleRate));
65 waveTable->generateBasicWaveform(OscillatorNode::SINE); 65 periodicWave->generateBasicWaveform(OscillatorNode::SINE);
66 return waveTable; 66 return periodicWave;
67 } 67 }
68 68
69 PassRefPtr<WaveTable> WaveTable::createSquare(float sampleRate) 69 PassRefPtr<PeriodicWave> PeriodicWave::createSquare(float sampleRate)
70 { 70 {
71 RefPtr<WaveTable> waveTable = adoptRef(new WaveTable(sampleRate)); 71 RefPtr<PeriodicWave> periodicWave = adoptRef(new PeriodicWave(sampleRate));
72 waveTable->generateBasicWaveform(OscillatorNode::SQUARE); 72 periodicWave->generateBasicWaveform(OscillatorNode::SQUARE);
73 return waveTable; 73 return periodicWave;
74 } 74 }
75 75
76 PassRefPtr<WaveTable> WaveTable::createSawtooth(float sampleRate) 76 PassRefPtr<PeriodicWave> PeriodicWave::createSawtooth(float sampleRate)
77 { 77 {
78 RefPtr<WaveTable> waveTable = adoptRef(new WaveTable(sampleRate)); 78 RefPtr<PeriodicWave> periodicWave = adoptRef(new PeriodicWave(sampleRate));
79 waveTable->generateBasicWaveform(OscillatorNode::SAWTOOTH); 79 periodicWave->generateBasicWaveform(OscillatorNode::SAWTOOTH);
80 return waveTable; 80 return periodicWave;
81 } 81 }
82 82
83 PassRefPtr<WaveTable> WaveTable::createTriangle(float sampleRate) 83 PassRefPtr<PeriodicWave> PeriodicWave::createTriangle(float sampleRate)
84 { 84 {
85 RefPtr<WaveTable> waveTable = adoptRef(new WaveTable(sampleRate)); 85 RefPtr<PeriodicWave> periodicWave = adoptRef(new PeriodicWave(sampleRate));
86 waveTable->generateBasicWaveform(OscillatorNode::TRIANGLE); 86 periodicWave->generateBasicWaveform(OscillatorNode::TRIANGLE);
87 return waveTable; 87 return periodicWave;
88 } 88 }
89 89
90 WaveTable::WaveTable(float sampleRate) 90 PeriodicWave::PeriodicWave(float sampleRate)
91 : m_sampleRate(sampleRate) 91 : m_sampleRate(sampleRate)
92 , m_waveTableSize(WaveTableSize) 92 , m_periodicWaveSize(PeriodicWaveSize)
93 , m_numberOfRanges(NumberOfRanges) 93 , m_numberOfRanges(NumberOfRanges)
94 , m_centsPerRange(CentsPerRange) 94 , m_centsPerRange(CentsPerRange)
95 { 95 {
96 ScriptWrappable::init(this); 96 ScriptWrappable::init(this);
97 float nyquist = 0.5 * m_sampleRate; 97 float nyquist = 0.5 * m_sampleRate;
98 m_lowestFundamentalFrequency = nyquist / maxNumberOfPartials(); 98 m_lowestFundamentalFrequency = nyquist / maxNumberOfPartials();
99 m_rateScale = m_waveTableSize / m_sampleRate; 99 m_rateScale = m_periodicWaveSize / m_sampleRate;
100 } 100 }
101 101
102 void WaveTable::waveDataForFundamentalFrequency(float fundamentalFrequency, floa t* &lowerWaveData, float* &higherWaveData, float& tableInterpolationFactor) 102 void PeriodicWave::waveDataForFundamentalFrequency(float fundamentalFrequency, f loat* &lowerWaveData, float* &higherWaveData, float& tableInterpolationFactor)
103 { 103 {
104 // Negative frequencies are allowed, in which case we alias to the positive frequency. 104 // Negative frequencies are allowed, in which case we alias to the positive frequency.
105 fundamentalFrequency = fabsf(fundamentalFrequency); 105 fundamentalFrequency = fabsf(fundamentalFrequency);
106 106
107 // Calculate the pitch range. 107 // Calculate the pitch range.
108 float ratio = fundamentalFrequency > 0 ? fundamentalFrequency / m_lowestFund amentalFrequency : 0.5; 108 float ratio = fundamentalFrequency > 0 ? fundamentalFrequency / m_lowestFund amentalFrequency : 0.5;
109 float centsAboveLowestFrequency = log2f(ratio) * 1200; 109 float centsAboveLowestFrequency = log2f(ratio) * 1200;
110 110
111 // Add one to round-up to the next range just in time to truncate partials b efore aliasing occurs. 111 // Add one to round-up to the next range just in time to truncate partials b efore aliasing occurs.
112 float pitchRange = 1 + centsAboveLowestFrequency / m_centsPerRange; 112 float pitchRange = 1 + centsAboveLowestFrequency / m_centsPerRange;
113 113
114 pitchRange = std::max(pitchRange, 0.0f); 114 pitchRange = std::max(pitchRange, 0.0f);
115 pitchRange = std::min(pitchRange, static_cast<float>(m_numberOfRanges - 1)); 115 pitchRange = std::min(pitchRange, static_cast<float>(m_numberOfRanges - 1));
116 116
117 // The words "lower" and "higher" refer to the table data having the lower a nd higher numbers of partials. 117 // The words "lower" and "higher" refer to the table data having the lower a nd higher numbers of partials.
118 // It's a little confusing since the range index gets larger the more partia ls we cull out. 118 // It's a little confusing since the range index gets larger the more partia ls we cull out.
119 // So the lower table data will have a larger range index. 119 // So the lower table data will have a larger range index.
120 unsigned rangeIndex1 = static_cast<unsigned>(pitchRange); 120 unsigned rangeIndex1 = static_cast<unsigned>(pitchRange);
121 unsigned rangeIndex2 = rangeIndex1 < m_numberOfRanges - 1 ? rangeIndex1 + 1 : rangeIndex1; 121 unsigned rangeIndex2 = rangeIndex1 < m_numberOfRanges - 1 ? rangeIndex1 + 1 : rangeIndex1;
122 122
123 lowerWaveData = m_bandLimitedTables[rangeIndex2]->data(); 123 lowerWaveData = m_bandLimitedTables[rangeIndex2]->data();
124 higherWaveData = m_bandLimitedTables[rangeIndex1]->data(); 124 higherWaveData = m_bandLimitedTables[rangeIndex1]->data();
125 125
126 // Ranges from 0 -> 1 to interpolate between lower -> higher. 126 // Ranges from 0 -> 1 to interpolate between lower -> higher.
127 tableInterpolationFactor = pitchRange - rangeIndex1; 127 tableInterpolationFactor = pitchRange - rangeIndex1;
128 } 128 }
129 129
130 unsigned WaveTable::maxNumberOfPartials() const 130 unsigned PeriodicWave::maxNumberOfPartials() const
131 { 131 {
132 return m_waveTableSize / 2; 132 return m_periodicWaveSize / 2;
133 } 133 }
134 134
135 unsigned WaveTable::numberOfPartialsForRange(unsigned rangeIndex) const 135 unsigned PeriodicWave::numberOfPartialsForRange(unsigned rangeIndex) const
136 { 136 {
137 // Number of cents below nyquist where we cull partials. 137 // Number of cents below nyquist where we cull partials.
138 float centsToCull = rangeIndex * m_centsPerRange; 138 float centsToCull = rangeIndex * m_centsPerRange;
139 139
140 // A value from 0 -> 1 representing what fraction of the partials to keep. 140 // A value from 0 -> 1 representing what fraction of the partials to keep.
141 float cullingScale = pow(2, -centsToCull / 1200); 141 float cullingScale = pow(2, -centsToCull / 1200);
142 142
143 // The very top range will have all the partials culled. 143 // The very top range will have all the partials culled.
144 unsigned numberOfPartials = cullingScale * maxNumberOfPartials(); 144 unsigned numberOfPartials = cullingScale * maxNumberOfPartials();
145 145
146 return numberOfPartials; 146 return numberOfPartials;
147 } 147 }
148 148
149 // Convert into time-domain wave tables. 149 // Convert into time-domain wave buffers.
150 // One table is created for each range for non-aliasing playback at different pl ayback rates. 150 // One table is created for each range for non-aliasing playback at different pl ayback rates.
151 // Thus, higher ranges have more high-frequency partials culled out. 151 // Thus, higher ranges have more high-frequency partials culled out.
152 void WaveTable::createBandLimitedTables(const float* realData, const float* imag Data, unsigned numberOfComponents) 152 void PeriodicWave::createBandLimitedTables(const float* realData, const float* i magData, unsigned numberOfComponents)
153 { 153 {
154 float normalizationScale = 1; 154 float normalizationScale = 1;
155 155
156 unsigned fftSize = m_waveTableSize; 156 unsigned fftSize = m_periodicWaveSize;
157 unsigned halfSize = fftSize / 2; 157 unsigned halfSize = fftSize / 2;
158 unsigned i; 158 unsigned i;
159 159
160 numberOfComponents = std::min(numberOfComponents, halfSize); 160 numberOfComponents = std::min(numberOfComponents, halfSize);
161 161
162 m_bandLimitedTables.reserveCapacity(m_numberOfRanges); 162 m_bandLimitedTables.reserveCapacity(m_numberOfRanges);
163 163
164 for (unsigned rangeIndex = 0; rangeIndex < m_numberOfRanges; ++rangeIndex) { 164 for (unsigned rangeIndex = 0; rangeIndex < m_numberOfRanges; ++rangeIndex) {
165 // This FFTFrame is used to cull partials (represented by frequency bins ). 165 // This FFTFrame is used to cull partials (represented by frequency bins ).
166 FFTFrame frame(fftSize); 166 FFTFrame frame(fftSize);
167 float* realP = frame.realData(); 167 float* realP = frame.realData();
168 float* imagP = frame.imagData(); 168 float* imagP = frame.imagData();
169 169
170 // Copy from loaded frequency data and scale. 170 // Copy from loaded frequency data and scale.
171 float scale = fftSize; 171 float scale = fftSize;
172 vsmul(realData, 1, &scale, realP, 1, numberOfComponents); 172 vsmul(realData, 1, &scale, realP, 1, numberOfComponents);
173 vsmul(imagData, 1, &scale, imagP, 1, numberOfComponents); 173 vsmul(imagData, 1, &scale, imagP, 1, numberOfComponents);
174 174
175 // If fewer components were provided than 1/2 FFT size, then clear the r emaining bins. 175 // If fewer components were provided than 1/2 FFT size, then clear the r emaining bins.
176 for (i = numberOfComponents; i < halfSize; ++i) { 176 for (i = numberOfComponents; i < halfSize; ++i) {
177 realP[i] = 0; 177 realP[i] = 0;
178 imagP[i] = 0; 178 imagP[i] = 0;
179 } 179 }
180 180
181 // Generate complex conjugate because of the way the inverse FFT is defi ned. 181 // Generate complex conjugate because of the way the inverse FFT is defi ned.
182 float minusOne = -1; 182 float minusOne = -1;
183 vsmul(imagP, 1, &minusOne, imagP, 1, halfSize); 183 vsmul(imagP, 1, &minusOne, imagP, 1, halfSize);
184 184
185 // Find the starting bin where we should start culling. 185 // Find the starting bin where we should start culling.
186 // We need to clear out the highest frequencies to band-limit the wavefo rm. 186 // We need to clear out the highest frequencies to band-limit the wavefo rm.
187 unsigned numberOfPartials = numberOfPartialsForRange(rangeIndex); 187 unsigned numberOfPartials = numberOfPartialsForRange(rangeIndex);
188 188
189 // Cull the aliasing partials for this pitch range. 189 // Cull the aliasing partials for this pitch range.
190 for (i = numberOfPartials + 1; i < halfSize; ++i) { 190 for (i = numberOfPartials + 1; i < halfSize; ++i) {
191 realP[i] = 0; 191 realP[i] = 0;
192 imagP[i] = 0; 192 imagP[i] = 0;
193 } 193 }
194 // Clear packed-nyquist if necessary. 194 // Clear packed-nyquist if necessary.
195 if (numberOfPartials < halfSize) 195 if (numberOfPartials < halfSize)
196 imagP[0] = 0; 196 imagP[0] = 0;
197 197
198 // Clear any DC-offset. 198 // Clear any DC-offset.
199 realP[0] = 0; 199 realP[0] = 0;
200 200
201 // Create the band-limited table. 201 // Create the band-limited table.
202 OwnPtr<AudioFloatArray> table = adoptPtr(new AudioFloatArray(m_waveTable Size)); 202 OwnPtr<AudioFloatArray> table = adoptPtr(new AudioFloatArray(m_periodicW aveSize));
203 m_bandLimitedTables.append(table.release()); 203 m_bandLimitedTables.append(table.release());
204 204
205 // Apply an inverse FFT to generate the time-domain table data. 205 // Apply an inverse FFT to generate the time-domain table data.
206 float* data = m_bandLimitedTables[rangeIndex]->data(); 206 float* data = m_bandLimitedTables[rangeIndex]->data();
207 frame.doInverseFFT(data); 207 frame.doInverseFFT(data);
208 208
209 // For the first range (which has the highest power), calculate its peak value then compute normalization scale. 209 // For the first range (which has the highest power), calculate its peak value then compute normalization scale.
210 if (!rangeIndex) { 210 if (!rangeIndex) {
211 float maxValue; 211 float maxValue;
212 vmaxmgv(data, 1, &maxValue, m_waveTableSize); 212 vmaxmgv(data, 1, &maxValue, m_periodicWaveSize);
213 213
214 if (maxValue) 214 if (maxValue)
215 normalizationScale = 1.0f / maxValue; 215 normalizationScale = 1.0f / maxValue;
216 } 216 }
217 217
218 // Apply normalization scale. 218 // Apply normalization scale.
219 vsmul(data, 1, &normalizationScale, data, 1, m_waveTableSize); 219 vsmul(data, 1, &normalizationScale, data, 1, m_periodicWaveSize);
220 } 220 }
221 } 221 }
222 222
223 void WaveTable::generateBasicWaveform(int shape) 223 void PeriodicWave::generateBasicWaveform(int shape)
224 { 224 {
225 unsigned fftSize = waveTableSize(); 225 unsigned fftSize = periodicWaveSize();
226 unsigned halfSize = fftSize / 2; 226 unsigned halfSize = fftSize / 2;
227 227
228 AudioFloatArray real(halfSize); 228 AudioFloatArray real(halfSize);
229 AudioFloatArray imag(halfSize); 229 AudioFloatArray imag(halfSize);
230 float* realP = real.data(); 230 float* realP = real.data();
231 float* imagP = imag.data(); 231 float* imagP = imag.data();
232 232
233 // Clear DC and Nyquist. 233 // Clear DC and Nyquist.
234 realP[0] = 0; 234 realP[0] = 0;
235 imagP[0] = 0; 235 imagP[0] = 0;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 realP[n] = a; 275 realP[n] = a;
276 imagP[n] = b; 276 imagP[n] = b;
277 } 277 }
278 278
279 createBandLimitedTables(realP, imagP, halfSize); 279 createBandLimitedTables(realP, imagP, halfSize);
280 } 280 }
281 281
282 } // namespace WebCore 282 } // namespace WebCore
283 283
284 #endif // ENABLE(WEB_AUDIO) 284 #endif // ENABLE(WEB_AUDIO)
OLDNEW
« no previous file with comments | « Source/modules/webaudio/PeriodicWave.h ('k') | Source/modules/webaudio/PeriodicWave.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698