| 1 |
{ *************************************************************************** } |
| 2 |
{ } |
| 3 |
{ Audio Tools Library } |
| 4 |
{ Class TID3v1 - for manipulating with ID3v1 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.2 (April 2005) by Gambit } |
| 13 |
{ - updated to unicode file access } |
| 14 |
{ } |
| 15 |
{ Version 1.1 (16 June 2004) by jtclipper } |
| 16 |
{ - added support for Lyrics3 v2.00 Tags } |
| 17 |
{ } |
| 18 |
{ Version 1.0 (25 July 2001) } |
| 19 |
{ - Reading & writing support for ID3v1.x tags } |
| 20 |
{ - Tag info: title, artist, album, track, year, genre, comment } |
| 21 |
{ } |
| 22 |
{ This library is free software; you can redistribute it and/or } |
| 23 |
{ modify it under the terms of the GNU Lesser General Public } |
| 24 |
{ License as published by the Free Software Foundation; either } |
| 25 |
{ version 2.1 of the License, or (at your option) any later version. } |
| 26 |
{ } |
| 27 |
{ This library is distributed in the hope that it will be useful, } |
| 28 |
{ but WITHOUT ANY WARRANTY; without even the implied warranty of } |
| 29 |
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU } |
| 30 |
{ Lesser General Public License for more details. } |
| 31 |
{ } |
| 32 |
{ You should have received a copy of the GNU Lesser General Public } |
| 33 |
{ License along with this library; if not, write to the Free Software } |
| 34 |
{ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA } |
| 35 |
{ } |
| 36 |
{ *************************************************************************** } |
| 37 |
|
| 38 |
unit ID3v1; |
| 39 |
|
| 40 |
interface |
| 41 |
|
| 42 |
uses |
| 43 |
Classes, SysUtils, StrUtils, DCClassesUtf8, DCOSUtils; |
| 44 |
|
| 45 |
const |
| 46 |
MAX_MUSIC_GENRES = 148; // Max. number of music genres |
| 47 |
DEFAULT_GENRE = 255; { Index for default genre } |
| 48 |
|
| 49 |
{ Used with VersionID property } |
| 50 |
TAG_VERSION_1_0 = 1; { Index for ID3v1.0 tag } |
| 51 |
TAG_VERSION_1_1 = 2; { Index for ID3v1.1 tag } |
| 52 |
|
| 53 |
var |
| 54 |
aTAG_MusicGenre: array [0..MAX_MUSIC_GENRES - 1] of string; // Genre names |
| 55 |
bTAG_PreserveDate: boolean; |
| 56 |
bTAG_ID3v2PreserveALL: boolean; |
| 57 |
bTAG_UseLYRICS3: boolean; |
| 58 |
bTAG_GenreOther: boolean; |
| 59 |
|
| 60 |
type |
| 61 |
|
| 62 |
String04 = string[4]; { String with max. 4 symbols } |
| 63 |
|
| 64 |
{ Real structure of ID3v1 tag } |
| 65 |
TagRecord = record |
| 66 |
Header: array [1..3] of Char; //Tag header - must be "TAG" |
| 67 |
Title: array [1..30] of Char; // Title data |
| 68 |
Artist: array [1..30] of Char; // Artist data |
| 69 |
Album: array [1..30] of Char; // Album data |
| 70 |
Year: array [1..4] of Char; // Year data |
| 71 |
Comment: array [1..30] of Char; // Comment data |
| 72 |
Genre: Byte; // Genre data |
| 73 |
end; |
| 74 |
|
| 75 |
Lyr2Mark = record |
| 76 |
Size: array [1..6] of Char; |
| 77 |
Mark: array [1..9] of Char; |
| 78 |
end; |
| 79 |
Lyr2Field = record |
| 80 |
ID: array [1..3] of Char; |
| 81 |
Size: array [1..5] of Char; |
| 82 |
end; |
| 83 |
|
| 84 |
{ Class TID3v1 } |
| 85 |
TID3v1 = class(TObject) |
| 86 |
private |
| 87 |
FExists: Boolean; |
| 88 |
FVersionID: Byte; |
| 89 |
FTitle: string; |
| 90 |
FArtist: string; |
| 91 |
FAlbum: string; |
| 92 |
FYear: String04; |
| 93 |
FComment: string; |
| 94 |
FTrack: Byte; |
| 95 |
//FTrackString: string; |
| 96 |
FGenreID: byte; |
| 97 |
|
| 98 |
//lyrics 2 |
| 99 |
FExists2: boolean; |
| 100 |
FLyrics2Size: integer; |
| 101 |
FArtist2: string; |
| 102 |
FAlbum2: string; |
| 103 |
FTitle2: string; |
| 104 |
FComment2: string; |
| 105 |
FIMG: string; |
| 106 |
|
| 107 |
function FGetLyrics2Size: integer; |
| 108 |
function FGetTagSize: integer; |
| 109 |
|
| 110 |
function ReadTag(const FileName: String; bSetFields: boolean=true): Boolean; |
| 111 |
function SaveTag(const FileName: String; bUseLYR2: boolean = true): Boolean; |
| 112 |
|
| 113 |
procedure FSetTitle(const NewTitle: string); |
| 114 |
procedure FSetArtist(const NewArtist: string); |
| 115 |
procedure FSetAlbum(const NewAlbum: string); |
| 116 |
procedure FSetYear(const NewYear: String04); |
| 117 |
procedure FSetComment(const NewComment: string); |
| 118 |
procedure FSetTrack(const NewTrack: Byte); |
| 119 |
procedure FSetGenreID(const NewGenreID: Byte); |
| 120 |
procedure FSetGenre(const NewGenre: string); |
| 121 |
|
| 122 |
function FGetTrackString: string; |
| 123 |
function FGetTitle: string; |
| 124 |
function FGetArtist: string; |
| 125 |
function FGetAlbum: string; |
| 126 |
function FGetComment: string; |
| 127 |
function FGetGenre: string; |
| 128 |
function FGetHasLyrics: boolean; |
| 129 |
|
| 130 |
public |
| 131 |
|
| 132 |
//lyrics 2 |
| 133 |
Writer: string; |
| 134 |
Lyrics: string; |
| 135 |
|
| 136 |
constructor Create; |
| 137 |
procedure ResetData; |
| 138 |
function ReadFromFile(const FileName: String): Boolean; |
| 139 |
function RemoveFromFile(const FileName: String; bLyr2Only: boolean = false): Boolean; |
| 140 |
function SaveToFile(const FileName: String ): Boolean; |
| 141 |
property Exists: Boolean read FExists; // True if tag found |
| 142 |
property ExistsLyrics2: Boolean read FExists2; // True if Lyrics 2 tag found |
| 143 |
property VersionID: Byte read FVersionID; // Version code |
| 144 |
|
| 145 |
property Track: Byte read FTrack write FSetTrack; // Track number |
| 146 |
property TrackString: string read FGetTrackString; |
| 147 |
property Title: string read FGetTitle write FSetTitle; |
| 148 |
property Artist: string read FGetArtist write FSetArtist; |
| 149 |
property Album: string read FGetAlbum write FSetAlbum; |
| 150 |
property Year: String04 read FYear write FSetYear; |
| 151 |
property Comment: string read FGetComment write FSetComment; |
| 152 |
property GenreID: Byte read FGenreID write FSetGenreID; // Genre code |
| 153 |
property Genre: string read FGetGenre write FSetGenre; // Genre name |
| 154 |
property HasLyrics: boolean read FGetHasLyrics; |
| 155 |
property Lyrics2Size: integer read FGetLyrics2Size; // full LYRICS2 tag size |
| 156 |
property TagSize: integer read FGetTagSize; // full tag size |
| 157 |
|
| 158 |
end; |
| 159 |
|
| 160 |
implementation |
| 161 |
|
| 162 |
uses |
| 163 |
DCConvertEncoding; |
| 164 |
|
| 165 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 166 |
// Private functions & procedures |
| 167 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 168 |
procedure TID3v1.FSetTitle(const NewTitle: String); |
| 169 |
begin |
| 170 |
FTitle := CeUtf8ToAnsi(TrimRight(NewTitle)); |
| 171 |
if Length( FTitle ) > 30 then begin |
| 172 |
FTitle2 := FTitle; |
| 173 |
end else begin |
| 174 |
FTitle2 := ''; |
| 175 |
end; |
| 176 |
end; |
| 177 |
|
| 178 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 179 |
procedure TID3v1.FSetArtist(const NewArtist: String); |
| 180 |
begin |
| 181 |
FArtist := CeUtf8ToAnsi(TrimRight(NewArtist)); |
| 182 |
if Length( FArtist ) > 30 then begin |
| 183 |
FArtist2 := FArtist; |
| 184 |
end else begin |
| 185 |
FArtist2 := ''; |
| 186 |
end; |
| 187 |
end; |
| 188 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 189 |
procedure TID3v1.FSetAlbum(const NewAlbum: string); |
| 190 |
begin |
| 191 |
FAlbum := CeUtf8ToAnsi(TrimRight(NewAlbum)); |
| 192 |
if Length( FAlbum ) > 30 then begin |
| 193 |
FAlbum2 := FAlbum; |
| 194 |
end else begin |
| 195 |
FAlbum2 := ''; |
| 196 |
end; |
| 197 |
end; |
| 198 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 199 |
procedure TID3v1.FSetYear(const NewYear: String04); |
| 200 |
begin |
| 201 |
FYear := TrimRight(NewYear); |
| 202 |
end; |
| 203 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 204 |
procedure TID3v1.FSetComment(const NewComment: string); |
| 205 |
begin |
| 206 |
FComment := CeUtf8ToAnsi(TrimRight(NewComment)); |
| 207 |
if Length( FComment ) > 30 then begin |
| 208 |
FComment2 := FComment; |
| 209 |
end else begin |
| 210 |
FComment2 := ''; |
| 211 |
end; |
| 212 |
end; |
| 213 |
//---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 214 |
procedure TID3v1.FSetTrack(const NewTrack: Byte); |
| 215 |
begin |
| 216 |
FTrack := NewTrack; |
| 217 |
end; |
| 218 |
//---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 219 |
procedure TID3v1.FSetGenreID(const NewGenreID: Byte); |
| 220 |
begin |
| 221 |
FGenreID := NewGenreID; |
| 222 |
end; |
| 223 |
//---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 224 |
procedure TID3v1.FSetGenre(const NewGenre: string); |
| 225 |
var |
| 226 |
i: integer; |
| 227 |
begin |
| 228 |
FGenreID := 255; |
| 229 |
for i := 0 to MAX_MUSIC_GENRES - 1 do begin |
| 230 |
if UpperCase( aTAG_MusicGenre[ i ] ) = UpperCase( NewGenre ) then begin |
| 231 |
FGenreID := i; |
| 232 |
break; |
| 233 |
end |
| 234 |
end; |
| 235 |
if bTAG_GenreOther and ((FGenreID = 255) and (NewGenre <> '')) then FGenreID := 12; // _OTHER_GENRE_ID = 12; |
| 236 |
end; |
| 237 |
|
| 238 |
//---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 239 |
function TID3v1.FGetTrackString: string; |
| 240 |
begin |
| 241 |
if FTrack = 0 then begin |
| 242 |
result := ''; |
| 243 |
end else begin |
| 244 |
result := IntToStr( FTrack ); |
| 245 |
end; |
| 246 |
end; |
| 247 |
//---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 248 |
function TID3v1.FGetTitle: string; |
| 249 |
begin |
| 250 |
if FTitle2 <> '' then begin |
| 251 |
result := FTitle2; |
| 252 |
end else begin |
| 253 |
result := FTitle; |
| 254 |
end; |
| 255 |
Result:= CeAnsiToUtf8(Result); |
| 256 |
end; |
| 257 |
//---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 258 |
function TID3v1.FGetArtist: string; |
| 259 |
begin |
| 260 |
if FArtist2 <> '' then begin |
| 261 |
result := FArtist2; |
| 262 |
end else begin |
| 263 |
result := FArtist; |
| 264 |
end; |
| 265 |
Result:= CeAnsiToUtf8(Result); |
| 266 |
end; |
| 267 |
//---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 268 |
function TID3v1.FGetAlbum: string; |
| 269 |
begin |
| 270 |
if FAlbum2 <> '' then begin |
| 271 |
result := FAlbum2; |
| 272 |
end else begin |
| 273 |
result := FAlbum; |
| 274 |
end; |
| 275 |
Result:= CeAnsiToUtf8(Result); |
| 276 |
end; |
| 277 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 278 |
function TID3v1.FGetComment: string; |
| 279 |
begin |
| 280 |
if FComment2 <> '' then begin |
| 281 |
result := FComment2; |
| 282 |
end else begin |
| 283 |
result := FComment; |
| 284 |
end; |
| 285 |
Result:= CeAnsiToUtf8(Result); |
| 286 |
end; |
| 287 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 288 |
function TID3v1.FGetGenre: string; |
| 289 |
begin |
| 290 |
Result := ''; |
| 291 |
// Return an empty string if the current GenreID is not valid |
| 292 |
if FGenreID in [0..MAX_MUSIC_GENRES - 1] then Result := aTAG_MusicGenre[ FGenreID ]; |
| 293 |
end; |
| 294 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 295 |
function TID3v1.FGetLyrics2Size: integer; |
| 296 |
begin |
| 297 |
if FLyrics2Size > 0 then begin |
| 298 |
result := FLyrics2Size + 15; |
| 299 |
end else begin |
| 300 |
result := 0; |
| 301 |
end; |
| 302 |
end; |
| 303 |
function TID3v1.FGetTagSize: integer; |
| 304 |
begin |
| 305 |
result := Lyrics2Size; |
| 306 |
if FExists then result := result + 128; |
| 307 |
end; |
| 308 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 309 |
function TID3v1.FGetHasLyrics: boolean; |
| 310 |
begin |
| 311 |
result := ( Trim( Lyrics ) <> '' ); |
| 312 |
end; |
| 313 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 314 |
function TID3v1.ReadTag(const FileName: String; bSetFields: boolean=true): Boolean; |
| 315 |
var |
| 316 |
TagData: TagRecord; |
| 317 |
SourceFile: TFileStreamEx; |
| 318 |
Mark: Lyr2Mark; |
| 319 |
Field: Lyr2Field; |
| 320 |
iOffSet, iFieldSize: integer; |
| 321 |
aBuff: array of char; |
| 322 |
begin |
| 323 |
|
| 324 |
try |
| 325 |
Result := true; |
| 326 |
// Set read-access and open file |
| 327 |
SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite); |
| 328 |
|
| 329 |
// Read id3v1 tag |
| 330 |
SourceFile.Seek(SourceFile.Size - 128, soFromBeginning); |
| 331 |
SourceFile.Read(TagData, 128); |
| 332 |
|
| 333 |
if TagData.Header = 'TAG' then begin |
| 334 |
FExists := true; |
| 335 |
if bSetFields then begin |
| 336 |
// set version |
| 337 |
if ((TagData.Comment[29] = #0) and (TagData.Comment[30] <> #0)) or |
| 338 |
((TagData.Comment[29] = #32) and (TagData.Comment[30] <> #32)) then begin // Terms for ID3v1.1 |
| 339 |
FVersionID := TAG_VERSION_1_1; |
| 340 |
end else begin |
| 341 |
FVersionID := TAG_VERSION_1_0; |
| 342 |
end; |
| 343 |
|
| 344 |
FTitle := TrimRight( TagData.Title ); |
| 345 |
FArtist := TrimRight( TagData.Artist ); |
| 346 |
FAlbum := TrimRight( TagData.Album ); |
| 347 |
FYear := TrimRight( TagData.Year ); |
| 348 |
|
| 349 |
if FVersionID = TAG_VERSION_1_0 then begin |
| 350 |
FComment := TrimRight( TagData.Comment ) |
| 351 |
end else begin |
| 352 |
FComment := TrimRight( Copy( TagData.Comment, 1, 28 ) ); |
| 353 |
FTrack := Ord( TagData.Comment[30] ); |
| 354 |
end; |
| 355 |
|
| 356 |
FGenreID := TagData.Genre; |
| 357 |
end; |
| 358 |
end; |
| 359 |
|
| 360 |
// try to read LYRICS2 tag |
| 361 |
iOffSet := 15; |
| 362 |
if FExists then iOffSet := iOffSet + 128; |
| 363 |
SourceFile.Seek(SourceFile.Size - iOffSet, soFromBeginning); |
| 364 |
SourceFile.Read(Mark, 15); |
| 365 |
if Mark.Mark = 'LYRICS200' then begin |
| 366 |
|
| 367 |
FLyrics2Size := StrToIntDef( Mark.Size, 0 ); |
| 368 |
if FLyrics2Size > 0 then begin |
| 369 |
SourceFile.Seek(SourceFile.Size - (FLyrics2Size + iOffSet), soFromBeginning); |
| 370 |
SetLength( aBuff, 11 ); // LYRICSBEGIN |
| 371 |
SourceFile.Read(aBuff[0], 11); |
| 372 |
|
| 373 |
if String( aBuff ) = 'LYRICSBEGIN' then begin // is it ok ? |
| 374 |
FExists2 := true; |
| 375 |
|
| 376 |
if bSetFields then begin |
| 377 |
while true do begin // read all fields |
| 378 |
|
| 379 |
SourceFile.Read(Field, SizeOf(Field)); |
| 380 |
iFieldSize := StrToIntDef( Field.Size, -1 ); |
| 381 |
if iFieldSize < 0 then break; |
| 382 |
SetLength( aBuff, iFieldSize ); |
| 383 |
SourceFile.Read(aBuff[0], iFieldSize); |
| 384 |
|
| 385 |
if Field.ID = 'IND' then begin |
| 386 |
|
| 387 |
end else if Field.ID = 'LYR' then begin |
| 388 |
Lyrics := Trim( String( aBuff ) ); |
| 389 |
Lyrics := StringReplace( Lyrics, #13, #13#10, [rfReplaceAll] ); |
| 390 |
Lyrics := StringReplace( Lyrics, #13#10#10, #13#10, [rfReplaceAll] ); |
| 391 |
end else if Field.ID = 'INF' then begin |
| 392 |
FComment2 := Trim( String( aBuff ) ); |
| 393 |
end else if Field.ID = 'AUT' then begin |
| 394 |
Writer := Trim( String( aBuff ) ); |
| 395 |
end else if Field.ID = 'EAL' then begin |
| 396 |
FAlbum2 := Trim( String( aBuff ) ); |
| 397 |
end else if Field.ID = 'EAR' then begin |
| 398 |
FArtist2 := Trim( String( aBuff ) ); |
| 399 |
end else if Field.ID = 'ETT' then begin |
| 400 |
FTitle2 := Trim( String( aBuff ) ); |
| 401 |
end else if Field.ID = 'IMG' then begin |
| 402 |
FIMG := String( aBuff ); |
| 403 |
end else begin |
| 404 |
break; |
| 405 |
end; |
| 406 |
|
| 407 |
end; //while |
| 408 |
end; |
| 409 |
end else begin |
| 410 |
FExists2 := false; |
| 411 |
FLyrics2Size := 0; |
| 412 |
end; |
| 413 |
|
| 414 |
end; |
| 415 |
|
| 416 |
end; |
| 417 |
|
| 418 |
// end |
| 419 |
SetLength( aBuff, 0 ); |
| 420 |
SourceFile.Free; |
| 421 |
except |
| 422 |
Result := false; // Error |
| 423 |
end; |
| 424 |
|
| 425 |
end; |
| 426 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 427 |
function TID3v1.SaveTag(const FileName: String; bUseLYR2: boolean = true): Boolean; |
| 428 |
var |
| 429 |
Tag: TagRecord; |
| 430 |
iFileAge: integer; |
| 431 |
SourceFile: TFileStreamEx; |
| 432 |
iFilePos: integer; |
| 433 |
sTmp: string; |
| 434 |
sTmp30: string[30]; |
| 435 |
|
| 436 |
procedure WriteField( sID, sValue: string ); |
| 437 |
var |
| 438 |
iLen: integer; |
| 439 |
begin |
| 440 |
if Trim( sValue ) <> '' then begin |
| 441 |
iLen := Length( sValue ); |
| 442 |
sTmp := sID + DupeString( '0', 5 - Length( IntToStr( iLen ) ) ) + IntToStr( iLen ) + sValue; |
| 443 |
SourceFile.Write(sTmp[1], Length(sTmp)); |
| 444 |
end; |
| 445 |
end; |
| 446 |
|
| 447 |
begin |
| 448 |
|
| 449 |
result := true; |
| 450 |
iFileAge := 0; |
| 451 |
try |
| 452 |
|
| 453 |
if bTAG_PreserveDate then iFileAge := mbFileAge(FileName); |
| 454 |
|
| 455 |
// Allow write-access and open file |
| 456 |
mbFileSetReadOnly(FileName, False); |
| 457 |
SourceFile := TFileStreamEx.Create(FileName, fmOpenReadWrite or fmShareDenyWrite); |
| 458 |
|
| 459 |
// Write lyrics2 |
| 460 |
if bUseLYR2 and ( bTAG_UseLYRICS3 or ( (Lyrics <> '') or (Writer <> '') or ( FIMG <> '' ) ) ) then begin |
| 461 |
|
| 462 |
if (Lyrics <> '') or (Writer <> '') or |
| 463 |
(FArtist2 <> '') or (FAlbum2 <> '' ) or (FComment2 <> '') or (FTitle2 <> '') then begin |
| 464 |
|
| 465 |
SourceFile.Seek(SourceFile.Size, soFromBeginning); |
| 466 |
iFilePos := SourceFile.Position; |
| 467 |
|
| 468 |
SourceFile.Write('LYRICSBEGIN', 11); |
| 469 |
if Lyrics <> '' then begin |
| 470 |
SourceFile.Write('IND0000210', 10); |
| 471 |
end else begin |
| 472 |
SourceFile.Write('IND0000200', 10); |
| 473 |
end; |
| 474 |
|
| 475 |
WriteField( 'EAL', FAlbum2 ); |
| 476 |
WriteField( 'EAR', FArtist2 ); |
| 477 |
WriteField( 'ETT', FTitle2 ); |
| 478 |
WriteField( 'INF', FComment2 ); |
| 479 |
WriteField( 'AUT', Writer ); |
| 480 |
WriteField( 'LYR', Lyrics ); |
| 481 |
WriteField( 'IMG', FIMG ); |
| 482 |
|
| 483 |
iFilepos := SourceFile.Position - iFilePos; |
| 484 |
|
| 485 |
sTmp := DupeString( '0', 6 - Length( IntToStr( iFilepos ) ) ) + IntToStr( iFilepos ) + 'LYRICS200'; |
| 486 |
SourceFile.Write(sTmp[1], Length(sTmp)); |
| 487 |
FExists2 := true; |
| 488 |
end; |
| 489 |
|
| 490 |
end; |
| 491 |
|
| 492 |
// Write id3v1 |
| 493 |
SourceFile.Seek(SourceFile.Size, soFromBeginning); |
| 494 |
|
| 495 |
FillChar( Tag, SizeOf( Tag ), 0); |
| 496 |
Tag.Header := 'TAG'; |
| 497 |
|
| 498 |
sTmp30 := TrimRight( Title ); |
| 499 |
Move( sTmp30[1], Tag.Title , Length( sTmp30 ) ); |
| 500 |
sTmp30 := TrimRight( Artist ); |
| 501 |
Move( sTmp30[1], Tag.Artist , Length( sTmp30 ) ); |
| 502 |
sTmp30 := TrimRight( Album ); |
| 503 |
Move( sTmp30[1], Tag.Album , Length( sTmp30 ) ); |
| 504 |
Move( Year[1], Tag.Year , Length( Year ) ); |
| 505 |
sTmp30 := TrimRight( Comment ); |
| 506 |
Move( sTmp30[1], Tag.Comment, Length( sTmp30 ) ); |
| 507 |
if FTrack > 0 then begin |
| 508 |
Tag.Comment[29] := #0; |
| 509 |
Tag.Comment[30] := Chr( FTrack ); |
| 510 |
end; |
| 511 |
|
| 512 |
Tag.Genre := FGenreID; |
| 513 |
|
| 514 |
SourceFile.Write(Tag, SizeOf(Tag)); |
| 515 |
|
| 516 |
SourceFile.Free; |
| 517 |
|
| 518 |
if bTAG_PreserveDate then mbFileSetTime(FileName, iFileAge); |
| 519 |
|
| 520 |
except |
| 521 |
result := false; // Error |
| 522 |
end; |
| 523 |
|
| 524 |
end; |
| 525 |
|
| 526 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 527 |
procedure TID3v1.ResetData; |
| 528 |
begin |
| 529 |
FExists := false; |
| 530 |
FVersionID := TAG_VERSION_1_0; |
| 531 |
FTitle := ''; |
| 532 |
FArtist := ''; |
| 533 |
FAlbum := ''; |
| 534 |
FYear := ''; |
| 535 |
FComment := ''; |
| 536 |
FTrack := 0; |
| 537 |
FGenreID := DEFAULT_GENRE; |
| 538 |
|
| 539 |
// lyrics 2 |
| 540 |
FExists2 := false; |
| 541 |
FLyrics2Size := 0; |
| 542 |
Lyrics := ''; |
| 543 |
Writer := ''; |
| 544 |
FArtist2 := ''; |
| 545 |
FAlbum2 := ''; |
| 546 |
FTitle2 := ''; |
| 547 |
FComment2 := ''; |
| 548 |
FIMG := ''; |
| 549 |
end; |
| 550 |
|
| 551 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 552 |
// Public functions & procedures |
| 553 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 554 |
constructor TID3v1.Create; |
| 555 |
begin |
| 556 |
inherited; |
| 557 |
ResetData; |
| 558 |
end; |
| 559 |
|
| 560 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 561 |
function TID3v1.ReadFromFile(const FileName: String): Boolean; |
| 562 |
begin |
| 563 |
// Reset and load tag data from file to variable |
| 564 |
ResetData; |
| 565 |
Result := ReadTag(FileName); |
| 566 |
end; |
| 567 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 568 |
function TID3v1.SaveToFile(const FileName: String): Boolean; |
| 569 |
begin |
| 570 |
// Delete old tag and write new tag |
| 571 |
result := (RemoveFromFile(FileName)) and (SaveTag(FileName)); |
| 572 |
if (result) and ( not FExists ) then FExists := true; |
| 573 |
// NOTE |
| 574 |
end; |
| 575 |
|
| 576 |
//----------------------------------------------------------------------------------------------------------------------------------- |
| 577 |
function TID3v1.RemoveFromFile(const FileName: String; bLyr2Only: boolean = false): Boolean; |
| 578 |
var |
| 579 |
iFileAge: integer; |
| 580 |
SourceFile: TFileStreamEx; |
| 581 |
begin |
| 582 |
|
| 583 |
result := true; |
| 584 |
try |
| 585 |
|
| 586 |
ReadTag(FileName, false); |
| 587 |
if FExists or FExists2 then begin |
| 588 |
|
| 589 |
iFileAge := 0; |
| 590 |
if bTAG_PreserveDate then iFileAge := mbFileAge(FileName); |
| 591 |
|
| 592 |
Result := true; |
| 593 |
// Allow write-access and open file |
| 594 |
mbFileSetReadOnly(FileName, False); |
| 595 |
SourceFile := TFileStreamEx.Create(FileName, fmOpenReadWrite or fmShareDenyWrite); |
| 596 |
|
| 597 |
// Delete id3v1 |
| 598 |
if FExists then begin |
| 599 |
SourceFile.Seek(SourceFile.Size - 128, soFromBeginning); |
| 600 |
//truncate |
| 601 |
SourceFile.Size := SourceFile.Position; |
| 602 |
FExists := false; |
| 603 |
end; |
| 604 |
// Delete lyrics2 |
| 605 |
if FExists2 then begin |
| 606 |
SourceFile.Seek(SourceFile.Size - Lyrics2Size, soFromBeginning); |
| 607 |
//truncate |
| 608 |
SourceFile.Size := SourceFile.Position; |
| 609 |
FExists2 := false; |
| 610 |
end; |
| 611 |
if bLyr2Only then begin |
| 612 |
if SaveTag(FileName, false) then begin |
| 613 |
FExists := true; |
| 614 |
end; |
| 615 |
end; |
| 616 |
|
| 617 |
SourceFile.Free; |
| 618 |
|
| 619 |
if bTAG_PreserveDate then mbFileSetTime(FileName, iFileAge); |
| 620 |
end; |
| 621 |
|
| 622 |
except |
| 623 |
result := false; // Error |
| 624 |
end; |
| 625 |
|
| 626 |
end; |
| 627 |
|
| 628 |
{ ************************** Initialize music genres ************************ } |
| 629 |
|
| 630 |
initialization |
| 631 |
begin |
| 632 |
//-- Initialize music genres |
| 633 |
{ Standard genres } |
| 634 |
aTAG_MusicGenre[0] := 'Blues'; |
| 635 |
aTAG_MusicGenre[1] := 'Classic Rock'; |
| 636 |
aTAG_MusicGenre[2] := 'Country'; |
| 637 |
aTAG_MusicGenre[3] := 'Dance'; |
| 638 |
aTAG_MusicGenre[4] := 'Disco'; |
| 639 |
aTAG_MusicGenre[5] := 'Funk'; |
| 640 |
aTAG_MusicGenre[6] := 'Grunge'; |
| 641 |
aTAG_MusicGenre[7] := 'Hip-Hop'; |
| 642 |
aTAG_MusicGenre[8] := 'Jazz'; |
| 643 |
aTAG_MusicGenre[9] := 'Metal'; |
| 644 |
aTAG_MusicGenre[10] := 'New Age'; |
| 645 |
aTAG_MusicGenre[11] := 'Oldies'; |
| 646 |
aTAG_MusicGenre[12] := 'Other'; |
| 647 |
aTAG_MusicGenre[13] := 'Pop'; |
| 648 |
aTAG_MusicGenre[14] := 'R&B'; |
| 649 |
aTAG_MusicGenre[15] := 'Rap'; |
| 650 |
aTAG_MusicGenre[16] := 'Reggae'; |
| 651 |
aTAG_MusicGenre[17] := 'Rock'; |
| 652 |
aTAG_MusicGenre[18] := 'Techno'; |
| 653 |
aTAG_MusicGenre[19] := 'Industrial'; |
| 654 |
aTAG_MusicGenre[20] := 'Alternative'; |
| 655 |
aTAG_MusicGenre[21] := 'Ska'; |
| 656 |
aTAG_MusicGenre[22] := 'Death Metal'; |
| 657 |
aTAG_MusicGenre[23] := 'Pranks'; |
| 658 |
aTAG_MusicGenre[24] := 'Soundtrack'; |
| 659 |
aTAG_MusicGenre[25] := 'Euro-Techno'; |
| 660 |
aTAG_MusicGenre[26] := 'Ambient'; |
| 661 |
aTAG_MusicGenre[27] := 'Trip-Hop'; |
| 662 |
aTAG_MusicGenre[28] := 'Vocal'; |
| 663 |
aTAG_MusicGenre[29] := 'Jazz+Funk'; |
| 664 |
aTAG_MusicGenre[30] := 'Fusion'; |
| 665 |
aTAG_MusicGenre[31] := 'Trance'; |
| 666 |
aTAG_MusicGenre[32] := 'Classical'; |
| 667 |
aTAG_MusicGenre[33] := 'Instrumental'; |
| 668 |
aTAG_MusicGenre[34] := 'Acid'; |
| 669 |
aTAG_MusicGenre[35] := 'House'; |
| 670 |
aTAG_MusicGenre[36] := 'Game'; |
| 671 |
aTAG_MusicGenre[37] := 'Sound Clip'; |
| 672 |
aTAG_MusicGenre[38] := 'Gospel'; |
| 673 |
aTAG_MusicGenre[39] := 'Noise'; |
| 674 |
aTAG_MusicGenre[40] := 'AlternRock'; |
| 675 |
aTAG_MusicGenre[41] := 'Bass'; |
| 676 |
aTAG_MusicGenre[42] := 'Soul'; |
| 677 |
aTAG_MusicGenre[43] := 'Punk'; |
| 678 |
aTAG_MusicGenre[44] := 'Space'; |
| 679 |
aTAG_MusicGenre[45] := 'Meditative'; |
| 680 |
aTAG_MusicGenre[46] := 'Instrumental Pop'; |
| 681 |
aTAG_MusicGenre[47] := 'Instrumental Rock'; |
| 682 |
aTAG_MusicGenre[48] := 'Ethnic'; |
| 683 |
aTAG_MusicGenre[49] := 'Gothic'; |
| 684 |
aTAG_MusicGenre[50] := 'Darkwave'; |
| 685 |
aTAG_MusicGenre[51] := 'Techno-Industrial'; |
| 686 |
aTAG_MusicGenre[52] := 'Electronic'; |
| 687 |
aTAG_MusicGenre[53] := 'Pop-Folk'; |
| 688 |
aTAG_MusicGenre[54] := 'Eurodance'; |
| 689 |
aTAG_MusicGenre[55] := 'Dream'; |
| 690 |
aTAG_MusicGenre[56] := 'Southern Rock'; |
| 691 |
aTAG_MusicGenre[57] := 'Comedy'; |
| 692 |
aTAG_MusicGenre[58] := 'Cult'; |
| 693 |
aTAG_MusicGenre[59] := 'Gangsta'; |
| 694 |
aTAG_MusicGenre[60] := 'Top 40'; |
| 695 |
aTAG_MusicGenre[61] := 'Christian Rap'; |
| 696 |
aTAG_MusicGenre[62] := 'Pop/Funk'; |
| 697 |
aTAG_MusicGenre[63] := 'Jungle'; |
| 698 |
aTAG_MusicGenre[64] := 'Native American'; |
| 699 |
aTAG_MusicGenre[65] := 'Cabaret'; |
| 700 |
aTAG_MusicGenre[66] := 'New Wave'; |
| 701 |
aTAG_MusicGenre[67] := 'Psychadelic'; |
| 702 |
aTAG_MusicGenre[68] := 'Rave'; |
| 703 |
aTAG_MusicGenre[69] := 'Showtunes'; |
| 704 |
aTAG_MusicGenre[70] := 'Trailer'; |
| 705 |
aTAG_MusicGenre[71] := 'Lo-Fi'; |
| 706 |
aTAG_MusicGenre[72] := 'Tribal'; |
| 707 |
aTAG_MusicGenre[73] := 'Acid Punk'; |
| 708 |
aTAG_MusicGenre[74] := 'Acid Jazz'; |
| 709 |
aTAG_MusicGenre[75] := 'Polka'; |
| 710 |
aTAG_MusicGenre[76] := 'Retro'; |
| 711 |
aTAG_MusicGenre[77] := 'Musical'; |
| 712 |
aTAG_MusicGenre[78] := 'Rock & Roll'; |
| 713 |
aTAG_MusicGenre[79] := 'Hard Rock'; |
| 714 |
{ Extended genres } |
| 715 |
aTAG_MusicGenre[80] := 'Folk'; |
| 716 |
aTAG_MusicGenre[81] := 'Folk-Rock'; |
| 717 |
aTAG_MusicGenre[82] := 'National Folk'; |
| 718 |
aTAG_MusicGenre[83] := 'Swing'; |
| 719 |
aTAG_MusicGenre[84] := 'Fast Fusion'; |
| 720 |
aTAG_MusicGenre[85] := 'Bebob'; |
| 721 |
aTAG_MusicGenre[86] := 'Latin'; |
| 722 |
aTAG_MusicGenre[87] := 'Revival'; |
| 723 |
aTAG_MusicGenre[88] := 'Celtic'; |
| 724 |
aTAG_MusicGenre[89] := 'Bluegrass'; |
| 725 |
aTAG_MusicGenre[90] := 'Avantgarde'; |
| 726 |
aTAG_MusicGenre[91] := 'Gothic Rock'; |
| 727 |
aTAG_MusicGenre[92] := 'Progressive Rock'; |
| 728 |
aTAG_MusicGenre[93] := 'Psychedelic Rock'; |
| 729 |
aTAG_MusicGenre[94] := 'Symphonic Rock'; |
| 730 |
aTAG_MusicGenre[95] := 'Slow Rock'; |
| 731 |
aTAG_MusicGenre[96] := 'Big Band'; |
| 732 |
aTAG_MusicGenre[97] := 'Chorus'; |
| 733 |
aTAG_MusicGenre[98] := 'Easy Listening'; |
| 734 |
aTAG_MusicGenre[99] := 'Acoustic'; |
| 735 |
aTAG_MusicGenre[100]:= 'Humour'; |
| 736 |
aTAG_MusicGenre[101]:= 'Speech'; |
| 737 |
aTAG_MusicGenre[102]:= 'Chanson'; |
| 738 |
aTAG_MusicGenre[103]:= 'Opera'; |
| 739 |
aTAG_MusicGenre[104]:= 'Chamber Music'; |
| 740 |
aTAG_MusicGenre[105]:= 'Sonata'; |
| 741 |
aTAG_MusicGenre[106]:= 'Symphony'; |
| 742 |
aTAG_MusicGenre[107]:= 'Booty Bass'; |
| 743 |
aTAG_MusicGenre[108]:= 'Primus'; |
| 744 |
aTAG_MusicGenre[109]:= 'Porn Groove'; |
| 745 |
aTAG_MusicGenre[110]:= 'Satire'; |
| 746 |
aTAG_MusicGenre[111]:= 'Slow Jam'; |
| 747 |
aTAG_MusicGenre[112]:= 'Club'; |
| 748 |
aTAG_MusicGenre[113]:= 'Tango'; |
| 749 |
aTAG_MusicGenre[114]:= 'Samba'; |
| 750 |
aTAG_MusicGenre[115]:= 'Folklore'; |
| 751 |
aTAG_MusicGenre[116]:= 'Ballad'; |
| 752 |
aTAG_MusicGenre[117]:= 'Power Ballad'; |
| 753 |
aTAG_MusicGenre[118]:= 'Rhythmic Soul'; |
| 754 |
aTAG_MusicGenre[119]:= 'Freestyle'; |
| 755 |
aTAG_MusicGenre[120]:= 'Duet'; |
| 756 |
aTAG_MusicGenre[121]:= 'Punk Rock'; |
| 757 |
aTAG_MusicGenre[122]:= 'Drum Solo'; |
| 758 |
aTAG_MusicGenre[123]:= 'A capella'; |
| 759 |
aTAG_MusicGenre[124]:= 'Euro-House'; |
| 760 |
aTAG_MusicGenre[125]:= 'Dance Hall'; |
| 761 |
aTAG_MusicGenre[126]:= 'Goa'; |
| 762 |
aTAG_MusicGenre[127]:= 'Drum & Bass'; |
| 763 |
aTAG_MusicGenre[128]:= 'Club-House'; |
| 764 |
aTAG_MusicGenre[129]:= 'Hardcore'; |
| 765 |
aTAG_MusicGenre[130]:= 'Terror'; |
| 766 |
aTAG_MusicGenre[131]:= 'Indie'; |
| 767 |
aTAG_MusicGenre[132]:= 'BritPop'; |
| 768 |
aTAG_MusicGenre[133]:= 'Negerpunk'; |
| 769 |
aTAG_MusicGenre[134]:= 'Polsk Punk'; |
| 770 |
aTAG_MusicGenre[135]:= 'Beat'; |
| 771 |
aTAG_MusicGenre[136]:= 'Christian Gangsta Rap'; |
| 772 |
aTAG_MusicGenre[137]:= 'Heavy Metal'; |
| 773 |
aTAG_MusicGenre[138]:= 'Black Metal'; |
| 774 |
aTAG_MusicGenre[139]:= 'Crossover'; |
| 775 |
aTAG_MusicGenre[140]:= 'Contemporary Christian'; |
| 776 |
aTAG_MusicGenre[141]:= 'Christian Rock'; |
| 777 |
aTAG_MusicGenre[142]:= 'Merengue'; |
| 778 |
aTAG_MusicGenre[143]:= 'Salsa'; |
| 779 |
aTAG_MusicGenre[144]:= 'Thrash Metal'; |
| 780 |
aTAG_MusicGenre[145]:= 'Anime'; |
| 781 |
aTAG_MusicGenre[146]:= 'JPop'; |
| 782 |
aTAG_MusicGenre[147]:= 'Synthpop'; |
| 783 |
|
| 784 |
//--- |
| 785 |
bTAG_PreserveDate := false; |
| 786 |
bTAG_ID3v2PreserveALL := false; |
| 787 |
bTAG_UseLYRICS3 := false; |
| 788 |
bTAG_GenreOther := false; |
| 789 |
end; |
| 790 |
|
| 791 |
|
| 792 |
end. |