MP3
mp3-Files

mp3-files are files with many frames. It has a sampling rate, but can have one or more bitrates for each frame.

Short Description: MP3.pdf

mp3-files with a hexViewer: mp3.zip




How can one read a mp3 file?

a) Byte for byte

handle = open file(mp3file)
length = filelength(mp3file)
b1 = readbyte()
while (fileposititon <length-4) {
b2 = readbyte()
actualPosition(handle)
b3 = readbyte()
b4 = readbyte()
bool isHeader = checkHeader(b1,b2,b3,b4)
if (isHeader) {
readHeader();
}
else {
seek(actualPosition)
b1 = b2
}
} // while
closefile(handle)
 
 
b) read all bytes and use a BitSet

handle = open file(mp3file)
length = filelength(mp3file)
byte[] buffer = readallbytes(handle)
closefile(handle)
long numberOfBits = length<<3
BitSet bitset = new BitSet(numberOfBits);
int ofs = 0;
int value = 0;
int[] bitArray = { 128, 64, 32, 16, 8, 4, 2, 1 }; // values of the bit position
for (byte b in buffer) {
if (b < 0) {
value = (b + 256); // only in Java: byte from -128 to 127
} else {
value = b;
}
for (int i = 0; i < 8; i++) {
if ((value & bitArray[i]) > 0) {
bitset.set(ofs + i);
}
}
ofs += 8;
} // for (byte b : buffer) {

for (int offset=0; offset<numberOfBits-10; offset+=8) {
bool isHeader = checkHeader(bitset, offset)
if (isHeader) {
readHeader();
}
} // for
 
 
c) read all bytes
 
handle = open file(mp3file)
length = filelength(mp3file)
byte[] buffer = readallbytes(handle)
closefile(handle)
for (int offset=0; offset<length-10; offset++) {
long headerValue = getUInt32(buffer,offset) // C#
long headerValue = getLong(buffer,offset) // Java, java has no UInt32
bool isHeader = checkHeader(headerValue)
if (isHeader) {
readHeader();
}
} // forbool readHeader(...) {
   1) And with FFF or FFE
   2) And with 3 for mpeg Version
   3) And with 3 for layer description
   4) And with 1 for protection bit
   5) And with F for "bit rate"
   6) And with 3 for "sampling rate"
   7) And with 3 for "channel mode"
}