diff options
Diffstat (limited to 'src/audio.c')
-rw-r--r-- | src/audio.c | 55 |
1 files changed, 42 insertions, 13 deletions
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; |