4d9cb8e9d84c0457e1ca67df617b66fbd2cab4ee
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webaudio / RealtimeAnalyser.cpp
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  * 1.  Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #if ENABLE(WEB_AUDIO)
28
29 #include "modules/webaudio/RealtimeAnalyser.h"
30
31 #include "platform/audio/AudioBus.h"
32 #include "platform/audio/AudioUtilities.h"
33 #include "platform/audio/FFTFrame.h"
34 #include "platform/audio/VectorMath.h"
35
36 #include <algorithm>
37 #include <limits.h>
38 #include "wtf/Complex.h"
39 #include "wtf/Float32Array.h"
40 #include "wtf/MainThread.h"
41 #include "wtf/MathExtras.h"
42 #include "wtf/Uint8Array.h"
43
44 using namespace std;
45
46 namespace WebCore {
47
48 const double RealtimeAnalyser::DefaultSmoothingTimeConstant  = 0.8;
49 const double RealtimeAnalyser::DefaultMinDecibels = -100;
50 const double RealtimeAnalyser::DefaultMaxDecibels = -30;
51
52 const unsigned RealtimeAnalyser::DefaultFFTSize = 2048;
53 // All FFT implementations are expected to handle power-of-two sizes MinFFTSize <= size <= MaxFFTSize.
54 const unsigned RealtimeAnalyser::MinFFTSize = 32;
55 const unsigned RealtimeAnalyser::MaxFFTSize = 2048;
56 const unsigned RealtimeAnalyser::InputBufferSize = RealtimeAnalyser::MaxFFTSize * 2;
57
58 RealtimeAnalyser::RealtimeAnalyser()
59     : m_inputBuffer(InputBufferSize)
60     , m_writeIndex(0)
61     , m_fftSize(DefaultFFTSize)
62     , m_magnitudeBuffer(DefaultFFTSize / 2)
63     , m_smoothingTimeConstant(DefaultSmoothingTimeConstant)
64     , m_minDecibels(DefaultMinDecibels)
65     , m_maxDecibels(DefaultMaxDecibels)
66 {
67     m_analysisFrame = adoptPtr(new FFTFrame(DefaultFFTSize));
68 }
69
70 bool RealtimeAnalyser::setFftSize(size_t size)
71 {
72     ASSERT(isMainThread());
73
74     // Only allow powers of two.
75     unsigned log2size = static_cast<unsigned>(log2(size));
76     bool isPOT(1UL << log2size == size);
77
78     if (!isPOT || size > MaxFFTSize || size < MinFFTSize)
79         return false;
80
81     if (m_fftSize != size) {
82         m_analysisFrame = adoptPtr(new FFTFrame(size));
83         // m_magnitudeBuffer has size = fftSize / 2 because it contains floats reduced from complex values in m_analysisFrame.
84         m_magnitudeBuffer.allocate(size / 2);
85         m_fftSize = size;
86     }
87
88     return true;
89 }
90
91 void RealtimeAnalyser::writeInput(AudioBus* bus, size_t framesToProcess)
92 {
93     bool isBusGood = bus && bus->numberOfChannels() > 0 && bus->channel(0)->length() >= framesToProcess;
94     ASSERT(isBusGood);
95     if (!isBusGood)
96         return;
97
98     // FIXME : allow to work with non-FFTSize divisible chunking
99     bool isDestinationGood = m_writeIndex < m_inputBuffer.size() && m_writeIndex + framesToProcess <= m_inputBuffer.size();
100     ASSERT(isDestinationGood);
101     if (!isDestinationGood)
102         return;
103
104     // Perform real-time analysis
105     const float* source = bus->channel(0)->data();
106     float* dest = m_inputBuffer.data() + m_writeIndex;
107
108     // The source has already been sanity checked with isBusGood above.
109     memcpy(dest, source, sizeof(float) * framesToProcess);
110
111     // Sum all channels in one if numberOfChannels > 1.
112     unsigned numberOfChannels = bus->numberOfChannels();
113     if (numberOfChannels > 1) {
114         for (unsigned i = 1; i < numberOfChannels; i++) {
115             source = bus->channel(i)->data();
116             VectorMath::vadd(dest, 1, source, 1, dest, 1, framesToProcess);
117         }
118         const float scale =  1.0 / numberOfChannels;
119         VectorMath::vsmul(dest, 1, &scale, dest, 1, framesToProcess);
120     }
121
122     m_writeIndex += framesToProcess;
123     if (m_writeIndex >= InputBufferSize)
124         m_writeIndex = 0;
125 }
126
127 namespace {
128
129 void applyWindow(float* p, size_t n)
130 {
131     ASSERT(isMainThread());
132
133     // Blackman window
134     double alpha = 0.16;
135     double a0 = 0.5 * (1 - alpha);
136     double a1 = 0.5;
137     double a2 = 0.5 * alpha;
138
139     for (unsigned i = 0; i < n; ++i) {
140         double x = static_cast<double>(i) / static_cast<double>(n);
141         double window = a0 - a1 * cos(2 * piDouble * x) + a2 * cos(4 * piDouble * x);
142         p[i] *= float(window);
143     }
144 }
145
146 } // namespace
147
148 void RealtimeAnalyser::doFFTAnalysis()
149 {
150     ASSERT(isMainThread());
151
152     // Unroll the input buffer into a temporary buffer, where we'll apply an analysis window followed by an FFT.
153     size_t fftSize = this->fftSize();
154
155     AudioFloatArray temporaryBuffer(fftSize);
156     float* inputBuffer = m_inputBuffer.data();
157     float* tempP = temporaryBuffer.data();
158
159     // Take the previous fftSize values from the input buffer and copy into the temporary buffer.
160     unsigned writeIndex = m_writeIndex;
161     if (writeIndex < fftSize) {
162         memcpy(tempP, inputBuffer + writeIndex - fftSize + InputBufferSize, sizeof(*tempP) * (fftSize - writeIndex));
163         memcpy(tempP + fftSize - writeIndex, inputBuffer, sizeof(*tempP) * writeIndex);
164     } else
165         memcpy(tempP, inputBuffer + writeIndex - fftSize, sizeof(*tempP) * fftSize);
166
167
168     // Window the input samples.
169     applyWindow(tempP, fftSize);
170
171     // Do the analysis.
172     m_analysisFrame->doFFT(tempP);
173
174     float* realP = m_analysisFrame->realData();
175     float* imagP = m_analysisFrame->imagData();
176
177     // Blow away the packed nyquist component.
178     imagP[0] = 0;
179
180     // Normalize so than an input sine wave at 0dBfs registers as 0dBfs (undo FFT scaling factor).
181     const double magnitudeScale = 1.0 / DefaultFFTSize;
182
183     // A value of 0 does no averaging with the previous result.  Larger values produce slower, but smoother changes.
184     double k = m_smoothingTimeConstant;
185     k = max(0.0, k);
186     k = min(1.0, k);
187
188     // Convert the analysis data from complex to magnitude and average with the previous result.
189     float* destination = magnitudeBuffer().data();
190     size_t n = magnitudeBuffer().size();
191     for (size_t i = 0; i < n; ++i) {
192         Complex c(realP[i], imagP[i]);
193         double scalarMagnitude = abs(c) * magnitudeScale;
194         destination[i] = float(k * destination[i] + (1 - k) * scalarMagnitude);
195     }
196 }
197
198 void RealtimeAnalyser::getFloatFrequencyData(Float32Array* destinationArray)
199 {
200     ASSERT(isMainThread());
201
202     if (!destinationArray)
203         return;
204
205     doFFTAnalysis();
206
207     // Convert from linear magnitude to floating-point decibels.
208     const double minDecibels = m_minDecibels;
209     unsigned sourceLength = magnitudeBuffer().size();
210     size_t len = min(sourceLength, destinationArray->length());
211     if (len > 0) {
212         const float* source = magnitudeBuffer().data();
213         float* destination = destinationArray->data();
214
215         for (unsigned i = 0; i < len; ++i) {
216             float linearValue = source[i];
217             double dbMag = !linearValue ? minDecibels : AudioUtilities::linearToDecibels(linearValue);
218             destination[i] = float(dbMag);
219         }
220     }
221 }
222
223 void RealtimeAnalyser::getByteFrequencyData(Uint8Array* destinationArray)
224 {
225     ASSERT(isMainThread());
226
227     if (!destinationArray)
228         return;
229
230     doFFTAnalysis();
231
232     // Convert from linear magnitude to unsigned-byte decibels.
233     unsigned sourceLength = magnitudeBuffer().size();
234     size_t len = min(sourceLength, destinationArray->length());
235     if (len > 0) {
236         const double rangeScaleFactor = m_maxDecibels == m_minDecibels ? 1 : 1 / (m_maxDecibels - m_minDecibels);
237         const double minDecibels = m_minDecibels;
238
239         const float* source = magnitudeBuffer().data();
240         unsigned char* destination = destinationArray->data();
241
242         for (unsigned i = 0; i < len; ++i) {
243             float linearValue = source[i];
244             double dbMag = !linearValue ? minDecibels : AudioUtilities::linearToDecibels(linearValue);
245
246             // The range m_minDecibels to m_maxDecibels will be scaled to byte values from 0 to UCHAR_MAX.
247             double scaledValue = UCHAR_MAX * (dbMag - minDecibels) * rangeScaleFactor;
248
249             // Clip to valid range.
250             if (scaledValue < 0)
251                 scaledValue = 0;
252             if (scaledValue > UCHAR_MAX)
253                 scaledValue = UCHAR_MAX;
254
255             destination[i] = static_cast<unsigned char>(scaledValue);
256         }
257     }
258 }
259
260 void RealtimeAnalyser::getByteTimeDomainData(Uint8Array* destinationArray)
261 {
262     ASSERT(isMainThread());
263
264     if (!destinationArray)
265         return;
266
267     unsigned fftSize = this->fftSize();
268     size_t len = min(fftSize, destinationArray->length());
269     if (len > 0) {
270         bool isInputBufferGood = m_inputBuffer.size() == InputBufferSize && m_inputBuffer.size() > fftSize;
271         ASSERT(isInputBufferGood);
272         if (!isInputBufferGood)
273             return;
274
275         float* inputBuffer = m_inputBuffer.data();
276         unsigned char* destination = destinationArray->data();
277
278         unsigned writeIndex = m_writeIndex;
279
280         for (unsigned i = 0; i < len; ++i) {
281             // Buffer access is protected due to modulo operation.
282             float value = inputBuffer[(i + writeIndex - fftSize + InputBufferSize) % InputBufferSize];
283
284             // Scale from nominal -1 -> +1 to unsigned byte.
285             double scaledValue = 128 * (value + 1);
286
287             // Clip to valid range.
288             if (scaledValue < 0)
289                 scaledValue = 0;
290             if (scaledValue > UCHAR_MAX)
291                 scaledValue = UCHAR_MAX;
292
293             destination[i] = static_cast<unsigned char>(scaledValue);
294         }
295     }
296 }
297
298 } // namespace WebCore
299
300 #endif // ENABLE(WEB_AUDIO)