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"
38 SoundManager* sound_manager = 0;
40 SoundManager::SoundManager()
41 : device(0), context(0), sound_enabled(false), music_source(0),
45 device = alcOpenDevice(0);
47 throw std::runtime_error("Couldn't open audio device.");
50 int attributes[] = { 0 };
51 context = alcCreateContext(device, attributes);
52 check_alc_error("Couldn't create audio context: ");
53 alcMakeContextCurrent(context);
54 check_alc_error("Couldn't select audio context: ");
56 check_al_error("Audio error after init: ");
59 } catch(std::exception& e) {
61 alcDestroyContext(context);
64 alcCloseDevice(device);
66 log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
67 print_openal_version();
71 SoundManager::~SoundManager()
75 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
79 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
80 ALuint buffer = i->second;
81 alDeleteBuffers(1, &buffer);
85 alcDestroyContext(context);
88 alcCloseDevice(device);
93 SoundManager::load_file_into_buffer(SoundFile* file)
95 ALenum format = get_sample_format(file);
97 alGenBuffers(1, &buffer);
98 check_al_error("Couldn't create audio buffer: ");
99 char* samples = new char[file->size];
101 file->read(samples, file->size);
102 alBufferData(buffer, format, samples,
103 static_cast<ALsizei> (file->size),
104 static_cast<ALsizei> (file->rate));
105 check_al_error("Couldn't fill audio buffer: ");
116 SoundManager::create_sound_source(const std::string& filename)
119 return create_dummy_sound_source();
121 std::auto_ptr<OpenALSoundSource> source;
123 source.reset(new OpenALSoundSource());
124 } catch(std::exception& e) {
125 log_warning << "Couldn't create audio source: " << e.what() << std::endl;
126 return create_dummy_sound_source();
131 // reuse an existing static sound buffer
132 SoundBuffers::iterator i = buffers.find(filename);
133 if(i != buffers.end()) {
138 std::auto_ptr<SoundFile> file (load_sound_file(filename));
140 if(file->size < 100000) {
141 buffer = load_file_into_buffer(file.get());
142 buffers.insert(std::make_pair(filename, buffer));
144 StreamSoundSource* source = new StreamSoundSource();
145 source->set_sound_file(file.release());
148 } catch(std::exception& e) {
149 log_warning << "Couldn't load soundfile '" << filename << "': " << e.what() << std::endl;
150 return create_dummy_sound_source();
154 alSourcei(source->source, AL_BUFFER, buffer);
155 return source.release();
159 SoundManager::preload(const std::string& filename)
164 SoundBuffers::iterator i = buffers.find(filename);
166 if(i != buffers.end())
169 std::auto_ptr<SoundFile> file (load_sound_file(filename));
170 // only keep small files
171 if(file->size >= 100000)
174 ALuint buffer = load_file_into_buffer(file.get());
175 buffers.insert(std::make_pair(filename, buffer));
179 SoundManager::play(const std::string& filename, const Vector& pos)
185 std::auto_ptr<OpenALSoundSource> source
186 (static_cast<OpenALSoundSource*> (create_sound_source(filename)));
188 if(pos == Vector(-1, -1)) {
189 source->set_rollof_factor(0);
191 source->set_position(pos);
194 sources.push_back(source.release());
195 } catch(std::exception& e) {
196 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
201 SoundManager::manage_source(SoundSource* source)
203 assert(source != NULL);
205 OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
206 if(openal_source != NULL) {
207 sources.push_back(openal_source);
212 SoundManager::register_for_update( StreamSoundSource* sss ){
214 update_list.push_back( sss );
219 SoundManager::remove_from_update( StreamSoundSource* sss ){
221 StreamSoundSources::iterator i = update_list.begin();
222 while( i != update_list.end() ){
224 i = update_list.erase(i);
233 SoundManager::enable_sound(bool enable)
238 sound_enabled = enable;
242 SoundManager::enable_music(bool enable)
247 music_enabled = enable;
249 play_music(current_music);
259 SoundManager::stop_music(float fadetime)
263 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
264 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
273 SoundManager::play_music(const std::string& filename, bool fade)
275 if(filename == current_music && music_source != NULL)
277 current_music = filename;
288 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
289 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
290 newmusic->set_sound_file(load_sound_file(filename));
291 newmusic->set_looping(true);
293 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
297 music_source = newmusic.release();
298 } catch(std::exception& e) {
299 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
304 SoundManager::set_listener_position(const Vector& pos)
306 static Uint32 lastticks = SDL_GetTicks();
308 Uint32 current_ticks = SDL_GetTicks();
309 if(current_ticks - lastticks < 300)
311 lastticks = current_ticks;
313 alListener3f(AL_POSITION, pos.x, pos.y, 0);
317 SoundManager::set_listener_velocity(const Vector& vel)
319 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
323 SoundManager::update()
325 static Uint32 lasttime = SDL_GetTicks();
326 Uint32 now = SDL_GetTicks();
328 if(now - lasttime < 300)
332 // update and check for finished sound sources
333 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
334 OpenALSoundSource* source = *i;
338 if(!source->playing()) {
340 i = sources.erase(i);
345 // check streaming sounds
347 music_source->update();
352 alcProcessContext(context);
353 check_alc_error("Error while processing audio context: ");
356 //run update() for stream_sound_source
357 StreamSoundSources::iterator s = update_list.begin();
358 while( s != update_list.end() ){
365 SoundManager::get_sample_format(SoundFile* file)
367 if(file->channels == 2) {
368 if(file->bits_per_sample == 16) {
369 return AL_FORMAT_STEREO16;
370 } else if(file->bits_per_sample == 8) {
371 return AL_FORMAT_STEREO8;
373 throw std::runtime_error("Only 16 and 8 bit samples supported");
375 } else if(file->channels == 1) {
376 if(file->bits_per_sample == 16) {
377 return AL_FORMAT_MONO16;
378 } else if(file->bits_per_sample == 8) {
379 return AL_FORMAT_MONO8;
381 throw std::runtime_error("Only 16 and 8 bit samples supported");
385 throw std::runtime_error("Only 1 and 2 channel samples supported");
389 SoundManager::print_openal_version()
391 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
392 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
393 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
394 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
398 SoundManager::check_alc_error(const char* message)
400 int err = alcGetError(device);
401 if(err != ALC_NO_ERROR) {
402 std::stringstream msg;
403 msg << message << alcGetString(device, err);
404 throw std::runtime_error(msg.str());
409 SoundManager::check_al_error(const char* message)
411 int err = alGetError();
412 if(err != AL_NO_ERROR) {
413 std::stringstream msg;
414 msg << message << alGetString(err);
415 throw std::runtime_error(msg.str());