unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop) { // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); EffectsMap::iterator iter = s_effects.find(fullPath); if (iter == s_effects.end()) { preloadEffect(fullPath.c_str()); // let's try again iter = s_effects.find(fullPath); if (iter == s_effects.end()) { fprintf(stderr, "could not find play sound %s\n", fullPath.c_str()); return -1; } } checkALError("playEffect"); iter->second->isLooped = bLoop; alSourcei(iter->second->source, AL_LOOPING, iter->second->isLooped ? AL_TRUE : AL_FALSE); alSourcePlay(iter->second->source); checkALError("playEffect"); return iter->second->source; }
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop) { EffectsMap::iterator iter = s_effects.find(pszFilePath); if (iter == s_effects.end()) { preloadEffect(pszFilePath); // let's try again iter = s_effects.find(pszFilePath); if (iter == s_effects.end()) { fprintf(stderr, "could not find play sound %s\n", pszFilePath); return -1; } } checkALError("playEffect"); iter->second->isLooped = bLoop; alSourcei(iter->second->source, AL_LOOPING, iter->second->isLooped ? AL_TRUE : AL_FALSE); alSourcePlay(iter->second->source); checkALError("playEffect"); return iter->second->source; }
static void stopBackground(bool bReleaseData) { // The background music might have been already stopped // Stop request can come from // - stopBackgroundMusic(..) // - end(..) if (s_backgroundSource != AL_NONE) alSourceStop(s_backgroundSource); if (bReleaseData) { for (auto it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it) { if (it->second->source == s_backgroundSource) { alDeleteSources(1, &it->second->source); checkALError("stopBackground:alDeleteSources"); alDeleteBuffers(1, &it->second->buffer); checkALError("stopBackground:alDeleteBuffers"); delete it->second; s_backgroundMusics.erase(it); break; } } } s_backgroundSource = AL_NONE; }
void LLAudioEngine_OpenAL::initWind() { if (true) return; llinfos << "initWind() start" << llendl; alGenBuffers(mNumWindBuffers,mWindBuffers); alGenSources(1,&mWindSource); checkALError(); // ok lets make a wind buffer now for(int counter=0;counter<mNumWindBuffers;counter++) { alBufferData(mWindBuffers[counter],AL_FORMAT_STEREO16,windDSP((void*)mWindData,mWindDataSize/mBytesPerSample),mWindDataSize,mSampleRate); checkALError(); } alSourceQueueBuffers(mWindSource, mNumWindBuffers, mWindBuffers); checkALError(); alSourcePlay(mWindSource); checkALError(); llinfos << "LLAudioEngine_OpenAL::initWind() done" << llendl; }
OPAL_SOUND_MGR bool SoundManager::setSoundPosition( unsigned int audioID, Vector3 position, Vector3 velocity, Vector3 direction ) { if ( audioID >= MAX_AUDIO_SOURCES || !mAudioSourceInUse[ audioID ] ) return false; // Set the position ALfloat pos[] = { position.x, position.y, position.z }; alSourcefv( mAudioSources[ audioID ], AL_POSITION, pos ); if ( checkALError( "setSound::alSourcefv:AL_POSITION" ) ) return false; // Set the veclocity ALfloat vel[] = { velocity.x, velocity.y, velocity.z }; alSourcefv( mAudioSources[ audioID ], AL_VELOCITY, vel ); if ( checkALError( "setSound::alSourcefv:AL_VELOCITY" ) ) return false; // Set the direction ALfloat dir[] = { velocity.x, velocity.y, velocity.z }; alSourcefv( mAudioSources[ audioID ], AL_DIRECTION, dir ); if ( checkALError( "setSound::alSourcefv:AL_DIRECTION" ) ) return false; return true; }
void SimpleAudioEngine::stopAllEffects() { EffectsMap::iterator iter = s_effects.begin(); if (iter != s_effects.end()) { checkALError("stopAllEffects:init"); alSourceStop(iter->second->source); checkALError("stopAllEffects:alSourceStop"); } }
void SimpleAudioEngine::resumeAllEffects() { EffectsMap::iterator iter = s_effects.begin(); if (iter != s_effects.end()) { checkALError("resumeAllEffects"); alSourcePlay(iter->second->source); checkALError("resumeAllEffects"); } }
void SimpleAudioEngine::preloadEffect(const char* pszFilePath) { // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); EffectsMap::iterator iter = s_effects.find(fullPath); // check if we have this already if (iter == s_effects.end()) { ALuint buffer; ALuint source; soundData *data = new soundData; string path = fullPath; checkALError("preloadEffect"); if (isOGGFile(path.data())) { buffer = createBufferFromOGG(path.data()); } else { buffer = alutCreateBufferFromFile(path.data()); checkALError("preloadEffect"); } if (buffer == AL_NONE) { fprintf(stderr, "Error loading file: '%s'\n", path.data()); alDeleteBuffers(1, &buffer); return; } alGenSources(1, &source); if (checkALError("preloadEffect") != AL_NO_ERROR) { alDeleteBuffers(1, &buffer); return; } alSourcei(source, AL_BUFFER, buffer); checkALError("preloadEffect"); data->isLooped = false; data->buffer = buffer; data->source = source; s_effects.insert(EffectsMap::value_type(fullPath, data)); } }
void SimpleAudioEngine::preloadEffect(const char* pszFilePath) { // Changing file path to full path std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath); EffectsMap::iterator iter = s_effects.find(fullPath); // check if we have this already if (iter == s_effects.end()) { ALuint buffer = AL_NONE; ALuint source = AL_NONE; checkALError("preloadEffect:init"); OpenALFile file; file.debugName = pszFilePath; file.file = fopen(fullPath.c_str(), "rb"); if (!file.file) { fprintf(stderr, "Cannot read file: '%s'\n", fullPath.data()); return; } bool success = false; const std::vector<OpenALDecoder *> &decoders = OpenALDecoder::getDecoders(); for (size_t i = 0, n = decoders.size(); !success && i < n; ++i) success = decoders[i]->decode(file, buffer); file.clear(); alGenSources(1, &source); if (checkALError("preloadEffect:alGenSources") != AL_NO_ERROR) { alDeleteBuffers(1, &buffer); return; } alSourcei(source, AL_BUFFER, buffer); checkALError("preloadEffect:alSourcei"); soundData *data = new soundData; data->isLooped = false; data->buffer = buffer; data->source = source; data->pitch = 1.0; data->pan = 0.0; data->gain = 1.0; s_effects.insert(EffectsMap::value_type(fullPath, data)); } }
static void stopBackground(bool bReleaseData) { alSourceStop(s_backgroundSource); if (bReleaseData) { s_currentBackgroundStr = ""; s_isBackgroundInitialized = false; alDeleteBuffers(1, &s_backgroundBuffer); checkALError("stopBackground"); alDeleteSources(1, &s_backgroundSource); checkALError("stopBackground"); } }
OPAL_SOUND_MGR bool SoundManager::setSound( unsigned int audioID, Vector3 position, Vector3 velocity, Vector3 direction, float maxDistance, bool playNow, bool forceRestart, float minGain ) { if ( audioID >= MAX_AUDIO_SOURCES || !mAudioSourceInUse[ audioID ] ) return false; // Set the position ALfloat pos[] = { position.x, position.y, position.z }; alSourcefv( mAudioSources[ audioID ], AL_POSITION, pos ); if ( checkALError( "setSound::alSourcefv:AL_POSITION" ) ) return false; // Set the veclocity ALfloat vel[] = { velocity.x, velocity.y, velocity.z }; alSourcefv( mAudioSources[ audioID ], AL_VELOCITY, vel ); if ( checkALError( "setSound::alSourcefv:AL_VELOCITY" ) ) return false; // Set the direction ALfloat dir[] = { velocity.x, velocity.y, velocity.z }; alSourcefv( mAudioSources[ audioID ], AL_DIRECTION, dir ); if ( checkALError( "setSound::alSourcefv:AL_DIRECTION" ) ) return false; // Set the max audible distance alSourcef( mAudioSources[ audioID ], AL_MAX_DISTANCE, maxDistance ); // Set the MIN_GAIN ( IMPORTANT - if not set, nothing audible! ) alSourcef( mAudioSources[ audioID ], AL_MIN_GAIN, minGain ); // Set the max gain alSourcef( mAudioSources[ audioID ], AL_MAX_GAIN, 1.0f ); // TODO as parameter ? global ? // Set the rollof factor alSourcef( mAudioSources[ audioID ], AL_ROLLOFF_FACTOR, 1.0f ); // TODO as parameter ? // Do we play the sound now ? if ( playNow ) return playAudio( audioID, forceRestart ); // TODO bof... not in this fct return true; }
static gboolean gst_openal_sink_unprepare (GstAudioSink * asink) { GstOpenALSink *openal = GST_OPENAL_SINK (asink); ALCcontext *old; if (!openal->context) return TRUE; old = pushContext (openal->context); alSourceStop (openal->sID); alSourcei (openal->sID, AL_BUFFER, 0); if (!openal->custom_sID) alDeleteSources (1, &openal->sID); openal->sID = 0; alDeleteBuffers (openal->bID_count, openal->bIDs); g_free (openal->bIDs); openal->bIDs = NULL; openal->bID_idx = 0; openal->bID_count = 0; openal->bID_length = 0; checkALError (); popContext (old, openal->context); if (!openal->custom_ctx) alcDestroyContext (openal->context); openal->context = NULL; return TRUE; }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { // If there is already a background music source we stop it first if (s_backgroundSource != AL_NONE) stopBackgroundMusic(false); // Changing file path to full path std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { preloadBackgroundMusic(fullPath.c_str()); it = s_backgroundMusics.find(fullPath); } if (it != s_backgroundMusics.end()) { s_backgroundSource = it->second->source; alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); setBackgroundVolume(s_volume); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic:alSourcePlay"); } }
OPAL_SOUND_MGR bool SoundManager::resumeAllAudio( void ) { if ( mAudioSourcesInUseCount >= MAX_AUDIO_SOURCES ) return false; alGetError(); int sourceAudioState = 0; for ( int i=0; i<mAudioSourcesInUseCount; i++ ) { // Are we currently playing the audio source? alGetSourcei( mAudioSources[i], AL_SOURCE_STATE, &sourceAudioState ); if ( sourceAudioState == AL_PAUSED ) { resumeAudio( i ); } } if ( checkALError( "resumeAllAudio::alSourceStop ") ) return false; return true; }
OPAL_SOUND_MGR bool SoundManager::playAudio( unsigned int audioID, bool forceRestart ) { // Make sure the audio source ident is valid and usable if ( audioID >= MAX_AUDIO_SOURCES || !mAudioSourceInUse[audioID]) return false; int sourceAudioState = 0; alGetError(); // Are we currently playing the audio source? alGetSourcei( mAudioSources[audioID], AL_SOURCE_STATE, &sourceAudioState ); if ( sourceAudioState == AL_PLAYING ) { if ( forceRestart ) stopAudio( audioID ); else return false; // Not forced, so we don't do anything } alSourcePlay( mAudioSources[ audioID ] ); if ( checkALError( "playAudio::alSourcePlay: ") ) return false; return true; }
static guint gst_openal_sink_delay (GstAudioSink * asink) { GstOpenALSink *openal = GST_OPENAL_SINK (asink); ALint queued, state, offset, delay; ALCcontext *old; if (!openal->context) return 0; GST_OPENAL_SINK_LOCK (openal); old = pushContext (openal->context); delay = 0; alGetSourcei (openal->sID, AL_BUFFERS_QUEUED, &queued); /* Order here is important. If the offset is queried after the state and an * underrun occurs in between the two calls, it can end up with a 0 offset * in a playing state, incorrectly reporting a len*queued/bps delay. */ alGetSourcei (openal->sID, AL_BYTE_OFFSET, &offset); alGetSourcei (openal->sID, AL_SOURCE_STATE, &state); /* Note: state=stopped is an underrun, meaning all buffers are processed * and there's no delay when writing the next buffer. Pre-buffering is * state=initial, which will introduce a delay while writing. */ if (checkALError () == AL_NO_ERROR && state != AL_STOPPED) delay = ((queued * openal->bID_length) - offset) / openal->bytes_per_sample; popContext (old, openal->context); GST_OPENAL_SINK_UNLOCK (openal); return delay; }
static gboolean gst_openal_sink_unprepare (GstAudioSink * audiosink) { GstOpenALSink *sink = GST_OPENAL_SINK (audiosink); ALCcontext *old; if (!sink->default_context) return TRUE; old = pushContext (sink->default_context); alSourceStop (sink->default_source); alSourcei (sink->default_source, AL_BUFFER, 0); if (!sink->user_source) alDeleteSources (1, &sink->default_source); sink->default_source = 0; alDeleteBuffers (sink->buffer_count, sink->buffers); g_free (sink->buffers); sink->buffers = NULL; sink->buffer_idx = 0; sink->buffer_count = 0; sink->buffer_length = 0; checkALError (); popContext (old, sink->default_context); if (!sink->user_context) alcDestroyContext (sink->default_context); sink->default_context = NULL; return TRUE; }
void SimpleAudioEngine::pauseBackgroundMusic() { ALint state; alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state); if (state == AL_PLAYING) alSourcePause(s_backgroundSource); checkALError("pauseBackgroundMusic"); }
void SimpleAudioEngine::resumeBackgroundMusic() { ALint state; alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state); if (state == AL_PAUSED) alSourcePlay(s_backgroundSource); checkALError("resumeBackgroundMusic"); }
void checkAndPrintALError(const char *file, int line) { #ifdef _DEBUG auto err = checkALError(); if (!err.empty()) base::glog << "OpenAL error:" << err << ". File:" << file << ". Line:" << line << base::logwarn; #endif }
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId) { ALint state; alGetSourcei(nSoundId, AL_SOURCE_STATE, &state); if (state == AL_PLAYING) alSourcePause(nSoundId); checkALError("pauseEffect:alSourcePause"); }
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId) { ALint state; alGetSourcei(nSoundId, AL_SOURCE_STATE, &state); if (state == AL_PAUSED) alSourcePlay(nSoundId); checkALError("resumeEffect:alSourcePlay"); }
void SimpleAudioEngine::end() { checkALError("end"); // clear all the sounds EffectsMap::const_iterator end = s_effects.end(); for (EffectsMap::iterator it = s_effects.begin(); it != end; it++) { alSourceStop(it->second->source); checkALError("end"); alDeleteBuffers(1, &it->second->buffer); checkALError("end"); alDeleteSources(1, &it->second->source); checkALError("end"); delete it->second; } s_effects.clear(); // and the background too stopBackground(true); for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it) { alSourceStop(it->second->source); checkALError("end"); alDeleteBuffers(1, &it->second->buffer); checkALError("end"); alDeleteSources(1, &it->second->source); checkALError("end"); delete it->second; } s_backgroundMusics.clear(); }
void RemoveSound() { alSourceStop(source); checkALError(); alDeleteSources(1, &source); checkALError(); alDeleteBuffers(BUFFER_QUANTITY, buffers); checkALError(); // Reset the current context to NULL. alcMakeContextCurrent(NULL); checkALCError(); // RELEASE the context and the device. alcDestroyContext(pContext); checkALCError(); alcCloseDevice(pDevice); }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (!s_isBackgroundInitialized) preloadBackgroundMusic(pszFilePath); alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic"); }
SimpleAudioEngine::SimpleAudioEngine() { alutInit(0, 0); #ifdef ENABLE_MPG123 mpg123_init(); #endif checkALError("SimpleAudioEngine:alutInit"); OpenALDecoder::installDecoders(); }
void LLAudioEngine_OpenAL::cleanupWind() { llinfos << "LLAudioEngine_OpenAL::cleanupWind()" << llendl; alDeleteBuffers(mNumWindBuffers,mWindBuffers); alDeleteSources(1, &mWindSource); checkALError(); }
void SimpleAudioEngine::pauseAllEffects() { EffectsMap::iterator iter = s_effects.begin(); if (iter != s_effects.end()) { alSourcePause(iter->second->source); checkALError("pauseAllEffects"); } }
OPAL_SOUND_MGR bool SoundManager::setListenerPosition( Vector3 position, Vector3 velocity, Quaternion orientation ) { Vector3 axis; // Set the position ALfloat pos[] = { position.x, position.y, position.z }; alListenerfv( AL_POSITION, pos ); if ( checkALError( "setListenerPosition::alListenerfv:AL_POSITION" ) ) return false; // Set the veclocity ALfloat vel[] = { velocity.x, velocity.y, velocity.z }; alListenerfv( AL_VELOCITY, vel ); if ( checkALError( "setListenerPosition::alListenerfv:AL_VELOCITY" ) ) return false; // Orientation of the listener : look at then look up axis = Vector3::ZERO; axis.x = orientation.getYaw().valueRadians(); axis.y = orientation.getPitch().valueRadians(); axis.z = orientation.getRoll().valueRadians(); // Set the direction ALfloat dir[] = { axis.x, axis.y, axis.z }; alListenerfv( AL_ORIENTATION, dir ); if ( checkALError( "setListenerPosition::alListenerfv:AL_DIRECTION" ) ) return false; // TODO as parameters ? alListenerf( AL_MAX_DISTANCE, 10000.0f ); alListenerf( AL_MIN_GAIN, 0.0f ); alListenerf( AL_MAX_GAIN, 1.0f ); alListenerf( AL_GAIN, 1.0f ); return true; }
bool SimpleAudioEngine::isBackgroundMusicPlaying() { // If there is no source, then there is nothing that is playing if (s_backgroundSource == AL_NONE) return false; ALint play_status; alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &play_status); checkALError("isBackgroundMusicPlaying:alGetSourcei"); return (play_status == AL_PLAYING); }