From bffcaf53098adaba13461e58c11d161ad047abec Mon Sep 17 00:00:00 2001 From: taw27 Date: Sun, 29 Jun 2008 15:50:29 +0000 Subject: Seamless audio looping Audio channel selection fix Audio debugging git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@112 84d2e878-0bd5-11dd-ad15-13eda11d74c5 --- src/audio.c | 43 ++++++++++++++++++++++--------------------- src/types.h | 2 ++ 2 files changed, 24 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 8bb95fa..c3a0492 100644 --- a/src/audio.c +++ b/src/audio.c @@ -22,7 +22,6 @@ static void audio_mix(void *data, Uint8 *stream8, int len) { AudioContext *a = data; int i, j; - Uint32 amount; Sint16 *stream = (Sint16 *)stream8; len /= 2; /* Samples */ @@ -38,27 +37,25 @@ static void audio_mix(void *data, Uint8 *stream8, int len) { /* Playing? */ if ( !a->sounds[i].inuse ) continue; - /* Calculate how many samples to mix */ - amount = a->sounds[i].dlen - a->sounds[i].dpos; + for ( j=0; j len ) { - /* The sound remaining in this channel more than fills the buffer */ - amount = len; - } - - for ( j=0; jsounds[i].data[a->sounds[i].dpos + j]; + Sint16 samp; + + samp = a->sounds[i].data[a->sounds[i].dpos++]; stream[j] += samp * a->sounds[i].volume; - } - - a->sounds[i].dpos += amount; - if ( a->sounds[i].dpos == a->sounds[i].dlen ) { - if ( a->sounds[i].repeat ) { - a->sounds[i].dpos = 0; - } else { - a->sounds[i].inuse = 0; - free(a->sounds[i].data); + + if ( a->sounds[i].dpos == a->sounds[i].dlen ) { + if ( a->sounds[i].repeat ) { + a->sounds[i].dpos = 0; + if ( a->debug ) printf("AU: Looping sound %i at buffer offset %i/%i\n", i, j, len); + } else { + a->sounds[i].inuse = 0; + free(a->sounds[i].data); + if ( a->debug ) printf("AU: End of sound %i\n", i); + break; + } } + } } @@ -74,10 +71,11 @@ void audio_play(AudioContext *a, char *file, float volume, int repeat) { SDL_AudioCVT cvt; char filename[128]; - /* Look for an empty (or finished) sound slot */ + /* Look for an empty sound slot */ for ( idx=0; idxsounds[idx].dpos == a->sounds[idx].dlen ) break; + if ( !a->sounds[idx].inuse ) break; } + if ( a->debug ) printf("AU: Selected channel %i for sound '%s'\n", idx, file); if ( idx == AUDIO_MAX_SOUNDS ) { fprintf(stderr, "Not enough audio channels to play '%s'\n", file); return; @@ -116,9 +114,12 @@ AudioContext *audio_setup() { SDL_AudioSpec fmt; int i; + /* Create audio context */ a = malloc(sizeof(AudioContext)); if ( a == NULL ) return NULL; + /* Initialise audio context */ + a->debug = 0; for ( i=0; isounds[i].inuse = 0; } diff --git a/src/types.h b/src/types.h index 8dcdc44..b163075 100644 --- a/src/types.h +++ b/src/types.h @@ -155,6 +155,8 @@ typedef struct { float volume; } sounds[AUDIO_MAX_SOUNDS]; + int debug; + } AudioContext; typedef struct { -- cgit v1.2.3