/* audio/audio.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include "audio/audio.h" #include "audio/buffers.h" #include "audio/pcm.h" #include "audio/sources.h" #include "sys/sys.h" namespace audio { ALCdevice *audio_device = 0; ALCcontext *audio_context = 0; void init() { con_print << "^BInitializing audio..."; // open the default audio device audio_device = alcOpenDevice(0); if (!audio_device) { con_warn << "Could not initialize audio!"; return; } // create the audio context audio_context = alcCreateContext(audio_device ,0); alcMakeContextCurrent(audio_context); // clear errors alGetError(); Buffers::init(); Sources::init(); //con_debug << " device ^B" << alcGetString(audio_device, ALC_DEFAULT_DEVICE_SPECIFIER) << std::endl; load("com/chat"); load("com/hail"); } void load (const char *name) { Buffers::load(std::string(name)); } void shutdown() { con_print << "^BShutting down audio..."; Sources::shutdown(); Buffers::shutdown(); if (audio_context) { alcMakeContextCurrent(0); alcDestroyContext(audio_context); audio_context = 0; } if (audio_device) { alcCloseDevice(audio_device); audio_device = 0; } } void play(const char *name) { if (!audio_context) return; size_t buffer = Buffers::load(std::string(name)); ALint state; for (size_t i = 0; i < MAXUISOURCES; i++) { ALuint source = Sources::source(i); alGetSourcei(source , AL_SOURCE_STATE , &state); if (state != AL_PLAYING) { //alSourceRewind(Sources::source(i)); Buffers::bind(source, buffer); alSourcef(source, AL_PITCH, 1.0); alSourcef(source, AL_GAIN, 1.0); alSourcePlay(source); } else { con_debug << "source " << Sources::source(i) << " playing!" << std::endl; } } } }