2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
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
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.
29 // Mac OS X - specific FFTFrame implementation
37 #include "platform/audio/FFTFrame.h"
39 #include "platform/audio/VectorMath.h"
43 const int kMaxFFTPow2Size = 24;
45 FFTSetup* FFTFrame::fftSetups = 0;
47 // Normal constructor: allocates for a given fftSize
48 FFTFrame::FFTFrame(unsigned fftSize)
53 m_log2FFTSize = static_cast<unsigned>(log2(fftSize));
55 // We only allow power of two
56 ASSERT(1UL << m_log2FFTSize == m_FFTSize);
58 // Lazily create and share fftSetup with other frames
59 m_FFTSetup = fftSetupForSize(fftSize);
62 m_frame.realp = m_realData.data();
63 m_frame.imagp = m_imagData.data();
66 // Creates a blank/empty frame (interpolate() must later be called)
71 // Later will be set to correct values when interpolate() is called
80 FFTFrame::FFTFrame(const FFTFrame& frame)
81 : m_FFTSize(frame.m_FFTSize)
82 , m_log2FFTSize(frame.m_log2FFTSize)
83 , m_FFTSetup(frame.m_FFTSetup)
84 , m_realData(frame.m_FFTSize)
85 , m_imagData(frame.m_FFTSize)
88 m_frame.realp = m_realData.data();
89 m_frame.imagp = m_imagData.data();
91 // Copy/setup frame data
92 unsigned nbytes = sizeof(float) * m_FFTSize;
93 memcpy(realData(), frame.m_frame.realp, nbytes);
94 memcpy(imagData(), frame.m_frame.imagp, nbytes);
101 void FFTFrame::multiply(const FFTFrame& frame)
103 FFTFrame& frame1 = *this;
104 const FFTFrame& frame2 = frame;
106 float* realP1 = frame1.realData();
107 float* imagP1 = frame1.imagData();
108 const float* realP2 = frame2.realData();
109 const float* imagP2 = frame2.imagData();
111 unsigned halfSize = m_FFTSize / 2;
112 float real0 = realP1[0];
113 float imag0 = imagP1[0];
116 VectorMath::zvmul(realP1, imagP1, realP2, imagP2, realP1, imagP1, halfSize);
118 // Multiply the packed DC/nyquist component
119 realP1[0] = real0 * realP2[0];
120 imagP1[0] = imag0 * imagP2[0];
122 // Scale accounts for vecLib's peculiar scaling
123 // This ensures the right scaling all the way back to inverse FFT
126 VectorMath::vsmul(realP1, 1, &scale, realP1, 1, halfSize);
127 VectorMath::vsmul(imagP1, 1, &scale, imagP1, 1, halfSize);
130 void FFTFrame::doFFT(const float* data)
132 vDSP_ctoz((DSPComplex*)data, 2, &m_frame, 1, m_FFTSize / 2);
133 vDSP_fft_zrip(m_FFTSetup, &m_frame, 1, m_log2FFTSize, FFT_FORWARD);
136 void FFTFrame::doInverseFFT(float* data)
138 vDSP_fft_zrip(m_FFTSetup, &m_frame, 1, m_log2FFTSize, FFT_INVERSE);
139 vDSP_ztoc(&m_frame, 1, (DSPComplex*)data, 2, m_FFTSize / 2);
141 // Do final scaling so that x == IFFT(FFT(x))
142 float scale = 0.5f / m_FFTSize;
143 vDSP_vsmul(data, 1, &scale, data, 1, m_FFTSize);
146 FFTSetup FFTFrame::fftSetupForSize(unsigned fftSize)
149 fftSetups = (FFTSetup*)malloc(sizeof(FFTSetup) * kMaxFFTPow2Size);
150 memset(fftSetups, 0, sizeof(FFTSetup) * kMaxFFTPow2Size);
153 int pow2size = static_cast<int>(log2(fftSize));
154 ASSERT(pow2size < kMaxFFTPow2Size);
155 if (!fftSetups[pow2size])
156 fftSetups[pow2size] = vDSP_create_fftsetup(pow2size, FFT_RADIX2);
158 return fftSetups[pow2size];
161 void FFTFrame::initialize()
165 void FFTFrame::cleanup()
170 for (int i = 0; i < kMaxFFTPow2Size; ++i) {
172 vDSP_destroy_fftsetup(fftSetups[i]);
179 float* FFTFrame::realData() const
181 return m_frame.realp;
184 float* FFTFrame::imagData() const
186 return m_frame.imagp;
189 } // namespace WebCore
191 #endif // #if OS(MACOSX)
193 #endif // ENABLE(WEB_AUDIO)