/* * audio.c * * Moo like a cow * * (c) 2008-2009 Thomas White * * This file is part of OpenMooCow - accelerometer moobox simulator * * OpenMooCow is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * OpenMooCow is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with OpenMooCow. If not, see . * */ /* SDL's headers don't like this. Grr. */ //#ifdef HAVE_CONFIG_H //#include //#endif #include #include #include #include #include #include #include #include #include #include #include "types.h" #include "audio.h" static AudioContext *audio_context = NULL; static void audio_mix(void *data, Uint8 *stream8, int len) { AudioContext *a = data; int j; Sint16 *stream = (Sint16 *)stream8; Sint16 samp; len /= 2; /* Number of samples to write */ for ( j=0; jmoo_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"); audio_setup(); 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 ) { a->moo_pos = 0; } } /* 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; fmt.channels = 1; fmt.samples = 512; 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); 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"); free(a); return; } memcpy(cvt.buf, data, dlen); 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); }