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.ImmutableBiMap;
26 import com.google.common.collect.ImmutableMap;
29 * A single MPEG audio frame.
31 * This uses information from <a href="http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm">mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm</a>.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 /** The MPEG audio version. */
38 public enum MpegAudioVersion {
54 /** The MPEG layer description. */
55 public enum LayerDescription {
71 /** The channel mode. */
72 public enum ChannelMode {
80 /** Dual-channel stereo. */
83 /** Single channel, aka mono. */
88 /** Mode of emphasis. */
89 public enum Emphasis {
105 /** Bitrate table. */
106 private static final Supplier<Map<MpegAudioVersion, Map<LayerDescription, Map<Integer, Integer>>>> bitrateSupplier = Suppliers.memoize(new Supplier<Map<MpegAudioVersion, Map<LayerDescription, Map<Integer, Integer>>>>() {
109 public Map<MpegAudioVersion, Map<LayerDescription, Map<Integer, Integer>>> get() {
110 ImmutableMap.Builder<MpegAudioVersion, Map<LayerDescription, Map<Integer, Integer>>> mpegAudioVersionMapBuilder = ImmutableMap.builder();
113 ImmutableMap.Builder<LayerDescription, Map<Integer, Integer>> mpeg1Builder = ImmutableMap.builder();
116 ImmutableMap.Builder<Integer, Integer> bitrates = ImmutableMap.builder();
117 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);
118 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);
119 mpeg1Builder.put(LayerDescription.LAYER_1, bitrates.build());
121 /* MPEG 1, Layer 2 bitrates. */
122 bitrates = ImmutableMap.builder();
123 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);
124 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);
125 mpeg1Builder.put(LayerDescription.LAYER_2, bitrates.build());
127 /* MPEG 1, Layer 3 bitrates. */
128 bitrates = ImmutableMap.builder();
129 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);
130 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);
131 mpeg1Builder.put(LayerDescription.LAYER_3, bitrates.build());
132 mpegAudioVersionMapBuilder.put(MpegAudioVersion.VERSION_1, mpeg1Builder.build());
135 ImmutableBiMap.Builder<LayerDescription, Map<Integer, Integer>> mpeg2Builder = ImmutableBiMap.builder();
138 bitrates = ImmutableMap.builder();
139 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);
140 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);
141 mpeg2Builder.put(LayerDescription.LAYER_1, bitrates.build());
144 bitrates = ImmutableMap.builder();
145 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);
146 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);
147 mpeg2Builder.put(LayerDescription.LAYER_2, bitrates.build());
148 mpeg2Builder.put(LayerDescription.LAYER_3, bitrates.build());
150 mpegAudioVersionMapBuilder.put(MpegAudioVersion.VERSION_2, mpeg2Builder.build());
151 mpegAudioVersionMapBuilder.put(MpegAudioVersion.VERSION_2_5, mpeg2Builder.build());
153 return mpegAudioVersionMapBuilder.build();
157 /** Sampling rate table. */
158 private static final Supplier<Map<MpegAudioVersion, Map<Integer, Integer>>> samplingRateSupplier = Suppliers.memoize(new Supplier<Map<MpegAudioVersion, Map<Integer, Integer>>>() {
161 public Map<MpegAudioVersion, Map<Integer, Integer>> get() {
162 ImmutableMap.Builder<MpegAudioVersion, Map<Integer, Integer>> mpegAudioVersions = ImmutableMap.builder();
165 ImmutableMap.Builder<Integer, Integer> samplingRates = ImmutableMap.builder();
166 samplingRates.put(0, 44100).put(1, 48000).put(2, 32000).put(3, 0);
167 mpegAudioVersions.put(MpegAudioVersion.VERSION_1, samplingRates.build());
170 samplingRates = ImmutableMap.builder();
171 samplingRates.put(0, 22050).put(1, 24000).put(2, 16000).put(3, 0);
172 mpegAudioVersions.put(MpegAudioVersion.VERSION_2, samplingRates.build());
175 samplingRates = ImmutableMap.builder();
176 samplingRates.put(0, 11025).put(1, 12000).put(2, 8000).put(3, 0);
177 mpegAudioVersions.put(MpegAudioVersion.VERSION_2_5, samplingRates.build());
179 return mpegAudioVersions.build();
183 /** The decoded MPEG audio version ID. */
184 private final int mpegAudioVersionId;
186 /** The decoded layer description. */
187 private final int layerDescription;
189 /** The decoded protection bit. */
190 private final int protectionBit;
192 /** The decoded bitrate index. */
193 private final int bitrateIndex;
195 /** The deocded sampling rate frequency index. */
196 private final int samplingRateFrequencyIndex;
198 /** The decoded padding bit. */
199 private final int paddingBit;
201 /** The decoded private bit. */
202 private final int privateBit;
204 /** The decoded channel mode. */
205 private final int channelMode;
207 /** The deocded mode extension. */
208 private final int modeExtension;
210 /** The decoded copyright bit. */
211 private final int copyrightBit;
213 /** The deocded original bit. */
214 private final int originalBit;
216 /** The decoded emphasis mode. */
217 private final int emphasis;
220 * Creates a new frame from the given values.
222 * @param mpegAudioVersionId
223 * The MPEG audio version ID
224 * @param layerDescription
225 * The layer description
226 * @param protectionBit
228 * @param bitrateIndex
230 * @param samplingRateFrequencyIndex
231 * The sampling rate frequency index
238 * @param modeExtension
240 * @param copyrightBit
247 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) {
248 this.mpegAudioVersionId = mpegAudioVersionId;
249 this.layerDescription = layerDescription;
250 this.protectionBit = protectionBit;
251 this.bitrateIndex = bitrateIndex;
252 this.samplingRateFrequencyIndex = samplingRateFrequencyIndex;
253 this.paddingBit = paddingBit;
254 this.privateBit = privateBit;
255 this.channelMode = channelMode;
256 this.modeExtension = modeExtension;
257 this.copyrightBit = copyrightBit;
258 this.originalBit = originalBit;
259 this.emphasis = emphasis;
267 * Returns the MPEG audio version.
269 * @return The MPEG audio version
271 public MpegAudioVersion mpegAudioVersion() {
272 return MpegAudioVersion.values()[mpegAudioVersionId];
276 * Returns the layer description.
278 * @return The layer description
280 public LayerDescription layerDescription() {
281 return LayerDescription.values()[layerDescription];
285 * Returns the protection bit.
287 * @return {@code true} if the protection bit is set, {@code false} otherwise
289 public boolean protectionBit() {
290 return protectionBit != 0;
294 * Returns the bitrate of this frame.
296 * @return The bitrate of this frame (in kbps)
298 public int bitrate() {
299 return bitrateSupplier.get().get(mpegAudioVersion()).get(layerDescription()).get(bitrateIndex);
303 * Returns the sampling rate of the audio data in this frame.
305 * @return The sample rate (in Hertz)
307 public int samplingRate() {
308 return samplingRateSupplier.get().get(mpegAudioVersion()).get(samplingRateFrequencyIndex);
312 * Returns the padding bit.
314 * @return {@code true} if the padding bit is set, {@code false} otherwise
316 public boolean paddingBit() {
317 return paddingBit != 0;
321 * Returns the private bit.
323 * @return {@code true} if the private bit is set, {@code false} otherwise
325 public boolean privateBit() {
326 return privateBit != 0;
330 * Returns the channel mode.
332 * @return The channel mode
334 public ChannelMode channelMode() {
335 return ChannelMode.values()[channelMode];
338 /* TODO - mode extension. */
341 * Returns the copyright bit.
343 * @return {@code true} if the copyright bit is set, {@code false} otherwise
345 public boolean copyrightBit() {
346 return copyrightBit != 0;
350 * Returns the original bit.
352 * @return {@code true} if the original bit is set, {@code false} otherwise
354 public boolean originalBit() {
355 return originalBit != 0;
359 * Returns the emphasis.
361 * @return The emphasis
363 public Emphasis emphasis() {
364 return Emphasis.values()[emphasis];
372 * Returns whether the data beginning at the given offset is an MPEG audio
376 * The buffer in which the data is stored
378 * The beginning of the data to parse
380 * The length of the data to parse
381 * @return {@code true} if the data at the given offset is an MPEG audio frame
383 public static boolean isFrame(byte[] buffer, int offset, int length) {
384 return (length > 3) && (((buffer[offset] & 0xff) == 0xff) && ((buffer[offset + 1] & 0xe0) == 0xe0));
388 * Tries to create an MPEG audio from the given data.
391 * The buffer in which the data is stored
393 * The offset at which to look for a frame
395 * The length of the data in the buffer
396 * @return The frame if it could be parsed, or {@link Optional#absent()} if no
397 * frame could be found
399 public static Optional<Frame> create(byte[] buffer, int offset, int length) {
400 if (isFrame(buffer, offset, length)) {
401 int mpegAudioVersionId = (buffer[offset + 1] & 0x18) >>> 3;
402 int layerDescription = (buffer[offset + 1] & 0x06) >>> 1;
403 int protectionBit = buffer[offset + 1] & 0x01;
404 int bitrateIndex = (buffer[offset + 2] & 0xf0) >>> 4;
405 int samplingRateFrequencyIndex = (buffer[offset + 2] & 0x0c) >>> 2;
406 int paddingBit = (buffer[offset + 2] & 0x02) >>> 1;
407 int privateBit = buffer[offset + 2] & 0x01;
408 int channelMode = (buffer[offset + 3] & 0xc0) >> 6;
409 int modeExtension = (buffer[offset + 3] & 0x60) >> 4;
410 int copyright = (buffer[offset + 3] & 0x08) >> 3;
411 int original = (buffer[offset + 3] & 0x04) >> 2;
412 int emphasis = buffer[offset + 3] & 0x03;
413 return Optional.of(new Frame(mpegAudioVersionId, layerDescription, protectionBit, bitrateIndex, samplingRateFrequencyIndex, paddingBit, privateBit, channelMode, modeExtension, copyright, original, emphasis));
415 return Optional.absent();