X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsink%2FAudioSink.java;h=689c23d5e3ba9857d054e0b99818b450de012cbc;hb=7188da95cfb6dc2bf140eb8ac7e4dc99a0761a97;hp=f29bde8b263e4e602d0932fac13d70af9cac2b11;hpb=3d073631242b121378ff9c60104170d13cab52d7;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 f29bde8..689c23d 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java @@ -17,23 +17,22 @@ package net.pterodactylus.sonitus.data.sink; -import static com.google.common.base.Preconditions.*; - +import java.io.IOException; import java.util.logging.Logger; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; 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.Metadata; import net.pterodactylus.sonitus.data.Sink; -import net.pterodactylus.sonitus.data.Source; + +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 net.pterodactylus.sonitus.data.Sink} implementation that uses the + * JDK’s {@link javax.sound.sampled.AudioSystem} to play all {@link + * net.pterodactylus.sonitus.data.Source}s. * * @author David ‘Bombe’ Roden */ @@ -42,47 +41,41 @@ public class AudioSink implements Sink { /** The logger. */ private static final Logger logger = Logger.getLogger(AudioSink.class.getName()); - /** The current source. */ - private Source source; + /** The current metadata. */ + private Metadata metadata; - @Override - public void connect(Source source) throws ConnectException { - this.source = checkNotNull(source, "source must not be null"); - checkState(source.metadata().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded"); + /** The audio output. */ + private SourceDataLine sourceDataLine; - final Metadata sourceMetadata = source.metadata(); - AudioFormat audioFormat = new AudioFormat(sourceMetadata.frequency(), 16, sourceMetadata.channels(), true, false); + @Override + public void open(Metadata metadata) throws IOException { + Preconditions.checkArgument(metadata.encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded"); + 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) { - - @Override - protected int bufferSize() { - return sourceMetadata.channels() * sourceMetadata.frequency() * 2; - } - - @Override - protected void feed(byte[] buffer) { - sourceDataLine.write(buffer, 0, buffer.length); - logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length)); - } - - @Override - protected void finish() { - sourceDataLine.stop(); - } - }).start(); - metadataUpdated(); - } catch (LineUnavailableException lue1) { - throw new ConnectException(lue1); + } catch (LineUnavailableException e) { + /* TODO */ + throw new IOException(e); } } @Override - public void metadataUpdated() { + public void close() { + sourceDataLine.stop(); + sourceDataLine.close(); + } + + @Override + public void metadataUpdated(Metadata metadata) { /* ignore. */ } + @Override + public void process(byte[] buffer) { + sourceDataLine.write(buffer, 0, buffer.length); + logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length)); + } + }