Compare commits

...

3 Commits

  1. 57
      src/main/java/moe/nekojimi/chords/MusicHandler.java
  2. 149
      src/main/java/moe/nekojimi/chords/TrackPlayer.java

@ -95,6 +95,11 @@ 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);
@ -108,6 +113,7 @@ public class MusicHandler implements AudioSendHandler, Closeable
arrayErr = false; arrayErr = false;
byteCount = 3840; byteCount = 3840;
player = new TrackPlayer(currentSong); player = new TrackPlayer(currentSong);
player.start();
// System.out.println("Queue filled to " + audioBuffer.getCurrentNumberOfBytes()); // System.out.println("Queue filled to " + audioBuffer.getCurrentNumberOfBytes());
return true; return true;
} catch (UnsupportedAudioFileException | IOException ex) } catch (UnsupportedAudioFileException | IOException ex)
@ -178,57 +184,6 @@ 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;

@ -8,6 +8,8 @@ package moe.nekojimi.chords;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
@ -19,9 +21,10 @@ import org.apache.commons.io.input.buffer.CircularByteBuffer;
* *
* @author jimj316 * @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 DESIRED_BUFFER_SIZE = 3840 * 500;
private static final int MAX_READ_FAILS = 3; private static final int MAX_READ_FAILS = 3;
@ -30,19 +33,82 @@ public class TrackPlayer implements Closeable
private final AudioInputStream input; private final AudioInputStream input;
private boolean arrayErr = false; // supresses ArrayIndexOutOfBoundsException after the first time, to prevent spam 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 public TrackPlayer(Song song) throws UnsupportedAudioFileException, IOException
{ {
setName("TrackPlayer disk thread: " + song.toString());
AudioInputStream in = AudioSystem.getAudioInputStream(song.getLocation()); AudioInputStream in = AudioSystem.getAudioInputStream(song.getLocation());
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT; AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT;
input = AudioSystem.getAudioInputStream(decodedFormat, in); input = AudioSystem.getAudioInputStream(decodedFormat, in);
fillBuffer(false); // fillBuffer(false);
} }
public TrackPlayer(AudioInputStream input) throws IOException public TrackPlayer(AudioInputStream input) throws IOException
{ {
this.input = input; 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) public boolean has(int byteCount)
@ -53,14 +119,17 @@ public class TrackPlayer implements Closeable
public ByteBuffer read(int length) throws IOException public ByteBuffer read(int length) throws IOException
{ {
boolean filled = fillBuffer(true); checkBuffer();
// if (!filled) // if (!filled)
// throw new OutOfInputException(); // throw new OutOfInputException();
int toRead = Math.min(length, audioBuffer.getCurrentNumberOfBytes()); int toRead = Math.min(length, audioBuffer.getCurrentNumberOfBytes());
// System.out.println("To read: " + toRead + " from " + audioBuffer.getCurrentNumberOfBytes()); // System.out.println("To read: " + toRead + " from " + audioBuffer.getCurrentNumberOfBytes());
if (toRead <= 0) if (toRead <= 0)
{
ended = true;
throw new OutOfInputException(); throw new OutOfInputException();
}
byte[] data = new byte[toRead]; byte[] data = new byte[toRead];
audioBuffer.read(data, 0, data.length); 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 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 * @param canSkip
@ -80,19 +187,20 @@ public class TrackPlayer implements Closeable
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE) while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
{ {
boolean read = readData(); boolean read = readData();
if (!read && !canSkip) if (!read)
return false; {
else if (fails < MAX_READ_FAILS) if (canSkip && fails < MAX_READ_FAILS)
fails++; fails++;
else else
return false; return false;
}
} }
return true; 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 private boolean readData() throws IOException
{ {
@ -102,23 +210,32 @@ public class TrackPlayer implements Closeable
{ {
// if (din.available() == 0) // if (din.available() == 0)
// return false; // return false;
int bytesToRead = DESIRED_BUFFER_SIZE - audioBuffer.getCurrentNumberOfBytes(); int bytesToRead;
int space = audioBuffer.getSpace(); int space;
synchronized (audioBuffer)
{
bytesToRead = DESIRED_BUFFER_SIZE - audioBuffer.getCurrentNumberOfBytes();
space = audioBuffer.getSpace();
}
if (input.available() > 0 && input.available() < bytesToRead) if (input.available() > 0 && input.available() < bytesToRead)
bytesToRead = input.available(); bytesToRead = input.available();
if (bytesToRead > space) if (bytesToRead > space)
bytesToRead = space; bytesToRead = space;
if (bytesToRead == 0) if (bytesToRead == 0)
return false; return true;
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);
// 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) if (read < 0)
return false; return false;
// queue.add(bytes); // queue.add(bytes);
audioBuffer.add(bytes, 0, read); synchronized (audioBuffer)
{
audioBuffer.add(bytes, 0, read);
}
// System.out.println("SAMPLES player buff: " + Util.printSamples(audioBuffer)); // System.out.println("SAMPLES player buff: " + Util.printSamples(audioBuffer));
} catch (ArrayIndexOutOfBoundsException ex) } catch (ArrayIndexOutOfBoundsException ex)
{ {

Loading…
Cancel
Save