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;
28 * {@link ExternalMp3Encoder} implementation that uses LAME to encode MP3s.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class LameMp3Encoder extends ExternalMp3Encoder {
34 /** Preset for LAME. */
37 /** “medium” preset. */
40 /** “standard” preset. */
43 /** “extreme” preset. */
46 /** “insane” preset. */
51 /** The location of the binary. */
52 private final String binary;
54 /** Whether to swap bytes in the input. */
55 private boolean swapBytes;
57 /** The preset to use. */
58 private final Optional<Preset> preset;
60 /** The bitrate to encode to. */
61 private final Optional<Integer> bitrate;
63 /** Whether to use highest quality encoding. */
64 private boolean hq = false;
67 * Creates a new LAME MP3 encoder.
70 * The location of the binary
74 public LameMp3Encoder(String binary, Preset preset) {
75 this(binary, preset, -1);
79 * Creates a new LAME MP3 encoder.
82 * The location of the binary
84 * The bitrate to encode to (in kbps)
86 public LameMp3Encoder(String binary, int bitrate) {
87 this(binary, null, bitrate);
91 * Creates a new LAME MP3 encoder.
94 * The location of the binary
98 * The bitrate to encode to (in kbps)
100 private LameMp3Encoder(String binary, Preset preset, int bitrate) {
101 this.binary = binary;
102 this.preset = Optional.fromNullable(preset);
103 this.bitrate = (bitrate < 0) ? Optional.<Integer>absent() : Optional.<Integer>of(bitrate);
107 * Sets whether to swap bytes on the input to encode
110 * {@code true} to swap the input bytes, {@code false} to use platform
112 * @return This MP3 encoder
114 public LameMp3Encoder swapBytes(boolean swapBytes) {
115 this.swapBytes = swapBytes;
120 * Sets whether to use highest quality encoding.
123 * {@code true} to use highest quality encoding, {@code false} otherwise
124 * @return This MP3 encoder
126 public LameMp3Encoder hq(boolean hq) {
132 // EXTERNALFILTER METHODS
136 protected String binary(Metadata metadata) {
141 protected Iterable<String> parameters(Metadata metadata) {
142 ImmutableList.Builder<String> parameters = ImmutableList.builder();
143 parameters.add("-r");
144 parameters.add("-s").add(String.valueOf(metadata.frequency() / 1000.0));
146 parameters.add("-x");
148 parameters.add("--preset");
149 if (preset.isPresent()) {
150 parameters.add(preset.get().name().toLowerCase());
152 if (bitrate.isPresent()) {
153 if (isSignificant(bitrate.get())) {
154 parameters.add("cbr");
156 parameters.add(String.valueOf(bitrate.get()));
158 parameters.add("-p");
160 parameters.add("-q").add("0");
162 parameters.add("-").add("-");
163 return parameters.build();
171 * Returns whether the given bitrate is one of the significant bitrates of
175 * The bitrate to check (in kbps)
176 * @return {@code true} if the given bitrate is one of the significant MP3
177 * bitrates, {@code false} otherwise
179 private static boolean isSignificant(int bitrate) {
180 return Arrays.asList(80, 96, 112, 128, 160, 192, 224, 256, 320).contains(bitrate);