From e3606ca6ebcdb5637a0131d2c47ca151e2494477 Mon Sep 17 00:00:00 2001
From: Markus Henschel <markus.henschel@yager.de>
Date: Thu, 31 Oct 2013 15:14:56 +0100
Subject: [PATCH] add function to get current stream position and fragment

---
 addons/audio/allegro5/allegro_audio.h |  2 ++
 addons/audio/kcm_stream.c             | 21 +++++++++++++++++++++
 docs/src/refman/audio.txt             | 16 ++++++++++++++++
 3 files changed, 39 insertions(+)

diff --git a/addons/audio/allegro5/allegro_audio.h b/addons/audio/allegro5/allegro_audio.h
index 2fd9a82..35d8c73 100644
--- a/addons/audio/allegro5/allegro_audio.h
+++ b/addons/audio/allegro5/allegro_audio.h
@@ -263,6 +263,8 @@ ALLEGRO_KCM_AUDIO_FUNC(bool, al_get_audio_stream_attached, (const ALLEGRO_AUDIO_
 
 ALLEGRO_KCM_AUDIO_FUNC(void *, al_get_audio_stream_fragment, (const ALLEGRO_AUDIO_STREAM *stream));
 
+ALLEGRO_KCM_AUDIO_FUNC(const void *, al_get_audio_stream_position, (const ALLEGRO_AUDIO_STREAM *stream, int * samplepos));
+
 ALLEGRO_KCM_AUDIO_FUNC(bool, al_set_audio_stream_speed, (ALLEGRO_AUDIO_STREAM *stream, float val));
 ALLEGRO_KCM_AUDIO_FUNC(bool, al_set_audio_stream_gain, (ALLEGRO_AUDIO_STREAM *stream, float val));
 ALLEGRO_KCM_AUDIO_FUNC(bool, al_set_audio_stream_pan, (ALLEGRO_AUDIO_STREAM *stream, float val));
diff --git a/addons/audio/kcm_stream.c b/addons/audio/kcm_stream.c
index c2835fc..cf0940a 100644
--- a/addons/audio/kcm_stream.c
+++ b/addons/audio/kcm_stream.c
@@ -460,6 +460,27 @@ bool al_set_audio_stream_playmode(ALLEGRO_AUDIO_STREAM *stream,
    return false;
 }
 
+/* Function: al_get_audio_stream_position
+ */
+const void *al_get_audio_stream_position(const ALLEGRO_AUDIO_STREAM *stream, int * samplepos)
+{
+   void *fragment=0;
+   ASSERT(stream);
+   
+   maybe_lock_mutex(stream->spl.mutex);
+   
+   fragment=stream->spl.spl_data.buffer.ptr;
+   if (samplepos)
+   {
+      if (fragment)
+         *samplepos=stream->spl.pos;
+      else
+         *samplepos=0;
+   }
+	  
+   maybe_unlock_mutex(stream->spl.mutex);
+   return fragment;
+}
 
 static void reset_stopped_stream(ALLEGRO_AUDIO_STREAM *stream)
 {
diff --git a/docs/src/refman/audio.txt b/docs/src/refman/audio.txt
index 7bd4f53..0d11598 100644
--- a/docs/src/refman/audio.txt
+++ b/docs/src/refman/audio.txt
@@ -915,6 +915,22 @@ Return the stream audio depth.
 
 See also: [ALLEGRO_AUDIO_DEPTH].
 
+### API: al_get_audio_stream_position
+
+This function can be used to retrieve the currently played fragment buffer
+together with the sample position inside this buffer. The pointer returned
+by this function matches a fragment buffer pointers provided by [al_get_audio_stream_fragment].
+So when new sample data is provided by [al_set_audio_stream_fragment] the application
+can keep track of what data it copied to which fragment buffer. To get the currently
+currently played sample the value written to samplepos should be added to the first sample
+written to the sample buffer.
+
+Return the currently played fragment buffer of the audio stream or NULL if no fragment has been submitted yet.
+
+See also: [al_get_audio_stream_fragment], [al_set_audio_stream_fragment].
+
+Since: 5.1.8
+
 ### API: al_get_audio_stream_length
 
 Return the stream length in samples.
-- 
1.8.3.msysgit.0

