Don’t connect sources and sinks directly, use a pipeline to move data around.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / sink / AudioSink.java
index f29bde8..689c23d 100644 (file)
 
 package net.pterodactylus.sonitus.data.sink;
 
-import static com.google.common.base.Preconditions.*;
-
+import java.io.IOException;
 import java.util.logging.Logger;
 import javax.sound.sampled.AudioFormat;
 import javax.sound.sampled.AudioSystem;
 import javax.sound.sampled.LineUnavailableException;
 import javax.sound.sampled.SourceDataLine;
 
-import net.pterodactylus.sonitus.data.ConnectException;
-import net.pterodactylus.sonitus.data.Connection;
 import net.pterodactylus.sonitus.data.Metadata;
 import net.pterodactylus.sonitus.data.Sink;
-import net.pterodactylus.sonitus.data.Source;
+
+import com.google.common.base.Preconditions;
 
 /**
- * {@link Sink} implementation that uses the JDK’s {@link
- * javax.sound.sampled.AudioSystem} to play all {@link net.pterodactylus.sonitus.data.Source}s.
+ * {@link net.pterodactylus.sonitus.data.Sink} implementation that uses the
+ * JDK’s {@link javax.sound.sampled.AudioSystem} to play all {@link
+ * net.pterodactylus.sonitus.data.Source}s.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
@@ -42,47 +41,41 @@ public class AudioSink implements Sink {
        /** The logger. */
        private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
 
-       /** The current source. */
-       private Source source;
+       /** The current metadata. */
+       private Metadata metadata;
 
-       @Override
-       public void connect(Source source) throws ConnectException {
-               this.source = checkNotNull(source, "source must not be null");
-               checkState(source.metadata().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
+       /** The audio output. */
+       private SourceDataLine sourceDataLine;
 
-               final Metadata sourceMetadata = source.metadata();
-               AudioFormat audioFormat = new AudioFormat(sourceMetadata.frequency(), 16, sourceMetadata.channels(), true, false);
+       @Override
+       public void open(Metadata metadata) throws IOException {
+               Preconditions.checkArgument(metadata.encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
+               AudioFormat audioFormat = new AudioFormat(metadata.frequency(), 16, metadata.channels(), true, false);
                try {
-                       final SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
+                       sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
                        sourceDataLine.open(audioFormat);
                        sourceDataLine.start();
-                       new Thread(new Connection(source) {
-
-                               @Override
-                               protected int bufferSize() {
-                                       return sourceMetadata.channels() * sourceMetadata.frequency() * 2;
-                               }
-
-                               @Override
-                               protected void feed(byte[] buffer) {
-                                       sourceDataLine.write(buffer, 0, buffer.length);
-                                       logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));
-                               }
-
-                               @Override
-                               protected void finish() {
-                                       sourceDataLine.stop();
-                               }
-                       }).start();
-                       metadataUpdated();
-               } catch (LineUnavailableException lue1) {
-                       throw new ConnectException(lue1);
+               } catch (LineUnavailableException e) {
+                       /* TODO */
+                       throw new IOException(e);
                }
        }
 
        @Override
-       public void metadataUpdated() {
+       public void close() {
+               sourceDataLine.stop();
+               sourceDataLine.close();
+       }
+
+       @Override
+       public void metadataUpdated(Metadata metadata) {
                /* ignore. */
        }
 
+       @Override
+       public void process(byte[] buffer) {
+               sourceDataLine.write(buffer, 0, buffer.length);
+               logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));
+       }
+
 }