2 * Sonitus - AudioSink.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.data.sink;
20 import static javax.sound.sampled.FloatControl.Type.VOLUME;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.logging.Logger;
26 import javax.sound.sampled.AudioFormat;
27 import javax.sound.sampled.AudioSystem;
28 import javax.sound.sampled.FloatControl;
29 import javax.sound.sampled.LineUnavailableException;
30 import javax.sound.sampled.SourceDataLine;
32 import net.pterodactylus.sonitus.data.Controller;
33 import net.pterodactylus.sonitus.data.Metadata;
34 import net.pterodactylus.sonitus.data.Sink;
35 import net.pterodactylus.sonitus.data.Source;
36 import net.pterodactylus.sonitus.data.controller.Fader;
38 import com.google.common.base.Preconditions;
41 * {@link Sink} implementation that uses the JDK’s {@link AudioSystem} to play
42 * all {@link Source}s.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class AudioSink implements Sink {
49 private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
51 /** The volume fader. */
52 private final Fader volumeFader;
54 /** The current metadata. */
55 private Metadata metadata;
57 /** The audio output. */
58 private SourceDataLine sourceDataLine;
60 /** Creates a new audio sink. */
63 volumeFader = new Fader() {
66 protected void valueSet(int value) {
67 if (sourceDataLine != null) {
68 FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
69 volumeControl.setValue(value * volumeControl.getMaximum() / (float) maximum());
80 public List<Controller> controllers() {
81 return Arrays.<Controller>asList(volumeFader);
89 public void open(Metadata metadata) throws IOException {
90 Preconditions.checkArgument(metadata.encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
91 AudioFormat audioFormat = new AudioFormat(metadata.frequency(), 16, metadata.channels(), true, false);
93 sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
94 sourceDataLine.open(audioFormat);
95 sourceDataLine.start();
96 } catch (LineUnavailableException e) {
98 throw new IOException(e);
103 public void close() {
104 sourceDataLine.stop();
105 sourceDataLine.close();
109 public void metadataUpdated(Metadata metadata) {
110 logger.info(String.format("Now playing %s.", metadata));
114 public void process(byte[] buffer) {
115 sourceDataLine.write(buffer, 0, buffer.length);
116 logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));