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"); } }
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 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 SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (s_backgroundSource != AL_NONE) stopBackgroundMusic(false); // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->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); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic"); } }
void SimpleAudioEngine::end() { checkALError("end:init"); // clear all the sound effects EffectsMap::const_iterator end = s_effects.end(); for (auto it = s_effects.begin(); it != end; ++it) { alSourceStop(it->second->source); checkALError("end:alSourceStop"); alDeleteSources(1, &it->second->source); checkALError("end:alDeleteSources"); alDeleteBuffers(1, &it->second->buffer); checkALError("end:alDeleteBuffers"); delete it->second; } s_effects.clear(); // and the background music too stopBackground(true); for (auto it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it) { alSourceStop(it->second->source); checkALError("end:alSourceStop"); alDeleteSources(1, &it->second->source); checkALError("end:alDeleteSources"); alDeleteBuffers(1, &it->second->buffer); checkALError("end:alDeleteBuffers"); delete it->second; } s_backgroundMusics.clear(); CC_SAFE_DELETE(s_engine); }
void SimpleAudioEngine::end() { // clear all the sounds EffectsMap::const_iterator end = s_effects.end(); for (EffectsMap::iterator it = s_effects.begin(); it != end; it++) { Mix_FreeChunk(it->second->chunk); delete it->second; } s_effects.clear(); // and the background too stopBackground(true); for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it) { Mix_FreeMusic(it->second->music); delete it->second; } s_backgroundMusics.clear(); }
// // background audio // void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) { // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { ALuint buffer = AL_NONE; if (isOGGFile(fullPath.data())) { buffer = createBufferFromOGG(fullPath.data()); } else { buffer = alutCreateBufferFromFile(fullPath.data()); } checkALError("preloadBackgroundMusic"); if (buffer == AL_NONE) { fprintf(stderr, "Error loading file: '%s'\n", fullPath.data()); alDeleteBuffers(1, &buffer); return; } ALuint source = AL_NONE; alGenSources(1, &source); checkALError("preloadBackgroundMusic"); alSourcei(source, AL_BUFFER, buffer); checkALError("preloadBackgroundMusic"); backgroundMusicData* data = new backgroundMusicData(); data->buffer = buffer; data->source = source; s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data)); } }
static void stopBackground(bool bReleaseData) { alSourceStop(s_backgroundSource); if (bReleaseData) { for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it) { if (it->second->source == s_backgroundSource) { alDeleteBuffers(1, &it->second->buffer); checkALError("stopBackground"); alDeleteSources(1, &it->second->source); checkALError("stopBackground"); delete it->second; s_backgroundMusics.erase(it); break; } } } s_backgroundSource = AL_NONE; }
// // background audio // void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) { // 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()) { ALuint buffer = AL_NONE; bool success = false; 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; } 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(); ALuint source = AL_NONE; alGenSources(1, &source); checkALError("preloadBackgroundMusic:alGenSources"); alSourcei(source, AL_BUFFER, buffer); checkALError("preloadBackgroundMusic:alSourcei"); backgroundMusicData* data = new backgroundMusicData(); data->buffer = buffer; data->source = source; s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data)); } }