1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package entagged.audioformats.mpc.util;
20
21 import entagged.audioformats.EncodingInfo;
22 import entagged.audioformats.exceptions.*;
23
24 import java.io.*;
25
26 public class MpcInfoReader {
27
28 public EncodingInfo read( RandomAccessFile raf ) throws CannotReadException, IOException {
29 EncodingInfo info = new EncodingInfo();
30
31
32 if ( raf.length()==0 ) {
33
34 System.err.println("Error: File empty");
35
36 throw new CannotReadException("File is empty");
37 }
38 raf.seek( 0 );
39
40
41
42 byte[] b = new byte[3];
43 raf.read(b);
44 String mpc = new String(b);
45 if (!mpc.equals("MP+") && mpc.equals("ID3")) {
46
47
48
49 raf.seek(6);
50 int tagSize = read_syncsafe_integer(raf);
51 raf.seek(tagSize+10);
52
53
54 b = new byte[3];
55 raf.read(b);
56 mpc = new String(b);
57 if (!mpc.equals("MP+")) {
58
59 throw new CannotReadException("MP+ Header not found");
60 }
61 } else if (!mpc.equals("MP+")){
62 throw new CannotReadException("MP+ Header not found");
63 }
64
65 b = new byte[25];
66 raf.read(b);
67 MpcHeader mpcH = new MpcHeader(b);
68
69
70
71 double pcm = mpcH.getSamplesNumber();
72
73 info.setPreciseLength( (float) ( pcm * 1152 / mpcH.getSamplingRate() ) );
74 info.setChannelNumber( mpcH.getChannelNumber() );
75 info.setSamplingRate( mpcH.getSamplingRate() );
76 info.setEncodingType( mpcH.getEncodingType() );
77 info.setExtraEncodingInfos( mpcH.getEncoderInfo() );
78 info.setBitrate( computeBitrate( info.getLength(), raf.length() ) );
79
80 return info;
81 }
82
83 private int read_syncsafe_integer(RandomAccessFile raf) throws IOException {
84 int value = 0;
85
86 value += (raf.read()& 0xFF) << 21;
87 value += (raf.read()& 0xFF) << 14;
88 value += (raf.read()& 0xFF) << 7;
89 value += raf.read() & 0xFF;
90
91 return value;
92 }
93
94 private int computeBitrate( int length, long size ) {
95 return (int) ( ( size / 1000 ) * 8 / length );
96 }
97 }
98