2 * Sonitus - Frame.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.io.mp3;
22 import com.google.common.base.Optional;
23 import com.google.common.base.Supplier;
24 import com.google.common.base.Suppliers;
25 import com.google.common.collect.ImmutableMap;
28 * A single MPEG audio frame.
30 * This uses information from <a href="http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm">mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm</a>.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 /** The MPEG audio version. */
37 public enum MpegAudioVersion {
53 /** The MPEG layer description. */
54 public enum LayerDescription {
70 /** The channel mode. */
71 public enum ChannelMode {
79 /** Dual-channel stereo. */
82 /** Single channel, aka mono. */
87 /** Mode of emphasis. */
88 public enum Emphasis {
104 /** Bitrate table. */
105 private static final Supplier<Map<MpegAudioVersion, Map<LayerDescription, Map<Integer, Integer>>>> bitrateSupplier = Suppliers.memoize(new Supplier<Map<MpegAudioVersion, Map<LayerDescription, Map<Integer, Integer>>>>() {
108 public Map<MpegAudioVersion, Map<LayerDescription, Map<Integer, Integer>>> get() {
109 ImmutableMap.Builder<MpegAudioVersion, Map<LayerDescription, Map<Integer, Integer>>> mpegAudioVersionMapBuilder = ImmutableMap.builder();
112 ImmutableMap.Builder<LayerDescription, Map<Integer, Integer>> mpeg1Builder = ImmutableMap.builder();
115 ImmutableMap.Builder<Integer, Integer> bitrates = ImmutableMap.builder();
116 bitrates.put(0, 0).put(1, 32).put(2, 64).put(3, 96).put(4, 128).put(5, 160).put(6, 192).put(7, 224);
117 bitrates.put(8, 256).put(9, 288).put(10, 320).put(11, 352).put(12, 384).put(13, 416).put(14, 448).put(15, -1);
118 mpeg1Builder.put(LayerDescription.LAYER_1, bitrates.build());
120 /* MPEG 1, Layer 2 bitrates. */
121 bitrates = ImmutableMap.builder();
122 bitrates.put(0, 0).put(1, 32).put(2, 48).put(3, 56).put(4, 64).put(5, 80).put(6, 96).put(7, 112);
123 bitrates.put(8, 128).put(9, 160).put(10, 192).put(11, 224).put(12, 256).put(13, 320).put(14, 384).put(15, -1);
124 mpeg1Builder.put(LayerDescription.LAYER_2, bitrates.build());
126 /* MPEG 1, Layer 3 bitrates. */
127 bitrates = ImmutableMap.builder();
128 bitrates.put(0, 0).put(1, 32).put(2, 40).put(3, 48).put(4, 56).put(5, 64).put(6, 80).put(7, 96);
129 bitrates.put(8, 112).put(9, 128).put(10, 160).put(11, 192).put(12, 224).put(13, 256).put(14, 320).put(15, -1);
130 mpeg1Builder.put(LayerDescription.LAYER_3, bitrates.build());
131 mpegAudioVersionMapBuilder.put(MpegAudioVersion.VERSION_1, mpeg1Builder.build());
134 ImmutableMap.Builder<LayerDescription, Map<Integer, Integer>> mpeg2Builder = ImmutableMap.builder();
137 bitrates = ImmutableMap.builder();
138 bitrates.put(0, 0).put(1, 32).put(2, 48).put(3, 56).put(4, 64).put(5, 80).put(6, 96).put(7, 112);
139 bitrates.put(8, 128).put(9, 144).put(10, 160).put(11, 176).put(12, 192).put(13, 224).put(14, 256).put(15, -1);
140 mpeg2Builder.put(LayerDescription.LAYER_1, bitrates.build());
143 bitrates = ImmutableMap.builder();
144 bitrates.put(0, 0).put(1, 8).put(2, 16).put(3, 24).put(4, 32).put(5, 40).put(6, 48).put(7, 56);
145 bitrates.put(8, 64).put(9, 80).put(10, 96).put(11, 112).put(12, 128).put(13, 144).put(14, 160).put(15, -1);
146 mpeg2Builder.put(LayerDescription.LAYER_2, bitrates.build());
147 mpeg2Builder.put(LayerDescription.LAYER_3, bitrates.build());
149 mpegAudioVersionMapBuilder.put(MpegAudioVersion.VERSION_2, mpeg2Builder.build());
150 mpegAudioVersionMapBuilder.put(MpegAudioVersion.VERSION_2_5, mpeg2Builder.build());
152 return mpegAudioVersionMapBuilder.build();
156 /** Sampling rate table. */
157 private static final Supplier<Map<MpegAudioVersion, Map<Integer, Integer>>> samplingRateSupplier = Suppliers.memoize(new Supplier<Map<MpegAudioVersion, Map<Integer, Integer>>>() {
160 public Map<MpegAudioVersion, Map<Integer, Integer>> get() {
161 ImmutableMap.Builder<MpegAudioVersion, Map<Integer, Integer>> mpegAudioVersions = ImmutableMap.builder();
164 ImmutableMap.Builder<Integer, Integer> samplingRates = ImmutableMap.builder();
165 samplingRates.put(0, 44100).put(1, 48000).put(2, 32000).put(3, 0);
166 mpegAudioVersions.put(MpegAudioVersion.VERSION_1, samplingRates.build());
169 samplingRates = ImmutableMap.builder();
170 samplingRates.put(0, 22050).put(1, 24000).put(2, 16000).put(3, 0);
171 mpegAudioVersions.put(MpegAudioVersion.VERSION_2, samplingRates.build());
174 samplingRates = ImmutableMap.builder();
175 samplingRates.put(0, 11025).put(1, 12000).put(2, 8000).put(3, 0);
176 mpegAudioVersions.put(MpegAudioVersion.VERSION_2_5, samplingRates.build());
178 return mpegAudioVersions.build();
182 /** The decoded MPEG audio version ID. */
183 private final int mpegAudioVersionId;
185 /** The decoded layer description. */
186 private final int layerDescription;
188 /** The decoded protection bit. */
189 private final int protectionBit;
191 /** The decoded bitrate index. */
192 private final int bitrateIndex;
194 /** The deocded sampling rate frequency index. */
195 private final int samplingRateFrequencyIndex;
197 /** The decoded padding bit. */
198 private final int paddingBit;
200 /** The decoded private bit. */
201 private final int privateBit;
203 /** The decoded channel mode. */
204 private final int channelMode;
206 /** The deocded mode extension. */
207 private final int modeExtension;
209 /** The decoded copyright bit. */
210 private final int copyrightBit;
212 /** The deocded original bit. */
213 private final int originalBit;
215 /** The decoded emphasis mode. */
216 private final int emphasis;
219 * Creates a new frame from the given values.
221 * @param mpegAudioVersionId
222 * The MPEG audio version ID
223 * @param layerDescription
224 * The layer description
225 * @param protectionBit
227 * @param bitrateIndex
229 * @param samplingRateFrequencyIndex
230 * The sampling rate frequency index
237 * @param modeExtension
239 * @param copyrightBit
246 private Frame(int mpegAudioVersionId, int layerDescription, int protectionBit, int bitrateIndex, int samplingRateFrequencyIndex, int paddingBit, int privateBit, int channelMode, int modeExtension, int copyrightBit, int originalBit, int emphasis) {
247 this.mpegAudioVersionId = mpegAudioVersionId;
248 this.layerDescription = layerDescription;
249 this.protectionBit = protectionBit;
250 this.bitrateIndex = bitrateIndex;
251 this.samplingRateFrequencyIndex = samplingRateFrequencyIndex;
252 this.paddingBit = paddingBit;
253 this.privateBit = privateBit;
254 this.channelMode = channelMode;
255 this.modeExtension = modeExtension;
256 this.copyrightBit = copyrightBit;
257 this.originalBit = originalBit;
258 this.emphasis = emphasis;
266 * Returns the MPEG audio version.
268 * @return The MPEG audio version
270 public MpegAudioVersion mpegAudioVersion() {
271 return MpegAudioVersion.values()[mpegAudioVersionId];
275 * Returns the layer description.
277 * @return The layer description
279 public LayerDescription layerDescription() {
280 return LayerDescription.values()[layerDescription];
284 * Returns the protection bit.
286 * @return {@code true} if the protection bit is set, {@code false} otherwise
288 public boolean protectionBit() {
289 return protectionBit != 0;
293 * Returns the bitrate of this frame.
295 * @return The bitrate of this frame (in kbps)
297 public int bitrate() {
298 return bitrateSupplier.get().get(mpegAudioVersion()).get(layerDescription()).get(bitrateIndex);
302 * Returns the sampling rate of the audio data in this frame.
304 * @return The sample rate (in Hertz)
306 public int samplingRate() {
307 return samplingRateSupplier.get().get(mpegAudioVersion()).get(samplingRateFrequencyIndex);
311 * Returns the padding bit.
313 * @return {@code true} if the padding bit is set, {@code false} otherwise
315 public boolean paddingBit() {
316 return paddingBit != 0;
320 * Returns the private bit.
322 * @return {@code true} if the private bit is set, {@code false} otherwise
324 public boolean privateBit() {
325 return privateBit != 0;
329 * Returns the channel mode.
331 * @return The channel mode
333 public ChannelMode channelMode() {
334 return ChannelMode.values()[channelMode];
337 /* TODO - mode extension. */
340 * Returns the copyright bit.
342 * @return {@code true} if the copyright bit is set, {@code false} otherwise
344 public boolean copyrightBit() {
345 return copyrightBit != 0;
349 * Returns the original bit.
351 * @return {@code true} if the original bit is set, {@code false} otherwise
353 public boolean originalBit() {
354 return originalBit != 0;
358 * Returns the emphasis.
360 * @return The emphasis
362 public Emphasis emphasis() {
363 return Emphasis.values()[emphasis];
371 * Returns whether the data beginning at the given offset is an MPEG audio
375 * The buffer in which the data is stored
377 * The beginning of the data to parse
379 * The length of the data to parse
380 * @return {@code true} if the data at the given offset is an MPEG audio frame
382 public static boolean isFrame(byte[] buffer, int offset, int length) {
383 return (length > 3) && (((buffer[offset] & 0xff) == 0xff) && ((buffer[offset + 1] & 0xe0) == 0xe0));
387 * Tries to create an MPEG audio from the given data.
390 * The buffer in which the data is stored
392 * The offset at which to look for a frame
394 * The length of the data in the buffer
395 * @return The frame if it could be parsed, or {@link Optional#absent()} if no
396 * frame could be found
398 public static Optional<Frame> create(byte[] buffer, int offset, int length) {
399 if (isFrame(buffer, offset, length)) {
400 int mpegAudioVersionId = (buffer[offset + 1] & 0x18) >>> 3;
401 int layerDescription = (buffer[offset + 1] & 0x06) >>> 1;
402 int protectionBit = buffer[offset + 1] & 0x01;
403 int bitrateIndex = (buffer[offset + 2] & 0xf0) >>> 4;
404 int samplingRateFrequencyIndex = (buffer[offset + 2] & 0x0c) >>> 2;
405 int paddingBit = (buffer[offset + 2] & 0x02) >>> 1;
406 int privateBit = buffer[offset + 2] & 0x01;
407 int channelMode = (buffer[offset + 3] & 0xc0) >> 6;
408 int modeExtension = (buffer[offset + 3] & 0x60) >> 4;
409 int copyright = (buffer[offset + 3] & 0x08) >> 3;
410 int original = (buffer[offset + 3] & 0x04) >> 2;
411 int emphasis = buffer[offset + 3] & 0x03;
412 return Optional.of(new Frame(mpegAudioVersionId, layerDescription, protectionBit, bitrateIndex, samplingRateFrequencyIndex, paddingBit, privateBit, channelMode, modeExtension, copyright, original, emphasis));
414 return Optional.absent();