diff options
Diffstat (limited to 'src/audio')
-rw-r--r-- | src/audio/audio.cc | 34 | ||||
-rw-r--r-- | src/audio/audio.h | 6 | ||||
-rw-r--r-- | src/audio/buffers.cc | 21 | ||||
-rw-r--r-- | src/audio/buffers.h | 10 | ||||
-rw-r--r-- | src/audio/pcm.cc | 3 | ||||
-rw-r--r-- | src/audio/pcm.h | 30 | ||||
-rw-r--r-- | src/audio/sources.cc | 18 | ||||
-rw-r--r-- | src/audio/sources.h | 18 | ||||
-rw-r--r-- | src/audio/wav.cc | 19 | ||||
-rw-r--r-- | src/audio/wav.h | 6 |
10 files changed, 96 insertions, 69 deletions
diff --git a/src/audio/audio.cc b/src/audio/audio.cc index 2192e2f..e1eb0d3 100644 --- a/src/audio/audio.cc +++ b/src/audio/audio.cc @@ -29,7 +29,7 @@ void init() } // create the audio context - audio_context = alcCreateContext(audio_device ,0); + audio_context = alcCreateContext(audio_device , 0); if (!audio_context) { alcCloseDevice(audio_device); @@ -42,11 +42,11 @@ void init() // clear errors alGetError(); - + Buffers::init(); Sources::init(); - + //con_debug << " audio device ^B" << alcGetString(audio_device, ALC_DEFAULT_DEVICE_SPECIFIER) << std::endl; // the "no sound" sound @@ -64,12 +64,12 @@ void reset() Buffers::shutdown(); Buffers::init(); Sources::init(); - + load("ui/nosnd"); load("ui/console"); } -void load (const char *name) +void load(const char *name) { Buffers::load(std::string(name)); } @@ -91,7 +91,7 @@ void shutdown() if (audio_device) { alcCloseDevice(audio_device); audio_device = 0; - } + } } size_t play(const char *name) @@ -101,14 +101,14 @@ size_t play(const char *name) size_t buffer_index = Buffers::load(std::string(name)); - for (size_t i = 0; i < MAXUISOURCES; i++) { + for (size_t i = 0; i < MAXUISOURCES; i++) { ALint srcstate = 0; ALuint source = Sources::source(i); alGetSourcei(source , AL_SOURCE_STATE , &srcstate); if (srcstate != AL_PLAYING) { Buffers::bind(source, buffer_index); - alSourcef(source, AL_PITCH, 1.0); - alSourcef(source, AL_GAIN, 1.0); + alSourcef(source, AL_PITCH, 1.0); + alSourcef(source, AL_GAIN, 1.0); alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE); ALfloat location[] = {0.0f, 0.0f, 0.0f}; alSourcefv(source, AL_POSITION, location); @@ -121,7 +121,7 @@ size_t play(const char *name) return 0; } -size_t play(size_t source_index, size_t buffer_index, float pitch, float gain) +size_t play(size_t source_index, size_t buffer_index, float pitch, float gain) { if (!audio_context || !buffer_index) return 0; @@ -135,9 +135,9 @@ size_t play(size_t source_index, size_t buffer_index, float pitch, float gain) Buffers::bind(source, buffer_index); alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE); alSourcef(source, AL_PITCH, pitch); - alSourcef(source, AL_GAIN, gain); + alSourcef(source, AL_GAIN, gain); - alSourcei(source, AL_LOOPING, AL_FALSE); + alSourcei(source, AL_LOOPING, AL_FALSE); alSourceRewind(source); alSourcePlay(source); @@ -161,9 +161,9 @@ size_t loop(size_t source_index, size_t buffer_index, float pitch, float gain) //alSourcef(source, AL_REFERENCE_DISTANCE, 2.0f); // might be the cause of cracks in the sound alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE); alSourcef(source, AL_PITCH, pitch); - alSourcef(source, AL_GAIN, gain); + alSourcef(source, AL_GAIN, gain); - alSourcei(source, AL_LOOPING, AL_TRUE); + alSourcei(source, AL_LOOPING, AL_TRUE); alSourceRewind(source); alSourcePlay(source); @@ -190,7 +190,7 @@ void update_listener(math::Vector3f const &location, math::Axis const &axis, mat alListenerfv(AL_POSITION, location.ptr()); ALfloat orientation[6]; - for (size_t i =0; i <3; i++) { + for (size_t i = 0; i < 3; i++) { orientation[i] = axis.forward()[i]; orientation[i+3] = axis.up()[i]; } @@ -208,7 +208,7 @@ void update_source(size_t source_index, math::Vector3f const & location, math::V alSourcefv(source, AL_POSITION, location.ptr()); //alSourcefv(source, AL_VELOCITY, velocity.ptr()); alSourcef(source, AL_PITCH, pitch); - alSourcef(source, AL_GAIN, gain); + alSourcef(source, AL_GAIN, gain); } - + } diff --git a/src/audio/audio.h b/src/audio/audio.h index 2849013..72a8fa4 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -45,16 +45,16 @@ void load(const char *name); size_t play(const char *name); /// playe a sound from a specific buffer on a specific source -size_t play(size_t source_index, size_t buffer_index, float pitch=1.0f, float gain=1.0f); +size_t play(size_t source_index, size_t buffer_index, float pitch = 1.0f, float gain = 1.0f); /// play a looping sound on a specified source -size_t loop( size_t source_index, const char *name, float pitch=1.0f, float gain=1.0f); +size_t loop(size_t source_index, const char *name, float pitch = 1.0f, float gain = 1.0f); /// play a looping sound from a specified buffer on a specified source size_t loop(size_t source_index, size_t buffer_index, float pitch, float gain); /// update source parameters -void update_source(size_t source_index, math::Vector3f const & location, math::Vector3f const & velocity, float pitch=1.0f, float gain=1.0f); +void update_source(size_t source_index, math::Vector3f const & location, math::Vector3f const & velocity, float pitch = 1.0f, float gain = 1.0f); /// update listener parameters void update_listener(math::Vector3f const &location, math::Axis const &axis, math::Vector3f const & velocity); diff --git a/src/audio/buffers.cc b/src/audio/buffers.cc index 774639e..7bf7eb1 100644 --- a/src/audio/buffers.cc +++ b/src/audio/buffers.cc @@ -11,7 +11,8 @@ #include "audio/wav.h" #include "sys/sys.h" -namespace audio { +namespace audio +{ std::map<std::string, size_t> Buffers::registry; size_t Buffers::index; @@ -19,8 +20,8 @@ ALuint Buffers::buffers[MAXBUFFERS]; void Buffers::init() { - clear(); - + clear(); + alGenBuffers(MAXBUFFERS, buffers); int error; @@ -33,20 +34,20 @@ void Buffers::init() void Buffers::shutdown() { alDeleteBuffers(MAXBUFFERS, buffers); - + int error; if ((error = alGetError()) != AL_NO_ERROR) { con_warn << "Error " << error << " clearing OpenAL buffers!" << std::endl; return; } - + clear(); } void Buffers::clear() { registry.clear(); - memset(buffers,0, sizeof(buffers)); + memset(buffers, 0, sizeof(buffers)); index = 0; } @@ -63,7 +64,7 @@ size_t Buffers::load(std::string name) if (!pcm) { registry[name] = 0; return 0; - } + } if (index == MAXBUFFERS) { con_error << "Buffer limit " << MAXBUFFERS << " exceeded!" << std::endl; @@ -71,7 +72,7 @@ size_t Buffers::load(std::string name) registry[name] = 0; return 0; } - + ALenum format = 0; if (pcm->bitspersample() == 16) { if (pcm->channels() == 1) { @@ -86,10 +87,10 @@ size_t Buffers::load(std::string name) format = AL_FORMAT_STEREO8; }; } - + size_t id = index; alBufferData(buffers[id], format, pcm->data(), pcm->size(), pcm->samplerate()); - if (alGetError()!= AL_NO_ERROR) { + if (alGetError() != AL_NO_ERROR) { con_warn << "Error loading PCM data " << name << std::endl; } diff --git a/src/audio/buffers.h b/src/audio/buffers.h index 2b31089..149c7f7 100644 --- a/src/audio/buffers.h +++ b/src/audio/buffers.h @@ -11,22 +11,24 @@ #ifdef _OSX #include "OpenAL/al.h" -#include "OpenAL/alc.h" +#include "OpenAL/alc.h" #else -#include "AL/al.h" +#include "AL/al.h" #include "AL/alc.h" #endif #include <string> #include <map> -namespace audio { +namespace audio +{ const size_t MAXBUFFERS = 128; /// OpenAL buffers wrapper class -class Buffers { +class Buffers +{ public: static void init(); static void shutdown(); diff --git a/src/audio/pcm.cc b/src/audio/pcm.cc index 7a3f767..dacb23c 100644 --- a/src/audio/pcm.cc +++ b/src/audio/pcm.cc @@ -10,7 +10,8 @@ #include "audio/pcm.h" #include "audio/wav.h" -namespace audio { +namespace audio +{ PCM::PCM(unsigned int samplerate, unsigned int bitspersample, unsigned int channels, size_t size) { diff --git a/src/audio/pcm.h b/src/audio/pcm.h index dc8eb16..8df44e8 100644 --- a/src/audio/pcm.h +++ b/src/audio/pcm.h @@ -9,10 +9,12 @@ #include <string> -namespace audio { +namespace audio +{ /// class to hold PCM audio data -class PCM { +class PCM +{ public: PCM(unsigned int samplerate, unsigned int samplesize, unsigned int channels, size_t size); @@ -21,22 +23,34 @@ public: void clear(); /// pointer to the raw pcm data - inline unsigned char *data() { return pcm_data; } + inline unsigned char *data() { + return pcm_data; + } /// index into the raw pcm data - inline unsigned char *operator[](size_t index) { return &pcm_data[index]; } + inline unsigned char *operator[](size_t index) { + return &pcm_data[index]; + } /// size in bytes - inline size_t size() { return pcm_size; } + inline size_t size() { + return pcm_size; + } /// samplerate in samples per second - inline unsigned int samplerate() const { return pcm_samplerate; } + inline unsigned int samplerate() const { + return pcm_samplerate; + } /// number of bits per sample - inline unsigned int bitspersample() const { return pcm_bitspersample; } + inline unsigned int bitspersample() const { + return pcm_bitspersample; + } /// number of channels - inline unsigned int channels() const { return pcm_channels; } + inline unsigned int channels() const { + return pcm_channels; + } private: unsigned char *pcm_data; diff --git a/src/audio/sources.cc b/src/audio/sources.cc index 2ec0e26..241332b 100644 --- a/src/audio/sources.cc +++ b/src/audio/sources.cc @@ -19,7 +19,7 @@ void Sources::init() { int error; clear(); - + alGenSources(MAXSOURCES, sources); if ((error = alGetError()) != AL_NO_ERROR) { @@ -33,7 +33,7 @@ void Sources::init() } // reserve ui sound sources - for (size_t i=0; i < MAXUISOURCES; i++) { + for (size_t i = 0; i < MAXUISOURCES; i++) { source_available[i] = false; } } @@ -41,7 +41,7 @@ void Sources::init() void Sources::shutdown() { // stop all sources - for (size_t index= 0; index < MAXSOURCES; index++) { + for (size_t index = 0; index < MAXSOURCES; index++) { alSourceRewind(sources[index]); } @@ -51,21 +51,21 @@ void Sources::shutdown() void Sources::clear() { - memset(sources,0, sizeof(sources)); + memset(sources, 0, sizeof(sources)); // all sources are available for use - for (size_t i=0; i < MAXSOURCES; i++) { + for (size_t i = 0; i < MAXSOURCES; i++) { source_available[i] = false; } } size_t Sources::get() { - for (size_t i= MAXUISOURCES; i < MAXSOURCES; i++) { + for (size_t i = MAXUISOURCES; i < MAXSOURCES; i++) { if (source_available[i]) { source_available[i] = false; - alSourcef(sources[i], AL_PITCH, 1.0); - alSourcef(sources[i], AL_GAIN, 0.0); + alSourcef(sources[i], AL_PITCH, 1.0); + alSourcef(sources[i], AL_GAIN, 0.0); //con_debug << "reserved source " << i << std::endl; return i; } @@ -75,7 +75,7 @@ size_t Sources::get() void Sources::remove(size_t index) { - if ( (index < MAXUISOURCES ) || (MAXSOURCES <= index)) + if ((index < MAXUISOURCES) || (MAXSOURCES <= index)) return; source_available[index] = true; diff --git a/src/audio/sources.h b/src/audio/sources.h index c9e38e2..6292ff6 100644 --- a/src/audio/sources.h +++ b/src/audio/sources.h @@ -11,29 +11,35 @@ #ifdef _OSX #include "OpenAL/al.h" -#include "OpenAL/alc.h" +#include "OpenAL/alc.h" #else -#include "AL/al.h" +#include "AL/al.h" #include "AL/alc.h" #endif #include <string> #include <map> -namespace audio { +namespace audio +{ const size_t MAXSOURCES = 32; const size_t MAXUISOURCES = 4; /// OpenAL sources wrapper class -class Sources { +class Sources +{ public: static void init(); static void shutdown(); - static inline bool available(size_t index) { return source_available[index]; } + static inline bool available(size_t index) { + return source_available[index]; + } - static inline ALuint source(size_t index) { return sources[index]; } + static inline ALuint source(size_t index) { + return sources[index]; + } static size_t get(); static void remove(size_t index); diff --git a/src/audio/wav.cc b/src/audio/wav.cc index 178f199..b6ca710 100644 --- a/src/audio/wav.cc +++ b/src/audio/wav.cc @@ -5,7 +5,7 @@ */ /* -see +see http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/ */ @@ -19,7 +19,8 @@ http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/ #include "filesystem/filesystem.h" #include "sys/sys.h" -namespace audio { +namespace audio +{ PCM *Wav::load(std::string const & name) { @@ -29,7 +30,7 @@ PCM *Wav::load(std::string const & name) std::string filename("sounds/"); filename.append(name); filename.append(".wav"); - + filesystem::File *wav_file = filesystem::open(filename.c_str()); if (!wav_file) { con_warn << "Could not open " << filename << std::endl; @@ -52,7 +53,7 @@ PCM *Wav::load(std::string const & name) } // format WAVE - if (strncmp((char *)header+8, "WAVE", 4) != 0) { + if (strncmp((char *)header + 8, "WAVE", 4) != 0) { con_warn << "Error reading " << filename << ": invalid WAVE header!" << std::endl; filesystem::close(wav_file); return 0; @@ -61,7 +62,7 @@ PCM *Wav::load(std::string const & name) // file size //size_t chunksize = header[4] + (header[5] << 8) + (header[6] << 16) + (header[7] << 24); - if (strncmp((char *)header+12, "fmt ", 4) != 0) { + if (strncmp((char *)header + 12, "fmt ", 4) != 0) { con_warn << "Error reading " << filename << ": invalid format header!" << std::endl; filesystem::close(wav_file); return 0; @@ -78,7 +79,7 @@ PCM *Wav::load(std::string const & name) } unsigned int channels = header[22] + (header[23] << 8); - if ((channels < 1) || (channels >2)) { + if ((channels < 1) || (channels > 2)) { con_warn << "Error reading " << filename << ": invalid number of channels!" << std::endl; filesystem::close(wav_file); return 0; @@ -89,7 +90,7 @@ PCM *Wav::load(std::string const & name) //size_t blockalign = header[32] + (header[33] << 8); size_t bitspersample = header[34] + (header[35] << 8); - if (strncmp((char *)header + 36, "data", 4) !=0) { + if (strncmp((char *)header + 36, "data", 4) != 0) { con_warn << "Error reading " << filename << ": invalid data header!" << std::endl; filesystem::close(wav_file); return 0; @@ -109,8 +110,8 @@ PCM *Wav::load(std::string const & name) con_warn << "Error reading " << filename << ": file truncated!" << std::endl; } - con_debug << " " << filename << " " << pcm->samplerate()<< "Hz " << pcm->bitspersample() << "bit " << - pcm->channels() << " chan " << pcm->size() << " bytes" << std::endl; + con_debug << " " << filename << " " << pcm->samplerate() << "Hz " << pcm->bitspersample() << "bit " << + pcm->channels() << " chan " << pcm->size() << " bytes" << std::endl; filesystem::close(wav_file); return pcm; diff --git a/src/audio/wav.h b/src/audio/wav.h index a94b6fb..2513b48 100644 --- a/src/audio/wav.h +++ b/src/audio/wav.h @@ -9,10 +9,12 @@ #include "audio/pcm.h" -namespace audio { +namespace audio +{ /// class to read microsoft PCM wav files -class Wav { +class Wav +{ public: static PCM *load(std::string const & name); }; |