From d376b34947d8755b815369e5d2eedd1e0555c371 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 26 May 2013 18:01:07 +0200 Subject: [PATCH] Ensure that we always write multiples-of-1024 byte blocks. --- .../pterodactylus/sonitus/data/sink/AudioSink.java | 26 ++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java index 611dcd9..d95df6e 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java @@ -20,6 +20,7 @@ package net.pterodactylus.sonitus.data.sink; import static javax.sound.sampled.FloatControl.Type.VOLUME; import java.io.IOException; +import java.io.OutputStream; import java.util.Arrays; import java.util.List; import java.util.logging.Logger; @@ -35,6 +36,7 @@ import net.pterodactylus.sonitus.data.Sink; import net.pterodactylus.sonitus.data.Source; import net.pterodactylus.sonitus.data.controller.Fader; import net.pterodactylus.sonitus.data.controller.Switch; +import net.pterodactylus.sonitus.io.IntegralWriteOutputStream; import com.google.common.base.Preconditions; @@ -61,6 +63,26 @@ public class AudioSink implements Sink { /** The audio output. */ private SourceDataLine sourceDataLine; + /** A buffered output stream to ensure correct writing to the source data line. */ + private OutputStream sourceDataLineOutputStream = new IntegralWriteOutputStream(new OutputStream() { + + @Override + public void write(int b) throws IOException { + } + + @Override + public void write(byte[] b) throws IOException { + write(b, 0, b.length); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + if (sourceDataLine != null) { + sourceDataLine.write(b, off, len); + } + } + }, 1024); + /** Creates a new audio sink. */ public AudioSink() { super(); @@ -132,8 +154,8 @@ public class AudioSink implements Sink { } @Override - public void process(byte[] buffer) { - sourceDataLine.write(buffer, 0, buffer.length); + public void process(byte[] buffer) throws IOException { + sourceDataLineOutputStream.write(buffer); logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length)); } -- 2.7.4