1 #include "sound_manager.hpp"
8 #include "sound_file.hpp"
9 #include "sound_source.hpp"
10 #include "stream_sound_source.hpp"
13 SoundManager* sound_manager = 0;
15 SoundManager::SoundManager()
16 : device(0), context(0), sound_enabled(false), music_source(0),
20 device = alcOpenDevice(0);
22 print_openal_version();
23 throw std::runtime_error("Couldn't open audio device.");
26 int attributes[] = { 0 };
27 context = alcCreateContext(device, attributes);
28 check_alc_error("Couldn't create audio context: ");
29 alcMakeContextCurrent(context);
30 check_alc_error("Couldn't select audio context: ");
32 check_al_error("Audio error after init: ");
34 } catch(std::exception& e) {
37 msg_warning("Couldn't initialize audio device:" << e.what());
38 print_openal_version();
43 SoundManager::~SoundManager()
47 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
51 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
52 ALuint buffer = i->second;
53 alDeleteBuffers(1, &buffer);
57 alcDestroyContext(context);
60 alcCloseDevice(device);
65 SoundManager::load_file_into_buffer(SoundFile* file)
67 ALenum format = get_sample_format(file);
69 alGenBuffers(1, &buffer);
70 check_al_error("Couldn't create audio buffer: ");
71 char* samples = new char[file->size];
73 file->read(samples, file->size);
74 alBufferData(buffer, format, samples,
75 static_cast<ALsizei> (file->size),
76 static_cast<ALsizei> (file->rate));
77 check_al_error("Couldn't fill audio buffer: ");
88 SoundManager::create_sound_source(const std::string& filename)
95 // reuse an existing static sound buffer
96 SoundBuffers::iterator i = buffers.find(filename);
97 if(i != buffers.end()) {
101 std::auto_ptr<SoundFile> file (load_sound_file(filename));
103 if(file->size < 100000) {
104 buffer = load_file_into_buffer(file.get());
105 buffers.insert(std::make_pair(filename, buffer));
107 StreamSoundSource* source = new StreamSoundSource();
108 source->set_sound_file(file.release());
113 SoundSource* source = new SoundSource();
114 alSourcei(source->source, AL_BUFFER, buffer);
119 SoundManager::play(const std::string& filename, const Vector& pos)
122 SoundSource* source = create_sound_source(filename);
125 if(pos == Vector(-1, -1)) {
126 alSourcef(source->source, AL_ROLLOFF_FACTOR, 0);
128 source->set_position(pos);
131 sources.push_back(source);
132 } catch(std::exception& e) {
133 msg_warning("Couldn't play sound " << filename << ": " << e.what());
138 SoundManager::enable_sound(bool enable)
142 sound_enabled = enable;
146 SoundManager::enable_music(bool enable)
150 music_enabled = enable;
152 play_music(current_music);
162 SoundManager::stop_music(float fadetime)
166 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
167 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
176 SoundManager::play_music(const std::string& filename, bool fade)
178 if(filename == current_music && music_source != NULL)
180 current_music = filename;
191 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
192 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
193 newmusic->set_sound_file(load_sound_file(filename));
194 newmusic->set_looping(true);
196 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
200 music_source = newmusic.release();
201 } catch(std::exception& e) {
202 msg_warning("Couldn't play music file '" << filename << "': "
208 SoundManager::set_listener_position(const Vector& pos)
210 static Uint32 lastticks = 0;
212 Uint32 current_ticks = SDL_GetTicks();
213 if(current_ticks - lastticks < 300)
215 lastticks = current_ticks;
217 alListener3f(AL_POSITION, pos.x, pos.y, 0);
221 SoundManager::set_listener_velocity(const Vector& vel)
223 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
227 SoundManager::update()
229 static Uint32 lastticks = 0;
231 Uint32 current_ticks = SDL_GetTicks();
232 if(current_ticks - lastticks < 300)
234 lastticks = current_ticks;
236 // update and check for finished sound sources
237 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
238 SoundSource* source = *i;
242 if(!source->playing()) {
244 i = sources.erase(i);
249 // check streaming sounds
251 music_source->update();
254 alcProcessContext(context);
255 check_alc_error("Error while processing audio context: ");
259 SoundManager::get_sample_format(SoundFile* file)
261 if(file->channels == 2) {
262 if(file->bits_per_sample == 16) {
263 return AL_FORMAT_STEREO16;
264 } else if(file->bits_per_sample == 8) {
265 return AL_FORMAT_STEREO8;
267 throw std::runtime_error("Only 16 and 8 bit samples supported");
269 } else if(file->channels == 1) {
270 if(file->bits_per_sample == 16) {
271 return AL_FORMAT_MONO16;
272 } else if(file->bits_per_sample == 8) {
273 return AL_FORMAT_MONO8;
275 throw std::runtime_error("Only 16 and 8 bit samples supported");
279 throw std::runtime_error("Only 1 and 2 channel samples supported");
283 SoundManager::print_openal_version()
285 msg_info("OpenAL Vendor: " << alGetString(AL_VENDOR));
286 msg_info("OpenAL Version: " << alGetString(AL_VERSION));
287 msg_info("OpenAL Renderer: " << alGetString(AL_RENDERER));
288 msg_info("OpenAl Extensions: " << alGetString(AL_EXTENSIONS));
292 SoundManager::check_alc_error(const char* message)
294 int err = alcGetError(device);
295 if(err != ALC_NO_ERROR) {
296 std::stringstream msg;
297 msg << message << alcGetString(device, err);
298 throw std::runtime_error(msg.str());
303 SoundManager::check_al_error(const char* message)
305 int err = alGetError();
306 if(err != AL_NO_ERROR) {
307 std::stringstream msg;
308 msg << message << alGetString(err);
309 throw std::runtime_error(msg.str());