Mercurial > sdl-ios-xcode
changeset 2664:344c8da164f4 gsoc2008_audio_resampling
Added streamer code. I haven't yet incorporated it into SDL_RunAudio() though.
author | Aaron Wishnick <schnarf@gmail.com> |
---|---|
date | Tue, 12 Aug 2008 00:50:58 +0000 |
parents | 0caed045d01b |
children | f39a056aec8b |
files | src/audio/SDL_audio.c src/audio/SDL_audio_c.h |
diffstat | 2 files changed, 60 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- 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: */