Debian Codegraph @

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

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TAPEtag - for manipulating with APE 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 2.1 (April 2005) by Gambit }
13 { - updated to unicode file access }
14 { }
15 { Version 2.0 (30 May 2003) by Jean-Marie Prat }
16 { - Writing support for APE 2.0 tags }
17 { - Removed UTF8 decoding since calling application is supposed to provide }
18 { or handle UTF8 strings. }
19 { - Removed direct tag infos. All fields are now stored into an array. A }
20 { specific field can be requested using SeekField function. }
21 { - Introduced procedures to add/remove/order fields. }
22 { }
23 { Version 1.0 (21 April 2002) }
24 { - Reading & writing support for APE 1.0 tags }
25 { - Reading support for APE 2.0 tags (UTF-8 decoding) }
26 { - Tag info: title, artist, album, track, year, genre, comment, copyright }
27 { }
28 { This library is free software; you can redistribute it and/or }
29 { modify it under the terms of the GNU Lesser General Public }
30 { License as published by the Free Software Foundation; either }
31 { version 2.1 of the License, or (at your option) any later version. }
32 { }
33 { This library is distributed in the hope that it will be useful, }
34 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
35 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
36 { Lesser General Public License for more details. }
37 { }
38 { You should have received a copy of the GNU Lesser General Public }
39 { License along with this library; if not, write to the Free Software }
40 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
41 { }
42 { *************************************************************************** }
43
44 unit APEtag;
45
46 interface
47
48 uses
49 Classes, SysUtils, DCClassesUtf8;
50
51 const
52 { Tag ID }
53 ID3V1_ID = 'TAG'; { ID3v1 }
54 APE_ID = 'APETAGEX'; { APE }
55
56 { Size constants }
57 ID3V1_TAG_SIZE = 128; { ID3v1 tag }
58 APE_TAG_FOOTER_SIZE = 32; { APE tag footer }
59 APE_TAG_HEADER_SIZE = 32; { APE tag header }
60
61 { Version of APE tag }
62 APE_VERSION_1_0 = 1000;
63 APE_VERSION_2_0 = 1000;
64
65 type
66 { APE tag header/footer - for internal use }
67 RTagHeader = record
68 { Real structure of APE footer }
69 ID: array [0..7] of Char; { Always "APETAGEX" }
70 Version: Integer; { Tag version }
71 Size: Integer; { Tag size including footer }
72 Fields: Integer; { Number of fields }
73 Flags: Integer; { Tag flags }
74 Reserved: array [0..7] of Char; { Reserved for later use }
75 { Extended data }
76 DataShift: Byte; { Used if ID3v1 tag found }
77 FileSize: Integer; { File size (bytes) }
78 end;
79
80 RField = record
81 Name: string;
82 Value: String;
83 end;
84 AField = array of RField;
85
86 { TAPETag }
87
88 TAPETag = class
89 private
90 pField: Afield;
91 pExists: Boolean;
92 pVersion: Integer;
93 pSize: Integer;
94 function ReadFooter(sFile: String; var footer: RTagHeader): boolean;
95 procedure ReadFields(sFile: String; footer: RTagHeader);
96 private
97 function GetTrack: Word;
98 function GetYear: String;
99 function GetGenre: String;
100 function GetTitle: String;
101 function GetAlbum: String;
102 function GetArtist: String;
103 function GetComment: String;
104 function GetComposer: String;
105 function GetCopyright: String;
106 public
107 property Exists: Boolean read pExists; { True if tag found }
108 property Version: Integer read pVersion; { Tag version }
109 property Fields: AField read pField;
110 property Size: Integer read pSize;
111 constructor Create();
112 function ReadFromFile(sFile: String): Boolean;
113 function RemoveTagFromFile(sFile: String): Boolean;
114 function WriteTagInFile(sFile: String): Boolean;
115 procedure InsertField(pos: integer ; name: string ; value: String);
116 { Insert field so that it has position pos}
117 procedure RemoveField(pos: integer);
118 procedure AppendField(name: string ; value: String);
119 procedure SwapFields(pos1, pos2: integer);
120 function SeekField(Field: string): String;
121 procedure ResetData;
122
123 property Title: String read GetTitle; { Song title }
124 property Artist: String read GetArtist; { Artist name }
125 property Album: String read GetAlbum; { Album title }
126 property Track: Word read GetTrack; { Track number }
127 property Year: String read GetYear; { Release year }
128 property Genre: String read GetGenre; { Genre name }
129 property Comment: String read GetComment; { Comment }
130 property Composer: String read GetComposer; { Composer }
131 property Copyright: String read GetCopyright; { Copyright }
132 end;
133
134 implementation
135
136 //----------------------------------------------------------------------------//
137 // Private stuff //
138 //----------------------------------------------------------------------------//
139
140 procedure TAPETag.ResetData();
141 begin
142 SetLength(pField,0);
143 pExists := False;
144 pVersion := 0;
145 pSize := 0;
146 end;
147
148 // ----------------------------------------------------------------------------
149
150 function TAPETag.ReadFooter(sFile: String; var footer: RTagHeader): boolean;
151 var
152 SourceFile: TFileStreamEx;
153 TagID: array [1..3] of Char;
154 Transferred: Integer;
155 begin
156 FillChar(Footer, SizeOf(Footer), 0);
157 try
158 Result := true;
159 { Set read-access and open file }
160 SourceFile := TFileStreamEx.Create(sFile, fmOpenRead or fmShareDenyWrite);
161 Footer.FileSize := SourceFile.Size;
162 if (IOResult <> 0) then
163 begin
164 SourceFile.Free;
165 Result := False;
166 Exit;
167 end;
168 { Check for existing ID3v1 tag }
169 if (Footer.FileSize - ID3V1_TAG_SIZE > 0) then
170 begin
171 SourceFile.Seek(Footer.FileSize - ID3V1_TAG_SIZE, soFromBeginning);
172 SourceFile.Read(TagID, SizeOf(TagID));
173
174 if TagID = ID3V1_ID then
175 Footer.DataShift := ID3V1_TAG_SIZE
176 else
177 Footer.DataShift := 0;
178 end;
179 { Read footer data }
180 Transferred := 0;
181 if (Footer.FileSize - Footer.DataShift - APE_TAG_FOOTER_SIZE) > 0 then
182 begin
183 SourceFile.Seek(Footer.FileSize - Footer.DataShift - APE_TAG_FOOTER_SIZE, soFromBeginning);
184 //BlockRead(SourceFile, Footer, APE_TAG_FOOTER_SIZE, Transferred);
185 Transferred := SourceFile.Read(Footer, APE_TAG_FOOTER_SIZE);
186 end;
187 SourceFile.Free;
188 { if transfer is not complete }
189 if Transferred < APE_TAG_FOOTER_SIZE then
190 Result := false;
191 except
192 { Error }
193 Result := false;
194 end;
195 end;
196
197 function TAPETag.GetAlbum: String;
198 begin
199 Result := SeekField('Album');
200 end;
201
202 function TAPETag.GetArtist: String;
203 begin
204 Result := SeekField('Artist');
205 end;
206
207 function TAPETag.GetComment: String;
208 begin
209 Result := SeekField('Comment');
210 end;
211
212 function TAPETag.GetComposer: String;
213 begin
214 Result := SeekField('Composer');
215 end;
216
217 function TAPETag.GetCopyright: String;
218 begin
219 Result := SeekField('Copyright');
220 end;
221
222 function TAPETag.GetYear: String;
223 begin
224 Result := SeekField('Year');
225 end;
226
227 function TAPETag.GetGenre: String;
228 begin
229 Result := SeekField('Genre');
230 end;
231
232 function TAPETag.GetTitle: String;
233 begin
234 Result := SeekField('Title');
235 end;
236
237 function TAPETag.GetTrack: Word;
238 var
239 TrackString: String;
240 Index, Value, Code: Integer;
241 begin
242 { Extract track from string }
243 TrackString := SeekField('Track');
244 Index := Pos('/', TrackString);
245 if Index = 0 then Val(TrackString, Value, Code)
246 else Val(Copy(TrackString, 1, Index - 1), Value, Code);
247 if Code = 0 then Result := Value
248 else Result := 0;
249 end;
250
251 // ----------------------------------------------------------------------------
252
253 procedure TAPETag.ReadFields(sFile: String; footer: RTagHeader);
254 var
255 SourceFile: TFileStreamEx;
256 FieldName: String;
257 FieldValue: array [1..250] of Char;
258 NextChar: Char;
259 Iterator, ValueSize, ValuePosition, FieldFlags: Integer;
260 begin
261 try
262 { Set read-access, open file }
263 SourceFile := TFileStreamEx.Create(sFile, fmOpenRead or fmShareDenyWrite);
264 SourceFile.Seek(footer.FileSize - footer.DataShift - footer.Size, soFromBeginning);
265 { Read all stored fields }
266 SetLength(pField,footer.Fields);
267 for Iterator := 0 to footer.Fields-1 do
268 begin
269 FillChar(FieldValue, SizeOf(FieldValue), 0);
270 SourceFile.Read(ValueSize, SizeOf(ValueSize));
271 SourceFile.Read(FieldFlags, SizeOf(FieldFlags));
272 FieldName := '';
273 repeat
274 SourceFile.Read(NextChar, SizeOf(NextChar));
275 FieldName := FieldName + NextChar;
276 until Ord(NextChar) = 0;
277 ValuePosition := SourceFile.Position;
278 SourceFile.Read(FieldValue, ValueSize mod SizeOf(FieldValue));
279 pField[Iterator].Name := Trim(FieldName);
280 pField[Iterator].Value := Trim(FieldValue);
281 SourceFile.Seek(ValuePosition + ValueSize, soFromBeginning);
282 end;
283 SourceFile.Free;
284 except
285 end;
286 end;
287
288 //----------------------------------------------------------------------------//
289 // Public stuff //
290 //----------------------------------------------------------------------------//
291
292 constructor TAPETag.Create();
293 begin
294 inherited;
295 ResetData;
296 end;
297
298 // ----------------------------------------------------------------------------
299
300 function TAPETag.ReadFromFile(sFile: String): Boolean;
301 var
302 Footer: RTagHeader;
303 begin
304 ResetData;
305 Result := ReadFooter(sFile, Footer);
306 { Process data if loaded and footer valid }
307 if (Result) and (Footer.ID = APE_ID) then
308 begin
309 pExists := True;
310 pVersion := Footer.Version;
311 pSize := Footer.Size;
312 ReadFields(sFile, Footer);
313 end;
314 end;
315
316 // ----------------------------------------------------------------------------
317
318 function TAPETag.RemoveTagFromFile(sFile: String): Boolean;
319 var
320 SourceFile: TFileStreamEx;
321 Footer: RTagHeader;
322 ID3: pointer;
323 begin
324 Result := ReadFooter(sFile, Footer);
325 { Process data if loaded and footer valid }
326 if (Result) and (Footer.ID = APE_ID) then
327 begin
328 SourceFile := TFileStreamEx.Create(sFile, fmOpenReadWrite or fmShareDenyWrite);
329 { If there is an ID3v1 tag roaming around behind the APE tag, we have to buffer it }
330 if Footer.DataShift = ID3V1_TAG_SIZE then
331 begin
332 GetMem(ID3,ID3V1_TAG_SIZE);
333 SourceFile.Seek(footer.FileSize - footer.DataShift, soFromBeginning);
334 SourceFile.Read(ID3^, ID3V1_TAG_SIZE);
335 end;
336 { If this is an APEv2, header size must be added }
337 if (Footer.Flags shr 31) > 0 then
338 Inc(Footer.Size, APE_TAG_HEADER_SIZE);
339 SourceFile.Seek(Footer.FileSize - footer.Size-Footer.DataShift, soFromBeginning);
340 { If there is an ID3v1 tag roaming around, we copy it }
341 if Footer.DataShift = ID3V1_TAG_SIZE then
342 begin
343 SourceFile.Write(ID3^, ID3V1_TAG_SIZE);
344 FreeMem(ID3,128);
345 end;
346 SourceFile.Seek(Footer.FileSize-Footer.Size, soFromBeginning);
347 //truncate
348 SourceFile.Size := SourceFile.Position;
349 SourceFile.Free;
350 end;
351 end;
352
353 // ----------------------------------------------------------------------------
354
355 function TAPETag.WriteTagInFile(sFile: String): Boolean;
356 const
357 APEPreample: array [0..7] of char = ('A','P','E','T','A','G','E','X');
358 var
359 SourceFile: TFileStreamEx;
360 Header, Footer, RefFooter: RTagHeader;
361 ID3: PChar;
362 i, len, TagSize, Flags: integer;
363 TagData: TStringStream;
364 begin
365 ID3 := nil;
366 // method : first, save any eventual ID3v1 tag lying around
367 // then we truncate the file after the audio data
368 // then write the APE tag (and possibly the ID3)
369 Result := ReadFooter(sFile, RefFooter);
370 { Process data if loaded and footer valid }
371 if (Result) and (RefFooter.ID = APE_ID) then
372 begin
373 SourceFile := TFileStreamEx.Create(sFile, fmOpenReadWrite or fmShareDenyWrite);
374 { If there is an ID3v1 tag roaming around behind the APE tag, we have to buffer it }
375 if RefFooter.DataShift = ID3V1_TAG_SIZE then
376 begin
377 GetMem(ID3,ID3V1_TAG_SIZE);
378 SourceFile.Seek(Reffooter.FileSize - Reffooter.DataShift, soFromBeginning);
379 SourceFile.Read(ID3^, ID3V1_TAG_SIZE);
380 end;
381 { If this is an APEv2, header size must be added }
382 //if (RefFooter.Flags shr 31) > 0 then
383 Inc(RefFooter.Size, APE_TAG_HEADER_SIZE);
384 SourceFile.Seek(RefFooter.FileSize - RefFooter.Size-RefFooter.DataShift, soFromBeginning);
385 //truncate
386 SourceFile.Size := SourceFile.Position;
387 SourceFile.Free;
388 end;
389 TagData := TStringStream.Create('');
390 TagSize := APE_TAG_FOOTER_SIZE;
391 for i:=0 to high(pField) do
392 begin
393 TagSize := TagSize + 9 + Length(pField[i].Name) + Length(pField[i].Value);
394 end;
395 Header.ID[0] := 'A';
396 Header.ID[1] := 'P';
397 Header.ID[2] := 'E';
398 Header.ID[3] := 'T';
399 Header.ID[4] := 'A';
400 Header.ID[5] := 'G';
401 Header.ID[6] := 'E';
402 Header.ID[7] := 'X';
403 Header.Version := 2000;
404 Header.Size := TagSize;
405 Header.Fields := Length(pField);
406 Header.Flags := Integer(0 or (1 shl 29) or (1 shl 31)); // tag contains a header and this is the header
407 //ShowMessage(IntToSTr(Header.Flags));
408 TagData.Write(Header,APE_TAG_HEADER_SIZE);
409 for i:=0 to high(pField) do
410 begin
411 len := Length(pField[i].Value);
412 Flags := 0;
413 TagData.Write(len, SizeOf(len));
414 TagData.Write(Flags, SizeOf(Flags));
415 TagData.WriteString(pField[i].Name + #0);
416 TagData.WriteString(pField[i].Value);
417 end;
418 Footer.ID[0] := 'A';
419 Footer.ID[1] := 'P';
420 Footer.ID[2] := 'E';
421 Footer.ID[3] := 'T';
422 Footer.ID[4] := 'A';
423 Footer.ID[5] := 'G';
424 Footer.ID[6] := 'E';
425 Footer.ID[7] := 'X';
426 Footer.Version := 2000;
427 Footer.Size := TagSize;
428 Footer.Fields := Length(pField);
429 Footer.Flags := Integer(0 or (1 shl 31)); // tag contains a header and this is the footer
430 TagData.Write(Footer,APE_TAG_FOOTER_SIZE);
431 if (RefFooter.DataShift = ID3V1_TAG_SIZE) and Assigned(ID3)then
432 begin
433 TagData.Write(ID3^,ID3V1_TAG_SIZE);
434 FreeMem(ID3);
435 end;
436 SourceFile := TFileStreamEx.Create(sFile, fmOpenReadWrite or fmShareDenyWrite);
437 SourceFile.Seek(0, soFromEnd);
438 TagData.Seek(0, soFromBeginning);
439 SourceFile.CopyFrom(TagData, TagData.Size);
440 SourceFile.Free;
441 TagData.Free;
442 end;
443
444 // ----------------------------------------------------------------------------
445
446 procedure TAPETag.InsertField (pos: integer ; name: string ; value: String);
447 var
448 dummy: AField;
449 i: integer;
450 begin
451 if pos>=Length(pField) then exit;
452 SetLength(dummy,Length(pField)-pos);
453 dummy := copy(pField,pos,Length(dummy));
454 pField[pos].Name := name;
455 pField[pos].Value := value;
456 SetLength(pField,Length(pField)+1);
457 for i:= pos+1 to high(pField) do
458 pField[i] := dummy[i-pos-1];
459 end;
460
461 // ----------------------------------------------------------------------------
462
463 procedure TAPETag.RemoveField (pos: integer);
464 var
465 i: integer;
466 begin
467 if pos>Length(pField) then exit;
468 for i:=pos+1 to high(pField) do
469 pField[i-1]:=pField[i];
470 SetLength(pField,Length(pField)-1);
471 end;
472
473 // ----------------------------------------------------------------------------
474
475 procedure TAPETag.AppendField(name: string ; value: String);
476 begin
477 SetLength(pField,Length(pField)+1);
478 pField[high(pField)].Name := name;
479 pField[high(pField)].Value := value;
480 end;
481
482 // ----------------------------------------------------------------------------
483
484 procedure TAPETag.SwapFields (pos1, pos2: integer);
485 var
486 dummy: RField;
487 begin
488 dummy.Name := pField[pos1].Name;
489 dummy.Value := pField[pos1].Value;
490 pField[pos1].Name := pField[pos2].Name;
491 pField[pos1].Value := pField[pos2].Value;
492 pField[pos2].Name := dummy.Name;
493 pField[pos2].Value := dummy.Value;
494 end;
495
496 // ----------------------------------------------------------------------------
497
498 function TAPETag.SeekField(Field: string): String;
499 var
500 i: integer;
501 begin
502 Result := '';
503 for i:=0 to high(pField) do
504 begin
505 if UpperCase(Field)=UpperCase(pField[i].Name) then
506 begin
507 Result := pField[i].Value;
508 Break;
509 end;
510 end;
511 end;
512
513 // ----------------------------------------------------------------------------
514
515 end.