|
|
|
@ -8,6 +8,8 @@ package moe.nekojimi.chords; |
|
|
|
|
import java.io.Closeable; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.nio.ByteBuffer; |
|
|
|
|
import java.util.logging.Level; |
|
|
|
|
import java.util.logging.Logger; |
|
|
|
|
import javax.sound.sampled.AudioFormat; |
|
|
|
|
import javax.sound.sampled.AudioInputStream; |
|
|
|
|
import javax.sound.sampled.AudioSystem; |
|
|
|
@ -19,9 +21,10 @@ import org.apache.commons.io.input.buffer.CircularByteBuffer; |
|
|
|
|
* |
|
|
|
|
* @author jimj316 |
|
|
|
|
*/ |
|
|
|
|
public class TrackPlayer implements Closeable |
|
|
|
|
public class TrackPlayer extends Thread implements Closeable |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
private static final boolean DEBUG_PRINT = false; |
|
|
|
|
private static final int DESIRED_BUFFER_SIZE = 3840 * 500; |
|
|
|
|
private static final int MAX_READ_FAILS = 3; |
|
|
|
|
|
|
|
|
@ -30,19 +33,82 @@ public class TrackPlayer implements Closeable |
|
|
|
|
private final AudioInputStream input; |
|
|
|
|
|
|
|
|
|
private boolean arrayErr = false; // supresses ArrayIndexOutOfBoundsException after the first time, to prevent spam
|
|
|
|
|
private boolean ended = false; |
|
|
|
|
|
|
|
|
|
private final Object fillBufferWait = new Object(); |
|
|
|
|
private final Object bufferFilledWait = new Object(); |
|
|
|
|
|
|
|
|
|
public TrackPlayer(Song song) throws UnsupportedAudioFileException, IOException |
|
|
|
|
{ |
|
|
|
|
setName("TrackPlayer disk thread: " + song.toString()); |
|
|
|
|
AudioInputStream in = AudioSystem.getAudioInputStream(song.getLocation()); |
|
|
|
|
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT; |
|
|
|
|
input = AudioSystem.getAudioInputStream(decodedFormat, in); |
|
|
|
|
fillBuffer(false); |
|
|
|
|
// fillBuffer(false);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public TrackPlayer(AudioInputStream input) throws IOException |
|
|
|
|
{ |
|
|
|
|
this.input = input; |
|
|
|
|
fillBuffer(false); |
|
|
|
|
// fillBuffer(false);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void end() |
|
|
|
|
{ |
|
|
|
|
ended = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() |
|
|
|
|
{ |
|
|
|
|
while (!ended) |
|
|
|
|
{ |
|
|
|
|
int bytes; |
|
|
|
|
synchronized (audioBuffer) |
|
|
|
|
{ |
|
|
|
|
bytes = audioBuffer.getCurrentNumberOfBytes(); |
|
|
|
|
} |
|
|
|
|
if (bytes >= DESIRED_BUFFER_SIZE) |
|
|
|
|
{ |
|
|
|
|
synchronized (fillBufferWait) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("DISK THREAD: waiting"); |
|
|
|
|
fillBufferWait.wait(5000); |
|
|
|
|
} catch (InterruptedException ex) |
|
|
|
|
{ |
|
|
|
|
// this is normal
|
|
|
|
|
} |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("DISK THREAD: kicked"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
boolean notAtEnd = fillBuffer(true); |
|
|
|
|
if (!notAtEnd) |
|
|
|
|
{ |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("DISK THREAD: end of input"); |
|
|
|
|
ended = true; |
|
|
|
|
} |
|
|
|
|
synchronized (bufferFilledWait) |
|
|
|
|
{ |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("DISK THREAD: buffer filled; kicking read thread"); |
|
|
|
|
bufferFilledWait.notifyAll(); |
|
|
|
|
} |
|
|
|
|
} catch (IOException ex) |
|
|
|
|
{ |
|
|
|
|
Logger.getLogger(TrackPlayer.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
|
ended = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("DISK THREAD ENDED"); |
|
|
|
|
bufferFilledWait.notifyAll(); // kick read thread in case it was waiting for us
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean has(int byteCount) |
|
|
|
@ -53,14 +119,17 @@ public class TrackPlayer implements Closeable |
|
|
|
|
|
|
|
|
|
public ByteBuffer read(int length) throws IOException |
|
|
|
|
{ |
|
|
|
|
boolean filled = fillBuffer(true); |
|
|
|
|
checkBuffer(); |
|
|
|
|
// if (!filled)
|
|
|
|
|
// throw new OutOfInputException();
|
|
|
|
|
|
|
|
|
|
int toRead = Math.min(length, audioBuffer.getCurrentNumberOfBytes()); |
|
|
|
|
// System.out.println("To read: " + toRead + " from " + audioBuffer.getCurrentNumberOfBytes());
|
|
|
|
|
if (toRead <= 0) |
|
|
|
|
{ |
|
|
|
|
ended = true; |
|
|
|
|
throw new OutOfInputException(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
byte[] data = new byte[toRead]; |
|
|
|
|
audioBuffer.read(data, 0, data.length); |
|
|
|
@ -68,6 +137,44 @@ public class TrackPlayer implements Closeable |
|
|
|
|
return ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean checkBuffer() |
|
|
|
|
{ |
|
|
|
|
synchronized (fillBufferWait) |
|
|
|
|
{ |
|
|
|
|
if (!ended) |
|
|
|
|
{ |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("READ THREAD: kicking disk thread"); |
|
|
|
|
fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
int bytes; |
|
|
|
|
synchronized (audioBuffer) |
|
|
|
|
{ |
|
|
|
|
bytes = audioBuffer.getCurrentNumberOfBytes(); |
|
|
|
|
} |
|
|
|
|
if (bytes == 0 && !ended) |
|
|
|
|
{ |
|
|
|
|
synchronized (bufferFilledWait) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
System.out.println("READ THREAD: waiting for disk thread"); |
|
|
|
|
bufferFilledWait.wait(5000); // wait for disk thread to fill the buffer
|
|
|
|
|
} catch (InterruptedException ex) |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("READ THREAD: kicked"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
synchronized (audioBuffer) |
|
|
|
|
{ |
|
|
|
|
bytes = audioBuffer.getCurrentNumberOfBytes(); |
|
|
|
|
} |
|
|
|
|
return bytes > 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
* @param canSkip |
|
|
|
@ -80,19 +187,20 @@ public class TrackPlayer implements Closeable |
|
|
|
|
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE) |
|
|
|
|
{ |
|
|
|
|
boolean read = readData(); |
|
|
|
|
if (!read && !canSkip) |
|
|
|
|
return false; |
|
|
|
|
else if (fails < MAX_READ_FAILS) |
|
|
|
|
if (!read) |
|
|
|
|
{ |
|
|
|
|
if (canSkip && fails < MAX_READ_FAILS) |
|
|
|
|
fails++; |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
* @return true if any data was read; false otherwise |
|
|
|
|
* @return true if there is still data to be read |
|
|
|
|
*/ |
|
|
|
|
private boolean readData() throws IOException |
|
|
|
|
{ |
|
|
|
@ -102,23 +210,32 @@ public class TrackPlayer implements Closeable |
|
|
|
|
{ |
|
|
|
|
// if (din.available() == 0)
|
|
|
|
|
// return false;
|
|
|
|
|
int bytesToRead = DESIRED_BUFFER_SIZE - audioBuffer.getCurrentNumberOfBytes(); |
|
|
|
|
int space = audioBuffer.getSpace(); |
|
|
|
|
int bytesToRead; |
|
|
|
|
int space; |
|
|
|
|
synchronized (audioBuffer) |
|
|
|
|
{ |
|
|
|
|
bytesToRead = DESIRED_BUFFER_SIZE - audioBuffer.getCurrentNumberOfBytes(); |
|
|
|
|
space = audioBuffer.getSpace(); |
|
|
|
|
} |
|
|
|
|
if (input.available() > 0 && input.available() < bytesToRead) |
|
|
|
|
bytesToRead = input.available(); |
|
|
|
|
if (bytesToRead > space) |
|
|
|
|
bytesToRead = space; |
|
|
|
|
if (bytesToRead == 0) |
|
|
|
|
return false; |
|
|
|
|
return true; |
|
|
|
|
byte[] bytes = new byte[bytesToRead]; |
|
|
|
|
// byte[] bytes = din.readNBytes(bytesToRead);
|
|
|
|
|
int read = input.read(bytes); |
|
|
|
|
// System.out.println("Wanted: " + byteCount + " Space:" + space + " Available: " + din.available() + " To read: " + bytesToRead + " Read: " + read);
|
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read); |
|
|
|
|
if (read < 0) |
|
|
|
|
return false; |
|
|
|
|
// queue.add(bytes);
|
|
|
|
|
|
|
|
|
|
synchronized (audioBuffer) |
|
|
|
|
{ |
|
|
|
|
audioBuffer.add(bytes, 0, read); |
|
|
|
|
} |
|
|
|
|
// System.out.println("SAMPLES player buff: " + Util.printSamples(audioBuffer));
|
|
|
|
|
} catch (ArrayIndexOutOfBoundsException ex) |
|
|
|
|
{ |
|
|
|
|