src/audio/SDL_audio.c
branchgsoc2008_audio_resampling
changeset 2664 344c8da164f4
parent 2274 aedfcdeb69b6
child 2665 f39a056aec8b
--- 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