2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "audio/sound_manager.hpp"
25 #include "audio/dummy_sound_source.hpp"
26 #include "audio/sound_file.hpp"
27 #include "audio/stream_sound_source.hpp"
28 #include "util/log.hpp"
31 /** Older openal versions often miss this function and it isn't that vital for
37 # define alcGetString(x,y) ""
40 SoundManager::SoundManager() :
52 device = alcOpenDevice(0);
54 throw std::runtime_error("Couldn't open audio device.");
57 int attributes[] = { 0 };
58 context = alcCreateContext(device, attributes);
59 check_alc_error("Couldn't create audio context: ");
60 alcMakeContextCurrent(context);
61 check_alc_error("Couldn't select audio context: ");
63 check_al_error("Audio error after init: ");
66 } catch(std::exception& e) {
68 alcDestroyContext(context);
71 alcCloseDevice(device);
73 log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
74 print_openal_version();
78 SoundManager::~SoundManager()
82 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
86 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
87 ALuint buffer = i->second;
88 alDeleteBuffers(1, &buffer);
92 alcDestroyContext(context);
95 alcCloseDevice(device);
100 SoundManager::load_file_into_buffer(SoundFile* file)
102 ALenum format = get_sample_format(file);
104 alGenBuffers(1, &buffer);
105 check_al_error("Couldn't create audio buffer: ");
106 char* samples = new char[file->size];
108 file->read(samples, file->size);
109 alBufferData(buffer, format, samples,
110 static_cast<ALsizei> (file->size),
111 static_cast<ALsizei> (file->rate));
112 check_al_error("Couldn't fill audio buffer: ");
123 SoundManager::intern_create_sound_source(const std::string& filename)
125 assert(sound_enabled);
127 std::auto_ptr<OpenALSoundSource> source (new OpenALSoundSource());
131 // reuse an existing static sound buffer
132 SoundBuffers::iterator i = buffers.find(filename);
133 if(i != buffers.end()) {
137 std::auto_ptr<SoundFile> file (load_sound_file(filename));
139 if(file->size < 100000) {
140 buffer = load_file_into_buffer(file.get());
141 buffers.insert(std::make_pair(filename, buffer));
143 StreamSoundSource* source = new StreamSoundSource();
144 source->set_sound_file(file.release());
148 log_debug << "Uncached sound \"" << filename << "\" requested to be played" << std::endl;
151 alSourcei(source->source, AL_BUFFER, buffer);
152 return source.release();
156 SoundManager::create_sound_source(const std::string& filename)
159 return create_dummy_sound_source();
162 return intern_create_sound_source(filename);
163 } catch(std::exception &e) {
164 log_warning << "Couldn't create audio source: " << e.what() << std::endl;
165 return create_dummy_sound_source();
170 SoundManager::preload(const std::string& filename)
175 SoundBuffers::iterator i = buffers.find(filename);
177 if(i != buffers.end())
180 std::auto_ptr<SoundFile> file (load_sound_file(filename));
181 // only keep small files
182 if(file->size >= 100000)
185 ALuint buffer = load_file_into_buffer(file.get());
186 buffers.insert(std::make_pair(filename, buffer));
187 } catch(std::exception& e) {
188 log_warning << "Error while preloading sound file: " << e.what() << std::endl;
193 SoundManager::play(const std::string& filename, const Vector& pos)
199 std::auto_ptr<OpenALSoundSource> source
200 (intern_create_sound_source(filename));
202 if(pos.x < 0 || pos.y < 0) {
203 source->set_relative(true);
205 source->set_position(pos);
208 sources.push_back(source.release());
209 } catch(std::exception& e) {
210 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
215 SoundManager::manage_source(SoundSource* source)
217 assert(source != NULL);
219 OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
220 if(openal_source != NULL) {
221 sources.push_back(openal_source);
226 SoundManager::register_for_update( StreamSoundSource* sss ){
228 update_list.push_back( sss );
233 SoundManager::remove_from_update( StreamSoundSource* sss ){
235 StreamSoundSources::iterator i = update_list.begin();
236 while( i != update_list.end() ){
238 i = update_list.erase(i);
247 SoundManager::enable_sound(bool enable)
252 sound_enabled = enable;
256 SoundManager::enable_music(bool enable)
261 music_enabled = enable;
263 play_music(current_music);
273 SoundManager::stop_music(float fadetime)
277 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
278 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
287 SoundManager::play_music(const std::string& filename, bool fade)
289 if(filename == current_music && music_source != NULL)
291 current_music = filename;
302 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
303 newmusic->set_sound_file(load_sound_file(filename));
304 newmusic->set_looping(true);
305 newmusic->set_relative(true);
307 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
311 music_source = newmusic.release();
312 } catch(std::exception& e) {
313 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
318 SoundManager::set_listener_position(const Vector& pos)
320 static Uint32 lastticks = SDL_GetTicks();
322 Uint32 current_ticks = SDL_GetTicks();
323 if(current_ticks - lastticks < 300)
325 lastticks = current_ticks;
327 alListener3f(AL_POSITION, pos.x, pos.y, 0);
331 SoundManager::set_listener_velocity(const Vector& vel)
333 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
337 SoundManager::update()
339 static Uint32 lasttime = SDL_GetTicks();
340 Uint32 now = SDL_GetTicks();
342 if(now - lasttime < 300)
346 // update and check for finished sound sources
347 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
348 OpenALSoundSource* source = *i;
352 if(!source->playing()) {
354 i = sources.erase(i);
359 // check streaming sounds
361 music_source->update();
366 alcProcessContext(context);
367 check_alc_error("Error while processing audio context: ");
370 //run update() for stream_sound_source
371 StreamSoundSources::iterator s = update_list.begin();
372 while( s != update_list.end() ){
379 SoundManager::get_sample_format(SoundFile* file)
381 if(file->channels == 2) {
382 if(file->bits_per_sample == 16) {
383 return AL_FORMAT_STEREO16;
384 } else if(file->bits_per_sample == 8) {
385 return AL_FORMAT_STEREO8;
387 throw std::runtime_error("Only 16 and 8 bit samples supported");
389 } else if(file->channels == 1) {
390 if(file->bits_per_sample == 16) {
391 return AL_FORMAT_MONO16;
392 } else if(file->bits_per_sample == 8) {
393 return AL_FORMAT_MONO8;
395 throw std::runtime_error("Only 16 and 8 bit samples supported");
399 throw std::runtime_error("Only 1 and 2 channel samples supported");
403 SoundManager::print_openal_version()
405 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
406 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
407 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
408 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
412 SoundManager::check_alc_error(const char* message)
414 int err = alcGetError(device);
415 if(err != ALC_NO_ERROR) {
416 std::stringstream msg;
417 msg << message << alcGetString(device, err);
418 throw std::runtime_error(msg.str());
423 SoundManager::check_al_error(const char* message)
425 int err = alGetError();
426 if(err != AL_NO_ERROR) {
427 std::stringstream msg;
428 msg << message << alGetString(err);
429 throw std::runtime_error(msg.str());