ITASoundSamplerImpl.cpp 24.8 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
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
/*
 *      +------------------+
 *  --->| ITA Sampler      |--->
 *  --->| o o o o      /// |--->
 *      +------------------+
 *
 *  ITASampler ist eine C++ Bibliothek die einen Sound sampler zur wavetable-basierten
 *  Klangsynthese bereitstellt. Der Begriff "sampler" bezieht sich hier auf den Terminus
 *  aus der Musikproduktion und nicht auf Abtastwerte.
 *
 *  Datei:		ITASoundSamplerImpl.cpp
 *  Zweck:		Implementierungsklasse des sound samplers
 *  Autoren:	Frank Wefers (Frank.Wefers@akustik.rwth-aachen.de)
 *  CVS-Datum:	$Id: ITASoundSamplerImpl.cpp,v 1.3 2010-04-19 14:24:57 fwefers Exp $
 *
 *  (c) Copyright Institut fr Technische Akustik (ITA) RWTH Aachen, 2008
 */

#include "ITASoundSamplerImpl.h"

#include "ITASoundSamplerPlayback.h"
#include "ITASoundSampleImpl.h"
#include "ITASoundSamplePoolImpl.h"
#include "PlaybackIDGenerator.h"

#include <ITASampleClock.h>
#include <algorithm>
#include <ITAException.h>
#include <ITAFade.h>
#include <ITAFunctors.h>
#include <ITANumericUtils.h>
#include <ITAStringUtils.h>

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
34
35
#include <cstring>

Jonas Stienen's avatar
Jonas Stienen committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Vorgabe: Anzahl Samples fr Fading bei Pause/Fortsetzen einer Wiedergabe
#define DEFAULT_PAUSE_FADE_LENGTH 256

ITASoundSamplerImpl::ITASoundSamplerImpl(int iOutputChannels, double dSamplerate, int iBlocklength, ITASoundSamplePool* pSamplePool)
: ITADatasourceRealization((unsigned) iOutputChannels, dSamplerate, (unsigned) iBlocklength, 2),
  m_pDefaultSampleClock(NULL),
  m_pDefaultSamplePool(NULL)
{
	m_iTrackIDCount = 0;

	m_iCurrentBlockIndex = 0;
	m_bProcess = true;
	m_dMasterGain = 1.0;

	// Standard Sampleclock = 1s Raster
51
	m_pDefaultSampleClock = new ITASampleClock(ITADatasourceRealization::m_dSampleRate, 1.0);
Jonas Stienen's avatar
Jonas Stienen committed
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
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
	m_pSampleClock = m_pDefaultSampleClock;

	if (pSamplePool) {
		// Benutzung eines fremden Samplepools
		m_pSamplePool = dynamic_cast<ITASoundSamplePoolImpl*>(pSamplePool);
	} else {
		// Eigen-verwalteter Samplepool
		m_pDefaultSamplePool = new ITASoundSamplePoolImpl(iOutputChannels, dSamplerate);
		m_pSamplePool = m_pDefaultSamplePool;
	}
}

ITASoundSamplerImpl::~ITASoundSamplerImpl() {
	// Alle Playbacks entfernen
	RemoveAllPlaybacks();

	// TODO: Resourcen freigeben
	delete m_pDefaultSampleClock;
	delete m_pDefaultSamplePool;
}

void ITASoundSamplerImpl::SetSampleClock(ITASampleClock* pClock) {
	// TODO: Was ist wenn man das whrend dem Streaming aufruft?
	m_pSampleClock = pClock;
}

ITASoundSamplePool* ITASoundSamplerImpl::GetSamplePool() const {
	return m_pSamplePool;
}

void ITASoundSamplerImpl::ResetSampleCount() {
	// TODO: Locking-Granularitt klren. Sicherheitshalber erstmal alles locken.
	//m_csSamples.enter();
	m_csTracks.enter();
	m_csSlotbuf.enter();

	// TODO: Clock-Resync erforderlich?
	m_iCurrentBlockIndex = 0;

	m_csSlotbuf.leave();
	m_csTracks.leave();
	//m_csSamples.leave();
}

int ITASoundSamplerImpl::GetSampleCount() {
	// TODO: Locking-Granularitt klren. Sicherheitshalber erstmal alles locken.
	//m_csSamples.enter();
	m_csTracks.enter();
	m_csSlotbuf.enter();

	// TODO: Clock-Resync erforderlich?
	int iResult = m_iCurrentBlockIndex * (signed) ITADatasourceRealization::m_uiBlocklength;

	m_csSlotbuf.leave();
	m_csTracks.leave();
	//m_csSamples.leave();

	return iResult;
}

int ITASoundSamplerImpl::AddTrack(int iTrackChannels) {
	// Sicherheitschecks:
	if (iTrackChannels > (signed) ITADatasourceRealization::m_uiChannels)
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid number of channels");

	m_csTracks.enter();

	int iTrackID = m_iTrackIDCount++;
	Track track(iTrackChannels);
	m_mTracks.insert( std::pair<int, Track>(iTrackID, track) );
	
	m_csTracks.leave();
		
	return iTrackID;
}

void ITASoundSamplerImpl::RouteTrackChannel(int iTrackID, int iTrackChannel, int iOutputChannel) {
	// Hinweis: Hier wird relativ grob-granular gelockt. Das ist aber nicht weiter schlimm, da
	//          Operationen auf den Spuren hauptschlich vor dem Streaming durchgefhrt werden. 

	if (iOutputChannel >= (signed) ITADatasourceRealization::m_uiChannels) 
		ITA_EXCEPT1(INVALID_PARAMETER, "Output channel out of range");

	m_csTracks.enter();

	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end()) {
		m_csTracks.leave();
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");
	}

	Track& track = it->second;
	if (iTrackChannel >= track.iChannels) {
		m_csTracks.leave();
		ITA_EXCEPT1(INVALID_PARAMETER, "Track channel out of range");
	}

	track.viChannelRoutings[iTrackChannel] = iOutputChannel;

	m_csTracks.leave();
}

int ITASoundSamplerImpl::AddMonoTrack(int iOutputChannel) {
	int iTrackID = AddTrack(1);
	RouteTrackChannel(iTrackID, 0, iOutputChannel);
	return iTrackID;
}

int ITASoundSamplerImpl::AddStereoTrack(int iLeftOutputChannel, int iRightOutputChannel) {
	int iTrackID = AddTrack(2);
	RouteTrackChannel(iTrackID, 0, iLeftOutputChannel);
	RouteTrackChannel(iTrackID, 1, iRightOutputChannel);
	return iTrackID;
}

void ITASoundSamplerImpl::RemoveTrack(int iTrackID) {
	// Hinweis: Tracks und Slotbuf locken, da ggf. Playbacks entfernt werden mssen
	m_csTracks.enter();
	m_csSlotbuf.enter();

	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end()) {
		m_csSlotbuf.leave();
		m_csTracks.leave();
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");
	}

	m_mTracks.erase(it);

	// Alle Playbacks des Track lschen
	m_sbSlotbuf.DeletePlaybacksOfTrack(iTrackID);

	m_csSlotbuf.leave();
	m_csTracks.leave();
}

std::string ITASoundSamplerImpl::GetTrackName(int iTrackID) {
	m_csTracks.enter();

	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end()) {
		m_csTracks.leave();
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");
	}

	Track& track = it->second;
	std::string sName = track.sName;

	m_csTracks.leave();
	return sName;
}

void ITASoundSamplerImpl::SetTrackName(int iTrackID, const std::string& sName) {
	m_csTracks.enter();

	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end()) {
		m_csTracks.leave();
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");
	}

	Track& track = it->second;
	track.sName = sName;

	m_csTracks.leave();
}

int ITASoundSamplerImpl::GetTrackNumberOfChannels(int iTrackID) {
	m_csTracks.enter();

	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end()) {
		m_csTracks.leave();
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");
	}

	Track& track = it->second;
	int iChannels = track.iChannels;

	m_csTracks.leave();
	return iChannels;
}

double ITASoundSamplerImpl::GetMasterGain() {
	// TODO: Absichern
	return m_dMasterGain;
}

void ITASoundSamplerImpl::SetMasterGain(double dGain) {
	// TODO: Absichern
	if (dGain < 0)
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid gain");

	m_dMasterGain = dGain;
}

double ITASoundSamplerImpl::GetTrackGain(int iTrackID) {
	// TODO: Absichern
	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end())
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");

	Track& track = it->second;
	return track.dGain;
}

void ITASoundSamplerImpl::SetTrackGain(int iTrackID, double dGain) {
	// TODO: Absichern
	if (dGain < 0)
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid gain");

	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end())
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");

	Track& track = it->second;
	track.dGain = dGain;
}

double ITASoundSamplerImpl::GetTrackChannelGain(int iTrackID, int iTrackChannel) {
	// TODO: Absichern
	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end())
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");

	Track& track = it->second;
	if (iTrackChannel >= track.iChannels)
		ITA_EXCEPT1(INVALID_PARAMETER, "Track channel out of range");

	return track.vdChannelGains[iTrackChannel];
}

void ITASoundSamplerImpl::SetTrackChannelGain(int iTrackID, int iTrackChannel, double dGain) {
	// TODO: Absichern
	if (dGain < 0)
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid gain");

	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end())
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");

	Track& track = it->second;
	if (iTrackChannel >= track.iChannels)
		ITA_EXCEPT1(INVALID_PARAMETER, "Track channel out of range");

	track.vdChannelGains[iTrackChannel] = dGain;
}

int ITASoundSamplerImpl::CreateSample(const float* pfData, int iLength, double dSamplerate, std::string sName) {
	// Delegat
	return m_pSamplePool->CreateSample(pfData, iLength, dSamplerate, sName);
}

int ITASoundSamplerImpl::CreateSample(const float* pfLeftChannelData, const float* pfRightChannelData, int iLength, double dSamplerate, std::string sName) {
	// Delegat
	return m_pSamplePool->CreateSample(pfLeftChannelData, pfRightChannelData, iLength, dSamplerate, sName);
}

int ITASoundSamplerImpl::CreateSample(const float** ppfChannelData, int iNumChannels, int iLength, double dSamplerate, std::string sName) {
	// Delegat
	return m_pSamplePool->CreateSample(ppfChannelData, iNumChannels, iLength, dSamplerate, sName);
}

int ITASoundSamplerImpl::CreateSample(std::vector<float*> vpfChannelData, int iLength, double dSamplerate, std::string sName) {
	// Delegat
	return m_pSamplePool->CreateSample(vpfChannelData, iLength, dSamplerate, sName);
}

int ITASoundSamplerImpl::LoadSample(const std::string& sFilename, std::string sName) {
	// Delegat
	return m_pSamplePool->LoadSample(sFilename, sName);
}

void ITASoundSamplerImpl::FreeSample(int iSampleID) {
	// Delegat
	m_pSamplePool->FreeSample(iSampleID);
}

int ITASoundSamplerImpl::AddPlaybackBySamplecount(int iSampleID, int iTrackID, int iSamplecount, bool bSetPaused, double dGain) {
	// Gain zuerst berprfen, da am einfachsten
	if (dGain < 0)
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid gain");

	//m_csSamples.enter();
	//if (m_mSamples.find(iSampleID) == m_mSamples.end()) {
	//	m_csSamples.leave();
	//	ITA_EXCEPT1(INVALID_PARAMETER, "Invalid sample ID");
	//}

	// Sample suchen und falls gefunden sofort (atomar) ein Playback vermerken
	const ITASoundSample* pSamplePtr = m_pSamplePool->GetSample(iSampleID, false);
	if (!pSamplePtr)
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid sample ID");

	ITASoundSampleImpl* pSample = dynamic_cast<ITASoundSampleImpl*>( const_cast<ITASoundSample*>( pSamplePtr ) );
	pSample->AddPlaybackReference();

	m_csTracks.enter();
	TrackMap::const_iterator trcit = m_mTracks.find(iTrackID);
	if (trcit == m_mTracks.end()) {
		m_csTracks.leave();
		pSample->RemovePlaybackReference();
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");
	}

	// Das Sample muss exakt die gleiche Anzahl Kanle wie die Track haben
	if (pSample->GetNumberOfChannels() != trcit->second.iChannels) {
		m_csTracks.leave();
		pSample->RemovePlaybackReference();
		ITA_EXCEPT1(INVALID_PARAMETER, "Sample and track number of channels do not match");
	}

	// Timecode berprfen - Muss in der Zukunft liegen
	// TODO: Hier erstmal Zwischenlsung - berarbeiten mit Clocks

	int iPlaybackPosition,
		iPlaybackBlock,
		iPlaybackOffset;

	if (iSamplecount > 0)
		iPlaybackPosition = iSamplecount;
	else
		// Timecode == 0 -> Im nchsten Block abspielen!
		iPlaybackPosition = (m_iCurrentBlockIndex+1) * (signed) ITADatasourceRealization::m_uiBlocklength - iSamplecount;

	iPlaybackBlock = iPlaybackPosition / (signed) ITADatasourceRealization::m_uiBlocklength;
	iPlaybackOffset = iPlaybackPosition % (signed) ITADatasourceRealization::m_uiBlocklength;

	// Entweder vor Wiedergabeblock oder darin aber noch nicht Daten angefordert
	if (! ((iPlaybackBlock > m_iCurrentBlockIndex) ||
	       ((iPlaybackBlock == m_iCurrentBlockIndex) && m_bProcess))) {
		m_csTracks.leave();
		pSample->RemovePlaybackReference();
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid timecode - Playback must occur in the future");
	}

	// Slot holen (kann inplizite Vergrerung des Slotpuffers einschlieen)
	m_csSlotbuf.enter();
	ITASoundSamplerSlot* pSlot = m_sbSlotbuf.GetSlot(iPlaybackBlock);

	// Playback hinzufgen
	ITASoundSamplerPlayback* pPlayback = new ITASoundSamplerPlayback;
	pPlayback->iID = generatePlaybackID();
	pPlayback->iSampleID = iSampleID;
	pPlayback->pSample = pSample;
	pPlayback->iTrackID = iTrackID;
	pPlayback->iPlaybackPosition = iPlaybackPosition;
	pPlayback->iPlaybackBlock = iPlaybackBlock;
	pPlayback->iPlaybackOffset = iPlaybackOffset;
	pPlayback->iSamplePosition = 0;
	pPlayback->dGain = dGain;

	pPlayback->iPauseState = (bSetPaused ? 2 : 0); 
	pPlayback->iPauseFadeOffset = 0;
	pPlayback->iPauseFadeLength = 0;

	m_mPlaybacks.insert( std::pair<int, ITASoundSamplerPlayback*>(pPlayback->iID, pPlayback) );
	
	// Playback im Slot eintragen
	pSlot->m_vPlaybacks.push_back(pPlayback);

	m_csSlotbuf.leave();
	m_csTracks.leave();

	return pPlayback->iID;
}

int ITASoundSamplerImpl::AddPlaybackByTimecode(int iSampleID, int iTrackID, double dTimecode, bool bSetPaused, double dGain) {
	if (dTimecode > 0)
		// Samplecount zum gegebenen Timecode mittels der Sampleclock berechnen
		return AddPlaybackBySamplecount(iSampleID, iTrackID, (int) m_pSampleClock->GetSampleForTime(dTimecode), bSetPaused, dGain);
	else
		return AddPlaybackBySamplecount(iSampleID, iTrackID, (int) 0, bSetPaused, dGain);
}

void ITASoundSamplerImpl::RemovePlayback(int iPlaybackID) {
	m_csSlotbuf.enter();
	PlaybackMap::iterator it = m_mPlaybacks.find(iPlaybackID);
	if (it == m_mPlaybacks.end()) {
		// Playback-ID ist nicht (mehr) gltig
		m_csSlotbuf.leave();
		return;
	}

	// Playback aus seinem Plot lschen
	ITASoundSamplerPlayback* pPlayback = it->second;
	pPlayback->pSample->RemovePlaybackReference();
	ITASoundSamplerSlot* pSlot = m_sbSlotbuf.GetSlot(pPlayback->iPlaybackBlock);
	pSlot->m_vPlaybacks.erase( std::find(pSlot->m_vPlaybacks.begin(), pSlot->m_vPlaybacks.end(), pPlayback) );

	// Playback aus dem Sampler entfernen und freigeben
	m_mPlaybacks.erase(it);
	delete pPlayback;

	m_csSlotbuf.leave();
}

void ITASoundSamplerImpl::RemovePlaybacksOfTrack(int iTrackID) {
	m_csTracks.enter();
	m_csSlotbuf.enter();

	TrackMap::iterator it = m_mTracks.find(iTrackID);
	if (it == m_mTracks.end()) {
		m_csSlotbuf.leave();
		m_csTracks.leave();
		ITA_EXCEPT1(INVALID_PARAMETER, "Invalid track ID");
	}

	m_sbSlotbuf.DeletePlaybacksOfTrack(iTrackID);

	m_csSlotbuf.leave();
	m_csTracks.leave();
}

void ITASoundSamplerImpl::RemoveAllPlaybacks() {
	m_csSlotbuf.enter();

	// Den Slotbuffer vollstndig leeren

	// Alle Playbacks verwerfen
	for (PlaybackMap::iterator it=m_mPlaybacks.begin(); it!=m_mPlaybacks.end(); ++it) {
		it->second->pSample->RemovePlaybackReference();
		delete it->second;
	}
	m_mPlaybacks.clear();

	m_csSlotbuf.leave();
}

bool ITASoundSamplerImpl::IsPlaybackPaused(int iPlaybackID) {
	m_csSlotbuf.enter();
	PlaybackMap::iterator it = m_mPlaybacks.find(iPlaybackID);
	if (it == m_mPlaybacks.end()) {
		// Playback-ID ist nicht (mehr) gltig
		m_csSlotbuf.leave();
		return false;
	}

	//bool bResult = it->second->bPauseStateNext;
	bool bResult = ((it->second->iPauseState == 2) || (it->second->iPauseState == 3));
	m_csSlotbuf.leave();

	return bResult;
}

void ITASoundSamplerImpl::PausePlayback(int iPlaybackID) {
	SetPlaybackPaused(iPlaybackID, true);
}

void ITASoundSamplerImpl::ResumePlayback(int iPlaybackID) {
	SetPlaybackPaused(iPlaybackID, false);
}

void ITASoundSamplerImpl::SetPlaybackPaused(int iPlaybackID, bool bPaused) {
	m_csSlotbuf.enter();
	PlaybackMap::iterator it = m_mPlaybacks.find(iPlaybackID);
	if (it == m_mPlaybacks.end()) {
		// Playback-ID ist nicht (mehr) gltig
		m_csSlotbuf.leave();
		// TODO: Error-Handling? Ausnahme?
		return;
	}

	/*
	if (it->second->bPauseStateNext == bPaused) {
		// Keine Zustandsnderung
		m_csSlotbuf.leave();
		return;
	}

	// Immer den nchsten Zustand zurckgeben, auf wenn dieser noch nicht bernommen wurde
	it->second->bPauseStateNext = bPaused;
	*/

	ITASoundSamplerPlayback* pPlayback = it->second;
	int iRemainSamples = pPlayback->pSample->GetLength() - pPlayback->iSamplePosition;

	// Blend-Modus (0 = Normale Wiedergabe, 1 = Ausblenden, 2 = Pausiert, 3 = Einblenden)
	switch (pPlayback->iPauseState) {
	case 0:	
		if (bPaused) {
			// Fall: Normale Wiedergabe. Jetzt pausieren.
			pPlayback->iPauseState = 1;
			pPlayback->iPauseFadeOffset = 0;
			// Blendlnge maximal so lang wie Samples verbleiben!
			pPlayback->iPauseFadeLength = (std::min)(DEFAULT_PAUSE_FADE_LENGTH, iRemainSamples);
		}
		break;

	case 1:
		if (!bPaused) {
			// Fall: Ausblenden fr Pause im Gange. Jetzt Pause wieder aufheben.
			pPlayback->iPauseState = 3;
			pPlayback->iPauseFadeOffset = pPlayback->iPauseFadeLength - pPlayback->iPauseFadeOffset;
		}
		break;

	case 2:
		if (!bPaused) {
			// Fall: Pause aktiv. Jetzt Pause aufheben.
			pPlayback->iPauseState = 3;
			pPlayback->iPauseFadeOffset = 0;
			// Blendlnge maximal so lang wie Samples verbleiben!
			pPlayback->iPauseFadeLength = (std::min)(DEFAULT_PAUSE_FADE_LENGTH, iRemainSamples);
		}
		break;

	case 3:
		if (bPaused) {
			// Fall: Pause wird gerade aufgehoben. Jetzt doch wieder pausieren.
			
			// Hinweis: Fade-Lnge usw. bleiben wie sie sind. Nur die Fade-Richtung wird umgekehrt.
			pPlayback->iPauseState = 1;
			pPlayback->iPauseFadeOffset = pPlayback->iPauseFadeLength - pPlayback->iPauseFadeOffset;
		}
		break;
	};

/*
	if (pPlayback->iPauseFadeOffset == pPlayback->iPauseFadeLength) {
		// Kein laufendes Fading im Gange
		pPlayback->iPauseFadeMode = (bPaused ? 0 : 1);
		pPlayback->iPauseFadeOffset = 0;
		pPlayback->iPauseFadeLength = DEFAULT_PAUSE_FADE_LENGTH;
	} else {
		// Laufendes Fading im Gange + nderung von Ein- zu Ausblenden bzw. umgekehrt

		// Blendrichtung umkehren
		pPlayback->iPauseFadeMode = 1-pPlayback->iPauseFadeMode;

		// Hinweis: Blendoffset und Lnge bleiben wie sie sind
	}
*/
	
	m_csSlotbuf.leave();
}

int ITASoundSamplerImpl::AddEvent(double dTimecode, ITASoundSamplerEventHandler* pHandler, int iTag, void* pParam) {
	// TODO: Implement
	return -1;
}

void ITASoundSamplerImpl::RemoveEvent(int iEventID) {
	// TODO: Implement
}

void ITASoundSamplerImpl::Print() {
	// Spuren
	if (m_mTracks.empty())
		printf("Keine Spuren angelegt\n\n");
	else {
		printf("%d Spuren angelegt:\n\n", m_mTracks.size());
		for (TrackMap::iterator it=m_mTracks.begin(); it!=m_mTracks.end(); ++it) {
			Track& track = it->second;
			printf("\t[%d] ", it->first);
			if (!track.sName.empty()) printf("\"%s\" ", track.sName.c_str());

			std::string sGains;
			for (int i=0; i<track.iChannels; i++) {
				if (i>0) sGains += ", ";
				sGains += ratio_to_db20_str(track.vdChannelGains[i]);
			}

			printf("(%d Ch, Gain %s, Routing <%s>, ChGains <%s>)\n",
				   track.iChannels, ratio_to_db20_str(track.dGain).c_str(),
				   IntVecToString(track.viChannelRoutings).c_str(), sGains.c_str());
		}
		printf("\n");
	}

/*	// Samples
	if (m_mSamples.empty())
		printf("Keine Samples geladen\n\n");
	else {
		printf("%d Samples geladen:\n\n", m_mSamples.size());
		for (SampleMap::iterator it=m_mSamples.begin(); it!=m_mSamples.end(); ++it)
			printf("\t[%d] \"%s\" (%d Kanaele, %d Samples = %0.3fs)\n",
				   it->first, it->second->GetFilename().c_str(), it->second->GetNumberOfChannels(),
				   it->second->GetLength(), (double) it->second->GetLength() / ITADatasourceRealization::m_dSamplerate);
		printf("\n");
	}
*/
}

unsigned int ITASoundSamplerImpl::GetBlocklength() const {
	return ITADatasourceRealization::GetBlocklength();
}

unsigned int ITASoundSamplerImpl::GetNumberOfChannels() const {
	return ITADatasourceRealization::GetNumberOfChannels();
}

644
645
double ITASoundSamplerImpl::GetSampleRate() const {
	return ITADatasourceRealization::GetSampleRate();
Jonas Stienen's avatar
Jonas Stienen committed
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
}

const float* ITASoundSamplerImpl::GetBlockPointer(unsigned int uiChannel, const ITAStreamInfo* pStreamInfo) {
	if (m_bProcess) {
		RenderOutputBlock();
		m_bProcess = false;
	}

	return ITADatasourceRealization::GetBlockPointer(uiChannel, pStreamInfo);
}

void ITASoundSamplerImpl::IncrementBlockPointer() {
	m_bProcess = true;
	ITADatasourceRealization::IncrementBlockPointer();
}

void ITASoundSamplerImpl::RenderOutputBlock() {
	// Alle Resourcen sperren
	m_csTracks.enter();
	m_csSlotbuf.enter();

	// Ausgabepuffer aller Kanle Null setzen
	for (unsigned int i=0; i<ITADatasourceRealization::m_uiChannels; i++)
		memset(GetWritePointer(i), 0, ITADatasourceRealization::m_uiBlocklength * sizeof(float));

	// Aktuellen Wiedergabeslot (sowie nchsten) holen
	ITASoundSamplerSlot* m_pCurrentSlot = m_sbSlotbuf.GetSlot(m_iCurrentBlockIndex);
	ITASoundSamplerSlot* m_pNextSlot = m_sbSlotbuf.GetSlot(m_iCurrentBlockIndex+1);

	// Alle Playbacks im Slot bearbeiten
	std::vector<ITASoundSamplerPlayback*>& vpPlaybacks = m_pCurrentSlot->m_vPlaybacks;
	while (!vpPlaybacks.empty()) {

		ITASoundSamplerPlayback* playback = vpPlaybacks.front();

		// Pausierungstatus bernehmen
		//playback->bPauseStateCurrent = playback->bPauseStateNext;
		
		//bool bStartPause = !playback->bPauseStateCurrent && playback->bPauseStateNext;
		//bool bStopPause = playback->bPauseStateCurrent && !playback->bPauseStateNext;
		//bool bTogglePauseState = bStartPause || bStopPause;

		// Neuen Pausierungstatus bernehmen
		//playback->bPauseStateCurrent = playback->bPauseStateNext;

		// Playback nicht pausiert bzw. nderung des Pausestatus -> (Teilweise) Wiedergabe.

		//
		//int iPauseFadeLength = 0;
		//if (bTogglePauseState) {
		//	/*
		//	 *  Sanftes Ein-/Ausblenden des Playbacks
		//	 *  Maximale Blendlnge beachten (verbleibende Samples insgesamt bzw. in diesem Block )
		//	 */

		//	iPauseFadeLength = (std::min)(DEFAULT_PAUSE_FADE_LENGTH, iRemainSamples);
		//	iPauseFadeLength = (std::min)(iPauseFadeLength, (signed) ITADatasourceRealization::m_uiBlocklength - playback->iPlaybackOffset);

		//	printf("%d\n", iPauseFadeLength);

		//	// Nur beim einsetzen der Pause die Wiedergabe verkrzen
		//	if (bStartPause) iNumSamples = iPauseFadeLength;
		//}

		if (playback->iPauseState != 2) {
			// Fall: Normales Playback, Pausierung setzt ein bzw. hrt auf

			Track& track = m_mTracks[playback->iTrackID];
			double dOuterGain = m_dMasterGain * track.dGain * playback->dGain;
			int iRemainSamples = playback->pSample->GetLength() - playback->iSamplePosition;
			int iFadeRemain = playback->iPauseFadeLength - playback->iPauseFadeOffset;
			int iNumSamples = (std::min)( iRemainSamples,
									      (signed) ITADatasourceRealization::m_uiBlocklength - playback->iPlaybackOffset );
			
			// Im Falle des Ausblendens die Anzahl Samples zustzlich auf die Blendlnge verkrzen
			if (playback->iPauseState == 1) {
				iNumSamples = (std::min)(iNumSamples, iFadeRemain);
			}

			// ber alle Spurkanle des Playbacks iterieren
			for (int i=0; i<track.iChannels; i++) {
				int iOutputChannel = track.viChannelRoutings[i];
				float* pfOutputData = GetWritePointer((unsigned) iOutputChannel);
				const float* pfInputData = playback->pSample->GetChannelData(i);
							
				// Daten einmischen
				float fGain = (float) (dOuterGain * track.vdChannelGains[i]);

				for (int j=0; j<iNumSamples; j++)
					pfOutputData[playback->iPlaybackOffset + j] += pfInputData[playback->iSamplePosition + j] * fGain;

				if (playback->iPauseState == 1) {
					// Fall: Ausblenden
					Fade(pfOutputData + playback->iPlaybackOffset,
						 playback->iPauseFadeLength, ITA_FADE_OUT | ITA_FADE_LINEAR,
						 playback->iPauseFadeOffset, iNumSamples);
					playback->iPauseFadeOffset += iNumSamples;
				}

				if (playback->iPauseState == 3) {
					// Fall: Einblenden
					Fade(pfOutputData + playback->iPlaybackOffset,
						 playback->iPauseFadeLength, ITA_FADE_IN | ITA_FADE_LINEAR,
						 playback->iPauseFadeOffset, iNumSamples);
					playback->iPauseFadeOffset += iNumSamples;					
				}

				//if (bStartPause) fade(pfOutputData + playback->iPlaybackOffset, iPauseFadeLength, ITA_FADE_OUT | ITA_FADE_COSINE_SQUARE);
				//if (bStopPause) fade(pfOutputData + playback->iPlaybackOffset + iNumSamples - iPauseFadeLength, iPauseFadeLength, ITA_FADE_IN | ITA_FADE_COSINE_SQUARE);
			}

			// Flags des Playbacks aktualisieren
			playback->iPlaybackPosition += iNumSamples;
			playback->iPlaybackBlock = playback->iPlaybackPosition / (signed) ITADatasourceRealization::m_uiBlocklength;
			playback->iPlaybackOffset = playback->iPlaybackPosition % (signed) ITADatasourceRealization::m_uiBlocklength;
			playback->iSamplePosition += iNumSamples;

			// Fading beenden
			if (playback->iPauseFadeOffset >= playback->iPauseFadeLength) {
				if (playback->iPauseState == 1) playback->iPauseState = 2;
				if (playback->iPauseState == 3) playback->iPauseState = 0;

				playback->iPauseFadeOffset = playback->iPauseFadeLength = 0;
			}
		} else {
			// Fall: Pause im Gange. Kein Fading mehr.
			
			// Playback einfach einen Block weiterschieben
			playback->iPlaybackBlock++;
		}


		
	//
	//} else {
	//		// Wiedergabe einfach um einen Slot weiterschieben
	//		playback->iPlaybackPosition += (signed) ITADatasourceRealization::m_uiBlocklength;
	//		playback->iPlaybackBlock++;
	//	}

		vpPlaybacks.erase(vpPlaybacks.begin());
		if (playback->iSamplePosition < playback->pSample->GetLength()) {
			// Noch nicht fertig abgespieltes Playback in den nchstfolgenden Slot platzieren
			m_pNextSlot->m_vPlaybacks.push_back(playback);
		} else {
			// Playback kann gelscht werden -> Referenz auf dem Sample entfernen
			playback->pSample->RemovePlaybackReference();
		}
	}

	ITADatasourceRealization::IncrementWritePointer();
	m_sbSlotbuf.IncrementPlaybackBlock();
	m_iCurrentBlockIndex++;

	// Alle Resourcen freigeben
	m_csSlotbuf.leave();
	m_csTracks.leave();

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
804
}