+ /** 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) {
+ if (sourceDataLine != null) {
+ FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
+ volumeControl.setValue((float) (value * volumeControl.getMaximum()));
+ }
+ }
+ };
+ muteSwitch = new Switch("Mute") {
+
+ private float previousValue;
+
+ @Override
+ protected void valueSet(Boolean value) {
+ if (sourceDataLine != null) {
+ FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
+ if (value) {
+ previousValue = volumeControl.getValue();
+ volumeControl.setValue(0);
+ } else {
+ volumeControl.setValue(previousValue);
+ }
+ }
+ }
+ };
+ }
+
+ //
+ // CONTROLLED METHODS
+ //
+