5extern int AUDIO_END_CB;
10cScolFileSource::cScolFileSource(
const char* filename) : Valid(false), pFile(NULL), Filesize(0)
12 cAudio::cAudioString safeFilename = cAudio::fromUTF8(filename);
13 if (safeFilename.length() != 0)
15 pFile = fopen(cAudio::toUTF8(safeFilename),
"rb");
24 fseek(pFile, 0, SEEK_END);
25 Filesize = ftell(pFile);
26 fseek(pFile, 0, SEEK_SET);
30cScolFileSource::~cScolFileSource()
36bool cScolFileSource::isValid()
41int cScolFileSource::getCurrentPos()
46int cScolFileSource::getSize()
51int cScolFileSource::read(
void* output,
int size)
53 return fread(output,
sizeof(
char), size,
pFile);
56bool cScolFileSource::seek(
int amount,
bool relative)
60 int oldamount = ftell(
pFile);
61 fseek(
pFile, amount, SEEK_CUR);
64 if (oldamount + amount != ftell(
pFile))
69 fseek(
pFile, amount, SEEK_SET);
70 if (amount != ftell(
pFile))
77SAudioManager::SAudioManager()
82 manager = cAudio::createAudioManager(
true);
83 manager->registerDataSource(scolDataFactory,
"ScolFileSystem", 100);
84 manager->registerEventHandler(
this);
86#if CAUDIO_EFX_ENABLED == 1
90 mFilterType = cAudio::EFT_NULL;
91 mFilterLowFreqVolume = 0.1f;
92 mFilterHighFreqVolume = 0.1f;
93 mEffectType = cAudio::EET_NULL;
187#if CAUDIO_EFX_ENABLED == 1
188void SAudioManager::InitEffects()
190 cAudio::IAudioEffects* effects = manager->getEffects();
193 mEffect = effects->createEffect();
194 mFilter = effects->createFilter();
196 if (mEffect && mEffect->isValid() && mFilter && mFilter->isValid())
198 mFilter->setHighFrequencyVolume(mFilterHighFreqVolume);
199 mFilter->setLowFrequencyVolume(mFilterLowFreqVolume);
249SAudioManager::~SAudioManager()
257 SAFE_DELETE(scolDataFactory);
258 manager->unRegisterDataSource(
"ScolFileSystem");
259 manager->unRegisterEventHandler(
this);
303#if CAUDIO_EFX_ENABLED == 1
310 cAudio::destroyAudioManager(manager);
315#if CAUDIO_EFX_ENABLED == 1
316bool SAudioManager::SetFilter(cAudio::FilterTypes type,
float lowFreqVolume,
float highFreqVolume)
319 mFilterLowFreqVolume = lowFreqVolume;
320 mFilterHighFreqVolume = highFreqVolume;
322 cAudio::IAudioEffects* effects = manager->getEffects();
323 if (mFilter && effects && effects->isFilterSupported(type))
325 mFilter->setType(type);
326 mFilter->setLowFrequencyVolume(lowFreqVolume);
327 mFilter->setHighFrequencyVolume(highFreqVolume);
333 mFilter->setType(cAudio::EFT_NULL);
338bool SAudioManager::SetEffect(cAudio::EffectTypes type, std::string preset)
341 mEffectPreset = preset;
343 cAudio::IAudioEffects* effects = manager->getEffects();
345 if ((type == cAudio::EET_REVERB) && effects && effects->isEffectSupported(cAudio::EET_EAX_REVERB))
346 type = cAudio::EET_EAX_REVERB;
348 if (mEffect && effects && effects->isEffectSupported(type))
350 if (effects->isEffectPresetRegistered(type, preset.c_str()))
354 case cAudio::EET_AUTOWAH:
355 mEffect->setAutowahParameters(effects->getAutowahEffectPreset(preset.c_str()));
358 case cAudio::EET_CHORUS:
359 mEffect->setChorusParameters(effects->getChorusEffectPreset(preset.c_str()));
362 case cAudio::EET_COMPRESSOR:
363 mEffect->setCompressorParameters(effects->getCompressorEffectPreset(preset.c_str()));
366 case cAudio::EET_DISTORTION:
367 mEffect->setDistortionParameters(effects->getDistortionEffectPreset(preset.c_str()));
370 case cAudio::EET_EAX_REVERB:
371 mEffect->setEAXReverbParameters(effects->getEAXReverbEffectPreset(preset.c_str()));
374 case cAudio::EET_ECHO:
375 mEffect->setEchoParameters(effects->getEchoEffectPreset(preset.c_str()));
378 case cAudio::EET_EQUALIZER:
379 mEffect->setEqualizerParameters(effects->getEqualizerEffectPreset(preset.c_str()));
382 case cAudio::EET_FLANGER:
383 mEffect->setFlangerParameters(effects->getFlangerEffectPreset(preset.c_str()));
386 case cAudio::EET_FREQUENCY_SHIFTER:
387 mEffect->setFrequencyShiftParameters(effects->getFrequencyShiftEffectPreset(preset.c_str()));
390 case cAudio::EET_PITCH_SHIFTER:
391 mEffect->setPitchShifterParameters(effects->getPitchShifterEffectPreset(preset.c_str()));
394 case cAudio::EET_REVERB:
395 mEffect->setReverbParameters(effects->getReverbEffectPreset(preset.c_str()));
398 case cAudio::EET_RING_MODULATOR:
399 mEffect->setRingModulatorParameters(effects->getRingModulatorEffectPreset(preset.c_str()));
402 case cAudio::EET_VOCAL_MORPHER:
403 mEffect->setVocalMorpherParameters(effects->getVocalMorpherEffectPreset(preset.c_str()));
408 mEffect->setType(type);
413 mEffect->setType(cAudio::EET_NULL);
418bool SAudioManager::SetReverbEffectParam(cAudio::sReverbParameters param)
420 cAudio::IAudioEffects* effects = manager->getEffects();
421 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_REVERB))
423 mEffect->setReverbParameters(param);
443bool SAudioManager::SetAutowahEffectParam(cAudio::sAutowahParameters param)
445 cAudio::IAudioEffects* effects = manager->getEffects();
446 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_AUTOWAH))
448 mEffect->setAutowahParameters(param);
455bool SAudioManager::SetChorusEffectParam(cAudio::sChorusParameters param)
457 cAudio::IAudioEffects* effects = manager->getEffects();
458 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_CHORUS))
460 mEffect->setChorusParameters(param);
467bool SAudioManager::SetCompressorEffectParam(cAudio::sCompressorParameters param)
469 cAudio::IAudioEffects* effects = manager->getEffects();
470 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_COMPRESSOR))
472 mEffect->setCompressorParameters(param);
479bool SAudioManager::SetDistortionEffectParam(cAudio::sDistortionParameters param)
481 cAudio::IAudioEffects* effects = manager->getEffects();
482 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_DISTORTION))
484 mEffect->setDistortionParameters(param);
491bool SAudioManager::SetEchoEffectParam(cAudio::sEchoParameters param)
493 cAudio::IAudioEffects* effects = manager->getEffects();
494 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_ECHO))
496 mEffect->setEchoParameters(param);
503bool SAudioManager::SetEqualizerEffectParam(cAudio::sEqualizerParameters param)
505 cAudio::IAudioEffects* effects = manager->getEffects();
506 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_EQUALIZER))
508 mEffect->setEqualizerParameters(param);
515bool SAudioManager::SetFlangerEffectParam(cAudio::sFlangerParameters param)
517 cAudio::IAudioEffects* effects = manager->getEffects();
518 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_FLANGER))
520 mEffect->setFlangerParameters(param);
527bool SAudioManager::SetFrequencyShiftEffectParam(cAudio::sFrequencyShiftParameters param)
529 cAudio::IAudioEffects* effects = manager->getEffects();
530 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_FREQUENCY_SHIFTER))
532 mEffect->setFrequencyShiftParameters(param);
539bool SAudioManager::SetPitchShifterEffectParam(cAudio::sPitchShifterParameters param)
541 cAudio::IAudioEffects* effects = manager->getEffects();
542 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_PITCH_SHIFTER))
544 mEffect->setPitchShifterParameters(param);
551bool SAudioManager::SetRingModulatorEffectParam(cAudio::sRingModulatorParameters param)
553 cAudio::IAudioEffects* effects = manager->getEffects();
554 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_RING_MODULATOR))
556 mEffect->setRingModulatorParameters(param);
563bool SAudioManager::SetVocalMorpherEffectParam(cAudio::sVocalMorpherParameters param)
565 cAudio::IAudioEffects* effects = manager->getEffects();
566 if (mEffect && effects && effects->isEffectSupported(cAudio::EET_VOCAL_MORPHER))
568 mEffect->setVocalMorpherParameters(param);
608#if CAUDIO_EFX_ENABLED == 1
613 SetFilter(mFilterType, mFilterLowFreqVolume, mFilterHighFreqVolume);
614 SetEffect(mEffectType, mEffectPreset);
616 for (std::set<SAudioSound*>::iterator it = validSounds.begin(); it != validSounds.end(); ++it)
619 sound->SetEffectState(sound->mEffectState);
620 sound->SetFilterState(sound->mFilterState);
627 if (NULL == _singleton)
634void SAudioManager::Kill()
636 SAFE_DELETE(_singleton);
639cAudio::IAudioManager* SAudioManager::GetManager()
644SAudioInput* SAudioManager::CreateAudioCapture(
int device,
unsigned int frequency, cAudio::AudioFormats format)
647 if (capture->IsValid())
649 validInput.insert(capture);
654 SAFE_DELETE(capture);
659void SAudioManager::RemoveAudioCapture(
SAudioInput* capture)
661 if ((manager != 0) && (capture != 0))
663 if (validInput.find(capture) != validInput.end())
664 validInput.erase(capture);
665 SAFE_DELETE(capture);
669void SAudioManager::SetListenerPosition(cAudio::cVector3 pos, cAudio::cVector3 dir)
673 cAudio::IListener* listener = manager->getListener();
674 listener->setPosition(pos);
675 listener->setDirection(dir);
679void SAudioManager::AddValidSound(
SAudioSound* sound)
681 if (sound->IsValid())
682 validSounds.insert(sound);
685void SAudioManager::RemoveValidSound(
SAudioSound* sound)
687 if (validSounds.find(sound) != validSounds.end())
688 validSounds.erase(sound);
691bool SAudioManager::GetValidSound(
SAudioSound* sound)
693 if (validSounds.find(sound) != validSounds.end())
701 if ((manager != 0) && (GetValidSound(sound)))
703 RemoveValidSound(sound);
708SAudioSound* SAudioManager::AddSound(std::string name, std::string path,
bool stream)
713 AddValidSound(sound);
722SAudioSound* SAudioManager::AddSound(std::string name,
unsigned int frequency, cAudio::AudioFormats format)
727 AddValidSound(sound);
736bool SAudioManager::GetPaused()
741void SAudioManager::Pause()
744 for(std::set<SAudioSound*>::iterator it = validSounds.begin(); it != validSounds.end(); ++it)
747 if (sound->IsPlaying())
749 sound->SetLoopOnResume(sound->IsLoop());
751 sound->SetPlayOnResume(
true);
755 for (std::set<SAudioInput*>::iterator it = validInput.begin(); it != validInput.end(); ++it)
758 if (input->GetState())
760 input->SetResumeState(
true);
766void SAudioManager::Resume()
769 for(std::set<SAudioSound*>::iterator it = validSounds.begin(); it != validSounds.end(); ++it)
772 if (sound->GetPlayOnResume())
775 sound->Play3d(sound->Get3DAttenuation(), sound->GetLoopOnResume());
777 sound->Play(sound->GetLoopOnResume());
779 sound->SetPlayOnResume(
false);
783 for (std::set<SAudioInput*>::iterator it = validInput.begin(); it != validInput.end(); ++it)
786 if (input->GetResumeState())
788 input->SetResumeState(
false);
794void SAudioManager::StopAllSounds()
796 manager->stopAllSounds();
799void SAudioManager::SetMuted(
bool value)
804 SetMasterVolume(unmutedVolume);
806 else if (!muted && value)
809 unmutedVolume = GetMasterVolume();
810 SetMasterVolume(0.0f);
814void SAudioManager::ToggleMute()
819 SetMasterVolume(unmutedVolume);
824 unmutedVolume = GetMasterVolume();
825 SetMasterVolume(0.0f);
829bool SAudioManager::IsMuted()
834void SAudioManager::SetMasterVolume(
float volume)
836 manager->setMasterVolume(volume);
839float SAudioManager::GetMasterVolume()
841 return manager->getMasterVolume();
844#if CAUDIO_EFX_ENABLED == 1
845void SAudioManager::AttachSoundEffect(cAudio::IAudioSource* source)
847 if (mEffect && mEffect->isValid())
849 source->attachEffect(0, mEffect);
853void SAudioManager::DettachSoundEffect(cAudio::IAudioSource* source)
855 source->removeEffect(0);
858void SAudioManager::AttachSoundFilter(cAudio::IAudioSource* source)
860 if (mFilter && mFilter->isValid())
862 source->attachFilter(mFilter);
866void SAudioManager::DettachSoundFilter(cAudio::IAudioSource* source)
868 source->removeFilter();
876bool SAudioInput::checkRecordAudioPermission()
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);
884 jobject oActivity = mApp->activity->clazz;
885 jclass cActivity = env->GetObjectClass(oActivity);
889 bool authPassed =
false;
892 jfieldID reqBoolean = env->GetFieldID(cActivity,
"mMinAuthPassed",
"Z");
893 authPassed = (bool) env->GetBooleanField(oActivity, reqBoolean);
895 std::this_thread::sleep_for(std::chrono::milliseconds(100));
899 jmethodID record_audio_permission = env->GetMethodID(cActivity,
"RequestRecordAudioPermission",
"()V");
900 if (record_audio_permission)
901 env->CallVoidMethod(oActivity, record_audio_permission);
903 if (env->ExceptionCheck())
905 env->ExceptionClear();
906 MMechostr(MSKRUNTIME,
"Android Record audio exception : Check android.permission.RECORD_AUDIO\n");
912 jfieldID reqBoolean = env->GetFieldID(cActivity,
"mRecordAudioAuthPassed",
"Z");
913 authPassed = (bool) env->GetBooleanField(oActivity, reqBoolean);
915 std::this_thread::sleep_for(std::chrono::milliseconds(100));
917 env->DeleteLocalRef(cActivity);
926SAudioInput::SAudioInput()
931 mResumeState =
false;
934SAudioInput::SAudioInput(
int device,
unsigned int frequency, cAudio::AudioFormats format)
939 mResumeState =
false;
941 bool captureReady =
false;
944 if (checkRecordAudioPermission())
947 mAudioCapture = cAudio::createAudioCapture(
false);
950 cAudio::IAudioDeviceList* pAudioDeviceList = cAudio::createAudioDeviceList(cAudio::DT_RECORDING);
952 if (pAudioDeviceList)
954 captureReady = pAudioDeviceList->isSupported();
955 unsigned int deviceSelection = 0;
956 MMechostr(MSKRUNTIME,
"Audio capturing Supported : %s ", (captureReady) ?
"Yes" :
"No");
959 unsigned int deviceCount = pAudioDeviceList->getDeviceCount();
960 cAudio::cAudioString defaultDeviceName = pAudioDeviceList->getDefaultDeviceName();
961 for (
unsigned int i = 0; i < deviceCount; ++i)
963 cAudio::cAudioString deviceName = pAudioDeviceList->getDeviceName(i);
964 if (deviceName.compare(defaultDeviceName) == 0)
966 if ((device == -1) || (device == i))
968 MMechostr(MSKRUNTIME,
"Audio capture device : %d - %s [DEFAULT]", i, deviceName.c_str());
974 MMechostr(MSKRUNTIME,
"Audio capture device : %d - %s", i, deviceName.c_str());
978 captureReady = mAudioCapture->initialize(pAudioDeviceList->getDeviceName(deviceSelection).c_str(), frequency, format);
979 CAUDIO_DELETE pAudioDeviceList;
980 pAudioDeviceList = 0;
986 cAudio::destroyAudioCapture(mAudioCapture);
1000SAudioInput::~SAudioInput()
1007 mAudioCapture->getCapturedAudioBuffer();
1008 mAudioCapture->stopCapture();
1009 cAudio::cAudioSleep(10);
1012 cAudio::destroyAudioCapture(mAudioCapture);
1018void SAudioInput::Start()
1020 if (mAudioCapture && !mState)
1022 mAudioCapture->beginCapture();
1027void SAudioInput::Stop()
1029 if (mAudioCapture && mState)
1031 mAudioCapture->stopCapture();
1036bool SAudioInput::IsValid()
1041bool SAudioInput::GetState()
1046void SAudioInput::SetResumeState(
bool state)
1048 mResumeState = state;
1051bool SAudioInput::GetResumeState()
1053 return mResumeState;
1056size_t SAudioInput::GetBuffer(
char* destBuffer,
size_t length)
1058 if (!mAudioCapture->getCurrentCapturedAudioSize())
1061 return mAudioCapture->getCapturedAudio(destBuffer, length);
1065SAudioSound::SAudioSound(std::string name, std::string path,
bool stream)
1068 mPlayOnResume =
false;
1069 mLoopOnResume =
false;
1070 mLast3dState =
false;
1072 mEffectState =
false;
1073 mFilterState =
false;
1074 mLast3dAttenuation = 1.0f;
1076 mSound = SAudioManager::GetInstance()->GetManager()->create(name.c_str(), path.c_str(), stream);
1079 mSound->registerEventHandler(
this);
1080 MMechostr(MSKDEBUG,
"SAudioSound OK : %s ", path.c_str());
1084 MMechostr(MSKDEBUG,
"SAudioSound Failed : %s ", path.c_str());
1088SAudioSound::SAudioSound(std::string name,
unsigned int frequency, cAudio::AudioFormats format)
1091 mPlayOnResume =
false;
1092 mLoopOnResume =
false;
1093 mLast3dState =
false;
1094 mEffectState =
false;
1095 mFilterState =
false;
1097 mLast3dAttenuation = 1.0f;
1099 mSound = mSound = SAudioManager::GetInstance()->GetManager()->createFromStream(name.c_str(), frequency, format);
1102 mSound->registerEventHandler(
this);
1103 MMechostr(MSKDEBUG,
"SAudioSound OK : %s ", name.c_str());
1107 MMechostr(MSKDEBUG,
"SAudioSound Failed : %s ", name.c_str());
1111SAudioSound::~SAudioSound()
1115 mSound->unRegisterEventHandler(
this);
1117#if CAUDIO_EFX_ENABLED == 1
1118 SAudioManager::GetInstance()->DettachSoundEffect(mSound);
1119 SAudioManager::GetInstance()->DettachSoundFilter(mSound);
1122 SAudioManager::GetInstance()->GetManager()->release(mSound);
1127bool SAudioSound::IsValid()
1135std::string SAudioSound::GetName()
1140bool SAudioSound::Play(
bool loop)
1144 mLast3dState =
false;
1145 if (!SAudioManager::GetInstance()->GetPaused())
1147 return mSound->play2d(loop && !mIsStream);
1151 SetLoopOnResume(loop && !mIsStream);
1152 SetPlayOnResume(
true);
1160bool SAudioSound::Play3d(
float attenuation,
bool loop)
1164 mLast3dState =
true;
1165 mLast3dAttenuation = attenuation;
1166 if (!SAudioManager::GetInstance()->GetPaused())
1168 return mSound->play3d(mSound->getPosition(), attenuation, loop);
1172 SetLoopOnResume(loop);
1173 SetPlayOnResume(
true);
1181void SAudioSound::Stop()
1186 SetPlayOnResume(
false);
1190void SAudioSound::Pause()
1198void SAudioSound::SetVolume(
float volume)
1201 mSound->setVolume(volume);
1204float SAudioSound::GetVolume()
1207 return mSound->getVolume();
1212bool SAudioSound::IsPlaying()
1215 return mSound->isPlaying();
1220bool SAudioSound::Is3d()
1222 return mLast3dState;
1225bool SAudioSound::IsLoop()
1228 return mSound->isLooping();
1233bool SAudioSound::GetLoopOnResume()
1235 return mPlayOnResume;
1238void SAudioSound::SetLoopOnResume(
bool state)
1240 mLoopOnResume = state;
1243bool SAudioSound::GetPlayOnResume()
1245 return mPlayOnResume;
1248void SAudioSound::SetPlayOnResume(
bool state)
1250 mPlayOnResume = state;
1253float SAudioSound::Get3DAttenuation()
1255 return mLast3dAttenuation;
1258float SAudioSound::GetTime()
1261 return mSound->getCurrentAudioTime();
1266float SAudioSound::GetTotalTime()
1269 return mSound->getTotalAudioTime();
1274void SAudioSound::SetPitch(
float pitch)
1277 mSound->setPitch(pitch);
1280float SAudioSound::GetPitch()
1283 return mSound->getPitch();
1288void SAudioSound::Seek(
float pos,
bool relative)
1291 mSound->seek(pos, relative);
1295void SAudioSound::SetPositionAndDirection(cAudio::cVector3 pos, cAudio::cVector3 dir)
1299 mSound->setPosition(pos);
1300 mSound->setDirection(dir);
1304cAudio::cVector3 SAudioSound::GetPosition()
1307 return mSound->getPosition();
1309 return cAudio::cVector3(0.0f, 0.0f, 0.0f);
1312cAudio::cVector3 SAudioSound::GetDirection()
1315 return mSound->getDirection();
1317 return cAudio::cVector3(0.0f, 0.0f, 0.0f);
1320void SAudioSound::SetCone(
float inner,
float outer,
float outvol)
1324 mSound->setInnerConeAngle(inner);
1325 mSound->setOuterConeAngle(outer);
1326 mSound->setOuterConeVolume(outvol);
1330float SAudioSound::GetInnerCone()
1333 return mSound->getInnerConeAngle();
1338float SAudioSound::GetOuterCone()
1341 return mSound->getOuterConeAngle();
1346float SAudioSound::GetOuterConeVolume()
1349 return mSound->getOuterConeVolume();
1354void SAudioSound::SetRolloffFactor(
float rolloff)
1357 mSound->setRolloffFactor(rolloff);
1360float SAudioSound::GetRolloffFactor()
1363 return mSound->getRolloffFactor();
1368bool SAudioSound::FillBuffer(
const char* &data,
const unsigned int &size)
1371 return mSound->fillStreamBuffer(data, size);
1376#if CAUDIO_EFX_ENABLED == 1
1377void SAudioSound::SetEffectState(
bool state)
1379 mEffectState = state;
1383 SAudioManager::GetInstance()->AttachSoundEffect(mSound);
1385 SAudioManager::GetInstance()->DettachSoundEffect(mSound);
1389void SAudioSound::SetFilterState(
bool state)
1391 mFilterState = state;
1395 SAudioManager::GetInstance()->AttachSoundFilter(mSound);
1397 SAudioManager::GetInstance()->DettachSoundFilter(mSound);
1403void SAudioSound::onUpdate()
1417 SetPlayOnResume(
false);
1418 OBJpostEvent(AUDIO_END_CB, SCOL_PTR
this, 0);
virtual void onDeviceChange()
This function is called when the default device has change.
virtual void onRelease()
This function is called on manager shutdown.
virtual void onDecoderRegister()
This function is called when an audio decoder is registered.
virtual void onDataSourceRegister()
This function is called when a data source is registered.
virtual void onInit()
This function is called on manager initialization.
virtual void onUpdate()
This function is called on every manager update.
virtual void onSourceCreate()
This function is called on when an audio source is created.
virtual void onRelease()
This function is called when a source is released and soon to be deleted.
virtual void onStop()
This function is called when a source stopped playback.
virtual void onPause()
This function is called when a source is paused.
virtual void onPlay()
This function is called when a source starts playing.
int Filesize
Holds file size.