2 * Sonitus - LameMp3Encoder.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.data.filter;
20 import java.util.Arrays;
22 import net.pterodactylus.sonitus.data.Metadata;
24 import com.google.common.base.Optional;
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.eventbus.EventBus;
29 * {@link ExternalMp3Encoder} implementation that uses LAME to encode MP3s.
31 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 public class LameMp3Encoder extends ExternalMp3Encoder {
35 /** Preset for LAME. */
38 /** “medium” preset. */
41 /** “standard” preset. */
44 /** “extreme” preset. */
47 /** “insane” preset. */
52 /** The location of the binary. */
53 private final String binary;
55 /** Whether to swap bytes in the input. */
56 private boolean swapBytes;
58 /** The preset to use. */
59 private final Optional<Preset> preset;
61 /** The bitrate to encode to. */
62 private final Optional<Integer> bitrate;
64 /** Whether to use highest quality encoding. */
65 private boolean hq = false;
68 * Creates a new LAME MP3 encoder.
73 * The location of the binary
77 public LameMp3Encoder(EventBus eventBus, String binary, Preset preset) {
78 this(eventBus, binary, preset, -1);
82 * Creates a new LAME MP3 encoder.
87 * The location of the binary
89 * The bitrate to encode to (in kbps)
91 public LameMp3Encoder(EventBus eventBus, String binary, int bitrate) {
92 this(eventBus, binary, null, bitrate);
96 * Creates a new LAME MP3 encoder.
101 * The location of the binary
105 * The bitrate to encode to (in kbps)
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);
115 * Sets whether to swap bytes on the input to encode
118 * {@code true} to swap the input bytes, {@code false} to use platform
120 * @return This MP3 encoder
122 public LameMp3Encoder swapBytes(boolean swapBytes) {
123 this.swapBytes = swapBytes;
128 * Sets whether to use highest quality encoding.
131 * {@code true} to use highest quality encoding, {@code false} otherwise
132 * @return This MP3 encoder
134 public LameMp3Encoder hq(boolean hq) {
140 // EXTERNALFILTER METHODS
144 protected String binary(Metadata metadata) {
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));
154 parameters.add("-x");
156 parameters.add("--preset");
157 if (preset.isPresent()) {
158 parameters.add(preset.get().name().toLowerCase());
160 if (bitrate.isPresent()) {
161 if (isSignificant(bitrate.get())) {
162 parameters.add("cbr");
164 parameters.add(String.valueOf(bitrate.get()));
166 parameters.add("-p");
168 parameters.add("-q").add("0");
170 parameters.add("-").add("-");
171 return parameters.build();
179 * Returns whether the given bitrate is one of the significant bitrates of
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
187 private static boolean isSignificant(int bitrate) {
188 return Arrays.asList(80, 96, 112, 128, 160, 192, 224, 256, 320).contains(bitrate);