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 "stream_sound_source.hpp"
34 SoundManager* sound_manager = 0;
36 SoundManager::SoundManager()
37 : device(0), context(0), sound_enabled(false), music_source(0),
41 device = alcOpenDevice(0);
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) {
56 alcDestroyContext(context);
59 alcCloseDevice(device);
61 log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
62 print_openal_version();
66 SoundManager::~SoundManager()
70 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
74 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
75 ALuint buffer = i->second;
76 alDeleteBuffers(1, &buffer);
80 alcDestroyContext(context);
83 alcCloseDevice(device);
88 SoundManager::load_file_into_buffer(SoundFile* file)
90 ALenum format = get_sample_format(file);
92 alGenBuffers(1, &buffer);
93 check_al_error("Couldn't create audio buffer: ");
94 char* samples = new char[file->size];
96 file->read(samples, file->size);
97 alBufferData(buffer, format, samples,
98 static_cast<ALsizei> (file->size),
99 static_cast<ALsizei> (file->rate));
100 check_al_error("Couldn't fill audio buffer: ");
111 SoundManager::create_sound_source(const std::string& filename)
118 // reuse an existing static sound buffer
119 SoundBuffers::iterator i = buffers.find(filename);
120 if(i != buffers.end()) {
124 std::auto_ptr<SoundFile> file (load_sound_file(filename));
126 if(file->size < 100000) {
127 buffer = load_file_into_buffer(file.get());
128 buffers.insert(std::make_pair(filename, buffer));
130 StreamSoundSource* source = new StreamSoundSource();
131 source->set_sound_file(file.release());
136 SoundSource* source = new SoundSource();
137 alSourcei(source->source, AL_BUFFER, buffer);
142 SoundManager::preload(const std::string& filename)
147 SoundBuffers::iterator i = buffers.find(filename);
149 if(i != buffers.end())
152 std::auto_ptr<SoundFile> file (load_sound_file(filename));
153 // only keep small files
154 if(file->size >= 100000)
157 ALuint buffer = load_file_into_buffer(file.get());
158 buffers.insert(std::make_pair(filename, buffer));
162 SoundManager::play(const std::string& filename, const Vector& pos)
165 SoundSource* source = create_sound_source(filename);
168 if(pos == Vector(-1, -1)) {
169 alSourcef(source->source, AL_ROLLOFF_FACTOR, 0);
171 source->set_position(pos);
174 sources.push_back(source);
175 } catch(std::exception& e) {
176 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
181 SoundManager::play_and_delete(SoundSource* source)
184 log_debug << "ignoring NULL SoundSource" << std::endl;
189 sources.push_back(source);
190 } catch(std::exception& e) {
191 log_warning << "Couldn't play SoundSource: " << e.what() << std::endl;
196 SoundManager::enable_sound(bool enable)
201 sound_enabled = enable;
205 SoundManager::enable_music(bool enable)
210 music_enabled = enable;
212 play_music(current_music);
222 SoundManager::stop_music(float fadetime)
226 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
227 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
236 SoundManager::play_music(const std::string& filename, bool fade)
238 if(filename == current_music && music_source != NULL)
240 current_music = filename;
251 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
252 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
253 newmusic->set_sound_file(load_sound_file(filename));
254 newmusic->set_looping(true);
256 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
260 music_source = newmusic.release();
261 } catch(std::exception& e) {
262 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
267 SoundManager::set_listener_position(const Vector& pos)
269 static Uint32 lastticks = 0;
271 Uint32 current_ticks = SDL_GetTicks();
272 if(current_ticks - lastticks < 300)
274 lastticks = current_ticks;
276 alListener3f(AL_POSITION, pos.x, pos.y, 0);
280 SoundManager::set_listener_velocity(const Vector& vel)
282 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
286 SoundManager::update()
288 static float lasttime = real_time;
290 if(real_time - lasttime < 0.3)
292 lasttime = real_time;
294 // update and check for finished sound sources
295 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
296 SoundSource* source = *i;
300 if(!source->playing()) {
302 i = sources.erase(i);
307 // check streaming sounds
309 music_source->update();
314 alcProcessContext(context);
315 check_alc_error("Error while processing audio context: ");
320 SoundManager::get_sample_format(SoundFile* file)
322 if(file->channels == 2) {
323 if(file->bits_per_sample == 16) {
324 return AL_FORMAT_STEREO16;
325 } else if(file->bits_per_sample == 8) {
326 return AL_FORMAT_STEREO8;
328 throw std::runtime_error("Only 16 and 8 bit samples supported");
330 } else if(file->channels == 1) {
331 if(file->bits_per_sample == 16) {
332 return AL_FORMAT_MONO16;
333 } else if(file->bits_per_sample == 8) {
334 return AL_FORMAT_MONO8;
336 throw std::runtime_error("Only 16 and 8 bit samples supported");
340 throw std::runtime_error("Only 1 and 2 channel samples supported");
344 SoundManager::print_openal_version()
346 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
347 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
348 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
349 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
353 SoundManager::check_alc_error(const char* message)
355 int err = alcGetError(device);
356 if(err != ALC_NO_ERROR) {
357 std::stringstream msg;
358 msg << message << alcGetString(device, err);
359 throw std::runtime_error(msg.str());
364 SoundManager::check_al_error(const char* message)
366 int err = alGetError();
367 if(err != AL_NO_ERROR) {
368 std::stringstream msg;
369 msg << message << alGetString(err);
370 throw std::runtime_error(msg.str());