cb232598304c9ea9b2d14053282331e5cb2902d4
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / LameMp3Decoder.java
1 /*
2  * Sonitus - LameMp3Decoder.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.Metadata;
21
22 import com.google.common.collect.ImmutableList;
23
24 /**
25  * {@link ExternalMp3Decoder} implementation that uses LAME to decode an MP3.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class LameMp3Decoder extends ExternalMp3Decoder {
30
31         /** The location of the binary. */
32         private final String binary;
33
34         /** Whether to swap bytes in the decoded output. */
35         private boolean swapBytes;
36
37         /**
38          * Creates a new LAME MP3 decoder.
39          *
40          * @param binary
41          *              The location of the binary
42          */
43         public LameMp3Decoder(String binary) {
44                 this.binary = binary;
45         }
46
47         /**
48          * Sets whether to swap bytes on the decoded output.
49          *
50          * @param swapBytes
51          *              {@code true} to swap the decoded bytes, {@code false} to use platform
52          *              endianness
53          * @return This MP3 decoder
54          */
55         public LameMp3Decoder swapBytes(boolean swapBytes) {
56                 this.swapBytes = swapBytes;
57                 return this;
58         }
59
60         //
61         // EXTERNALFILTER METHODS
62         //
63
64         @Override
65         protected String binary(Metadata metadata) {
66                 return binary;
67         }
68
69         @Override
70         protected Iterable<String> parameters(Metadata metadata) {
71                 ImmutableList.Builder<String> parameters = ImmutableList.builder();
72                 parameters.add("--mp3input").add("--decode").add("-t");
73                 if (swapBytes) {
74                         parameters.add("-x");
75                 }
76                 parameters.add("-").add("-");
77                 return parameters.build();
78         }
79
80 }