+ * Calculates the frame length in bytes for the frame starting at the given
+ * offset in the given buffer. This method should only be called for a buffer
+ * and an offset for which {@link #isFrame(byte[], int, int)} returns {@code
+ * true}.
+ *
+ * @param buffer
+ * The buffer storing the frame
+ * @param offset
+ * The offset of the frame
+ * @return The length of the frame in bytes, or {@code -1} if the frame length
+ * can not be calculated
+ */
+ public static int getFrameLength(byte[] buffer, int offset) {
+ MpegAudioVersion mpegAudioVersion = MpegAudioVersion.values()[(buffer[offset + 1] & 0x18) >>> 3];
+ LayerDescription layerDescription = LayerDescription.values()[(buffer[offset + 1] & 0x06) >>> 1];
+ int bitrate = bitrateSupplier.get().get(mpegAudioVersion).get(layerDescription).get((buffer[offset + 2] & 0xf0) >>> 4) * 1000;
+ int samplingRate = samplingRateSupplier.get().get(mpegAudioVersion).get((buffer[offset + 2] & 0x0c) >>> 2);
+ int paddingBit = (buffer[offset + 2] & 0x02) >>> 1;
+ if (layerDescription == LayerDescription.LAYER_1) {
+ return (12 * bitrate / samplingRate + paddingBit) * 4;
+ } else if ((layerDescription == LayerDescription.LAYER_2) || (layerDescription == LayerDescription.LAYER_3)) {
+ return 144 * bitrate / samplingRate + paddingBit;
+ }
+ return -1;
+ }
+
+ /**