Debian Codegraph @

plugins/wdx/audioinfo/src/atl/Musepack.pas

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TMPEGplus - for manipulating with Musepack file information }
5 { }
6 { http://mac.sourceforge.net/atl/ }
7 { e-mail: [email protected] }
8 { }
9 { Copyright (c) 2000-2002 by Jurgen Faul }
10 { Copyright (c) 2003-2005 by The MAC Team }
11 { }
12 { Version 2.0 (April 2005) by Gambit }
13 { - updated to unicode file access }
14 { }
15 { Version 1.9 (13 April 2004) by Gambit }
16 { - Added Ratio property }
17 { }
18 { Version 1.81 (27 September 2003) }
19 { - changed minimal allowed bitrate to '3' (e.g. encoded digital silence) }
20 { }
21 { Version 1.8 (20 August 2003) by Madah }
22 { - Will now read files with different samplerates correctly }
23 { - Also changed GetProfileID() for this to work }
24 { - Added the ability to determine encoder used }
25 { }
26 { Version 1.7 (7 June 2003) by Gambit }
27 { - --quality 0 to 10 detection (all profiles) }
28 { - Stream Version 7.1 detected and supported }
29 { }
30 { Version 1.6 (8 February 2002) }
31 { - Fixed bug with property Corrupted }
32 { }
33 { Version 1.2 (2 August 2001) }
34 { - Some class properties added/changed }
35 { }
36 { Version 1.1 (26 July 2001) }
37 { - Fixed reading problem with "read only" files }
38 { }
39 { Version 1.0 (23 May 2001) }
40 { - Support for MPEGplus files (stream versions 4-7) }
41 { - Class TID3v1: reading & writing support for ID3v1 tags }
42 { - Class TID3v2: reading & writing support for ID3v2 tags }
43 { - Class TAPEtag: reading & writing support for APE tags }
44 { }
45 { This library is free software; you can redistribute it and/or }
46 { modify it under the terms of the GNU Lesser General Public }
47 { License as published by the Free Software Foundation; either }
48 { version 2.1 of the License, or (at your option) any later version. }
49 { }
50 { This library is distributed in the hope that it will be useful, }
51 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
52 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
53 { Lesser General Public License for more details. }
54 { }
55 { You should have received a copy of the GNU Lesser General Public }
56 { License along with this library; if not, write to the Free Software }
57 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
58 { }
59 { *************************************************************************** }
60
61 unit Musepack;
62
63 interface
64
65 uses
66 Classes, SysUtils, ID3v1, ID3v2, APEtag, DCClassesUtf8;
67
68 const
69 { Used with ChannelModeID property }
70 MPP_CM_STEREO = 1; { Index for stereo mode }
71 MPP_CM_JOINT_STEREO = 2; { Index for joint-stereo mode }
72
73 { Channel mode names }
74 MPP_MODE: array [0..2] of string = ('Unknown', 'Stereo', 'Joint Stereo');
75
76 { Used with ProfileID property }
77 MPP_PROFILE_QUALITY0 = 9; { '--quality 0' profile }
78 MPP_PROFILE_QUALITY1 = 10; { '--quality 1' profile }
79 MPP_PROFILE_TELEPHONE = 11; { 'Telephone' profile }
80 MPP_PROFILE_THUMB = 1; { 'Thumb' (poor) quality }
81 MPP_PROFILE_RADIO = 2; { 'Radio' (normal) quality }
82 MPP_PROFILE_STANDARD = 3; { 'Standard' (good) quality }
83 MPP_PROFILE_XTREME = 4; { 'Xtreme' (very good) quality }
84 MPP_PROFILE_INSANE = 5; { 'Insane' (excellent) quality }
85 MPP_PROFILE_BRAINDEAD = 6; { 'BrainDead' (excellent) quality }
86 MPP_PROFILE_QUALITY9 = 7; { '--quality 9' (excellent) quality }
87 MPP_PROFILE_QUALITY10 = 8; { '--quality 10' (excellent) quality }
88 MPP_PROFILE_UNKNOWN = 0; { Unknown profile }
89 MPP_PROFILE_EXPERIMENTAL = 12;
90 { Profile names }
91 MPP_PROFILE: array [0..12] of string =
92 ('Unknown', 'Thumb', 'Radio', 'Standard', 'Xtreme', 'Insane', 'BrainDead',
93 '--quality 9', '--quality 10', '--quality 0', '--quality 1', 'Telephone', 'Experimental');
94
95 type
96 { Class TMPEGplus }
97 TMPEGplus = class(TObject)
98 private
99 { Private declarations }
100 FValid: Boolean;
101 FChannelModeID: Byte;
102 FFileSize: Integer;
103 FFrameCount: Integer;
104 FSampleRate: Integer;
105 FBitRate: Word;
106 FStreamVersion: Byte;
107 FProfileID: Byte;
108 FID3v1: TID3v1;
109 FID3v2: TID3v2;
110 FAPEtag: TAPEtag;
111 FEncoder : string;
112 procedure FResetData;
113 function FGetChannelMode: string;
114 function FGetBitRate: Word;
115 function FGetProfile: string;
116 function FGetDuration: Double;
117 function FIsCorrupted: Boolean;
118 function FGetRatio: Double;
119 function FGetStreamStreamVersionString: String;
120 public
121 { Public declarations }
122 constructor Create; { Create object }
123 destructor Destroy; override; { Destroy object }
124 function ReadFromFile(const FileName: String): Boolean;{ Load header }
125 property Valid: Boolean read FValid; { True if header valid }
126 property ChannelModeID: Byte read FChannelModeID; { Channel mode code }
127 property ChannelMode: string read FGetChannelMode; { Channel mode name }
128 property FileSize: Integer read FFileSize; { File size (bytes) }
129 property FrameCount: Integer read FFrameCount; { Number of frames }
130 property BitRate: Word read FGetBitRate; { Bit rate }
131 property StreamVersion: Byte read FStreamVersion; { Stream version }
132 property StreamStreamVersionString: String read FGetStreamStreamVersionString;
133 property SampleRate: Integer read FSampleRate;
134 property ProfileID: Byte read FProfileID; { Profile code }
135 property Profile: string read FGetProfile; { Profile name }
136 property ID3v1: TID3v1 read FID3v1; { ID3v1 tag data }
137 property ID3v2: TID3v2 read FID3v2; { ID3v2 tag data }
138 property APEtag: TAPEtag read FAPEtag; { APE tag data }
139 property Duration: Double read FGetDuration; { Duration (seconds) }
140 property Corrupted: Boolean read FIsCorrupted; { True if file corrupted }
141 property Encoder: string read FEncoder; { Encoder used }
142 property Ratio: Double read FGetRatio; { Compression ratio (%) }
143 end;
144
145 implementation
146
147 const
148 { ID code for stream version 7 and 7.1 }
149 STREAM_VERSION_7_ID = 120279117; { 120279117 = 'MP+' + #7 }
150 STREAM_VERSION_71_ID = 388714573; { 388714573 = 'MP+' + #23 }
151
152 type
153 { File header data - for internal use }
154 HeaderRecord = record
155 ByteArray: array [1..32] of Byte; { Data as byte array }
156 IntegerArray: array [1..8] of Integer; { Data as integer array }
157 FileSize: Integer; { File size }
158 ID3v2Size: Integer; { ID3v2 tag size (bytes) }
159 end;
160
161 { ********************* Auxiliary functions & procedures ******************** }
162
163 function ReadHeader(const FileName: String; var Header: HeaderRecord): Boolean;
164 var
165 SourceFile: TFileStreamEx;
166 Transferred: Integer;
167 begin
168 try
169 Result := true;
170 { Set read-access and open file }
171 SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
172 SourceFile.Seek(Header.ID3v2Size, soFromBeginning);
173 { Read header and get file size }
174 Transferred := SourceFile.Read(Header, 32);
175 Header.FileSize := SourceFile.Size;
176 SourceFile.Free;
177 { if transfer is not complete }
178 if Transferred < 32 then Result := false
179 else Move(Header.ByteArray, Header.IntegerArray, SizeOf(Header.ByteArray));
180 except
181 { Error }
182 Result := false;
183 end;
184 end;
185
186 { --------------------------------------------------------------------------- }
187
188 function GetStreamVersion(const Header: HeaderRecord): Byte;
189 begin
190 { Get MPEGplus stream version }
191 if Header.IntegerArray[1] = STREAM_VERSION_7_ID then
192 Result := 7
193 else if Header.IntegerArray[1] = STREAM_VERSION_71_ID then
194 Result := 71
195 else
196 case (Header.ByteArray[2] mod 32) div 2 of
197 3: Result := 4;
198 7: Result := 5;
199 11: Result := 6
200 else Result := 0;
201 end;
202 end;
203
204 { --------------------------------------------------------------------------- }
205
206 function GetSampleRate(const Header: HeaderRecord): Integer;
207 const mpp_samplerates : array[0..3] of integer = ( 44100, 48000, 37800, 32000 );
208 begin
209 (* get samplerate from header
210 note: this is the same byte where profile is stored
211 *)
212 Result := mpp_samplerates[Header.ByteArray[11] and 3];
213 end;
214
215 { --------------------------------------------------------------------------- }
216
217 function GetEncoder(const Header: HeaderRecord): string;
218 var EncoderID : integer;
219 begin
220 EncoderID := Header.ByteArray[11+2+15];
221 Result := '';
222 if EncoderID = 0 then begin
223 //FEncoder := 'Buschmann 1.7.0...9, Klemm 0.90...1.05';
224 end else begin
225 case ( EncoderID mod 10 ) of
226 0: Result := format('%u.%u Release', [EncoderID div 100, (EncoderID div 10) mod 10]);
227 2,4,6,8 : Result := format('%u.%.2u Beta', [EncoderID div 100, EncoderID mod 100] );
228 else Result := format('%u.%.2u --Alpha--', [EncoderID div 100, EncoderID mod 100] );
229 end;
230 end;
231 end;
232
233 { --------------------------------------------------------------------------- }
234
235 function GetChannelModeID(const Header: HeaderRecord): Byte;
236 begin
237 if (GetStreamVersion(Header) = 7) or (GetStreamVersion(Header) = 71) then
238 { Get channel mode for stream version 7 }
239 if (Header.ByteArray[12] mod 128) < 64 then Result := MPP_CM_STEREO
240 else Result := MPP_CM_JOINT_STEREO
241 else
242 { Get channel mode for stream version 4-6 }
243 if (Header.ByteArray[3] mod 128) = 0 then Result := MPP_CM_STEREO
244 else Result := MPP_CM_JOINT_STEREO;
245 end;
246
247 { --------------------------------------------------------------------------- }
248
249 function GetFrameCount(const Header: HeaderRecord): Integer;
250 begin
251 { Get frame count }
252 case GetStreamVersion(Header) of
253 4: Result := Header.IntegerArray[2] shr 16;
254 5..71: Result := Header.IntegerArray[2];
255 else Result := 0;
256 end;
257 end;
258
259 { --------------------------------------------------------------------------- }
260
261 function GetBitRate(const Header: HeaderRecord): Word;
262 begin
263 { Try to get bit rate }
264 case GetStreamVersion(Header) of
265 4, 5: Result := Header.IntegerArray[1] shr 23;
266 else Result := 0;
267 end;
268 end;
269
270 { --------------------------------------------------------------------------- }
271
272 function GetProfileID(const Header: HeaderRecord): Byte;
273 begin
274 Result := MPP_PROFILE_UNKNOWN;
275 { Get MPEGplus profile (exists for stream version 7 only) }
276 if (GetStreamVersion(Header) = 7) or (GetStreamVersion(Header) = 71) then
277 // ((and $F0) shr 4) is needed because samplerate is stored in the same byte!
278 case ((Header.ByteArray[11] and $F0) shr 4) of
279 1: Result := MPP_PROFILE_EXPERIMENTAL;
280 5: Result := MPP_PROFILE_QUALITY0;
281 6: Result := MPP_PROFILE_QUALITY1;
282 7: Result := MPP_PROFILE_TELEPHONE;
283 8: Result := MPP_PROFILE_THUMB;
284 9: Result := MPP_PROFILE_RADIO;
285 10: Result := MPP_PROFILE_STANDARD;
286 11: Result := MPP_PROFILE_XTREME;
287 12: Result := MPP_PROFILE_INSANE;
288 13: Result := MPP_PROFILE_BRAINDEAD;
289 14: Result := MPP_PROFILE_QUALITY9;
290 15: Result := MPP_PROFILE_QUALITY10;
291 end;
292 end;
293
294 { ********************** Private functions & procedures ********************* }
295
296 procedure TMPEGplus.FResetData;
297 begin
298 FValid := false;
299 FChannelModeID := 0;
300 FFileSize := 0;
301 FFrameCount := 0;
302 FBitRate := 0;
303 FStreamVersion := 0;
304 FSampleRate := 0;
305 FEncoder := '';
306 FProfileID := MPP_PROFILE_UNKNOWN;
307 FID3v1.ResetData;
308 FID3v2.ResetData;
309 FAPEtag.ResetData;
310 end;
311
312 { --------------------------------------------------------------------------- }
313
314 function TMPEGplus.FGetChannelMode: string;
315 begin
316 Result := MPP_MODE[FChannelModeID];
317 end;
318
319 { --------------------------------------------------------------------------- }
320
321 function TMPEGplus.FGetBitRate: Word;
322 var
323 CompressedSize: Integer;
324 begin
325 Result := FBitRate;
326 { Calculate bit rate if not given }
327 CompressedSize := FFileSize - FID3v2.Size - FAPEtag.Size;
328 if FID3v1.Exists then Dec(FFileSize, 128);
329 if (Result = 0) and (FFrameCount > 0) then
330 Result := Round(CompressedSize * 8 * (FSampleRate/1000) / FFRameCount / 1152);
331 end;
332
333 { --------------------------------------------------------------------------- }
334
335 function TMPEGplus.FGetProfile: string;
336 begin
337 Result := MPP_PROFILE[FProfileID];
338 end;
339
340 { --------------------------------------------------------------------------- }
341
342 function TMPEGplus.FGetDuration: Double;
343 begin
344 { Calculate duration time }
345 if FSampleRate > 0 then
346 Result := FFRameCount * 1152 / FSampleRate
347 else Result := 0;
348 end;
349
350 { --------------------------------------------------------------------------- }
351
352 function TMPEGplus.FIsCorrupted: Boolean;
353 begin
354 { Check for file corruption }
355 Result := (FValid) and ((FGetBitRate < 3) or (FGetBitRate > 480));
356 end;
357
358 { ********************** Public functions & procedures ********************** }
359
360 constructor TMPEGplus.Create;
361 begin
362 inherited;
363 FID3v1 := TID3v1.Create;
364 FID3v2 := TID3v2.Create;
365 FAPEtag := TAPEtag.Create;
366 FResetData;
367 end;
368
369 { --------------------------------------------------------------------------- }
370
371 destructor TMPEGplus.Destroy;
372 begin
373 FID3v1.Free;
374 FID3v2.Free;
375 FAPEtag.Free;
376 inherited;
377 end;
378
379 { --------------------------------------------------------------------------- }
380
381 function TMPEGplus.ReadFromFile(const FileName: String): Boolean;
382 var
383 Header: HeaderRecord;
384 begin
385 { Reset data and load header from file to variable }
386 FResetData;
387 FillChar(Header, SizeOf(Header), 0);
388 { At first try to load ID3v2 tag data, then header }
389 if FID3v2.ReadFromFile(FileName) then Header.ID3v2Size := FID3v2.Size;
390 Result := ReadHeader(FileName, Header);
391 { Process data if loaded and file valid }
392 if (Result) and (Header.FileSize > 0) and (GetStreamVersion(Header) > 0) then
393 begin
394 FValid := true;
395 { Fill properties with header data }
396 FSampleRate := GetSampleRate(Header);
397 FChannelModeID := GetChannelModeID(Header);
398 FFileSize := Header.FileSize;
399 FFrameCount := GetFrameCount(Header);
400 FBitRate := GetBitRate(Header);
401 FStreamVersion := GetStreamVersion(Header);
402 FProfileID := GetProfileID(Header);
403 FEncoder := GetEncoder(Header);
404 FID3v1.ReadFromFile(FileName);
405 FAPEtag.ReadFromFile(FileName);
406 end;
407 end;
408
409 { --------------------------------------------------------------------------- }
410
411 function TMPEGplus.FGetRatio: Double;
412 begin
413 { Get compression ratio }
414 if (FValid) and ((FChannelModeID = MPP_CM_STEREO) or (FChannelModeID = MPP_CM_JOINT_STEREO)) then
415 Result := FFileSize / ((FFrameCount * 1152) * (2 * 16 / 8) + 44) * 100
416 else
417 Result := 0;
418 end;
419
420 { --------------------------------------------------------------------------- }
421
422 function TMPEGplus.FGetStreamStreamVersionString: String;
423 begin
424 case FStreamVersion of
425 4: Result := '4.0';
426 5: Result := '5.0';
427 6: Result := '6.0';
428 7: Result := '7.0';
429 71: Result := '7.1';
430 else Result := IntToStr(FStreamVersion);
431 end;
432 end;
433
434 { --------------------------------------------------------------------------- }
435
436 end.