From 2603c54b476dceb9ae60429100515db8475bd2bb Mon Sep 17 00:00:00 2001
From: Markus Henschel <markus.henschel@yager.de>
Date: Mon, 4 Nov 2013 15:29:13 +0100
Subject: [PATCH] audio: Add al_get_audio_stream_position.

---
 addons/audio/allegro5/allegro_audio.h          |  1 +
 addons/audio/allegro5/internal/aintern_audio.h |  5 +++++
 addons/audio/kcm_stream.c                      | 23 +++++++++++++++++++++++
 docs/src/refman/audio.txt                      |  7 +++++++
 4 files changed, 36 insertions(+)

diff --git a/addons/audio/allegro5/allegro_audio.h b/addons/audio/allegro5/allegro_audio.h
index 2fd9a82..bff3f1e 100644
--- a/addons/audio/allegro5/allegro_audio.h
+++ b/addons/audio/allegro5/allegro_audio.h
@@ -260,6 +260,7 @@ ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_PLAYMODE, al_get_audio_stream_playmode, (const AL
 
 ALLEGRO_KCM_AUDIO_FUNC(bool, al_get_audio_stream_playing, (const ALLEGRO_AUDIO_STREAM *spl));
 ALLEGRO_KCM_AUDIO_FUNC(bool, al_get_audio_stream_attached, (const ALLEGRO_AUDIO_STREAM *spl));
+ALLEGRO_KCM_AUDIO_FUNC(uint64_t, al_get_audio_stream_position, (const ALLEGRO_AUDIO_STREAM *stream));
 
 ALLEGRO_KCM_AUDIO_FUNC(void *, al_get_audio_stream_fragment, (const ALLEGRO_AUDIO_STREAM *stream));
 
diff --git a/addons/audio/allegro5/internal/aintern_audio.h b/addons/audio/allegro5/internal/aintern_audio.h
index cf6f899..bb5373c 100644
--- a/addons/audio/allegro5/internal/aintern_audio.h
+++ b/addons/audio/allegro5/internal/aintern_audio.h
@@ -246,6 +246,11 @@ struct ALLEGRO_AUDIO_STREAM {
                           * played.
                           */
 
+   uint64_t              consumed_fragments;
+                         /* Number of complete fragment buffers consumed since
+                          * the stream was started.
+                          */
+
    ALLEGRO_THREAD        *feed_thread;
    volatile bool         quit_feed_thread;
    unload_feeder_t       unload_feeder;
diff --git a/addons/audio/kcm_stream.c b/addons/audio/kcm_stream.c
index c2835fc..afa04fd 100644
--- a/addons/audio/kcm_stream.c
+++ b/addons/audio/kcm_stream.c
@@ -303,6 +303,26 @@ bool al_get_audio_stream_attached(const ALLEGRO_AUDIO_STREAM *stream)
 }
 
 
+/* Function: al_get_audio_stream_position
+*/
+uint64_t al_get_audio_stream_position(const ALLEGRO_AUDIO_STREAM *stream)
+{
+   uint64_t result;
+
+   maybe_lock_mutex(stream->spl.mutex);
+   if (stream->spl.spl_data.buffer.ptr) {
+      result = stream->consumed_fragments * stream->spl.spl_data.len +
+         stream->spl.pos;
+   }
+   else {
+      result = 0;
+   }
+   maybe_unlock_mutex(stream->spl.mutex);
+
+   return result;
+}
+
+
 /* Function: al_get_audio_stream_fragment
 */
 void *al_get_audio_stream_fragment(const ALLEGRO_AUDIO_STREAM *stream)
@@ -502,6 +522,7 @@ static void reset_stopped_stream(ALLEGRO_AUDIO_STREAM *stream)
    stream->spl.spl_data.buffer.ptr = NULL;
    stream->spl.pos = stream->spl.spl_data.len;
    stream->spl.pos_bresenham_error = 0;
+   stream->consumed_fragments = 0;
 }
 
 
@@ -627,6 +648,8 @@ bool _al_kcm_refill_stream(ALLEGRO_AUDIO_STREAM *stream)
          (char *) new_buf - bytes_per_sample * MAX_LAG,
          (char *) old_buf + bytes_per_sample * (spl->pos-MAX_LAG),
          bytes_per_sample * MAX_LAG);
+
+      stream->consumed_fragments++;
    }
 
    stream->spl.pos = 0;
diff --git a/docs/src/refman/audio.txt b/docs/src/refman/audio.txt
index 7bd4f53..6b399e3 100644
--- a/docs/src/refman/audio.txt
+++ b/docs/src/refman/audio.txt
@@ -1011,6 +1011,13 @@ if anything.
 See also: [al_attach_audio_stream_to_mixer], [al_attach_audio_stream_to_voice],
 [al_get_audio_stream_attached].
 
+### API: al_get_audio_stream_position
+
+Get the number of samples consumed by the parent since the audio stream was
+started.
+
+Since: 5.1.8
+
 ### API: al_get_audio_stream_fragment
 
 When using Allegro's audio streaming, you will use this function to continuously
-- 
1.7.12.1

