Debian Codegraph @

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

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TMPEGaudio - for manipulating with MPEG audio 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.1 (April 2005) by Gambit }
13 { - updated to unicode file access }
14 { }
15 { Version 2.00 (December 2004) by e-w@re }
16 { - returns the correct MPEG data position in file }
17 { - added property MPEGstart -> returns start of MPEG data in file }
18 { - added property MPEGend -> returns end of MPEG data in file }
19 { }
20 { Version 1.99 (April 2004) by Gambit }
21 { - Improved LAME detection }
22 { (checks for the LAME string in the padding) }
23 { }
24 { Version 1.91 (April 2004) by Gambit }
25 { - Added Ratio property }
26 { }
27 { Version 1.9 (22 February 2004) by Gambit }
28 { - Added Samples property }
29 { }
30 { Version 1.8 (29 June 2003) by Gambit }
31 { - Reads ape tags in mp3 files }
32 { }
33 { Version 1.7 (4 November 2002) }
34 { - Ability to recognize QDesign MPEG audio encoder }
35 { - Fixed bug with MPEG Layer II }
36 { - Fixed bug with very big files }
37 { }
38 { Version 1.6 (23 May 2002) }
39 { - Improved reading performance (up to 50% faster) }
40 { }
41 { Version 1.1 (11 September 2001) }
42 { - Improved encoder guessing for CBR files }
43 { }
44 { Version 1.0 (31 August 2001) }
45 { - Support for MPEG audio (versions 1, 2, 2.5, layers I, II, III) }
46 { - Support for Xing & FhG VBR }
47 { - Ability to guess audio encoder (Xing, FhG, LAME, Blade, GoGo, Shine) }
48 { - Class TID3v1: reading & writing support for ID3v1 tags }
49 { - Class TID3v2: reading & writing support for ID3v2 tags }
50 { }
51 { This library is free software; you can redistribute it and/or }
52 { modify it under the terms of the GNU Lesser General Public }
53 { License as published by the Free Software Foundation; either }
54 { version 2.1 of the License, or (at your option) any later version. }
55 { }
56 { This library is distributed in the hope that it will be useful, }
57 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
58 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
59 { Lesser General Public License for more details. }
60 { }
61 { You should have received a copy of the GNU Lesser General Public }
62 { License along with this library; if not, write to the Free Software }
63 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
64 { }
65 { *************************************************************************** }
66
67 unit MPEGaudio;
68
69 interface
70
71 uses
72 Classes, SysUtils, ID3v1, ID3v2, APEtag;
73
74 const
75 { Table for bit rates }
76 MPEG_BIT_RATE: array [0..3, 0..3, 0..15] of Word =
77 (
78 { For MPEG 2.5 }
79 ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
80 (0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0),
81 (0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0),
82 (0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0)),
83 { Reserved }
84 ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
85 (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
86 (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
87 (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)),
88 { For MPEG 2 }
89 ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
90 (0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0),
91 (0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0),
92 (0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0)),
93 { For MPEG 1 }
94 ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
95 (0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0),
96 (0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0),
97 (0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0))
98 );
99
100 { Sample rate codes }
101 MPEG_SAMPLE_RATE_LEVEL_3 = 0; { Level 3 }
102 MPEG_SAMPLE_RATE_LEVEL_2 = 1; { Level 2 }
103 MPEG_SAMPLE_RATE_LEVEL_1 = 2; { Level 1 }
104 MPEG_SAMPLE_RATE_UNKNOWN = 3; { Unknown value }
105
106 { Table for sample rates }
107 MPEG_SAMPLE_RATE: array [0..3, 0..3] of Word =
108 (
109 (11025, 12000, 8000, 0), { For MPEG 2.5 }
110 (0, 0, 0, 0), { Reserved }
111 (22050, 24000, 16000, 0), { For MPEG 2 }
112 (44100, 48000, 32000, 0) { For MPEG 1 }
113 );
114
115 { VBR header ID for Xing/FhG }
116 VBR_ID_XING = 'Xing'; { Xing VBR ID }
117 VBR_ID_FHG = 'VBRI'; { FhG VBR ID }
118
119 { MPEG version codes }
120 MPEG_VERSION_2_5 = 0; { MPEG 2.5 }
121 MPEG_VERSION_UNKNOWN = 1; { Unknown version }
122 MPEG_VERSION_2 = 2; { MPEG 2 }
123 MPEG_VERSION_1 = 3; { MPEG 1 }
124
125 { MPEG version names }
126 MPEG_VERSION: array [0..3] of string =
127 ('MPEG 2.5', 'MPEG ?', 'MPEG 2', 'MPEG 1');
128
129 { MPEG layer codes }
130 MPEG_LAYER_UNKNOWN = 0; { Unknown layer }
131 MPEG_LAYER_III = 1; { Layer III }
132 MPEG_LAYER_II = 2; { Layer II }
133 MPEG_LAYER_I = 3; { Layer I }
134
135 { MPEG layer names }
136 MPEG_LAYER: array [0..3] of string =
137 ('Layer ?', 'Layer III', 'Layer II', 'Layer I');
138
139 { Channel mode codes }
140 MPEG_CM_STEREO = 0; { Stereo }
141 MPEG_CM_JOINT_STEREO = 1; { Joint Stereo }
142 MPEG_CM_DUAL_CHANNEL = 2; { Dual Channel }
143 MPEG_CM_MONO = 3; { Mono }
144 MPEG_CM_UNKNOWN = 4; { Unknown mode }
145
146 { Channel mode names }
147 MPEG_CM_MODE: array [0..4] of string =
148 ('Stereo', 'Joint Stereo', 'Dual Channel', 'Mono', 'Unknown');
149
150 { Extension mode codes (for Joint Stereo) }
151 MPEG_CM_EXTENSION_OFF = 0; { IS and MS modes set off }
152 MPEG_CM_EXTENSION_IS = 1; { Only IS mode set on }
153 MPEG_CM_EXTENSION_MS = 2; { Only MS mode set on }
154 MPEG_CM_EXTENSION_ON = 3; { IS and MS modes set on }
155 MPEG_CM_EXTENSION_UNKNOWN = 4; { Unknown extension mode }
156
157 { Emphasis mode codes }
158 MPEG_EMPHASIS_NONE = 0; { None }
159 MPEG_EMPHASIS_5015 = 1; { 50/15 ms }
160 MPEG_EMPHASIS_UNKNOWN = 2; { Unknown emphasis }
161 MPEG_EMPHASIS_CCIT = 3; { CCIT J.17 }
162
163 { Emphasis names }
164 MPEG_EMPHASIS: array [0..3] of string =
165 ('None', '50/15 ms', 'Unknown', 'CCIT J.17');
166
167 { Encoder codes }
168 MPEG_ENCODER_UNKNOWN = 0; { Unknown encoder }
169 MPEG_ENCODER_XING = 1; { Xing }
170 MPEG_ENCODER_FHG = 2; { FhG }
171 MPEG_ENCODER_LAME = 3; { LAME }
172 MPEG_ENCODER_BLADE = 4; { Blade }
173 MPEG_ENCODER_GOGO = 5; { GoGo }
174 MPEG_ENCODER_SHINE = 6; { Shine }
175 MPEG_ENCODER_QDESIGN = 7; { QDesign }
176
177 { Encoder names }
178 MPEG_ENCODER: array [0..7] of string =
179 ('Unknown', 'Xing', 'FhG', 'LAME', 'Blade', 'GoGo', 'Shine', 'QDesign');
180
181 type
182 hFileInt = Integer;
183
184 { Xing/FhG VBR header data }
185 VBRData = record
186 Found: Boolean; { True if VBR header found }
187 ID: array [1..4] of Char; { Header ID: "Xing" or "VBRI" }
188 Frames: Integer; { Total number of frames }
189 Bytes: Integer; { Total number of bytes }
190 Scale: Byte; { VBR scale (1..100) }
191 VendorID: string; { Vendor ID (if present) }
192 end;
193
194 { MPEG frame header data}
195 FrameData = record
196 Found: Boolean; { True if frame found }
197 Position: Integer; { Frame position in the file }
198 Size: Word; { Frame size (bytes) }
199 Xing: Boolean; { True if Xing encoder }
200 Data: array [1..4] of Byte; { The whole frame header data }
201 VersionID: Byte; { MPEG version ID }
202 LayerID: Byte; { MPEG layer ID }
203 ProtectionBit: Boolean; { True if protected by CRC }
204 BitRateID: Word; { Bit rate ID }
205 SampleRateID: Word; { Sample rate ID }
206 PaddingBit: Boolean; { True if frame padded }
207 PrivateBit: Boolean; { Extra information }
208 ModeID: Byte; { Channel mode ID }
209 ModeExtensionID: Byte; { Mode extension ID (for Joint Stereo) }
210 CopyrightBit: Boolean; { True if audio copyrighted }
211 OriginalBit: Boolean; { True if original media }
212 EmphasisID: Byte; { Emphasis ID }
213 end;
214
215 { Class TMPEGaudio }
216 TMPEGaudio = class(TObject)
217 private
218 { Private declarations }
219 FFileLength: Integer;
220 FVendorID: string;
221 FVBR: VBRData;
222 FFrame: FrameData;
223 FMPEGStart: Int64;
224 FMPEGEnd: Int64;
225 FAudioSizeTag: Int64;
226 FID3v1: TID3v1;
227 FID3v2: TID3v2;
228 FAPEtag: TAPEtag;
229 procedure FResetData;
230 function FGetVersion: string;
231 function FGetLayer: string;
232 function FGetBitRate: Word;
233 function FGetSampleRate: Word;
234 function FGetChannelMode: string;
235 function FGetEmphasis: string;
236 function FGetFrames: Integer;
237 function FGetDuration: Double;
238 function FGetVBREncoderID: Byte;
239 function FGetCBREncoderID: Byte;
240 function FGetEncoderID: Byte;
241 function FGetEncoder: string;
242 function FGetValid: Boolean;
243 function FGetSamples: Cardinal;
244 function FGetRatio: Double;
245 public
246 { Public declarations }
247 constructor Create; { Create object }
248 destructor Destroy; override; { Destroy object }
249 function ReadFromFile(const FileName: String): Boolean; { Load data }
250 property FileLength: Integer read FFileLength; { File length (bytes) }
251 property VBR: VBRData read FVBR; { VBR header data }
252 property Frame: FrameData read FFrame; { Frame header data }
253 property ID3v1: TID3v1 read FID3v1; { ID3v1 tag data }
254 property ID3v2: TID3v2 read FID3v2; { ID3v2 tag data }
255 property APEtag: TAPEtag read FAPEtag; { APE tag data }
256 property Version: string read FGetVersion; { MPEG version name }
257 property Layer: string read FGetLayer; { MPEG layer name }
258 property BitRate: Word read FGetBitRate; { Bit rate (kbit/s) }
259 property SampleRate: Word read FGetSampleRate; { Sample rate (hz) }
260 property ChannelMode: string read FGetChannelMode; { Channel mode name }
261 property Emphasis: string read FGetEmphasis; { Emphasis name }
262 property Frames: Integer read FGetFrames; { Total number of frames }
263 property Duration: Double read FGetDuration; { Song duration (sec) }
264 property EncoderID: Byte read FGetEncoderID; { Guessed encoder ID }
265 property Encoder: string read FGetEncoder; { Guessed encoder name }
266 property Valid: Boolean read FGetValid; { True if MPEG file valid }
267 property Samples: Cardinal read FGetSamples;
268 property Ratio: Double read FGetRatio; { Compression ratio (%) }
269 property MPEGStart: Int64 read FMPEGStart;{Returns start pos of MPEG data}
270 property MPEGEnd: Int64 read FMPEGEnd; { Returns end pos of MPEG data }
271 property AudioSizeTag: Int64 read FAudioSizeTag; { Returns ID3v2 TSIZ value or 0 }
272 end;
273
274 implementation
275
276 uses
277 DCOSUtils;
278
279 const
280 { Limitation constants }
281 MAX_MPEG_FRAME_LENGTH = 1729; { Max. MPEG frame length }
282 MIN_MPEG_BIT_RATE = 8; { Min. bit rate value }
283 MAX_MPEG_BIT_RATE = 448; { Max. bit rate value }
284 MIN_ALLOWED_DURATION = 0.1; { Min. song duration value }
285
286 { VBR Vendor ID strings }
287 VENDOR_ID_LAME = 'LAME'; { For LAME }
288 VENDOR_ID_GOGO_NEW = 'GOGO'; { For GoGo (New) }
289 VENDOR_ID_GOGO_OLD = 'MPGE'; { For GoGo (Old) }
290
291 hINVALID_HANDLE_VALUE = hFileInt(-1);
292
293 { ********************* Auxiliary functions & procedures ******************** }
294
295 function IsFrameHeader(const HeaderData: array of Byte): Boolean;
296 begin
297 { Check for valid frame header }
298 if ((HeaderData[0] and $FF) <> $FF) or
299 ((HeaderData[1] and $E0) <> $E0) or
300 (((HeaderData[1] shr 3) and 3) = 1) or
301 (((HeaderData[1] shr 1) and 3) = 0) or
302 ((HeaderData[2] and $F0) = $F0) or
303 ((HeaderData[2] and $F0) = 0) or
304 (((HeaderData[2] shr 2) and 3) = 3) or
305 ((HeaderData[3] and 3) = 2) then
306 Result := false
307 else
308 Result := true;
309 end;
310
311 { --------------------------------------------------------------------------- }
312
313 procedure DecodeHeader(const HeaderData: array of Byte; var Frame: FrameData);
314 begin
315 { Decode frame header data }
316 Move(HeaderData, Frame.Data, SizeOf(Frame.Data));
317 Frame.VersionID := (HeaderData[1] shr 3) and 3;
318 Frame.LayerID := (HeaderData[1] shr 1) and 3;
319 Frame.ProtectionBit := (HeaderData[1] and 1) <> 1;
320 Frame.BitRateID := HeaderData[2] shr 4;
321 Frame.SampleRateID := (HeaderData[2] shr 2) and 3;
322 Frame.PaddingBit := ((HeaderData[2] shr 1) and 1) = 1;
323 Frame.PrivateBit := (HeaderData[2] and 1) = 1;
324 Frame.ModeID := (HeaderData[3] shr 6) and 3;
325 Frame.ModeExtensionID := (HeaderData[3] shr 4) and 3;
326 Frame.CopyrightBit := ((HeaderData[3] shr 3) and 1) = 1;
327 Frame.OriginalBit := ((HeaderData[3] shr 2) and 1) = 1;
328 Frame.EmphasisID := HeaderData[3] and 3;
329 end;
330
331 { --------------------------------------------------------------------------- }
332
333 function ValidFrameAt(const Index: Word; Data: array of Byte): Boolean;
334 var
335 HeaderData: array [1..4] of Byte;
336 begin
337 { Check for frame at given position }
338 HeaderData[1] := Data[Index];
339 HeaderData[2] := Data[Index + 1];
340 HeaderData[3] := Data[Index + 2];
341 HeaderData[4] := Data[Index + 3];
342 if IsFrameHeader(HeaderData) then Result := true
343 else Result := false;
344 end;
345
346 { --------------------------------------------------------------------------- }
347
348 function GetCoefficient(const Frame: FrameData): Byte;
349 begin
350 { Get frame size coefficient }
351 if Frame.VersionID = MPEG_VERSION_1 then
352 if Frame.LayerID = MPEG_LAYER_I then Result := 48
353 else Result := 144
354 else
355 if Frame.LayerID = MPEG_LAYER_I then Result := 24
356 else if Frame.LayerID = MPEG_LAYER_II then Result := 144
357 else Result := 72;
358 end;
359
360 { --------------------------------------------------------------------------- }
361
362 function GetBitRate(const Frame: FrameData): Word;
363 begin
364 { Get bit rate }
365 Result := MPEG_BIT_RATE[Frame.VersionID, Frame.LayerID, Frame.BitRateID];
366 end;
367
368 { --------------------------------------------------------------------------- }
369
370 function GetSampleRate(const Frame: FrameData): Word;
371 begin
372 { Get sample rate }
373 Result := MPEG_SAMPLE_RATE[Frame.VersionID, Frame.SampleRateID];
374 end;
375
376 { --------------------------------------------------------------------------- }
377
378 function GetPadding(const Frame: FrameData): Byte;
379 begin
380 { Get frame padding }
381 if Frame.PaddingBit then
382 if Frame.LayerID = MPEG_LAYER_I then Result := 4
383 else Result := 1
384 else Result := 0;
385 end;
386
387 { --------------------------------------------------------------------------- }
388
389 function GetFrameLength(const Frame: FrameData): Word;
390 var
391 Coefficient, BitRate, SampleRate, Padding: Word;
392 begin
393 { Calculate MPEG frame length }
394 Coefficient := GetCoefficient(Frame);
395 BitRate := GetBitRate(Frame);
396 SampleRate := GetSampleRate(Frame);
397 Padding := GetPadding(Frame);
398 Result := Trunc(Coefficient * BitRate * 1000 / SampleRate) + Padding;
399 end;
400
401 { --------------------------------------------------------------------------- }
402
403 function IsXing(const Index: Word; Data: array of Byte): Boolean;
404 begin
405 { Get true if Xing encoder }
406 Result :=
407 (Data[Index] = 0) and
408 (Data[Index + 1] = 0) and
409 (Data[Index + 2] = 0) and
410 (Data[Index + 3] = 0) and
411 (Data[Index + 4] = 0) and
412 (Data[Index + 5] = 0);
413 end;
414
415 { --------------------------------------------------------------------------- }
416
417 function GetXingInfo(const Index: Word; Data: array of Byte): VBRData;
418 begin
419 { Extract Xing VBR info at given position }
420 FillChar(Result, SizeOf(Result), 0);
421 Result.Found := true;
422 Result.ID := VBR_ID_XING;
423 Result.Frames :=
424 Data[Index + 8] * $1000000 +
425 Data[Index + 9] * $10000 +
426 Data[Index + 10] * $100 +
427 Data[Index + 11];
428 Result.Bytes :=
429 Data[Index + 12] * $1000000 +
430 Data[Index + 13] * $10000 +
431 Data[Index + 14] * $100 +
432 Data[Index + 15];
433 Result.Scale := Data[Index + 119];
434 { Vendor ID can be not present }
435 Result.VendorID :=
436 Chr(Data[Index + 120]) +
437 Chr(Data[Index + 121]) +
438 Chr(Data[Index + 122]) +
439 Chr(Data[Index + 123]) +
440 Chr(Data[Index + 124]) +
441 Chr(Data[Index + 125]) +
442 Chr(Data[Index + 126]) +
443 Chr(Data[Index + 127]);
444 end;
445
446 { --------------------------------------------------------------------------- }
447
448 function GetFhGInfo(const Index: Word; Data: array of Byte): VBRData;
449 begin
450 { Extract FhG VBR info at given position }
451 FillChar(Result, SizeOf(Result), 0);
452 Result.Found := true;
453 Result.ID := VBR_ID_FHG;
454 Result.Scale := Data[Index + 9];
455 Result.Bytes :=
456 Data[Index + 10] * $1000000 +
457 Data[Index + 11] * $10000 +
458 Data[Index + 12] * $100 +
459 Data[Index + 13];
460 Result.Frames :=
461 Data[Index + 14] * $1000000 +
462 Data[Index + 15] * $10000 +
463 Data[Index + 16] * $100 +
464 Data[Index + 17];
465 end;
466
467 { --------------------------------------------------------------------------- }
468
469 function FindVBR(const Index: Word; Data: array of Byte): VBRData;
470 begin
471 { Check for VBR header at given position }
472 FillChar(Result, SizeOf(Result), 0);
473 if Chr(Data[Index]) +
474 Chr(Data[Index + 1]) +
475 Chr(Data[Index + 2]) +
476 Chr(Data[Index + 3]) = VBR_ID_XING then Result := GetXingInfo(Index, Data);
477 if Chr(Data[Index]) +
478 Chr(Data[Index + 1]) +
479 Chr(Data[Index + 2]) +
480 Chr(Data[Index + 3]) = VBR_ID_FHG then Result := GetFhGInfo(Index, Data);
481 end;
482
483 { --------------------------------------------------------------------------- }
484
485 function GetVBRDeviation(const Frame: FrameData): Byte;
486 begin
487 { Calculate VBR deviation }
488 if Frame.VersionID = MPEG_VERSION_1 then
489 if Frame.ModeID <> MPEG_CM_MONO then Result := 36
490 else Result := 21
491 else
492 if Frame.ModeID <> MPEG_CM_MONO then Result := 21
493 else Result := 13;
494 end;
495
496 { --------------------------------------------------------------------------- }
497
498 function FindFrame(const Data: array of Byte; var VBR: VBRData): FrameData;
499 var
500 HeaderData: array [1..4] of Byte;
501 Iterator, VBRIdx: Integer;
502 begin
503 { Search for valid frame }
504 FillChar(Result, SizeOf(Result), 0);
505 Move(Data, HeaderData, SizeOf(HeaderData));
506 for Iterator := 0 to SizeOf(Data) - MAX_MPEG_FRAME_LENGTH do
507 begin
508 { Decode data if frame header found }
509 if IsFrameHeader(HeaderData) then
510 begin
511 DecodeHeader(HeaderData, Result);
512 { Check for next frame and try to find VBR header }
513 VBRIdx := Iterator + GetFrameLength(Result);
514 if (VBRIdx < SizeOf(Data)) and ValidFrameAt(VBRIdx, Data) then
515 begin
516 Result.Found := true;
517 Result.Position := Iterator;
518 Result.Size := GetFrameLength(Result);
519 Result.Xing := IsXing(Iterator + SizeOf(HeaderData), Data);
520 VBR := FindVBR(Iterator + GetVBRDeviation(Result), Data);
521 break;
522 end;
523 end;
524 { Prepare next data block }
525 HeaderData[1] := HeaderData[2];
526 HeaderData[2] := HeaderData[3];
527 HeaderData[3] := HeaderData[4];
528 HeaderData[4] := Data[Iterator + SizeOf(HeaderData)];
529 end;
530 end;
531
532 { --------------------------------------------------------------------------- }
533
534 function FindVendorID(const Data: array of Byte; Size: Word): string;
535 var
536 Iterator: Integer;
537 VendorID: string;
538 begin
539 { Search for vendor ID }
540 Result := '';
541 if (SizeOf(Data) - Size - 8) < 0 then Size := SizeOf(Data) - 8;
542 for Iterator := 0 to Size do
543 begin
544 VendorID :=
545 Chr(Data[SizeOf(Data) - Iterator - 8]) +
546 Chr(Data[SizeOf(Data) - Iterator - 7]) +
547 Chr(Data[SizeOf(Data) - Iterator - 6]) +
548 Chr(Data[SizeOf(Data) - Iterator - 5]);
549 if VendorID = VENDOR_ID_LAME then
550 begin
551 Result := VendorID +
552 Chr(Data[SizeOf(Data) - Iterator - 4]) +
553 Chr(Data[SizeOf(Data) - Iterator - 3]) +
554 Chr(Data[SizeOf(Data) - Iterator - 2]) +
555 Chr(Data[SizeOf(Data) - Iterator - 1]);
556 break;
557 end;
558 if VendorID = VENDOR_ID_GOGO_NEW then
559 begin
560 Result := VendorID;
561 break;
562 end;
563 end;
564 end;
565
566 { ********************** Private functions & procedures ********************* }
567
568 procedure TMPEGaudio.FResetData;
569 begin
570 { Reset all variables }
571 FFileLength := 0;
572 FMPEGStart := 0;
573 FMPEGEnd := 0;
574 FAudioSizeTag := 0;
575 FVendorID := '';
576 FillChar(FVBR, SizeOf(FVBR), 0);
577 FillChar(FFrame, SizeOf(FFrame), 0);
578 FFrame.VersionID := MPEG_VERSION_UNKNOWN;
579 FFrame.SampleRateID := MPEG_SAMPLE_RATE_UNKNOWN;
580 FFrame.ModeID := MPEG_CM_UNKNOWN;
581 FFrame.ModeExtensionID := MPEG_CM_EXTENSION_UNKNOWN;
582 FFrame.EmphasisID := MPEG_EMPHASIS_UNKNOWN;
583 FID3v1.ResetData;
584 FID3v2.ResetData;
585 FAPEtag.ResetData;
586 end;
587
588 { --------------------------------------------------------------------------- }
589
590 function TMPEGaudio.FGetVersion: string;
591 begin
592 { Get MPEG version name }
593 Result := MPEG_VERSION[FFrame.VersionID];
594 end;
595
596 { --------------------------------------------------------------------------- }
597
598 function TMPEGaudio.FGetLayer: string;
599 begin
600 { Get MPEG layer name }
601 Result := MPEG_LAYER[FFrame.LayerID];
602 end;
603
604 { --------------------------------------------------------------------------- }
605
606 function TMPEGaudio.FGetBitRate: Word;
607 begin
608 { Get bit rate, calculate average bit rate if VBR header found }
609 if (FVBR.Found) and (FVBR.Frames > 0) then
610 Result := Round((FVBR.Bytes / FVBR.Frames - GetPadding(FFrame)) *
611 GetSampleRate(FFrame) / GetCoefficient(FFrame) / 1000)
612 else
613 Result := GetBitRate(FFrame);
614 end;
615
616 { --------------------------------------------------------------------------- }
617
618 function TMPEGaudio.FGetSampleRate: Word;
619 begin
620 { Get sample rate }
621 Result := GetSampleRate(FFrame);
622 end;
623
624 { --------------------------------------------------------------------------- }
625
626 function TMPEGaudio.FGetChannelMode: string;
627 begin
628 { Get channel mode name }
629 Result := MPEG_CM_MODE[FFrame.ModeID];
630 end;
631
632 { --------------------------------------------------------------------------- }
633
634 function TMPEGaudio.FGetEmphasis: string;
635 begin
636 { Get emphasis name }
637 Result := MPEG_EMPHASIS[FFrame.EmphasisID];
638 end;
639
640 { --------------------------------------------------------------------------- }
641
642 function TMPEGaudio.FGetFrames: Integer;
643 var
644 MPEGSize: Integer;
645 begin
646 { Get total number of frames, calculate if VBR header not found }
647 if FVBR.Found then
648 Result := FVBR.Frames
649 else
650 begin
651 MPEGSize := FMPEGEnd - FMPEGStart;
652 Result := (MPEGSize - FFrame.Position) div GetFrameLength(FFrame);
653 end;
654 end;
655
656 { --------------------------------------------------------------------------- }
657
658 function TMPEGaudio.FGetDuration: Double;
659 var
660 MPEGSize: Int64;
661 begin
662 { Calculate song duration }
663 if FFrame.Found then
664 if (FVBR.Found) and (FVBR.Frames > 0) then
665 Result := FVBR.Frames * GetCoefficient(FFrame) * 8 /
666 GetSampleRate(FFrame)
667 else
668 begin
669 MPEGSize := FMPEGEnd - FMPEGStart;
670 Result := (MPEGSize - FFrame.Position) / GetBitRate(FFrame) / 1000 * 8;
671 end
672 else
673 Result := 0;
674 end;
675
676 { --------------------------------------------------------------------------- }
677
678 function TMPEGaudio.FGetVBREncoderID: Byte;
679 begin
680 { Guess VBR encoder and get ID }
681 Result := 0;
682 if Copy(FVBR.VendorID, 1, 4) = VENDOR_ID_LAME then
683 Result := MPEG_ENCODER_LAME;
684 if Copy(FVBR.VendorID, 1, 4) = VENDOR_ID_GOGO_NEW then
685 Result := MPEG_ENCODER_GOGO;
686 if Copy(FVBR.VendorID, 1, 4) = VENDOR_ID_GOGO_OLD then
687 Result := MPEG_ENCODER_GOGO;
688 if (FVBR.ID = VBR_ID_XING) and
689 (Copy(FVBR.VendorID, 1, 4) <> VENDOR_ID_LAME) and
690 (Copy(FVBR.VendorID, 1, 4) <> VENDOR_ID_GOGO_NEW) and
691 (Copy(FVBR.VendorID, 1, 4) <> VENDOR_ID_GOGO_OLD) then
692 Result := MPEG_ENCODER_XING;
693 if FVBR.ID = VBR_ID_FHG then
694 Result := MPEG_ENCODER_FHG;
695 if (Copy(FVendorID, 1, 4) = VENDOR_ID_LAME) then
696 Result := MPEG_ENCODER_LAME;
697 end;
698
699 { --------------------------------------------------------------------------- }
700
701 function TMPEGaudio.FGetCBREncoderID: Byte;
702 begin
703 { Guess CBR encoder and get ID }
704 Result := MPEG_ENCODER_FHG;
705 if (FFrame.OriginalBit) and
706 (FFrame.ProtectionBit) then
707 Result := MPEG_ENCODER_LAME;
708 if (GetBitRate(FFrame) <= 160) and
709 (FFrame.ModeID = MPEG_CM_STEREO) then
710 Result := MPEG_ENCODER_BLADE;
711 if (FFrame.CopyrightBit) and
712 (FFrame.OriginalBit) and
713 (not FFrame.ProtectionBit) then
714 Result := MPEG_ENCODER_XING;
715 if (FFrame.Xing) and
716 (FFrame.OriginalBit) then
717 Result := MPEG_ENCODER_XING;
718 if FFrame.LayerID = MPEG_LAYER_II then
719 Result := MPEG_ENCODER_QDESIGN;
720 if (FFrame.ModeID = MPEG_CM_DUAL_CHANNEL) and
721 (FFrame.ProtectionBit) then
722 Result := MPEG_ENCODER_SHINE;
723 if Copy(FVendorID, 1, 4) = VENDOR_ID_LAME then
724 Result := MPEG_ENCODER_LAME;
725 if Copy(FVendorID, 1, 4) = VENDOR_ID_GOGO_NEW then
726 Result := MPEG_ENCODER_GOGO;
727 end;
728
729 { --------------------------------------------------------------------------- }
730
731 function TMPEGaudio.FGetEncoderID: Byte;
732 begin
733 { Get guessed encoder ID }
734 if FFrame.Found then
735 if FVBR.Found then Result := FGetVBREncoderID
736 else Result := FGetCBREncoderID
737 else
738 Result := 0;
739 end;
740
741 { --------------------------------------------------------------------------- }
742
743 function TMPEGaudio.FGetEncoder: string;
744 var
745 VendorID: string;
746 begin
747 { Get guessed encoder name and encoder version for LAME }
748 Result := MPEG_ENCODER[FGetEncoderID];
749 if FVBR.VendorID <> '' then VendorID := FVBR.VendorID;
750 if FVendorID <> '' then VendorID := FVendorID;
751 if (FGetEncoderID = MPEG_ENCODER_LAME) and
752 (Length(VendorID) >= 8) and
753 (VendorID[5] in ['0'..'9']) and
754 (VendorID[6] = '.') and
755 (VendorID[7] in ['0'..'9']) and
756 (VendorID[8] in ['0'..'9']) then
757 Result :=
758 Result + #32 +
759 VendorID[5] +
760 VendorID[6] +
761 VendorID[7] +
762 VendorID[8];
763 end;
764
765 { --------------------------------------------------------------------------- }
766
767 function TMPEGaudio.FGetValid: Boolean;
768 begin
769 { Check for right MPEG file data }
770 Result :=
771 (FFrame.Found) and
772 (FGetBitRate >= MIN_MPEG_BIT_RATE) and
773 (FGetBitRate <= MAX_MPEG_BIT_RATE) and
774 (FGetDuration >= MIN_ALLOWED_DURATION);
775 end;
776
777 { ********************** Public functions & procedures ********************** }
778
779 constructor TMPEGaudio.Create;
780 begin
781 { Object constructor }
782 inherited;
783 FID3v1 := TID3v1.Create;
784 FID3v2 := TID3v2.Create;
785 FAPEtag := TAPEtag.Create;
786 FResetData;
787 end;
788
789 { --------------------------------------------------------------------------- }
790
791 destructor TMPEGaudio.Destroy;
792 begin
793 { Object destructor }
794 FID3v1.Free;
795 FID3v2.Free;
796 FAPEtag.Free;
797 inherited;
798 end;
799
800 { --------------------------------------------------------------------------- }
801
802 function TMPEGaudio.ReadFromFile(const FileName: String): Boolean;
803 var
804 SourceFile: hFileInt;
805 Data: array [1..MAX_MPEG_FRAME_LENGTH * 2] of Byte;
806 Transferred: DWORD;
807 Position : Int64;
808 tmp : Integer;
809 str: string;
810 Value: Int64;
811 Code: Integer;
812 begin
813 FResetData;
814 SourceFile := hINVALID_HANDLE_VALUE;
815 try
816 SourceFile := mbFileOpen(FileName, fmOpenRead or fmShareDenyWrite);
817 if (SourceFile = hINVALID_HANDLE_VALUE) then
818 begin
819 Result := false;
820 Exit;
821 end;
822
823 { At first search for tags & Lyrics3 then search for a MPEG frame and VBR data }
824 if (FID3v2.ReadFromFile(FileName)) and
825 (FID3v1.ReadFromFile(FileName)) then
826 begin
827 FFileLength := mbFileSize(FileName);
828 Position := FID3v2.Size;
829 FileSeek(SourceFile, Position, soFromBeginning);
830 Transferred:= FileRead(SourceFile, Data, SizeOf(Data));
831 FFrame := FindFrame(Data, FVBR);
832 // Search for vendor ID at the beginning
833 FVendorID := FindVendorID(Data, FFrame.Size * 5);
834
835 { patched by e-w@re }
836 { Try to find the first frame if no frame at the beginning found ]}
837 if (not FFrame.Found) and (Transferred = SizeOf(Data)) then
838 repeat
839 Transferred:= FileRead(SourceFile, Data, SizeOf(Data));
840 Inc(Position, Transferred);
841 FFrame := FindFrame(Data, FVBR);
842 until (FFrame.Found) or (Transferred < SizeOf(Data));
843
844 if FFrame.Found then
845 begin
846 FFrame.Position := Position + FFrame.Position;
847 FMPEGStart := FFrame.Position;
848 tmp := FID3v1.TagSize;
849 FMPEGEnd := FFileLength - tmp;
850 end;
851
852 if FID3v2.Exists then
853 begin
854 str := FID3v2.TSIZ;
855 if Length(str) > 0 then
856 try
857 Val(str, Value, Code);
858 if (Code = 0) then FAudioSizeTag := Value;
859 except
860 // ignore
861 end;
862 end;
863
864 { Search for vendor ID at the end if CBR encoded }
865 if (FFrame.Found) and (FVendorID = '') then
866 begin
867 if not FID3v1.Exists then Position := FFileLength - SizeOf(Data)
868 else Position := FFileLength - SizeOf(Data) - 128;
869 FileSeek(SourceFile, Position, soFromBeginning);
870 Transferred:= FileRead(SourceFile, Data, SizeOf(Data));
871 FVendorID := FindVendorID(Data, FFrame.Size * 5);
872 end;
873 end;
874 FileClose(SourceFile);
875 Result := true;
876 except
877 if (SourceFile <> hINVALID_HANDLE_VALUE) then FileClose(SourceFile);
878 Result := false;
879 end;
880 if not FFrame.Found then FResetData;
881 end;
882
883 { --------------------------------------------------------------------------- }
884
885 function TMPEGaudio.FGetSamples: Cardinal;
886 begin
887 Result := 0;
888 if FFrame.Found then
889 // rework, it's the same
890 if (FVBR.Found) and (FVBR.Frames > 0) then
891 Result := FVBR.Frames * GetCoefficient(FFrame) * 8
892 else
893 Result := FGetFrames * GetCoefficient(FFrame) * 8;
894 end;
895
896 { --------------------------------------------------------------------------- }
897
898 function TMPEGaudio.FGetRatio: Double;
899 begin
900 { Get compression ratio }
901 if FGetValid then
902 begin
903 //Result := FFileSize / (FGetSamples * FChannels * FBits / 8 + 44) * 100
904 if ChannelMode = 'Mono' then
905 Result := FFileLength / (FGetSamples * (1 * 16 / 8) + 44) * 100
906 else
907 Result := FFileLength / (FGetSamples * (2 * 16 / 8) + 44) * 100;
908 end
909 else
910 Result := 0;
911 end;
912
913 { --------------------------------------------------------------------------- }
914
915 end.