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