From c324c1f0bc2974d154a2dd2ff1472d652f3f45c2 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Mon, 1 Jun 2009 20:25:38 +0100 Subject: Fall back to 'aplay' if the mixer callback doesn't get called, or if SDL audio can't initialise --- src/audio.c | 52 +++++++++++++++++++++++++++++++++++++--------------- src/types.h | 1 + 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/audio.c b/src/audio.c index 3cb498b..1ec8108 100644 --- a/src/audio.c +++ b/src/audio.c @@ -64,12 +64,19 @@ static void audio_mix(void *data, Uint8 *stream8, int len) } } + + a->aplay_fallback = 0; } void audio_trigger_moo() { AudioContext *a = audio_context; + if ( a->mootex != 0 ) { + printf("Mootex says 'no'\n"); + return; + } + if ( a == NULL ) { /* Try to open the audio again */ printf("Trying to open the audio again...\n"); @@ -77,15 +84,30 @@ void audio_trigger_moo() a = audio_context; if ( a == NULL ) return; } - if ( a->mootex != 0 ) { - printf("Mootex says 'no'\n"); - return; - } - printf("Moo!\n"); a->mootex = 1; - if ( a->moo_pos == a->moo_len ) { + if ( a->aplay_fallback ) { + + pid_t pid; + int status; + + printf("Using aplay fallback\n"); + pid = fork(); + if ( !( (pid != 0) && (pid != -1) ) ) { + if ( pid == -1 ) { + fprintf(stderr, "fork() failed.\n"); + return; + } else { + /* Forked successfully, child process */ + execlp("aplay", "aplay", + DATADIR"/openmoocow/moo.wav", NULL); + } + } /* else forked successfully, parent process */ + waitpid(pid, &status, 0); + a->mootex = 0; + + } else if ( a->moo_pos == a->moo_len ) { a->moo_pos = 0; } } @@ -103,7 +125,8 @@ void audio_setup() /* Create audio context */ a = malloc(sizeof(AudioContext)); assert(a != NULL); - a->mootex = 0; + a->mootex = 1; /* Not ready yet */ + audio_context = a; /* 16-bit mono audio at 44.1 kHz */ fmt.freq = 44100; @@ -116,24 +139,21 @@ void audio_setup() if ( SDL_OpenAudio(&fmt, NULL) < 0 ) { fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError()); - free(a); - return; + goto out; /* and use fallback */ } if ( SDL_LoadWAV(DATADIR"/openmoocow/moo.wav", &wave, &data, &dlen) == NULL ) { fprintf(stderr, "Couldn't load moo sound: %s\n", SDL_GetError()); - free(a); - return; + goto out; /* and use fallback */ } 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"); - free(a); - return; + goto out; /* and use fallback */ } memcpy(cvt.buf, data, dlen); cvt.len = dlen; @@ -143,10 +163,12 @@ void audio_setup() a->moo_len = cvt.len_cvt/2 - 2; /* Convert bytes to samples */ a->moo_pos = a->moo_len; /* Play nothing to start with */ a->moo_buf = (Sint16 *)cvt.buf; - - audio_context = a; + a->aplay_fallback = 1; SDL_PauseAudio(0); + +out: + a->mootex = 0; /* Ready now */ } void audio_shutdown() diff --git a/src/types.h b/src/types.h index b1752f7..b59c933 100644 --- a/src/types.h +++ b/src/types.h @@ -63,6 +63,7 @@ typedef struct { long moo_len; long moo_pos; Sint16 *moo_buf; + int aplay_fallback; unsigned int mootex; -- cgit v1.2.3