# HG changeset patch # User Aaron Wishnick # Date 1218502258 0 # Node ID 344c8da164f49b7d6cd26df92b498fe7e3971727 # Parent 0caed045d01b49ec0a3eee632e25b937a2b524af Added streamer code. I haven't yet incorporated it into SDL_RunAudio() though. diff -r 0caed045d01b -r 344c8da164f4 src/audio/SDL_audio.c --- a/src/audio/SDL_audio.c Tue Aug 12 00:24:42 2008 +0000 +++ b/src/audio/SDL_audio.c Tue Aug 12 00:50:58 2008 +0000 @@ -256,6 +256,58 @@ #undef FILL_STUB } +/* Streaming functions (for when the input and output buffer sizes are different) */ +/* Write [length] bytes from buf into the streamer */ +void SDL_StreamWrite(SDL_AudioStreamer * stream, Uint8 * buf, int length) { + int i; + + for(i = 0; i < length; ++i) { + stream->buffer[stream->write_pos] = buf[i]; + ++stream->write_pos; + } +} + +/* Read [length] bytes out of the streamer into buf */ +void SDL_StreamRead(SDL_AudioStreamer * stream, Uint8 * buf, int length) { + int i; + + for(i = 0; i < length; ++i) { + buf[i] = stream->buffer[stream->read_pos]; + ++stream->read_pos; + } +} + +int SDL_StreamLength(SDL_AudioStreamer * stream) { + return (stream->write_pos - stream->read_pos) % stream->max_len; +} + +/* Initialize the stream by allocating the buffer and setting the read/write heads to the beginning */ +int SDL_StreamInit(SDL_AudioStreamer * stream, int max_len) { + int i; + + /* First try to allocate the buffer */ + stream->buffer = (Uint8 *)malloc(max_len); + if(stream->buffer == NULL) { + return -1; + } + + stream->max_len = max_len; + stream->read_pos = 0; + stream->write_pos = 0; + + /* Zero out the buffer */ + for(i = 0; i < max_len; ++i) { + stream->buffer[i] = 0; + } +} + +/* Deinitialize the stream simply by freeing the buffer */ +void SDL_StreamDeinit(SDL_AudioStreamer * stream) { + if(stream->buffer != NULL) { + free(stream->buffer); + } +} + /* The general mixing thread function */ int SDLCALL diff -r 0caed045d01b -r 344c8da164f4 src/audio/SDL_audio_c.h --- a/src/audio/SDL_audio_c.h Tue Aug 12 00:24:42 2008 +0000 +++ b/src/audio/SDL_audio_c.h Tue Aug 12 00:50:58 2008 +0000 @@ -42,4 +42,12 @@ } SDL_AudioTypeFilters; extern const SDL_AudioTypeFilters sdl_audio_type_filters[]; +/* Streamer */ +typedef struct +{ + Uint8 *buffer; + int max_len; // the maximum length in bytes + int read_pos, write_pos; // the position of the write and read heads in bytes +} SDL_AudioStreamer; + /* vi: set ts=4 sw=4 expandtab: */