+ /** The event bus. */
+ private final EventBus eventBus;
+
+ /** The volume fader. */
+ private final Fader volumeFader;
+
+ /** The “mute” switch. */
+ private final Switch muteSwitch;
+
+ /** The current metadata. */
+ private Metadata metadata;
+
+ /** 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.
+ *
+ * @param eventBus
+ * The event bus
+ */
+ public AudioSink(EventBus eventBus) {
+ this.eventBus = eventBus;
+ volumeFader = new Fader("Volume") {
+
+ @Override
+ protected void valueSet(Double value) {
+ /* search for preferred volume control. */
+ FloatControl volumeControl = getVolumeControl(sourceDataLine);
+ if (volumeControl == null) {
+ /* could not find volume control! */
+ return;
+ }
+
+ volumeControl.setValue((float) (value * volumeControl.getMaximum()));
+ }
+ };
+ muteSwitch = new Switch("Mute") {
+
+ /** The previous value in case we have to emulate the mute control. */
+ private float previousValue;
+
+ @Override
+ protected void valueSet(Boolean value) {
+ /* search for mute control. */
+ BooleanControl muteControl = getMuteControl(sourceDataLine);
+ if (muteControl != null) {
+ muteControl.setValue(value);
+ return;
+ }
+
+ /* could not find mute control, use volume control! */
+ FloatControl volumeControl = getVolumeControl(sourceDataLine);
+ if (volumeControl == null) {
+ /* no volume control, either? */
+ return;
+ }
+
+ if (value) {
+ previousValue = volumeControl.getValue();
+ volumeControl.setValue(0);
+ } else {
+ volumeControl.setValue(previousValue);
+ }
+ }
+
+ };
+ }
+
+ //
+ // CONTROLLED METHODS
+ //
+