Send metadata updates events when the metadata changes.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / sink / AudioSink.java
index fdcbbd6..b0f0067 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,8 +36,11 @@ 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.data.event.MetadataUpdated;
+import net.pterodactylus.sonitus.io.IntegralWriteOutputStream;
 
 import com.google.common.base.Preconditions;
+import com.google.common.eventbus.EventBus;
 
 /**
  * {@link Sink} implementation that uses the JDK’s {@link AudioSystem} to play
@@ -49,6 +53,9 @@ public class AudioSink implements Sink {
        /** The logger. */
        private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
 
+       /** The event bus. */
+       private final EventBus eventBus;
+
        /** The volume fader. */
        private final Fader volumeFader;
 
@@ -61,10 +68,35 @@ public class AudioSink implements Sink {
        /** The audio output. */
        private SourceDataLine sourceDataLine;
 
-       /** Creates a new audio sink. */
-       public AudioSink() {
-               super();
-               volumeFader = new Fader() {
+       /** 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.
+        *
+        * @param eventBus
+        *              The event bus
+        */
+       public AudioSink(EventBus eventBus) {
+               this.eventBus = eventBus;
+               volumeFader = new Fader("Volume") {
 
                        @Override
                        protected void valueSet(Double value) {
@@ -74,7 +106,7 @@ public class AudioSink implements Sink {
                                }
                        }
                };
-               muteSwitch = new Switch() {
+               muteSwitch = new Switch("Mute") {
 
                        private float previousValue;
 
@@ -98,6 +130,16 @@ public class AudioSink implements Sink {
        //
 
        @Override
+       public String name() {
+               return "Audio Output";
+       }
+
+       @Override
+       public Metadata metadata() {
+               return metadata;
+       }
+
+       @Override
        public List<Controller<?>> controllers() {
                return Arrays.<Controller<?>>asList(volumeFader, muteSwitch);
        }
@@ -114,6 +156,7 @@ public class AudioSink implements Sink {
                        sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
                        sourceDataLine.open(audioFormat);
                        sourceDataLine.start();
+                       metadataUpdated(metadata);
                } catch (LineUnavailableException e) {
                        /* TODO */
                        throw new IOException(e);
@@ -129,11 +172,13 @@ public class AudioSink implements Sink {
        @Override
        public void metadataUpdated(Metadata metadata) {
                logger.info(String.format("Now playing %s.", metadata));
+               this.metadata = metadata;
+               eventBus.post(new MetadataUpdated(this, metadata));
        }
 
        @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));
        }