aboutsummaryrefslogtreecommitdiff
path: root/src/audio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio.c')
-rw-r--r--src/audio.c45
1 files changed, 22 insertions, 23 deletions
diff --git a/src/audio.c b/src/audio.c
index cfe6939..fd54041 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -3,7 +3,7 @@
*
* Moo like a cow
*
- * (c) 2008 Thomas White <taw27@srcf.ucam.org>
+ * (c) 2008-2009 Thomas White <taw@bitwiz.org.uk>
*
* This file is part of OpenMooCow - accelerometer moobox simulator
*
@@ -49,28 +49,28 @@ static void audio_mix(void *data, Uint8 *stream8, int len) {
int j;
Sint16 *stream = (Sint16 *)stream8;
Sint16 samp;
-
+
len /= 2; /* Number of samples to write */
-
+
for ( j=0; j<len; j++ ) {
-
+
stream[j] = 0;
-
+
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 = audio_context;
-
+
if ( a == NULL ) {
/* Try to open the audio again */
printf("Trying to open the audio again...\n");
@@ -85,7 +85,7 @@ void audio_trigger_moo() {
printf("Moo!\n");
a->mootex = 1;
-
+
if ( a->moo_pos == a->moo_len ) {
a->moo_pos = 0;
}
@@ -94,19 +94,19 @@ void audio_trigger_moo() {
/* SDL audio initial setup */
void audio_setup() {
-
+
AudioContext *a;
SDL_AudioSpec fmt;
SDL_AudioSpec wave;
Uint8 *data;
Uint32 dlen;
SDL_AudioCVT cvt;
-
+
/* Create audio context */
a = malloc(sizeof(AudioContext));
assert(a != NULL);
a->mootex = 0;
-
+
/* 16-bit mono audio at 44.1 kHz */
fmt.freq = 44100;
fmt.format = AUDIO_S16;
@@ -115,13 +115,13 @@ void audio_setup() {
fmt.callback = audio_mix;
fmt.userdata = a;
fmt.silence = 0;
-
+
if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
free(a);
return;
}
-
+
if ( SDL_LoadWAV(DATADIR"/openmoocow/moo.wav", &wave, &data, &dlen) == NULL ) {
fprintf(stderr, "Couldn't load moo sound: %s\n", SDL_GetError());
free(a);
@@ -138,27 +138,26 @@ void audio_setup() {
cvt.len = dlen;
SDL_ConvertAudio(&cvt);
SDL_FreeWAV(data);
-
+
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;
-
+
SDL_PauseAudio(0);
-
+
}
void audio_shutdown() {
AudioContext *a = audio_context;
-
+
if ( a == NULL ) return;
-
+
SDL_CloseAudio();
-
+
/* Now this can be freed */
free(a);
-
-}
+}