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"
30 SoundManager::SoundManager() :
42 device = alcOpenDevice(0);
44 throw std::runtime_error("Couldn't open audio device.");
47 int attributes[] = { 0 };
48 context = alcCreateContext(device, attributes);
49 check_alc_error("Couldn't create audio context: ");
50 alcMakeContextCurrent(context);
51 check_alc_error("Couldn't select audio context: ");
53 check_al_error("Audio error after init: ");
56 } catch(std::exception& e) {
58 alcDestroyContext(context);
62 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);
88 alcCloseDevice(device);
94 SoundManager::load_file_into_buffer(SoundFile* file)
96 ALenum format = get_sample_format(file);
98 alGenBuffers(1, &buffer);
99 check_al_error("Couldn't create audio buffer: ");
100 char* samples = new char[file->size];
102 file->read(samples, file->size);
103 alBufferData(buffer, format, samples,
104 static_cast<ALsizei> (file->size),
105 static_cast<ALsizei> (file->rate));
106 check_al_error("Couldn't fill audio buffer: ");
117 SoundManager::intern_create_sound_source(const std::string& filename)
119 assert(sound_enabled);
121 std::auto_ptr<OpenALSoundSource> source (new OpenALSoundSource());
125 // reuse an existing static sound buffer
126 SoundBuffers::iterator i = buffers.find(filename);
127 if(i != buffers.end()) {
131 std::auto_ptr<SoundFile> file (load_sound_file(filename));
133 if(file->size < 100000) {
134 buffer = load_file_into_buffer(file.get());
135 buffers.insert(std::make_pair(filename, buffer));
137 StreamSoundSource* source = new StreamSoundSource();
138 source->set_sound_file(file.release());
142 log_debug << "Uncached sound \"" << filename << "\" requested to be played" << std::endl;
145 alSourcei(source->source, AL_BUFFER, buffer);
146 return source.release();
150 SoundManager::create_sound_source(const std::string& filename)
153 return create_dummy_sound_source();
156 return intern_create_sound_source(filename);
157 } catch(std::exception &e) {
158 log_warning << "Couldn't create audio source: " << e.what() << std::endl;
159 return create_dummy_sound_source();
164 SoundManager::preload(const std::string& filename)
169 SoundBuffers::iterator i = buffers.find(filename);
171 if(i != buffers.end())
174 std::auto_ptr<SoundFile> file (load_sound_file(filename));
175 // only keep small files
176 if(file->size >= 100000)
179 ALuint buffer = load_file_into_buffer(file.get());
180 buffers.insert(std::make_pair(filename, buffer));
181 } catch(std::exception& e) {
182 log_warning << "Error while preloading sound file: " << e.what() << std::endl;
187 SoundManager::play(const std::string& filename, const Vector& pos)
193 std::auto_ptr<OpenALSoundSource> source
194 (intern_create_sound_source(filename));
196 if(pos.x < 0 || pos.y < 0) {
197 source->set_relative(true);
199 source->set_position(pos);
202 sources.push_back(source.release());
203 } catch(std::exception& e) {
204 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
209 SoundManager::manage_source(SoundSource* source)
211 assert(source != NULL);
213 OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
214 if(openal_source != NULL) {
215 sources.push_back(openal_source);
220 SoundManager::register_for_update( StreamSoundSource* sss ){
222 update_list.push_back( sss );
227 SoundManager::remove_from_update( StreamSoundSource* sss ){
229 StreamSoundSources::iterator i = update_list.begin();
230 while( i != update_list.end() ){
232 i = update_list.erase(i);
241 SoundManager::enable_sound(bool enable)
246 sound_enabled = enable;
250 SoundManager::enable_music(bool enable)
255 music_enabled = enable;
257 play_music(current_music);
267 SoundManager::stop_music(float fadetime)
271 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
272 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
281 SoundManager::play_music(const std::string& filename, bool fade)
283 if(filename == current_music && music_source != NULL)
285 current_music = filename;
296 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
297 newmusic->set_sound_file(load_sound_file(filename));
298 newmusic->set_looping(true);
299 newmusic->set_relative(true);
301 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
305 music_source = newmusic.release();
306 } catch(std::exception& e) {
307 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
312 SoundManager::set_listener_position(const Vector& pos)
314 static Uint32 lastticks = SDL_GetTicks();
316 Uint32 current_ticks = SDL_GetTicks();
317 if(current_ticks - lastticks < 300)
319 lastticks = current_ticks;
321 alListener3f(AL_POSITION, pos.x, pos.y, 0);
325 SoundManager::set_listener_velocity(const Vector& vel)
327 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
331 SoundManager::update()
333 static Uint32 lasttime = SDL_GetTicks();
334 Uint32 now = SDL_GetTicks();
336 if(now - lasttime < 300)
340 // update and check for finished sound sources
341 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
342 OpenALSoundSource* source = *i;
346 if(!source->playing()) {
348 i = sources.erase(i);
353 // check streaming sounds
355 music_source->update();
360 alcProcessContext(context);
361 check_alc_error("Error while processing audio context: ");
364 //run update() for stream_sound_source
365 StreamSoundSources::iterator s = update_list.begin();
366 while( s != update_list.end() ){
373 SoundManager::get_sample_format(SoundFile* file)
375 if(file->channels == 2) {
376 if(file->bits_per_sample == 16) {
377 return AL_FORMAT_STEREO16;
378 } else if(file->bits_per_sample == 8) {
379 return AL_FORMAT_STEREO8;
381 throw std::runtime_error("Only 16 and 8 bit samples supported");
383 } else if(file->channels == 1) {
384 if(file->bits_per_sample == 16) {
385 return AL_FORMAT_MONO16;
386 } else if(file->bits_per_sample == 8) {
387 return AL_FORMAT_MONO8;
389 throw std::runtime_error("Only 16 and 8 bit samples supported");
393 throw std::runtime_error("Only 1 and 2 channel samples supported");
397 SoundManager::print_openal_version()
399 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
400 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
401 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
402 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
406 SoundManager::check_alc_error(const char* message)
408 int err = alcGetError(device);
409 if(err != ALC_NO_ERROR) {
410 std::stringstream msg;
411 msg << message << alcGetString(device, err);
412 throw std::runtime_error(msg.str());
417 SoundManager::check_al_error(const char* message)
419 int err = alGetError();
420 if(err != AL_NO_ERROR) {
421 std::stringstream msg;
422 msg << message << alGetString(err);
423 throw std::runtime_error(msg.str());