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.
20 #include "sound_manager.hpp"
27 #include "sound_file.hpp"
28 #include "sound_source.hpp"
29 #include "stream_sound_source.hpp"
33 SoundManager* sound_manager = 0;
35 SoundManager::SoundManager()
36 : device(0), context(0), sound_enabled(false), music_source(0),
40 device = alcOpenDevice(0);
42 print_openal_version();
43 throw std::runtime_error("Couldn't open audio device.");
46 int attributes[] = { 0 };
47 context = alcCreateContext(device, attributes);
48 check_alc_error("Couldn't create audio context: ");
49 alcMakeContextCurrent(context);
50 check_alc_error("Couldn't select audio context: ");
52 check_al_error("Audio error after init: ");
54 } catch(std::exception& e) {
57 log_warning << "Couldn't initialize audio device:" << e.what() << std::endl;
58 print_openal_version();
63 SoundManager::~SoundManager()
67 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
71 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
72 ALuint buffer = i->second;
73 alDeleteBuffers(1, &buffer);
77 alcDestroyContext(context);
80 alcCloseDevice(device);
85 SoundManager::load_file_into_buffer(SoundFile* file)
87 ALenum format = get_sample_format(file);
89 alGenBuffers(1, &buffer);
90 check_al_error("Couldn't create audio buffer: ");
91 char* samples = new char[file->size];
93 file->read(samples, file->size);
94 alBufferData(buffer, format, samples,
95 static_cast<ALsizei> (file->size),
96 static_cast<ALsizei> (file->rate));
97 check_al_error("Couldn't fill audio buffer: ");
108 SoundManager::create_sound_source(const std::string& filename)
115 // reuse an existing static sound buffer
116 SoundBuffers::iterator i = buffers.find(filename);
117 if(i != buffers.end()) {
121 std::auto_ptr<SoundFile> file (load_sound_file(filename));
123 if(file->size < 100000) {
124 buffer = load_file_into_buffer(file.get());
125 buffers.insert(std::make_pair(filename, buffer));
127 StreamSoundSource* source = new StreamSoundSource();
128 source->set_sound_file(file.release());
133 SoundSource* source = new SoundSource();
134 alSourcei(source->source, AL_BUFFER, buffer);
139 SoundManager::play(const std::string& filename, const Vector& pos)
142 SoundSource* source = create_sound_source(filename);
145 if(pos == Vector(-1, -1)) {
146 alSourcef(source->source, AL_ROLLOFF_FACTOR, 0);
148 source->set_position(pos);
151 sources.push_back(source);
152 } catch(std::exception& e) {
153 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
158 SoundManager::enable_sound(bool enable)
162 sound_enabled = enable;
166 SoundManager::enable_music(bool enable)
170 music_enabled = enable;
172 play_music(current_music);
182 SoundManager::stop_music(float fadetime)
186 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
187 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
196 SoundManager::play_music(const std::string& filename, bool fade)
198 if(filename == current_music && music_source != NULL)
200 current_music = filename;
211 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
212 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
213 newmusic->set_sound_file(load_sound_file(filename));
214 newmusic->set_looping(true);
216 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
220 music_source = newmusic.release();
221 } catch(std::exception& e) {
222 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
227 SoundManager::set_listener_position(const Vector& pos)
229 static Uint32 lastticks = 0;
231 Uint32 current_ticks = SDL_GetTicks();
232 if(current_ticks - lastticks < 300)
234 lastticks = current_ticks;
236 alListener3f(AL_POSITION, pos.x, pos.y, 0);
240 SoundManager::set_listener_velocity(const Vector& vel)
242 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
246 SoundManager::update()
248 static float lasttime = real_time;
250 if(real_time - lasttime < 0.3)
252 lasttime = real_time;
254 // update and check for finished sound sources
255 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
256 SoundSource* source = *i;
260 if(!source->playing()) {
262 i = sources.erase(i);
267 // check streaming sounds
269 music_source->update();
272 alcProcessContext(context);
273 check_alc_error("Error while processing audio context: ");
277 SoundManager::get_sample_format(SoundFile* file)
279 if(file->channels == 2) {
280 if(file->bits_per_sample == 16) {
281 return AL_FORMAT_STEREO16;
282 } else if(file->bits_per_sample == 8) {
283 return AL_FORMAT_STEREO8;
285 throw std::runtime_error("Only 16 and 8 bit samples supported");
287 } else if(file->channels == 1) {
288 if(file->bits_per_sample == 16) {
289 return AL_FORMAT_MONO16;
290 } else if(file->bits_per_sample == 8) {
291 return AL_FORMAT_MONO8;
293 throw std::runtime_error("Only 16 and 8 bit samples supported");
297 throw std::runtime_error("Only 1 and 2 channel samples supported");
301 SoundManager::print_openal_version()
303 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
304 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
305 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
306 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
310 SoundManager::check_alc_error(const char* message)
312 int err = alcGetError(device);
313 if(err != ALC_NO_ERROR) {
314 std::stringstream msg;
315 msg << message << alcGetString(device, err);
316 throw std::runtime_error(msg.str());
321 SoundManager::check_al_error(const char* message)
323 int err = alGetError();
324 if(err != AL_NO_ERROR) {
325 std::stringstream msg;
326 msg << message << alGetString(err);
327 throw std::runtime_error(msg.str());