Add switch for highest quality encoding.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / LameMp3Encoder.java
1 /*
2  * Sonitus - LameMp3Encoder.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 java.util.Arrays;
21
22 import net.pterodactylus.sonitus.data.Format;
23
24 import com.google.common.base.Optional;
25 import com.google.common.collect.ImmutableList;
26
27 /**
28  * {@link ExternalMp3Encoder} implementation that uses LAME to encode MP3s.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class LameMp3Encoder extends ExternalMp3Encoder {
33
34         /** Preset for LAME. */
35         public enum Preset {
36
37                 /** “medium” preset. */
38                 MEDIUM,
39
40                 /** “standard” preset. */
41                 STANDARD,
42
43                 /** “extreme” preset. */
44                 EXTREME,
45
46                 /** “insane” preset. */
47                 INSANE
48
49         }
50
51         /** The location of the binary. */
52         private final String binary;
53
54         /** Whether to swap bytes in the input. */
55         private final boolean swapBytes;
56
57         /** The preset to use. */
58         private final Optional<Preset> preset;
59
60         /** The bitrate to encode to. */
61         private final Optional<Integer> bitrate;
62
63         /** Whether to use highest quality encoding. */
64         private boolean hq = false;
65
66         /**
67          * Creates a new LAME MP3 encoder.
68          *
69          * @param binary
70          *              The location of the binary
71          * @param swapBytes
72          *              {@code true} to swap bytes in the input, {@code false} to use platform
73          *              endianness
74          * @param preset
75          *              The preset to use
76          */
77         public LameMp3Encoder(String binary, boolean swapBytes, Preset preset) {
78                 this(binary, swapBytes, preset, -1);
79         }
80
81         /**
82          * Creates a new LAME MP3 encoder.
83          *
84          * @param binary
85          *              The location of the binary
86          * @param swapBytes
87          *              {@code true} to swap bytes in the input, {@code false} to use platform
88          *              endianness
89          * @param bitrate
90          *              The bitrate to encode to (in kbps)
91          */
92         public LameMp3Encoder(String binary, boolean swapBytes, int bitrate) {
93                 this(binary, swapBytes, null, bitrate);
94         }
95
96         /**
97          * Creates a new LAME MP3 encoder.
98          *
99          * @param binary
100          *              The location of the binary
101          * @param swapBytes
102          *              {@code true} to swap bytes in the input, {@code false} to use platform
103          *              endianness
104          * @param preset
105          *              The preset to use
106          * @param bitrate
107          *              The bitrate to encode to (in kbps)
108          */
109         private LameMp3Encoder(String binary, boolean swapBytes, Preset preset, int bitrate) {
110                 this.binary = binary;
111                 this.swapBytes = swapBytes;
112                 this.preset = Optional.fromNullable(preset);
113                 this.bitrate = (bitrate < 0) ? Optional.<Integer>absent() : Optional.<Integer>of(bitrate);
114         }
115
116         /**
117          * Sets whether to use highest quality encoding.
118          *
119          * @param hq
120          *              {@code true} to use highest quality encoding, {@code false} otherwise
121          * @return This MP3 encoder
122          */
123         public LameMp3Encoder hq(boolean hq) {
124                 this.hq = hq;
125                 return this;
126         }
127
128         //
129         // EXTERNALFILTER METHODS
130         //
131
132         @Override
133         protected String binary(Format format) {
134                 return binary;
135         }
136
137         @Override
138         protected Iterable<String> parameters(Format format) {
139                 ImmutableList.Builder parameters = ImmutableList.builder();
140                 parameters.add("-r");
141                 parameters.add("-s").add(String.valueOf(format.frequency() / 1000.0));
142                 if (swapBytes) {
143                         parameters.add("-x");
144                 }
145                 parameters.add("--preset");
146                 if (preset.isPresent()) {
147                         parameters.add(preset.get().name().toLowerCase());
148                 }
149                 if (bitrate.isPresent()) {
150                         if (isSignificant(bitrate.get())) {
151                                 parameters.add("cbr");
152                         }
153                         parameters.add(String.valueOf(bitrate.get()));
154                 }
155                 parameters.add("-p");
156                 if (hq) {
157                         parameters.add("-q").add("0");
158                 }
159                 parameters.add("-").add("-");
160                 return parameters.build();
161         }
162
163         //
164         // STATIC METHODS
165         //
166
167         /**
168          * Returns whether the given bitrate is one of the significant bitrates of
169          * MP3.
170          *
171          * @param bitrate
172          *              The bitrate to check (in kbps)
173          * @return {@code true} if the given bitrate is one of the significant MP3
174          *         bitrates, {@code false} otherwise
175          */
176         private static boolean isSignificant(int bitrate) {
177                 return Arrays.asList(80, 96, 112, 128, 160, 192, 224, 256, 320).contains(bitrate);
178         }
179
180 }