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(SoundFile* file)
65 ALenum format = get_sample_format(file);
67 alGenBuffers(1, &buffer);
68 check_al_error("Couldn't create audio buffer: ");
69 char* samples = new char[file->size];
71 file->read(samples, file->size);
72 alBufferData(buffer, format, samples,
73 static_cast<ALsizei> (file->size),
74 static_cast<ALsizei> (file->rate));
75 check_al_error("Couldn't fill audio buffer: ");
86 SoundManager::create_sound_source(const std::string& filename)
93 // reuse an existing static sound buffer
94 SoundBuffers::iterator i = buffers.find(filename);
95 if(i != buffers.end()) {
99 std::auto_ptr<SoundFile> file (load_sound_file(filename));
101 if(file->size < 100000) {
102 buffer = load_file_into_buffer(file.get());
103 buffers.insert(std::make_pair(filename, buffer));
105 StreamSoundSource* source = new StreamSoundSource();
106 source->set_sound_file(file.release());
111 SoundSource* source = new SoundSource();
112 alSourcei(source->source, AL_BUFFER, buffer);
117 SoundManager::play(const std::string& filename, const Vector& pos)
120 SoundSource* source = create_sound_source(filename);
123 if(pos == Vector(-1, -1)) {
124 alSourcef(source->source, AL_ROLLOFF_FACTOR, 0);
126 source->set_position(pos);
129 sources.push_back(source);
130 } catch(std::exception& e) {
131 std::cout << "Couldn't play sound " << filename << ": " << e.what() << "\n";
136 SoundManager::enable_sound(bool enable)
140 sound_enabled = enable;
144 SoundManager::enable_music(bool enable)
148 music_enabled = enable;
150 play_music(current_music);
160 SoundManager::stop_music(float fadetime)
164 && music_source->get_fade_state() != StreamSoundSource::FadingOff)
165 music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
174 SoundManager::play_music(const std::string& filename, bool fade)
176 if(filename == current_music && music_source != NULL)
178 current_music = filename;
189 std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
190 alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
191 newmusic->set_sound_file(load_sound_file(filename));
193 newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
197 music_source = newmusic.release();
198 } catch(std::exception& e) {
199 std::cerr << "Couldn't play music file '" << filename << "': "
205 SoundManager::set_listener_position(const Vector& pos)
207 static Uint32 lastticks = 0;
209 Uint32 current_ticks = SDL_GetTicks();
210 if(current_ticks - lastticks < 300)
212 lastticks = current_ticks;
214 alListener3f(AL_POSITION, pos.x, pos.y, 0);
218 SoundManager::set_listener_velocity(const Vector& vel)
220 alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
224 SoundManager::update()
226 static Uint32 lastticks = 0;
228 Uint32 current_ticks = SDL_GetTicks();
229 if(current_ticks - lastticks < 300)
231 lastticks = current_ticks;
233 // update and check for finished sound sources
234 for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
235 SoundSource* source = *i;
239 if(!source->playing()) {
241 i = sources.erase(i);
246 // check streaming sounds
248 music_source->update();
251 alcProcessContext(context);
252 check_alc_error("Error while processing audio context: ");
256 SoundManager::get_sample_format(SoundFile* file)
258 if(file->channels == 2) {
259 if(file->bits_per_sample == 16) {
260 return AL_FORMAT_STEREO16;
261 } else if(file->bits_per_sample == 8) {
262 return AL_FORMAT_STEREO8;
264 throw std::runtime_error("Only 16 and 8 bit samples supported");
266 } else if(file->channels == 1) {
267 if(file->bits_per_sample == 16) {
268 return AL_FORMAT_MONO16;
269 } else if(file->bits_per_sample == 8) {
270 return AL_FORMAT_MONO8;
272 throw std::runtime_error("Only 16 and 8 bit samples supported");
276 throw std::runtime_error("Only 1 and 2 channel samples supported");
280 SoundManager::print_openal_version()
282 std::cout << "OpenAL Vendor: " << alGetString(AL_VENDOR) << "\n"
283 << "OpenAL Version: " << alGetString(AL_VERSION) << "\n"
284 << "OpenAL Renderer: " << alGetString(AL_RENDERER) << "\n"
285 << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << "\n";
289 SoundManager::check_alc_error(const char* message)
291 int err = alcGetError(device);
292 if(err != ALC_NO_ERROR) {
293 std::stringstream msg;
294 msg << message << alcGetString(device, err);
295 throw std::runtime_error(msg.str());
300 SoundManager::check_al_error(const char* message)
302 int err = alGetError();
303 if(err != AL_NO_ERROR) {
304 std::stringstream msg;
305 msg << message << alGetString(err);
306 throw std::runtime_error(msg.str());