Clean up and it doesn't throw that slick exception anymore.
This commit is contained in:
parent
08406ac038
commit
a0bf2293cf
|
@ -82,8 +82,6 @@ public class MusicController {
|
||||||
trackLoader = new Thread() {
|
trackLoader = new Thread() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
//Loading ogg async seems to screw up
|
|
||||||
//So does mp3, but much less
|
|
||||||
loadTrack(osu.audioFilename, osu.previewTime, loop);
|
loadTrack(osu.audioFilename, osu.previewTime, loop);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -94,9 +92,6 @@ public class MusicController {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
loadTrack(osu.audioFilename, osu.previewTime, loop);
|
loadTrack(osu.audioFilename, osu.previewTime, loop);
|
||||||
//convertMp3(osu.audioFilename);
|
|
||||||
//if (!Thread.currentThread().isInterrupted())
|
|
||||||
// loadTrack(wavFile, osu.previewTime, loop);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
trackLoader.start();
|
trackLoader.start();
|
||||||
|
@ -339,15 +334,18 @@ public class MusicController {
|
||||||
|
|
||||||
// TODO: properly interrupt instead of using deprecated Thread.stop();
|
// TODO: properly interrupt instead of using deprecated Thread.stop();
|
||||||
// interrupt the conversion/track loading
|
// interrupt the conversion/track loading
|
||||||
if (isTrackLoading())
|
// Not sure if the interrupt does anything
|
||||||
|
// And the join kind of defeats the purpose of threading it.
|
||||||
|
// But is needed since bad things happen when OpenALStreamPlayer source is released asynchronously I think.
|
||||||
|
if (isTrackLoading()){
|
||||||
|
//trackLoader.stop();
|
||||||
|
trackLoader.interrupt();
|
||||||
try {
|
try {
|
||||||
trackLoader.join();
|
trackLoader.join();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
// trackLoader.interrupt();
|
}
|
||||||
//trackLoader.stop();
|
|
||||||
trackLoader = null;
|
trackLoader = null;
|
||||||
|
|
||||||
// delete temporary WAV file
|
// delete temporary WAV file
|
||||||
|
@ -402,9 +400,8 @@ public class MusicController {
|
||||||
AL10.alDeleteSources(buf);
|
AL10.alDeleteSources(buf);
|
||||||
int exc = AL10.alGetError();
|
int exc = AL10.alGetError();
|
||||||
if (exc != AL10.AL_NO_ERROR) {
|
if (exc != AL10.AL_NO_ERROR) {
|
||||||
//Seems It can't delete mp3 source?
|
throw new SlickException(
|
||||||
//throw new SlickException(
|
"Could not clear SoundStore sources, err: " + exc);
|
||||||
// "Could not clear SoundStore sources, err: " + exc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete any buffer data stored in memory, too...
|
// delete any buffer data stored in memory, too...
|
||||||
|
|
|
@ -283,9 +283,9 @@ public class Music {
|
||||||
if (volume > 1.0f)
|
if (volume > 1.0f)
|
||||||
volume = 1.0f;
|
volume = 1.0f;
|
||||||
|
|
||||||
sound.playAsMusic(pitch, volume, loop);
|
|
||||||
playing = true;
|
playing = true;
|
||||||
currentMusic = this;
|
currentMusic = this;
|
||||||
|
sound.playAsMusic(pitch, volume, loop);
|
||||||
setVolume(volume);
|
setVolume(volume);
|
||||||
if (requiredPosition != -1) {
|
if (requiredPosition != -1) {
|
||||||
setPosition(requiredPosition);
|
setPosition(requiredPosition);
|
||||||
|
|
|
@ -19,11 +19,8 @@ import java.net.URL;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
import javax.sound.midi.SysexMessage;
|
|
||||||
|
|
||||||
import org.lwjgl.BufferUtils;
|
import org.lwjgl.BufferUtils;
|
||||||
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;
|
||||||
|
@ -38,7 +35,7 @@ import org.newdawn.slick.util.ResourceLoader;
|
||||||
*/
|
*/
|
||||||
public class OpenALStreamPlayer {
|
public class OpenALStreamPlayer {
|
||||||
/** The number of buffers to maintain */
|
/** The number of buffers to maintain */
|
||||||
public static final int BUFFER_COUNT = 9;
|
public static final int BUFFER_COUNT = 20;
|
||||||
/** The size of the sections to stream from the stream */
|
/** The size of the sections to stream from the stream */
|
||||||
private static final int sectionSize = 4096;
|
private static final int sectionSize = 4096;
|
||||||
|
|
||||||
|
@ -151,20 +148,15 @@ public class OpenALStreamPlayer {
|
||||||
* Clean up the buffers applied to the sound source
|
* Clean up the buffers applied to the sound source
|
||||||
*/
|
*/
|
||||||
private synchronized void removeBuffers() {
|
private synchronized void removeBuffers() {
|
||||||
|
AL10.alSourceStop(source);
|
||||||
IntBuffer buffer = BufferUtils.createIntBuffer(1);
|
IntBuffer buffer = BufferUtils.createIntBuffer(1);
|
||||||
//int queued = AL10.alGetSourcei(source, AL10.AL_BUFFERS_QUEUED);
|
|
||||||
|
|
||||||
/*while (queued > 0)
|
|
||||||
{
|
|
||||||
AL10.alSourceUnqueueBuffers(source, buffer);
|
|
||||||
queued--;
|
|
||||||
}/*/
|
|
||||||
|
|
||||||
while (AL10.alGetSourcei(source, AL10.AL_BUFFERS_QUEUED) > 0)
|
while (AL10.alGetSourcei(source, AL10.AL_BUFFERS_QUEUED) > 0)
|
||||||
{
|
{
|
||||||
AL10.alSourceUnqueueBuffers(source, buffer);
|
AL10.alSourceUnqueueBuffers(source, buffer);
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
}//*/
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,17 +167,14 @@ public class OpenALStreamPlayer {
|
||||||
* @throws IOException Indicates a failure to read from the stream
|
* @throws IOException Indicates a failure to read from the stream
|
||||||
*/
|
*/
|
||||||
public void play(boolean loop) throws IOException {
|
public void play(boolean loop) throws IOException {
|
||||||
//System.out.println("play "+loop);
|
|
||||||
this.loop = loop;
|
this.loop = loop;
|
||||||
initStreams();
|
initStreams();
|
||||||
|
|
||||||
done = false;
|
done = false;
|
||||||
|
|
||||||
AL10.alSourceStop(source);
|
AL10.alSourceStop(source);
|
||||||
//removeBuffers();
|
|
||||||
|
|
||||||
startPlayback();
|
startPlayback();
|
||||||
//AL10.alSourcePlay(source);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -225,32 +214,30 @@ public class OpenALStreamPlayer {
|
||||||
|
|
||||||
int bufferIndex = unqueued.get(0);
|
int bufferIndex = unqueued.get(0);
|
||||||
|
|
||||||
//float bufferLength = (AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE) / sampleSize) / sampleRate;
|
|
||||||
int bufferLength = AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE);
|
int bufferLength = AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE);
|
||||||
//positionOffset += bufferLength;
|
|
||||||
playedPos += bufferLength;
|
playedPos += bufferLength;
|
||||||
|
lastUpdateTime = System.currentTimeMillis();
|
||||||
|
|
||||||
if(musicLength>0 && playedPos>musicLength)
|
if(musicLength>0 && playedPos>musicLength)
|
||||||
playedPos -= musicLength;
|
playedPos -= musicLength;
|
||||||
|
|
||||||
unqueued.clear();
|
if (stream(bufferIndex)) {
|
||||||
unqueued.put(bufferIndex);
|
AL10.alSourceQueueBuffers(source, unqueued);
|
||||||
unqueued.flip();
|
} else {
|
||||||
if (stream(bufferIndex)) {
|
remainingBufferCount--;
|
||||||
AL10.alSourceQueueBuffers(source, unqueued);
|
if (remainingBufferCount == 0) {
|
||||||
} else {
|
done = true;
|
||||||
remainingBufferCount--;
|
}
|
||||||
if (remainingBufferCount == 0) {
|
}
|
||||||
done = true;
|
processed--;
|
||||||
}
|
|
||||||
}
|
|
||||||
processed--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int state = AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE);
|
int state = AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE);
|
||||||
|
|
||||||
if (state != AL10.AL_PLAYING) {
|
if (state != AL10.AL_PLAYING) {
|
||||||
AL10.alSourcePlay(source);
|
AL10.alSourcePlay(source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -260,13 +247,11 @@ public class OpenALStreamPlayer {
|
||||||
* @return True if another section was available
|
* @return True if another section was available
|
||||||
*/
|
*/
|
||||||
public synchronized boolean stream(int bufferId) {
|
public synchronized boolean stream(int bufferId) {
|
||||||
//Thread.dumpStack();
|
|
||||||
try {
|
try {
|
||||||
int count = audio.read(buffer);
|
int count = audio.read(buffer);
|
||||||
if (count != -1) {
|
if (count != -1) {
|
||||||
lastUpdateTime = System.currentTimeMillis();
|
|
||||||
streamPos += count;
|
streamPos += count;
|
||||||
//bufferData = BufferUtils.createByteBuffer(sectionSize);
|
|
||||||
bufferData.clear();
|
bufferData.clear();
|
||||||
bufferData.put(buffer,0,count);
|
bufferData.put(buffer,0,count);
|
||||||
bufferData.flip();
|
bufferData.flip();
|
||||||
|
@ -291,7 +276,6 @@ public class OpenALStreamPlayer {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
Log.error(e);
|
Log.error(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -305,23 +289,20 @@ public class OpenALStreamPlayer {
|
||||||
*/
|
*/
|
||||||
public synchronized boolean setPosition(float position) {
|
public synchronized boolean setPosition(float position) {
|
||||||
try {
|
try {
|
||||||
//int state = AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE);
|
|
||||||
//AL10.alSourceStop(source);
|
|
||||||
|
|
||||||
long samplePos = (long) (position*sampleRate)*sampleSize;
|
long samplePos = (long) (position*sampleRate)*sampleSize;
|
||||||
|
|
||||||
//System.out.println("offset:"+samplePos%sampleSize);
|
if(streamPos > samplePos){
|
||||||
if(streamPos > samplePos){//(getPosition() > position) {
|
|
||||||
initStreams();
|
initStreams();
|
||||||
}
|
}
|
||||||
//if(audio instanceof Mp3InputStream){
|
|
||||||
long skiped = audio.skip(samplePos - streamPos);
|
long skiped = audio.skip(samplePos - streamPos);
|
||||||
if(skiped>=0)
|
if(skiped>=0)
|
||||||
streamPos+=skiped;
|
streamPos+=skiped;
|
||||||
else{
|
else{
|
||||||
System.out.println("Failed to skip?");
|
System.out.println("Failed to skip?");
|
||||||
}
|
}
|
||||||
//}
|
|
||||||
while(streamPos+buffer.length < samplePos){
|
while(streamPos+buffer.length < samplePos){
|
||||||
int count = audio.read(buffer);
|
int count = audio.read(buffer);
|
||||||
if (count != -1) {
|
if (count != -1) {
|
||||||
|
@ -335,18 +316,10 @@ public class OpenALStreamPlayer {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//System.out.println("offset2:"+samplePos%sampleSize);
|
|
||||||
|
|
||||||
/*while(streamPos%sampleSize!=0){
|
|
||||||
audio.read();
|
|
||||||
streamPos++;
|
|
||||||
}*/
|
|
||||||
playedPos = streamPos;
|
playedPos = streamPos;
|
||||||
|
|
||||||
startPlayback();
|
startPlayback();
|
||||||
//if (state != AL10.AL_PLAYING) {
|
|
||||||
// AL10.alSourcePlay(source);
|
|
||||||
//}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -371,6 +344,8 @@ public class OpenALStreamPlayer {
|
||||||
|
|
||||||
AL10.alSourceQueueBuffers(source, bufferNames);
|
AL10.alSourceQueueBuffers(source, bufferNames);
|
||||||
AL10.alSourcePlay(source);
|
AL10.alSourcePlay(source);
|
||||||
|
lastUpdateTime = System.currentTimeMillis();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -379,20 +354,20 @@ public class OpenALStreamPlayer {
|
||||||
* @return The current position in seconds.
|
* @return The current position in seconds.
|
||||||
*/
|
*/
|
||||||
public float getPosition() {
|
public float getPosition() {
|
||||||
float time = ((float)playedPos/(float)sampleSize)/(float)sampleRate;
|
float playedTime = ((float)playedPos/(float)sampleSize)/(float)sampleRate;
|
||||||
float timePosition = time + (System.currentTimeMillis()-lastUpdateTime)/1000f;
|
float timePosition = playedTime
|
||||||
//System.out.println(playedPos +" "+streamPos+" "+AL10.alGetSourcef(source, AL11.AL_SAMPLE_OFFSET)+" "+System.currentTimeMillis()+" "+time+" "+sampleRate+" "+sampleSize+" "+timePosition);
|
+ (System.currentTimeMillis()-lastUpdateTime)/1000f;
|
||||||
return timePosition;//AL10.alGetSourcef(source, AL11.AL_SEC_OFFSET);
|
//+ AL10.alGetSourcef(source, AL11.AL_SEC_OFFSET);
|
||||||
|
return timePosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long offsetTime = 0;
|
||||||
public void pausing() {
|
public void pausing() {
|
||||||
//System.out.println("Pasuing ");
|
offsetTime = System.currentTimeMillis()-lastUpdateTime;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resuming() {
|
public void resuming() {
|
||||||
//System.out.println("Resuming ");
|
lastUpdateTime = System.currentTimeMillis()-offsetTime;
|
||||||
lastUpdateTime = System.currentTimeMillis();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user