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
35 #if OS(DARWIN) && !USE(WEBAUDIO_FFMPEG)
41 const int kMaxFFTPow2Size = 24;
43 FFTSetup* FFTFrame::fftSetups = 0;
45 // Normal constructor: allocates for a given fftSize
46 FFTFrame::FFTFrame(unsigned fftSize)
51 m_log2FFTSize = static_cast<unsigned>(log2(fftSize));
53 // We only allow power of two
54 ASSERT(1UL << m_log2FFTSize == m_FFTSize);
56 // Lazily create and share fftSetup with other frames
57 m_FFTSetup = fftSetupForSize(fftSize);
60 m_frame.realp = m_realData.data();
61 m_frame.imagp = m_imagData.data();
64 // Creates a blank/empty frame (interpolate() must later be called)
69 // Later will be set to correct values when interpolate() is called
78 FFTFrame::FFTFrame(const FFTFrame& frame)
79 : m_FFTSize(frame.m_FFTSize)
80 , m_log2FFTSize(frame.m_log2FFTSize)
81 , m_FFTSetup(frame.m_FFTSetup)
82 , m_realData(frame.m_FFTSize)
83 , m_imagData(frame.m_FFTSize)
86 m_frame.realp = m_realData.data();
87 m_frame.imagp = m_imagData.data();
89 // Copy/setup frame data
90 unsigned nbytes = sizeof(float) * m_FFTSize;
91 memcpy(realData(), frame.m_frame.realp, nbytes);
92 memcpy(imagData(), frame.m_frame.imagp, nbytes);
99 void FFTFrame::multiply(const FFTFrame& frame)
101 FFTFrame& frame1 = *this;
102 const FFTFrame& frame2 = frame;
104 float* realP1 = frame1.realData();
105 float* imagP1 = frame1.imagData();
106 const float* realP2 = frame2.realData();
107 const float* imagP2 = frame2.imagData();
109 // Scale accounts for vecLib's peculiar scaling
110 // This ensures the right scaling all the way back to inverse FFT
113 // Multiply packed DC/nyquist component
114 realP1[0] *= scale * realP2[0];
115 imagP1[0] *= scale * imagP2[0];
117 // Multiply the rest, skipping packed DC/Nyquist components
118 DSPSplitComplex sc1 = frame1.dspSplitComplex();
122 DSPSplitComplex sc2 = frame2.dspSplitComplex();
126 unsigned halfSize = m_FFTSize / 2;
129 vDSP_zvmul(&sc1, 1, &sc2, 1, &sc1, 1, halfSize - 1, 1 /* normal multiplication */);
131 // We've previously scaled the packed part, now scale the rest.....
132 vDSP_vsmul(sc1.realp, 1, &scale, sc1.realp, 1, halfSize - 1);
133 vDSP_vsmul(sc1.imagp, 1, &scale, sc1.imagp, 1, halfSize - 1);
136 void FFTFrame::doFFT(float* data)
138 vDSP_ctoz((DSPComplex*)data, 2, &m_frame, 1, m_FFTSize / 2);
139 vDSP_fft_zrip(m_FFTSetup, &m_frame, 1, m_log2FFTSize, FFT_FORWARD);
142 void FFTFrame::doInverseFFT(float* data)
144 vDSP_fft_zrip(m_FFTSetup, &m_frame, 1, m_log2FFTSize, FFT_INVERSE);
145 vDSP_ztoc(&m_frame, 1, (DSPComplex*)data, 2, m_FFTSize / 2);
147 // Do final scaling so that x == IFFT(FFT(x))
148 float scale = 0.5f / m_FFTSize;
149 vDSP_vsmul(data, 1, &scale, data, 1, m_FFTSize);
152 FFTSetup FFTFrame::fftSetupForSize(unsigned fftSize)
155 fftSetups = (FFTSetup*)malloc(sizeof(FFTSetup) * kMaxFFTPow2Size);
156 memset(fftSetups, 0, sizeof(FFTSetup) * kMaxFFTPow2Size);
159 int pow2size = static_cast<int>(log2(fftSize));
160 ASSERT(pow2size < kMaxFFTPow2Size);
161 if (!fftSetups[pow2size])
162 fftSetups[pow2size] = vDSP_create_fftsetup(pow2size, FFT_RADIX2);
164 return fftSetups[pow2size];
167 void FFTFrame::initialize()
171 void FFTFrame::cleanup()
176 for (int i = 0; i < kMaxFFTPow2Size; ++i) {
178 vDSP_destroy_fftsetup(fftSetups[i]);
185 float* FFTFrame::realData() const
187 return m_frame.realp;
190 float* FFTFrame::imagData() const
192 return m_frame.imagp;
195 } // namespace WebCore
197 #endif // #if OS(DARWIN) && !USE(WEBAUDIO_FFMPEG)
199 #endif // ENABLE(WEB_AUDIO)