- // TODO keep track of the object later and move the sound along with the
- // object.
- play_sound(sound, object->get_pos(), pos);
+ if(!sound_enabled)
+ throw std::runtime_error("sound disabled");
+
+ 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 create_dummy_sound_source();
+
+ try {
+ return intern_create_sound_source(filename);
+ } catch(std::exception &e) {
+ log_warning << "Couldn't create audio source: " << e.what() << std::endl;
+ return create_dummy_sound_source();
+ }