Ensure that we always write multiples-of-1024 byte blocks.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 26 May 2013 16:01:07 +0000 (18:01 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 May 2013 20:54:39 +0000 (22:54 +0200)
src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java

index 611dcd9..d95df6e 100644 (file)
@@ -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));
        }