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; }
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; }
void SimpleAudioEngine::preloadEffect(const char* pszFilePath) { EffectsMap::iterator iter = s_effects.find(pszFilePath); // check if we have this already if (iter == s_effects.end()) { ALuint buffer; ALuint source; soundData *data = new soundData; string path = pszFilePath; buffer = alutCreateBufferFromFile(path.data()); if (buffer == AL_NONE) { fprintf(stderr, "Error loading file: '%s'\n", path.data()); alDeleteBuffers(1, &buffer); return; } alGenSources(1, &source); alSourcei(source, AL_BUFFER, buffer); data->isLooped = false; data->buffer = buffer; data->source = source; s_effects.insert(EffectsMap::value_type(pszFilePath, data)); } }
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)); } }
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop, float pitch, float pan, float gain) { std::string fullPath = FileUtils::getInstance()->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:init"); soundData &d = *iter->second; d.isLooped = bLoop; d.pitch = pitch; d.pan = pan; d.gain = gain; alSourcei(d.source, AL_LOOPING, d.isLooped ? AL_TRUE : AL_FALSE); alSourcef(d.source, AL_GAIN, s_effectVolume * d.gain); alSourcef(d.source, AL_PITCH, d.pitch); float sourcePosAL[] = {d.pan, 0.0f, 0.0f};//Set position - just using left and right panning alSourcefv(d.source, AL_POSITION, sourcePosAL); alSourcePlay(d.source); checkALError("playEffect:alSourcePlay"); return d.source; }
void SimpleAudioEngine::unloadEffect(const char* pszFilePath) { EffectsMap::iterator iter = s_effects.find(pszFilePath); if (iter != s_effects.end()) { alSourceStop(iter->second->source); alDeleteSources(1, &iter->second->source); alDeleteBuffers(1, &iter->second->buffer); delete iter->second; int err = alGetError(); if (err != AL_NO_ERROR) printALError(err); s_effects.erase(iter); } }
void SimpleAudioEngine::unloadEffect(const char* pszFilePath) { EffectsMap::iterator iter = s_effects.find(pszFilePath); if (iter != s_effects.end()) { checkALError("unloadEffect"); alSourceStop(iter->second->source); checkALError("unloadEffect"); alDeleteSources(1, &iter->second->source); checkALError("unloadEffect"); alDeleteBuffers(1, &iter->second->buffer); checkALError("unloadEffect"); delete iter->second; s_effects.erase(iter); } }
void SimpleAudioEngine::unloadEffect(const char* pszFilePath) { // Changing file path to full path std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath); EffectsMap::iterator iter = s_effects.find(fullPath); if (iter != s_effects.end()) { checkALError("unloadEffect:init"); alSourceStop(iter->second->source); checkALError("unloadEffect:alSourceStop"); alDeleteSources(1, &iter->second->source); checkALError("unloadEffect:DeletSources"); alDeleteBuffers(1, &iter->second->buffer); checkALError("unloadEffect:alDeleteBuffers"); delete iter->second; s_effects.erase(iter); } }