Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
forteri76 committed May 11, 2014
1 parent 34cc808 commit cc10338
Showing 1 changed file with 43 additions and 40 deletions.
83 changes: 43 additions & 40 deletions src/com/peculiargames/andmodplug/PlayerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public class PlayerThread extends Thread {
private int mRate;
private int posWas;
private boolean songFinished;
private boolean songFinishedWas;

/*
* Track if player has started (after loading a new mod).
Expand Down Expand Up @@ -313,10 +314,11 @@ public PlayerThread(byte[] modData, int desiredrate) {
/*
* Get info (name and number of tracks) for the loaded MOD file.
*/
mModname = ModPlug_JGetName();
mNumChannels = ModPlug_JNumChannels();
posWas = 0;
songFinished = false;
mModname = ModPlug_JGetName();
mNumChannels = ModPlug_JNumChannels();
posWas = 0;
songFinished = false;
songFinishedWas = false;
}
}

Expand Down Expand Up @@ -348,6 +350,24 @@ public PlayerThread(int desiredrate) {
mPlayerValid = true;
}

/**
* Determine when a song has played to completion. This method also
* detects when a looping track has started over from the beginning.
*/
private void CheckSongCompleted() {
int posNow = getCurrentPos();

if ((posNow >= posWas) && (posNow < getMaxPos())) {
songFinished = false;
}

if (!songFinished && ((posNow < posWas) || (posNow >= getMaxPos()))) {
songFinished = true;
}

posWas = posNow;
}

/**
* Try to get an Android stereo audio track used by the various
* constructors.
Expand Down Expand Up @@ -458,10 +478,11 @@ public void LoadMODData(byte[] modData) {
mLoad_ok = ModPlug_JLoad(modData, modData.length);

if (mLoad_ok) {
mModname = ModPlug_JGetName();
mNumChannels = ModPlug_JNumChannels();
posWas = 0;
songFinished = false;
mModname = ModPlug_JGetName();
mNumChannels = ModPlug_JNumChannels();
posWas = 0;
songFinished = false;
songFinishedWas = false;
}

/*
Expand Down Expand Up @@ -537,10 +558,16 @@ public void run() {
* Pre-load another packet.
*/
synchronized(sRDlock) {
ModPlug_JGetSoundData(mBuffer, BUFFERSIZE);
if (mRunning && mPlaying && mLoad_ok) try {
ModPlug_JGetSoundData(mBuffer, BUFFERSIZE);

if (ModPlug_CheckPatternChange())
pattern_change = true;

if (ModPlug_CheckPatternChange())
pattern_change = true;
CheckSongCompleted();
} catch (Exception e) {
e.getCause().printStackTrace();
}
}

/*
Expand All @@ -559,49 +586,25 @@ public void run() {
mPlayerListener.onPlayerEvent(eventEnum.PLAYER_STARTED);
}
}
}

synchronized(this) {
if (pattern_change) {
pattern_change = false;

if (mPlayerListener != null)
mPlayerListener.onPlayerEvent(eventEnum.PATTERN_CHANGE);
}
}

synchronized(sRDlock) {
if (mRunning && mPlaying && mLoad_ok) try {
int posNow = getCurrentPos();

if ((posNow >= posWas) && (posNow < getMaxPos()))
songFinished = false;

if (!songFinished &&
((posNow < posWas) || (posNow >= getMaxPos()))) {
if (mPlayerListener != null)
mPlayerListener.onPlayerEvent(eventEnum.SONG_COMPLETED);

songFinished = true;
}

posWas = posNow;
} catch (Exception e) {
e.getCause().printStackTrace();
if (!songFinished) {
if (mPlayerListener != null)
mPlayerListener.onPlayerEvent(eventEnum.SONG_COMPLETED);

songFinished = true;
}
if (songFinished && !songFinishedWas) {
if (mPlayerListener != null)
mPlayerListener.onPlayerEvent(eventEnum.SONG_COMPLETED);
}
songFinishedWas = songFinished;
}
}

/*
* Wait until notify() is called.
*/
synchronized (this) {
synchronized(this) {
if (mWaitFlag) {
try {
wait();
Expand Down

0 comments on commit cc10338

Please sign in to comment.