Merge pull request #44 from fluddokt/omaster
Attempt to fix inaccurate track positions #42
This commit is contained in:
commit
4fc27eb0d0
|
@ -36,10 +36,12 @@ import java.nio.IntBuffer;
|
||||||
import org.lwjgl.BufferUtils;
|
import org.lwjgl.BufferUtils;
|
||||||
import org.lwjgl.Sys;
|
import org.lwjgl.Sys;
|
||||||
import org.lwjgl.openal.AL10;
|
import org.lwjgl.openal.AL10;
|
||||||
|
import org.lwjgl.openal.AL11;
|
||||||
import org.lwjgl.openal.OpenALException;
|
import org.lwjgl.openal.OpenALException;
|
||||||
import org.newdawn.slick.util.Log;
|
import org.newdawn.slick.util.Log;
|
||||||
import org.newdawn.slick.util.ResourceLoader;
|
import org.newdawn.slick.util.ResourceLoader;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A generic tool to work on a supplied stream, pulling out PCM data and buffered it to OpenAL
|
* A generic tool to work on a supplied stream, pulling out PCM data and buffered it to OpenAL
|
||||||
* as required.
|
* as required.
|
||||||
|
@ -96,11 +98,19 @@ public class OpenALStreamPlayer {
|
||||||
/** The music length. */
|
/** The music length. */
|
||||||
long musicLength = -1;
|
long musicLength = -1;
|
||||||
|
|
||||||
/** The time of the last update, in ms. */
|
|
||||||
long lastUpdateTime = getTime();
|
|
||||||
|
|
||||||
/** The offset time. */
|
/** The assumed time of when the music position would be 0 */
|
||||||
long offsetTime = 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
|
* Create a new player to work on an audio stream
|
||||||
|
@ -207,6 +217,7 @@ public class OpenALStreamPlayer {
|
||||||
AL10.alSourceStop(source);
|
AL10.alSourceStop(source);
|
||||||
|
|
||||||
startPlayback();
|
startPlayback();
|
||||||
|
syncStartTime = getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -249,7 +260,6 @@ public class OpenALStreamPlayer {
|
||||||
int bufferLength = AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE);
|
int bufferLength = AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE);
|
||||||
|
|
||||||
playedPos += bufferLength;
|
playedPos += bufferLength;
|
||||||
lastUpdateTime = getTime();
|
|
||||||
|
|
||||||
if (musicLength > 0 && playedPos > musicLength)
|
if (musicLength > 0 && playedPos > musicLength)
|
||||||
playedPos -= musicLength;
|
playedPos -= musicLength;
|
||||||
|
@ -347,6 +357,7 @@ public class OpenALStreamPlayer {
|
||||||
}
|
}
|
||||||
|
|
||||||
playedPos = streamPos;
|
playedPos = streamPos;
|
||||||
|
syncStartTime = getTime() - playedPos*1000/sampleSize/sampleRate;
|
||||||
|
|
||||||
startPlayback();
|
startPlayback();
|
||||||
|
|
||||||
|
@ -373,7 +384,17 @@ public class OpenALStreamPlayer {
|
||||||
|
|
||||||
AL10.alSourceQueueBuffers(source, bufferNames);
|
AL10.alSourceQueueBuffers(source, bufferNames);
|
||||||
AL10.alSourcePlay(source);
|
AL10.alSourcePlay(source);
|
||||||
lastUpdateTime = getTime();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the current playing position in the sound
|
||||||
|
*
|
||||||
|
* @return The current position in seconds.
|
||||||
|
*/
|
||||||
|
public float getALPosition() {
|
||||||
|
float playedTime = ((float) playedPos / (float) sampleSize) / sampleRate;
|
||||||
|
float timePosition = playedTime + AL10.alGetSourcef(source, AL11.AL_SEC_OFFSET);
|
||||||
|
return timePosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -382,24 +403,39 @@ public class OpenALStreamPlayer {
|
||||||
* @return The current position in seconds.
|
* @return The current position in seconds.
|
||||||
*/
|
*/
|
||||||
public float getPosition() {
|
public float getPosition() {
|
||||||
float playedTime = ((float) playedPos / (float) sampleSize) / sampleRate;
|
float thisPosition = getALPosition();
|
||||||
float timePosition = playedTime + (getTime() - lastUpdateTime) / 1000f;
|
long thisTime = getTime();
|
||||||
// + AL10.alGetSourcef(source, AL11.AL_SEC_OFFSET);
|
float dxPosition = thisPosition - lastUpdatePosition;
|
||||||
return timePosition;
|
long dxTime = thisTime - syncStartTime;
|
||||||
|
|
||||||
|
//hard reset
|
||||||
|
if (Math.abs(thisPosition - dxTime / 1000f) > 1 / 2f) {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return dxTime / 1000f;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Processes a track pause.
|
* Processes a track pause.
|
||||||
*/
|
*/
|
||||||
public void pausing() {
|
public void pausing() {
|
||||||
offsetTime = getTime() - lastUpdateTime;
|
pauseTime = getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes a track resume.
|
* Processes a track resume.
|
||||||
*/
|
*/
|
||||||
public void resuming() {
|
public void resuming() {
|
||||||
lastUpdateTime = getTime() - offsetTime;
|
syncStartTime += getTime() - pauseTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user