Compare commits

..

No commits in common. "c1d58da8a45369e2d375bfa765426e48ecd6f8e6" and "f8139bd4f68ab00cb7c3a61bb026b3912c08aa26" have entirely different histories.

2 changed files with 68 additions and 39 deletions

View File

@ -95,11 +95,6 @@ public class MusicHandler implements AudioSendHandler, Closeable
currentSong.delete(); currentSong.delete();
currentSong = null; currentSong = null;
} }
if (player != null)
{
player.close();
player = null;
}
currentSong = queueManager.nextSongNeeded(); currentSong = queueManager.nextSongNeeded();
if (nowPlayingConsumer != null) if (nowPlayingConsumer != null)
nowPlayingConsumer.accept(currentSong); nowPlayingConsumer.accept(currentSong);
@ -184,6 +179,57 @@ public class MusicHandler implements AudioSendHandler, Closeable
return ret; return ret;
} }
// private void fillBuffer(boolean canSkip)
// {
// // use what we have in our buffer to send audio as PCM
// while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
// if (!readData())
// if (!canSkip || !nextSong())
// break;
// }
//
// private boolean readData()
// {
// if (din == null)
// return false;
// try
// {
// // if (din.available() == 0)
// // return false;
// int bytesToRead = DESIRED_BUFFER_SIZE - audioBuffer.getCurrentNumberOfBytes();
// int space = audioBuffer.getSpace();
// if (din.available() > 0 && din.available() < bytesToRead)
// bytesToRead = din.available();
// if (bytesToRead > space)
// bytesToRead = space;
// if (bytesToRead == 0)
// return false;
// byte[] bytes = new byte[bytesToRead];
// // byte[] bytes = din.readNBytes(bytesToRead);
// int read = din.read(bytes);
//// System.out.println("Wanted: " + byteCount + " Space:" + space + " Available: " + din.available() + " To read: " + bytesToRead + " Read: " + read);
// if (read < 0)
// return false;
//// queue.add(bytes);
//
// audioBuffer.add(bytes, 0, read);
// } catch (IOException ex)
// {
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
// return false;
// } catch (ArrayIndexOutOfBoundsException ex)
// {
// if (!arrayErr)
// arrayErr = true;
// else
// {
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
// return false;
// }
// }
// return true;
// }
public Song getCurrentSong() public Song getCurrentSong()
{ {
return currentSong; return currentSong;

View File

@ -24,7 +24,6 @@ import org.apache.commons.io.input.buffer.CircularByteBuffer;
public class TrackPlayer extends Thread 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 DESIRED_BUFFER_SIZE = 3840 * 500;
private static final int MAX_READ_FAILS = 3; private static final int MAX_READ_FAILS = 3;
@ -74,14 +73,12 @@ public class TrackPlayer extends Thread implements Closeable
{ {
try try
{ {
if (DEBUG_PRINT)
System.out.println("DISK THREAD: waiting"); System.out.println("DISK THREAD: waiting");
fillBufferWait.wait(5000); fillBufferWait.wait();
} catch (InterruptedException ex) } catch (InterruptedException ex)
{ {
// this is normal // this is normal
} }
if (DEBUG_PRINT)
System.out.println("DISK THREAD: kicked"); System.out.println("DISK THREAD: kicked");
} }
} }
@ -89,14 +86,9 @@ public class TrackPlayer extends Thread implements Closeable
{ {
boolean notAtEnd = fillBuffer(true); boolean notAtEnd = fillBuffer(true);
if (!notAtEnd) if (!notAtEnd)
{
if (DEBUG_PRINT)
System.out.println("DISK THREAD: end of input");
ended = true; ended = true;
}
synchronized (bufferFilledWait) synchronized (bufferFilledWait)
{ {
if (DEBUG_PRINT)
System.out.println("DISK THREAD: buffer filled; kicking read thread"); System.out.println("DISK THREAD: buffer filled; kicking read thread");
bufferFilledWait.notifyAll(); bufferFilledWait.notifyAll();
} }
@ -106,9 +98,7 @@ public class TrackPlayer extends Thread implements Closeable
ended = true; ended = true;
} }
} }
if (DEBUG_PRINT)
System.out.println("DISK THREAD ENDED"); System.out.println("DISK THREAD ENDED");
bufferFilledWait.notifyAll(); // kick read thread in case it was waiting for us
} }
public boolean has(int byteCount) public boolean has(int byteCount)
@ -141,30 +131,25 @@ public class TrackPlayer extends Thread implements Closeable
{ {
synchronized (fillBufferWait) synchronized (fillBufferWait)
{ {
if (!ended)
{
if (DEBUG_PRINT)
System.out.println("READ THREAD: kicking disk thread"); System.out.println("READ THREAD: kicking disk thread");
fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed
} }
}
int bytes; int bytes;
synchronized (audioBuffer) synchronized (audioBuffer)
{ {
bytes = audioBuffer.getCurrentNumberOfBytes(); bytes = audioBuffer.getCurrentNumberOfBytes();
} }
if (bytes == 0 && !ended) if (bytes == 0)
{ {
synchronized (bufferFilledWait) synchronized (bufferFilledWait)
{ {
try try
{ {
System.out.println("READ THREAD: waiting for disk thread"); System.out.println("READ THREAD: waiting for disk thread");
bufferFilledWait.wait(5000); // wait for disk thread to fill the buffer bufferFilledWait.wait(); // wait for disk thread to fill the buffer
} catch (InterruptedException ex) } catch (InterruptedException ex)
{ {
} }
if (DEBUG_PRINT)
System.out.println("READ THREAD: kicked"); System.out.println("READ THREAD: kicked");
} }
} }
@ -187,14 +172,13 @@ public class TrackPlayer extends Thread implements Closeable
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE) while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
{ {
boolean read = readData(); boolean read = readData();
if (!read) if (!read && !canSkip)
{ return false;
if (canSkip && fails < MAX_READ_FAILS) else if (fails < MAX_READ_FAILS)
fails++; fails++;
else else
return false; return false;
} }
}
return true; return true;
} }
@ -226,7 +210,6 @@ public class TrackPlayer extends Thread implements Closeable
byte[] bytes = new byte[bytesToRead]; byte[] bytes = new byte[bytesToRead];
// byte[] bytes = din.readNBytes(bytesToRead); // byte[] bytes = din.readNBytes(bytesToRead);
int read = input.read(bytes); int read = input.read(bytes);
if (DEBUG_PRINT)
System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read); System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read);
if (read < 0) if (read < 0)
return false; return false;