From: David ‘Bombe’ Roden Date: Sun, 26 May 2013 16:01:07 +0000 (+0200) Subject: Ensure that we always write multiples-of-1024 byte blocks. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=d376b34947d8755b815369e5d2eedd1e0555c371;p=sonitus.git Ensure that we always write multiples-of-1024 byte blocks. --- 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)); }