From f27744ceaa992ad6544f2023debad0f6342c536e Mon Sep 17 00:00:00 2001 From: fd Date: Tue, 10 Mar 2015 22:14:22 -0400 Subject: [PATCH 1/3] Attempt to fix inaccurate track positions #42 --- .../slick/openal/OpenALStreamPlayer.java | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/org/newdawn/slick/openal/OpenALStreamPlayer.java b/src/org/newdawn/slick/openal/OpenALStreamPlayer.java index f349b20e..d50b9690 100644 --- a/src/org/newdawn/slick/openal/OpenALStreamPlayer.java +++ b/src/org/newdawn/slick/openal/OpenALStreamPlayer.java @@ -36,10 +36,12 @@ import java.nio.IntBuffer; import org.lwjgl.BufferUtils; import org.lwjgl.Sys; import org.lwjgl.openal.AL10; +import org.lwjgl.openal.AL11; import org.lwjgl.openal.OpenALException; import org.newdawn.slick.util.Log; import org.newdawn.slick.util.ResourceLoader; + /** * A generic tool to work on a supplied stream, pulling out PCM data and buffered it to OpenAL * as required. @@ -207,6 +209,7 @@ public class OpenALStreamPlayer { AL10.alSourceStop(source); startPlayback(); + syncStartTime = getTime(); } /** @@ -347,6 +350,7 @@ public class OpenALStreamPlayer { } playedPos = streamPos; + syncStartTime = getTime() - playedPos*1000/sampleSize/sampleRate; startPlayback(); @@ -381,18 +385,53 @@ public class OpenALStreamPlayer { * * @return The current position in seconds. */ - public float getPosition() { + public float getALPosition() { float playedTime = ((float) playedPos / (float) sampleSize) / sampleRate; - float timePosition = playedTime + (getTime() - lastUpdateTime) / 1000f; -// + AL10.alGetSourcef(source, AL11.AL_SEC_OFFSET); + float timePosition = playedTime + AL10.alGetSourcef(source, AL11.AL_SEC_OFFSET); return timePosition; } + float lastUpdatePosition = 0; + long syncStartTime = getTime(); //the assumed start time of the music + float avgDiff; + long pauseTime; + + public float getPosition() { + float thisPosition = getALPosition(); + long thisTime = getTime(); + float dxPosition = thisPosition - lastUpdatePosition; + + long dxTime = thisTime - syncStartTime; + + //hard reset + if (Math.abs(thisPosition - dxTime / 1000f) > 1 / 2f) { + System.out.println("Time HARD Reset" + " " + thisPosition + " " + + (dxTime / 1000f) + " " + + (int) (thisPosition * 1000 - (dxTime)) + " " + + (int) (thisPosition * 1000 - (thisTime - syncStartTime))); + syncStartTime = thisTime - ((long) (thisPosition * 1000)); + dxTime = thisTime - syncStartTime; + avgDiff = 0; + } + if ((int) (dxPosition * 1000) != 0) { // lastPosition != thisPosition + float diff = thisPosition * 1000 - (dxTime); + avgDiff = (diff + avgDiff * 9) / 10; + syncStartTime -= (int) (avgDiff/2); + dxTime = thisTime - syncStartTime; + lastUpdatePosition = thisPosition; + //System.out.println(diff); + + } + //System.out.println("AL2Tme:"+(dxTime/1000f)+" "+thisPosition+" time:"+getTime()+" "+avgDiff+" "+syncStartTime); + + return dxTime / 1000f; + } /** * Processes a track pause. */ public void pausing() { offsetTime = getTime() - lastUpdateTime; + pauseTime = getTime(); } /** @@ -400,6 +439,7 @@ public class OpenALStreamPlayer { */ public void resuming() { lastUpdateTime = getTime() - offsetTime; + syncStartTime += getTime() - pauseTime; } /** From ab9ea57a4c10d8db0f514e0b933468e51aa67ae2 Mon Sep 17 00:00:00 2001 From: fd Date: Tue, 10 Mar 2015 22:55:58 -0400 Subject: [PATCH 2/3] Attempt to fix inaccurate track positions #42 cleanup --- .../slick/openal/OpenALStreamPlayer.java | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/org/newdawn/slick/openal/OpenALStreamPlayer.java b/src/org/newdawn/slick/openal/OpenALStreamPlayer.java index d50b9690..616911a2 100644 --- a/src/org/newdawn/slick/openal/OpenALStreamPlayer.java +++ b/src/org/newdawn/slick/openal/OpenALStreamPlayer.java @@ -98,12 +98,20 @@ public class OpenALStreamPlayer { /** The music length. */ long musicLength = -1; - /** The time of the last update, in ms. */ - long lastUpdateTime = getTime(); - - /** The offset time. */ - long offsetTime = 0; - + + /** The assumed time of when the music position would be 0 */ + long syncStartTime; + + /** The last value that was returned the music position */ + float lastUpdatePosition = 0; + + /** The average difference between the sync time and the music position */ + float avgDiff; + + /** The time when it was paused */ + long pauseTime; + + /** * Create a new player to work on an audio stream * @@ -252,7 +260,6 @@ public class OpenALStreamPlayer { int bufferLength = AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE); playedPos += bufferLength; - lastUpdateTime = getTime(); if (musicLength > 0 && playedPos > musicLength) playedPos -= musicLength; @@ -377,7 +384,6 @@ public class OpenALStreamPlayer { AL10.alSourceQueueBuffers(source, bufferNames); AL10.alSourcePlay(source); - lastUpdateTime = getTime(); } /** @@ -391,24 +397,19 @@ public class OpenALStreamPlayer { return timePosition; } - float lastUpdatePosition = 0; - long syncStartTime = getTime(); //the assumed start time of the music - float avgDiff; - long pauseTime; - + /** + * Return the current playing position in the sound + * + * @return The current position in seconds. + */ public float getPosition() { float thisPosition = getALPosition(); long thisTime = getTime(); float dxPosition = thisPosition - lastUpdatePosition; - long dxTime = thisTime - syncStartTime; //hard reset if (Math.abs(thisPosition - dxTime / 1000f) > 1 / 2f) { - System.out.println("Time HARD Reset" + " " + thisPosition + " " - + (dxTime / 1000f) + " " - + (int) (thisPosition * 1000 - (dxTime)) + " " - + (int) (thisPosition * 1000 - (thisTime - syncStartTime))); syncStartTime = thisTime - ((long) (thisPosition * 1000)); dxTime = thisTime - syncStartTime; avgDiff = 0; @@ -419,18 +420,14 @@ public class OpenALStreamPlayer { syncStartTime -= (int) (avgDiff/2); dxTime = thisTime - syncStartTime; lastUpdatePosition = thisPosition; - //System.out.println(diff); - } - //System.out.println("AL2Tme:"+(dxTime/1000f)+" "+thisPosition+" time:"+getTime()+" "+avgDiff+" "+syncStartTime); - + return dxTime / 1000f; } /** * Processes a track pause. */ public void pausing() { - offsetTime = getTime() - lastUpdateTime; pauseTime = getTime(); } @@ -438,7 +435,6 @@ public class OpenALStreamPlayer { * Processes a track resume. */ public void resuming() { - lastUpdateTime = getTime() - offsetTime; syncStartTime += getTime() - pauseTime; } From 65c50771aceb603152dba0a22cfb7635779ba41a Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Wed, 11 Mar 2015 00:37:57 -0400 Subject: [PATCH 3/3] Insignificant whitespace changes from #44. Signed-off-by: Jeffrey Han --- .../slick/openal/OpenALStreamPlayer.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/org/newdawn/slick/openal/OpenALStreamPlayer.java b/src/org/newdawn/slick/openal/OpenALStreamPlayer.java index 616911a2..8fbe3694 100644 --- a/src/org/newdawn/slick/openal/OpenALStreamPlayer.java +++ b/src/org/newdawn/slick/openal/OpenALStreamPlayer.java @@ -41,7 +41,6 @@ import org.lwjgl.openal.OpenALException; import org.newdawn.slick.util.Log; import org.newdawn.slick.util.ResourceLoader; - /** * A generic tool to work on a supplied stream, pulling out PCM data and buffered it to OpenAL * as required. @@ -98,20 +97,18 @@ public class OpenALStreamPlayer { /** The music length. */ long musicLength = -1; - - /** The assumed time of when the music position would be 0 */ + /** The assumed time of when the music position would be 0. */ long syncStartTime; - - /** The last value that was returned the music position */ + + /** The last value that was returned for the music position. */ float lastUpdatePosition = 0; - - /** The average difference between the sync time and the music position */ + + /** The average difference between the sync time and the music position. */ float avgDiff; - - /** The time when it was paused */ + + /** The time when the music was paused. */ long pauseTime; - - + /** * Create a new player to work on an audio stream * @@ -357,7 +354,7 @@ public class OpenALStreamPlayer { } playedPos = streamPos; - syncStartTime = getTime() - playedPos*1000/sampleSize/sampleRate; + syncStartTime = getTime() - playedPos * 1000 / sampleSize / sampleRate; startPlayback(); @@ -408,7 +405,7 @@ public class OpenALStreamPlayer { float dxPosition = thisPosition - lastUpdatePosition; long dxTime = thisTime - syncStartTime; - //hard reset + // hard reset if (Math.abs(thisPosition - dxTime / 1000f) > 1 / 2f) { syncStartTime = thisTime - ((long) (thisPosition * 1000)); dxTime = thisTime - syncStartTime; @@ -421,9 +418,10 @@ public class OpenALStreamPlayer { dxTime = thisTime - syncStartTime; lastUpdatePosition = thisPosition; } - + return dxTime / 1000f; } + /** * Processes a track pause. */