Move format into metadata.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / sink / AudioSink.java
index 5d1ebbc..b3aa7e7 100644 (file)
@@ -27,13 +27,13 @@ import javax.sound.sampled.SourceDataLine;
 
 import net.pterodactylus.sonitus.data.ConnectException;
 import net.pterodactylus.sonitus.data.Connection;
-import net.pterodactylus.sonitus.data.Format;
+import net.pterodactylus.sonitus.data.Metadata;
 import net.pterodactylus.sonitus.data.Sink;
 import net.pterodactylus.sonitus.data.Source;
 
 /**
- * {@link Sink} implementation that uses the JDK’s {@link javax.sound.sampled.AudioSystem} to play all {@link
- * net.pterodactylus.sonitus.data.Source}s.
+ * {@link 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>
  */
@@ -45,18 +45,19 @@ public class AudioSink implements Sink {
        @Override
        public void connect(Source source) throws ConnectException {
                checkNotNull(source, "source must not be null");
-               checkState(source.format().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
+               checkState(source.metadata().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
 
-               final Format sourceFormat = source.format();
-               AudioFormat audioFormat = new AudioFormat(sourceFormat.frequency(), 16, sourceFormat.channels(), true, false);
+               final Metadata sourceMetadata = source.metadata();
+               AudioFormat audioFormat = new AudioFormat(sourceMetadata.frequency(), 16, sourceMetadata.channels(), true, false);
                try {
                        final SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
                        sourceDataLine.open(audioFormat);
                        sourceDataLine.start();
                        new Thread(new Connection(source) {
+
                                @Override
                                protected int bufferSize() {
-                                       return sourceFormat.channels() * sourceFormat.frequency() * 2;
+                                       return sourceMetadata.channels() * sourceMetadata.frequency() * 2;
                                }
 
                                @Override
@@ -64,11 +65,20 @@ public class AudioSink implements Sink {
                                        sourceDataLine.write(buffer, 0, buffer.length);
                                        logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));
                                }
+
+                               @Override
+                               protected void finish() {
+                                       sourceDataLine.stop();
+                               }
                        }).start();
-               }
-               catch (LineUnavailableException lue1) {
+               } catch (LineUnavailableException lue1) {
                        throw new ConnectException(lue1);
                }
        }
 
+       @Override
+       public void metadataUpdated() {
+               /* ignore. */
+       }
+
 }