1 (**************************************************************************)
2 (* Copyright 2003, 2002 b8_bavard, b8_zoggy, , b52_simon INRIA *)
4 (* This file is part of mldonkey. *)
6 (* mldonkey is free software; you can redistribute it and/or modify *)
7 (* it under the terms of the GNU General Public License as published *)
8 (* by the Free Software Foundation; either version 2 of the License, *)
9 (* or (at your option) any later version. *)
11 (* mldonkey is distributed in the hope that it will be useful, *)
12 (* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
13 (* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
14 (* GNU General Public License for more details. *)
16 (* You should have received a copy of the GNU General Public License *)
17 (* along with mldonkey; if not, write to the Free Software *)
18 (* Foundation, Inc., 59 Temple Place, Suite 330, Boston, *)
19 (* MA 02111-1307 USA *)
21 (**************************************************************************)
29 type mp3_encoding
= CBR
| VBR
32 { duration
: int; (** in seconds *)
33 samplerate
: int; (** in kilobits per second *)
34 mode
: channel_mode
; (** stereo, mono, etc *)
35 bitrate
: int; (** in kilobits per second *)
36 encoding
: mp3_encoding
; (** variable or constant bit rate *)
37 filesize
: int (** in bytes *)
41 let h0 = Char.code h
.[0]
42 and h1
= Char.code h
.[1]
43 and h2
= Char.code h
.[2] in
45 h1
land 0xE0 = 0xE0 &&
46 h1
land 0x18 <> 0x08 &&
47 h1
land 0x06 <> 0x00 &&
48 h2
land 0xF0 <> 0xF0 &&
52 [| [| [|0; 32; 64; 96; 128; 160; 192; 224; 256; 288; 320; 352; 384; 416; 448|];
53 [|0; 32; 48; 56; 64; 80; 96; 112; 128; 160; 192; 224; 256; 320; 384|];
54 [|0; 32; 40; 48; 56; 64; 80; 96; 112; 128; 160; 192; 224; 256; 320|]
56 [| [|0; 32; 48; 56; 64; 80; 96; 112; 128; 144; 160; 176; 192; 224; 256|];
57 [|0; 8; 16; 24; 32; 40; 48; 56; 64; 80; 96; 112; 128; 144; 160|];
58 [|0; 8; 16; 24; 32; 40; 48; 56; 64; 80; 96; 112; 128; 144; 160|]
63 [|44100; 48000; 32000; 22050; 24000; 16000; 11025; 12000; 8000|]
66 [|0; 384; 1152; 1152|]
69 let b1 = input_byte ic
in let b2 = input_byte ic
in
70 let b3 = input_byte ic
in let b4 = input_byte ic
in
71 (b1 lsl 24) lor (b2 lsl 16) lor (b3 lsl 8) lor b4
73 let get_xing_header ic header
=
74 let id = (header
lsr 19) land 1
75 and mode
= (header
lsr 6) land 3 in
78 then if mode
<> 3 then 32 else 17
79 else if mode
<> 3 then 17 else 9 in
80 seek_in ic
(pos_in ic
+ offset);
81 let buf = String.create
4 in
82 really_input ic
buf 0 4;
83 if buf <> "Xing" then raise Not_found
;
84 let flags = read_i4 ic
in
85 (* 3 = FRAMES_FLAG | BYTES_FLAG *)
86 if flags land 3 <> 3 then raise Not_found
;
87 let frames = read_i4 ic
in
88 let bytes = read_i4 ic
in
93 let buf = String.create
4 in
94 really_input ic
buf 0 4;
95 while not
(check_head buf) do
96 String.blit
buf 1 buf 0 3;
97 buf.[3] <- input_char ic
100 (Char.code
buf.[1] lsl 16) lor
101 (Char.code
buf.[2] lsl 8) lor
102 (Char.code
buf.[3]) in
104 if header land 0x100000 <> 0
105 then ((if header land 0x80000 = 0 then 1 else 0), false)
107 let lay = 4 - ((header lsr 17) land 3) in
108 let sampling_frequency =
110 then 6 + ((header lsr 10) land 3)
111 else ((header lsr 10) land 3) + 3 * lsf
in
113 mpg123_freqs.(sampling_frequency) in
114 let bitrate_index = (header lsr 12) land 0xF in
115 (* let padding = (header lsr 9) land 1 in *)
117 match (header lsr 6) land 3 with
118 0 -> Stereo
| 1 -> Joint_stereo
| 2 -> Dual_channel_stereo
| _
-> Mono
in
120 float mpg123_bs.(lay) /. float (max
1 (sample_rate lsl lsf
)) in
122 float tabsel_123.(lsf
).(lay - 1).(bitrate_index) *.
123 (if lay = 1 then 12000.0 *. 4.0 else 144000.0) /.
124 float (max
1 (sample_rate lsl lsf
)) in
125 let filesize = in_channel_length ic
in
126 let (enc
, duration
, bitrate
) =
128 let (frames, bytes) = get_xing_header ic
header in
131 truncate
(float bytes *. 8.0 /. (
132 max
0.00001 (tpf *. float frames *. 1000.0))))
134 let len = filesize - pos_in ic
in
136 float len /. (max
0.00001 bpf) *. tpf,
137 tabsel_123.(lsf
).(lay - 1).(bitrate_index)) in
138 { duration
= truncate duration
;
139 samplerate
= sample_rate / 1000;
143 filesize = filesize }
147 if bitrate_index = 0 then 0 else
150 let a = tabsel_123.(lsf).(0).(bitrate_index) * 12000 in
151 let b = a / mpg123_freqs.(sampling_frequency) in
152 ((b + padding) lsl 2) - 4
154 let a = tabsel_123.(lsf).(1).(bitrate_index) * 144000 in
155 let b = a / mpg123_freqs.(sampling_frequency) in
158 let a = tabsel_123.(lsf).(1).(bitrate_index) * 144000 in
159 let b = a / (mpg123_freqs.(sampling_frequency) lsl lsf) in
166 let ic = open_in_bin filename
in
168 let res = for_channel ic in close_in
ic; res