X-Git-Url: https://git.jsancho.org/?p=lugaru.git;a=blobdiff_plain;f=Source%2FAudio%2Fopenal_wrapper.cpp;h=269b868bd71bbd6dde0704f5053b6f67772f102c;hp=d728e5ad8062352b124f8784c5b6586b42318962;hb=b9a46d8e2b7e7e22c706e7dd3734f31015db4408;hpb=8b6e8f3ad7390309795eb35c0959264cb7924402 diff --git a/Source/Audio/openal_wrapper.cpp b/Source/Audio/openal_wrapper.cpp index d728e5a..269b868 100644 --- a/Source/Audio/openal_wrapper.cpp +++ b/Source/Audio/openal_wrapper.cpp @@ -65,8 +65,9 @@ static void set_channel_position(const int channel, const float x, chan->position[2] = z; OPENAL_SAMPLE* sptr = chan->sample; - if (sptr == NULL) + if (sptr == NULL) { return; + } const ALuint sid = chan->sid; const bool no_attenuate = sptr->is2d; @@ -82,8 +83,9 @@ static void set_channel_position(const int channel, const float x, AL_API void OPENAL_3D_Listener_SetAttributes(const float* pos, const float*, float fx, float fy, float fz, float tx, float ty, float tz) { - if (!initialized) + if (!initialized) { return; + } if (pos != NULL) { alListener3f(AL_POSITION, pos[0], pos[1], -pos[2]); listener_position[0] = pos[0]; @@ -105,13 +107,16 @@ AL_API void OPENAL_3D_Listener_SetAttributes(const float* pos, const float*, flo AL_API signed char OPENAL_3D_SetAttributes(int channel, const float* pos) { - if (!initialized) + if (!initialized) { return false; - if ((channel < 0) || (channel >= (int)num_channels)) + } + if ((channel < 0) || (channel >= (int)num_channels)) { return false; + } - if (pos != NULL) + if (pos != NULL) { set_channel_position(channel, pos[0], pos[1], -pos[2]); + } // we ignore velocity, since doppler's broken in the Linux AL at the moment... @@ -120,10 +125,12 @@ AL_API signed char OPENAL_3D_SetAttributes(int channel, const float* pos) AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ& pos) { - if (!initialized) + if (!initialized) { return false; - if ((channel < 0) || (channel >= (int)num_channels)) + } + if ((channel < 0) || (channel >= (int)num_channels)) { return false; + } set_channel_position(channel, pos.x, pos.y, -pos.z); @@ -132,17 +139,21 @@ AL_API signed char OPENAL_3D_SetAttributes_(int channel, const XYZ& pos) AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned int flags) { - if (initialized) + if (initialized) { return false; - if (maxsoftwarechannels == 0) + } + if (maxsoftwarechannels == 0) { return false; + } - if (flags != 0) // unsupported. + if (flags != 0) { // unsupported. return false; + } ALCdevice* dev = alcOpenDevice(NULL); - if (!dev) + if (!dev) { return false; + } ALint caps[] = { ALC_FREQUENCY, mixrate, 0 }; ALCcontext* ctx = alcCreateContext(dev, caps); @@ -164,8 +175,9 @@ AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned in num_channels = maxsoftwarechannels; impl_channels = new OPENAL_Channels[maxsoftwarechannels]; memset(impl_channels, '\0', sizeof(OPENAL_Channels) * num_channels); - for (unsigned i = 0; i < num_channels; i++) + for (unsigned i = 0; i < num_channels; i++) { alGenSources(1, &impl_channels[i].sid); // !!! FIXME: verify this didn't fail! + } initialized = true; return true; @@ -173,8 +185,9 @@ AL_API signed char OPENAL_Init(int mixrate, int maxsoftwarechannels, unsigned in AL_API void OPENAL_Close() { - if (!initialized) + if (!initialized) { return; + } ALCcontext* ctx = alcGetCurrentContext(); if (ctx) { @@ -199,21 +212,26 @@ AL_API void OPENAL_Close() static OPENAL_SAMPLE* OPENAL_GetCurrentSample(int channel) { - if (!initialized) + if (!initialized) { return NULL; - if ((channel < 0) || (channel >= (int)num_channels)) + } + if ((channel < 0) || (channel >= (int)num_channels)) { return NULL; + } return impl_channels[channel].sample; } static signed char OPENAL_GetPaused(int channel) { - if (!initialized) + if (!initialized) { return false; - if ((channel < 0) || (channel >= (int)num_channels)) + } + if ((channel < 0) || (channel >= (int)num_channels)) { return false; - if (impl_channels[channel].startpaused) + } + if (impl_channels[channel].startpaused) { return (true); + } ALint state = 0; alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state); @@ -222,23 +240,28 @@ static signed char OPENAL_GetPaused(int channel) static unsigned int OPENAL_GetLoopMode(int channel) { - if (!initialized) + if (!initialized) { return 0; - if ((channel < 0) || (channel >= (int)num_channels)) + } + if ((channel < 0) || (channel >= (int)num_channels)) { return 0; + } ALint loop = 0; alGetSourceiv(impl_channels[channel].sid, AL_LOOPING, &loop); - if (loop) + if (loop) { return (OPENAL_LOOP_NORMAL); + } return OPENAL_LOOP_OFF; } static signed char OPENAL_IsPlaying(int channel) { - if (!initialized) + if (!initialized) { return false; - if ((channel < 0) || (channel >= (int)num_channels)) + } + if ((channel < 0) || (channel >= (int)num_channels)) { return false; + } ALint state = 0; alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state); return ((state == AL_PLAYING) ? true : false); @@ -246,12 +269,15 @@ static signed char OPENAL_IsPlaying(int channel) static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE* sptr, OPENAL_DSPUNIT* dsp, signed char startpaused) { - if (!initialized) + if (!initialized) { return -1; - if (sptr == NULL) + } + if (sptr == NULL) { return -1; - if (dsp != NULL) + } + if (dsp != NULL) { return -1; + } if (channel == OPENAL_FREE) { for (unsigned i = 0; i < num_channels; i++) { ALint state = 0; @@ -263,8 +289,9 @@ static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE* sptr, OPENAL_DSPUNIT* } } - if ((channel < 0) || (channel >= (int)num_channels)) + if ((channel < 0) || (channel >= (int)num_channels)) { return -1; + } alSourceStop(impl_channels[channel].sid); impl_channels[channel].sample = sptr; alSourcei(impl_channels[channel].sid, AL_BUFFER, sptr->bid); @@ -272,8 +299,9 @@ static int OPENAL_PlaySoundEx(int channel, OPENAL_SAMPLE* sptr, OPENAL_DSPUNIT* set_channel_position(channel, 0.0f, 0.0f, 0.0f); impl_channels[channel].startpaused = ((startpaused) ? true : false); - if (!startpaused) + if (!startpaused) { alSourcePlay(impl_channels[channel].sid); + } return channel; } @@ -289,14 +317,16 @@ static void* decode_to_pcm(const char* _fname, ALenum& format, ALsizei& size, AL char* fname = (char*)alloca(strlen(_fname) + 16); strcpy(fname, _fname); char* ptr = strchr(fname, '.'); - if (ptr) + if (ptr) { *ptr = '\0'; + } strcat(fname, ".ogg"); // just in case... FILE* io = fopen(fname, "rb"); - if (io == NULL) + if (io == NULL) { return NULL; + } ALubyte* retval = NULL; @@ -364,24 +394,30 @@ static void* decode_to_pcm(const char* _fname, ALenum& format, ALsizei& size, AL AL_API OPENAL_SAMPLE* OPENAL_Sample_Load(int index, const char* name_or_data, unsigned int mode, int offset, int length) { - if (!initialized) + if (!initialized) { return NULL; - if (index != OPENAL_FREE) + } + if (index != OPENAL_FREE) { return NULL; // this is all the game does... - if (offset != 0) + } + if (offset != 0) { return NULL; // this is all the game does... - if (length != 0) + } + if (length != 0) { return NULL; // this is all the game does... - if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) + } + if ((mode != OPENAL_HW3D) && (mode != OPENAL_2D)) { return NULL; // this is all the game does... + } OPENAL_SAMPLE* retval = NULL; ALenum format = AL_NONE; ALsizei size = 0; ALuint frequency = 0; void* data = decode_to_pcm(name_or_data, format, size, frequency); - if (data == NULL) + if (data == NULL) { return NULL; + } ALuint bid = 0; alGetError(); @@ -393,8 +429,9 @@ AL_API OPENAL_SAMPLE* OPENAL_Sample_Load(int index, const char* name_or_data, un retval->mode = OPENAL_LOOP_OFF; retval->is2d = (mode == OPENAL_2D); retval->name = new char[strlen(name_or_data) + 1]; - if (retval->name) + if (retval->name) { strcpy(retval->name, name_or_data); + } } free(data); @@ -403,8 +440,9 @@ AL_API OPENAL_SAMPLE* OPENAL_Sample_Load(int index, const char* name_or_data, un AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE* sptr) { - if (!initialized) + if (!initialized) { return; + } if (sptr) { for (unsigned i = 0; i < num_channels; i++) { if (impl_channels[i].sample == sptr) { @@ -421,53 +459,64 @@ AL_API void OPENAL_Sample_Free(OPENAL_SAMPLE* sptr) static signed char OPENAL_Sample_SetMode(OPENAL_SAMPLE* sptr, unsigned int mode) { - if (!initialized) + if (!initialized) { return false; - if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF)) + } + if ((mode != OPENAL_LOOP_NORMAL) && (mode != OPENAL_LOOP_OFF)) { return false; - if (!sptr) + } + if (!sptr) { return false; + } sptr->mode = mode; return true; } AL_API signed char OPENAL_SetFrequency(int channel, bool slomo) { - if (!initialized) + if (!initialized) { return false; + } if (channel == OPENAL_ALL) { - for (unsigned i = 0; i < num_channels; i++) + for (unsigned i = 0; i < num_channels; i++) { OPENAL_SetFrequency(i, slomo); + } return true; } - if ((channel < 0) || (channel >= (int)num_channels)) + if ((channel < 0) || (channel >= (int)num_channels)) { return false; - if (slomo) + } + if (slomo) { alSourcef(impl_channels[channel].sid, AL_PITCH, ((ALfloat)slomofreq) / 44100.0f); - else + } else { alSourcef(impl_channels[channel].sid, AL_PITCH, 1.0f); + } return true; } AL_API signed char OPENAL_SetVolume(int channel, int vol) { - if (!initialized) + if (!initialized) { return false; + } if (channel == OPENAL_ALL) { - for (unsigned i = 0; i < num_channels; i++) + for (unsigned i = 0; i < num_channels; i++) { OPENAL_SetVolume(i, vol); + } return true; } - if ((channel < 0) || (channel >= (int)num_channels)) + if ((channel < 0) || (channel >= (int)num_channels)) { return false; + } - if (vol < 0) + if (vol < 0) { vol = 0; - else if (vol > 255) + } else if (vol > 255) { vol = 255; + } ALfloat gain = ((ALfloat)vol) / 255.0f; alSourcef(impl_channels[channel].sid, AL_GAIN, gain); return true; @@ -475,27 +524,31 @@ AL_API signed char OPENAL_SetVolume(int channel, int vol) AL_API signed char OPENAL_SetPaused(int channel, signed char paused) { - if (!initialized) + if (!initialized) { return false; + } if (channel == OPENAL_ALL) { - for (unsigned i = 0; i < num_channels; i++) + for (unsigned i = 0; i < num_channels; i++) { OPENAL_SetPaused(i, paused); + } return true; } - if ((channel < 0) || (channel >= (int)num_channels)) + if ((channel < 0) || (channel >= (int)num_channels)) { return false; + } ALint state = 0; - if (impl_channels[channel].startpaused) + if (impl_channels[channel].startpaused) { state = AL_PAUSED; - else + } else { alGetSourceiv(impl_channels[channel].sid, AL_SOURCE_STATE, &state); + } - if ((paused) && (state == AL_PLAYING)) + if ((paused) && (state == AL_PLAYING)) { alSourcePause(impl_channels[channel].sid); - else if ((!paused) && (state == AL_PAUSED)) { + } else if ((!paused) && (state == AL_PAUSED)) { alSourcePlay(impl_channels[channel].sid); impl_channels[channel].startpaused = false; } @@ -504,25 +557,29 @@ AL_API signed char OPENAL_SetPaused(int channel, signed char paused) AL_API void OPENAL_SetSFXMasterVolume(int volume) { - if (!initialized) + if (!initialized) { return; + } ALfloat gain = ((ALfloat)volume) / 255.0f; alListenerf(AL_GAIN, gain); } AL_API signed char OPENAL_StopSound(int channel) { - if (!initialized) + if (!initialized) { return false; + } if (channel == OPENAL_ALL) { - for (unsigned i = 0; i < num_channels; i++) + for (unsigned i = 0; i < num_channels; i++) { OPENAL_StopSound(i); + } return true; } - if ((channel < 0) || (channel >= (int)num_channels)) + if ((channel < 0) || (channel >= (int)num_channels)) { return false; + } alSourceStop(impl_channels[channel].sid); impl_channels[channel].startpaused = false; return true; @@ -530,8 +587,9 @@ AL_API signed char OPENAL_StopSound(int channel) static OPENAL_SAMPLE* OPENAL_Stream_GetSample(OPENAL_STREAM* stream) { - if (!initialized) + if (!initialized) { return NULL; + } return (OPENAL_SAMPLE*)stream; } @@ -542,8 +600,9 @@ static int OPENAL_Stream_PlayEx(int channel, OPENAL_STREAM* stream, OPENAL_DSPUN static signed char OPENAL_Stream_Stop(OPENAL_STREAM* stream) { - if (!initialized) + if (!initialized) { return false; + } for (unsigned i = 0; i < num_channels; i++) { if (impl_channels[i].sample == (OPENAL_SAMPLE*)stream) { alSourceStop(impl_channels[i].sid); @@ -560,8 +619,9 @@ AL_API signed char OPENAL_Stream_SetMode(OPENAL_STREAM* stream, unsigned int mod AL_API void OPENAL_Update() { - if (!initialized) + if (!initialized) { return; + } alcProcessContext(alcGetCurrentContext()); }