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.
32 #include "AudioArray.h"
34 #if OS(DARWIN) && !USE(WEBAUDIO_FFMPEG)
35 #define USE_ACCELERATE_FFT 1
37 #define USE_ACCELERATE_FFT 0
40 #if USE_ACCELERATE_FFT
41 #include <Accelerate/Accelerate.h>
44 #if !USE_ACCELERATE_FFT
48 #endif // USE(WEBAUDIO_MKL)
50 #if USE(WEBAUDIO_GSTREAMER)
53 #include <gst/fft/gstfftf32.h>
55 #endif // USE(WEBAUDIO_GSTREAMER)
57 #if USE(WEBAUDIO_FFMPEG)
59 #endif // USE(WEBAUDIO_FFMPEG)
61 #endif // !USE_ACCELERATE_FFT
63 #include <wtf/PassOwnPtr.h>
64 #include <wtf/Platform.h>
65 #include <wtf/Threading.h>
69 // Defines the interface for an "FFT frame", an object which is able to perform a forward
70 // and reverse FFT, internally storing the resultant frequency-domain data.
74 // The constructors, destructor, and methods up to the CROSS-PLATFORM section have platform-dependent implementations.
76 FFTFrame(unsigned fftSize);
77 FFTFrame(); // creates a blank/empty frame for later use with createInterpolatedFrame()
78 FFTFrame(const FFTFrame& frame);
81 static void initialize();
82 static void cleanup();
83 void doFFT(const float* data);
84 void doInverseFFT(float* data);
85 void multiply(const FFTFrame& frame); // multiplies ourself with frame : effectively operator*=()
87 float* realData() const;
88 float* imagData() const;
90 void print(); // for debugging
93 // The remaining public methods have cross-platform implementations:
95 // Interpolates from frame1 -> frame2 as x goes from 0.0 -> 1.0
96 static PassOwnPtr<FFTFrame> createInterpolatedFrame(const FFTFrame& frame1, const FFTFrame& frame2, double x);
98 void doPaddedFFT(const float* data, size_t dataSize); // zero-padding with dataSize <= fftSize
99 double extractAverageGroupDelay();
100 void addConstantGroupDelay(double sampleFrameDelay);
102 unsigned fftSize() const { return m_FFTSize; }
103 unsigned log2FFTSize() const { return m_log2FFTSize; }
107 unsigned m_log2FFTSize;
109 void interpolateFrequencyComponents(const FFTFrame& frame1, const FFTFrame& frame2, double x);
111 #if USE_ACCELERATE_FFT
112 DSPSplitComplex& dspSplitComplex() { return m_frame; }
113 DSPSplitComplex dspSplitComplex() const { return m_frame; }
115 static FFTSetup fftSetupForSize(unsigned fftSize);
117 static FFTSetup* fftSetups;
121 DSPSplitComplex m_frame;
122 AudioFloatArray m_realData;
123 AudioFloatArray m_imagData;
124 #else // !USE_ACCELERATE_FFT
126 #if USE(WEBAUDIO_MKL)
127 // Interleaves the planar real and imaginary data and returns a
128 // pointer to the resulting storage which can be used for in-place
129 // or out-of-place operations. FIXME: ideally all of the MKL
130 // routines would operate on planar data and this method would be
132 float* getUpToDateComplexData();
134 static DFTI_DESCRIPTOR_HANDLE descriptorHandleForSize(unsigned fftSize);
136 static DFTI_DESCRIPTOR_HANDLE* descriptorHandles;
138 DFTI_DESCRIPTOR_HANDLE m_handle;
139 AudioFloatArray m_complexData;
140 AudioFloatArray m_realData;
141 AudioFloatArray m_imagData;
142 #endif // USE(WEBAUDIO_MKL)
144 #if USE(WEBAUDIO_FFMPEG)
145 static RDFTContext* contextForSize(unsigned fftSize, int trans);
147 RDFTContext* m_forwardContext;
148 RDFTContext* m_inverseContext;
150 float* getUpToDateComplexData();
151 AudioFloatArray m_complexData;
152 AudioFloatArray m_realData;
153 AudioFloatArray m_imagData;
154 #endif // USE(WEBAUDIO_FFMPEG)
156 #if USE(WEBAUDIO_GSTREAMER)
158 GstFFTF32* m_inverseFft;
159 GstFFTF32Complex* m_complexData;
160 AudioFloatArray m_realData;
161 AudioFloatArray m_imagData;
162 #endif // USE(WEBAUDIO_GSTREAMER)
164 #if USE(WEBAUDIO_FFTW)
165 fftwf_plan m_forwardPlan;
166 fftwf_plan m_backwardPlan;
173 // Both the real and imaginary data are stored here.
174 // The real data is stored first, followed by three float values of padding.
175 // The imaginary data is stored after the padding and is 16-byte aligned (if m_data itself is aligned).
176 // The reason we don't use separate arrays for real and imaginary is because the FFTW plans are shared
177 // between FFTFrame instances and require that the real and imaginary data pointers be the same distance apart.
178 AudioFloatArray m_data;
180 static Mutex *s_planLock;
181 static fftwf_plan* fftwForwardPlans;
182 static fftwf_plan* fftwBackwardPlans;
184 static fftwf_plan fftwPlanForSize(unsigned fftSize, Direction,
185 float*, float*, float*);
186 #endif // USE(WEBAUDIO_FFTW)
188 #endif // !USE_ACCELERATE_FFT
191 } // namespace WebCore