WIP: initial version of multithreaded audio playback.

multithread-playback
Nekojimi 3 years ago
parent f6fde8d694
commit f8139bd4f6
  1. 1
      src/main/java/moe/nekojimi/chords/MusicHandler.java
  2. 118
      src/main/java/moe/nekojimi/chords/TrackPlayer.java

@ -108,6 +108,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)

@ -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,7 +21,7 @@ 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 int DESIRED_BUFFER_SIZE = 3840 * 500; private static final int DESIRED_BUFFER_SIZE = 3840 * 500;
@ -30,19 +32,73 @@ 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
{
System.out.println("DISK THREAD: waiting");
fillBufferWait.wait();
} catch (InterruptedException ex)
{
// this is normal
}
System.out.println("DISK THREAD: kicked");
}
}
try
{
boolean notAtEnd = fillBuffer(true);
if (!notAtEnd)
ended = true;
synchronized (bufferFilledWait)
{
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;
}
}
System.out.println("DISK THREAD ENDED");
} }
public boolean has(int byteCount) public boolean has(int byteCount)
@ -53,14 +109,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 +127,39 @@ 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)
{
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)
{
synchronized (bufferFilledWait)
{
try
{
System.out.println("READ THREAD: waiting for disk thread");
bufferFilledWait.wait(); // wait for disk thread to fill the buffer
} catch (InterruptedException ex)
{
}
System.out.println("READ THREAD: kicked");
}
}
synchronized (audioBuffer)
{
bytes = audioBuffer.getCurrentNumberOfBytes();
}
return bytes > 0;
}
/** /**
* *
* @param canSkip * @param canSkip
@ -92,7 +184,7 @@ public class TrackPlayer implements Closeable
/** /**
* *
* @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 +194,31 @@ 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); 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);
synchronized (audioBuffer)
{
audioBuffer.add(bytes, 0, read); 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