From cc13a882de37b9bf4be81c36202e3aeb42fda79c Mon Sep 17 00:00:00 2001
From: Markus Henschel <markus.henschel@yager.de>
Date: Fri, 1 Nov 2013 12:33:21 +0100
Subject: [PATCH] add function to audio streams that can retrieved the number
 of played samples

---
 addons/audio/allegro5/allegro_audio.h          |  2 ++
 addons/audio/allegro5/internal/aintern_audio.h |  2 ++
 addons/audio/kcm_stream.c                      | 15 +++++++++++++++
 3 files changed, 19 insertions(+)

diff --git a/addons/audio/allegro5/allegro_audio.h b/addons/audio/allegro5/allegro_audio.h
index 2fd9a82..2786f49 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(LONG_LONG, al_get_audio_stream_played_samples, (const ALLEGRO_AUDIO_STREAM *stream));
+
 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/allegro5/internal/aintern_audio.h b/addons/audio/allegro5/internal/aintern_audio.h
index cf6f899..f519975 100644
--- a/addons/audio/allegro5/internal/aintern_audio.h
+++ b/addons/audio/allegro5/internal/aintern_audio.h
@@ -263,6 +263,8 @@ struct ALLEGRO_AUDIO_STREAM {
 
    void                  *extra;
                          /* Extra data for use by the flac/vorbis addons. */
+   
+   unsigned int          played_fragments; //number of complete fragment buffers played since the stream has been started the last time
 };
 
 bool _al_kcm_refill_stream(ALLEGRO_AUDIO_STREAM *stream);
diff --git a/addons/audio/kcm_stream.c b/addons/audio/kcm_stream.c
index c2835fc..5925ecc 100644
--- a/addons/audio/kcm_stream.c
+++ b/addons/audio/kcm_stream.c
@@ -330,6 +330,18 @@ void *al_get_audio_stream_fragment(const ALLEGRO_AUDIO_STREAM *stream)
    return fragment;
 }
 
+/* Function: al_get_audio_stream_played_samples
+*/
+LONG_LONG al_get_audio_stream_played_samples(const ALLEGRO_AUDIO_STREAM *stream)
+{
+   LONG_LONG result=0;
+   maybe_lock_mutex(stream->spl.mutex);
+   if (stream->spl.spl_data.buffer.ptr)
+      result=stream->played_fragments*stream->spl.spl_data.len+stream->spl.pos;
+   maybe_unlock_mutex(stream->spl.mutex);
+   return result;
+}
+
 
 /* Function: al_set_audio_stream_speed
  */
@@ -502,6 +514,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->played_fragments=0;
 }
 
 
@@ -627,6 +640,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->played_fragments;
    }
 
    stream->spl.pos = 0;
-- 
1.8.3.msysgit.0

