diff options
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/audio.c | 55 | ||||
-rw-r--r-- | src/physics.c | 2 | ||||
-rw-r--r-- | src/types.h | 8 |
4 files changed, 52 insertions, 15 deletions
diff --git a/configure.ac b/configure.ac index 01b7e6d..b592d4f 100644 --- a/configure.ac +++ b/configure.ac @@ -63,7 +63,7 @@ else AC_MSG_ERROR([*** vorbisfile not found. Do you have 'libvorbis-dev' or similar installed?]) fi -CFLAGS="$CFLAGS $LIBSDL_CFLAGS $OPENGL_CFLAGS $LIBPNG_CFLAGS $VORBISFILE_CFLAGS" +CFLAGS="$CFLAGS $LIBSDL_CFLAGS $OPENGL_CFLAGS $LIBPNG_CFLAGS $VORBISFILE_CFLAGS -pthread" LIBS="$LIBS $LIBSDL_LIBS $OPENGL_LIBS $GLU_LIBS $GLEW_LIBS $LIBPNG_LIBS $VORBISFILE_LIBS" AC_OUTPUT(Makefile src/Makefile data/Makefile) diff --git a/src/audio.c b/src/audio.c index dc89927..82d70f9 100644 --- a/src/audio.c +++ b/src/audio.c @@ -20,6 +20,7 @@ #include <SDL_audio.h> #include <vorbis/vorbisfile.h> #include <vorbis/codec.h> +#include <pthread.h> #include "types.h" @@ -41,7 +42,7 @@ static void audio_mix(void *data, Uint8 *stream8, int len) { for ( i=0; i<AUDIO_MAX_SOUNDS; i++ ) { /* Playing? */ - if ( !a->sounds[i].inuse ) continue; + if ( !a->sounds[i].playing ) continue; for ( j=0; j<len; j++ ) { @@ -65,6 +66,7 @@ static void audio_mix(void *data, Uint8 *stream8, int len) { if ( a->debug ) printf("AU: Looping sound %i at buffer offset %i/%i\n", i, j, len); } else { a->sounds[i].inuse = 0; + a->sounds[i].playing = 0; free(a->sounds[i].data); if ( a->debug ) printf("AU: End of sound %i\n", i); break; @@ -105,11 +107,12 @@ static void audio_play_wav(AudioContext *a, char *filename, float volume, int re for ( idx=0; idx<AUDIO_MAX_SOUNDS; idx++ ) { if ( !a->sounds[idx].inuse ) break; } - if ( a->debug ) printf("AU: Selected channel %i for sound '%s'\n", idx, filename); if ( idx == AUDIO_MAX_SOUNDS ) { fprintf(stderr, "Not enough audio channels to play '%s'\n", filename); return; } + a->sounds[idx].inuse = 1; + if ( a->debug ) printf("AU: Selected channel %i for sound '%s'\n", idx, filename); /* Load the sound file and convert it to 16-bit stereo at 44.1 kHz */ if ( SDL_LoadWAV(filename, &wave, &data, &dlen) == NULL ) { @@ -128,14 +131,18 @@ static void audio_play_wav(AudioContext *a, char *filename, float volume, int re a->sounds[idx].data = (Sint16 *)cvt.buf; a->sounds[idx].dlen = cvt.len_cvt / 2; a->sounds[idx].dpos = 0; - a->sounds[idx].inuse = 1; + a->sounds[idx].playing = 1; a->sounds[idx].repeat = repeat; a->sounds[idx].volume = volume; SDL_UnlockAudio(); } -static void audio_play_vorbis(AudioContext *a, char *filename, float volume, int repeat) { +static void *audio_play_vorbis(void *add_void) { + + AudioDispatchData *add = add_void; + AudioContext *a = add->audiocontext; + char *filename = add->filename; int idx; char *data; @@ -150,16 +157,17 @@ static void audio_play_vorbis(AudioContext *a, char *filename, float volume, int for ( idx=0; idx<AUDIO_MAX_SOUNDS; idx++ ) { if ( !a->sounds[idx].inuse ) break; } - if ( a->debug ) printf("AU: Selected channel %i for sound '%s'\n", idx, filename); if ( idx == AUDIO_MAX_SOUNDS ) { fprintf(stderr, "Not enough audio channels to play '%s'\n", filename); - return; + return NULL; } + a->sounds[idx].inuse = 1; + if ( a->debug ) printf("AU: Selected channel %i for sound '%s'\n", idx, filename); err = ov_fopen(filename, &vf); if ( err != 0 ) { fprintf(stderr, "Couldn't open Vorbis file '%s' (code %i,%i)\n", filename, err, errno); - return; + return NULL; } len = ov_pcm_total(&vf, -1); /* Length in samples 'per channel' */ @@ -197,11 +205,16 @@ static void audio_play_vorbis(AudioContext *a, char *filename, float volume, int a->sounds[idx].data = (Sint16 *)cvt.buf; a->sounds[idx].dlen = cvt.len_cvt / 2; a->sounds[idx].dpos = 0; - a->sounds[idx].inuse = 1; - a->sounds[idx].repeat = repeat; - a->sounds[idx].volume = volume; + a->sounds[idx].playing = 1; + a->sounds[idx].repeat = add->repeat; + a->sounds[idx].volume = add->volume; SDL_UnlockAudio(); + free(add->filename); + free(add); + if ( a->debug ) printf("AU: audio_play_vorbis dispatch thread completed.\n"); + return NULL; + } void audio_play(AudioContext *a, char *name, float volume, int repeat) { @@ -212,8 +225,23 @@ void audio_play(AudioContext *a, char *name, float volume, int repeat) { /* Try to find an Ogg/Vorbis file */ snprintf(filename, 127, "%s%s.ogg", DATADIR"/sound/", name); if ( stat(filename, &statbuf) == 0 ) { - audio_play_vorbis(a, filename, volume, repeat); + + int rval; + AudioDispatchData *add; + pthread_t thread; + + add = malloc(sizeof(AudioDispatchData)); + add->audiocontext = a; + add->filename = strdup(filename); + add->volume = volume; + add->repeat = repeat; + rval = pthread_create(&thread, NULL, audio_play_vorbis, add); + if ( rval != 0 ) { + fprintf(stderr, "Couldn't create audio dispatch thread (%i)\n", rval); + } + goto done; + } /* Try to find a WAV file */ @@ -254,6 +282,7 @@ AudioContext *audio_setup(int debug) { a->paused = 1; for ( i=0; i<AUDIO_MAX_SOUNDS; i++ ) { a->sounds[i].inuse = 0; + a->sounds[i].playing = 0; } /* 16-bit stereo audio at 44.1 kHz */ @@ -271,8 +300,8 @@ AudioContext *audio_setup(int debug) { return NULL; } - audio_play(a, "kraftwerk", 1.0, 1); -// audio_play(a, "moan", 0.4, 1); + audio_play(a, "kraftwerk", 0.5, 1); + audio_play(a, "moan", 0.2, 1); return a; diff --git a/src/physics.c b/src/physics.c index ba45f61..7677a02 100644 --- a/src/physics.c +++ b/src/physics.c @@ -276,7 +276,7 @@ static void physics_process(ModelInstance *obj, Uint32 dt, Game *game) { for ( j=0; j<room->num_objects; j++ ) { if ( physics_check_collide(obj, room->objects[j], dt) ) { /* Should be dt - ttc */ collided = 1; - audio_play(game->audio, "clang", 1.0, 0); + audio_play(game->audio, "clang", 0.7, 0); } } } diff --git a/src/types.h b/src/types.h index d398899..b43015a 100644 --- a/src/types.h +++ b/src/types.h @@ -151,6 +151,7 @@ typedef struct { int dpos; int dlen; int inuse; + int playing; int repeat; float volume; } sounds[AUDIO_MAX_SOUNDS]; @@ -163,6 +164,13 @@ typedef struct { } AudioContext; typedef struct { + AudioContext *audiocontext; + char *filename; + float volume; + int repeat; +} AudioDispatchData; + +typedef struct { int rx; int ry; int rz; |