X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsink%2FAudioSink.java;h=6cace627ec200a4fb3e8e05eb73e80f2a30bbaf6;hb=633a841142f978235ed9f745b6ba16c278963e62;hp=1e4f6eca4e33260289be17ac894dfe697a22f720;hpb=f71b3478fa765605de8e53ae0c7a150abf175262;p=sonitus.git
diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java
index 1e4f6ec..6cace62 100644
--- a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java
+++ b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java
@@ -17,63 +17,218 @@
package net.pterodactylus.sonitus.data.sink;
-import static com.google.common.base.Preconditions.*;
+import static javax.sound.sampled.BooleanControl.Type.MUTE;
+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;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
+import javax.sound.sampled.BooleanControl;
+import javax.sound.sampled.Control;
+import javax.sound.sampled.DataLine;
+import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
-import net.pterodactylus.sonitus.data.ConnectException;
-import net.pterodactylus.sonitus.data.Connection;
-import net.pterodactylus.sonitus.data.Format;
-import net.pterodactylus.sonitus.data.Sink;
-import net.pterodactylus.sonitus.data.Source;
+import net.pterodactylus.sonitus.data.AbstractFilter;
+import net.pterodactylus.sonitus.data.Controller;
+import net.pterodactylus.sonitus.data.Filter;
+import net.pterodactylus.sonitus.data.Metadata;
+import net.pterodactylus.sonitus.data.controller.Fader;
+import net.pterodactylus.sonitus.data.controller.Switch;
+import net.pterodactylus.sonitus.io.IntegralWriteOutputStream;
+
+import com.google.common.base.Preconditions;
/**
- * {@link Sink} implementation that uses the JDKâs {@link
- * javax.sound.sampled.AudioSystem} to play all {@link net.pterodactylus.sonitus.data.Source}s.
+ * {@link Filter} implementation that uses the JDKâs {@link AudioSystem} to play
+ * all the audio signal.
*
* @author David âBombeâ Roden
*/
-public class AudioSink implements Sink {
+public class AudioSink extends AbstractFilter {
/** The logger. */
private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
+ /** The volume fader. */
+ private final Fader volumeFader;
+
+ /** The âmuteâ switch. */
+ private final Switch muteSwitch;
+
+ /** 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. */
+ public AudioSink() {
+ super("Audio Output");
+ 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);
+ }
+ }
+
+ };
+ }
+
+ //
+ // FILTER METHODS
+ //
+
@Override
- public void connect(Source source) throws ConnectException {
- checkNotNull(source, "source must not be null");
- checkState(source.format().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
+ public List> controllers() {
+ return Arrays.>asList(volumeFader, muteSwitch);
+ }
- final Format sourceFormat = source.format();
- AudioFormat audioFormat = new AudioFormat(sourceFormat.frequency(), 16, sourceFormat.channels(), true, false);
+ @Override
+ public void open(Metadata metadata) throws IOException {
+ Preconditions.checkArgument(metadata.encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
+ super.open(metadata);
+ AudioFormat audioFormat = new AudioFormat(metadata.frequency(), 16, metadata.channels(), true, false);
try {
- final SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
+ sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
- new Thread(new Connection(source) {
+ metadataUpdated(metadata);
+ } catch (LineUnavailableException e) {
+ /* TODO */
+ sourceDataLine = null;
+ throw new IOException(e);
+ }
+ }
- @Override
- protected int bufferSize() {
- return sourceFormat.channels() * sourceFormat.frequency() * 2;
- }
+ @Override
+ public void close() {
+ sourceDataLine.stop();
+ sourceDataLine.close();
+ }
- @Override
- protected void feed(byte[] buffer) {
- sourceDataLine.write(buffer, 0, buffer.length);
- logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));
- }
+ @Override
+ public void metadataUpdated(Metadata metadata) {
+ super.metadataUpdated(metadata);
+ logger.fine(String.format("Now playing %s.", metadata));
+ }
- @Override
- protected void finish() {
- sourceDataLine.stop();
- }
- }).start();
- } catch (LineUnavailableException lue1) {
- throw new ConnectException(lue1);
+ @Override
+ public void process(byte[] buffer) throws IOException {
+ sourceDataLineOutputStream.write(buffer);
+ super.process(buffer);
+ logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Returns the {@link FloatControl.Type#VOLUME} control.
+ *
+ * @param dataLine
+ * The data line to search for the control
+ * @return The control, or {@code null} if no volume control could be found
+ */
+ private static FloatControl getVolumeControl(DataLine dataLine) {
+ return getControl(dataLine, VOLUME, FloatControl.class);
+ }
+
+ /**
+ * Returns the {@link BooleanControl.Type#MUTE} control.
+ *
+ * @param dataLine
+ * The data line to search for the control
+ * @return The control, or {@code null} if no mute control could be found
+ */
+ private static BooleanControl getMuteControl(DataLine dataLine) {
+ return getControl(dataLine, MUTE, BooleanControl.class);
+ }
+
+ /**
+ * Searches the given data line for a control of the given type and returns it.
+ * If the given data line is {@code null}, {@code null} is returned.
+ *
+ * @param dataLine
+ * The data line to search for a control
+ * @param controlType
+ * The type of the control to search
+ * @param controlClass
+ * The class of the control
+ * @param
+ * The class of the control
+ * @return The control, or {@code null} if no control could be found
+ */
+ private static T getControl(DataLine dataLine, Control.Type controlType, Class controlClass) {
+ if (dataLine == null) {
+ return null;
+ }
+ Control[] controls = dataLine.getControls();
+ for (Control control : controls) {
+ if (control.getType().equals(controlType)) {
+ return (T) control;
+ }
}
+ return null;
}
}