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"
28 #include "sound_file.hpp"
29 #include "sound_source.hpp"
30 #include "openal_sound_source.hpp"
31 #include "stream_sound_source.hpp"
32 #include "dummy_sound_source.hpp"
36 SoundManager* sound_manager = 0;
38 SoundManager::SoundManager()
39 : device(0), context(0), sound_enabled(false), music_source(0),
43 device = alcOpenDevice(0);
45 throw std::runtime_error("Couldn't open audio device.");
48 int attributes[] = { 0 };
49 context = alcCreateContext(device, attributes);
50 check_alc_error("Couldn't create audio context: ");
51 alcMakeContextCurrent(context);
52 check_alc_error("Couldn't select audio context: ");
54 check_al_error("Audio error after init: ");
57 } catch(std::exception& e) {
59 alcDestroyContext(context);
62 alcCloseDevice(device);
64 log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
65 print_openal_version();
69 SoundManager::~SoundManager()
73 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
77 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
78 ALuint buffer = i->second;
79 alDeleteBuffers(1, &buffer);
83 alcDestroyContext(context);
86 alcCloseDevice(device);
91 SoundManager::load_file_into_buffer(SoundFile* file)
93 ALenum format = get_sample_format(file);
95 alGenBuffers(1, &buffer);
96 check_al_error("Couldn't create audio buffer: ");
97 char* samples = new char[file->size];
99 file->read(samples, file->size);
100 alBufferData(buffer, format, samples,
101 static_cast<ALsizei> (file->size),
102 static_cast<ALsizei> (file->rate));
103 check_al_error("Couldn't fill audio buffer: ");
114 SoundManager::create_sound_source(const std::string& filename)
117 return create_dummy_sound_source();
121 // reuse an existing static sound buffer
122 SoundBuffers::iterator i = buffers.find(filename);
123 if(i != buffers.end()) {
128 std::auto_ptr<SoundFile> file (load_sound_file(filename));
130 if(file->size < 100000) {
131 buffer = load_file_into_buffer(file.get());
132 buffers.insert(std::make_pair(filename, buffer));
134 StreamSoundSource* source = new StreamSoundSource();
135 source->set_sound_file(file.release());
138 } catch(std::exception& e) {
139 log_warning << "Couldn't load soundfile '" << filename << "': " << e.what() << std::endl;
140 return create_dummy_sound_source();
144 OpenALSoundSource* source = new OpenALSoundSource();
145 alSourcei(source->source, AL_BUFFER, buffer);
150 SoundManager::preload(const std::string& filename)
155 SoundBuffers::iterator i = buffers.find(filename);
157 if(i != buffers.end())
160 std::auto_ptr<SoundFile> file (load_sound_file(filename));
161 // only keep small files
162 if(file->size >= 100000)
165 ALuint buffer = load_file_into_buffer(file.get());
166 buffers.insert(std::make_pair(filename, buffer));
170 SoundManager::play(const std::string& filename, const Vector& pos)
176 OpenALSoundSource* source
177 = static_cast<OpenALSoundSource*> (create_sound_source(filename));
179 if(pos == Vector(-1, -1)) {
180 source->set_rollof_factor(0);
182 source->set_position(pos);
185 sources.push_back(source);
186 } catch(std::exception& e) {
187 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
192 SoundManager::manage_source(SoundSource* source)
194 assert(source != NULL);
196 OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
197 if(openal_source != NULL) {
198 sources.push_back(openal_source);
203 SoundManager::enable_sound(bool enable)
208 sound_enabled = enable;
212 SoundManager::enable_music(bool enable)
217 music_enabled = enable;
219 play_music(current_music);
229 SoundManager::stop_music(float fadetime)
233 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
234 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
243 SoundManager::play_music(const std::string& filename, bool fade)
245 if(filename == current_music && music_source != NULL)
247 current_music = filename;
258 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
259 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
260 newmusic->set_sound_file(load_sound_file(filename));
261 newmusic->set_looping(true);
263 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
267 music_source = newmusic.release();
268 } catch(std::exception& e) {
269 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
274 SoundManager::set_listener_position(const Vector& pos)
276 static Uint32 lastticks = 0;
278 Uint32 current_ticks = SDL_GetTicks();
279 if(current_ticks - lastticks < 300)
281 lastticks = current_ticks;
283 alListener3f(AL_POSITION, pos.x, pos.y, 0);
287 SoundManager::set_listener_velocity(const Vector& vel)
289 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
293 SoundManager::update()
295 static float lasttime = real_time;
297 if(real_time - lasttime < 0.3)
299 lasttime = real_time;
301 // update and check for finished sound sources
302 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
303 OpenALSoundSource* source = *i;
307 if(!source->playing()) {
309 i = sources.erase(i);
314 // check streaming sounds
316 music_source->update();
321 alcProcessContext(context);
322 check_alc_error("Error while processing audio context: ");
327 SoundManager::get_sample_format(SoundFile* file)
329 if(file->channels == 2) {
330 if(file->bits_per_sample == 16) {
331 return AL_FORMAT_STEREO16;
332 } else if(file->bits_per_sample == 8) {
333 return AL_FORMAT_STEREO8;
335 throw std::runtime_error("Only 16 and 8 bit samples supported");
337 } else if(file->channels == 1) {
338 if(file->bits_per_sample == 16) {
339 return AL_FORMAT_MONO16;
340 } else if(file->bits_per_sample == 8) {
341 return AL_FORMAT_MONO8;
343 throw std::runtime_error("Only 16 and 8 bit samples supported");
347 throw std::runtime_error("Only 1 and 2 channel samples supported");
351 SoundManager::print_openal_version()
353 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
354 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
355 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
356 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
360 SoundManager::check_alc_error(const char* message)
362 int err = alcGetError(device);
363 if(err != ALC_NO_ERROR) {
364 std::stringstream msg;
365 msg << message << alcGetString(device, err);
366 throw std::runtime_error(msg.str());
371 SoundManager::check_al_error(const char* message)
373 int err = alGetError();
374 if(err != AL_NO_ERROR) {
375 std::stringstream msg;
376 msg << message << alGetString(err);
377 throw std::runtime_error(msg.str());