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