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