Every source, sink, and filter is now also a controller.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / sink / AudioSink.java
1 /*
2  * Sonitus - AudioSink.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sonitus.data.sink;
19
20 import static javax.sound.sampled.FloatControl.Type.VOLUME;
21
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;
31
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
38 import com.google.common.base.Preconditions;
39
40 /**
41  * {@link Sink} implementation that uses the JDK’s {@link AudioSystem} to play
42  * all {@link Source}s.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class AudioSink implements Sink {
47
48         /** The logger. */
49         private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
50
51         /** The volume fader. */
52         private final Fader volumeFader;
53
54         /** The current metadata. */
55         private Metadata metadata;
56
57         /** The audio output. */
58         private SourceDataLine sourceDataLine;
59
60         /** Creates a new audio sink. */
61         public AudioSink() {
62                 super();
63                 volumeFader = new Fader() {
64
65                         @Override
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());
70                                 }
71                         }
72                 };
73         }
74
75         //
76         // CONTROLLED METHODS
77         //
78
79         @Override
80         public List<Controller> controllers() {
81                 return Arrays.<Controller>asList(volumeFader);
82         }
83
84         //
85         // SINK METHODS
86         //
87
88         @Override
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);
92                 try {
93                         sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
94                         sourceDataLine.open(audioFormat);
95                         sourceDataLine.start();
96                 } catch (LineUnavailableException e) {
97                         /* TODO */
98                         throw new IOException(e);
99                 }
100         }
101
102         @Override
103         public void close() {
104                 sourceDataLine.stop();
105                 sourceDataLine.close();
106         }
107
108         @Override
109         public void metadataUpdated(Metadata metadata) {
110                 logger.info(String.format("Now playing %s.", metadata));
111         }
112
113         @Override
114         public void process(byte[] buffer) {
115                 sourceDataLine.write(buffer, 0, buffer.length);
116                 logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));
117         }
118
119 }