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::unique_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::unique_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::unique_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::unique_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::unique_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;
308 // When this happens, previous music continued playing, stop it, just in case.
314 SoundManager::set_listener_position(const Vector& pos)
316 static Uint32 lastticks = SDL_GetTicks();
318 Uint32 current_ticks = SDL_GetTicks();
319 if(current_ticks - lastticks < 300)
321 lastticks = current_ticks;
323 alListener3f(AL_POSITION, pos.x, pos.y, 0);
327 SoundManager::set_listener_velocity(const Vector& vel)
329 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
333 SoundManager::update()
335 static Uint32 lasttime = SDL_GetTicks();
336 Uint32 now = SDL_GetTicks();
338 if(now - lasttime < 300)
342 // update and check for finished sound sources
343 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
344 OpenALSoundSource* source = *i;
348 if(!source->playing()) {
350 i = sources.erase(i);
355 // check streaming sounds
357 music_source->update();
362 alcProcessContext(context);
363 check_alc_error("Error while processing audio context: ");
366 //run update() for stream_sound_source
367 StreamSoundSources::iterator s = update_list.begin();
368 while( s != update_list.end() ){
375 SoundManager::get_sample_format(SoundFile* file)
377 if(file->channels == 2) {
378 if(file->bits_per_sample == 16) {
379 return AL_FORMAT_STEREO16;
380 } else if(file->bits_per_sample == 8) {
381 return AL_FORMAT_STEREO8;
383 throw std::runtime_error("Only 16 and 8 bit samples supported");
385 } else if(file->channels == 1) {
386 if(file->bits_per_sample == 16) {
387 return AL_FORMAT_MONO16;
388 } else if(file->bits_per_sample == 8) {
389 return AL_FORMAT_MONO8;
391 throw std::runtime_error("Only 16 and 8 bit samples supported");
395 throw std::runtime_error("Only 1 and 2 channel samples supported");
399 SoundManager::print_openal_version()
401 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
402 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
403 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
404 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
408 SoundManager::check_alc_error(const char* message)
410 int err = alcGetError(device);
411 if(err != ALC_NO_ERROR) {
412 std::stringstream msg;
413 msg << message << alcGetString(device, err);
414 throw std::runtime_error(msg.str());
419 SoundManager::check_al_error(const char* message)
421 int err = alGetError();
422 if(err != AL_NO_ERROR) {
423 std::stringstream msg;
424 msg << message << alGetString(err);
425 throw std::runtime_error(msg.str());