diff options
Diffstat (limited to 'src/audio.c')
-rw-r--r-- | src/audio.c | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/src/audio.c b/src/audio.c index ed37a89..765ca77 100644 --- a/src/audio.c +++ b/src/audio.c @@ -22,6 +22,7 @@ * */ +/* SDL's headers don't like this. Grr. */ //#ifdef HAVE_CONFIG_H //#include <config.h> //#endif @@ -35,9 +36,12 @@ #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> +#include <assert.h> #include "types.h" +static AudioContext *audio_context = NULL; + static void audio_mix(void *data, Uint8 *stream8, int len) { AudioContext *a = data; @@ -54,14 +58,23 @@ static void audio_mix(void *data, Uint8 *stream8, int len) { if ( a->moo_pos < a->moo_len ) { samp = a->moo_buf[a->moo_pos++]; stream[j] += samp; + } else { + a->mootex = 0; } } } -void audio_trigger_moo(AudioContext *a) { +void audio_trigger_moo() { + AudioContext *a = audio_context; + + if ( a->mootex != 0 ) return; + + printf("Moo!\n"); + a->mootex = 1; + if ( a->aplay_fallback ) { pid_t pid; @@ -77,9 +90,8 @@ void audio_trigger_moo(AudioContext *a) { execlp("aplay", "aplay", DATADIR"/openmoocow/moo.wav", NULL); } } /* else forked successfully, parent process */ - printf("Waiting...\n"); fflush(stdout); waitpid(pid, &status, 0); - printf("Done mooing\n"); fflush(stdout); + a->mootex = 0; } else { if ( a->moo_pos == a->moo_len ) { @@ -90,7 +102,7 @@ void audio_trigger_moo(AudioContext *a) { } /* SDL audio initial setup */ -AudioContext *audio_setup() { +void audio_setup() { AudioContext *a; SDL_AudioSpec fmt; @@ -101,7 +113,9 @@ AudioContext *audio_setup() { /* Create audio context */ a = malloc(sizeof(AudioContext)); - if ( a == NULL ) return NULL; + assert(a != NULL); + + a->mootex = 0; /* 16-bit mono audio at 44.1 kHz */ fmt.freq = 44100; @@ -115,19 +129,19 @@ AudioContext *audio_setup() { if ( SDL_OpenAudio(&fmt, NULL) < 0 ) { fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError()); a->aplay_fallback = 1; - return a; + return; } a->aplay_fallback = 0; if ( SDL_LoadWAV(DATADIR"/openmoocow/moo.wav", &wave, &data, &dlen) == NULL ) { fprintf(stderr, "Couldn't load moo sound: %s\n", SDL_GetError()); - return a; + return; } SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 1, 44100); cvt.buf = malloc(dlen*cvt.len_mult); if ( cvt.buf == NULL ) { fprintf(stderr, "Not enough memory to convert audio \n"); - return a; + return; } memcpy(cvt.buf, data, dlen); cvt.len = dlen; @@ -140,7 +154,7 @@ AudioContext *audio_setup() { SDL_PauseAudio(0); - return a; + audio_context = a; } |