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 throw std::runtime_error("Couldn't open audio device.");
45 int attributes[] = { 0 };
46 context = alcCreateContext(device, attributes);
47 check_alc_error("Couldn't create audio context: ");
48 alcMakeContextCurrent(context);
49 check_alc_error("Couldn't select audio context: ");
51 check_al_error("Audio error after init: ");
53 } catch(std::exception& e) {
55 alcDestroyContext(context);
58 alcCloseDevice(device);
60 log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
61 print_openal_version();
65 SoundManager::~SoundManager()
69 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
73 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
74 ALuint buffer = i->second;
75 alDeleteBuffers(1, &buffer);
79 alcDestroyContext(context);
82 alcCloseDevice(device);
87 SoundManager::load_file_into_buffer(SoundFile* file)
89 ALenum format = get_sample_format(file);
91 alGenBuffers(1, &buffer);
92 check_al_error("Couldn't create audio buffer: ");
93 char* samples = new char[file->size];
95 file->read(samples, file->size);
96 alBufferData(buffer, format, samples,
97 static_cast<ALsizei> (file->size),
98 static_cast<ALsizei> (file->rate));
99 check_al_error("Couldn't fill audio buffer: ");
110 SoundManager::create_sound_source(const std::string& filename)
117 // reuse an existing static sound buffer
118 SoundBuffers::iterator i = buffers.find(filename);
119 if(i != buffers.end()) {
123 std::auto_ptr<SoundFile> file (load_sound_file(filename));
125 if(file->size < 100000) {
126 buffer = load_file_into_buffer(file.get());
127 buffers.insert(std::make_pair(filename, buffer));
129 StreamSoundSource* source = new StreamSoundSource();
130 source->set_sound_file(file.release());
135 SoundSource* source = new SoundSource();
136 alSourcei(source->source, AL_BUFFER, buffer);
141 SoundManager::play(const std::string& filename, const Vector& pos)
144 SoundSource* source = create_sound_source(filename);
147 if(pos == Vector(-1, -1)) {
148 alSourcef(source->source, AL_ROLLOFF_FACTOR, 0);
150 source->set_position(pos);
153 sources.push_back(source);
154 } catch(std::exception& e) {
155 log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
160 SoundManager::enable_sound(bool enable)
165 sound_enabled = enable;
169 SoundManager::enable_music(bool enable)
174 music_enabled = enable;
176 play_music(current_music);
186 SoundManager::stop_music(float fadetime)
190 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
191 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
200 SoundManager::play_music(const std::string& filename, bool fade)
202 if(filename == current_music && music_source != NULL)
204 current_music = filename;
215 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
216 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
217 newmusic->set_sound_file(load_sound_file(filename));
218 newmusic->set_looping(true);
220 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
224 music_source = newmusic.release();
225 } catch(std::exception& e) {
226 log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
231 SoundManager::set_listener_position(const Vector& pos)
233 static Uint32 lastticks = 0;
235 Uint32 current_ticks = SDL_GetTicks();
236 if(current_ticks - lastticks < 300)
238 lastticks = current_ticks;
240 alListener3f(AL_POSITION, pos.x, pos.y, 0);
244 SoundManager::set_listener_velocity(const Vector& vel)
246 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
250 SoundManager::update()
252 static float lasttime = real_time;
254 if(real_time - lasttime < 0.3)
256 lasttime = real_time;
258 // update and check for finished sound sources
259 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
260 SoundSource* source = *i;
264 if(!source->playing()) {
266 i = sources.erase(i);
271 // check streaming sounds
273 music_source->update();
278 alcProcessContext(context);
279 check_alc_error("Error while processing audio context: ");
284 SoundManager::get_sample_format(SoundFile* file)
286 if(file->channels == 2) {
287 if(file->bits_per_sample == 16) {
288 return AL_FORMAT_STEREO16;
289 } else if(file->bits_per_sample == 8) {
290 return AL_FORMAT_STEREO8;
292 throw std::runtime_error("Only 16 and 8 bit samples supported");
294 } else if(file->channels == 1) {
295 if(file->bits_per_sample == 16) {
296 return AL_FORMAT_MONO16;
297 } else if(file->bits_per_sample == 8) {
298 return AL_FORMAT_MONO8;
300 throw std::runtime_error("Only 16 and 8 bit samples supported");
304 throw std::runtime_error("Only 1 and 2 channel samples supported");
308 SoundManager::print_openal_version()
310 log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
311 log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
312 log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
313 log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
317 SoundManager::check_alc_error(const char* message)
319 int err = alcGetError(device);
320 if(err != ALC_NO_ERROR) {
321 std::stringstream msg;
322 msg << message << alcGetString(device, err);
323 throw std::runtime_error(msg.str());
328 SoundManager::check_al_error(const char* message)
330 int err = alGetError();
331 if(err != AL_NO_ERROR) {
332 std::stringstream msg;
333 msg << message << alGetString(err);
334 throw std::runtime_error(msg.str());