Fix javadoc.
[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 java.io.IOException;
21 import java.util.logging.Logger;
22 import javax.sound.sampled.AudioFormat;
23 import javax.sound.sampled.AudioSystem;
24 import javax.sound.sampled.LineUnavailableException;
25 import javax.sound.sampled.SourceDataLine;
26
27 import net.pterodactylus.sonitus.data.Metadata;
28 import net.pterodactylus.sonitus.data.Sink;
29 import net.pterodactylus.sonitus.data.Source;
30
31 import com.google.common.base.Preconditions;
32
33 /**
34  * {@link Sink} implementation that uses the JDK’s {@link AudioSystem} to play
35  * all {@link Source}s.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class AudioSink implements Sink {
40
41         /** The logger. */
42         private static final Logger logger = Logger.getLogger(AudioSink.class.getName());
43
44         /** The current metadata. */
45         private Metadata metadata;
46
47         /** The audio output. */
48         private SourceDataLine sourceDataLine;
49
50         @Override
51         public void open(Metadata metadata) throws IOException {
52                 Preconditions.checkArgument(metadata.encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded");
53                 AudioFormat audioFormat = new AudioFormat(metadata.frequency(), 16, metadata.channels(), true, false);
54                 try {
55                         sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
56                         sourceDataLine.open(audioFormat);
57                         sourceDataLine.start();
58                 } catch (LineUnavailableException e) {
59                         /* TODO */
60                         throw new IOException(e);
61                 }
62         }
63
64         @Override
65         public void close() {
66                 sourceDataLine.stop();
67                 sourceDataLine.close();
68         }
69
70         @Override
71         public void metadataUpdated(Metadata metadata) {
72                 /* ignore. */
73         }
74
75         @Override
76         public void process(byte[] buffer) {
77                 sourceDataLine.write(buffer, 0, buffer.length);
78                 logger.finest(String.format("AudioSink: Wrote %d Bytes.", buffer.length));
79         }
80
81 }