1 /* Copyright (C) 2013 Google Inc. All rights reserved.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE 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 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
20 * ON 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
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #if OS(ANDROID) && USE(WEBAUDIO_OPENMAX_DL_FFT)
31 #include "platform/audio/FFTFrame.h"
33 #include "platform/audio/AudioArray.h"
34 #include "wtf/MathExtras.h"
35 #include <dl/sp/api/armSP.h>
36 #include <dl/sp/api/omxSP.h>
41 const unsigned kMaxFFTPow2Size = 15;
44 // Normal constructor: allocates for a given fftSize.
45 FFTFrame::FFTFrame(unsigned fftSize)
47 , m_log2FFTSize(static_cast<unsigned>(log2(fftSize)))
50 , m_complexData(fftSize)
51 , m_realData(fftSize / 2)
52 , m_imagData(fftSize / 2)
54 // We only allow power of two.
55 ASSERT(1UL << m_log2FFTSize == m_FFTSize);
57 m_forwardContext = contextForSize(m_log2FFTSize);
58 m_inverseContext = contextForSize(m_log2FFTSize);
61 // Creates a blank/empty frame (interpolate() must later be called).
71 FFTFrame::FFTFrame(const FFTFrame& frame)
72 : m_FFTSize(frame.m_FFTSize)
73 , m_log2FFTSize(frame.m_log2FFTSize)
76 , m_complexData(frame.m_FFTSize)
77 , m_realData(frame.m_FFTSize / 2)
78 , m_imagData(frame.m_FFTSize / 2)
80 m_forwardContext = contextForSize(m_log2FFTSize);
81 m_inverseContext = contextForSize(m_log2FFTSize);
83 // Copy/setup frame data.
84 unsigned nbytes = sizeof(float) * (m_FFTSize / 2);
85 memcpy(realData(), frame.realData(), nbytes);
86 memcpy(imagData(), frame.imagData(), nbytes);
89 void FFTFrame::initialize()
93 void FFTFrame::cleanup()
100 free(m_forwardContext);
101 if (m_inverseContext)
102 free(m_inverseContext);
105 void FFTFrame::doFFT(const float* data)
107 ASSERT(m_forwardContext);
109 if (m_forwardContext) {
110 AudioFloatArray complexFFT(m_FFTSize + 2);
112 omxSP_FFTFwd_RToCCS_F32(data, complexFFT.data(), m_forwardContext);
114 unsigned len = m_FFTSize / 2;
116 // Split FFT data into real and imaginary arrays.
117 const float* c = complexFFT.data();
118 float* real = m_realData.data();
119 float* imag = m_imagData.data();
120 for (unsigned k = 1; k < len; ++k) {
123 imag[k] = c[index + 1];
126 imag[0] = c[m_FFTSize];
130 void FFTFrame::doInverseFFT(float* data)
132 ASSERT(m_inverseContext);
134 if (m_inverseContext) {
135 AudioFloatArray fftDataArray(m_FFTSize + 2);
137 unsigned len = m_FFTSize / 2;
139 // Pack the real and imaginary data into the complex array format
140 float* fftData = fftDataArray.data();
141 const float* real = m_realData.data();
142 const float* imag = m_imagData.data();
143 for (unsigned k = 1; k < len; ++k) {
145 fftData[index] = real[k];
146 fftData[index + 1] = imag[k];
148 fftData[0] = real[0];
150 fftData[m_FFTSize] = imag[0];
151 fftData[m_FFTSize + 1] = 0;
153 omxSP_FFTInv_CCSToR_F32(fftData, data, m_inverseContext);
157 float* FFTFrame::realData() const
159 return const_cast<float*>(m_realData.data());
162 float* FFTFrame::imagData() const
164 return const_cast<float*>(m_imagData.data());
167 OMXFFTSpec_R_F32* FFTFrame::contextForSize(unsigned log2FFTSize)
170 ASSERT(log2FFTSize <= kMaxFFTPow2Size);
172 OMXResult status = omxSP_FFTGetBufSize_R_F32(log2FFTSize, &bufSize);
174 if (status == OMX_Sts_NoErr) {
175 OMXFFTSpec_R_F32* context = static_cast<OMXFFTSpec_R_F32*>(malloc(bufSize));
176 omxSP_FFTInit_R_F32(context, log2FFTSize);
185 #endif // #if OS(ANDROID) && !USE(WEBAUDIO_OPENMAX_DL_FFT)
187 #endif // ENABLE(WEB_AUDIO)