import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
+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.Sink;
-import net.pterodactylus.sonitus.data.Source;
import net.pterodactylus.sonitus.data.controller.Fader;
import net.pterodactylus.sonitus.data.controller.Switch;
-import net.pterodactylus.sonitus.data.event.MetadataUpdated;
import net.pterodactylus.sonitus.io.IntegralWriteOutputStream;
import com.google.common.base.Preconditions;
-import com.google.common.eventbus.EventBus;
/**
- * {@link Sink} implementation that uses the JDK’s {@link AudioSystem} to play
- * all {@link Source}s.
+ * {@link Filter} implementation that uses the JDK’s {@link AudioSystem} to play
+ * all the audio signal.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class AudioSink implements Sink {
+public class AudioSink extends AbstractFilter {
/** The logger. */
private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
- /** 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;
}
}, 1024);
- /**
- * Creates a new audio sink.
- *
- * @param eventBus
- * The event bus
- */
- public AudioSink(EventBus eventBus) {
- this.eventBus = eventBus;
+ /** Creates a new audio sink. */
+ public AudioSink() {
+ super("Audio Output");
volumeFader = new Fader("Volume") {
@Override
}
//
- // CONTROLLED METHODS
+ // FILTER 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
- //
-
@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 {
sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
metadataUpdated(metadata);
} catch (LineUnavailableException e) {
/* TODO */
+ sourceDataLine = null;
throw new IOException(e);
}
}
@Override
public void metadataUpdated(Metadata metadata) {
- logger.info(String.format("Now playing %s.", metadata));
- this.metadata = metadata;
- eventBus.post(new MetadataUpdated(this, metadata));
+ super.metadataUpdated(metadata);
+ logger.fine(String.format("Now playing %s.", metadata));
}
@Override
public void process(byte[] buffer) throws IOException {
sourceDataLineOutputStream.write(buffer);
+ super.process(buffer);
logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));
}
//
/**
- * Returns the {@link FloatControl.Type.VOLUME} control.
+ * Returns the {@link FloatControl.Type#VOLUME} control.
*
* @param dataLine
* The data line to search for the control
}
/**
- * Returns the {@link BooleanControl.Type.MUTE} control.
+ * Returns the {@link BooleanControl.Type#MUTE} control.
*
* @param dataLine
* The data line to search for the control