Add mute switch to audio sink.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 26 May 2013 11:58:56 +0000 (13:58 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 May 2013 20:54:38 +0000 (22:54 +0200)
src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java

index 72442f5..720b919 100644 (file)
@@ -34,6 +34,7 @@ import net.pterodactylus.sonitus.data.Metadata;
 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 com.google.common.base.Preconditions;
 
@@ -51,6 +52,9 @@ public class AudioSink implements Sink {
        /** The volume fader. */
        private final Fader volumeFader;
 
+       /** The “mute” switch. */
+       private final Switch muteSwitch;
+
        /** The current metadata. */
        private Metadata metadata;
 
@@ -70,6 +74,23 @@ public class AudioSink implements Sink {
                                }
                        }
                };
+               muteSwitch = new Switch() {
+
+                       private float previousValue;
+
+                       @Override
+                       protected void valueSet(int value) {
+                               if (sourceDataLine != null) {
+                                       FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
+                                       if (value == 1) {
+                                               previousValue = volumeControl.getValue();
+                                               volumeControl.setValue(0);
+                                       } else {
+                                               volumeControl.setValue(previousValue);
+                                       }
+                               }
+                       }
+               };
        }
 
        //
@@ -78,7 +99,7 @@ public class AudioSink implements Sink {
 
        @Override
        public List<Controller> controllers() {
-               return Arrays.<Controller>asList(volumeFader);
+               return Arrays.<Controller>asList(volumeFader, muteSwitch);
        }
 
        //