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 "RealtimeAnalyser.h"
30
31#include "AudioBus.h"
32#include "AudioUtilities.h"
33#include "FFTFrame.h"
34
35#if ENABLE(WEBGL)
36#include "Float32Array.h"
37#include "Uint8Array.h"
38#endif
39
40#include <algorithm>
41#include <limits.h>
42#include <wtf/Complex.h>
43#include <wtf/MathExtras.h>
44#include <wtf/Threading.h>
45
46using namespace std;
47
48namespace WebCore {
49
50const double RealtimeAnalyser::DefaultSmoothingTimeConstant  = 0.8;
51const double RealtimeAnalyser::DefaultMinDecibels = -100.0;
52const double RealtimeAnalyser::DefaultMaxDecibels = -30.0;
53
54const unsigned RealtimeAnalyser::DefaultFFTSize = 2048;
55const unsigned RealtimeAnalyser::MaxFFTSize = 2048;
56const unsigned RealtimeAnalyser::InputBufferSize = RealtimeAnalyser::MaxFFTSize * 2;
57
58RealtimeAnalyser::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
70RealtimeAnalyser::~RealtimeAnalyser()
71{
72}
73
74void RealtimeAnalyser::reset()
75{
76    m_writeIndex = 0;
77    m_inputBuffer.zero();
78    m_magnitudeBuffer.zero();
79}
80
81void RealtimeAnalyser::setFftSize(size_t size)
82{
83    ASSERT(isMainThread());
84
85    // Only allow powers of two.
86    unsigned log2size = static_cast<unsigned>(log2(size));
87    bool isPOT(1UL << log2size == size);
88
89    if (!isPOT || size > MaxFFTSize) {
90        // FIXME: It would be good to also set an exception.
91        return;
92    }
93
94    if (m_fftSize != size) {
95        m_analysisFrame = adoptPtr(new FFTFrame(m_fftSize));
96        m_magnitudeBuffer.resize(size);
97        m_fftSize = size;
98    }
99}
100
101void RealtimeAnalyser::writeInput(AudioBus* bus, size_t framesToProcess)
102{
103    bool isBusGood = bus && bus->numberOfChannels() > 0 && bus->channel(0)->length() >= framesToProcess;
104    ASSERT(isBusGood);
105    if (!isBusGood)
106        return;
107
108    // FIXME : allow to work with non-FFTSize divisible chunking
109    bool isDestinationGood = m_writeIndex < m_inputBuffer.size() && m_writeIndex + framesToProcess <= m_inputBuffer.size();
110    ASSERT(isDestinationGood);
111    if (!isDestinationGood)
112        return;
113
114    // Perform real-time analysis
115    // FIXME : for now just use left channel (must mix if stereo source)
116    float* source = bus->channel(0)->data();
117
118    // The source has already been sanity checked with isBusGood above.
119
120    memcpy(m_inputBuffer.data() + m_writeIndex, source, sizeof(float) * framesToProcess);
121
122    m_writeIndex += framesToProcess;
123    if (m_writeIndex >= InputBufferSize)
124        m_writeIndex = 0;
125}
126
127namespace {
128
129void 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.0 - 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.0 * piDouble * x) + a2 * cos(4.0 * piDouble * x);
142        p[i] *= float(window);
143    }
144}
145
146} // namespace
147
148void 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    // FIXME : optimize with memcpy().
161    unsigned writeIndex = m_writeIndex;
162    for (unsigned i = 0; i < fftSize; ++i)
163        tempP[i] = inputBuffer[(i + writeIndex - fftSize + InputBufferSize) % InputBufferSize];
164
165    // Window the input samples.
166    applyWindow(tempP, fftSize);
167
168    // Do the analysis.
169    m_analysisFrame->doFFT(tempP);
170
171    size_t n = DefaultFFTSize / 2;
172
173    float* realP = m_analysisFrame->realData();
174    float* imagP = m_analysisFrame->imagData();
175
176    // Blow away the packed nyquist component.
177    imagP[0] = 0.0f;
178
179    // Normalize so than an input sine wave at 0dBfs registers as 0dBfs (undo FFT scaling factor).
180    const double MagnitudeScale = 1.0 / DefaultFFTSize;
181
182    // A value of 0 does no averaging with the previous result.  Larger values produce slower, but smoother changes.
183    double k = m_smoothingTimeConstant;
184    k = max(0.0, k);
185    k = min(1.0, k);
186
187    // Convert the analysis data from complex to magnitude and average with the previous result.
188    float* destination = magnitudeBuffer().data();
189    for (unsigned i = 0; i < n; ++i) {
190        Complex c(realP[i], imagP[i]);
191        double scalarMagnitude = abs(c) * MagnitudeScale;
192        destination[i] = float(k * destination[i] + (1.0 - k) * scalarMagnitude);
193    }
194}
195
196#if ENABLE(WEBGL)
197
198void 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
223void 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.0 : 1.0 / (m_maxDecibels - m_minDecibels);
237
238        const float* source = magnitudeBuffer().data();
239        unsigned char* destination = destinationArray->data();
240
241        for (unsigned i = 0; i < len; ++i) {
242            float linearValue = source[i];
243            double dbMag = !linearValue ? m_minDecibels : AudioUtilities::linearToDecibels(linearValue);
244
245            // The range m_minDecibels to m_maxDecibels will be scaled to byte values from 0 to UCHAR_MAX.
246            double scaledValue = UCHAR_MAX * (dbMag - m_minDecibels) * RangeScaleFactor;
247
248            // Clip to valid range.
249            if (scaledValue < 0.0)
250                scaledValue = 0.0;
251            if (scaledValue > UCHAR_MAX)
252                scaledValue = UCHAR_MAX;
253
254            destination[i] = static_cast<unsigned char>(scaledValue);
255        }
256    }
257}
258
259void RealtimeAnalyser::getByteTimeDomainData(Uint8Array* destinationArray)
260{
261    ASSERT(isMainThread());
262
263    if (!destinationArray)
264        return;
265
266    unsigned fftSize = this->fftSize();
267    size_t len = min(fftSize, destinationArray->length());
268    if (len > 0) {
269        bool isInputBufferGood = m_inputBuffer.size() == InputBufferSize && m_inputBuffer.size() > fftSize;
270        ASSERT(isInputBufferGood);
271        if (!isInputBufferGood)
272            return;
273
274        float* inputBuffer = m_inputBuffer.data();
275        unsigned char* destination = destinationArray->data();
276
277        unsigned writeIndex = m_writeIndex;
278
279        for (unsigned i = 0; i < len; ++i) {
280            // Buffer access is protected due to modulo operation.
281            float value = inputBuffer[(i + writeIndex - fftSize + InputBufferSize) % InputBufferSize];
282
283            // Scale from nominal -1.0 -> +1.0 to unsigned byte.
284            double scaledValue = 128.0 * (value + 1.0);
285
286            // Clip to valid range.
287            if (scaledValue < 0.0)
288                scaledValue = 0.0;
289            if (scaledValue > UCHAR_MAX)
290                scaledValue = UCHAR_MAX;
291
292            destination[i] = static_cast<unsigned char>(scaledValue);
293        }
294    }
295}
296
297#endif // WEBGL
298
299} // namespace WebCore
300
301#endif // ENABLE(WEB_AUDIO)
302