ITASampleBuffer.cpp 18.7 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
2
3
4
5
6
7
8
// $Id: ITASampleBuffer.cpp 4124 2015-07-14 13:00:26Z fwefers $

#include <ITASampleBuffer.h>

#include <algorithm>
#include <assert.h>
#include <math.h>
#include <sstream>
js908001's avatar
js908001 committed
9
#include <memory.h>
Jonas Stienen's avatar
Jonas Stienen committed
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

#include <ITAException.h>
#include <ITAFade.h>
#include <ITAFastMath.h>

//#define FM_XSIZE(V, F) V = F * (V%F ? (V/F)+1 : V/F);

//! C-related sample field mathematical operations
/*
namespace CRels
{

	inline void fm_set(float* buf, float value, unsigned int count) {
		for (unsigned int i = 0; i<count; i++) buf[i] = value;
	}

	inline void fm_copy(float* dest, const float* src, unsigned int count) {
		memcpy(dest, src, count*sizeof(float));
	}

	inline void fm_add(float* dest, const float* summand, unsigned int count) {
		for (unsigned int i = 0; i<count; i++) dest[i] += summand[i];
	}

	inline void fm_sub(float* dest, const float* src, unsigned int count) {
		for (unsigned int i = 0; i<count; i++) dest[i] -= src[i];
	}

	inline void fm_mul(float* dest, float factor, unsigned int count) {
		for (unsigned int i = 0; i<count; i++) dest[i] *= factor;
	}

	inline void fm_cmul(float* dest, const float* factor, unsigned int count) {
		float re, im;
		for (unsigned int i = 0; i<count; i++) {
			// Realteil: Re'[k] = Re{dest[k]}*Re{factor[k]} - Im{dest[k]}*Im{factor[k]}
			re = dest[2 * i] * factor[2 * i] - dest[2 * i + 1] * factor[2 * i + 1];
			// Imaginärteil: Im'[k] = Re{dest[k]}*Im{factor[k]} + Im{dest[k]}*Re{factor[k]}
			im = dest[2 * i] * factor[2 * i + 1] + dest[2 * i + 1] * factor[2 * i];

			dest[2 * i] = re;
			dest[2 * i + 1] = im;
		}
	}

	inline void fm_cmul_x(float* dest, const float* factor1, const float* factor2, unsigned int count) {
		for (unsigned int i = 0; i<count; i++) {
			// Realteil: Re'[k] = Re{dest[k]}*Re{factor[k]} - Im{dest[k]}*Im{factor[k]}
			dest[2 * i] = factor1[2 * i] * factor2[2 * i] - factor1[2 * i + 1] * factor2[2 * i + 1];
			// Imaginärteil: Im'[k] = Re{dest[k]}*Im{factor[k]} + Im{dest[k]}*Re{factor[k]}
			dest[2 * i + 1] = factor1[2 * i] * factor2[2 * i + 1] + factor1[2 * i + 1] * factor2[2 * i];
		}
	}

	inline void fm_cdiv(float* dest, const float* div, unsigned int count) {
		float re, im, len;
		for (unsigned int i = 0; i<count; i++) {
			// Nenner im Gesamtergebnis |div[i]| berechnen
			len = div[2 * i] * div[2 * i] + div[2 * i + 1] * div[2 * i + 1];

			// Realteil: Re'[k] = [ Re{dest[k]}*Re{div[k]} + Im{dest[k]}*Im{div[k]} ] / len
			re = (dest[2 * i] * div[2 * i] + dest[2 * i + 1] * div[2 * i + 1]) / len;
			// Imaginärteil: Im'[k] = [ Im{dest[k]}*Re{div[k]} - Re{dest[k]}*Im{div[k]} ] / len
			im = (dest[2 * i + 1] * div[2 * i] - dest[2 * i] * div[2 * i + 1]) / len;

			dest[2 * i] = re;
			dest[2 * i + 1] = im;
		}
	}

	// Real-valued addition (in-place)
	// Semantic: srcdest[i] += src[i]
	void fm_add_f32(float* srcdest, const float* src, int count) {
		for (int i = 0; i<count; i++)
			srcdest[i] += src[i];
	}

	// Real-valued multiplication (in-place)
	// Semantic: srcdest[i] *= src[i]
	void fm_mul_f32(float* srcdest, const float* src, int count) {
		for (int i = 0; i<count; i++)
			srcdest[i] *= src[i];
	}

	// Complex-valued multiply
	// Semantic: dest[i] = src1[i]*src2[i]
	void fm_cmul_f32(float* dest, const float* src1, const float* src2, int count) {
		for (int i = 0; i<count; i++) {
			// Realteil: Re'[k] = Re{dest[k]}*Re{factor[k]} - Im{dest[k]}*Im{factor[k]}
			dest[2 * i] = src1[2 * i] * src2[2 * i] - src1[2 * i + 1] * src2[2 * i + 1];
			// Imaginärteil: Im'[k] = Re{dest[k]}*Im{src[k]} + Im{dest[k]}*Re{src[k]}
			dest[2 * i + 1] = src1[2 * i] * src2[2 * i + 1] + src1[2 * i + 1] * src2[2 * i];
		}
	}

js908001's avatar
js908001 committed
105
	// Complex-valued multiply-accumulate
Jonas Stienen's avatar
Jonas Stienen committed
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
	// Complex-valued multiply-accumulate
	// Semantic: srcdest[i] += src1[i]*src2[i]
	void fm_cmuladd_f32(float* srcdest, const float* src1, const float* src2, int count) {
		for (int i = 0; i<count; i++) {
			// Realteil: Re'[k] = Re{dest[k]}*Re{factor[k]} - Im{dest[k]}*Im{factor[k]}
			srcdest[2 * i] += src1[2 * i] * src2[2 * i] - src1[2 * i + 1] * src2[2 * i + 1];
			// Imaginärteil: Im'[k] = Re{dest[k]}*Im{src[k]} + Im{dest[k]}*Re{src[k]}
			srcdest[2 * i + 1] += src1[2 * i] * src2[2 * i + 1] + src1[2 * i + 1] * src2[2 * i];
		}
	}
} // End namespace "CRels"
*/

ITASampleBuffer::ITASampleBuffer()
: m_pParent(NULL), m_iLength(0), m_pfData(NULL) {}

ITASampleBuffer::ITASampleBuffer(int iLength, bool bZeroinit)
: m_pParent(NULL), m_iLength(0), m_pfData(NULL)
{
	Init( iLength, bZeroinit);
}

ITASampleBuffer::ITASampleBuffer(const ITASampleBuffer* pSource)
: m_pParent(NULL), m_iLength(0), m_pfData(NULL)
{
	*this = *pSource;
}

ITASampleBuffer::ITASampleBuffer(const ITASampleBuffer& sbSource)
: m_pParent(NULL), m_iLength(0), m_pfData(NULL)
{
	*this = sbSource;
}

ITASampleBuffer::~ITASampleBuffer()
{
	Free();
}

void ITASampleBuffer::Init( int iLength, bool bZeroinit )
{
	if( m_pParent )
		ITA_EXCEPT1(MODAL_EXCEPTION, "Init impossible. This sample buffer is a channel of a parent sample frame.");

	assert( iLength >= 0 );

	Free();

	size_t iBytes = size_t( iLength*sizeof(float) );
#ifdef __GNUC__
	// TODO: Für den g++ erstmal kein aligned malloc
	m_pfData = (float*)malloc(iBytes);
#else
	m_pfData = (float*)_aligned_malloc(iBytes, 16);
#endif
	m_iLength = iLength;
}

void ITASampleBuffer::Free()
{
#ifdef __GNUC__
	if (m_pfData)
		free(m_pfData); // unaligned
#else
	if (m_pfData)
		_aligned_free( m_pfData );
#endif
	m_pfData = NULL;
	m_iLength = 0;
}

bool ITASampleBuffer::IsEmpty() const
{
	return (m_iLength == 0);
}

int ITASampleBuffer::GetLength() const
{
	return m_iLength;
}

const float* ITASampleBuffer::GetData() const
{
	return m_pfData;
}

float* ITASampleBuffer::GetData()
{
	return m_pfData;
}

void ITASampleBuffer::Fill(float fValue)
{
	fm_set(m_pfData, fValue, m_iLength);
}

void ITASampleBuffer::Fill(int iOffset, int iCount, float fValue) {
	if ((iOffset < 0) || (iOffset >= m_iLength))
		ITA_EXCEPT1(INVALID_PARAMETER, "Offset out of range");

	if ((iCount < 0) || (iOffset+iCount > m_iLength))
		ITA_EXCEPT1(INVALID_PARAMETER, "Count out of range");

	// TODO: Schnelle Implementierung?
	for (int i=iOffset; i<iOffset+iCount; i++) m_pfData[i] = fValue;
}

void ITASampleBuffer::Zero()
{
	Fill(0);
}

void ITASampleBuffer::Zero(int iOffset, int iCount, float fValue)
{
	Fill(iOffset, iCount, 0);
}

void ITASampleBuffer::Identity()
{
	if (m_iLength == 0)
		ITA_EXCEPT1(INVALID_PARAMETER, "Sample buffer has no length");
	
	m_pfData[0] = 1;
	for (int i=1; i<m_iLength; i++) m_pfData[i] = 0;
}

void ITASampleBuffer::Fade(int iOffset, int iCount, int iFadeDirection, int iFadeFunction)
{
	assert( iOffset >= 0 );
	assert( iCount > 0 );
	assert( (iOffset+iCount) <= m_iLength );

	int iFlags = 0;

	switch (iFadeDirection)
	{
	case FADE_IN:
		iFlags |= ITA_FADE_IN;
		break;
	case FADE_OUT:
		iFlags |= ITA_FADE_OUT;
		break;	
	default:
		// Ungültiger Wert
		assert( false );
	}

	switch (iFadeFunction) {
	case LINEAR:
		iFlags |= ITA_FADE_LINEAR;
		break;
	case COSINE_SQUARE:
		iFlags |= ITA_FADE_COSINE_SQUARE;
		break;	
	default:
		// Ungültiger Wert
		assert( false );
	}

	::Fade(m_pfData + iOffset, iCount, iFlags); 
}

void ITASampleBuffer::Crossfade(const ITASampleBuffer* psbSrc, int iOffset, int iCount, int iFadeDirection, int iFadeFunction)
{
	assert( psbSrc );
	assert( iOffset >= 0 );
	assert( iCount >= 0 );
	assert( (iOffset+iCount) <= m_iLength );
	assert( (iOffset+iCount) <= psbSrc->m_iLength );

	int iFlags = 0;

	switch (iFadeFunction) {
	case LINEAR:
		iFlags |= ITA_FADE_LINEAR;
		break;
	case COSINE_SQUARE:
		iFlags |= ITA_FADE_COSINE_SQUARE;
		break;	
	default:
		// Ungültiger Wert
		assert( false );
	}

	switch (iFadeDirection) {
	case CROSSFADE_FROM_SOURCE:
		// Samples am Anfang kopieren
		for (int i=0; i<iOffset; i++) m_pfData[i] = psbSrc->m_pfData[i];
		::Crossfade(psbSrc->m_pfData + iOffset, m_pfData + iOffset, m_pfData + iOffset, iCount, iFlags);
		return;
	case CROSSFADE_TO_SOURCE:
		assert( psbSrc->m_iLength >= m_iLength );
		::Crossfade(m_pfData + iOffset, psbSrc->m_pfData + iOffset, m_pfData + iOffset, iCount, iFlags);
		// Samples am Ende kopieren
		for (int i=iOffset+iCount; i<m_iLength; i++) m_pfData[i] = psbSrc->m_pfData[i];
		return;

	default:
		// Ungültiger Wert
		assert( false );
	}
}

void ITASampleBuffer::Crossfade(const ITASampleBuffer& sbSrc, int iOffset, int iCount, int iFadeDirection, int iFadeFunction)
{
	Crossfade(&sbSrc, iOffset, iCount, iFadeDirection, iFadeFunction);
}

void ITASampleBuffer::Envelope(float fGain0, float fGain1)
{
	if (IsEmpty()) return;

	// Linear interpolation
	float m = (fGain1 - fGain0) / m_iLength;
	for (int i=0; i<m_iLength; i++)
		m_pfData[i] *= (fGain0 + m*i);
}

void ITASampleBuffer::read(float* pfDest, int iCount, int iSrcOffset) const
{
	assert( pfDest != NULL );
	assert( iCount >= 0 );
	assert( (iSrcOffset >= 0) );
	assert( (iSrcOffset + iCount) <= m_iLength );

	// TODO: Erkennung von MemoryAlignment und schnelle SSE-Variante?
	memcpy(pfDest, m_pfData + iSrcOffset, iCount * sizeof(float));
}

void ITASampleBuffer::write(const float* pfSrc, int iCount, int iDestOffset)
{
	assert( pfSrc != NULL );
	assert( iCount >= 0 );
	assert( (iDestOffset >= 0) );
	assert( (iDestOffset + iCount) <= m_iLength );

	// TODO: Erkennung von MemoryAlignment und schnelle SSE-Variante?
	memcpy(m_pfData + iDestOffset, pfSrc, iCount * sizeof(float));
}

void ITASampleBuffer::write(const ITASampleBuffer* psbSrc, int iCount, int iSrcOffset, int iDestOffset)
{
	assert( psbSrc != NULL );
	assert( iCount >= 0 );
	assert( (iSrcOffset >= 0) );
	assert( (iSrcOffset + iCount) <= psbSrc->GetLength() );
	assert( (iDestOffset + iCount) <= m_iLength );

	// TODO: Erkennung von MemoryAlignment und schnelle SSE-Variante?
	memcpy(m_pfData + iDestOffset, psbSrc->GetData() + iSrcOffset, iCount * sizeof(float));
}

void ITASampleBuffer::write(const ITASampleBuffer& sbSrc, int iCount, int iSrcOffset, int iDestOffset) {
	write(&sbSrc, iCount, iSrcOffset, iDestOffset);
}

void ITASampleBuffer::cyclic_read(float* pfDest, int iCount, int iSrcOffset) const {
	assert( pfDest != NULL );
	assert( iCount >= 0 );
	assert( (iSrcOffset >= 0) && (iSrcOffset < m_iLength) );

	int n = 0;			// Anzahl kopierter Samples
	int p = ((iSrcOffset % m_iLength) + m_iLength) % m_iLength; // Leseposition

	while (n < iCount) {
		// Verbleibende Samples berechnen
		int r = std::min(iCount - n, m_iLength - p);
		read(pfDest+n, r, p);
		p = (p+r) % m_iLength;
		n += r;
	}
}

void ITASampleBuffer::cyclic_write(const float* pfSrc, int iCount, int iDestOffset) {
	assert( pfSrc != NULL );
	assert( iCount >= 0 );

	int n = 0;				// Anzahl kopierter Samples
	int p = ((iDestOffset % m_iLength) + m_iLength) % m_iLength; // Schreibposition

	while (n < iCount) {
		// Verbleibende Samples berechnen
		int r = std::min(iCount - n, m_iLength - p);
		write(pfSrc+n, r, p);
		p = (p+r) % m_iLength;
		n += r;
	}
}

void ITASampleBuffer::cyclic_write(const ITASampleBuffer* psbSrc, int iCount, int iSrcOffset, int iDestOffset) {
	assert( psbSrc != NULL );
	assert( iCount >= 0 );

	int iSrcLength = psbSrc->GetLength();

	int n = 0;				// Anzahl kopierter Samples
	// Beliebige Offsets werden in das positive Intervall abgebildet
	int p = ((iSrcOffset % iSrcLength) + iSrcLength) % iSrcLength; // Leseposition
	int q = ((iDestOffset % m_iLength) + m_iLength) % m_iLength; // Schreibposition

	while (n < iCount) {
		// Verbleibende Samples berechnen
		int rs = iSrcLength - p;
		int rd = m_iLength - q;
		int r = std::min(iCount - n, std::min(rs, rd));
		write(psbSrc, r, p, q);
		p = (p+r) % iSrcLength;
		q = (q+r) % m_iLength;
		n += r;
	}	
}

void ITASampleBuffer::cyclic_write(const ITASampleBuffer& sbSrc, int iCount, int iSrcOffset, int iDestOffset) {
	cyclic_write(&sbSrc, iCount, iSrcOffset, iDestOffset);
}


void ITASampleBuffer::CyclicShift(int iCount)
{
	if (iCount >= GetLength())
		ITA_EXCEPT1(INVALID_PARAMETER, "Shifting by a count greater than buffer length not allowed");
	ITASampleBuffer sbTemp(this);
	cyclic_write(sbTemp, GetLength(), iCount);
}

void ITASampleBuffer::add_scalar(float fValue) {
	// Invariante
	if (fValue == 0) return;

	// TODO: Schnelle Implementierung?
	for (int i=0; i<m_iLength; i++) m_pfData[i] += fValue;
}

void ITASampleBuffer::sub_scalar(float fValue) {
	// Invariante
	if (fValue == 0) return;

	// TODO: Schnelle Implementierung?
	for (int i=0; i<m_iLength; i++) m_pfData[i] -= fValue;
}

void ITASampleBuffer::mul_scalar(float fValue) {
	// Invariante
	if (fValue == 1) return;

	// TODO: Schnelle Implementierung?
	for (int i=0; i<m_iLength; i++) m_pfData[i] *= fValue;
}

void ITASampleBuffer::div_scalar(float fValue) {
	if (fValue == 0) ITA_EXCEPT1(INVALID_PARAMETER, "Division by zero");

	// Invariante
	if (fValue == 1) return;

	// TODO: Schnelle Implementierung?
	for (int i=0; i<m_iLength; i++) m_pfData[i] /= fValue;
}

void ITASampleBuffer::add_buf(const ITASampleBuffer* pSource) {
	add_buf(pSource, pSource->GetLength());
}

void ITASampleBuffer::add_buf(const ITASampleBuffer* pSource, const int iCount) {
	if (!pSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (pSource->GetLength() < iCount)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths of source buffer too small");
	if (m_iLength < iCount)  ITA_EXCEPT1(INVALID_PARAMETER, "Requested lengths of source buffer too big to fit into target buffer");

	fm_add(m_pfData, pSource->GetData(), iCount);
}


void ITASampleBuffer::add_buf_pos( const ITASampleBuffer* pSource, const int iPos ) // TODO: move this to add_buf(mit offset=0)
{
	if (!pSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	//if (pSource->GetLength() < iCount)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths of source buffer too small");
	if ((pSource->m_iLength+iPos) > m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Source length + delay exceed length of filter");

	fm_add(&(m_pfData[iPos]), pSource->GetData(), pSource->GetLength());
}

void ITASampleBuffer::add_buf_pos( float* fSource,const int iSize, const int iPos )
{
	if (!fSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (m_iLength < iPos+iSize)  ITA_EXCEPT1(INVALID_PARAMETER, "Source length + delay exceed length of filter");

	fm_add(&(m_pfData[iPos]), fSource, iSize);
}



void ITASampleBuffer::sub_buf(const ITASampleBuffer* pSource) {
	if (!pSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (pSource->GetLength() != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Block lengths do not match");

	fm_sub(m_pfData, pSource->GetData(), m_iLength);
}

void ITASampleBuffer::mul_buf(const ITASampleBuffer* pSource) {
	if (!pSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (pSource->GetLength() != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Block lengths do not match");

	// TODO: Schnelle Implementierung?
	const float* pfSourceData = pSource->GetData();
	for (int i=0; i<m_iLength; i++) m_pfData[i] *= pfSourceData[i];
}

void ITASampleBuffer::div_buf(const ITASampleBuffer* pSource) {
	if (!pSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (pSource->GetLength() != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Block lengths do not match");

	// TODO: Schnelle Implementierung?
	// TODO: Was wenn Division durch Null?
	const float* pfSourceData = pSource->GetData();
	for (int i=0; i<m_iLength; i++) m_pfData[i] /= pfSourceData[i];
}

void ITASampleBuffer::add_buf(const ITASampleBuffer& sbSource, const int iCount) { add_buf( &sbSource, iCount ); }
void ITASampleBuffer::add_buf(const ITASampleBuffer& sbSource) { add_buf( &sbSource ); }
void ITASampleBuffer::sub_buf(const ITASampleBuffer& sbSource) { sub_buf( &sbSource ); }
void ITASampleBuffer::mul_buf(const ITASampleBuffer& sbSource) { mul_buf( &sbSource ); }
void ITASampleBuffer::div_buf(const ITASampleBuffer& sbSource) { div_buf( &sbSource ); }

void ITASampleBuffer::MulAdd(const ITASampleBuffer* pSource, float fScalar, int iSrcOffset, int iDestOffset, int iCount) {
	if (!pSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	
	if (iSrcOffset < 0) ITA_EXCEPT1(INVALID_PARAMETER, "Invalid source offset");
	if (iDestOffset < 0) ITA_EXCEPT1(INVALID_PARAMETER, "Invalid destination offset");
	if (iCount < 0) ITA_EXCEPT1(INVALID_PARAMETER, "Invalid count");

	// Test: Lesen innerhalb des Quellblockes
	if ((iSrcOffset + iCount) > pSource->GetLength()) ITA_EXCEPT1(INVALID_PARAMETER, "Source range exceeds source buffer");
	if ((iDestOffset + iCount) > m_iLength) ITA_EXCEPT1(INVALID_PARAMETER, "Destination range exceeds destination buffer");

	// Spezialfall: Count = 0 => Nichts zu tun
	if (iCount == 0) return;

	// Spezialfall: Skalar = 0 => Nichts zu tun
	if (fScalar == 0) return;

	for (int i=0; i<iCount; i++)
		m_pfData[iDestOffset+i] += (pSource->m_pfData[iSrcOffset+i] * fScalar);
}

void ITASampleBuffer::MulAdd(const ITASampleBuffer& sbSource, float fScalar, int iSrcOffset, int iDestOffset, int iCount) {
	MulAdd( &sbSource, fScalar, iSrcOffset, iDestOffset, iCount );
}

float ITASampleBuffer::FindPeak(int* piPeakIndex) {
	if (m_iLength == 0) {
		if (piPeakIndex) *piPeakIndex = 0;
		return 0;
	}

	float fPeak = 0;
	int iPeakIndex = 0;

	// TODO: Schnelle Implementierung? (SSE3?)
	for (int i=0; i<m_iLength; i++) {
		float x = fabs( m_pfData[i] );
		if (x > fPeak) {
			fPeak = x;
			iPeakIndex = i;
		}
	}

	if (piPeakIndex) *piPeakIndex = iPeakIndex;
	return fPeak;
}

void ITASampleBuffer::Negate()
{
	mul_scalar(-1);
};

float& ITASampleBuffer::operator[](int iSample) {
	return m_pfData[iSample];
}

const float& ITASampleBuffer::operator[](int iSample) const {
	return m_pfData[iSample];
}

ITASampleBuffer& ITASampleBuffer::operator=(const ITASampleBuffer& rhs) {
	// Selbstzuweisung abfangen
	if (&rhs == this) return *this;

	if (m_pParent)
	{
			ITA_EXCEPT1(MODAL_EXCEPTION, "Assignment impossible. Would change length, but sample buffer is part of a parent sample frame.");
	}

	// Schnelle Abkürzung
	if (rhs.IsEmpty()) {
		Free();
		return *this;
	}

	if ((m_iLength != rhs.m_iLength))
	{
		// Neu allozieren
		Init(  rhs.GetLength(), false );
	}

	// Samples kopieren
	fm_copy(m_pfData, rhs.GetData(), m_iLength);

	return *this;
}

ITASampleBuffer& ITASampleBuffer::operator+=(const float rhs) {
	add_scalar(rhs);
	return *this;
}

ITASampleBuffer& ITASampleBuffer::operator-=(const float rhs) {
	sub_scalar(rhs);
	return *this;
}

ITASampleBuffer& ITASampleBuffer::operator*=(const float rhs) {
	mul_scalar(rhs);
	return *this;
}

ITASampleBuffer& ITASampleBuffer::operator/=(const float rhs) {
	div_scalar(rhs);
	return *this;
}

ITASampleBuffer& ITASampleBuffer::operator+=(const ITASampleBuffer& rhs) {
	add_buf(rhs);
	return *this;
}

ITASampleBuffer& ITASampleBuffer::operator-=(const ITASampleBuffer& rhs) {
	sub_buf(rhs);
	return *this;
}

ITASampleBuffer& ITASampleBuffer::operator*=(const ITASampleBuffer& rhs) {
	mul_buf(rhs);
	return *this;
}

ITASampleBuffer& ITASampleBuffer::operator/=(const ITASampleBuffer& rhs) {
	div_buf(rhs);
	return *this;
}

std::string ITASampleBuffer::toString() const {
	std::stringstream ss;
	ss << "Sample buffer { ";
	if (IsEmpty())
		ss << "emtpy";
	else
		ss << m_iLength << " samples";
	ss << " }";
	return ss.str();
}