aboutsummaryrefslogtreecommitdiff
path: root/src/audio.c
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-06-29 15:50:29 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-06-29 15:50:29 +0000
commitbffcaf53098adaba13461e58c11d161ad047abec (patch)
treebc970ce1f6a7b61a66f0f88747469ea8be65a964 /src/audio.c
parent8014bafd7352ffc856747de4bc2178e3c3fbcf12 (diff)
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
Diffstat (limited to 'src/audio.c')
-rw-r--r--src/audio.c43
1 files changed, 22 insertions, 21 deletions
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; j++ ) {
- if ( amount > len ) {
- /* The sound remaining in this channel more than fills the buffer */
- amount = len;
- }
-
- for ( j=0; j<amount; j++ ) {
- Sint16 samp = a->sounds[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; idx<AUDIO_MAX_SOUNDS; idx++ ) {
- if ( a->sounds[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; i<AUDIO_MAX_SOUNDS; i++ ) {
a->sounds[i].inuse = 0;
}