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();
122 // reuse an existing static sound buffer
123 SoundBuffers::iterator i = buffers.find(filename);
124 if(i != buffers.end()) {
129 std::auto_ptr<SoundFile> file (load_sound_file(filename));
131 if(file->size < 100000) {
132 buffer = load_file_into_buffer(file.get());
133 buffers.insert(std::make_pair(filename, buffer));
135 StreamSoundSource* source = new StreamSoundSource();
136 source->set_sound_file(file.release());
139 } catch(std::exception& e) {
140 log_warning << "Couldn't load soundfile '" << filename << "': " << e.what() << std::endl;
141 return create_dummy_sound_source();
145 OpenALSoundSource* source = new OpenALSoundSource();
146 alSourcei(source->source, AL_BUFFER, buffer);
151 SoundManager::preload(const std::string& filename)
156 SoundBuffers::iterator i = buffers.find(filename);
158 if(i != buffers.end())
161 std::auto_ptr<SoundFile> file (load_sound_file(filename));
162 // only keep small files
163 if(file->size >= 100000)
166 ALuint buffer = load_file_into_buffer(file.get());
167 buffers.insert(std::make_pair(filename, buffer));
171 SoundManager::play(const std::string& filename, const Vector& pos)
177 std::auto_ptr<OpenALSoundSource> source
178 (static_cast<OpenALSoundSource*> (create_sound_source(filename)));
180 if(pos == Vector(-1, -1)) {
181 source->set_rollof_factor(0);
183 source->set_position(pos);
186 sources.push_back(source.release());
187 } catch(std::exception& e) {
188 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
193 SoundManager::manage_source(SoundSource* source)
195 assert(source != NULL);
197 OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
198 if(openal_source != NULL) {
199 sources.push_back(openal_source);
204 SoundManager::register_for_update( StreamSoundSource* sss ){
206 update_list.push_back( sss );
211 SoundManager::remove_from_update( StreamSoundSource* sss ){
213 StreamSoundSources::iterator i = update_list.begin();
214 while( i != update_list.end() ){
216 i = update_list.erase(i);
225 SoundManager::enable_sound(bool enable)
230 sound_enabled = enable;
234 SoundManager::enable_music(bool enable)
239 music_enabled = enable;
241 play_music(current_music);
251 SoundManager::stop_music(float fadetime)
255 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
256 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
265 SoundManager::play_music(const std::string& filename, bool fade)
267 if(filename == current_music && music_source != NULL)
269 current_music = filename;
280 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
281 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
282 newmusic->set_sound_file(load_sound_file(filename));
283 newmusic->set_looping(true);
285 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
289 music_source = newmusic.release();
290 } catch(std::exception& e) {
291 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
296 SoundManager::set_listener_position(const Vector& pos)
298 static Uint32 lastticks = 0;
300 Uint32 current_ticks = SDL_GetTicks();
301 if(current_ticks - lastticks < 300)
303 lastticks = current_ticks;
305 alListener3f(AL_POSITION, pos.x, pos.y, 0);
309 SoundManager::set_listener_velocity(const Vector& vel)
311 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
315 SoundManager::update()
317 static float lasttime = real_time;
319 if(real_time - lasttime < 0.3)
321 lasttime = real_time;
323 // update and check for finished sound sources
324 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
325 OpenALSoundSource* source = *i;
329 if(!source->playing()) {
331 i = sources.erase(i);
336 // check streaming sounds
338 music_source->update();
343 alcProcessContext(context);
344 check_alc_error("Error while processing audio context: ");
347 //run update() for stream_sound_source
348 StreamSoundSources::iterator s = update_list.begin();
349 while( s != update_list.end() ){
356 SoundManager::get_sample_format(SoundFile* file)
358 if(file->channels == 2) {
359 if(file->bits_per_sample == 16) {
360 return AL_FORMAT_STEREO16;
361 } else if(file->bits_per_sample == 8) {
362 return AL_FORMAT_STEREO8;
364 throw std::runtime_error("Only 16 and 8 bit samples supported");
366 } else if(file->channels == 1) {
367 if(file->bits_per_sample == 16) {
368 return AL_FORMAT_MONO16;
369 } else if(file->bits_per_sample == 8) {
370 return AL_FORMAT_MONO8;
372 throw std::runtime_error("Only 16 and 8 bit samples supported");
376 throw std::runtime_error("Only 1 and 2 channel samples supported");
380 SoundManager::print_openal_version()
382 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
383 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
384 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
385 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
389 SoundManager::check_alc_error(const char* message)
391 int err = alcGetError(device);
392 if(err != ALC_NO_ERROR) {
393 std::stringstream msg;
394 msg << message << alcGetString(device, err);
395 throw std::runtime_error(msg.str());
400 SoundManager::check_al_error(const char* message)
402 int err = alGetError();
403 if(err != AL_NO_ERROR) {
404 std::stringstream msg;
405 msg << message << alGetString(err);
406 throw std::runtime_error(msg.str());