Declare the parameters builder correctly.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / OggVorbisDecoder.java
1 /*
2  * Sonitus - OggVorbisDecoder.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.filter;
19
20 import net.pterodactylus.sonitus.data.ConnectException;
21 import net.pterodactylus.sonitus.data.Metadata;
22 import net.pterodactylus.sonitus.data.Source;
23
24 import com.google.common.base.Preconditions;
25 import com.google.common.collect.ImmutableList;
26
27 /**
28  * Ogg Vorbis decoder that uses {@code oggdec} (from the {@code vorbis-tools}
29  * package) to decode the Ogg Vorbis stream.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class OggVorbisDecoder extends ExternalFilter {
34
35         /** The location of the binary. */
36         private final String binary;
37
38         /** Whether to swap bytes. */
39         private boolean swapBytes;
40
41         /**
42          * Creates a new Ogg Vorbis decoder.
43          *
44          * @param binary
45          *              The location of the binary
46          */
47         public OggVorbisDecoder(String binary) {
48                 this.binary = binary;
49         }
50
51         /**
52          * Sets whether to swap bytes on the decoded output.
53          *
54          * @param swapBytes
55          *              {@code true} to swap bytes on the decoded output, {@code false} otherwise
56          * @return This Ogg Vorbis decoder
57          */
58         public OggVorbisDecoder swapBytes(boolean swapBytes) {
59                 this.swapBytes = swapBytes;
60                 return this;
61         }
62
63         //
64         // FILTER METHODS
65         //
66
67         @Override
68         public Metadata metadata() {
69                 return super.metadata().encoding("PCM");
70         }
71
72         @Override
73         public void connect(Source source) throws ConnectException {
74                 Preconditions.checkNotNull(source, "source must not be null");
75                 Preconditions.checkArgument(source.metadata().encoding().equalsIgnoreCase("Vorbis"), "source must be Vorbis-encoded");
76
77                 super.connect(source);
78         }
79
80         //
81         // EXTERNALFILTER METHODS
82         //
83
84         @Override
85         protected String binary(Metadata metadata) {
86                 return binary;
87         }
88
89         @Override
90         protected Iterable<String> parameters(Metadata metadata) {
91                 ImmutableList.Builder<String> parameters = ImmutableList.builder();
92                 parameters.add("-R");
93                 if (swapBytes) {
94                         parameters.add("-e").add("1");
95                 }
96                 parameters.add("-o").add("-");
97                 parameters.add("-");
98                 return parameters.build();
99         }
100
101 }