# HG changeset patch # User koda # Date 1271845326 0 # Node ID 8d3b4d19ce27292ec2e2ccacac65ed2ffa826104 # Parent cfc6cd502f85a175351518252cb7addca7ebe561 in reprocessing openalbridge, a lot of code cleanup and simplification diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/CMakeLists.txt --- a/misc/openalbridge/CMakeLists.txt Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/CMakeLists.txt Wed Apr 21 10:22:06 2010 +0000 @@ -14,11 +14,11 @@ #build a static library for human systems set (build_type STATIC) -#visualstudio and windows in general doesn't like static linking, so we're building the library in shared mode +#visualstudio and windows in general don't like static linking, so we're building the library in shared mode if(WIN32) #workaround for visualstudio (wants headers in the source list) set(openal_src - openalbridge.h loaders.h wrappers.h globals.h oggvorbis.h errlib.h ${openal_src} + openalbridge.h openalbridge_t.h loaders.h wrappers.h globals.h oggvorbis.h errlib.h ${openal_src} ) #deps for the shared library link_libraries(${VORBISFILE_LIBRARY}) diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/errlib.c --- a/misc/openalbridge/errlib.c Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/errlib.c Wed Apr 21 10:22:06 2010 +0000 @@ -1,11 +1,11 @@ /* - + module: errlib.c - + purpose: library of error functions - + reference: Stevens, Unix network programming (2ed), p.922 - + */ #include "errlib.h" @@ -13,84 +13,70 @@ #define MAXLINE 4095 -#ifdef __CPLUSPLUS -extern "C" { -#endif - - int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */ - - static void err_doit (int errnoflag, int level, const char *fmt, va_list ap) - { - int errno_save = errno, n; - char buf[MAXLINE+1]; - - vsnprintf (buf, MAXLINE, fmt, ap); - n = strlen(buf); - if (errnoflag) - snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save)); - strcat (buf, "\n"); - - if (daemon_proc) - syslog (level, buf); - else { - fflush (stdout); - fprintf (stderr, "%s", buf); - fflush (stderr); - } - - return; - } - - void err_ret (const char *fmt, ...) - { - va_list ap; - - va_start (ap, fmt); - err_doit (1, LOG_INFO, fmt, ap); - va_end (ap); - return; +int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */ + +static void err_doit (int errnoflag, int level, const char *fmt, va_list ap) { + int errno_save = errno, n; + char buf[MAXLINE+1]; + + vsnprintf (buf, MAXLINE, fmt, ap); + n = strlen(buf); + if (errnoflag) + snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save)); + strcat (buf, "\n"); + + if (daemon_proc) + syslog (level, buf); + else { + fflush (stdout); + fprintf (stderr, "%s", buf); + fflush (stderr); } - - void err_sys (const char *fmt, ...) - { - va_list ap; - - va_start (ap, fmt); - err_doit (1, LOG_ERR, fmt, ap); - va_end (ap); - exit (1); - } - - void err_msg (const char *fmt, ...) - { - va_list ap; - - va_start (ap, fmt); - err_doit (0, LOG_INFO, fmt, ap); - va_end (ap); - return; - } - - void err_quit (const char *fmt, ...) - { - va_list ap; - - va_start (ap, fmt); - err_doit (0, LOG_ERR, fmt, ap); - va_end (ap); - exit (1); - } - - void err_dump (const char *fmt, ...) - { - va_list ap; - - va_start (ap, fmt); - err_doit (1, LOG_ERR, fmt, ap); - va_end (ap); - abort(); - } - -#ifdef __CPLUSPLUS + + return; +} + +void err_ret (const char *fmt, ...) { + va_list ap; + + va_start (ap, fmt); + err_doit (1, LOG_INFO, fmt, ap); + va_end (ap); + return; +} + +void err_sys (const char *fmt, ...) { + va_list ap; + + va_start (ap, fmt); + err_doit (1, LOG_ERR, fmt, ap); + va_end (ap); + exit (1); } -#endif + +void err_msg (const char *fmt, ...) { + va_list ap; + + va_start (ap, fmt); + err_doit (0, LOG_INFO, fmt, ap); + va_end (ap); + return; +} + +void err_quit (const char *fmt, ...) { + va_list ap; + + va_start (ap, fmt); + err_doit (0, LOG_ERR, fmt, ap); + va_end (ap); + exit (1); +} + +void err_dump (const char *fmt, ...) { + va_list ap; + + va_start (ap, fmt); + err_doit (1, LOG_ERR, fmt, ap); + va_end (ap); + abort(); +} diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/errlib.h --- a/misc/openalbridge/errlib.h Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/errlib.h Wed Apr 21 10:22:06 2010 +0000 @@ -1,11 +1,11 @@ /* - + module: errlib.h - + purpose: definitions of function sin errlib.c - + reference: Stevens, Unix network programming (2ed), p.922 - + */ #ifndef _ERRLIB_H @@ -13,31 +13,23 @@ #include "globals.h" -#ifdef __CPLUSPLUS -extern "C" { -#endif - - extern int daemon_proc; - - void err_msg (const char *fmt, ...); - void err_quit (const char *fmt, ...); - void err_ret (const char *fmt, ...); - void err_sys (const char *fmt, ...); - void err_dump (const char *fmt, ...); - -#ifdef __CPLUSPLUS -} -#endif +extern int daemon_proc; + +void err_msg (const char *fmt, ...); +void err_quit (const char *fmt, ...); +void err_ret (const char *fmt, ...); +void err_sys (const char *fmt, ...); +void err_dump (const char *fmt, ...); #endif /*_ERRLIB_H*/ /* suggested error string ( PROG ) LEVEL - TEXT : ERRNO - - errno? closeprog? log level - err_msg no no LOG_INFO - err_quit no exit(1) LOG_ERR - err_ret si no LOG_INFO - err_sys si exit(1) LOG_ERR + + errno? closeprog? log level + err_msg no no LOG_INFO + err_quit no exit(1) LOG_ERR + err_ret si no LOG_INFO + err_sys si exit(1) LOG_ERR err_dump si abort( ) LOG_ERR - */ \ No newline at end of file + */ diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/globals.h --- a/misc/openalbridge/globals.h Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/globals.h Wed Apr 21 10:22:06 2010 +0000 @@ -40,14 +40,14 @@ #include "errlib.h" -/*control debug verbosity*/ +// control debug verbosity #ifdef TRACE #ifndef DEBUG #define DEBUG #endif #endif -/** 1.0 02/03/10 - Defines cross-platform sleep, usleep, etc. [Wu Yongwei] **/ +// 1.0 02/03/10 - Defines cross-platform sleep, usleep, etc. [Wu Yongwei] #ifndef _SLEEP_H #define _SLEEP_H #ifdef _WIN32 @@ -69,21 +69,20 @@ # define msleep(t) usleep((t) * 1000) # endif #endif -#endif /* _SLEEP_H */ +#endif // _SLEEP_H -/* check compiler requirements */ /*FIXME*/ +// check compiler requirements #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__) #warning __BIG_ENDIAN__ or __LITTLE_ENDIAN__ not found, going to set __LITTLE_ENDIAN__ as default #define __LITTLE_ENDIAN__ 1 -//#error Do not know the endianess of this architecture #endif -/* use byteswap macros from the host system, hopefully optimized ones ;-) +/* use byteswap macros from the host system, hopefully optimized ones ;-) * or define our own version, simple, stupid, straight-forward... */ #ifdef HAVE_BYTESWAP_H #include -#else +#else #define bswap_16(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) #define bswap_32(x) ((((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | \ (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) ) @@ -99,51 +98,14 @@ #define ENDIAN_LITTLE_32(x) bswap_32(x) #define ENDIAN_BIG_32(x) x #define ENDIAN_LITTLE_16(x) bswap_16(x) -#define ENDIAN_BIG_16(x) x +#define ENDIAN_BIG_16(x) x #endif - -#ifdef __CPLUSPLUS -extern "C" { -#endif - - /*data type for WAV header*/ -#pragma pack(1) - typedef struct _WAV_header_t { - uint32_t ChunkID; - uint32_t ChunkSize; - uint32_t Format; - uint32_t Subchunk1ID; - uint32_t Subchunk1Size; - uint16_t AudioFormat; - uint16_t NumChannels; - uint32_t SampleRate; - uint32_t ByteRate; - uint16_t BlockAlign; - uint16_t BitsPerSample; - uint32_t Subchunk2ID; - uint32_t Subchunk2Size; - } WAV_header_t; -#pragma pack() - - /*data type for passing data between threads*/ -#pragma pack(1) - typedef struct _fade_t { - uint32_t index; - uint16_t quantity; - } fade_t; -#pragma pack() - - - /*file format defines*/ +/*file format defines*/ #define OGG_FILE_FORMAT 0x4F676753 #define WAV_FILE_FORMAT 0x52494646 #define WAV_HEADER_SUBCHUNK2ID 0x64617461 - - char *prog; - -#ifdef __CPLUSPLUS -} -#endif + +char *prog = "OpenAL subsystem"; #endif /*_OALB_GLOBALS_H*/ diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/loaders.c --- a/misc/openalbridge/loaders.c Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/loaders.c Wed Apr 21 10:22:06 2010 +0000 @@ -1,251 +1,244 @@ /* - * OpenAL Bridge - a simple portable library for OpenAL interface - * Copyright (c) 2009 Vittorio Giovara - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; version 2 of the License - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ +* OpenAL Bridge - a simple portable library for OpenAL interface +* Copyright (c) 2009 Vittorio Giovara +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published by +* the Free Software Foundation; version 2 of the License +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA +*/ #include "loaders.h" #include "wrappers.h" #include "vorbis/vorbisfile.h" +#include "openalbridge_t.h" -#ifdef __CPLUSPLUS -extern "C" { -#endif - - int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) { - WAV_header_t WAVHeader; - FILE *wavfile; - int32_t t; - uint32_t n = 0; - uint8_t sub0, sub1, sub2, sub3; - - wavfile = Fopen(filename, "rb"); - - fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile); /*RIFF*/ - fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile); - fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile); /*WAVE*/ - +int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) { + WAV_header_t WAVHeader; + FILE *wavfile; + int32_t t; + uint32_t n = 0; + uint8_t sub0, sub1, sub2, sub3; + + wavfile = Fopen(filename, "rb"); + + fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile); /*RIFF*/ + fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile); + fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile); /*WAVE*/ + #ifdef DEBUG - fprintf(stderr, "ChunkID: %X\n", ENDIAN_BIG_32(WAVHeader.ChunkID)); - fprintf(stderr, "ChunkSize: %d\n", ENDIAN_LITTLE_32(WAVHeader.ChunkSize)); - fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format)); + fprintf(stderr, "ChunkID: %X\n", ENDIAN_BIG_32(WAVHeader.ChunkID)); + fprintf(stderr, "ChunkSize: %d\n", ENDIAN_LITTLE_32(WAVHeader.ChunkSize)); + fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format)); #endif - - fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile); /*fmt */ - fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile); - fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile); - fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile); - fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile); - fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile); - fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile); - fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile); - + + fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile); /*fmt */ + fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile); + fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile); + fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile); + fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile); + fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile); + fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile); + fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile); + #ifdef DEBUG - fprintf(stderr, "Subchunk1ID: %X\n", ENDIAN_BIG_32(WAVHeader.Subchunk1ID)); - fprintf(stderr, "Subchunk1Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk1Size)); - fprintf(stderr, "AudioFormat: %d\n", ENDIAN_LITTLE_16(WAVHeader.AudioFormat)); - fprintf(stderr, "NumChannels: %d\n", ENDIAN_LITTLE_16(WAVHeader.NumChannels)); - fprintf(stderr, "SampleRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.SampleRate)); - fprintf(stderr, "ByteRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.ByteRate)); - fprintf(stderr, "BlockAlign: %d\n", ENDIAN_LITTLE_16(WAVHeader.BlockAlign)); - fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample)); + fprintf(stderr, "Subchunk1ID: %X\n", ENDIAN_BIG_32(WAVHeader.Subchunk1ID)); + fprintf(stderr, "Subchunk1Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk1Size)); + fprintf(stderr, "AudioFormat: %d\n", ENDIAN_LITTLE_16(WAVHeader.AudioFormat)); + fprintf(stderr, "NumChannels: %d\n", ENDIAN_LITTLE_16(WAVHeader.NumChannels)); + fprintf(stderr, "SampleRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.SampleRate)); + fprintf(stderr, "ByteRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.ByteRate)); + fprintf(stderr, "BlockAlign: %d\n", ENDIAN_LITTLE_16(WAVHeader.BlockAlign)); + fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample)); #endif - - /*remove useless header chunks by looking for the WAV_HEADER_SUBCHUNK2ID integer */ - do { - t = fread(&sub0, sizeof(uint8_t), 1, wavfile); - if(sub0 == 0x64) { - t = fread(&sub1, sizeof(uint8_t), 1, wavfile); - if(sub1 == 0x61) { - t = fread(&sub2, sizeof(uint8_t), 1, wavfile); - if(sub2 == 0x74) { - t = fread(&sub3, sizeof(uint8_t), 1, wavfile); - if(sub3 == 0x61) { - WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID; - break; - } - } + + /*remove useless header chunks by looking for the WAV_HEADER_SUBCHUNK2ID integer */ + do { + t = fread(&sub0, sizeof(uint8_t), 1, wavfile); + if(sub0 == 0x64) { + t = fread(&sub1, sizeof(uint8_t), 1, wavfile); + if(sub1 == 0x61) { + t = fread(&sub2, sizeof(uint8_t), 1, wavfile); + if(sub2 == 0x74) { + t = fread(&sub3, sizeof(uint8_t), 1, wavfile); + if(sub3 == 0x61) { + WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID; + break; + } } } - - if (t <= 0) { - /*eof*/ + } + + if (t <= 0) { + /*eof*/ + errno = EILSEQ; + err_ret("(%s) ERROR - wrong WAV header", prog); + return AL_FALSE; + } + } while (1); + + fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile); + +#ifdef DEBUG + fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID)); + fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); +#endif + + *data = (char*) Malloc (sizeof(char) * ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); + + /*read the actual sound data*/ + do { + n += fread(&((*data)[n]), sizeof(uint8_t), 4, wavfile); + } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); + + fclose(wavfile); + +#ifdef DEBUG + err_msg("(%s) INFO - WAV data loaded", prog); +#endif + + /*set parameters for OpenAL*/ + /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/ + if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) { + if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8) + *format = AL_FORMAT_MONO8; + else { + if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16) + *format = AL_FORMAT_MONO16; + else { errno = EILSEQ; - err_ret("(%s) ERROR - wrong WAV header", prog); + err_ret("(%s) ERROR - wrong WAV header [bitsample value]", prog); return AL_FALSE; } - } while (1); - - fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile); - -#ifdef DEBUG - fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID)); - fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); -#endif - - *data = (char*) Malloc (sizeof(char) * ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); - - /*read the actual sound data*/ - do { - n += fread(&((*data)[n]), sizeof(uint8_t), 4, wavfile); - } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); - - fclose(wavfile); - -#ifdef DEBUG - err_msg("(%s) INFO - WAV data loaded", prog); -#endif - - /*set parameters for OpenAL*/ - /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/ - if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) { + } + } else { + if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) { if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8) - *format = AL_FORMAT_MONO8; + *format = AL_FORMAT_STEREO8; else { if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16) - *format = AL_FORMAT_MONO16; + *format = AL_FORMAT_STEREO16; else { errno = EILSEQ; err_ret("(%s) ERROR - wrong WAV header [bitsample value]", prog); return AL_FALSE; } - } + } } else { - if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) { - if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8) - *format = AL_FORMAT_STEREO8; - else { - if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16) - *format = AL_FORMAT_STEREO16; - else { - errno = EILSEQ; - err_ret("(%s) ERROR - wrong WAV header [bitsample value]", prog); - return AL_FALSE; - } - } - } else { - errno = EILSEQ; - err_ret("(%s) ERROR - wrong WAV header [format value]", prog); - return AL_FALSE; - } + errno = EILSEQ; + err_ret("(%s) ERROR - wrong WAV header [format value]", prog); + return AL_FALSE; } - - *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size); - *freq = ENDIAN_LITTLE_32(WAVHeader.SampleRate); - return AL_TRUE; } - - - int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) { - /*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */ - - /*ogg handle*/ - FILE *oggFile; - /*stream handle*/ - OggVorbis_File oggStream; - /*some formatting data*/ - vorbis_info *vorbisInfo; - /*length of the decoded data*/ - int64_t pcm_length; - /*other vars*/ - int section, result, size, endianness; + + *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size); + *freq = ENDIAN_LITTLE_32(WAVHeader.SampleRate); + return AL_TRUE; +} + + +int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) { + /*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */ + + /*ogg handle*/ + FILE *oggFile; + /*stream handle*/ + OggVorbis_File oggStream; + /*some formatting data*/ + vorbis_info *vorbisInfo; + /*length of the decoded data*/ + int64_t pcm_length; + /*other vars*/ + int section, result, size, endianness; #ifdef DEBUG - int i; - /*other less useful data*/ - vorbis_comment *vorbisComment; + int i; + /*other less useful data*/ + vorbis_comment *vorbisComment; #endif - - oggFile = Fopen(filename, "rb"); - result = ov_open_callbacks(oggFile, &oggStream, NULL, 0, OV_CALLBACKS_DEFAULT); - if (result < 0) { - errno = EINVAL; - err_ret("(%s) ERROR - ov_fopen() failed with %X", prog, result); + + oggFile = Fopen(filename, "rb"); + result = ov_open_callbacks(oggFile, &oggStream, NULL, 0, OV_CALLBACKS_DEFAULT); + if (result < 0) { + errno = EINVAL; + err_ret("(%s) ERROR - ov_fopen() failed with %X", prog, result); + ov_clear(&oggStream); + return -1; + } + + /*load OGG header and determine the decoded data size*/ + vorbisInfo = ov_info(&oggStream, -1); + pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels; + +#ifdef DEBUG + vorbisComment = ov_comment(&oggStream, -1); + fprintf(stderr, "Version: %d\n", vorbisInfo->version); + fprintf(stderr, "Channels: %d\n", vorbisInfo->channels); + fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate); + fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper); + fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal); + fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower); + fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window); + fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor); + fprintf(stderr, "PCM data size: %lld\n", pcm_length); + fprintf(stderr, "# comment: %d\n", vorbisComment->comments); + for (i = 0; i < vorbisComment->comments; i++) + fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]); +#endif + + /*allocates enough room for the decoded data*/ + *data = (char*) Malloc (sizeof(char) * pcm_length); + + /*there *should* not be ogg at 8 bits*/ + if (vorbisInfo->channels == 1) + *format = AL_FORMAT_MONO16; + else { + if (vorbisInfo->channels == 2) + *format = AL_FORMAT_STEREO16; + else { + errno = EILSEQ; + err_ret("(%s) ERROR - wrong OGG header [channel %d]", prog, vorbisInfo->channels); ov_clear(&oggStream); return -1; } - - /*load OGG header and determine the decoded data size*/ - vorbisInfo = ov_info(&oggStream, -1); - pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels; - -#ifdef DEBUG - vorbisComment = ov_comment(&oggStream, -1); - fprintf(stderr, "Version: %d\n", vorbisInfo->version); - fprintf(stderr, "Channels: %d\n", vorbisInfo->channels); - fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate); - fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper); - fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal); - fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower); - fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window); - fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor); - fprintf(stderr, "PCM data size: %lld\n", pcm_length); - fprintf(stderr, "# comment: %d\n", vorbisComment->comments); - for (i = 0; i < vorbisComment->comments; i++) - fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]); + } + + size = 0; +#ifdef __LITTLE_ENDIAN__ + endianness = 0; +#elif __BIG_ENDIAN__ + endianness = 1; #endif - - /*allocates enough room for the decoded data*/ - *data = (char*) Malloc (sizeof(char) * pcm_length); - - /*there *should* not be ogg at 8 bits*/ - if (vorbisInfo->channels == 1) - *format = AL_FORMAT_MONO16; - else { - if (vorbisInfo->channels == 2) - *format = AL_FORMAT_STEREO16; + while (size < pcm_length) { + /*ov_read decodes the ogg stream and storse the pcm in data*/ + result = ov_read (&oggStream, *data + size, pcm_length - size, endianness, 2, 1, §ion); + if (result > 0) { + size += result; + } else { + if (result == 0) + break; else { errno = EILSEQ; - err_ret("(%s) ERROR - wrong OGG header [channel %d]", prog, vorbisInfo->channels); + err_ret("(%s) ERROR - End of file from OGG stream", prog); ov_clear(&oggStream); return -1; } } - - size = 0; -#ifdef __LITTLE_ENDIAN__ - endianness = 0; -#elif __BIG_ENDIAN__ - endianness = 1; -#endif - while (size < pcm_length) { - /*ov_read decodes the ogg stream and storse the pcm in data*/ - result = ov_read (&oggStream, *data + size, pcm_length - size, endianness, 2, 1, §ion); - if (result > 0) { - size += result; - } else { - if (result == 0) - break; - else { - errno = EILSEQ; - err_ret("(%s) ERROR - End of file from OGG stream", prog); - ov_clear(&oggStream); - return -1; - } - } - } - - /*set the last fields*/ - *bitsize = size; - *freq = vorbisInfo->rate; - - /*cleaning time (ov_clear also closes file handler)*/ - ov_clear(&oggStream); + } + + /*set the last fields*/ + *bitsize = size; + *freq = vorbisInfo->rate; - return 0; - } - -#ifdef __CPLUSPLUS + /*cleaning time (ov_clear also closes file handler)*/ + ov_clear(&oggStream); + + return 0; } -#endif diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/loaders.h --- a/misc/openalbridge/loaders.h Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/loaders.h Wed Apr 21 10:22:06 2010 +0000 @@ -21,16 +21,7 @@ #include "globals.h" - -#ifdef __CPLUSPLUS -extern "C" { -#endif - - int load_wavpcm (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq); - int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq); - -#ifdef __CPLUSPLUS -} -#endif +int load_wavpcm (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq); +int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq); #endif /*_OALB_LOADERS_H*/ diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/openalbridge.c --- a/misc/openalbridge/openalbridge.c Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/openalbridge.c Wed Apr 21 10:22:06 2010 +0000 @@ -1,20 +1,20 @@ /* - * OpenAL Bridge - a simple portable library for OpenAL interface - * Copyright (c) 2009 Vittorio Giovara - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; version 2 of the License - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ +* OpenAL Bridge - a simple portable library for OpenAL interface +* Copyright (c) 2009 Vittorio Giovara +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published by +* the Free Software Foundation; version 2 of the License +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA +*/ #include "openalbridge.h" #include "globals.h" @@ -22,353 +22,341 @@ #include "alc.h" #include "loaders.h" -#ifdef __CPLUSPLUS -extern "C" { -#endif - - /*Sources are points emitting sound*/ - ALuint *Sources; - /*Buffers hold sound data*/ - ALuint *Buffers; - /*index for Sources and Buffers*/ - ALuint globalindex, globalsize, increment; - - ALboolean openalReady = AL_FALSE; - ALfloat old_gain; - ALboolean openal_init(ALboolean usehardware, int memorysize) { - /*Initialize an OpenAL contex and allocate memory space for data and buffers*/ - ALCcontext *context; - ALCdevice *device; - - prog = "OpenAL subsystem"; +/*Sources are points emitting sound*/ +ALuint *Sources; +/*Buffers hold sound data*/ +ALuint *Buffers; +/*index for Sources and Buffers*/ +ALuint globalindex, globalsize, increment; + +ALboolean openalReady = AL_FALSE; +ALfloat old_gain; - // set the memory dimentsion and the increment width when reallocating - if (memorysize <= 0) - globalsize = 50; - else - globalsize = memorysize; - increment = globalsize; - - // reuse old context but keep the new value for increment - if (openalReady == AL_TRUE) { - err_msg("(%s) WARN - already initialized", prog); - return AL_FALSE; - } - - if (usehardware == AL_TRUE) - device = alcOpenDevice(NULL); - else - device = alcOpenDevice("Generic Software"); - - err_msg("(%s) INFO - Output device: %s", prog, alcGetString(device, ALC_DEVICE_SPECIFIER)); - - if (device == NULL) { - errno = ENODEV; - err_ret("(%s) WARN - failed to open sound device", prog); - return AL_FALSE; - } - - context = alcCreateContext(device, NULL); - alcMakeContextCurrent(context); - alcProcessContext(context); - - if (AlGetError("(%s) WARN - Failed to create a new contex") != AL_TRUE) - return AL_FALSE; - - // allocate memory space for buffers and sources - Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); - Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); - - // set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation - // Position, Velocity and Orientation of the listener - ALfloat ListenerPos[] = {0.0, 0.0, 0.0}; - ALfloat ListenerVel[] = {0.0, 0.0, 0.0}; - ALfloat ListenerOri[] = {0.0, 0.0, -1.0, 0.0, 1.0, 0.0}; +int openal_init(int memorysize) { + /*Initialize an OpenAL contex and allocate memory space for data and buffers*/ + ALCcontext *context; + ALCdevice *device; - alListenerf (AL_GAIN, 1.0f ); - alListenerfv(AL_POSITION, ListenerPos); - alListenerfv(AL_VELOCITY, ListenerVel); - alListenerfv(AL_ORIENTATION, ListenerOri); - - if (AlGetError("(%s) WARN - Failed to set Listener properties") != AL_TRUE) - return AL_FALSE; - - openalReady = AL_TRUE; - - alGetError(); // clear any AL errors beforehand - return AL_TRUE; + // set the memory dimentsion and the increment width when reallocating + if (memorysize <= 0) + globalsize = 50; + else + globalsize = memorysize; + increment = globalsize; + + // reuse old context but keep the new value for increment + if (openalReady == AL_TRUE) { + err_msg("(%s) WARN - already initialized", prog); + return 0; } - void openal_close (void) { - /*Stop all sounds, deallocate all memory and close OpenAL */ - ALCcontext *context; - ALCdevice *device; - - if (openalReady == AL_FALSE) { - errno = EPERM; - err_ret("(%s) WARN - OpenAL not initialized", prog); - return; + // open hardware device if present + device = alcOpenDevice(NULL); + + if (device == NULL) { + errno = ENODEV; + err_ret("(%s) WARN - failed to open sound device, using software renderer", prog); + device = alcOpenDevice("Generic Software"); + if (device == NULL) { + err_ret("(%s) ERROR - failed to open sound software device, sound will be disabled", prog); + return -1; } - - alSourceStopv (globalsize, Sources); - alDeleteSources (globalsize, Sources); - alDeleteBuffers (globalsize, Buffers); - - free(Sources); - free(Buffers); - - context = alcGetCurrentContext(); - device = alcGetContextsDevice(context); - + } + + err_msg("(%s) INFO - Output device: %s", prog, alcGetString(device, ALC_DEVICE_SPECIFIER)); + + context = alcCreateContext(device, NULL); + alcMakeContextCurrent(context); + alcProcessContext(context); + + if (AL_NO_ERROR != alGetError()) { + err_msg("(%s) ERROR - Failed to create a new contex",prog); alcMakeContextCurrent(NULL); alcDestroyContext(context); alcCloseDevice(device); - - openalReady = AL_FALSE; - - err_msg("(%s) INFO - closed", prog); - - return; - } - - ALboolean openal_ready(void) { - return openalReady; + return -2; } - - - void helper_realloc (void) { - /*expands allocated memory when loading more sound files than expected*/ - int oldsize = globalsize; - globalsize += increment; - - err_msg("(%s) INFO - Realloc in process from %d to %d\n", prog, oldsize, globalsize); - - Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize); - Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize); - + + // allocate memory space for buffers and sources + Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); + Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); + + // set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation + // Position, Velocity and Orientation of the listener + ALfloat ListenerPos[] = {0.0, 0.0, 0.0}; + ALfloat ListenerVel[] = {0.0, 0.0, 0.0}; + ALfloat ListenerOri[] = {0.0, 0.0, -1.0, 0.0, 1.0, 0.0}; + + alListenerf (AL_GAIN, 1.0f ); + alListenerfv(AL_POSITION, ListenerPos); + alListenerfv(AL_VELOCITY, ListenerVel); + alListenerfv(AL_ORIENTATION, ListenerOri); + + if (AL_NO_ERROR != alGetError()) { + err_msg("(%s) ERROR - Failed to set Listener properties",prog); + return -3; + } + openalReady = AL_TRUE; + + alGetError(); // clear any AL errors beforehand + return AL_TRUE; +} + +void openal_close (void) { + /*Stop all sounds, deallocate all memory and close OpenAL */ + ALCcontext *context; + ALCdevice *device; + + if (openalReady == AL_FALSE) { + errno = EPERM; + err_ret("(%s) WARN - OpenAL not initialized", prog); return; } - - - int openal_loadfile (const char *filename){ - /*Open a file, load into memory and allocate the Source buffer for playing*/ - ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/ - ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/ - ALenum format; - ALsizei bitsize, freq; - char *data; - uint32_t fileformat; - ALenum error; - FILE *fp; - - if (openalReady == AL_FALSE) { - err_msg("(%s) WARN - not initialized", prog); - return -1; - } - - /*when the buffers are all used, we can expand memory to accept new files*/ - if (globalindex == globalsize) - helper_realloc(); - - /*detect the file format, as written in the first 4 bytes of the header*/ - fp = Fopen (filename, "rb"); - - if (fp == NULL) - return -2; - - error = fread (&fileformat, sizeof(uint32_t), 1, fp); - fclose (fp); - - if (error < 0) { - err_msg("(%s) ERROR - File %s is too short", prog, filename); - return -3; - } - - /*prepare the buffer to receive data*/ - alGenBuffers(1, &Buffers[globalindex]); - - if (AlGetError("(%s) ERROR - Failed to allocate memory for buffers") != AL_TRUE) - return -4; - - /*prepare the source to emit sound*/ - alGenSources(1, &Sources[globalindex]); - - if (AlGetError("(%s) ERROR - Failed to allocate memory for sources") != AL_TRUE) - return -5; - - - switch (ENDIAN_BIG_32(fileformat)) { - case OGG_FILE_FORMAT: - error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); - break; - case WAV_FILE_FORMAT: - error = load_wavpcm (filename, &format, &data, &bitsize, &freq); - break; - default: - err_msg ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(fileformat)); - return -6; - break; - } - - - /*copy pcm data in one buffer*/ - alBufferData(Buffers[globalindex], format, data, bitsize, freq); - free(data); /*deallocate data to save memory*/ - - if (AlGetError("(%s) ERROR - Failed to write data to buffers") != AL_TRUE) + + alSourceStopv (globalsize, Sources); + alDeleteSources (globalsize, Sources); + alDeleteBuffers (globalsize, Buffers); + + free(Sources); + free(Buffers); + + context = alcGetCurrentContext(); + device = alcGetContextsDevice(context); + + alcMakeContextCurrent(NULL); + alcDestroyContext(context); + alcCloseDevice(device); + + openalReady = AL_FALSE; + + err_msg("(%s) INFO - closed", prog); + + return; +} + +ALboolean openal_ready(void) { + return openalReady; +} + + +void helper_realloc (void) { + /*expands allocated memory when loading more sound files than expected*/ + int oldsize = globalsize; + globalsize += increment; + + err_msg("(%s) INFO - Realloc in process from %d to %d\n", prog, oldsize, globalsize); + + Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize); + Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize); + + return; +} + + +int openal_loadfile (const char *filename){ + /*Open a file, load into memory and allocate the Source buffer for playing*/ + ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/ + ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/ + ALenum format; + ALsizei bitsize, freq; + char *data; + uint32_t fileformat; + ALenum error; + FILE *fp; + + if (openalReady == AL_FALSE) { + err_msg("(%s) WARN - not initialized", prog); + return -1; + } + + /*when the buffers are all used, we can expand memory to accept new files*/ + if (globalindex == globalsize) + helper_realloc(); + + /*detect the file format, as written in the first 4 bytes of the header*/ + fp = Fopen (filename, "rb"); + + if (fp == NULL) + return -2; + + error = fread (&fileformat, sizeof(uint32_t), 1, fp); + fclose (fp); + + if (error < 0) { + err_msg("(%s) ERROR - File %s is too short", prog, filename); + return -3; + } + + /*prepare the buffer to receive data*/ + alGenBuffers(1, &Buffers[globalindex]); + + if (AL_NO_ERROR != alGetError()) { + err_msg("(%s) ERROR - Failed to allocate memory for buffers",prog); + return -4; + } + + /*prepare the source to emit sound*/ + alGenSources(1, &Sources[globalindex]); + + if (AL_NO_ERROR != alGetError()) { + err_msg("(%s) ERROR - Failed to allocate memory for sources",prog); + return -5; + } + + switch (ENDIAN_BIG_32(fileformat)) { + case OGG_FILE_FORMAT: + error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); + break; + case WAV_FILE_FORMAT: + error = load_wavpcm (filename, &format, &data, &bitsize, &freq); + break; + default: + err_msg ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(fileformat)); return -6; - - /*set source properties that it will use when it's in playback*/ - alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); - alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); - alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); - alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); - alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); - alSourcei (Sources[globalindex], AL_LOOPING, 0 ); - - if (AlGetError("(%s) ERROR - Failed to set Source properties") != AL_TRUE) - return -7; - - alGetError(); /* clear any AL errors beforehand */ - - /*returns the index of the source you just loaded, increments it and exits*/ - return globalindex++; - } - - - void openal_playsound (uint32_t index) { - openal_playsound_loop (index, 0); - } - - - void openal_pausesound (uint32_t index) { - if (openalReady == AL_TRUE && index < globalsize) - alSourcePause(Sources[index]); - } - - - void openal_stopsound (uint32_t index) { - openal_stopsound_free(index, 0); - } - - - void openal_freesound (uint32_t index){ - if (openalReady == AL_TRUE && index < globalsize) - alSourceStop(Sources[index]); - // STUB + break; } - void openal_playsound_loop (unsigned int index, char loops) { - if (openalReady == AL_TRUE && index < globalsize) { - alSourcePlay(Sources[index]); - if (loops != 0) - openal_toggleloop(index); - } + //copy pcm data in one buffer and free it + alBufferData(Buffers[globalindex], format, data, bitsize, freq); + free(data); + + if (AL_NO_ERROR != alGetError()) { + err_msg("(%s) ERROR - Failed to write data to buffers",prog); + return -6; } - void openal_stopsound_free (unsigned int index, char freesource) { - if (openalReady == AL_TRUE && index < globalsize) { - alSourceStop(Sources[index]); - if (freesource != 0) - openal_freesound(index); - } + /*set source properties that it will use when it's in playback*/ + alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); + alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); + alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); + alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); + alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); + alSourcei (Sources[globalindex], AL_LOOPING, 0 ); + + if (AL_NO_ERROR != alGetError()) { + err_msg("(%s) ERROR - Failed to set Source properties",prog); + return -7; } - void openal_toggleloop (uint32_t index) { - ALint loop; - - if (openalReady == AL_TRUE && index < globalsize) { - alGetSourcei (Sources[index], AL_LOOPING, &loop); - alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); - } + alGetError(); /* clear any AL errors beforehand */ + + /*returns the index of the source you just loaded, increments it and exits*/ + return globalindex++; +} + + +void openal_playsound (uint32_t index) { + openal_playsound_loop (index, 0); +} + + +void openal_pausesound (uint32_t index) { + if (openalReady == AL_TRUE && index < globalsize) + alSourcePause(Sources[index]); +} + +void openal_stopsound (uint32_t index) { + openal_stopsound_free(index, 0); +} + + +void openal_freesound (uint32_t index){ + if (openalReady == AL_TRUE && index < globalsize) + alSourceStop(Sources[index]); + // STUB +} + + +void openal_playsound_loop (unsigned int index, char loops) { +if (openalReady == AL_TRUE && index < globalsize) { + alSourcePlay(Sources[index]); + if (loops != 0) + openal_toggleloop(index); } - +} - void openal_setvolume (uint32_t index, float gain) { - if (openalReady == AL_TRUE && index < globalsize) - alSourcef (Sources[index], AL_GAIN, gain); - } - - - void openal_setglobalvolume (float gain) { - if (openalReady == AL_TRUE) - alListenerf (AL_GAIN, gain); +void openal_stopsound_free (unsigned int index, char freesource) { + if (openalReady == AL_TRUE && index < globalsize) { + alSourceStop(Sources[index]); + if (freesource != 0) + openal_freesound(index); } - - void openal_togglemute () { - ALfloat gain; +} + +void openal_toggleloop (uint32_t index) { + ALint loop; + + if (openalReady == AL_TRUE && index < globalsize) { + alGetSourcei (Sources[index], AL_LOOPING, &loop); + alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); + } + +} + + +void openal_setvolume (uint32_t index, float gain) { + if (openalReady == AL_TRUE && index < globalsize) + alSourcef (Sources[index], AL_GAIN, gain); +} + - if (openalReady == AL_TRUE) { - alGetListenerf (AL_GAIN, &gain); - if (gain > 0) { - old_gain = gain; - gain = 0; - } else - gain = old_gain; - - alListenerf (AL_GAIN, gain); - } - } - - - void openal_fade (uint32_t index, uint16_t quantity, char direction) { - /*Fade in or out by calling a helper thread*/ +void openal_setglobalvolume (float gain) { + if (openalReady == AL_TRUE) + alListenerf (AL_GAIN, gain); +} + +void openal_togglemute () { + ALfloat gain; + + if (openalReady == AL_TRUE) { + alGetListenerf (AL_GAIN, &gain); + if (gain > 0) { + old_gain = gain; + gain = 0; + } else + gain = old_gain; + + alListenerf (AL_GAIN, gain); +} +} + +// Fade in or out by calling a helper thread +void openal_fade (uint32_t index, uint16_t quantity, al_fade_t direction) { #ifndef _WIN32 - pthread_t thread; + pthread_t thread; #else - HANDLE Thread; - DWORD threadID; + HANDLE Thread; #endif - fade_t *fade; - - if (openalReady == AL_TRUE && index < globalsize) { - - fade = (fade_t*) Malloc(sizeof(fade_t)); - fade->index = index; - fade->quantity = quantity; - - if (direction > 0) { + fade_t *fade; + + if (openalReady == AL_TRUE && index < globalsize) { + fade = (fade_t*) Malloc(sizeof(fade_t)); + fade->index = index; + fade->quantity = quantity; + fade->type = direction; + #ifndef _WIN32 - pthread_create(&thread, NULL, helper_fadein, (void*) fade); + pthread_create(&thread, NULL, (void *)helper_fade, (void *)fade); + pthread_detach(thread); #else - Thread = _beginthread(&helper_fadein, 0, (void*) fade); -#endif - } else { -#ifndef _WIN32 - pthread_create(&thread, NULL, helper_fadeout, (void*) fade); -#else - Thread = _beginthread(&helper_fadeout, 0, (void*) fade); -#endif - } - -#ifndef _WIN32 - pthread_detach(thread); + Thread = (HANDLE) _beginthread((void *)helper_fade, 0, (void *)fade); #endif - } } - - - void openal_fadeout (uint32_t index, uint16_t quantity) { - openal_fade(index, quantity, AL_FADE_OUT); - } - - - void openal_fadein (uint32_t index, uint16_t quantity) { - openal_fade(index, quantity, AL_FADE_IN); - } - - - void openal_setposition (uint32_t index, float x, float y, float z) { - if (openalReady == AL_TRUE && index < globalsize) - alSource3f(Sources[index], AL_POSITION, x, y, z);; - } - - +} + +void openal_fadein (uint32_t index, uint16_t quantity) { + openal_fade(index, quantity, AL_FADE_IN); +} - -#ifdef __CPLUSPLUS +void openal_fadeout (uint32_t index, uint16_t quantity) { + openal_fade(index, quantity, AL_FADE_OUT); } -#endif + + +void openal_setposition (uint32_t index, float x, float y, float z) { + if (openalReady == AL_TRUE && index < globalsize) + alSource3f(Sources[index], AL_POSITION, x, y, z);; +} diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/openalbridge.def --- a/misc/openalbridge/openalbridge.def Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/openalbridge.def Wed Apr 21 10:22:06 2010 +0000 @@ -4,14 +4,17 @@ openal_close openal_ready openal_loadfile + openal_playsound + openal_pausesound + openal_stopsound + openal_playsound_loop + openal_stopsound_free + openal_freesound openal_toggleloop openal_setvolume openal_setglobalvolume openal_togglemute - openal_fadeout + openal_fade openal_fadein - openal_fade - openal_playsound - openal_pausesound - openal_stopsound + openal_fadeout diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/openalbridge.h --- a/misc/openalbridge/openalbridge.h Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/openalbridge.h Wed Apr 21 10:22:06 2010 +0000 @@ -19,13 +19,14 @@ #ifndef _OALB_INTERFACE_H #define _OALB_INTERFACE_H +#include "openalbridge_t.h" #ifdef __CPLUSPLUS extern "C" { -#endif - +#endif + // init audio context and allocate memory - char openal_init (char usehardware, int memorysize); + int openal_init (int memorysize); // close audio subsytem and free memory void openal_close (void); @@ -37,7 +38,7 @@ int openal_loadfile (const char *filename); // play, pause, stop a single sound source - void openal_playsound (unsigned int index); + void openal_playsound (unsigned int index); void openal_pausesound (unsigned int index); void openal_stopsound (unsigned int index); @@ -59,16 +60,14 @@ // set volume for all sounds (gain interval is [0-1]) void openal_setglobalvolume (float gain); - // mute or unmute all sounds + // mute or unmute all sounds void openal_togglemute (void); - // fade effect, - void openal_fadeout (unsigned int index, unsigned short int quantity); + // fade effect, + void openal_fade (unsigned int index, unsigned short int quantity, al_fade_t direction); void openal_fadein (unsigned int index, unsigned short int quantity); - void openal_fade (unsigned int index, unsigned short int quantity, char direction); + void openal_fadeout (unsigned int index, unsigned short int quantity); -#define AL_FADE_IN 1 -#define AL_FADE_OUT -1 #ifdef __CPLUSPLUS } diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/openalbridge_t.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/misc/openalbridge/openalbridge_t.h Wed Apr 21 10:22:06 2010 +0000 @@ -0,0 +1,61 @@ +/* + * OpenAL Bridge - a simple portable library for OpenAL interface + * Copyright (c) 2009 Vittorio Giovara + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include + +#ifndef _OALB_INTERFACE_TYPES_H +#define _OALB_INTERFACE_TYPES_H + +enum al_fade_enum {AL_FADE_IN, AL_FADE_OUT}; +typedef enum al_fade_enum al_fade_t; + +//data type for passing data between threads +#pragma pack(1) +typedef struct _fade_t { + uint32_t index; + uint16_t quantity; + al_fade_t type; +} fade_t; +#pragma pack() + + +//data type for WAV header +#pragma pack(1) +typedef struct _WAV_header_t { + uint32_t ChunkID; + uint32_t ChunkSize; + uint32_t Format; + uint32_t Subchunk1ID; + uint32_t Subchunk1Size; + uint16_t AudioFormat; + uint16_t NumChannels; + uint32_t SampleRate; + uint32_t ByteRate; + uint16_t BlockAlign; + uint16_t BitsPerSample; + uint32_t Subchunk2ID; + uint32_t Subchunk2Size; +} WAV_header_t; +#pragma pack() + + +#ifdef __CPLUSPLUS +} +#endif + +#endif /*_OALB_INTERFACE_TYPES_H*/ diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/wrappers.c --- a/misc/openalbridge/wrappers.c Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/wrappers.c Wed Apr 21 10:22:06 2010 +0000 @@ -1,106 +1,83 @@ /* - * OpenAL Bridge - a simple portable library for OpenAL interface - * Copyright (c) 2009 Vittorio Giovara - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; version 2 of the License - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ +* OpenAL Bridge - a simple portable library for OpenAL interface +* Copyright (c) 2009 Vittorio Giovara +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published by +* the Free Software Foundation; version 2 of the License +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA +*/ #include "wrappers.h" +#include "openalbridge_t.h" + +extern ALint *Sources; + +void *Malloc (size_t nbytes) { + void *aptr; + + if ((aptr = malloc(nbytes)) == NULL) + err_dump("(%s) FATAL - not enough memory"); + + return aptr; +} -#ifdef __CPLUSPLUS -extern "C" { -#endif - - extern ALint *Sources; - - void *Malloc (size_t nbytes) { - void *aptr; - - if ((aptr = malloc(nbytes)) == NULL) - err_dump("(%s) FATAL - not enough memory"); - - return aptr; - } - - - void *Realloc (void *aptr, size_t nbytes) { - aptr = realloc(aptr, nbytes); - - if (aptr == NULL) - err_dump("(%s) FATAL - not enough memory"); - - return aptr; - } - - - FILE *Fopen (const char *fname, char *mode) { - FILE *fp; - - fp = fopen(fname,mode); - if (fp == NULL) - err_ret("(%s) ERROR - can't open file %s in mode '%s'", prog, fname, mode); - - return fp; - } - - /*TODO make a proper error reporting routine*/ - ALint AlGetError (const char *str) { - ALenum error; - - error = alGetError(); - if (error != AL_NO_ERROR) { - err_msg(str, prog); - return error; - } else - return AL_TRUE; - } - - ALint AlGetError2 (const char *str, int num) { - ALenum error; - - error = alGetError(); - if (error != AL_NO_ERROR) { - err_msg(str, prog, num); - return error; - } else - return AL_TRUE; - } - - void *helper_fadein(void *tmp) { - ALfloat gain; - ALfloat target_gain; - fade_t *fade; - uint32_t index; - uint16_t quantity; - - fade = tmp; - index = fade->index; - quantity = fade->quantity; - free (fade); - +void *Realloc (void *aptr, size_t nbytes) { + aptr = realloc(aptr, nbytes); + + if (aptr == NULL) + err_dump("(%s) FATAL - not enough memory"); + + return aptr; +} + + +FILE *Fopen (const char *fname, char *mode) { + FILE *fp; + + fp = fopen(fname,mode); + if (fp == NULL) + err_ret("(%s) ERROR - can't open file %s in mode '%s'", prog, fname, mode); + + return fp; +} + + +void helper_fade(void *tmp) { + ALfloat gain; + ALfloat target_gain; + fade_t *fade; + uint32_t index; + uint16_t quantity; + al_fade_t type; + + fade = tmp; + index = fade->index; + quantity = fade->quantity; + type = fade->type; + free (fade); + + if (type == AL_FADE_IN) { #ifdef DEBUG err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity); #endif - - /*save the volume desired after the fade*/ + + // save the volume desired after the fade alGetSourcef(Sources[index], AL_GAIN, &target_gain); if (target_gain > 1.0f || target_gain <= 0.0f) target_gain = 1.0f; - + alSourcePlay(Sources[index]); - + for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) { #ifdef TRACE err_msg("(%s) DEBUG - Fade-in set gain to %f", gain); @@ -108,58 +85,32 @@ alSourcef(Sources[index], AL_GAIN, gain); usleep(10000); } - - AlGetError("(%s) WARN - Failed to set fade-in volume level"); - -#ifndef _WIN32 - pthread_exit(NULL); -#else - _endthread(); -#endif - return 0; - } - - void *helper_fadeout(void *tmp) { - ALfloat gain; - ALfloat old_gain; - fade_t *fade; - uint32_t index; - uint16_t quantity; - - fade = tmp; - index = fade->index; - quantity = fade->quantity; - free(fade); - -#ifdef DEBUG - err_msg("(%s) INFO - Fade-out in progress [index %d quantity %d]", prog, index, quantity); -#endif - - alGetSourcef(Sources[index], AL_GAIN, &old_gain); - - for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) { + } else { + alGetSourcef(Sources[index], AL_GAIN, &target_gain); + + for (gain = target_gain; gain >= 0.00f; gain -= (float) quantity/10000) { #ifdef TRACE err_msg("(%s) DEBUG - Fade-out set gain to %f", gain); #endif alSourcef(Sources[index], AL_GAIN, gain); usleep(10000); } - - AlGetError("(%s) WARN - Failed to set fade-out volume level"); - - /*stop that sound and reset its volume*/ + + if (AL_NO_ERROR != alGetError()) + err_msg("(%s) WARN - Failed to set fade-out effect",prog); + + // stop that sound and reset its volume alSourceStop (Sources[index]); - alSourcef (Sources[index], AL_GAIN, old_gain); - + alSourcef (Sources[index], AL_GAIN, target_gain); + } + + if (AL_NO_ERROR != alGetError()) + err_msg("(%s) WARN - Failed to set fade effect",prog); + #ifndef _WIN32 - pthread_exit(NULL); + pthread_exit(NULL); #else - _endthread(); + _endthread(); #endif - return 0; - } - - -#ifdef __CPLUSPLUS } -#endif + diff -r cfc6cd502f85 -r 8d3b4d19ce27 misc/openalbridge/wrappers.h --- a/misc/openalbridge/wrappers.h Wed Apr 21 01:57:23 2010 +0000 +++ b/misc/openalbridge/wrappers.h Wed Apr 21 10:22:06 2010 +0000 @@ -21,21 +21,9 @@ #include "globals.h" - -#ifdef __CPLUSPLUS -extern "C" { -#endif - - void *Malloc (size_t nbytes); - void *Realloc (void *aptr, size_t nbytes); - FILE *Fopen (const char *fname, char *mode); - ALint AlGetError (const char *str); - ALint AlGetError2 (const char *str, int num); - void *helper_fadein (void *tmp); - void *helper_fadeout (void *tmp); - -#ifdef __CPLUSPLUS -} -#endif +void *Malloc (size_t nbytes); +void *Realloc (void *aptr, size_t nbytes); +FILE *Fopen (const char *fname, char *mode); +void helper_fade (void *tmp); #endif /*_OALB_WRAPPERS_H*/