Audio Scol plugin 1.0
Audio Scol plugin based on CAudio library
audio.cpp
1
2#include <scolPlugin.h>
3#include "audio.h"
4
5extern int AUDIO_END_CB;
6
7#include <cstring>
8#include <thread>
9
10cScolFileSource::cScolFileSource(const char* filename) : Valid(false), pFile(NULL), Filesize(0)
11{
12 cAudio::cAudioString safeFilename = cAudio::fromUTF8(filename);
13 if (safeFilename.length() != 0)
14 {
15 pFile = fopen(cAudio::toUTF8(safeFilename), "rb");
16 if (pFile)
17 {
18 Valid = true;
19 }
20 }
21
22 if (Valid)
23 {
24 fseek(pFile, 0, SEEK_END);
25 Filesize = ftell(pFile);
26 fseek(pFile, 0, SEEK_SET);
27 }
28}
29
30cScolFileSource::~cScolFileSource()
31{
32 if (pFile)
33 fclose(pFile);
34}
35
36bool cScolFileSource::isValid()
37{
38 return Valid;
39}
40
41int cScolFileSource::getCurrentPos()
42{
43 return ftell(pFile);
44}
45
46int cScolFileSource::getSize()
47{
48 return Filesize;
49}
50
51int cScolFileSource::read(void* output, int size)
52{
53 return fread(output, sizeof(char), size, pFile);
54}
55
56bool cScolFileSource::seek(int amount, bool relative)
57{
58 if (relative == true)
59 {
60 int oldamount = ftell(pFile);
61 fseek(pFile, amount, SEEK_CUR);
62
63 //check against the absolute position
64 if (oldamount + amount != ftell(pFile))
65 return false;
66 }
67 else
68 {
69 fseek(pFile, amount, SEEK_SET);
70 if (amount != ftell(pFile))
71 return false;
72 }
73
74 return true;
75}
76
77SAudioManager::SAudioManager()
78{
79 // create cAudio manager
80 mPaused = false;
81 scolDataFactory = new cScolFileSourceFactory();
82 manager = cAudio::createAudioManager(true);
83 manager->registerDataSource(scolDataFactory, "ScolFileSystem", 100);
84 manager->registerEventHandler(this);
85
86#if CAUDIO_EFX_ENABLED == 1
87 mEffect = 0;
88 mFilter = 0;
89
90 mFilterType = cAudio::EFT_NULL;
91 mFilterLowFreqVolume = 0.1f;
92 mFilterHighFreqVolume = 0.1f;
93 mEffectType = cAudio::EET_NULL;
94
95 InitEffects();
96#endif
97 //effects presets
98 /*
99 cAudio::IAudioEffects* effects = manager->getEffects();
100 if (effects)
101 {
102 if (effects->isEffectSupported(cAudio::EET_REVERB))
103 {
104 cAudio::sReverbParameters param;
105 // bathroom
106 param.Gain = 0.316f;
107 param.GainHF = 0.251f;
108 param.Density = 0.17f;
109 param.DecayHFRatio = 0.54f;
110 param.ReflectionsGain = 0.653f;
111 param.ReflectionsDelay = 0.01f;
112 param.LateReverbDelay = 0.01f;
113 param.LateReverbGain = 3.273f;
114
115 effects->addReverbEffectPreset("Default", param);
116 }
117
118 if (effects->isEffectSupported(cAudio::EET_AUTOWAH))
119 {
120 cAudio::sAutowahParameters param;
121 effects->addAutowahEffectPreset("Default", param);
122 }
123
124 if (effects->isEffectSupported(cAudio::EET_CHORUS))
125 {
126 cAudio::sChorusParameters param;
127 effects->addChorusEffectPreset("Default", param);
128 }
129
130 if (effects->isEffectSupported(cAudio::EET_COMPRESSOR))
131 {
132 cAudio::sCompressorParameters param;
133 effects->addCompressorEffectPreset("Default", param);
134 }
135
136 if (effects->isEffectSupported(cAudio::EET_DISTORTION))
137 {
138 cAudio::sDistortionParameters param;
139 effects->addDistortionEffectPreset("Default", param);
140 }
141
142 if (effects->isEffectSupported(cAudio::EET_ECHO))
143 {
144 cAudio::sEchoParameters param;
145 effects->addEchoEffectPreset("Default", param);
146 }
147
148 if (effects->isEffectSupported(cAudio::EET_EQUALIZER))
149 {
150 cAudio::sEqualizerParameters param;
151 effects->addEqualizerEffectPreset("Default", param);
152 }
153
154 if (effects->isEffectSupported(cAudio::EET_FLANGER))
155 {
156 cAudio::sFlangerParameters param;
157 effects->addFlangerEffectPreset("Default", param);
158 }
159
160 if (effects->isEffectSupported(cAudio::EET_FREQUENCY_SHIFTER))
161 {
162 cAudio::sFrequencyShiftParameters param;
163 effects->addFrequencyShiftEffectPreset("Default", param);
164 }
165
166 if (effects->isEffectSupported(cAudio::EET_PITCH_SHIFTER))
167 {
168 cAudio::sPitchShifterParameters param;
169 effects->addPitchShifterEffectPreset("Default", param);
170 }
171
172 if (effects->isEffectSupported(cAudio::EET_RING_MODULATOR))
173 {
174 cAudio::sRingModulatorParameters param;
175 effects->addRingModulatorEffectPreset("Default", param);
176 }
177
178 if (effects->isEffectSupported(cAudio::EET_VOCAL_MORPHER))
179 {
180 cAudio::sVocalMorpherParameters param;
181 effects->addVocalMorpherEffectPreset("Default", param);
182 }
183 }
184 */
185}
186
187#if CAUDIO_EFX_ENABLED == 1
188void SAudioManager::InitEffects()
189{
190 cAudio::IAudioEffects* effects = manager->getEffects();
191 if (effects)
192 {
193 mEffect = effects->createEffect();
194 mFilter = effects->createFilter();
195
196 if (mEffect && mEffect->isValid() && mFilter && mFilter->isValid())
197 {
198 mFilter->setHighFrequencyVolume(mFilterHighFreqVolume);
199 mFilter->setLowFrequencyVolume(mFilterLowFreqVolume);
200
201 //mSound->attachFilter(mFilter);
202 //mSound->attachEffect(0, mEffect);
203
204 /*
205 if (effects->isEffectPresetRegistered(cAudio::EET_AUTOWAH, "Default"))
206 mEffect->setAutowahParameters(effects->getAutowahEffectPreset("Default"));
207
208 if (effects->isEffectPresetRegistered(cAudio::EET_CHORUS, "Default"))
209 mEffect->setChorusParameters(effects->getChorusEffectPreset("Default"));
210
211 if (effects->isEffectPresetRegistered(cAudio::EET_COMPRESSOR, "Default"))
212 mEffect->setCompressorParameters(effects->getCompressorEffectPreset("Default"));
213
214 if (effects->isEffectPresetRegistered(cAudio::EET_DISTORTION, "Default"))
215 mEffect->setDistortionParameters(effects->getDistortionEffectPreset("Default"));
216
217 if (effects->isEffectPresetRegistered(cAudio::EET_EAX_REVERB, "Psychotic"))
218 mEffect->setEAXReverbParameters(effects->getEAXReverbEffectPreset("Psychotic"));
219
220 if (effects->isEffectPresetRegistered(cAudio::EET_ECHO, "Default"))
221 mEffect->setEchoParameters(effects->getEchoEffectPreset("Default"));
222
223 if (effects->isEffectPresetRegistered(cAudio::EET_EQUALIZER, "Default"))
224 mEffect->setEqualizerParameters(effects->getEqualizerEffectPreset("Default"));
225
226 if (effects->isEffectPresetRegistered(cAudio::EET_FLANGER, "Default"))
227 mEffect->setFlangerParameters(effects->getFlangerEffectPreset("Default"));
228
229 if (effects->isEffectPresetRegistered(cAudio::EET_FREQUENCY_SHIFTER, "Default"))
230 mEffect->setFrequencyShiftParameters(effects->getFrequencyShiftEffectPreset("Default"));
231
232 if (effects->isEffectPresetRegistered(cAudio::EET_PITCH_SHIFTER, "Default"))
233 mEffect->setPitchShifterParameters(effects->getPitchShifterEffectPreset("Default"));
234
235 if (effects->isEffectPresetRegistered(cAudio::EET_REVERB, "Default"))
236 mEffect->setReverbParameters(effects->getReverbEffectPreset("Default"));
237
238 if (effects->isEffectPresetRegistered(cAudio::EET_RING_MODULATOR, "Default"))
239 mEffect->setRingModulatorParameters(effects->getRingModulatorEffectPreset("Default"));
240
241 if (effects->isEffectPresetRegistered(cAudio::EET_VOCAL_MORPHER, "Default"))
242 mEffect->setVocalMorpherParameters(effects->getVocalMorpherEffectPreset("Default"));
243 */
244 }
245 }
246}
247#endif
248
249SAudioManager::~SAudioManager()
250{
251 // destroy cAudio manager
252 if (manager != 0)
253 {
254 validInput.clear();
255 validSounds.clear();
256
257 SAFE_DELETE(scolDataFactory);
258 manager->unRegisterDataSource("ScolFileSystem");
259 manager->unRegisterEventHandler(this);
260
261 /*
262 cAudio::IAudioEffects* effects = manager->getEffects();
263 if (effects)
264 {
265 if (effects->isEffectSupported(cAudio::EET_REVERB))
266 effects->removeEffectPreset(cAudio::EET_REVERB, "Default");
267
268 if (effects->isEffectSupported(cAudio::EET_AUTOWAH))
269 effects->removeEffectPreset(cAudio::EET_AUTOWAH, "Default");
270
271 if (effects->isEffectSupported(cAudio::EET_CHORUS))
272 effects->removeEffectPreset(cAudio::EET_CHORUS, "Default");
273
274 if (effects->isEffectSupported(cAudio::EET_COMPRESSOR))
275 effects->removeEffectPreset(cAudio::EET_COMPRESSOR, "Default");
276
277 if (effects->isEffectSupported(cAudio::EET_DISTORTION))
278 effects->removeEffectPreset(cAudio::EET_DISTORTION, "Default");
279
280 if (effects->isEffectSupported(cAudio::EET_ECHO))
281 effects->removeEffectPreset(cAudio::EET_ECHO, "Default");
282
283 if (effects->isEffectSupported(cAudio::EET_EQUALIZER))
284 effects->removeEffectPreset(cAudio::EET_EQUALIZER, "Default");
285
286 if (effects->isEffectSupported(cAudio::EET_FLANGER))
287 effects->removeEffectPreset(cAudio::EET_FLANGER, "Default");
288
289 if (effects->isEffectSupported(cAudio::EET_FREQUENCY_SHIFTER))
290 effects->removeEffectPreset(cAudio::EET_FREQUENCY_SHIFTER, "Default");
291
292 if (effects->isEffectSupported(cAudio::EET_PITCH_SHIFTER))
293 effects->removeEffectPreset(cAudio::EET_PITCH_SHIFTER, "Default");
294
295 if (effects->isEffectSupported(cAudio::EET_RING_MODULATOR))
296 effects->removeEffectPreset(cAudio::EET_RING_MODULATOR, "Default");
297
298 if (effects->isEffectSupported(cAudio::EET_VOCAL_MORPHER))
299 effects->removeEffectPreset(cAudio::EET_VOCAL_MORPHER, "Default");
300 }*/
301
302
303#if CAUDIO_EFX_ENABLED == 1
304 if (mEffect)
305 mEffect->drop();
306 if (mFilter)
307 mFilter->drop();
308#endif
309
310 cAudio::destroyAudioManager(manager);
311 manager = 0;
312 }
313}
314
315#if CAUDIO_EFX_ENABLED == 1
316bool SAudioManager::SetFilter(cAudio::FilterTypes type, float lowFreqVolume, float highFreqVolume)
317{
318 mFilterType = type;
319 mFilterLowFreqVolume = lowFreqVolume;
320 mFilterHighFreqVolume = highFreqVolume;
321
322 cAudio::IAudioEffects* effects = manager->getEffects();
323 if (mFilter && effects && effects->isFilterSupported(type))
324 {
325 mFilter->setType(type);
326 mFilter->setLowFrequencyVolume(lowFreqVolume);
327 mFilter->setHighFrequencyVolume(highFreqVolume);
328
329 return true;
330 }
331
332 if (mFilter)
333 mFilter->setType(cAudio::EFT_NULL);
334
335 return false;
336}
337
338bool SAudioManager::SetEffect(cAudio::EffectTypes type, std::string preset)
339{
340 mEffectType = type;
341 mEffectPreset = preset;
342
343 cAudio::IAudioEffects* effects = manager->getEffects();
344
345 if ((type == cAudio::EET_REVERB) && effects && effects->isEffectSupported(cAudio::EET_EAX_REVERB))
346 type = cAudio::EET_EAX_REVERB;
347
348 if (mEffect && effects && effects->isEffectSupported(type))
349 {
350 if (effects->isEffectPresetRegistered(type, preset.c_str()))
351 {
352 switch (type)
353 {
354 case cAudio::EET_AUTOWAH:
355 mEffect->setAutowahParameters(effects->getAutowahEffectPreset(preset.c_str()));
356 break;
357
358 case cAudio::EET_CHORUS:
359 mEffect->setChorusParameters(effects->getChorusEffectPreset(preset.c_str()));
360 break;
361
362 case cAudio::EET_COMPRESSOR:
363 mEffect->setCompressorParameters(effects->getCompressorEffectPreset(preset.c_str()));
364 break;
365
366 case cAudio::EET_DISTORTION:
367 mEffect->setDistortionParameters(effects->getDistortionEffectPreset(preset.c_str()));
368 break;
369
370 case cAudio::EET_EAX_REVERB:
371 mEffect->setEAXReverbParameters(effects->getEAXReverbEffectPreset(preset.c_str()));
372 break;
373
374 case cAudio::EET_ECHO:
375 mEffect->setEchoParameters(effects->getEchoEffectPreset(preset.c_str()));
376 break;
377
378 case cAudio::EET_EQUALIZER:
379 mEffect->setEqualizerParameters(effects->getEqualizerEffectPreset(preset.c_str()));
380 break;
381
382 case cAudio::EET_FLANGER:
383 mEffect->setFlangerParameters(effects->getFlangerEffectPreset(preset.c_str()));
384 break;
385
386 case cAudio::EET_FREQUENCY_SHIFTER:
387 mEffect->setFrequencyShiftParameters(effects->getFrequencyShiftEffectPreset(preset.c_str()));
388 break;
389
390 case cAudio::EET_PITCH_SHIFTER:
391 mEffect->setPitchShifterParameters(effects->getPitchShifterEffectPreset(preset.c_str()));
392 break;
393
394 case cAudio::EET_REVERB:
395 mEffect->setReverbParameters(effects->getReverbEffectPreset(preset.c_str()));
396 break;
397
398 case cAudio::EET_RING_MODULATOR:
399 mEffect->setRingModulatorParameters(effects->getRingModulatorEffectPreset(preset.c_str()));
400 break;
401
402 case cAudio::EET_VOCAL_MORPHER:
403 mEffect->setVocalMorpherParameters(effects->getVocalMorpherEffectPreset(preset.c_str()));
404 break;
405 }
406 }
407
408 mEffect->setType(type);
409 return true;
410 }
411
412 if (mEffect)
413 mEffect->setType(cAudio::EET_NULL);
414
415 return false;
416}
417
418bool SAudioManager::SetReverbEffectParam(cAudio::sReverbParameters param)
419{
420 cAudio::IAudioEffects* effects = manager->getEffects();
421 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_REVERB))
422 {
423 mEffect->setReverbParameters(param);
424
425 /*
426 cAudio::sReverbParameters param
427 param.Gain = gain; //0.316f;
428 param.GainHF = gainHF; //0.251f;
429 param.Density = density; //0.17f;
430 param.DecayHFRatio = decayHFRatio; //0.54f;
431 param.ReflectionsGain = reflectionsGain ; //0.653f;
432 param.ReflectionsDelay = reflectionsDelay; //0.01f;
433 param.LateReverbDelay = lateReverbDelay; //0.01f;
434 param.LateReverbGain = lateReverbGain; //3.273f;
435 */
436
437 return true;
438 }
439
440 return false;
441}
442
443bool SAudioManager::SetAutowahEffectParam(cAudio::sAutowahParameters param)
444{
445 cAudio::IAudioEffects* effects = manager->getEffects();
446 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_AUTOWAH))
447 {
448 mEffect->setAutowahParameters(param);
449 return true;
450 }
451
452 return false;
453}
454
455bool SAudioManager::SetChorusEffectParam(cAudio::sChorusParameters param)
456{
457 cAudio::IAudioEffects* effects = manager->getEffects();
458 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_CHORUS))
459 {
460 mEffect->setChorusParameters(param);
461 return true;
462 }
463
464 return false;
465}
466
467bool SAudioManager::SetCompressorEffectParam(cAudio::sCompressorParameters param)
468{
469 cAudio::IAudioEffects* effects = manager->getEffects();
470 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_COMPRESSOR))
471 {
472 mEffect->setCompressorParameters(param);
473 return true;
474 }
475
476 return false;
477}
478
479bool SAudioManager::SetDistortionEffectParam(cAudio::sDistortionParameters param)
480{
481 cAudio::IAudioEffects* effects = manager->getEffects();
482 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_DISTORTION))
483 {
484 mEffect->setDistortionParameters(param);
485 return true;
486 }
487
488 return false;
489}
490
491bool SAudioManager::SetEchoEffectParam(cAudio::sEchoParameters param)
492{
493 cAudio::IAudioEffects* effects = manager->getEffects();
494 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_ECHO))
495 {
496 mEffect->setEchoParameters(param);
497 return true;
498 }
499
500 return false;
501}
502
503bool SAudioManager::SetEqualizerEffectParam(cAudio::sEqualizerParameters param)
504{
505 cAudio::IAudioEffects* effects = manager->getEffects();
506 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_EQUALIZER))
507 {
508 mEffect->setEqualizerParameters(param);
509 return true;
510 }
511
512 return false;
513}
514
515bool SAudioManager::SetFlangerEffectParam(cAudio::sFlangerParameters param)
516{
517 cAudio::IAudioEffects* effects = manager->getEffects();
518 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_FLANGER))
519 {
520 mEffect->setFlangerParameters(param);
521 return true;
522 }
523
524 return false;
525}
526
527bool SAudioManager::SetFrequencyShiftEffectParam(cAudio::sFrequencyShiftParameters param)
528{
529 cAudio::IAudioEffects* effects = manager->getEffects();
530 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_FREQUENCY_SHIFTER))
531 {
532 mEffect->setFrequencyShiftParameters(param);
533 return true;
534 }
535
536 return false;
537}
538
539bool SAudioManager::SetPitchShifterEffectParam(cAudio::sPitchShifterParameters param)
540{
541 cAudio::IAudioEffects* effects = manager->getEffects();
542 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_PITCH_SHIFTER))
543 {
544 mEffect->setPitchShifterParameters(param);
545 return true;
546 }
547
548 return false;
549}
550
551bool SAudioManager::SetRingModulatorEffectParam(cAudio::sRingModulatorParameters param)
552{
553 cAudio::IAudioEffects* effects = manager->getEffects();
554 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_RING_MODULATOR))
555 {
556 mEffect->setRingModulatorParameters(param);
557 return true;
558 }
559
560 return false;
561}
562
563bool SAudioManager::SetVocalMorpherEffectParam(cAudio::sVocalMorpherParameters param)
564{
565 cAudio::IAudioEffects* effects = manager->getEffects();
566 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_VOCAL_MORPHER))
567 {
568 mEffect->setVocalMorpherParameters(param);
569 return true;
570 }
571
572 return false;
573}
574#endif // CAUDIO_EFX_ENABLED == 1
575
577{
578
579}
580
582{
583
584}
585
587{
588
589}
590
592{
593
594}
595
600
605
607{
608#if CAUDIO_EFX_ENABLED == 1
609 mEffect = 0;
610 mFilter = 0;
611
612 InitEffects();
613 SetFilter(mFilterType, mFilterLowFreqVolume, mFilterHighFreqVolume);
614 SetEffect(mEffectType, mEffectPreset);
615
616 for (std::set<SAudioSound*>::iterator it = validSounds.begin(); it != validSounds.end(); ++it)
617 {
618 SAudioSound* sound = (*it);
619 sound->SetEffectState(sound->mEffectState);
620 sound->SetFilterState(sound->mFilterState);
621 }
622#endif
623}
624
625SAudioManager* SAudioManager::GetInstance()
626{
627 if (NULL == _singleton)
628 {
629 _singleton = new SAudioManager;
630 }
631 return _singleton;
632}
633
634void SAudioManager::Kill()
635{
636 SAFE_DELETE(_singleton);
637}
638
639cAudio::IAudioManager* SAudioManager::GetManager()
640{
641 return manager;
642}
643
644SAudioInput* SAudioManager::CreateAudioCapture(int device, unsigned int frequency, cAudio::AudioFormats format)
645{
646 SAudioInput* capture = new SAudioInput(device, frequency, format);
647 if (capture->IsValid())
648 {
649 validInput.insert(capture);
650 return capture;
651 }
652 else
653 {
654 SAFE_DELETE(capture);
655 return NULL;
656 }
657}
658
659void SAudioManager::RemoveAudioCapture(SAudioInput* capture)
660{
661 if ((manager != 0) && (capture != 0))
662 {
663 if (validInput.find(capture) != validInput.end())
664 validInput.erase(capture);
665 SAFE_DELETE(capture);
666 }
667}
668
669void SAudioManager::SetListenerPosition(cAudio::cVector3 pos, cAudio::cVector3 dir)
670{
671 if (manager != 0)
672 {
673 cAudio::IListener* listener = manager->getListener();
674 listener->setPosition(pos);
675 listener->setDirection(dir);
676 }
677}
678
679void SAudioManager::AddValidSound(SAudioSound* sound)
680{
681 if (sound->IsValid())
682 validSounds.insert(sound);
683}
684
685void SAudioManager::RemoveValidSound(SAudioSound* sound)
686{
687 if (validSounds.find(sound) != validSounds.end())
688 validSounds.erase(sound);
689}
690
691bool SAudioManager::GetValidSound(SAudioSound* sound)
692{
693 if (validSounds.find(sound) != validSounds.end())
694 return true;
695 else
696 return false;
697}
698
699void SAudioManager::RemoveSound(SAudioSound* sound)
700{
701 if ((manager != 0) && (GetValidSound(sound)))
702 {
703 RemoveValidSound(sound);
704 SAFE_DELETE(sound);
705 }
706}
707
708SAudioSound* SAudioManager::AddSound(std::string name, std::string path, bool stream)
709{
710 if (manager != 0)
711 {
712 SAudioSound* sound = new SAudioSound(name, path, stream);
713 AddValidSound(sound);
714 return sound;
715 }
716 else
717 {
718 return 0;
719 }
720}
721
722SAudioSound* SAudioManager::AddSound(std::string name, unsigned int frequency, cAudio::AudioFormats format)
723{
724 if (manager != 0)
725 {
726 SAudioSound* sound = new SAudioSound(name, frequency, format);
727 AddValidSound(sound);
728 return sound;
729 }
730 else
731 {
732 return 0;
733 }
734}
735
736bool SAudioManager::GetPaused()
737{
738 return mPaused;
739}
740
741void SAudioManager::Pause()
742{
743 mPaused = true;
744 for(std::set<SAudioSound*>::iterator it = validSounds.begin(); it != validSounds.end(); ++it)
745 {
746 SAudioSound* sound = (*it);
747 if (sound->IsPlaying())
748 {
749 sound->SetLoopOnResume(sound->IsLoop());
750 sound->Pause();
751 sound->SetPlayOnResume(true);
752 }
753 }
754
755 for (std::set<SAudioInput*>::iterator it = validInput.begin(); it != validInput.end(); ++it)
756 {
757 SAudioInput* input = (*it);
758 if (input->GetState())
759 {
760 input->SetResumeState(true);
761 input->Stop();
762 }
763 }
764}
765
766void SAudioManager::Resume()
767{
768 mPaused = false;
769 for(std::set<SAudioSound*>::iterator it = validSounds.begin(); it != validSounds.end(); ++it)
770 {
771 SAudioSound* sound = (*it);
772 if (sound->GetPlayOnResume())
773 {
774 if (sound->Is3d())
775 sound->Play3d(sound->Get3DAttenuation(), sound->GetLoopOnResume());
776 else
777 sound->Play(sound->GetLoopOnResume());
778
779 sound->SetPlayOnResume(false);
780 }
781 }
782
783 for (std::set<SAudioInput*>::iterator it = validInput.begin(); it != validInput.end(); ++it)
784 {
785 SAudioInput* input = (*it);
786 if (input->GetResumeState())
787 {
788 input->SetResumeState(false);
789 input->Start();
790 }
791 }
792}
793
794void SAudioManager::StopAllSounds()
795{
796 manager->stopAllSounds();
797}
798
799void SAudioManager::SetMuted(bool value)
800{
801 if (muted && !value)
802 {
803 muted = false;
804 SetMasterVolume(unmutedVolume);
805 }
806 else if (!muted && value)
807 {
808 muted = true;
809 unmutedVolume = GetMasterVolume();
810 SetMasterVolume(0.0f);
811 }
812}
813
814void SAudioManager::ToggleMute()
815{
816 if (muted)
817 {
818 muted = false;
819 SetMasterVolume(unmutedVolume);
820 }
821 else if (!muted)
822 {
823 muted = true;
824 unmutedVolume = GetMasterVolume();
825 SetMasterVolume(0.0f);
826 }
827}
828
829bool SAudioManager::IsMuted()
830{
831 return muted;
832}
833
834void SAudioManager::SetMasterVolume(float volume)
835{
836 manager->setMasterVolume(volume);
837}
838
839float SAudioManager::GetMasterVolume()
840{
841 return manager->getMasterVolume();
842}
843
844#if CAUDIO_EFX_ENABLED == 1
845void SAudioManager::AttachSoundEffect(cAudio::IAudioSource* source)
846{
847 if (mEffect && mEffect->isValid())
848 {
849 source->attachEffect(0, mEffect);
850 }
851}
852
853void SAudioManager::DettachSoundEffect(cAudio::IAudioSource* source)
854{
855 source->removeEffect(0);
856}
857
858void SAudioManager::AttachSoundFilter(cAudio::IAudioSource* source)
859{
860 if (mFilter && mFilter->isValid())
861 {
862 source->attachFilter(mFilter);
863 }
864}
865
866void SAudioManager::DettachSoundFilter(cAudio::IAudioSource* source)
867{
868 source->removeFilter();
869}
870#endif
871
872// Initialize singleton
873SAudioManager* SAudioManager::_singleton = NULL;
874
875#ifdef ANDROID
876bool SAudioInput::checkRecordAudioPermission()
877{
878 JNIEnv* env = 0;
879 struct android_app* mApp = (struct android_app*)SCgetExtra("this_inst");
880 if (mApp->activity->vm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
881 mApp->activity->vm->AttachCurrentThread(&env, NULL);
882
883 //wait for default authorization
884 jobject oActivity = mApp->activity->clazz;
885 jclass cActivity = env->GetObjectClass(oActivity);
886
887 if (cActivity)
888 {
889 bool authPassed = false;
890 while (!authPassed)
891 {
892 jfieldID reqBoolean = env->GetFieldID(cActivity, "mMinAuthPassed", "Z");
893 authPassed = (bool) env->GetBooleanField(oActivity, reqBoolean);
894 if (!authPassed)
895 std::this_thread::sleep_for(std::chrono::milliseconds(100));
896 }
897
898 //check camera permission
899 jmethodID record_audio_permission = env->GetMethodID(cActivity, "RequestRecordAudioPermission", "()V");
900 if (record_audio_permission)
901 env->CallVoidMethod(oActivity, record_audio_permission);
902
903 if (env->ExceptionCheck())
904 {
905 env->ExceptionClear();
906 MMechostr(MSKRUNTIME, "Android Record audio exception : Check android.permission.RECORD_AUDIO\n");
907 }
908
909 authPassed = false;
910 while (!authPassed)
911 {
912 jfieldID reqBoolean = env->GetFieldID(cActivity, "mRecordAudioAuthPassed", "Z");
913 authPassed = (bool) env->GetBooleanField(oActivity, reqBoolean);
914 if (!authPassed)
915 std::this_thread::sleep_for(std::chrono::milliseconds(100));
916 }
917 env->DeleteLocalRef(cActivity);
918
919 return authPassed;
920 }
921 else
922 return false;
923}
924#endif //ANDROID
925
926SAudioInput::SAudioInput()
927{
928 mAudioCapture = 0;
929 mState = false;
930 mValid = false;
931 mResumeState = false;
932}
933
934SAudioInput::SAudioInput(int device, unsigned int frequency, cAudio::AudioFormats format)
935{
936 mAudioCapture = 0;
937 mState = false;
938 mValid = false;
939 mResumeState = false;
940
941 bool captureReady = false;
942
943#ifdef ANDROID
944 if (checkRecordAudioPermission())
945 {
946#endif
947 mAudioCapture = cAudio::createAudioCapture(false);
948
949 // ! Here we enumerate different devices
950 cAudio::IAudioDeviceList* pAudioDeviceList = cAudio::createAudioDeviceList(cAudio::DT_RECORDING);
951
952 if (pAudioDeviceList)
953 {
954 captureReady = pAudioDeviceList->isSupported();
955 unsigned int deviceSelection = 0;
956 MMechostr(MSKRUNTIME, "Audio capturing Supported : %s ", (captureReady) ? "Yes" : "No");
957 if (captureReady)
958 {
959 unsigned int deviceCount = pAudioDeviceList->getDeviceCount();
960 cAudio::cAudioString defaultDeviceName = pAudioDeviceList->getDefaultDeviceName();
961 for (unsigned int i = 0; i < deviceCount; ++i)
962 {
963 cAudio::cAudioString deviceName = pAudioDeviceList->getDeviceName(i);
964 if (deviceName.compare(defaultDeviceName) == 0)
965 {
966 if ((device == -1) || (device == i))
967 deviceSelection = i;
968 MMechostr(MSKRUNTIME, "Audio capture device : %d - %s [DEFAULT]", i, deviceName.c_str());
969 }
970 else
971 {
972 if (device == i)
973 deviceSelection = i;
974 MMechostr(MSKRUNTIME, "Audio capture device : %d - %s", i, deviceName.c_str());
975 }
976 }
977
978 captureReady = mAudioCapture->initialize(pAudioDeviceList->getDeviceName(deviceSelection).c_str(), frequency, format);
979 CAUDIO_DELETE pAudioDeviceList; // Free up some memory
980 pAudioDeviceList = 0;
981 }
982 }
983
984 if (!captureReady)
985 {
986 cAudio::destroyAudioCapture(mAudioCapture);
987 mAudioCapture = 0;
988 mValid = false;
989 }
990 else
991 {
992 mValid = true;
993 }
994
995#ifdef ANDROID
996 }
997#endif
998}
999
1000SAudioInput::~SAudioInput()
1001{
1002 if (mAudioCapture)
1003 {
1004 if (mState)
1005 {
1006 //purge
1007 mAudioCapture->getCapturedAudioBuffer();
1008 mAudioCapture->stopCapture();
1009 cAudio::cAudioSleep(10);
1010 }
1011
1012 cAudio::destroyAudioCapture(mAudioCapture);
1013 }
1014 mAudioCapture = 0;
1015 mState = false;
1016}
1017
1018void SAudioInput::Start()
1019{
1020 if (mAudioCapture && !mState)
1021 {
1022 mAudioCapture->beginCapture();
1023 mState = true;
1024 }
1025}
1026
1027void SAudioInput::Stop()
1028{
1029 if (mAudioCapture && mState)
1030 {
1031 mAudioCapture->stopCapture();
1032 mState = false;
1033 }
1034}
1035
1036bool SAudioInput::IsValid()
1037{
1038 return mValid;
1039}
1040
1041bool SAudioInput::GetState()
1042{
1043 return mState;
1044}
1045
1046void SAudioInput::SetResumeState(bool state)
1047{
1048 mResumeState = state;
1049}
1050
1051bool SAudioInput::GetResumeState()
1052{
1053 return mResumeState;
1054}
1055
1056size_t SAudioInput::GetBuffer(char* destBuffer, size_t length)
1057{
1058 if (!mAudioCapture->getCurrentCapturedAudioSize())
1059 return 0;
1060
1061 return mAudioCapture->getCapturedAudio(destBuffer, length);
1062}
1063
1064
1065SAudioSound::SAudioSound(std::string name, std::string path, bool stream)
1066{
1067 mName = name;
1068 mPlayOnResume = false;
1069 mLoopOnResume = false;
1070 mLast3dState = false;
1071 mIsStream = false;
1072 mEffectState = false;
1073 mFilterState = false;
1074 mLast3dAttenuation = 1.0f;
1075
1076 mSound = SAudioManager::GetInstance()->GetManager()->create(name.c_str(), path.c_str(), stream);
1077 if (mSound)
1078 {
1079 mSound->registerEventHandler(this);
1080 MMechostr(MSKDEBUG, "SAudioSound OK : %s ", path.c_str());
1081 }
1082 else
1083 {
1084 MMechostr(MSKDEBUG, "SAudioSound Failed : %s ", path.c_str());
1085 }
1086}
1087
1088SAudioSound::SAudioSound(std::string name, unsigned int frequency, cAudio::AudioFormats format)
1089{
1090 mName = name;
1091 mPlayOnResume = false;
1092 mLoopOnResume = false;
1093 mLast3dState = false;
1094 mEffectState = false;
1095 mFilterState = false;
1096 mIsStream = true;
1097 mLast3dAttenuation = 1.0f;
1098
1099 mSound = mSound = SAudioManager::GetInstance()->GetManager()->createFromStream(name.c_str(), frequency, format);
1100 if (mSound)
1101 {
1102 mSound->registerEventHandler(this);
1103 MMechostr(MSKDEBUG, "SAudioSound OK : %s ", name.c_str());
1104 }
1105 else
1106 {
1107 MMechostr(MSKDEBUG, "SAudioSound Failed : %s ", name.c_str());
1108 }
1109}
1110
1111SAudioSound::~SAudioSound()
1112{
1113 if (mSound)
1114 {
1115 mSound->unRegisterEventHandler(this);
1116
1117#if CAUDIO_EFX_ENABLED == 1
1118 SAudioManager::GetInstance()->DettachSoundEffect(mSound);
1119 SAudioManager::GetInstance()->DettachSoundFilter(mSound);
1120#endif
1121
1122 SAudioManager::GetInstance()->GetManager()->release(mSound);
1123 mSound = 0;
1124 }
1125}
1126
1127bool SAudioSound::IsValid()
1128{
1129 if (mSound)
1130 return true;
1131 else
1132 return false;
1133}
1134
1135std::string SAudioSound::GetName()
1136{
1137 return mName;
1138}
1139
1140bool SAudioSound::Play(bool loop)
1141{
1142 if (mSound)
1143 {
1144 mLast3dState = false;
1145 if (!SAudioManager::GetInstance()->GetPaused())
1146 {
1147 return mSound->play2d(loop && !mIsStream);
1148 }
1149 else
1150 {
1151 SetLoopOnResume(loop && !mIsStream);
1152 SetPlayOnResume(true);
1153 return true;
1154 }
1155 }
1156 else
1157 return false;
1158}
1159
1160bool SAudioSound::Play3d(float attenuation, bool loop)
1161{
1162 if (mSound)
1163 {
1164 mLast3dState = true;
1165 mLast3dAttenuation = attenuation;
1166 if (!SAudioManager::GetInstance()->GetPaused())
1167 {
1168 return mSound->play3d(mSound->getPosition(), attenuation, loop);
1169 }
1170 else
1171 {
1172 SetLoopOnResume(loop);
1173 SetPlayOnResume(true);
1174 return true;
1175 }
1176 }
1177 else
1178 return false;
1179}
1180
1181void SAudioSound::Stop()
1182{
1183 if (mSound)
1184 {
1185 mSound->stop();
1186 SetPlayOnResume(false);
1187 }
1188}
1189
1190void SAudioSound::Pause()
1191{
1192 if (mSound)
1193 {
1194 mSound->pause();
1195 }
1196}
1197
1198void SAudioSound::SetVolume(float volume)
1199{
1200 if (mSound)
1201 mSound->setVolume(volume);
1202}
1203
1204float SAudioSound::GetVolume()
1205{
1206 if (mSound)
1207 return mSound->getVolume();
1208 else
1209 return 0.0f;
1210}
1211
1212bool SAudioSound::IsPlaying()
1213{
1214 if (mSound)
1215 return mSound->isPlaying();
1216 else
1217 return false;
1218}
1219
1220bool SAudioSound::Is3d()
1221{
1222 return mLast3dState;
1223}
1224
1225bool SAudioSound::IsLoop()
1226{
1227 if (mSound)
1228 return mSound->isLooping();
1229 else
1230 return false;
1231}
1232
1233bool SAudioSound::GetLoopOnResume()
1234{
1235 return mPlayOnResume;
1236}
1237
1238void SAudioSound::SetLoopOnResume(bool state)
1239{
1240 mLoopOnResume = state;
1241}
1242
1243bool SAudioSound::GetPlayOnResume()
1244{
1245 return mPlayOnResume;
1246}
1247
1248void SAudioSound::SetPlayOnResume(bool state)
1249{
1250 mPlayOnResume = state;
1251}
1252
1253float SAudioSound::Get3DAttenuation()
1254{
1255 return mLast3dAttenuation;
1256}
1257
1258float SAudioSound::GetTime()
1259{
1260 if (mSound)
1261 return mSound->getCurrentAudioTime();
1262 else
1263 return 0.0f;
1264}
1265
1266float SAudioSound::GetTotalTime()
1267{
1268 if (mSound)
1269 return mSound->getTotalAudioTime();
1270 else
1271 return 0.0f;
1272}
1273
1274void SAudioSound::SetPitch(float pitch)
1275{
1276 if (mSound)
1277 mSound->setPitch(pitch);
1278}
1279
1280float SAudioSound::GetPitch()
1281{
1282 if (mSound)
1283 return mSound->getPitch();
1284 else
1285 return 0.0f;
1286}
1287
1288void SAudioSound::Seek(float pos, bool relative)
1289{
1290 if (mSound)
1291 mSound->seek(pos, relative);
1292}
1293
1294// 3D
1295void SAudioSound::SetPositionAndDirection(cAudio::cVector3 pos, cAudio::cVector3 dir)
1296{
1297 if (mSound)
1298 {
1299 mSound->setPosition(pos);
1300 mSound->setDirection(dir);
1301 }
1302}
1303
1304cAudio::cVector3 SAudioSound::GetPosition()
1305{
1306 if (mSound)
1307 return mSound->getPosition();
1308 else
1309 return cAudio::cVector3(0.0f, 0.0f, 0.0f);
1310}
1311
1312cAudio::cVector3 SAudioSound::GetDirection()
1313{
1314 if (mSound)
1315 return mSound->getDirection();
1316 else
1317 return cAudio::cVector3(0.0f, 0.0f, 0.0f);
1318}
1319
1320void SAudioSound::SetCone(float inner, float outer, float outvol)
1321{
1322 if (mSound)
1323 {
1324 mSound->setInnerConeAngle(inner);
1325 mSound->setOuterConeAngle(outer);
1326 mSound->setOuterConeVolume(outvol);
1327 }
1328}
1329
1330float SAudioSound::GetInnerCone()
1331{
1332 if (mSound)
1333 return mSound->getInnerConeAngle();
1334 else
1335 return 0.0f;
1336}
1337
1338float SAudioSound::GetOuterCone()
1339{
1340 if (mSound)
1341 return mSound->getOuterConeAngle();
1342 else
1343 return 0.0f;
1344}
1345
1346float SAudioSound::GetOuterConeVolume()
1347{
1348 if (mSound)
1349 return mSound->getOuterConeVolume();
1350 else
1351 return 0.0f;
1352}
1353
1354void SAudioSound::SetRolloffFactor(float rolloff)
1355{
1356 if (mSound)
1357 mSound->setRolloffFactor(rolloff);
1358}
1359
1360float SAudioSound::GetRolloffFactor()
1361{
1362 if (mSound)
1363 return mSound->getRolloffFactor();
1364 else
1365 return 0.0f;
1366}
1367
1368bool SAudioSound::FillBuffer(const char* &data, const unsigned int &size)
1369{
1370 if (mSound && size)
1371 return mSound->fillStreamBuffer(data, size);
1372 else
1373 return false;
1374}
1375
1376#if CAUDIO_EFX_ENABLED == 1
1377void SAudioSound::SetEffectState(bool state)
1378{
1379 mEffectState = state;
1380 if (mSound)
1381 {
1382 if (mEffectState)
1383 SAudioManager::GetInstance()->AttachSoundEffect(mSound);
1384 else
1385 SAudioManager::GetInstance()->DettachSoundEffect(mSound);
1386 }
1387}
1388
1389void SAudioSound::SetFilterState(bool state)
1390{
1391 mFilterState = state;
1392 if (mSound)
1393 {
1394 if (mFilterState)
1395 SAudioManager::GetInstance()->AttachSoundFilter(mSound);
1396 else
1397 SAudioManager::GetInstance()->DettachSoundFilter(mSound);
1398 }
1399}
1400#endif
1401
1402// EVENTS
1403void SAudioSound::onUpdate()
1404{
1405}
1406
1408{
1409}
1410
1412{
1413}
1414
1416{
1417 SetPlayOnResume(false);
1418 OBJpostEvent(AUDIO_END_CB, SCOL_PTR this, 0);
1419}
1420
1422{
1423}
virtual void onDeviceChange()
This function is called when the default device has change.
Definition audio.cpp:606
virtual void onRelease()
This function is called on manager shutdown.
Definition audio.cpp:586
virtual void onDecoderRegister()
This function is called when an audio decoder is registered.
Definition audio.cpp:596
virtual void onDataSourceRegister()
This function is called when a data source is registered.
Definition audio.cpp:601
virtual void onInit()
This function is called on manager initialization.
Definition audio.cpp:576
virtual void onUpdate()
This function is called on every manager update.
Definition audio.cpp:581
virtual void onSourceCreate()
This function is called on when an audio source is created.
Definition audio.cpp:591
virtual void onRelease()
This function is called when a source is released and soon to be deleted.
Definition audio.cpp:1407
virtual void onStop()
This function is called when a source stopped playback.
Definition audio.cpp:1415
virtual void onPause()
This function is called when a source is paused.
Definition audio.cpp:1421
virtual void onPlay()
This function is called when a source starts playing.
Definition audio.cpp:1411
FILE * pFile
File stream.
Definition audio.h:36
int Filesize
Holds file size.
Definition audio.h:34
bool Valid
Holds if valid
Definition audio.h:32