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"
29 #include "sound_file.hpp"
30 #include "sound_source.hpp"
31 #include "openal_sound_source.hpp"
32 #include "stream_sound_source.hpp"
33 #include "dummy_sound_source.hpp"
37 SoundManager* sound_manager = 0;
39 SoundManager::SoundManager()
40 : device(0), context(0), sound_enabled(false), music_source(0),
44 device = alcOpenDevice(0);
46 throw std::runtime_error("Couldn't open audio device.");
49 int attributes[] = { 0 };
50 context = alcCreateContext(device, attributes);
51 check_alc_error("Couldn't create audio context: ");
52 alcMakeContextCurrent(context);
53 check_alc_error("Couldn't select audio context: ");
55 check_al_error("Audio error after init: ");
58 } catch(std::exception& e) {
60 alcDestroyContext(context);
63 alcCloseDevice(device);
65 log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
66 print_openal_version();
70 SoundManager::~SoundManager()
74 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
78 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
79 ALuint buffer = i->second;
80 alDeleteBuffers(1, &buffer);
84 alcDestroyContext(context);
87 alcCloseDevice(device);
92 SoundManager::load_file_into_buffer(SoundFile* file)
94 ALenum format = get_sample_format(file);
96 alGenBuffers(1, &buffer);
97 check_al_error("Couldn't create audio buffer: ");
98 char* samples = new char[file->size];
100 file->read(samples, file->size);
101 alBufferData(buffer, format, samples,
102 static_cast<ALsizei> (file->size),
103 static_cast<ALsizei> (file->rate));
104 check_al_error("Couldn't fill audio buffer: ");
115 SoundManager::create_sound_source(const std::string& filename)
118 return create_dummy_sound_source();
120 std::auto_ptr<OpenALSoundSource> source;
122 source.reset(new OpenALSoundSource());
123 } catch(std::exception& e) {
124 log_warning << "Couldn't create audio source: " << e.what() << std::endl;
125 return create_dummy_sound_source();
130 // reuse an existing static sound buffer
131 SoundBuffers::iterator i = buffers.find(filename);
132 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());
147 } catch(std::exception& e) {
148 log_warning << "Couldn't load soundfile '" << filename << "': " << e.what() << std::endl;
149 return create_dummy_sound_source();
153 alSourcei(source->source, AL_BUFFER, buffer);
154 return source.release();
158 SoundManager::preload(const std::string& filename)
163 SoundBuffers::iterator i = buffers.find(filename);
165 if(i != buffers.end())
168 std::auto_ptr<SoundFile> file (load_sound_file(filename));
169 // only keep small files
170 if(file->size >= 100000)
173 ALuint buffer = load_file_into_buffer(file.get());
174 buffers.insert(std::make_pair(filename, buffer));
178 SoundManager::play(const std::string& filename, const Vector& pos)
184 std::auto_ptr<OpenALSoundSource> source
185 (static_cast<OpenALSoundSource*> (create_sound_source(filename)));
187 if(pos == Vector(-1, -1)) {
188 source->set_rollof_factor(0);
190 source->set_position(pos);
193 sources.push_back(source.release());
194 } catch(std::exception& e) {
195 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
200 SoundManager::manage_source(SoundSource* source)
202 assert(source != NULL);
204 OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
205 if(openal_source != NULL) {
206 sources.push_back(openal_source);
211 SoundManager::register_for_update( StreamSoundSource* sss ){
213 update_list.push_back( sss );
218 SoundManager::remove_from_update( StreamSoundSource* sss ){
220 StreamSoundSources::iterator i = update_list.begin();
221 while( i != update_list.end() ){
223 i = update_list.erase(i);
232 SoundManager::enable_sound(bool enable)
237 sound_enabled = enable;
241 SoundManager::enable_music(bool enable)
246 music_enabled = enable;
248 play_music(current_music);
258 SoundManager::stop_music(float fadetime)
262 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
263 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
272 SoundManager::play_music(const std::string& filename, bool fade)
274 if(filename == current_music && music_source != NULL)
276 current_music = filename;
287 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
288 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
289 newmusic->set_sound_file(load_sound_file(filename));
290 newmusic->set_looping(true);
292 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
296 music_source = newmusic.release();
297 } catch(std::exception& e) {
298 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
303 SoundManager::set_listener_position(const Vector& pos)
305 static Uint32 lastticks = 0;
307 Uint32 current_ticks = SDL_GetTicks();
308 if(current_ticks - lastticks < 300)
310 lastticks = current_ticks;
312 alListener3f(AL_POSITION, pos.x, pos.y, 0);
316 SoundManager::set_listener_velocity(const Vector& vel)
318 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
322 SoundManager::update()
324 static float lasttime = real_time;
326 if(real_time - lasttime < 0.3)
328 lasttime = real_time;
330 // update and check for finished sound sources
331 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
332 OpenALSoundSource* source = *i;
336 if(!source->playing()) {
338 i = sources.erase(i);
343 // check streaming sounds
345 music_source->update();
350 alcProcessContext(context);
351 check_alc_error("Error while processing audio context: ");
354 //run update() for stream_sound_source
355 StreamSoundSources::iterator s = update_list.begin();
356 while( s != update_list.end() ){
363 SoundManager::get_sample_format(SoundFile* file)
365 if(file->channels == 2) {
366 if(file->bits_per_sample == 16) {
367 return AL_FORMAT_STEREO16;
368 } else if(file->bits_per_sample == 8) {
369 return AL_FORMAT_STEREO8;
371 throw std::runtime_error("Only 16 and 8 bit samples supported");
373 } else if(file->channels == 1) {
374 if(file->bits_per_sample == 16) {
375 return AL_FORMAT_MONO16;
376 } else if(file->bits_per_sample == 8) {
377 return AL_FORMAT_MONO8;
379 throw std::runtime_error("Only 16 and 8 bit samples supported");
383 throw std::runtime_error("Only 1 and 2 channel samples supported");
387 SoundManager::print_openal_version()
389 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
390 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
391 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
392 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
396 SoundManager::check_alc_error(const char* message)
398 int err = alcGetError(device);
399 if(err != ALC_NO_ERROR) {
400 std::stringstream msg;
401 msg << message << alcGetString(device, err);
402 throw std::runtime_error(msg.str());
407 SoundManager::check_al_error(const char* message)
409 int err = alGetError();
410 if(err != AL_NO_ERROR) {
411 std::stringstream msg;
412 msg << message << alGetString(err);
413 throw std::runtime_error(msg.str());