OpenALSoundSource* SoundManager::intern_create_sound_source(const std::string& filename) { assert(sound_enabled); std::auto_ptr<OpenALSoundSource> source (new OpenALSoundSource()); ALuint buffer; // reuse an existing static sound buffer SoundBuffers::iterator i = buffers.find(filename); if(i != buffers.end()) { buffer = i->second; } else { // Load sound file std::auto_ptr<SoundFile> file (load_sound_file(filename)); if(file->size < 100000) { buffer = load_file_into_buffer(file.get()); buffers.insert(std::make_pair(filename, buffer)); } else { StreamSoundSource* source = new StreamSoundSource(); source->set_sound_file(file.release()); return source; } log_debug << "Uncached sound \"" << filename << "\" requested to be played" << std::endl; } alSourcei(source->source, AL_BUFFER, buffer); return source.release(); }
SoundSource* SoundManager::create_sound_source(const std::string& filename) { if(!sound_enabled) return 0; ALuint buffer; // reuse an existing static sound buffer SoundBuffers::iterator i = buffers.find(filename); if(i != buffers.end()) { buffer = i->second; } else { // Load sound file std::auto_ptr<SoundFile> file (load_sound_file(filename)); if(file->size < 100000) { buffer = load_file_into_buffer(file.get()); buffers.insert(std::make_pair(filename, buffer)); } else { StreamSoundSource* source = new StreamSoundSource(); source->set_sound_file(file.release()); return source; } } SoundSource* source = new SoundSource(); alSourcei(source->source, AL_BUFFER, buffer); return source; }
void SoundPlayerOpenAL::play(StrSound* strSound, int64 fadeOn) { assert(strSound != 0); try { StreamSoundSource* source = findStreamSoundSource(); source->play(strSound, fadeOn); } catch(std::exception& e) { std::cerr << "Couldn't play streaming sound: " << e.what() << "\n"; } }
void SoundPlayerOpenAL::stop(StrSound* strSound, int64 fadeOff) { assert(strSound != 0); for(StreamSoundSources::iterator i = streamSources.begin(); i != streamSources.end(); ++i) { StreamSoundSource* source = *i; if(source->sound == strSound) { source->stop(fadeOff); } } }
void SoundPlayerOpenAL::stopAllSounds() { for(StaticSoundSources::iterator i = staticSources.begin(); i != staticSources.end(); ++i) { StaticSoundSource* source = *i; source->stop(); } for(StreamSoundSources::iterator i = streamSources.begin(); i != streamSources.end(); ++i) { StreamSoundSource* source = *i; source->stop(); } }
StreamSoundSource* SoundPlayerOpenAL::findStreamSoundSource() { // try to find a stopped source for(StreamSoundSources::iterator i = streamSources.begin(); i != streamSources.end(); ++i) { StreamSoundSource* source = *i; if(! source->playing()) { return source; } } // create a new source if(streamSources.size() >= params.strBufferCount) { throw std::runtime_error("Too many stream sounds played at once"); } StreamSoundSource* source = new StreamSoundSource(); streamSources.push_back(source); return source; }
void SoundPlayerOpenAL::updateStreams() { assert(context != 0); try { for(StreamSoundSources::iterator i = streamSources.begin(); i != streamSources.end(); ++i) { StreamSoundSource* source = *i; try { source->update(); } catch(std::exception& e) { std::cerr << "Error while updating sound stream: " << e.what() << "\n"; } } alcProcessContext(context); checkAlcError("Error while processing audio context: "); } catch(...) { printOpenALInfo(); throw; } }