1 #include "sound_manager.hpp"
8 #include "sound_file.hpp"
9 #include "sound_source.hpp"
10 #include "stream_sound_source.hpp"
12 SoundManager* sound_manager = 0;
14 SoundManager::SoundManager()
15 : device(0), context(0), sound_enabled(false), music_source(0),
19 device = alcOpenDevice(0);
21 print_openal_version();
22 throw std::runtime_error("Couldn't open audio device.");
25 int attributes[] = { 0 };
26 context = alcCreateContext(device, attributes);
27 check_alc_error("Couldn't create audio context: ");
28 alcMakeContextCurrent(context);
29 check_alc_error("Couldn't select audio context: ");
31 check_al_error("Audio error after init: ");
33 } catch(std::exception& e) {
36 std::cerr << "Couldn't initialize audio device:" << e.what() << "\n";
37 print_openal_version();
41 SoundManager::~SoundManager()
45 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
49 for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
50 ALuint buffer = i->second;
51 alDeleteBuffers(1, &buffer);
55 alcDestroyContext(context);
58 alcCloseDevice(device);
63 SoundManager::load_file_into_buffer(const std::string& filename)
66 std::auto_ptr<SoundFile> file (load_sound_file(filename));
68 ALenum format = get_sample_format(file.get());
70 alGenBuffers(1, &buffer);
71 check_al_error("Couldn't create audio buffer: ");
72 char* samples = new char[file->size];
74 file->read(samples, file->size);
75 alBufferData(buffer, format, samples,
76 static_cast<ALsizei> (file->size),
77 static_cast<ALsizei> (file->rate));
78 check_al_error("Couldn't fill audio buffer: ");
89 SoundManager::create_sound_source(const std::string& filename)
96 // reuse an existing static sound buffer
97 SoundBuffers::iterator i = buffers.find(filename);
98 if(i != buffers.end()) {
101 buffer = load_file_into_buffer(filename);
102 buffers.insert(std::make_pair(filename, buffer));
105 SoundSource* source = new SoundSource();
106 alSourcei(source->source, AL_BUFFER, buffer);
111 SoundManager::play(const std::string& filename, const Vector& pos)
114 SoundSource* source = create_sound_source(filename);
117 if(pos == Vector(-1, -1)) {
118 alSourcef(source->source, AL_ROLLOFF_FACTOR, 0);
120 source->set_position(pos);
123 sources.push_back(source);
124 } catch(std::exception& e) {
125 std::cout << "Couldn't play sound " << filename << ": " << e.what() << "\n";
130 SoundManager::enable_sound(bool enable)
134 sound_enabled = enable;
138 SoundManager::enable_music(bool enable)
142 music_enabled = enable;
144 play_music(current_music);
154 SoundManager::stop_music(bool fade)
158 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
159 music_source->set_fading(StreamSoundSource::FadingOff, .5f);
168 SoundManager::play_music(const std::string& filename, bool fade)
170 if(filename == current_music)
172 current_music = filename;
177 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
178 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
179 newmusic->set_sound_file(load_sound_file(filename));
181 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
185 music_source = newmusic.release();
186 } catch(std::exception& e) {
187 std::cerr << "Couldn't play music file '" << filename << "': "
193 SoundManager::set_listener_position(const Vector& pos)
195 static Uint32 lastticks = 0;
197 Uint32 current_ticks = SDL_GetTicks();
198 if(current_ticks - lastticks < 300)
200 lastticks = current_ticks;
202 alListener3f(AL_POSITION, pos.x, pos.y, 0);
206 SoundManager::set_listener_velocity(const Vector& vel)
208 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
212 SoundManager::update()
214 static Uint32 lastticks = 0;
216 Uint32 current_ticks = SDL_GetTicks();
217 if(current_ticks - lastticks < 300)
219 lastticks = current_ticks;
221 // check for finished sound sources
222 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
223 SoundSource* source = *i;
224 if(!source->playing()) {
226 i = sources.erase(i);
231 // check streaming sounds
233 music_source->update();
236 alcProcessContext(context);
237 check_alc_error("Error while processing audio context: ");
241 SoundManager::get_sample_format(SoundFile* file)
243 if(file->channels == 2) {
244 if(file->bits_per_sample == 16) {
245 return AL_FORMAT_STEREO16;
246 } else if(file->bits_per_sample == 8) {
247 return AL_FORMAT_STEREO8;
249 throw std::runtime_error("Only 16 and 8 bit samples supported");
251 } else if(file->channels == 1) {
252 if(file->bits_per_sample == 16) {
253 return AL_FORMAT_MONO16;
254 } else if(file->bits_per_sample == 8) {
255 return AL_FORMAT_MONO8;
257 throw std::runtime_error("Only 16 and 8 bit samples supported");
261 throw std::runtime_error("Only 1 and 2 channel samples supported");
265 SoundManager::print_openal_version()
267 std::cout << "OpenAL Vendor: " << alGetString(AL_VENDOR) << "\n"
268 << "OpenAL Version: " << alGetString(AL_VERSION) << "\n"
269 << "OpenAL Renderer: " << alGetString(AL_RENDERER) << "\n"
270 << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << "\n";
274 SoundManager::check_alc_error(const char* message)
276 int err = alcGetError(device);
277 if(err != ALC_NO_ERROR) {
278 std::stringstream msg;
279 msg << message << alcGetString(device, err);
280 throw std::runtime_error(msg.str());
285 SoundManager::check_al_error(const char* message)
287 int err = alGetError();
288 if(err != AL_NO_ERROR) {
289 std::stringstream msg;
290 msg << message << alGetString(err);
291 throw std::runtime_error(msg.str());