+ /** 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
+ //
+
+ @Override
+ public String name() {
+ return "Audio Output";
+ }
+
+ @Override
+ public Metadata metadata() {
+ return metadata;
+ }
+
+ @Override
+ public List<Controller<?>> controllers() {
+ return Arrays.<Controller<?>>asList(volumeFader, muteSwitch);
+ }
+
+ //
+ // SINK METHODS
+ //
+