Jamoma API  0.6.0.a19
CASpectralProcessor.h
1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #ifndef _SpectralProcesor_H_
42 #define _SpectralProcesor_H_
43 
44 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
45 #include <CoreAudio/CoreAudioTypes.h>
46 #include <CoreFoundation/CoreFoundation.h>
47 #else
48 #include <CoreAudioTypes.h>
49 #include <CoreFoundation.h>
50 #endif
51 
52 #include <Accelerate/Accelerate.h>
53 
54 #include "CAAutoDisposer.h"
55 
56 struct SpectralBufferList
57 {
58  UInt32 mNumberSpectra;
59  DSPSplitComplex mDSPSplitComplex[1];
60 };
61 
62 class CASpectralProcessor
63 {
64 public:
65  CASpectralProcessor(UInt32 inFFTSize, UInt32 inHopSize, UInt32 inNumChannels, UInt32 inMaxFrames);
66  virtual ~CASpectralProcessor();
67 
68  void Reset();
69 
70  void Process(UInt32 inNumFrames, AudioBufferList* inInput, AudioBufferList* outOutput);
71 
72  typedef void (*SpectralFunction)(SpectralBufferList* inSpectra, void* inUserData);
73 
74  void SetSpectralFunction(SpectralFunction inFunction, void* inUserData);
75 
76  UInt32 FFTSize() const { return mFFTSize; }
77  UInt32 MaxFrames() const { return mMaxFrames; }
78  UInt32 NumChannels() const { return mNumChannels; }
79  UInt32 HopSize() const { return mHopSize; }
80  Float32* Window() const { return mWindow; }
81 
82 
83  void HanningWindow(); // set up a hanning window
84  void SineWindow();
85 
86  void GetFrequencies(Float32* freqs, Float32 sampleRate); // only for processed forward
87  void GetMagnitude(AudioBufferList* inCopy, Float32* min, Float32* max); // only for processed forward
88 
89  virtual bool ProcessForwards(UInt32 inNumFrames, AudioBufferList* inInput);
90  bool ProcessBackwards(UInt32 inNumFrames, AudioBufferList* outOutput);
91 
92 
93  void PrintSpectralBufferList();
94 
95 protected:
96  void CopyInput(UInt32 inNumFrames, AudioBufferList* inInput);
97  void CopyInputToFFT();
98  void DoWindowing();
99  void DoFwdFFT();
100  void DoInvFFT();
101  void OverlapAddOutput();
102  void CopyOutput(UInt32 inNumFrames, AudioBufferList* inOutput);
103  void ProcessSpectrum(UInt32 inFFTSize, SpectralBufferList* inSpectra);
104 
105  UInt32 mFFTSize;
106  UInt32 mHopSize;
107  UInt32 mNumChannels;
108  UInt32 mMaxFrames;
109 
110  UInt32 mLog2FFTSize;
111  UInt32 mFFTMask;
112  UInt32 mFFTByteSize;
113  UInt32 mIOBufSize;
114  UInt32 mIOMask;
115  UInt32 mInputSize;
116  UInt32 mInputPos;
117  UInt32 mOutputPos;
118  UInt32 mInFFTPos;
119  UInt32 mOutFFTPos;
120  FFTSetup mFFTSetup;
121 
122  CAAutoFree<Float32> mWindow;
123  struct SpectralChannel
124  {
125  CAAutoFree<Float32> mInputBuf; // log2ceil(FFT size + max frames)
126  CAAutoFree<Float32> mOutputBuf; // log2ceil(FFT size + max frames)
127  CAAutoFree<Float32> mFFTBuf; // FFT size
128  CAAutoFree<Float32> mSplitFFTBuf; // FFT size
129  };
130  CAAutoArrayDelete<SpectralChannel> mChannels;
131 
132  CAAutoFree<SpectralBufferList> mSpectralBufferList;
133 
134  SpectralFunction mSpectralFunction;
135  void *mUserData;
136 
137 };
138 
139 
140 #endif
This implements a window function as described @ http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/...