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(float fadetime)
158 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
159 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
168 SoundManager::play_music(const std::string& filename, bool fade)
170 if(filename == current_music && music_source != NULL)
172 current_music = filename;
183 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
184 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
185 newmusic->set_sound_file(load_sound_file(filename));
187 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
191 music_source = newmusic.release();
192 } catch(std::exception& e) {
193 std::cerr << "Couldn't play music file '" << filename << "': "
199 SoundManager::set_listener_position(const Vector& pos)
201 static Uint32 lastticks = 0;
203 Uint32 current_ticks = SDL_GetTicks();
204 if(current_ticks - lastticks < 300)
206 lastticks = current_ticks;
208 alListener3f(AL_POSITION, pos.x, pos.y, 0);
212 SoundManager::set_listener_velocity(const Vector& vel)
214 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
218 SoundManager::update()
220 static Uint32 lastticks = 0;
222 Uint32 current_ticks = SDL_GetTicks();
223 if(current_ticks - lastticks < 300)
225 lastticks = current_ticks;
227 // check for finished sound sources
228 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
229 SoundSource* source = *i;
230 if(!source->playing()) {
232 i = sources.erase(i);
237 // check streaming sounds
239 music_source->update();
242 alcProcessContext(context);
243 check_alc_error("Error while processing audio context: ");
247 SoundManager::get_sample_format(SoundFile* file)
249 if(file->channels == 2) {
250 if(file->bits_per_sample == 16) {
251 return AL_FORMAT_STEREO16;
252 } else if(file->bits_per_sample == 8) {
253 return AL_FORMAT_STEREO8;
255 throw std::runtime_error("Only 16 and 8 bit samples supported");
257 } else if(file->channels == 1) {
258 if(file->bits_per_sample == 16) {
259 return AL_FORMAT_MONO16;
260 } else if(file->bits_per_sample == 8) {
261 return AL_FORMAT_MONO8;
263 throw std::runtime_error("Only 16 and 8 bit samples supported");
267 throw std::runtime_error("Only 1 and 2 channel samples supported");
271 SoundManager::print_openal_version()
273 std::cout << "OpenAL Vendor: " << alGetString(AL_VENDOR) << "\n"
274 << "OpenAL Version: " << alGetString(AL_VERSION) << "\n"
275 << "OpenAL Renderer: " << alGetString(AL_RENDERER) << "\n"
276 << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << "\n";
280 SoundManager::check_alc_error(const char* message)
282 int err = alcGetError(device);
283 if(err != ALC_NO_ERROR) {
284 std::stringstream msg;
285 msg << message << alcGetString(device, err);
286 throw std::runtime_error(msg.str());
291 SoundManager::check_al_error(const char* message)
293 int err = alGetError();
294 if(err != AL_NO_ERROR) {
295 std::stringstream msg;
296 msg << message << alGetString(err);
297 throw std::runtime_error(msg.str());