4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "sound_manager.hpp"
30 #include "sound_file.hpp"
31 #include "sound_source.hpp"
32 #include "openal_sound_source.hpp"
33 #include "stream_sound_source.hpp"
34 #include "dummy_sound_source.hpp"
39 /** Older openal versions often miss this function and it isn't that vital for
45 #define alcGetString(x,y) ""
48 SoundManager* sound_manager = 0;
50 SoundManager::SoundManager()
51 : device(0), context(0), sound_enabled(false), music_source(0),
55 device = alcOpenDevice(0);
57 throw std::runtime_error("Couldn't open audio device.");
60 int attributes[] = { 0 };
61 context = alcCreateContext(device, attributes);
62 check_alc_error("Couldn't create audio context: ");
63 alcMakeContextCurrent(context);
64 check_alc_error("Couldn't select audio context: ");
66 check_al_error("Audio error after init: ");
69 } catch(std::exception& e) {
71 alcDestroyContext(context);
74 alcCloseDevice(device);
76 log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
77 print_openal_version();
81 SoundManager::~SoundManager()
85 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
89 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
90 ALuint buffer = i->second;
91 alDeleteBuffers(1, &buffer);
95 alcDestroyContext(context);
98 alcCloseDevice(device);
103 SoundManager::load_file_into_buffer(SoundFile* file)
105 ALenum format = get_sample_format(file);
107 alGenBuffers(1, &buffer);
108 check_al_error("Couldn't create audio buffer: ");
109 char* samples = new char[file->size];
111 file->read(samples, file->size);
112 alBufferData(buffer, format, samples,
113 static_cast<ALsizei> (file->size),
114 static_cast<ALsizei> (file->rate));
115 check_al_error("Couldn't fill audio buffer: ");
126 SoundManager::intern_create_sound_source(const std::string& filename)
129 throw std::runtime_error("sound disabled");
131 std::auto_ptr<OpenALSoundSource> source (new OpenALSoundSource());
135 // reuse an existing static sound buffer
136 SoundBuffers::iterator i = buffers.find(filename);
137 if(i != buffers.end()) {
141 std::auto_ptr<SoundFile> file (load_sound_file(filename));
143 if(file->size < 100000) {
144 buffer = load_file_into_buffer(file.get());
145 buffers.insert(std::make_pair(filename, buffer));
147 StreamSoundSource* source = new StreamSoundSource();
148 source->set_sound_file(file.release());
152 log_debug << "Uncached sound \"" << filename << "\" requested to be played" << std::endl;
155 alSourcei(source->source, AL_BUFFER, buffer);
156 return source.release();
160 SoundManager::create_sound_source(const std::string& filename)
163 return create_dummy_sound_source();
166 return intern_create_sound_source(filename);
167 } catch(std::exception &e) {
168 log_warning << "Couldn't create audio source: " << e.what() << std::endl;
169 return create_dummy_sound_source();
174 SoundManager::preload(const std::string& filename)
179 SoundBuffers::iterator i = buffers.find(filename);
181 if(i != buffers.end())
184 std::auto_ptr<SoundFile> file (load_sound_file(filename));
185 // only keep small files
186 if(file->size >= 100000)
189 ALuint buffer = load_file_into_buffer(file.get());
190 buffers.insert(std::make_pair(filename, buffer));
194 SoundManager::play(const std::string& filename, const Vector& pos)
200 std::auto_ptr<OpenALSoundSource> source
201 (intern_create_sound_source(filename));
203 if(pos == Vector(-1, -1)) {
204 source->set_rollof_factor(0);
206 source->set_position(pos);
209 sources.push_back(source.release());
210 } catch(std::exception& e) {
211 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
216 SoundManager::manage_source(SoundSource* source)
218 assert(source != NULL);
220 OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
221 if(openal_source != NULL) {
222 sources.push_back(openal_source);
227 SoundManager::register_for_update( StreamSoundSource* sss ){
229 update_list.push_back( sss );
234 SoundManager::remove_from_update( StreamSoundSource* sss ){
236 StreamSoundSources::iterator i = update_list.begin();
237 while( i != update_list.end() ){
239 i = update_list.erase(i);
248 SoundManager::enable_sound(bool enable)
253 sound_enabled = enable;
257 SoundManager::enable_music(bool enable)
262 music_enabled = enable;
264 play_music(current_music);
274 SoundManager::stop_music(float fadetime)
278 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
279 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
288 SoundManager::play_music(const std::string& filename, bool fade)
290 if(filename == current_music && music_source != NULL)
292 current_music = filename;
303 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
304 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
305 newmusic->set_sound_file(load_sound_file(filename));
306 newmusic->set_looping(true);
308 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
312 music_source = newmusic.release();
313 } catch(std::exception& e) {
314 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
319 SoundManager::set_listener_position(const Vector& pos)
321 static Uint32 lastticks = SDL_GetTicks();
323 Uint32 current_ticks = SDL_GetTicks();
324 if(current_ticks - lastticks < 300)
326 lastticks = current_ticks;
328 alListener3f(AL_POSITION, pos.x, pos.y, 0);
332 SoundManager::set_listener_velocity(const Vector& vel)
334 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
338 SoundManager::update()
340 static Uint32 lasttime = SDL_GetTicks();
341 Uint32 now = SDL_GetTicks();
343 if(now - lasttime < 300)
347 // update and check for finished sound sources
348 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
349 OpenALSoundSource* source = *i;
353 if(!source->playing()) {
355 i = sources.erase(i);
360 // check streaming sounds
362 music_source->update();
367 alcProcessContext(context);
368 check_alc_error("Error while processing audio context: ");
371 //run update() for stream_sound_source
372 StreamSoundSources::iterator s = update_list.begin();
373 while( s != update_list.end() ){
380 SoundManager::get_sample_format(SoundFile* file)
382 if(file->channels == 2) {
383 if(file->bits_per_sample == 16) {
384 return AL_FORMAT_STEREO16;
385 } else if(file->bits_per_sample == 8) {
386 return AL_FORMAT_STEREO8;
388 throw std::runtime_error("Only 16 and 8 bit samples supported");
390 } else if(file->channels == 1) {
391 if(file->bits_per_sample == 16) {
392 return AL_FORMAT_MONO16;
393 } else if(file->bits_per_sample == 8) {
394 return AL_FORMAT_MONO8;
396 throw std::runtime_error("Only 16 and 8 bit samples supported");
400 throw std::runtime_error("Only 1 and 2 channel samples supported");
404 SoundManager::print_openal_version()
406 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
407 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
408 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
409 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
413 SoundManager::check_alc_error(const char* message)
415 int err = alcGetError(device);
416 if(err != ALC_NO_ERROR) {
417 std::stringstream msg;
418 msg << message << alcGetString(device, err);
419 throw std::runtime_error(msg.str());
424 SoundManager::check_al_error(const char* message)
426 int err = alGetError();
427 if(err != AL_NO_ERROR) {
428 std::stringstream msg;
429 msg << message << alGetString(err);
430 throw std::runtime_error(msg.str());