[AD] Callback functionality for audiostreams

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


For my application, I needed the ability to have a callback into the
audiostreams, so that I get notified whenever free_audio_stream is called.
(This way I can capture the sound output by almp3, alogg, etc and modify it
before it gets to the speakers).

Anyway, I was wondering whether this would be suitable to add into allegro
itself. Personally I'd rather it was added so that I didn't have to use a
patched version of the library, but what do you think?

I've attached a patch which adds the capability (no documentation, I won't
bother with that unless it's accepted).

(patch is against STREAM.C, 4.1.12)
Cheers
chris
--- stream_old.c	Sun Nov  9 19:43:10 2003
+++ stream.c	Sun Feb 29 17:48:40 2004
@@ -18,6 +18,8 @@
 
 #include "allegro.h"
 
+/* The callback */
+static void (*audiostream_callback)(AUDIOSTREAM *, char *buffer, int len) = NULL;
 
 
 /* play_audio_stream:
@@ -113,6 +115,17 @@ void stop_audio_stream(AUDIOSTREAM *stre
 }
 
 
+/* get_audio_stream_refill_buffer:
+ *  Helper function to get the location of the current buffer
+ *  being filled for the supplied audiostream
+ */
+static char *get_audio_stream_refill_buffer(AUDIOSTREAM *stream)
+{
+   return (char *)stream->locked + ((stream->bufnum % stream->bufcount) *
+				    stream->len *
+				    ((stream->samp->bits==8) ? 1 : sizeof(short)) *
+				    ((stream->samp->stereo) ? 2 : 1));
+}
 
 /* get_audio_stream_buffer:
  *  Returns a pointer to the next audio buffer, or NULL if the previous 
@@ -157,10 +170,7 @@ void *get_audio_stream_buffer(AUDIOSTREA
 	 stream->locked = (char *)stream->samp->data + (pos * ((stream->samp->bits==8) ? 1 : sizeof(short)) * ((stream->samp->stereo) ? 2 : 1));
    }
 
-   return (char *)stream->locked + ((stream->bufnum % stream->bufcount) *
-				    stream->len *
-				    ((stream->samp->bits==8) ? 1 : sizeof(short)) *
-				    ((stream->samp->stereo) ? 2 : 1));
+   return get_audio_stream_refill_buffer(stream);
 }
 
 
@@ -172,6 +182,9 @@ void *get_audio_stream_buffer(AUDIOSTREA
 void free_audio_stream_buffer(AUDIOSTREAM *stream)
 {
    ASSERT(stream);
+
+   if (audiostream_callback)
+      audiostream_callback(stream, get_audio_stream_refill_buffer(stream), stream->len);
    
    /* flip buffers */
    stream->bufnum++;
@@ -192,4 +205,14 @@ void free_audio_stream_buffer(AUDIOSTREA
    /* start playing if it wasn't already */
    if (voice_get_position(stream->voice) == -1)
       voice_start(stream->voice);
+}
+
+
+/* set_audio_stream_callback:
+ *  Sets the supplied function as the one to be called whenever
+ *  an audio stream has new data ready.
+ */
+void set_audio_stream_callback(void (*proc)(AUDIOSTREAM *, char *, int))
+{
+   audiostream_callback = proc;
 }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/