Debian Codegraph @

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

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TID3v2 - for manipulating with ID3v2 tags }
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 1.8 (April 2005) by Gambit }
13 { - updated to unicode file access }
14 { }
15 { Version 1.7 (2 October 2002) }
16 { - Added property TrackString }
17 { }
18 { Version 1.6 (29 July 2002) }
19 { - Reading support for Unicode }
20 { - Removed limitation for the track number }
21 { }
22 { Version 1.5 (23 May 2002) }
23 { - Support for padding }
24 { }
25 { Version 1.4 (24 March 2002) }
26 { - Reading support for ID3v2.2.x & ID3v2.4.x tags }
27 { }
28 { Version 1.3 (16 February 2002) }
29 { - Fixed bug with property Comment }
30 { - Added info: composer, encoder, copyright, language, link }
31 { }
32 { Version 1.2 (17 October 2001) }
33 { - Writing support for ID3v2.3.x tags }
34 { - Fixed bug with track number detection }
35 { - Fixed bug with tag reading }
36 { }
37 { Version 1.1 (31 August 2001) }
38 { - Added public procedure ResetData }
39 { }
40 { Version 1.0 (14 August 2001) }
41 { - Reading support for ID3v2.3.x tags }
42 { - Tag info: title, artist, album, track, year, genre, comment }
43 { }
44 { This library is free software; you can redistribute it and/or }
45 { modify it under the terms of the GNU Lesser General Public }
46 { License as published by the Free Software Foundation; either }
47 { version 2.1 of the License, or (at your option) any later version. }
48 { }
49 { This library is distributed in the hope that it will be useful, }
50 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
51 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
52 { Lesser General Public License for more details. }
53 { }
54 { You should have received a copy of the GNU Lesser General Public }
55 { License along with this library; if not, write to the Free Software }
56 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
57 { }
58 { *************************************************************************** }
59
60 unit ID3v2;
61
62 interface
63
64 uses
65 Classes, SysUtils, DCClassesUtf8, DCOSUtils;
66
67 const
68 TAG_VERSION_2_2 = 2; { Code for ID3v2.2.x tag }
69 TAG_VERSION_2_3 = 3; { Code for ID3v2.3.x tag }
70 TAG_VERSION_2_4 = 4; { Code for ID3v2.4.x tag }
71
72 type
73 { Class TID3v2 }
74 TID3v2 = class(TObject)
75 private
76 { Private declarations }
77 FExists: Boolean;
78 FVersionID: Byte;
79 FSize: Integer;
80 FTitle: string;
81 FArtist: string;
82 FAlbum: string;
83 FTrack: Word;
84 FTrackString: string;
85 FYear: string;
86 FGenre: string;
87 FComment: string;
88 FComposer: string;
89 FEncoder: string;
90 FCopyright: string;
91 FLanguage: string;
92 FLink: string;
93 FTSIZ: string;
94 procedure FSetTitle(const NewTitle: string);
95 procedure FSetArtist(const NewArtist: string);
96 procedure FSetAlbum(const NewAlbum: string);
97 procedure FSetTrack(const NewTrack: Word);
98 procedure FSetYear(const NewYear: string);
99 procedure FSetGenre(const NewGenre: string);
100 procedure FSetComment(const NewComment: string);
101 procedure FSetComposer(const NewComposer: string);
102 procedure FSetEncoder(const NewEncoder: string);
103 procedure FSetCopyright(const NewCopyright: string);
104 procedure FSetLanguage(const NewLanguage: string);
105 procedure FSetLink(const NewLink: string);
106 public
107 { Public declarations }
108 constructor Create; { Create object }
109 procedure ResetData; { Reset all data }
110 function ReadFromFile(const FileName: String): Boolean; { Load tag }
111 function SaveToFile(const FileName: String): Boolean; { Save tag }
112 function RemoveFromFile(const FileName: String): Boolean;{ Delete tag }
113 property Exists: Boolean read FExists; { True if tag found }
114 property VersionID: Byte read FVersionID; { Version code }
115 property Size: Integer read FSize; { Total tag size }
116 property Title: string read FTitle write FSetTitle; { Song title }
117 property Artist: string read FArtist write FSetArtist; { Artist name }
118 property Album: string read FAlbum write FSetAlbum; { Album title }
119 property Track: Word read FTrack write FSetTrack; { Track number }
120 property TrackString: string read FTrackString; { Track number (string) }
121 property Year: string read FYear write FSetYear; { Release year }
122 property Genre: string read FGenre write FSetGenre; { Genre name }
123 property Comment: string read FComment write FSetComment; { Comment }
124 property Composer: string read FComposer write FSetComposer; { Composer }
125 property Encoder: string read FEncoder write FSetEncoder; { Encoder }
126 property Copyright: string read FCopyright write FSetCopyright; { (c) }
127 property Language: string read FLanguage write FSetLanguage; { Language }
128 property Link: string read FLink write FSetLink; { URL link }
129 property TSIZ: string read FTSIZ;
130 end;
131
132 implementation
133
134 uses
135 LazUTF8, DCConvertEncoding, DCUnicodeUtils;
136
137 const
138 { ID3v2 tag ID }
139 ID3V2_ID = 'ID3';
140
141 { Max. number of supported tag frames }
142 ID3V2_FRAME_COUNT = 17;
143
144 { Names of supported tag frames (ID3v2.3.x & ID3v2.4.x) }
145 ID3V2_FRAME_NEW: array [1..ID3V2_FRAME_COUNT] of string =
146 ('TIT2', 'TPE1', 'TALB', 'TRCK', 'TYER', 'TCON', 'COMM', 'TCOM', 'TENC',
147 'TCOP', 'TLAN', 'WXXX', 'TDRC', 'TOPE', 'TIT1', 'TOAL', 'TSIZ');
148
149 { Names of supported tag frames (ID3v2.2.x) }
150 ID3V2_FRAME_OLD: array [1..ID3V2_FRAME_COUNT] of string =
151 ('TT2', 'TP1', 'TAL', 'TRK', 'TYE', 'TCO', 'COM', 'TCM', 'TEN',
152 'TCR', 'TLA', 'WXX', 'TOR', 'TOA', 'TT1', 'TOT', 'TSI');
153
154 { Max. tag size for saving }
155 ID3V2_MAX_SIZE = 4096;
156
157 { Unicode ID }
158 UTF16_ID = #01;
159 UTF16BE_ID = #02;
160 UTF8_ID = #03;
161
162 type
163 { Frame header (ID3v2.3.x & ID3v2.4.x) }
164 FrameHeaderNew = record
165 ID: array [1..4] of Char; { Frame ID }
166 Size: Integer; { Size excluding header }
167 Flags: Word; { Flags }
168 end;
169
170 { Frame header (ID3v2.2.x) }
171 FrameHeaderOld = record
172 ID: array [1..3] of Char; { Frame ID }
173 Size: array [1..3] of Byte; { Size excluding header }
174 end;
175
176 { ID3v2 header data - for internal use }
177 TagInfo = record
178 { Real structure of ID3v2 header }
179 ID: array [1..3] of Char; { Always "ID3" }
180 Version: Byte; { Version number }
181 Revision: Byte; { Revision number }
182 Flags: Byte; { Flags of tag }
183 Size: array [1..4] of Byte; { Tag size excluding header }
184 { Extended data }
185 FileSize: Integer; { File size (bytes) }
186 Frame: array [1..ID3V2_FRAME_COUNT] of string; { Information from frames }
187 NeedRewrite: Boolean; { Tag should be rewritten }
188 PaddingSize: Integer; { Padding size (bytes) }
189 end;
190
191 { ********************* Auxiliary functions & procedures ******************** }
192
193 function ReadHeader(const FileName: String; var Tag: TagInfo): Boolean;
194 var
195 SourceFile: TFileStreamEx;
196 Transferred: Integer;
197 begin
198 try
199 Result := true;
200 { Set read-access and open file }
201 SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
202
203 { Read header and get file size }
204 Transferred := SourceFile.Read(Tag, 10);
205 Tag.FileSize := SourceFile.Size;
206 SourceFile.Free;
207 { if transfer is not complete }
208 if Transferred < 10 then Result := false;
209 except
210 { Error }
211 Result := false;
212 end;
213 end;
214
215 { --------------------------------------------------------------------------- }
216
217 function GetTagSize(const Tag: TagInfo): Integer;
218 begin
219 { Get total tag size }
220 Result :=
221 Tag.Size[1] * $200000 +
222 Tag.Size[2] * $4000 +
223 Tag.Size[3] * $80 +
224 Tag.Size[4] + 10;
225 if Tag.Flags and $10 = $10 then Inc(Result, 10);
226 if Result > Tag.FileSize then Result := 0;
227 end;
228
229 { --------------------------------------------------------------------------- }
230
231 procedure SetTagItem(const ID, Data: string; var Tag: TagInfo);
232 var
233 Iterator: Byte;
234 FrameID: string;
235 begin
236 { Set tag item if supported frame found }
237 for Iterator := 1 to ID3V2_FRAME_COUNT do
238 begin
239 if Tag.Version > TAG_VERSION_2_2 then
240 FrameID := ID3V2_FRAME_NEW[Iterator]
241 else
242 FrameID := ID3V2_FRAME_OLD[Iterator];
243 if (FrameID = ID) and (Data[1] <= UTF8_ID) then
244 Tag.Frame[Iterator] := Data;
245 end;
246 end;
247
248 { --------------------------------------------------------------------------- }
249
250 function Swap32(const Figure: Integer): Integer;
251 var
252 ByteArray: array [1..4] of Byte absolute Figure;
253 begin
254 { Swap 4 bytes }
255 Result :=
256 ByteArray[1] * $1000000 +
257 ByteArray[2] * $10000 +
258 ByteArray[3] * $100 +
259 ByteArray[4];
260 end;
261
262 { --------------------------------------------------------------------------- }
263
264 procedure ReadFramesNew(const FileName: String; var Tag: TagInfo);
265 var
266 ASize: Cardinal;
267 SourceFile: TFileStreamEx;
268 Frame: FrameHeaderNew;
269 Data: array [1..500] of Char;
270 DataPosition, DataSize: Integer;
271 begin
272 { Get information from frames (ID3v2.3.x & ID3v2.4.x) }
273 try
274 { Set read-access, open file }
275 SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
276 SourceFile.Seek(10, soFromBeginning);
277 // ID3v2 extended header
278 if Tag.Flags and $40 = $40 then
279 begin
280 ASize:= BEToN(SourceFile.ReadDWord);
281 SourceFile.Seek(ASize - 4, soFromCurrent);
282 end;
283 while (SourceFile.Position < GetTagSize(Tag)) and (SourceFile.Position < SourceFile.Size) do
284 begin
285 FillChar(Data, SizeOf(Data), 0);
286 { Read frame header and check frame ID }
287 SourceFile.Read(Frame, 10);
288 if not (Frame.ID[1] in ['A'..'Z']) then break;
289 { Note data position and determine significant data size }
290 DataPosition := SourceFile.Position;
291 if Swap32(Frame.Size) > SizeOf(Data) then DataSize := SizeOf(Data)
292 else DataSize := Swap32(Frame.Size);
293 { Read frame data and set tag item if frame supported }
294 SourceFile.Read(Data, DataSize);
295 if Frame.Flags and $8000 <> $8000 then SetTagItem(Frame.ID, Data, Tag);
296 SourceFile.Seek(DataPosition + Swap32(Frame.Size), soFromBeginning);
297 end;
298 SourceFile.Free;
299 except
300 end;
301 end;
302
303 { --------------------------------------------------------------------------- }
304
305 procedure ReadFramesOld(const FileName: String; var Tag: TagInfo);
306 var
307 SourceFile: TFileStreamEx;
308 Frame: FrameHeaderOld;
309 Data: array [1..500] of Char;
310 DataPosition, FrameSize, DataSize: Integer;
311 begin
312 { Get information from frames (ID3v2.2.x) }
313 try
314 { Set read-access, open file }
315 SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
316 SourceFile.Seek(10, soFromBeginning);
317 while (SourceFile.Position < GetTagSize(Tag)) and (SourceFile.Position < SourceFile.Size) do
318 begin
319 FillChar(Data, SizeOf(Data), 0);
320 { Read frame header and check frame ID }
321 SourceFile.Read(Frame, 6);
322 if not (Frame.ID[1] in ['A'..'Z']) then break;
323 { Note data position and determine significant data size }
324 DataPosition := SourceFile.Position;
325 FrameSize := Frame.Size[1] shl 16 + Frame.Size[2] shl 8 + Frame.Size[3];
326 if FrameSize > SizeOf(Data) then DataSize := SizeOf(Data)
327 else DataSize := FrameSize;
328 { Read frame data and set tag item if frame supported }
329 SourceFile.Read(Data, DataSize);
330 SetTagItem(Frame.ID, Data, Tag);
331 SourceFile.Seek(DataPosition + FrameSize, soFromBeginning);
332 end;
333 SourceFile.Free;
334 except
335 end;
336 end;
337
338 { --------------------------------------------------------------------------- }
339
340 function GetStringUtf8(const Source: string): string;
341 const
342 UTF16BEBOM = #$FE#$FF;
343 UTF16LEBOM = #$FF#$FE;
344 begin
345 { Convert string from unicode if needed and trim spaces }
346 if (Length(Source) > 0) and (Source[1] = UTF16_ID) then
347 begin
348 if (Length(Source) < 3) then
349 Result := EmptyStr
350 else if (CompareWord(Source[2], UTF16BEBOM, 1) = 0) then
351 Result := Trim(Utf16BEToUtf8(Copy(Source, 4, MaxInt)))
352 else if (CompareWord(Source[2], UTF16LEBOM, 1) = 0) then
353 Result := Trim(Utf16LEToUtf8(Copy(Source, 4, MaxInt)))
354 else
355 Result := EmptyStr
356 end
357 else if (Length(Source) > 0) and (Source[1] = UTF16BE_ID) then
358 Result := Trim(Utf16BEToUtf8(Copy(Source, 2, MaxInt)))
359 else if (Length(Source) > 0) and (Source[1] = UTF8_ID) then
360 Result := Trim(Copy(Source, 2, MaxInt))
361 else
362 Result := CeAnsiToUtf8(Trim(Source));
363 end;
364
365 { --------------------------------------------------------------------------- }
366
367 function GetContent(const Content1, Content2: string): string;
368 begin
369 { Get content preferring the first content }
370 Result := GetStringUtf8(Content1);
371 if Result = '' then Result := GetStringUtf8(Content2);
372 end;
373
374 { --------------------------------------------------------------------------- }
375
376 function ExtractTrack(const TrackString: string): Word;
377 var
378 Track: string;
379 Index, Value, Code: Integer;
380 begin
381 { Extract track from string }
382 Track := GetStringUtf8(TrackString);
383 Index := Pos('/', Track);
384 if Index = 0 then Val(Track, Value, Code)
385 else Val(Copy(Track, 1, Index - 1), Value, Code);
386 if Code = 0 then Result := Value
387 else Result := 0;
388 end;
389
390 { --------------------------------------------------------------------------- }
391
392 function ExtractYear(const YearString, DateString: string): string;
393 begin
394 { Extract year from strings }
395 Result := GetStringUtf8(YearString);
396 if Result = '' then Result := Copy(GetStringUtf8(DateString), 1, 4);
397 end;
398
399 { --------------------------------------------------------------------------- }
400
401 function ExtractGenre(const GenreString: string): string;
402 begin
403 { Extract genre from string }
404 Result := GetStringUtf8(GenreString);
405 if Pos(')', Result) > 0 then Delete(Result, 1, LastDelimiter(')', Result));
406 end;
407
408 { --------------------------------------------------------------------------- }
409
410 function ExtractText(const SourceString: string; LanguageID: Boolean): string;
411 var
412 Source, Separator: string;
413 EncodingID: Char;
414 begin
415 { Extract significant text data from a complex field }
416 Source := SourceString;
417 Result := '';
418 if Length(Source) > 0 then
419 begin
420 EncodingID := Source[1];
421 if EncodingID in [UTF16_ID, UTF16BE_ID] then
422 Separator := #0#0
423 else begin
424 Separator := #0;
425 end;
426 if LanguageID then
427 Delete(Source, 1, 4)
428 else begin
429 Delete(Source, 1, 1);
430 end;
431 Delete(Source, 1, Pos(Separator, Source) + Length(Separator) - 1);
432 Result := GetStringUtf8(EncodingID + Source);
433 end;
434 end;
435
436 { --------------------------------------------------------------------------- }
437
438 procedure BuildHeader(var Tag: TagInfo);
439 var
440 Iterator, TagSize: Integer;
441 begin
442 { Calculate new tag size (without padding) }
443 TagSize := 10;
444 for Iterator := 1 to ID3V2_FRAME_COUNT do
445 if Tag.Frame[Iterator] <> '' then
446 Inc(TagSize, Length(Tag.Frame[Iterator]) + 11);
447 { Check for ability to change existing tag }
448 Tag.NeedRewrite :=
449 (Tag.ID <> ID3V2_ID) or
450 (GetTagSize(Tag) < TagSize) or
451 (GetTagSize(Tag) > ID3V2_MAX_SIZE);
452 { Calculate padding size and set padded tag size }
453 if Tag.NeedRewrite then Tag.PaddingSize := ID3V2_MAX_SIZE - TagSize
454 else Tag.PaddingSize := GetTagSize(Tag) - TagSize;
455 if Tag.PaddingSize > 0 then Inc(TagSize, Tag.PaddingSize);
456 { Build tag header }
457 Tag.ID := ID3V2_ID;
458 Tag.Version := TAG_VERSION_2_3;
459 Tag.Revision := 0;
460 Tag.Flags := 0;
461 { Convert tag size }
462 for Iterator := 1 to 4 do
463 Tag.Size[Iterator] := ((TagSize - 10) shr ((4 - Iterator) * 7)) and $7F;
464 end;
465
466 { --------------------------------------------------------------------------- }
467
468 function ReplaceTag(const FileName: String; TagData: TStream): Boolean;
469 var
470 Destination: TFileStreamEx;
471 begin
472 { Replace old tag with new tag data }
473 Result := false;
474 if (not mbFileExists(FileName)) or (not mbFileSetReadOnly(FileName, False)) then exit;
475 try
476 TagData.Position := 0;
477 Destination := TFileStreamEx.Create(FileName, fmOpenReadWrite);
478 Destination.CopyFrom(TagData, TagData.Size);
479 Destination.Free;
480 Result := true;
481 except
482 { Access error }
483 end;
484 end;
485
486 { --------------------------------------------------------------------------- }
487
488 function RebuildFile(const FileName: String; TagData: TStream): Boolean;
489 var
490 Tag: TagInfo;
491 Source, Destination: TFileStreamEx;
492 BufferName: string;
493 begin
494 { Rebuild file with old file data and new tag data (optional) }
495 Result := false;
496 if (not mbFileExists(FileName)) or (not mbFileSetReadOnly(FileName, False)) then exit;
497 if not ReadHeader(FileName, Tag) then exit;
498 if (TagData = nil) and (Tag.ID <> ID3V2_ID) then exit;
499 try
500 { Create file streams }
501 BufferName := FileName + '~';
502 Source := TFileStreamEx.Create(FileName, fmOpenRead);
503 Destination := TFileStreamEx.Create(BufferName, fmCreate);
504 { Copy data blocks }
505 if Tag.ID = ID3V2_ID then Source.Seek(GetTagSize(Tag), soFromBeginning);
506 if TagData <> nil then Destination.CopyFrom(TagData, 0);
507 Destination.CopyFrom(Source, Source.Size - Source.Position);
508 { Free resources }
509 Source.Free;
510 Destination.Free;
511 { Replace old file and delete temporary file }
512 if (mbDeleteFile(FileName)) and (mbRenameFile(BufferName, FileName)) then
513 Result := true
514 else
515 raise Exception.Create('');
516 except
517 { Access error }
518 if mbFileExists(BufferName) then mbDeleteFile(BufferName);
519 end;
520 end;
521
522 { --------------------------------------------------------------------------- }
523
524 function SaveTag(const FileName: String; Tag: TagInfo): Boolean;
525 var
526 TagData: TStringStream;
527 Iterator, FrameSize: Integer;
528 Padding: array [1..ID3V2_MAX_SIZE] of Byte;
529 begin
530 { Build and write tag header and frames to stream }
531 TagData := TStringStream.Create('');
532 BuildHeader(Tag);
533 TagData.Write(Tag, 10);
534 for Iterator := 1 to ID3V2_FRAME_COUNT do
535 if Tag.Frame[Iterator] <> '' then
536 begin
537 TagData.WriteString(ID3V2_FRAME_NEW[Iterator]);
538 FrameSize := Swap32(Length(Tag.Frame[Iterator]) + 1);
539 TagData.Write(FrameSize, SizeOf(FrameSize));
540 TagData.WriteString(#0#0#0 + Tag.Frame[Iterator]);
541 end;
542 { Add padding }
543 FillChar(Padding, SizeOf(Padding), 0);
544 if Tag.PaddingSize > 0 then TagData.Write(Padding, Tag.PaddingSize);
545 { Rebuild file or replace tag with new tag data }
546 if Tag.NeedRewrite then Result := RebuildFile(FileName, TagData)
547 else Result := ReplaceTag(FileName, TagData);
548 TagData.Free;
549 end;
550
551 { ********************** Private functions & procedures ********************* }
552
553 procedure TID3v2.FSetTitle(const NewTitle: string);
554 begin
555 { Set song title }
556 FTitle := Trim(NewTitle);
557 end;
558
559 { --------------------------------------------------------------------------- }
560
561 procedure TID3v2.FSetArtist(const NewArtist: string);
562 begin
563 { Set artist name }
564 FArtist := Trim(NewArtist);
565 end;
566
567 { --------------------------------------------------------------------------- }
568
569 procedure TID3v2.FSetAlbum(const NewAlbum: string);
570 begin
571 { Set album title }
572 FAlbum := Trim(NewAlbum);
573 end;
574
575 { --------------------------------------------------------------------------- }
576
577 procedure TID3v2.FSetTrack(const NewTrack: Word);
578 begin
579 { Set track number }
580 FTrack := NewTrack;
581 end;
582
583 { --------------------------------------------------------------------------- }
584
585 procedure TID3v2.FSetYear(const NewYear: string);
586 begin
587 { Set release year }
588 FYear := Trim(NewYear);
589 end;
590
591 { --------------------------------------------------------------------------- }
592
593 procedure TID3v2.FSetGenre(const NewGenre: string);
594 begin
595 { Set genre name }
596 FGenre := Trim(NewGenre);
597 end;
598
599 { --------------------------------------------------------------------------- }
600
601 procedure TID3v2.FSetComment(const NewComment: string);
602 begin
603 { Set comment }
604 FComment := Trim(NewComment);
605 end;
606
607 { --------------------------------------------------------------------------- }
608
609 procedure TID3v2.FSetComposer(const NewComposer: string);
610 begin
611 { Set composer name }
612 FComposer := Trim(NewComposer);
613 end;
614
615 { --------------------------------------------------------------------------- }
616
617 procedure TID3v2.FSetEncoder(const NewEncoder: string);
618 begin
619 { Set encoder name }
620 FEncoder := Trim(NewEncoder);
621 end;
622
623 { --------------------------------------------------------------------------- }
624
625 procedure TID3v2.FSetCopyright(const NewCopyright: string);
626 begin
627 { Set copyright information }
628 FCopyright := Trim(NewCopyright);
629 end;
630
631 { --------------------------------------------------------------------------- }
632
633 procedure TID3v2.FSetLanguage(const NewLanguage: string);
634 begin
635 { Set language }
636 FLanguage := Trim(NewLanguage);
637 end;
638
639 { --------------------------------------------------------------------------- }
640
641 procedure TID3v2.FSetLink(const NewLink: string);
642 begin
643 { Set URL link }
644 FLink := Trim(NewLink);
645 end;
646
647 { ********************** Public functions & procedures ********************** }
648
649 constructor TID3v2.Create;
650 begin
651 { Create object }
652 inherited;
653 ResetData;
654 end;
655
656 { --------------------------------------------------------------------------- }
657
658 procedure TID3v2.ResetData;
659 begin
660 { Reset all variables }
661 FExists := false;
662 FVersionID := 0;
663 FSize := 0;
664 FTitle := '';
665 FArtist := '';
666 FAlbum := '';
667 FTrack := 0;
668 FTrackString := '';
669 FYear := '';
670 FGenre := '';
671 FComment := '';
672 FComposer := '';
673 FEncoder := '';
674 FCopyright := '';
675 FLanguage := '';
676 FLink := '';
677 FTSIZ := '';
678 end;
679
680 { --------------------------------------------------------------------------- }
681
682 function TID3v2.ReadFromFile(const FileName: String): Boolean;
683 var
684 Tag: TagInfo;
685 begin
686 { Reset data and load header from file to variable }
687 ResetData;
688 Result := ReadHeader(FileName, Tag);
689 { Process data if loaded and header valid }
690 if (Result) and (Tag.ID = ID3V2_ID) then
691 begin
692 FExists := true;
693 { Fill properties with header data }
694 FVersionID := Tag.Version;
695 FSize := GetTagSize(Tag);
696 { Get information from frames if version supported }
697 if (FVersionID in [TAG_VERSION_2_2..TAG_VERSION_2_4]) and (FSize > 0) then
698 begin
699 if FVersionID > TAG_VERSION_2_2 then ReadFramesNew(FileName, Tag)
700 else ReadFramesOld(FileName, Tag);
701 FTitle := GetContent(Tag.Frame[1], Tag.Frame[15]);
702 FArtist := GetContent(Tag.Frame[2], Tag.Frame[14]);
703 FAlbum := GetContent(Tag.Frame[3], Tag.Frame[16]);
704 FTrack := ExtractTrack(Tag.Frame[4]);
705 FTrackString := GetStringUtf8(Tag.Frame[4]);
706 FYear := ExtractYear(Tag.Frame[5], Tag.Frame[13]);
707 FGenre := ExtractGenre(Tag.Frame[6]);
708 FComment := ExtractText(Tag.Frame[7], true);
709 FComposer := GetStringUtf8(Tag.Frame[8]);
710 FEncoder := GetStringUtf8(Tag.Frame[9]);
711 FCopyright := GetStringUtf8(Tag.Frame[10]);
712 FLanguage := GetStringUtf8(Tag.Frame[11]);
713 FLink := ExtractText(Tag.Frame[12], false);
714 FTSIZ := GetStringUtf8(Tag.Frame[17]);
715 end;
716 end;
717 end;
718
719 { --------------------------------------------------------------------------- }
720
721 function TID3v2.SaveToFile(const FileName: String): Boolean;
722 var
723 Tag: TagInfo;
724 begin
725 { Check for existing tag }
726 FillChar(Tag, SizeOf(Tag), 0);
727 ReadHeader(FileName, Tag);
728 { Prepare tag data and save to file }
729 Tag.Frame[1] := FTitle;
730 Tag.Frame[2] := FArtist;
731 Tag.Frame[3] := FAlbum;
732 if FTrack > 0 then Tag.Frame[4] := IntToStr(FTrack);
733 Tag.Frame[5] := FYear;
734 Tag.Frame[6] := FGenre;
735 if FComment <> '' then Tag.Frame[7] := 'eng' + #0 + FComment;
736 Tag.Frame[8] := FComposer;
737 Tag.Frame[9] := FEncoder;
738 Tag.Frame[10] := FCopyright;
739 Tag.Frame[11] := FLanguage;
740 if FLink <> '' then Tag.Frame[12] := #0 + FLink;
741 Result := SaveTag(FileName, Tag);
742 end;
743
744 { --------------------------------------------------------------------------- }
745
746 function TID3v2.RemoveFromFile(const FileName: String): Boolean;
747 begin
748 { Remove tag from file }
749 Result := RebuildFile(FileName, nil);
750 end;
751
752 { --------------------------------------------------------------------------- }
753
754 end.