-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMp3ParserTest.java
More file actions
64 lines (48 loc) · 2.42 KB
/
Mp3ParserTest.java
File metadata and controls
64 lines (48 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package me.tamkungz.codecmedia.internal.audio.mp3;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import me.tamkungz.codecmedia.internal.audio.BitrateMode;
class Mp3ParserTest {
@Test
void shouldParseSimpleMpeg1Layer3FrameSequenceAsCbr() throws Exception {
byte[] frameHeader = new byte[] {(byte) 0xFF, (byte) 0xFB, (byte) 0x90, 0x00};
int frameLength = 417; // 128 kbps @ 44100, MPEG1 L3 no padding
byte[] frame = new byte[frameLength];
System.arraycopy(frameHeader, 0, frame, 0, frameHeader.length);
byte[] data = new byte[frameLength * 3];
System.arraycopy(frame, 0, data, 0, frameLength);
System.arraycopy(frame, 0, data, frameLength, frameLength);
System.arraycopy(frame, 0, data, frameLength * 2, frameLength);
Mp3ProbeInfo info = Mp3Parser.parse(data);
assertEquals("mp3", info.codec());
assertEquals(44100, info.sampleRate());
assertEquals(2, info.channels());
assertEquals(BitrateMode.CBR, info.bitrateMode());
}
@Test
void shouldDetectVbrWhenFrameBitratesDiffer() throws Exception {
byte[] frame128 = createFrame(new byte[] {(byte) 0xFF, (byte) 0xFB, (byte) 0x90, 0x00}, 417);
byte[] frame64 = createFrame(new byte[] {(byte) 0xFF, (byte) 0xFB, (byte) 0x50, 0x00}, 208);
byte[] data = new byte[frame128.length + frame64.length];
System.arraycopy(frame128, 0, data, 0, frame128.length);
System.arraycopy(frame64, 0, data, frame128.length, frame64.length);
Mp3ProbeInfo info = Mp3Parser.parse(data);
assertEquals(44100, info.sampleRate());
assertEquals(BitrateMode.VBR, info.bitrateMode());
}
@Test
void shouldParseMonoFromChannelMode() throws Exception {
byte[] frame = createFrame(new byte[] {(byte) 0xFF, (byte) 0xFB, (byte) 0x90, (byte) 0xC0}, 417);
byte[] data = new byte[frame.length * 2];
System.arraycopy(frame, 0, data, 0, frame.length);
System.arraycopy(frame, 0, data, frame.length, frame.length);
Mp3ProbeInfo info = Mp3Parser.parse(data);
assertEquals(1, info.channels());
assertEquals(BitrateMode.CBR, info.bitrateMode());
}
private static byte[] createFrame(byte[] header, int frameLength) {
byte[] frame = new byte[frameLength];
System.arraycopy(header, 0, frame, 0, header.length);
return frame;
}
}