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;
37 import net.pterodactylus.sonitus.data.controller.Switch;
39 import com.google.common.base.Preconditions;
42 * {@link Sink} implementation that uses the JDK’s {@link AudioSystem} to play
43 * all {@link Source}s.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class AudioSink implements Sink {
50 private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
52 /** The volume fader. */
53 private final Fader volumeFader;
55 /** The “mute” switch. */
56 private final Switch muteSwitch;
58 /** The current metadata. */
59 private Metadata metadata;
61 /** The audio output. */
62 private SourceDataLine sourceDataLine;
64 /** Creates a new audio sink. */
67 volumeFader = new Fader("Volume") {
70 protected void valueSet(Double value) {
71 if (sourceDataLine != null) {
72 FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
73 volumeControl.setValue((float) (value * volumeControl.getMaximum()));
77 muteSwitch = new Switch("Mute") {
79 private float previousValue;
82 protected void valueSet(Boolean value) {
83 if (sourceDataLine != null) {
84 FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
86 previousValue = volumeControl.getValue();
87 volumeControl.setValue(0);
89 volumeControl.setValue(previousValue);
101 public List<Controller<?>> controllers() {
102 return Arrays.<Controller<?>>asList(volumeFader, muteSwitch);
110 public void open(Metadata metadata) throws IOException {
111 Preconditions.checkArgument(metadata.encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
112 AudioFormat audioFormat = new AudioFormat(metadata.frequency(), 16, metadata.channels(), true, false);
114 sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
115 sourceDataLine.open(audioFormat);
116 sourceDataLine.start();
117 } catch (LineUnavailableException e) {
119 throw new IOException(e);
124 public void close() {
125 sourceDataLine.stop();
126 sourceDataLine.close();
130 public void metadataUpdated(Metadata metadata) {
131 logger.info(String.format("Now playing %s.", metadata));
135 public void process(byte[] buffer) {
136 sourceDataLine.write(buffer, 0, buffer.length);
137 logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));