From d6152103cbdee66c1b047d6411c8e9cbf180b79b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 15 Mar 2013 23:38:27 +0100 Subject: [PATCH] Add audio sink. --- .../pterodactylus/sonitus/data/sink/AudioSink.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java new file mode 100644 index 0000000..5d1ebbc --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java @@ -0,0 +1,74 @@ +/* + * Sonitus - AudioSink.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sonitus.data.sink; + +import static com.google.common.base.Preconditions.*; + +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.Format; +import net.pterodactylus.sonitus.data.Sink; +import net.pterodactylus.sonitus.data.Source; + +/** + * {@link 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 + */ +public class AudioSink implements Sink { + + /** The logger. */ + private static final Logger logger = Logger.getLogger(AudioSink.class.getName()); + + @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"); + + final Format sourceFormat = source.format(); + AudioFormat audioFormat = new AudioFormat(sourceFormat.frequency(), 16, sourceFormat.channels(), true, false); + try { + final SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(audioFormat); + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + new Thread(new Connection(source) { + @Override + protected int bufferSize() { + return sourceFormat.channels() * sourceFormat.frequency() * 2; + } + + @Override + protected void feed(byte[] buffer) { + sourceDataLine.write(buffer, 0, buffer.length); + logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length)); + } + }).start(); + } + catch (LineUnavailableException lue1) { + throw new ConnectException(lue1); + } + } + +} -- 2.7.4