Skip to content

Commit

Permalink
improved buffering (should not desync as much)
Browse files Browse the repository at this point in the history
  • Loading branch information
seannorris committed Feb 3, 2021
1 parent bc1613a commit a9f54ff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions src/main/java/AsciiConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

public class AsciiConverter
{
private static SourceDataLine line;
private static final Object soundSync = new Object();
private static boolean audioWait = true;

public static void main(String[] args) throws IOException, JCodecException, InterruptedException, LineUnavailableException
{
if(args.length < 1)
Expand Down Expand Up @@ -66,27 +70,25 @@ public static void main(String[] args) throws IOException, JCodecException, Inte
var audioDecodedMeta = aacDecoder.getCodecMeta(prevAudioFrame[0].getData());
var audioFormat = new AudioFormat(audioDecodedMeta.getSampleRate(), audioDecodedMeta.getSampleSize() * 8, audioDecodedMeta.getChannelCount(), true, false);
var audioInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
var line = (SourceDataLine)AudioSystem.getLine(audioInfo);
line = (SourceDataLine)AudioSystem.getLine(audioInfo);
line.open();

var sync = new Object();


var byteBuffer = ByteBuffer.allocate(audioMeta.getBytesPerFrame());
new Thread(() ->
{
try
{
var wait = true;
do
{
var audioBuffer = aacDecoder.decodeFrame(prevAudioFrame[0].getData(), byteBuffer).getData().array();
line.write(audioBuffer, 0, audioBuffer.length);
if(wait)
if(audioWait)
{
wait = false;
synchronized(sync)
audioWait = false;
synchronized(soundSync)
{
sync.wait();
soundSync.wait();
}
}
}
Expand Down Expand Up @@ -115,9 +117,9 @@ public static void main(String[] args) throws IOException, JCodecException, Inte
buffer.fill();

line.start();
synchronized(sync)
synchronized(soundSync)
{
sync.notify();
soundSync.notify();
}

System.out.print("\u001b[2J");
Expand Down Expand Up @@ -173,7 +175,7 @@ public String call()
private static class Buffer implements Runnable
{
private final FrameGrab frameGrab;
private final String[] buffer;
private String[] buffer;
private final Thread thread;

private final int regionWidth;
Expand Down Expand Up @@ -257,10 +259,31 @@ private void addToArray(String input) throws InterruptedException
size++;
}

private boolean buffering = false;
public String nextFrame()
{
if(size <= 0)
return "\u001b[Hbuffering...";//fill();
if(buffering || size <= 0)
{
if(!buffering)
{
buffering = true;
line.stop();
audioWait = true;
var oldBuffer = buffer;
buffer = new String[buffer.length * 2];
for(int x = 0, size = this.size; x < size && buffer[x] == null; x++)
buffer[x] = oldBuffer[x];
}
if(size < buffer.length)
return "\u001b[Hbuffering...";//fill();

buffering = false;
line.start();
synchronized(soundSync)
{
soundSync.notify();
}
}

var out = buffer[tail];
buffer[tail] = null;
Expand Down
Binary file modified target/HackTheBubble2020-1.0.jar
Binary file not shown.

0 comments on commit a9f54ff

Please sign in to comment.