summaryrefslogtreecommitdiffstats
path: root/noatun/library/noatunarts/Equalizer_impl.cpp
blob: 552c3f91c83ea99a5bd1edd160bad0d59dd1b688 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
Copyright (C) 2001 Charles Samuels   <charles@kde.org>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
  
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.
   
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.

*/

#include "noatunarts.h"
#include "artsflow.h"
#include "fft.h"
#include <stdsynthmodule.h>
#include <math.h>
#include <cstring>

using namespace std;
using namespace Arts;

/**
 * This class is _VERY_ picky
 * which is why Noatun has it's own Equalizer class,
 * that does all the error checking and sends it to here
 **/

namespace Noatun
{

void resize(vector<float> &vec, unsigned int newsize)
{
	while (newsize < vec.size())
		vec.pop_back();
	while (newsize > vec.size())
		vec.push_back(0.0);
}

class Equalizer_impl : public Equalizer_skel, public StdSynthModule
{
	vector<float> mLevels;

	vector<BandPassInfo> mBandLeft, mBandRight;
	
	vector<float> mLevelWidths;
	vector<float> mLevelCenters;

	bool mEnabled;
	float mPreamp;
		

	float *mBuffer;
	unsigned int mBufferLength;

	void reinit()
	{
		mBandLeft.clear();
		mBandRight.clear();
		for (unsigned int i=0; i< mLevelWidths.size(); ++i)
		{
			BandPassInfo nfo;
			BandPassInit(&nfo, mLevelCenters[i], mLevelWidths[i]);
			mBandLeft.push_back(nfo);
			mBandRight.push_back(nfo);
		}
		
	}
	
public:
	void set(const std::vector<float>& levels, const std::vector<float>& centers, const std::vector<float>& widths)
	{
		mLevelCenters=centers;
		mLevelWidths=widths;

		mLevels=levels;
		reinit();
	}

	vector<float>* levelCenters()
	{
		return new vector<float>(mLevelCenters);
	}

	void levelCenters(const vector<float> &l)
	{
		mLevelCenters=l;
		reinit();
	}
	
	vector<float>* levelWidths()
	{
		return new vector<float>(mLevelWidths);
	}

	void levelWidths(const vector<float> &l)
	{
		mLevelWidths=l;
		reinit();
	}
	
	vector<float>* levels()
	{
		return new vector<float>(mLevels);
	}

	void levels(const vector<float> &l)
	{
		mLevels=l;
		reinit();
	}
	
	long bands()
	{
		return mLevels.size();
	}

	void bands(long b)
	{
		resize(mLevels, (int)b);
		resize(mLevelWidths, (int)b);
		resize(mLevelCenters, (int)b);
		reinit();
	}

	long enabled()
	{
		return (long)mEnabled;
	}

	void enabled(long enabled)
	{
		mEnabled=(bool)enabled;
	}

	float preamp()
	{
		return mPreamp;
	}

	void preamp(float a)
	{
		mPreamp=a;
	}
	
	void streamInit()
	{
	
	}
	
	void streamStart()
	{
	
	}

/* BandPassInit(&nfoLeft, 15000.0, 5000.0);
 * BandPassInit(&nfoLeft, 15000.0, 5000.0);
 */
	
	void calculateBlock(unsigned long samples)
	{
		// works by separating the bands
		// multiplying, then adding
		if (mEnabled && samples && &mLevels.front())
		{
			
			{ // preamp;

				float *left=inleft;
				float *right=inright;
				float *end=left+samples;

				float *oleft=outleft;
				float *oright=outright;

				while (left<end)
				{
					// see the _long_ comment in
					// tdemultimedia/arts/modules/synth_std_equalizer_impl.cc
					if (::fabs(*left) + ::fabs(*right) < 0.00000001)
						goto copy; // if you apologize, it's becomes ok
					*oleft=*left * mPreamp;
					*oright=*right * mPreamp;
					++left;
					++right;
					++oleft;
					++oright;
				}
			}
				
			BandPassInfo *leftBand=&mBandLeft.front();
			BandPassInfo *rightBand=&mBandRight.front();
			float *level=&mLevels.front();
			float *end=&mLevels.back();
			float intensity=1.0/(float)mLevels.size();

			if (mBufferLength != samples)
			{
				delete mBuffer;
				mBuffer = new float[samples];
				mBufferLength = samples;
			}
			
			float *buffer=mBuffer;
			float *bufferEnd=buffer+samples;
			while (level<end)
			{
				float *buffIter, *outIter;
				float levelAndIntensity=*level * intensity;
				
				BandPass(leftBand, outleft, buffer, samples);
				for (buffIter=buffer, outIter=outleft; buffIter<bufferEnd; ++buffIter, ++outIter)
					*outIter+=(*buffIter) * levelAndIntensity;

				BandPass(rightBand, outright, buffer, samples);
				for (buffIter=buffer, outIter=outright; buffIter<bufferEnd; ++buffIter, ++outIter)
					*outIter+=(*buffIter) * levelAndIntensity;

				++level;
				++leftBand;
				++rightBand;
			}
		}
		else
		{
		copy:
			// ASM optimized, so much faster
			memcpy(outleft, inleft, samples*sizeof(float));
			memcpy(outright, inright, samples*sizeof(float));
		}

	}

	Equalizer_impl() : mEnabled(false)
	{
		mBuffer=0;
		mBufferLength=0;
	}
	
	~Equalizer_impl()
	{
		delete [] mBuffer;
	}

	// speed hack! assume that someone else will
	// suspend us
	AutoSuspendState autoSuspend() { return asSuspend; }
	
};






class EqualizerSSE_impl : public EqualizerSSE_skel, public StdSynthModule
{
	vector<float> mLevels;

	vector<BandPassInfo> mBandLeft, mBandRight;
	
	vector<float> mLevelWidths;
	vector<float> mLevelCenters;

	bool mEnabled;
	float mPreamp;
		


	void reinit()
	{
		mBandLeft.clear();
		mBandRight.clear();
		for (unsigned int i=0; i< mLevelWidths.size(); ++i)
		{
			BandPassInfo nfo;
			BandPassInit(&nfo, mLevelCenters[i], mLevelWidths[i]);
			mBandLeft.push_back(nfo);
			mBandRight.push_back(nfo);
		}
		
	}
	
public:
	void set(const std::vector<float>& levels, const std::vector<float>& centers, const std::vector<float>& widths)
	{
		mLevelCenters=centers;
		mLevelWidths=widths;

		mLevels=levels;
		reinit();
	}

	vector<float>* levelCenters()
	{
		return new vector<float>(mLevelCenters);
	}

	void levelCenters(const vector<float> &l)
	{
		mLevelCenters=l;
		reinit();
	}
	
	vector<float>* levelWidths()
	{
		return new vector<float>(mLevelWidths);
	}

	void levelWidths(const vector<float> &l)
	{
		mLevelWidths=l;
		reinit();
	}
	
	vector<float>* levels()
	{
		return new vector<float>(mLevels);
	}

	void levels(const vector<float> &l)
	{
		mLevels=l;
		reinit();
	}
	
	long bands()
	{
		return mLevels.size();
	}

	void bands(long b)
	{
		resize(mLevels, (int)b);
		resize(mLevelWidths, (int)b);
		resize(mLevelCenters, (int)b);
		reinit();
	}

	long enabled()
	{
		return (long)mEnabled;
	}

	void enabled(long enabled)
	{
		mEnabled=(bool)enabled;
	}

	float preamp()
	{
		return mPreamp;
	}

	void preamp(float a)
	{
		mPreamp=a;
	}
	
	void streamInit()
	{
	
	}
	
	void streamStart()
	{
	
	}

/* BandPassInit(&nfoLeft, 15000.0, 5000.0);
 * BandPassInit(&nfoLeft, 15000.0, 5000.0);
 */
	
	void calculateBlock(unsigned long samples)
	{
#ifdef __i386__
		// works by separating the bands
		// multiplying, then adding
		if (mEnabled && samples)
		{
			if (*inleft + *inright == 0.0)
				goto copy; // just shut up :)
			
			{ // preamp;

				float *left=inleft;
				float *right=inright;
				float *end=left+samples;

				float *oleft=outleft;
				float *oright=outright;

				while (left<end)
				{
					*oleft=*left * mPreamp;
					*oright=*right * mPreamp;
					++left;
					++right;
					++oleft;
					++oright;
				}
			}
				
			BandPassInfo *leftBand=&mBandLeft.front();
			BandPassInfo *rightBand=&mBandRight.front();
			float *level=&mLevels.front();
			float *end=&mLevels.back();
			float intensity=1.0/(float)mLevels.size();

			float *buffer=new float[samples];
			float *bufferEnd=buffer+samples;
			while (level<end)
			{
				float *buffIter, *outIter;
				float levelAndIntensity=*level * intensity;
				
				BandPassSSE(leftBand, outleft, buffer, samples);
				for (buffIter=buffer, outIter=outleft; buffIter<bufferEnd; ++buffIter, ++outIter)
					*outIter+=(*buffIter) * levelAndIntensity;

				BandPassSSE(rightBand, outright, buffer, samples);
				for (buffIter=buffer, outIter=outright; buffIter<bufferEnd; ++buffIter, ++outIter)
					*outIter+=(*buffIter) * levelAndIntensity;

				++level;
				++leftBand;
				++rightBand;
			}
			delete [] buffer;
		}
		else
		{
		copy:
			// ASM optimized, so much faster
			memcpy(outleft, inleft, samples*sizeof(float));
			memcpy(outright, inright, samples*sizeof(float));
		}
#else
		(void)samples; // squelch warnings
#endif
	}

	EqualizerSSE_impl() : mEnabled(false)
	{
		
	}
	
	~EqualizerSSE_impl()
	{
	}

	// speed hack! assume that someone else will
	// suspend us
	AutoSuspendState autoSuspend() { return asSuspend; }
};


REGISTER_IMPLEMENTATION(Equalizer_impl);
REGISTER_IMPLEMENTATION(EqualizerSSE_impl);

}

#undef SAMPLES