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::enable_sound(bool enable)
209 sound_enabled = enable;
213 SoundManager::enable_music(bool enable)
218 music_enabled = enable;
220 play_music(current_music);
230 SoundManager::stop_music(float fadetime)
234 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
235 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
244 SoundManager::play_music(const std::string& filename, bool fade)
246 if(filename == current_music && music_source != NULL)
248 current_music = filename;
259 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
260 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
261 newmusic->set_sound_file(load_sound_file(filename));
262 newmusic->set_looping(true);
264 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
268 music_source = newmusic.release();
269 } catch(std::exception& e) {
270 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
275 SoundManager::set_listener_position(const Vector& pos)
277 static Uint32 lastticks = 0;
279 Uint32 current_ticks = SDL_GetTicks();
280 if(current_ticks - lastticks < 300)
282 lastticks = current_ticks;
284 alListener3f(AL_POSITION, pos.x, pos.y, 0);
288 SoundManager::set_listener_velocity(const Vector& vel)
290 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
294 SoundManager::update()
296 static float lasttime = real_time;
298 if(real_time - lasttime < 0.3)
300 lasttime = real_time;
302 // update and check for finished sound sources
303 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
304 OpenALSoundSource* source = *i;
308 if(!source->playing()) {
310 i = sources.erase(i);
315 // check streaming sounds
317 music_source->update();
322 alcProcessContext(context);
323 check_alc_error("Error while processing audio context: ");
328 SoundManager::get_sample_format(SoundFile* file)
330 if(file->channels == 2) {
331 if(file->bits_per_sample == 16) {
332 return AL_FORMAT_STEREO16;
333 } else if(file->bits_per_sample == 8) {
334 return AL_FORMAT_STEREO8;
336 throw std::runtime_error("Only 16 and 8 bit samples supported");
338 } else if(file->channels == 1) {
339 if(file->bits_per_sample == 16) {
340 return AL_FORMAT_MONO16;
341 } else if(file->bits_per_sample == 8) {
342 return AL_FORMAT_MONO8;
344 throw std::runtime_error("Only 16 and 8 bit samples supported");
348 throw std::runtime_error("Only 1 and 2 channel samples supported");
352 SoundManager::print_openal_version()
354 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
355 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
356 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
357 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
361 SoundManager::check_alc_error(const char* message)
363 int err = alcGetError(device);
364 if(err != ALC_NO_ERROR) {
365 std::stringstream msg;
366 msg << message << alcGetString(device, err);
367 throw std::runtime_error(msg.str());
372 SoundManager::check_al_error(const char* message)
374 int err = alGetError();
375 if(err != AL_NO_ERROR) {
376 std::stringstream msg;
377 msg << message << alGetString(err);
378 throw std::runtime_error(msg.str());