611dcd97219a92b9b49d3eac929de5be661691a9
[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 import net.pterodactylus.sonitus.data.controller.Switch;
38
39 import com.google.common.base.Preconditions;
40
41 /**
42  * {@link Sink} implementation that uses the JDK’s {@link AudioSystem} to play
43  * all {@link Source}s.
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public class AudioSink implements Sink {
48
49         /** The logger. */
50         private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
51
52         /** The volume fader. */
53         private final Fader volumeFader;
54
55         /** The “mute” switch. */
56         private final Switch muteSwitch;
57
58         /** The current metadata. */
59         private Metadata metadata;
60
61         /** The audio output. */
62         private SourceDataLine sourceDataLine;
63
64         /** Creates a new audio sink. */
65         public AudioSink() {
66                 super();
67                 volumeFader = new Fader("Volume") {
68
69                         @Override
70                         protected void valueSet(Double value) {
71                                 if (sourceDataLine != null) {
72                                         FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
73                                         volumeControl.setValue((float) (value * volumeControl.getMaximum()));
74                                 }
75                         }
76                 };
77                 muteSwitch = new Switch("Mute") {
78
79                         private float previousValue;
80
81                         @Override
82                         protected void valueSet(Boolean value) {
83                                 if (sourceDataLine != null) {
84                                         FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
85                                         if (value) {
86                                                 previousValue = volumeControl.getValue();
87                                                 volumeControl.setValue(0);
88                                         } else {
89                                                 volumeControl.setValue(previousValue);
90                                         }
91                                 }
92                         }
93                 };
94         }
95
96         //
97         // CONTROLLED METHODS
98         //
99
100         @Override
101         public List<Controller<?>> controllers() {
102                 return Arrays.<Controller<?>>asList(volumeFader, muteSwitch);
103         }
104
105         //
106         // SINK METHODS
107         //
108
109         @Override
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);
113                 try {
114                         sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
115                         sourceDataLine.open(audioFormat);
116                         sourceDataLine.start();
117                 } catch (LineUnavailableException e) {
118                         /* TODO */
119                         throw new IOException(e);
120                 }
121         }
122
123         @Override
124         public void close() {
125                 sourceDataLine.stop();
126                 sourceDataLine.close();
127         }
128
129         @Override
130         public void metadataUpdated(Metadata metadata) {
131                 logger.info(String.format("Now playing %s.", metadata));
132         }
133
134         @Override
135         public void process(byte[] buffer) {
136                 sourceDataLine.write(buffer, 0, buffer.length);
137                 logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));
138         }
139
140 }