This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioLoop.cpp
472 lines (416 loc) · 9.31 KB
/
AudioLoop.cpp
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
#include "audioloop.h"
int AudioLoop::maxnumberofrepeats = 0;
AudioLoop::AudioLoop()
{
this->init();
}
AudioLoop::~AudioLoop()
{
}
AudioLoop::AudioLoop(char *filename, int maxnumberofrepeats, int crossfadetime, std::vector<int> breakpoints, FMOD::System *system)
{
this->init();
setdata(filename, maxnumberofrepeats, crossfadetime, breakpoints, system);
}
FMOD::Sound* AudioLoop::LoadSound(FMOD::System *sys)
{
//if (!sound)
//{
result = sys->createStream(filename, /*FMOD_NONBLOCKING*/FMOD_DEFAULT, 0, &sound);
ERRCHECK(result);
//}
return sound;
}
void AudioLoop::init()
{
strcpy(filename, "");
maxnumberofrepeats = 0;
channel = 0;
numberoftimesplayed = 0;
channelid = -1;
channelcrossfadetime = 44100;
channelpaused = false;
beentroughCrossFadeOnce = false;
beentroughCurrentUpdate = false;
sound = 0;
channel = 0;
//breakpoint = 0;
//beentroughLoadSound = false;
//soundloaded = false;
//soundavailable = false;
//channelavailable = false;
percentageloaded = 0;
delaytime = 44100;/*176400; // 4 seconds @ 44100 SR!*/
currentlyplaying = false;
_SETTEST = 0;
}
void AudioLoop::setdata(char *newfilename, int maxnumberofrepeats, int crossfadetime, std::vector<int> breakpoints,FMOD::System *system)
{
strcpy(filename, newfilename);
if (_SETTEST)
{
std::cout << "FILENAME!: " << newfilename << std::endl;
std::cout << "AudioLoop::setdata(char *newfilename, int maxnumberofrepeats, int crossfadetime, std::vector<int> breakpoints, FMOD::System *system) has filename: " << filename << std::endl;
}
this->maxnumberofrepeats = maxnumberofrepeats;
this->channelcrossfadetime = crossfadetime;
this->breakpoints = breakpoints;
this->sys = system;
}
int AudioLoop::getCurrentLocationReference()
{
if (!beentroughCurrentUpdate)
{
unsigned int position, length = 0;
result = channel->getPosition(&position, FMOD_TIMEUNIT_PCM);
ERRCHECK(result);
result = sound->getLength(&length, FMOD_TIMEUNIT_PCM);
ERRCHECK(result);
for (std::vector<int>::const_iterator i = breakpoints.begin(); i != breakpoints.end(); i++)
{
if (((unsigned int)*i) > (position + delaytime)) {
beentroughCurrentUpdate = true;
return *i;
}
}
beentroughCurrentUpdate = true;
return length;
}
return NULL;
}
int AudioLoop::updatePosition(AudioLoop *otherfile, unsigned int breakpoint)
{
unsigned int position = 1;
result = channel->getPosition(&position, FMOD_TIMEUNIT_PCM);
ERRCHECK(result);
//FMOD::System *tempsys;
FMOD_OPENSTATE state = FMOD_OPENSTATE_LOADING;
if(otherfile->getSound())
{
result = otherfile->getSound()->getOpenState(&state, 0,0);
ERRCHECK(result);
}
if (position >= breakpoint)
{
if (!beentroughCrossFadeOnce)
{
if (_SETTEST)
{
std::cout << "Position for crossfading reached/passed!" << std::endl;
}
}
return Crossfade(otherfile);
}
unsigned int length = 0;
result = sound->getLength(&length, FMOD_TIMEUNIT_PCM);
ERRCHECK(result);
if (position >= (length - (delaytime*2)))
{
/*if (otherfile)
{
return Crossfade(otherfile);
}
return Crossfade(NULL);*/
return 3;
}
return false;
}
FMOD::Channel* AudioLoop::getChannel()
{
return channel;
}
FMOD::Sound* AudioLoop::getSound()
{
if (sound)
{
return sound;
}
return LoadSound(sys);
if (_SETTEST)
{
std:: cerr << "There is no Sound available. Use AudioLoop::LoadSound(FMOD::System *sys) to load a sound." << std::endl;
}
return NULL;
}
bool AudioLoop::Crossfade(AudioLoop *otherfile)
{
float channelvolume;
bool b;
if (this != otherfile) // this is very dirty code, but it seemed that files which will be played
{ // recursive are not being crossfaded correctly.
if (!beentroughCrossFadeOnce)
{
setChannelVolume(1.0);
otherfile->Play(this);
//otherfile->setChannelPaused(false);
beentroughCrossFadeOnce = true;
numberoftimesplayed++;
}
result = channel->getVolume(&channelvolume);
ERRCHECK(result);
channelvolume *= (float)0.9;
if (_SETTEST)
{
std::cout << "Volume van spelend file: " << channelvolume << std::endl;
}
result = channel->setVolume(channelvolume);
ERRCHECK(result);
otherfile->setChannelVolume((float)1.0 - channelvolume);
if (_SETTEST)
{
std::cout << "Volume van pending file: " << otherfile->getChannelVolume() << std::endl;
}
if (b = (getChannelVolume() <= 0.01))
{
otherfile->setChannelVolume(1.0);
Singleton::setCurrentlyPlaying(otherfile);
channel->setPaused(true);
this->Stop();
}
return b;
}
else if (this == otherfile)
{
FMOD::Sound *tempsnd;
FMOD::Channel *tempchan;
FMOD_OPENSTATE tempstate;
tempsnd = otherfile->getSound();
//result = sys->createSound(otherfile->getFileName(), FMOD_NONBLOCKING, 0, &tempsnd);
//ERRCHECK(result);
result = sys->playSound(FMOD_CHANNEL_FREE, tempsnd, false, &tempchan);
ERRCHECK(result);
if (b = (getChannelVolume() <= 0.01))
{
result = tempchan->setVolume(1.0);
channel->setPaused(true);
this->Stop();
this->setChannel(tempchan);
}
}
}
bool AudioLoop::getPausedChannel()
{
result = channel->getPaused(&channelpaused);
ERRCHECK(result);
return channelpaused;
}
bool AudioLoop::getbeentroughCrossFadeOnce()
{
return beentroughCrossFadeOnce;
}
bool AudioLoop::getbeentroughCurrentUpdate()
{
return beentroughCurrentUpdate;
}
float AudioLoop::getChannelVolume()
{
if (channel) {
float volume;
result = channel->getVolume(&volume);
ERRCHECK(result);
return volume;
}
return 0.0;
}
char* AudioLoop::getFileName()
{
return filename;
}
bool AudioLoop::getSoundLoaded()
{
FMOD_OPENSTATE status;
if (sound)
{
result = sound->getOpenState(&status, &percentageloaded, 0);
ERRCHECK(result);
if (status == FMOD_OPENSTATE_READY)
{
return true;
}
}
return false;
}
int AudioLoop::getPercentageLoaded()
{
FMOD_OPENSTATE status;
result = this->sound->getOpenState(&status, &percentageloaded, 0);
ERRCHECK(result);
return percentageloaded;
}
int AudioLoop::getNumberoftimesplayed()
{
return numberoftimesplayed;
}
bool AudioLoop::getRecentlyPlayed()
{
return recentlyplayed;
}
int AudioLoop::getNumbermaxtimeplay()
{
return maxnumberofrepeats;
}
int AudioLoop::getDelayTime()
{
return delaytime;
}
void AudioLoop::setbeentroughCrossFadeOnce(bool truefalse)
{
beentroughCrossFadeOnce = truefalse;
}
void AudioLoop::setbeentroughCurrentUpdate(bool truefalse)
{
beentroughCurrentUpdate = truefalse;
}
void AudioLoop::setChannelPaused(bool truefalse)
{
result = channel->setPaused(truefalse);
ERRCHECK(result);
}
void AudioLoop::setChannelVolume(float newvolume)
{
result = channel->setVolume(newvolume);
ERRCHECK(result);
}
void AudioLoop::setChannel(FMOD::Channel *newchan)
{
channel = newchan;
if (sound && channel)
{
recentlyplayed = true;
}
}
void AudioLoop::addNumberoftimeplayed()
{
numberoftimesplayed++;
}
void AudioLoop::addNumberoftimeplayed(int addition)
{
numberoftimesplayed += addition;
}
void AudioLoop::setRecentlyPlayed(bool truefalse)
{
if (truefalse != recentlyplayed)
{
recentlyplayed = truefalse;
}
}
void AudioLoop::setDelaytime(int newdelaytime)
{
delaytime = newdelaytime;
}
void AudioLoop::Play(AudioLoop *currentlyPlaying)
{
int getchanplaying;
if (!channel)
{
FMOD_OPENSTATE status;
if (!sound)
{
LoadSound(sys);
}
if (getChannelsPlaying() > 1)
{
unsigned int breakpoint = 0;
if (currentlyPlaying)
{
if (!currentlyPlaying->getbeentroughCurrentUpdate())
{
breakpoint = currentlyPlaying->getCurrentLocationReference();
}
if (currentlyPlaying->updatePosition(this, breakpoint))
{
currentlyPlaying->Stop();
}
}
}
else if(getChannelsPlaying() == -1)
{
std::cout << "Cannot retrieve Channels loading!" << std::endl;
}
else
{
if (getSoundLoaded())
{
result = sys->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);
ERRCHECK(result);
currentlyplaying = true;
}
else
{
if (_SETTEST)
{
std::cout << "percentage loaded: " << getPercentageLoaded() << std::endl;
}
}
}
}
else if(sound && channel)
{
result = channel->setPaused(false);
ERRCHECK(result);
currentlyplaying = true;
Singleton::setCurrentlyPlaying(this);
}
else
{
if (_SETTEST)
{
std:: cerr << "Can't play atm check your flow of start - end" << std::endl;
}
}
}
void AudioLoop::Stop()
{
if (channel)
{
beentroughCrossFadeOnce = false;
beentroughCurrentUpdate = false;
result = channel->setPaused(true);
ERRCHECK(result);
result = channel->stop();
ERRCHECK(result);
result = sound->release();
ERRCHECK(result);
channel = 0;
currentlyplaying = false;
}
else
{
if(_SETTEST)
{
std::cout << "Error can't find channel, this AudioLoop is not playing yet!" << std::endl;
}
}
}
int AudioLoop::getChannelsPlaying()
{
FMOD::System *sys;
int nochan;
if (sound)
{
result = sound->getSystemObject(&sys);
ERRCHECK(result);
result = sys->getChannelsPlaying(&nochan);
ERRCHECK(result);
return nochan;
}
return -1;
}
bool AudioLoop::getCurrentlyPlaying()
{
return currentlyplaying;
}
int AudioLoop::getSoundLength()
{
unsigned int length = 0;
result = sound->getLength(&length, FMOD_TIMEUNIT_PCM);
ERRCHECK(result);
return length;
}
int AudioLoop::getCurrentPosition()
{
unsigned int position = 0;
result = channel->getPosition(&position, FMOD_TIMEUNIT_PCM);
ERRCHECK(result);
return position;
}