SDL13 audio input API

I’ve been playing around with the SDL13 source and I think I have the
beginnings of something usable. So far it works with the basic /dev/dsp
driver.

Instead of modifying the SDL_AudioSpec structure to add another callback,
which would mean big trouble for things statically linked to SDL, I added the
SDL_OpenAudioIn function. It’s called after SDL_OpenAudio, to prepare the
extra callback. The new flag SDL_INIT_AUDIOIN was also necessary to inform
the audio drivers to prepare to record.

The audioin api also mirrors most of the functions of the output api, with
SDL_LockAudioIn, SDL_UnlockAudioIn, SDL_GetAudioInStatus, and
SDL_PauseAudioIn.

Limitations of this scheme include having to use the same input and output
format(lots of drivers are limited to that anyway), needing an extra flag and
function to enable audio input, and needing at least a do-nothing stub for
audio input.

Here’s a simple example:

/* Beginning of program */
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

void audio_stub(void *userdata, Uint8 *stream, int bytes)
{
}

void audioin_callback(void *userdata, Uint8 *stream, int bytes)
{
fprintf(stderr,“audioin_callback(%p,%p,%p)\n”,userdata,stream,bytes);
}

int main(int argc, char *argv[])
{
int audioin_userdata;
SDL_AudioSpec spec;

if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_AUDIOIN)<0)
{
fprintf(stderr,“Couldn’t init SDL: %s\n”,SDL_GetError());
return(1);
}
atexit(SDL_Quit);

spec.format=AUDIO_S16SYS;
spec.channels=2;
spec.samples=512;
spec.freq=44100;
spec.callback=audio_stub;

if(SDL_OpenAudio(&spec,NULL)<0)
{
fprintf(stderr,“Couldn’t open audio: %s\n”,SDL_GetError());
return(2);
}

if(SDL_OpenAudioIn(audioin_callback,&audioin_userdata)<0)
{
fprintf(stderr,“Couldn’t open audio input: %s\n”,SDL_GetError());
return(3);
}

SDL_PauseAudioIn(0);
SDL_Delay(5000); // wait 5 seconds, audioin_callback should print
SDL_PauseAudioIn(1);
SDL_CloseAudio();
return(0);
}
/* end of program */

How does this look? Can you use this scheme? Any ideas for simplifications
or improvements?