summaryrefslogtreecommitdiff
path: root/distrho/src/CoreAudio106/PublicUtility/CASpectralProcessor.cpp
blob: 1c49e7aa6ab84447136d0c6236b65240c710d205 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*	Copyright © 2007 Apple Inc. All Rights Reserved.
	
	Disclaimer: IMPORTANT:  This Apple software is supplied to you by 
			Apple Inc. ("Apple") in consideration of your agreement to the
			following terms, and your use, installation, modification or
			redistribution of this Apple software constitutes acceptance of these
			terms.  If you do not agree with these terms, please do not use,
			install, modify or redistribute this Apple software.
			
			In consideration of your agreement to abide by the following terms, and
			subject to these terms, Apple grants you a personal, non-exclusive
			license, under Apple's copyrights in this original Apple software (the
			"Apple Software"), to use, reproduce, modify and redistribute the Apple
			Software, with or without modifications, in source and/or binary forms;
			provided that if you redistribute the Apple Software in its entirety and
			without modifications, you must retain this notice and the following
			text and disclaimers in all such redistributions of the Apple Software. 
			Neither the name, trademarks, service marks or logos of Apple Inc. 
			may be used to endorse or promote products derived from the Apple
			Software without specific prior written permission from Apple.  Except
			as expressly stated in this notice, no other rights or licenses, express
			or implied, are granted by Apple herein, including but not limited to
			any patent rights that may be infringed by your derivative works or by
			other works in which the Apple Software may be incorporated.
			
			The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
			MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
			THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
			FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
			OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
			
			IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
			OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
			SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
			INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
			MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
			AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
			STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
			POSSIBILITY OF SUCH DAMAGE.
*/
 
//#include "AudioFormulas.h"
#include "CASpectralProcessor.h"
#include "CABitOperations.h"


#include <vecLib/vectorOps.h>


#define OFFSETOF(class, field)((size_t)&((class*)0)->field)

CASpectralProcessor::CASpectralProcessor(UInt32 inFFTSize, UInt32 inHopSize, UInt32 inNumChannels, UInt32 inMaxFrames)
	: mFFTSize(inFFTSize), mHopSize(inHopSize), mNumChannels(inNumChannels), mMaxFrames(inMaxFrames),
	mLog2FFTSize(Log2Ceil(mFFTSize)), 
	mFFTMask(mFFTSize - 1),
	mFFTByteSize(mFFTSize * sizeof(Float32)),
	mIOBufSize(NextPowerOfTwo(mFFTSize + mMaxFrames)),
	mIOMask(mIOBufSize - 1),
	mInputSize(0),
	mInputPos(0), mOutputPos(-mFFTSize & mIOMask), 
	mInFFTPos(0), mOutFFTPos(0),
	mSpectralFunction(0), mUserData(0)
{
	mWindow.alloc(mFFTSize, false);
	SineWindow(); // set default window.
	
	mChannels.alloc(mNumChannels);
	mSpectralBufferList.allocBytes(OFFSETOF(SpectralBufferList, mDSPSplitComplex[mNumChannels]), true);
	mSpectralBufferList->mNumberSpectra = mNumChannels;
	for (UInt32 i = 0; i < mNumChannels; ++i) 
	{
		mChannels[i].mInputBuf.alloc(mIOBufSize, true);
		mChannels[i].mOutputBuf.alloc(mIOBufSize, true);
		mChannels[i].mFFTBuf.alloc(mFFTSize, true);
		mChannels[i].mSplitFFTBuf.alloc(mFFTSize, true);
		mSpectralBufferList->mDSPSplitComplex[i].realp = mChannels[i].mSplitFFTBuf();
		mSpectralBufferList->mDSPSplitComplex[i].imagp = mChannels[i].mSplitFFTBuf() + (mFFTSize >> 1);
	}

	mFFTSetup = vDSP_create_fftsetup (mLog2FFTSize, FFT_RADIX2);
	
}

CASpectralProcessor::~CASpectralProcessor()
{
	mWindow.free();
	mChannels.free();
	mSpectralBufferList.free();
	vDSP_destroy_fftsetup(mFFTSetup);
}

void CASpectralProcessor::Reset()
{
	mInputPos = 0;
	mOutputPos = -mFFTSize & mIOMask;
	mInFFTPos = 0;
	mOutFFTPos = 0;
	
	for (UInt32 i = 0; i < mNumChannels; ++i) 
	{
		memset(mChannels[i].mInputBuf(), 0, mIOBufSize * sizeof(Float32));
		memset(mChannels[i].mOutputBuf(), 0, mIOBufSize * sizeof(Float32));
		memset(mChannels[i].mFFTBuf(), 0, mFFTSize * sizeof(Float32));
	}
}

const double two_pi = 2. * M_PI;

void CASpectralProcessor::HanningWindow()
{ 
	// this is also vector optimized

	double w = two_pi / (double)(mFFTSize - 1);
	for (UInt32 i = 0; i < mFFTSize; ++i)
	{
		mWindow[i] = (0.5 - 0.5 * cos(w * (double)i));	
	}
}

void CASpectralProcessor::SineWindow()
{
	double w = M_PI / (double)(mFFTSize - 1);
	for (UInt32 i = 0; i < mFFTSize; ++i)
	{
		mWindow[i] = sin(w * (double)i);
	}
}

void CASpectralProcessor::Process(UInt32 inNumFrames, AudioBufferList* inInput, AudioBufferList* outOutput)
{
	// copy from buffer list to input buffer
	CopyInput(inNumFrames, inInput);
	
	// if enough input to process, then process.
	while (mInputSize >= mFFTSize) 
	{
		CopyInputToFFT(); // copy from input buffer to fft buffer
		DoWindowing();
		DoFwdFFT();
		ProcessSpectrum(mFFTSize, mSpectralBufferList());
		DoInvFFT();
		DoWindowing();
		OverlapAddOutput();
	}

	// copy from output buffer to buffer list
	CopyOutput(inNumFrames, outOutput);
}

void CASpectralProcessor::DoWindowing()
{
	Float32 *win = mWindow();
	if (!win) return;
	for (UInt32 i=0; i<mNumChannels; ++i) {
		Float32 *x = mChannels[i].mFFTBuf();
		vDSP_vmul(x, 1, win, 1, x, 1, mFFTSize);
	}
	//printf("DoWindowing %g %g\n", mChannels[0].mFFTBuf()[0], mChannels[0].mFFTBuf()[200]);
}



void CASpectralProcessor::CopyInput(UInt32 inNumFrames, AudioBufferList* inInput)
{
	UInt32 numBytes = inNumFrames * sizeof(Float32);
	UInt32 firstPart = mIOBufSize - mInputPos;
	

	if (firstPart < inNumFrames) {
		UInt32 firstPartBytes = firstPart * sizeof(Float32);
		UInt32 secondPartBytes = numBytes - firstPartBytes;
		for (UInt32 i=0; i<mNumChannels; ++i) {		
			memcpy(mChannels[i].mInputBuf + mInputPos, inInput->mBuffers[i].mData, firstPartBytes);
			memcpy(mChannels[i].mInputBuf, (UInt8*)inInput->mBuffers[i].mData + firstPartBytes, secondPartBytes);
		}
	} else {
		UInt32 numBytes = inNumFrames * sizeof(Float32);
		for (UInt32 i=0; i<mNumChannels; ++i) {		
			memcpy(mChannels[i].mInputBuf + mInputPos, inInput->mBuffers[i].mData, numBytes);
		}
	}
	//printf("CopyInput %g %g\n", mChannels[0].mInputBuf[mInputPos], mChannels[0].mInputBuf[(mInputPos + 200) & mIOMask]);
	//printf("CopyInput mInputPos %u   mIOBufSize %u\n", (unsigned)mInputPos, (unsigned)mIOBufSize);
	mInputSize += inNumFrames;
	mInputPos = (mInputPos + inNumFrames) & mIOMask;
}

void CASpectralProcessor::CopyOutput(UInt32 inNumFrames, AudioBufferList* outOutput)
{
	//printf("->CopyOutput %g %g\n", mChannels[0].mOutputBuf[mOutputPos], mChannels[0].mOutputBuf[(mOutputPos + 200) & mIOMask]);
	//printf("CopyOutput mOutputPos %u\n", (unsigned)mOutputPos);
	UInt32 numBytes = inNumFrames * sizeof(Float32);
	UInt32 firstPart = mIOBufSize - mOutputPos;
	if (firstPart < inNumFrames) {
		UInt32 firstPartBytes = firstPart * sizeof(Float32);
		UInt32 secondPartBytes = numBytes - firstPartBytes;
		for (UInt32 i=0; i<mNumChannels; ++i) {
			memcpy(outOutput->mBuffers[i].mData, mChannels[i].mOutputBuf + mOutputPos, firstPartBytes);
			memcpy((UInt8*)outOutput->mBuffers[i].mData + firstPartBytes, mChannels[i].mOutputBuf, secondPartBytes);
			memset(mChannels[i].mOutputBuf + mOutputPos, 0, firstPartBytes);
			memset(mChannels[i].mOutputBuf, 0, secondPartBytes);
		}
	} else {
		for (UInt32 i=0; i<mNumChannels; ++i) {
			memcpy(outOutput->mBuffers[i].mData, mChannels[i].mOutputBuf + mOutputPos, numBytes);
			memset(mChannels[i].mOutputBuf + mOutputPos, 0, numBytes);
		}
	}
	//printf("<-CopyOutput %g %g\n", ((Float32*)outOutput->mBuffers[0].mData)[0], ((Float32*)outOutput->mBuffers[0].mData)[200]);
	mOutputPos = (mOutputPos + inNumFrames) & mIOMask;
}

void CASpectralProcessor::PrintSpectralBufferList()
{
	UInt32 half = mFFTSize >> 1;
	for (UInt32 i=0; i<mNumChannels; ++i) {
		DSPSplitComplex	&freqData = mSpectralBufferList->mDSPSplitComplex[i];
	
		for (UInt32 j=0; j<half; j++){
			printf(" bin[%d]: %lf + %lfi\n", (int) j, freqData.realp[j], freqData.imagp[j]);
		}
	}
}


void CASpectralProcessor::CopyInputToFFT()
{
	//printf("CopyInputToFFT mInFFTPos %u\n", (unsigned)mInFFTPos);
	UInt32 firstPart = mIOBufSize - mInFFTPos;
	UInt32 firstPartBytes = firstPart * sizeof(Float32);
	if (firstPartBytes < mFFTByteSize) {
		UInt32 secondPartBytes = mFFTByteSize - firstPartBytes;
		for (UInt32 i=0; i<mNumChannels; ++i) {
			memcpy(mChannels[i].mFFTBuf(), mChannels[i].mInputBuf() + mInFFTPos, firstPartBytes);
			memcpy((UInt8*)mChannels[i].mFFTBuf() + firstPartBytes, mChannels[i].mInputBuf(), secondPartBytes);
		}
	} else {
		for (UInt32 i=0; i<mNumChannels; ++i) {
			memcpy(mChannels[i].mFFTBuf(), mChannels[i].mInputBuf() + mInFFTPos, mFFTByteSize);
		}
	}
	mInputSize -= mHopSize;
	mInFFTPos = (mInFFTPos + mHopSize) & mIOMask;
	//printf("CopyInputToFFT %g %g\n", mChannels[0].mFFTBuf()[0], mChannels[0].mFFTBuf()[200]);
}

void CASpectralProcessor::OverlapAddOutput()
{
	//printf("OverlapAddOutput mOutFFTPos %u\n", (unsigned)mOutFFTPos);
	UInt32 firstPart = mIOBufSize - mOutFFTPos;
	if (firstPart < mFFTSize) {
		UInt32 secondPart = mFFTSize - firstPart;
		for (UInt32 i=0; i<mNumChannels; ++i) {
			float* out1 = mChannels[i].mOutputBuf() + mOutFFTPos;
			vDSP_vadd(out1, 1, mChannels[i].mFFTBuf(), 1, out1, 1, firstPart);
			float* out2 = mChannels[i].mOutputBuf();
			vDSP_vadd(out2, 1, mChannels[i].mFFTBuf() + firstPart, 1, out2, 1, secondPart);
		}
	} else {
		for (UInt32 i=0; i<mNumChannels; ++i) {
			float* out1 = mChannels[i].mOutputBuf() + mOutFFTPos;
			vDSP_vadd(out1, 1, mChannels[i].mFFTBuf(), 1, out1, 1, mFFTSize);
		}
	}
	//printf("OverlapAddOutput %g %g\n", mChannels[0].mOutputBuf[mOutFFTPos], mChannels[0].mOutputBuf[(mOutFFTPos + 200) & mIOMask]);
	mOutFFTPos = (mOutFFTPos + mHopSize) & mIOMask;
}


void CASpectralProcessor::DoFwdFFT()
{
	//printf("->DoFwdFFT %g %g\n", mChannels[0].mFFTBuf()[0], mChannels[0].mFFTBuf()[200]);
	UInt32 half = mFFTSize >> 1;
	for (UInt32 i=0; i<mNumChannels; ++i) 
	{
		vDSP_ctoz((DSPComplex*)mChannels[i].mFFTBuf(), 2, &mSpectralBufferList->mDSPSplitComplex[i], 1, half);
		vDSP_fft_zrip(mFFTSetup, &mSpectralBufferList->mDSPSplitComplex[i], 1, mLog2FFTSize, FFT_FORWARD);
	}
	//printf("<-DoFwdFFT %g %g\n", direction, mChannels[0].mFFTBuf()[0], mChannels[0].mFFTBuf()[200]);
}

void CASpectralProcessor::DoInvFFT()
{
	//printf("->DoInvFFT %g %g\n", mChannels[0].mFFTBuf()[0], mChannels[0].mFFTBuf()[200]);
	UInt32 half = mFFTSize >> 1;
	for (UInt32 i=0; i<mNumChannels; ++i) 
	{
		vDSP_fft_zrip(mFFTSetup, &mSpectralBufferList->mDSPSplitComplex[i], 1, mLog2FFTSize, FFT_INVERSE);
		vDSP_ztoc(&mSpectralBufferList->mDSPSplitComplex[i], 1, (DSPComplex*)mChannels[i].mFFTBuf(), 2, half);		
		float scale = 0.5 / mFFTSize;
		vDSP_vsmul(mChannels[i].mFFTBuf(), 1, &scale, mChannels[i].mFFTBuf(), 1, mFFTSize );
	}
	//printf("<-DoInvFFT %g %g\n", direction, mChannels[0].mFFTBuf()[0], mChannels[0].mFFTBuf()[200]);
}

void CASpectralProcessor::SetSpectralFunction(SpectralFunction inFunction, void* inUserData)
{
	mSpectralFunction = inFunction; 
	mUserData = inUserData;
}

void CASpectralProcessor::ProcessSpectrum(UInt32 inFFTSize, SpectralBufferList* inSpectra)
{
	if (mSpectralFunction)
		(mSpectralFunction)(inSpectra, mUserData);
}

#pragma mark ___Utility___

void CASpectralProcessor::GetMagnitude(AudioBufferList* list, Float32* min, Float32* max) 
{	
	UInt32 half = mFFTSize >> 1;	
	for (UInt32 i=0; i<mNumChannels; ++i) {
		DSPSplitComplex	&freqData = mSpectralBufferList->mDSPSplitComplex[i];		
		
		Float32* b = (Float32*) list->mBuffers[i].mData;
		
		vDSP_zvabs(&freqData,1,b,1,half); 		
   
		vDSP_maxmgv(b, 1, &max[i], half); 
 		vDSP_minmgv(b, 1, &min[i], half); 
		
   } 
}


void CASpectralProcessor::GetFrequencies(Float32* freqs, Float32 sampleRate)
{
	UInt32 half = mFFTSize >> 1;	

	for (UInt32 i=0; i< half; i++){
		freqs[i] = ((Float32)(i))*sampleRate/((Float32)mFFTSize);	
	}
}


bool CASpectralProcessor::ProcessForwards(UInt32 inNumFrames, AudioBufferList* inInput)
{
	// copy from buffer list to input buffer
	CopyInput(inNumFrames, inInput);
		
	bool processed = false;
	// if enough input to process, then process.
	while (mInputSize >= mFFTSize) 
	{
		CopyInputToFFT(); // copy from input buffer to fft buffer
		DoWindowing();
		DoFwdFFT();
		ProcessSpectrum(mFFTSize, mSpectralBufferList()); // here you would copy the fft results out to a buffer indicated in mUserData, say for sonogram drawing
		processed = true;
	}
	
	return processed;
}

bool CASpectralProcessor::ProcessBackwards(UInt32 inNumFrames, AudioBufferList* outOutput)
{		
	
	ProcessSpectrum(mFFTSize, mSpectralBufferList());
	DoInvFFT();
	DoWindowing();
	OverlapAddOutput();		
	
	// copy from output buffer to buffer list
	CopyOutput(inNumFrames, outOutput);
	
	return true;
}