| 1 |
{ *************************************************************************** } |
| 2 |
{ } |
| 3 |
{ Audio Tools Library } |
| 4 |
{ Class TFLACfile - for manipulating with FLAC 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 1.4 (April 2005) by Gambit } |
| 13 |
{ - updated to unicode file access } |
| 14 |
{ } |
| 15 |
{ Version 1.3 (13 August 2004) by jtclipper } |
| 16 |
{ - unit rewritten, VorbisComment is obsolete now } |
| 17 |
{ } |
| 18 |
{ Version 1.2 (23 June 2004) by sundance } |
| 19 |
{ - Check for ID3 tags (although not supported) } |
| 20 |
{ - Don't parse for other FLAC metablocks if FLAC header is missing } |
| 21 |
{ } |
| 22 |
{ Version 1.1 (6 July 2003) by Erik } |
| 23 |
{ - Class: Vorbis comments (native comment to FLAC files) added } |
| 24 |
{ } |
| 25 |
{ Version 1.0 (13 August 2002) } |
| 26 |
{ - Info: channels, sample rate, bits/sample, file size, duration, ratio } |
| 27 |
{ - Class TID3v1: reading & writing support for ID3v1 tags } |
| 28 |
{ - Class TID3v2: reading & writing support for ID3v2 tags } |
| 29 |
{ } |
| 30 |
{ This library is free software; you can redistribute it and/or } |
| 31 |
{ modify it under the terms of the GNU Lesser General Public } |
| 32 |
{ License as published by the Free Software Foundation; either } |
| 33 |
{ version 2.1 of the License, or (at your option) any later version. } |
| 34 |
{ } |
| 35 |
{ This library is distributed in the hope that it will be useful, } |
| 36 |
{ but WITHOUT ANY WARRANTY; without even the implied warranty of } |
| 37 |
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU } |
| 38 |
{ Lesser General Public License for more details. } |
| 39 |
{ } |
| 40 |
{ You should have received a copy of the GNU Lesser General Public } |
| 41 |
{ License along with this library; if not, write to the Free Software } |
| 42 |
{ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA } |
| 43 |
{ } |
| 44 |
{ *************************************************************************** } |
| 45 |
|
| 46 |
unit FLACfile; |
| 47 |
|
| 48 |
interface |
| 49 |
|
| 50 |
uses |
| 51 |
Classes, SysUtils, StrUtils, ID3v2, |
| 52 |
DCClassesUtf8, DCBasicTypes, DCOSUtils; |
| 53 |
|
| 54 |
const |
| 55 |
META_STREAMINFO = 0; |
| 56 |
META_PADDING = 1; |
| 57 |
META_APPLICATION = 2; |
| 58 |
META_SEEKTABLE = 3; |
| 59 |
META_VORBIS_COMMENT = 4; |
| 60 |
META_CUESHEET = 5; |
| 61 |
META_PICTURE = 6; |
| 62 |
|
| 63 |
type |
| 64 |
TFlacHeader = record |
| 65 |
StreamMarker: array[1..4] of Char; //should always be 'fLaC' |
| 66 |
MetaDataBlockHeader: array[1..4] of Byte; |
| 67 |
Info: array[1..18] of Byte; |
| 68 |
MD5Sum: array[1..16] of Byte; |
| 69 |
end; |
| 70 |
|
| 71 |
TMetaData = record |
| 72 |
MetaDataBlockHeader: array[1..4] of Byte; |
| 73 |
Data: TMemoryStream; |
| 74 |
end; |
| 75 |
|
| 76 |
|
| 77 |
{ TFLACfile } |
| 78 |
|
| 79 |
TFLACfile = class(TObject) |
| 80 |
private |
| 81 |
|
| 82 |
FHeader: TFlacHeader; |
| 83 |
FFileName: String; |
| 84 |
FPaddingIndex: integer; |
| 85 |
FPaddingLast: boolean; |
| 86 |
FPaddingFragments: boolean; |
| 87 |
FVorbisIndex: integer; |
| 88 |
FPadding: integer; |
| 89 |
FVCOffset: integer; |
| 90 |
FAudioOffset: integer; |
| 91 |
FChannels: byte; |
| 92 |
FSampleRate: integer; |
| 93 |
FBitsPerSample: byte; |
| 94 |
FBitrate: integer; |
| 95 |
FFileLength: integer; |
| 96 |
FSamples: Int64; |
| 97 |
|
| 98 |
aMetaBlockOther: array of TMetaData; |
| 99 |
|
| 100 |
// tag data |
| 101 |
FVendor: string; |
| 102 |
FTagSize: integer; |
| 103 |
FExists: boolean; |
| 104 |
|
| 105 |
FID3v2: TID3v2; |
| 106 |
|
| 107 |
function FGetHasLyrics: boolean; |
| 108 |
|
| 109 |
procedure FResetData( const bHeaderInfo, bTagFields :boolean ); |
| 110 |
function FIsValid: Boolean; |
| 111 |
function FGetDuration: Double; |
| 112 |
function FGetTrack: Word; |
| 113 |
function FGetRatio: Double; |
| 114 |
function FGetChannelMode: string; |
| 115 |
|
| 116 |
function GetInfo( sFile: String; bSetTags: boolean ): boolean; |
| 117 |
procedure AddMetaDataOther( aMetaHeader: array of Byte; stream: TFileStreamEx; const iBlocklength,iIndex: integer ); |
| 118 |
procedure ReadTag( Source: TFileStreamEx; bSetTagFields: boolean ); |
| 119 |
function RebuildFile( const sFile: String; VorbisBlock: TStringStream ): Boolean; |
| 120 |
|
| 121 |
public |
| 122 |
|
| 123 |
TrackString: string; |
| 124 |
Title: string; |
| 125 |
Artist: string; |
| 126 |
Album: string; |
| 127 |
Year: string; |
| 128 |
Genre: string; |
| 129 |
Comment: string; |
| 130 |
//extra |
| 131 |
xTones: string; |
| 132 |
xStyles: string; |
| 133 |
xMood: string; |
| 134 |
xSituation: string; |
| 135 |
xRating: string; |
| 136 |
xQuality: string; |
| 137 |
xTempo: string; |
| 138 |
xType: string; |
| 139 |
|
| 140 |
// |
| 141 |
Composer: string; |
| 142 |
Language: string; |
| 143 |
Copyright: string; |
| 144 |
Link: string; |
| 145 |
Encoder: string; |
| 146 |
Lyrics: string; |
| 147 |
Performer: string; |
| 148 |
License: string; |
| 149 |
Organization: string; |
| 150 |
Description: string; |
| 151 |
Location: string; |
| 152 |
Contact: string; |
| 153 |
ISRC: string; |
| 154 |
aExtraFields: array of array of string; |
| 155 |
|
| 156 |
constructor Create; |
| 157 |
destructor Destroy; override; |
| 158 |
|
| 159 |
function ReadFromFile( const sFile: String ): boolean; |
| 160 |
function SaveToFile( const sFile: String; const bBasicOnly: boolean = false ): boolean; |
| 161 |
function RemoveFromFile( const sFile: String ):boolean; |
| 162 |
procedure AddExtraField(const sID, sValue: string); |
| 163 |
|
| 164 |
property Channels: Byte read FChannels; // Number of channels |
| 165 |
property SampleRate: Integer read FSampleRate; // Sample rate (hz) |
| 166 |
property BitsPerSample: Byte read FBitsPerSample; // Bits per sample |
| 167 |
property FileLength: integer read FFileLength; // File length (bytes) |
| 168 |
property Samples: Int64 read FSamples; // Number of samples |
| 169 |
property Valid: Boolean read FIsValid; // True if header valid |
| 170 |
property Duration: Double read FGetDuration; // Duration (seconds) |
| 171 |
property Ratio: Double read FGetRatio; // Compression ratio (%) |
| 172 |
property Track: Word read FGetTrack; // Track number |
| 173 |
property Bitrate: integer read FBitrate; |
| 174 |
property ChannelMode: string read FGetChannelMode; |
| 175 |
property Exists: boolean read FExists; |
| 176 |
property Vendor: string read FVendor; |
| 177 |
property FileName: String read FFileName; |
| 178 |
property AudioOffset: integer read FAudioOffset; //offset of audio data |
| 179 |
property HasLyrics: boolean read FGetHasLyrics; |
| 180 |
end; |
| 181 |
|
| 182 |
var |
| 183 |
bTAG_PreserveDate: boolean; |
| 184 |
|
| 185 |
|
| 186 |
implementation |
| 187 |
|
| 188 |
(* -------------------------------------------------------------------------- *) |
| 189 |
|
| 190 |
procedure TFLACfile.FResetData( const bHeaderInfo, bTagFields :boolean ); |
| 191 |
var |
| 192 |
i: integer; |
| 193 |
begin |
| 194 |
|
| 195 |
if bHeaderInfo then begin |
| 196 |
FFileName := ''; |
| 197 |
FPadding := 0; |
| 198 |
FPaddingLast := false; |
| 199 |
FPaddingFragments := false; |
| 200 |
FChannels := 0; |
| 201 |
FSampleRate := 0; |
| 202 |
FBitsPerSample := 0; |
| 203 |
FFileLength := 0; |
| 204 |
FSamples := 0; |
| 205 |
FVorbisIndex := 0; |
| 206 |
FPaddingIndex := 0; |
| 207 |
FVCOffset := 0; |
| 208 |
FAudioOffset := 0; |
| 209 |
|
| 210 |
for i := 0 to Length( aMetaBlockOther ) - 1 do aMetaBlockOther[ i ].Data.Free; |
| 211 |
SetLength( aMetaBlockOther, 0 ); |
| 212 |
end; |
| 213 |
|
| 214 |
//tag data |
| 215 |
if bTagFields then begin |
| 216 |
FVendor := ''; |
| 217 |
FTagSize := 0; |
| 218 |
FExists := false; |
| 219 |
|
| 220 |
Title := ''; |
| 221 |
Artist := ''; |
| 222 |
Album := ''; |
| 223 |
TrackString := ''; |
| 224 |
Year := ''; |
| 225 |
Genre := ''; |
| 226 |
Comment := ''; |
| 227 |
//extra |
| 228 |
xTones := ''; |
| 229 |
xStyles := ''; |
| 230 |
xMood := ''; |
| 231 |
xSituation := ''; |
| 232 |
xRating := ''; |
| 233 |
xQuality := ''; |
| 234 |
xTempo := ''; |
| 235 |
xType := ''; |
| 236 |
|
| 237 |
// |
| 238 |
Composer := ''; |
| 239 |
Language := ''; |
| 240 |
Copyright := ''; |
| 241 |
Link := ''; |
| 242 |
Encoder := ''; |
| 243 |
Lyrics := ''; |
| 244 |
Performer := ''; |
| 245 |
License := ''; |
| 246 |
Organization := ''; |
| 247 |
Description := ''; |
| 248 |
Location := ''; |
| 249 |
Contact := ''; |
| 250 |
ISRC := ''; |
| 251 |
SetLength( aExtraFields, 0 ); |
| 252 |
end; |
| 253 |
end; |
| 254 |
|
| 255 |
(* -------------------------------------------------------------------------- *) |
| 256 |
// Check for right FLAC file data |
| 257 |
function TFLACfile.FIsValid: Boolean; |
| 258 |
begin |
| 259 |
result := (FHeader.StreamMarker = 'fLaC') and |
| 260 |
(FChannels > 0) and |
| 261 |
(FSampleRate > 0) and |
| 262 |
(FBitsPerSample > 0) and |
| 263 |
(FSamples > 0); |
| 264 |
end; |
| 265 |
|
| 266 |
(* -------------------------------------------------------------------------- *) |
| 267 |
|
| 268 |
function TFLACfile.FGetDuration: Double; |
| 269 |
begin |
| 270 |
if (FIsValid) and (FSampleRate > 0) then begin |
| 271 |
result := FSamples / FSampleRate |
| 272 |
end else begin |
| 273 |
result := 0; |
| 274 |
end; |
| 275 |
end; |
| 276 |
|
| 277 |
(* -------------------------------------------------------------------------- *) |
| 278 |
|
| 279 |
function TFLACfile.FGetTrack: Word; |
| 280 |
var |
| 281 |
Index, Value, Code: Integer; |
| 282 |
begin |
| 283 |
{ Extract track from string } |
| 284 |
Index := Pos('/', TrackString); |
| 285 |
if Index = 0 then Val(TrackString, Value, Code) |
| 286 |
else Val(Copy(TrackString, 1, Index - 1), Value, Code); |
| 287 |
if Code = 0 then Result := Value |
| 288 |
else Result := 0; |
| 289 |
end; |
| 290 |
|
| 291 |
(* -------------------------------------------------------------------------- *) |
| 292 |
// Get compression ratio |
| 293 |
function TFLACfile.FGetRatio: Double; |
| 294 |
begin |
| 295 |
if FIsValid then begin |
| 296 |
result := FFileLength / (FSamples * FChannels * FBitsPerSample / 8) * 100 |
| 297 |
end else begin |
| 298 |
result := 0; |
| 299 |
end; |
| 300 |
end; |
| 301 |
|
| 302 |
(* -------------------------------------------------------------------------- *) |
| 303 |
// Get channel mode |
| 304 |
function TFLACfile.FGetChannelMode: string; |
| 305 |
begin |
| 306 |
if FIsValid then begin |
| 307 |
case FChannels of |
| 308 |
1 : result := 'Mono'; |
| 309 |
2 : result := 'Stereo'; |
| 310 |
else result := 'Multi Channel'; |
| 311 |
end; |
| 312 |
end else begin |
| 313 |
result := ''; |
| 314 |
end; |
| 315 |
end; |
| 316 |
|
| 317 |
(* -------------------------------------------------------------------------- *) |
| 318 |
|
| 319 |
function TFLACfile.FGetHasLyrics: boolean; |
| 320 |
begin |
| 321 |
result := ( Trim( Lyrics ) <> '' ); |
| 322 |
end; |
| 323 |
|
| 324 |
(* -------------------------------------------------------------------------- *) |
| 325 |
|
| 326 |
constructor TFLACfile.Create; |
| 327 |
begin |
| 328 |
inherited; |
| 329 |
FID3v2 := TID3v2.Create; |
| 330 |
FResetData( true, true ); |
| 331 |
end; |
| 332 |
destructor TFLACfile.Destroy; |
| 333 |
begin |
| 334 |
FResetData( true, true ); |
| 335 |
FID3v2.Free; |
| 336 |
inherited; |
| 337 |
end; |
| 338 |
|
| 339 |
(* -------------------------------------------------------------------------- *) |
| 340 |
|
| 341 |
function TFLACfile.ReadFromFile( const sFile: String ): boolean; |
| 342 |
begin |
| 343 |
FResetData( false, true ); |
| 344 |
result := GetInfo( sFile, true ); |
| 345 |
end; |
| 346 |
|
| 347 |
(* -------------------------------------------------------------------------- *) |
| 348 |
|
| 349 |
function TFLACfile.GetInfo( sFile: String; bSetTags: boolean ): boolean; |
| 350 |
var |
| 351 |
SourceFile: TFileStreamEx; |
| 352 |
aMetaDataBlockHeader: array[1..4] of byte; |
| 353 |
iBlockLength, iMetaType, iIndex: integer; |
| 354 |
bPaddingFound: boolean; |
| 355 |
begin |
| 356 |
|
| 357 |
result := true; |
| 358 |
bPaddingFound := false; |
| 359 |
FResetData( true, false ); |
| 360 |
try |
| 361 |
{ Read data from ID3 tags } |
| 362 |
FID3v2.ReadFromFile(sFile); |
| 363 |
|
| 364 |
// Set read-access and open file |
| 365 |
SourceFile := TFileStreamEx.Create(sFile, fmOpenRead or fmShareDenyWrite); |
| 366 |
FFileLength := SourceFile.Size; |
| 367 |
FFileName := sFile; |
| 368 |
|
| 369 |
{ Seek past the ID3v2 tag, if there is one } |
| 370 |
if FID3v2.Exists then begin |
| 371 |
SourceFile.Seek(FID3v2.Size, soFromBeginning) |
| 372 |
end; |
| 373 |
|
| 374 |
// Read header data |
| 375 |
FillChar( FHeader, SizeOf(FHeader), 0 ); |
| 376 |
SourceFile.Read( FHeader, SizeOf(FHeader) ); |
| 377 |
|
| 378 |
// Process data if loaded and header valid |
| 379 |
if FHeader.StreamMarker = 'fLaC' then begin |
| 380 |
|
| 381 |
with FHeader do begin |
| 382 |
FChannels := ( Info[13] shr 1 and $7 + 1 ); |
| 383 |
FSampleRate := ( Info[11] shl 12 or Info[12] shl 4 or Info[13] shr 4 ); |
| 384 |
FBitsPerSample := ( Info[13] and 1 shl 4 or Info[14] shr 4 + 1 ); |
| 385 |
FSamples := ( Info[15] shl 24 or Info[16] shl 16 or Info[17] shl 8 or Info[18] ); |
| 386 |
end; |
| 387 |
|
| 388 |
if (FHeader.MetaDataBlockHeader[1] and $80) <> 0 then exit; //no metadata blocks exist |
| 389 |
iIndex := 0; |
| 390 |
repeat // read more metadata blocks if available |
| 391 |
|
| 392 |
SourceFile.Read( aMetaDataBlockHeader, 4 ); |
| 393 |
|
| 394 |
iIndex := iIndex + 1; // metadatablock index |
| 395 |
iBlockLength := (aMetaDataBlockHeader[2] shl 16 or aMetaDataBlockHeader[3] shl 8 or aMetaDataBlockHeader[4]); //decode length |
| 396 |
if iBlockLength <= 0 then exit; // can it be 0 ? |
| 397 |
|
| 398 |
iMetaType := (aMetaDataBlockHeader[1] and $7F); // decode metablock type |
| 399 |
|
| 400 |
if iMetaType = META_VORBIS_COMMENT then begin // read vorbis block |
| 401 |
FVCOffset := SourceFile.Position; |
| 402 |
FTagSize := iBlockLength; |
| 403 |
FVorbisIndex := iIndex; |
| 404 |
ReadTag(SourceFile, bSetTags); // set up fields |
| 405 |
end else if (iMetaType = META_PADDING) and not bPaddingFound then begin // we have padding block |
| 406 |
FPadding := iBlockLength; // if we find more skip & put them in metablock array |
| 407 |
FPaddingLast := ((aMetaDataBlockHeader[1] and $80) <> 0); |
| 408 |
FPaddingIndex := iIndex; |
| 409 |
bPaddingFound := true; |
| 410 |
SourceFile.Seek(FPadding, soCurrent); // advance into file till next block or audio data start |
| 411 |
end else begin // all other |
| 412 |
if iMetaType <= META_PICTURE then begin // is it a valid metablock ? |
| 413 |
if (iMetaType = META_PADDING) then begin // set flag for fragmented padding blocks |
| 414 |
FPaddingFragments := true; |
| 415 |
end; |
| 416 |
AddMetaDataOther(aMetaDataBlockHeader, SourceFile, iBlocklength, iIndex); |
| 417 |
end else begin |
| 418 |
FSamples := 0; // ops... |
| 419 |
Exit; |
| 420 |
end; |
| 421 |
end; |
| 422 |
|
| 423 |
until ((aMetaDataBlockHeader[1] and $80) <> 0); // until is last flag ( first bit = 1 ) |
| 424 |
|
| 425 |
end; |
| 426 |
finally |
| 427 |
if FIsValid then begin |
| 428 |
FAudioOffset := SourceFile.Position; // we need that to rebuild the file if nedeed |
| 429 |
FBitrate := Round( ( ( FFileLength - FAudioOffset ) / 1000 ) * 8 / FGetDuration ); //time to calculate average bitrate |
| 430 |
end else begin |
| 431 |
result := false; |
| 432 |
end; |
| 433 |
FreeAndNil(SourceFile); |
| 434 |
end; |
| 435 |
|
| 436 |
end; |
| 437 |
|
| 438 |
(* -------------------------------------------------------------------------- *) |
| 439 |
|
| 440 |
procedure TFLACfile.AddMetaDataOther( aMetaHeader: array of Byte; stream: TFileStreamEx; const iBlocklength,iIndex: integer ); |
| 441 |
var |
| 442 |
iMetaLen: integer; |
| 443 |
begin |
| 444 |
// enlarge array |
| 445 |
iMetaLen := Length( aMetaBlockOther ) + 1; |
| 446 |
SetLength( aMetaBlockOther, iMetaLen ); |
| 447 |
// save header |
| 448 |
aMetaBlockOther[ iMetaLen - 1 ].MetaDataBlockHeader[1] := aMetaHeader[0]; |
| 449 |
aMetaBlockOther[ iMetaLen - 1 ].MetaDataBlockHeader[2] := aMetaHeader[1]; |
| 450 |
aMetaBlockOther[ iMetaLen - 1 ].MetaDataBlockHeader[3] := aMetaHeader[2]; |
| 451 |
aMetaBlockOther[ iMetaLen - 1 ].MetaDataBlockHeader[4] := aMetaHeader[3]; |
| 452 |
// save content in a stream |
| 453 |
aMetaBlockOther[ iMetaLen - 1 ].Data := TMemoryStream.Create; |
| 454 |
aMetaBlockOther[ iMetaLen - 1 ].Data.Position := 0; |
| 455 |
aMetaBlockOther[ iMetaLen - 1 ].Data.CopyFrom( stream, iBlocklength ); |
| 456 |
end; |
| 457 |
|
| 458 |
(* -------------------------------------------------------------------------- *) |
| 459 |
|
| 460 |
procedure TFLACfile.ReadTag( Source: TFileStreamEx; bSetTagFields: boolean ); |
| 461 |
var |
| 462 |
i, iCount, iSize, iSepPos: Integer; |
| 463 |
Data, sFieldID, sFieldData: String; |
| 464 |
begin |
| 465 |
|
| 466 |
Source.Read( iSize, SizeOf( iSize ) ); // vendor |
| 467 |
SetLength( Data, iSize ); |
| 468 |
Source.Read( Data[ 1 ], iSize ); |
| 469 |
FVendor := String( Data ); |
| 470 |
|
| 471 |
Source.Read( iCount, SizeOf( iCount ) ); //fieldcount |
| 472 |
|
| 473 |
FExists := ( iCount > 0 ); |
| 474 |
|
| 475 |
for i := 0 to iCount - 1 do begin |
| 476 |
Source.Read( iSize, SizeOf( iSize ) ); |
| 477 |
SetLength( Data , iSize ); |
| 478 |
Source.Read( Data[ 1 ], iSize ); |
| 479 |
|
| 480 |
if not bSetTagFields then Continue; // if we don't want to re asign fields we skip |
| 481 |
|
| 482 |
iSepPos := Pos( '=', String( Data ) ); |
| 483 |
if iSepPos > 0 then begin |
| 484 |
|
| 485 |
sFieldID := UpperCase( Copy( String( Data ), 1, iSepPos - 1) ); |
| 486 |
sFieldData := Copy( String( Data ), iSepPos + 1, MaxInt ); |
| 487 |
|
| 488 |
if (sFieldID = 'TRACKNUMBER') and (TrackString = '') then begin |
| 489 |
TrackString := sFieldData; |
| 490 |
end else if (sFieldID = 'ARTIST') and (Artist = '') then begin |
| 491 |
Artist := sFieldData; |
| 492 |
end else if (sFieldID = 'ALBUM') and (Album = '') then begin |
| 493 |
Album := sFieldData; |
| 494 |
end else if (sFieldID = 'TITLE') and (Title = '') then begin |
| 495 |
Title := sFieldData; |
| 496 |
end else if (sFieldID = 'DATE') and (Year = '') then begin |
| 497 |
Year := sFieldData; |
| 498 |
end else if (sFieldID = 'GENRE') and (Genre = '') then begin |
| 499 |
Genre := sFieldData; |
| 500 |
end else if (sFieldID = 'COMMENT') and (Comment = '') then begin |
| 501 |
Comment := sFieldData; |
| 502 |
end else if (sFieldID = 'COMPOSER') and (Composer = '') then begin |
| 503 |
Composer := sFieldData; |
| 504 |
end else if (sFieldID = 'LANGUAGE') and (Language = '') then begin |
| 505 |
Language := sFieldData; |
| 506 |
end else if (sFieldID = 'COPYRIGHT') and (Copyright = '') then begin |
| 507 |
Copyright := sFieldData; |
| 508 |
end else if (sFieldID = 'URL') and (Link = '') then begin |
| 509 |
Link := sFieldData; |
| 510 |
end else if (sFieldID = 'ENCODER') and (Encoder = '') then begin |
| 511 |
Encoder := sFieldData; |
| 512 |
end else if (sFieldID = 'TONES') and (xTones = '') then begin |
| 513 |
xTones := sFieldData; |
| 514 |
end else if (sFieldID = 'STYLES') and (xStyles = '') then begin |
| 515 |
xStyles := sFieldData; |
| 516 |
end else if (sFieldID = 'MOOD') and (xMood = '') then begin |
| 517 |
xMood := sFieldData; |
| 518 |
end else if (sFieldID = 'SITUATION') and (xSituation = '') then begin |
| 519 |
xSituation := sFieldData; |
| 520 |
end else if (sFieldID = 'RATING') and (xRating = '') then begin |
| 521 |
xRating := sFieldData; |
| 522 |
end else if (sFieldID = 'QUALITY') and (xQuality = '') then begin |
| 523 |
xQuality := sFieldData; |
| 524 |
end else if (sFieldID = 'TEMPO') and (xTempo = '') then begin |
| 525 |
xTempo := sFieldData; |
| 526 |
end else if (sFieldID = 'TYPE') and (xType = '') then begin |
| 527 |
xType := sFieldData; |
| 528 |
end else if (sFieldID = 'LYRICS') and (Lyrics = '') then begin |
| 529 |
Lyrics := sFieldData; |
| 530 |
end else if (sFieldID = 'PERFORMER') and (Performer = '') then begin |
| 531 |
Performer := sFieldData; |
| 532 |
end else if (sFieldID = 'LICENSE') and (License = '') then begin |
| 533 |
License := sFieldData; |
| 534 |
end else if (sFieldID = 'ORGANIZATION') and (Organization = '') then begin |
| 535 |
Organization := sFieldData; |
| 536 |
end else if (sFieldID = 'DESCRIPTION') and (Description = '') then begin |
| 537 |
Description := sFieldData; |
| 538 |
end else if (sFieldID = 'LOCATION') and (Location = '') then begin |
| 539 |
Location := sFieldData; |
| 540 |
end else if (sFieldID = 'CONTACT') and (Contact = '') then begin |
| 541 |
Contact := sFieldData; |
| 542 |
end else if (sFieldID = 'ISRC') and (ISRC = '') then begin |
| 543 |
ISRC := sFieldData; |
| 544 |
end else begin // more fields |
| 545 |
AddExtraField( sFieldID, sFieldData ); |
| 546 |
end; |
| 547 |
|
| 548 |
end; |
| 549 |
|
| 550 |
end; |
| 551 |
|
| 552 |
end; |
| 553 |
|
| 554 |
(* -------------------------------------------------------------------------- *) |
| 555 |
|
| 556 |
procedure TFLACfile.AddExtraField(const sID, sValue: string); |
| 557 |
var |
| 558 |
iExtraLen: integer; |
| 559 |
begin |
| 560 |
iExtraLen := Length( aExtraFields ) + 1; |
| 561 |
SetLength( aExtraFields, iExtraLen ); |
| 562 |
SetLength( aExtraFields[ iExtraLen - 1 ], 2 ); |
| 563 |
|
| 564 |
aExtraFields[ iExtraLen - 1, 0 ] := sID; |
| 565 |
aExtraFields[ iExtraLen - 1, 1 ] := sValue; |
| 566 |
end; |
| 567 |
|
| 568 |
(* -------------------------------------------------------------------------- *) |
| 569 |
|
| 570 |
function TFLACfile.SaveToFile( const sFile: String; const bBasicOnly: boolean = false ): boolean; |
| 571 |
var |
| 572 |
i, iFieldCount, iSize: Integer; |
| 573 |
VorbisBlock, Tag: TStringStream; |
| 574 |
|
| 575 |
procedure _WriteTagBuff( sID, sData: string ); |
| 576 |
var |
| 577 |
sTmp: string; |
| 578 |
iTmp: integer; |
| 579 |
begin |
| 580 |
if sData <> '' then begin |
| 581 |
sTmp := sID + '=' + sData; |
| 582 |
iTmp := Length( sTmp ); |
| 583 |
Tag.Write( iTmp, SizeOf( iTmp ) ); |
| 584 |
Tag.WriteString( sTmp ); |
| 585 |
iFieldCount := iFieldCount + 1; |
| 586 |
end; |
| 587 |
end; |
| 588 |
|
| 589 |
begin |
| 590 |
|
| 591 |
try |
| 592 |
result := false; |
| 593 |
|
| 594 |
Tag := TStringStream.Create(''); |
| 595 |
VorbisBlock := TStringStream.Create(''); |
| 596 |
if not GetInfo( sFile, false ) then exit; //reload all except tag fields |
| 597 |
|
| 598 |
iFieldCount := 0; |
| 599 |
|
| 600 |
_WriteTagBuff( 'TRACKNUMBER', TrackString ); |
| 601 |
_WriteTagBuff( 'ARTIST', Artist ); |
| 602 |
_WriteTagBuff( 'ALBUM', Album ); |
| 603 |
_WriteTagBuff( 'TITLE', Title ); |
| 604 |
_WriteTagBuff( 'DATE', Year ); |
| 605 |
_WriteTagBuff( 'GENRE', Genre ); |
| 606 |
_WriteTagBuff( 'COMMENT', Comment ); |
| 607 |
_WriteTagBuff( 'COMPOSER', Composer ); |
| 608 |
_WriteTagBuff( 'LANGUAGE', Language ); |
| 609 |
_WriteTagBuff( 'COPYRIGHT', Copyright ); |
| 610 |
_WriteTagBuff( 'URL', Link ); |
| 611 |
_WriteTagBuff( 'ENCODER', Encoder ); |
| 612 |
|
| 613 |
_WriteTagBuff( 'TONES', xTones ); |
| 614 |
_WriteTagBuff( 'STYLES', xStyles ); |
| 615 |
_WriteTagBuff( 'MOOD', xMood ); |
| 616 |
_WriteTagBuff( 'SITUATION', xSituation ); |
| 617 |
_WriteTagBuff( 'RATING', xRating ); |
| 618 |
_WriteTagBuff( 'QUALITY', xQuality ); |
| 619 |
_WriteTagBuff( 'TEMPO', xTempo ); |
| 620 |
_WriteTagBuff( 'TYPE', xType ); |
| 621 |
|
| 622 |
if not bBasicOnly then begin |
| 623 |
_WriteTagBuff( 'PERFORMER', Performer ); |
| 624 |
_WriteTagBuff( 'LICENSE', License ); |
| 625 |
_WriteTagBuff( 'ORGANIZATION', Organization ); |
| 626 |
_WriteTagBuff( 'DESCRIPTION', Description ); |
| 627 |
_WriteTagBuff( 'LOCATION', Location ); |
| 628 |
_WriteTagBuff( 'CONTACT', Contact ); |
| 629 |
_WriteTagBuff( 'ISRC', ISRC ); |
| 630 |
_WriteTagBuff( 'LYRICS', Lyrics ); |
| 631 |
|
| 632 |
for i := 0 to Length( aExtraFields ) - 1 do begin |
| 633 |
if Trim( aExtraFields[ i, 0 ] ) <> '' then _WriteTagBuff( aExtraFields[ i, 0 ], aExtraFields[ i, 1 ] ); |
| 634 |
end; |
| 635 |
end; |
| 636 |
|
| 637 |
// Write vendor info and number of fields |
| 638 |
with VorbisBlock do begin |
| 639 |
if FVendor = '' then FVendor := 'reference libFLAC 1.1.0 20030126'; // guess it |
| 640 |
iSize := Length( FVendor ); |
| 641 |
Write( iSize, SizeOf( iSize ) ); |
| 642 |
WriteString( FVendor ); |
| 643 |
Write( iFieldCount, SizeOf( iFieldCount ) ); |
| 644 |
end; |
| 645 |
|
| 646 |
VorbisBlock.CopyFrom( Tag, 0 ); // All tag data is here now |
| 647 |
VorbisBlock.Position := 0; |
| 648 |
|
| 649 |
result := RebuildFile( sFile, VorbisBlock ); |
| 650 |
FExists := result and (Tag.Size > 0 ); |
| 651 |
|
| 652 |
finally |
| 653 |
FreeAndNil( Tag ); |
| 654 |
FreeAndNil( VorbisBlock ); |
| 655 |
end; |
| 656 |
|
| 657 |
end; |
| 658 |
|
| 659 |
(* -------------------------------------------------------------------------- *) |
| 660 |
|
| 661 |
function TFLACfile.RemoveFromFile( const sFile: String ):boolean; |
| 662 |
begin |
| 663 |
FResetData( false, true ); |
| 664 |
result := SaveToFile( sFile ); |
| 665 |
if FExists then FExists := not result; |
| 666 |
end; |
| 667 |
|
| 668 |
(* -------------------------------------------------------------------------- *) |
| 669 |
// saves metablocks back to the file |
| 670 |
// always tries to rebuild header so padding exists after comment block and no more than 1 padding block exists |
| 671 |
function TFLACfile.RebuildFile( const sFile: String; VorbisBlock: TStringStream ): Boolean; |
| 672 |
var |
| 673 |
iFileAge: TFileTime; |
| 674 |
Source, Destination: TFileStreamEx; |
| 675 |
i, iNewPadding, iMetaCount, iExtraPadding: Integer; |
| 676 |
BufferName, sTmp: string; |
| 677 |
MetaDataBlockHeader: array[1..4] of Byte; |
| 678 |
oldHeader: TFlacHeader; |
| 679 |
MetaBlocks: TMemoryStream; |
| 680 |
bRebuild, bRearange: boolean; |
| 681 |
begin |
| 682 |
|
| 683 |
result := false; |
| 684 |
bRearange := false; |
| 685 |
iExtraPadding := 0; |
| 686 |
if (not mbFileExists(FileName)) or (not mbFileSetReadOnly(FileName, False)) then exit; |
| 687 |
|
| 688 |
try |
| 689 |
iFileAge := 0; |
| 690 |
if bTAG_PreserveDate then iFileAge := mbFileAge( FileName ); |
| 691 |
|
| 692 |
// re arrange other metadata in case of |
| 693 |
// 1. padding block is not aligned after vorbis comment |
| 694 |
// 2. insufficient padding - rearange upon file rebuild |
| 695 |
// 3. fragmented padding blocks |
| 696 |
iMetaCount := Length( aMetaBlockOther ); |
| 697 |
if (FPaddingIndex <> FVorbisIndex + 1) or (FPadding <= VorbisBlock.Size - FTagSize ) or FPaddingFragments then begin |
| 698 |
MetaBlocks := TMemoryStream.Create; |
| 699 |
for i := 0 to iMetaCount - 1 do begin |
| 700 |
aMetaBlockOther[ i ].MetaDataBlockHeader[ 1 ] := ( aMetaBlockOther[ i ].MetaDataBlockHeader[ 1 ] and $7f ); // not last |
| 701 |
|
| 702 |
if aMetaBlockOther[ i ].MetaDataBlockHeader[ 1 ] = META_PADDING then begin |
| 703 |
iExtraPadding := iExtraPadding + aMetaBlockOther[ i ].Data.Size + 4; // add padding size plus 4 bytes of header block |
| 704 |
end else begin |
| 705 |
aMetaBlockOther[ i ].Data.Position := 0; |
| 706 |
MetaBlocks.Write( aMetaBlockOther[ i ].MetaDataBlockHeader[ 1 ], 4 ); |
| 707 |
MetaBlocks.CopyFrom( aMetaBlockOther[ i ].Data, 0 ); |
| 708 |
end; |
| 709 |
|
| 710 |
end; |
| 711 |
MetaBlocks.Position := 0; |
| 712 |
bRearange := true; |
| 713 |
end; |
| 714 |
|
| 715 |
// set up file |
| 716 |
if (FPadding <= VorbisBlock.Size - FTagSize ) then begin // no room rebuild the file from scratch |
| 717 |
bRebuild := true; |
| 718 |
BufferName := FileName + '~'; |
| 719 |
Source := TFileStreamEx.Create( FileName, fmOpenRead ); // Set read-only and open old file, and create new |
| 720 |
Destination := TFileStreamEx.Create( BufferName, fmCreate ); |
| 721 |
Source.Read( oldHeader, sizeof( oldHeader ) ); |
| 722 |
oldHeader.MetaDataBlockHeader[ 1 ] := (oldHeader.MetaDataBlockHeader[ 1 ] and $7f ); //just in case no metadata existed |
| 723 |
Destination.Write( oldHeader, Sizeof( oldHeader ) ); |
| 724 |
Destination.CopyFrom( MetaBlocks, 0 ); |
| 725 |
end else begin |
| 726 |
bRebuild := false; |
| 727 |
Source := nil; |
| 728 |
Destination := TFileStreamEx.Create( FileName, fmOpenWrite); // Set write-access and open file |
| 729 |
if bRearange then begin |
| 730 |
Destination.Seek( SizeOf( FHeader ), soFromBeginning ); |
| 731 |
Destination.CopyFrom( MetaBlocks, 0 ); |
| 732 |
end else begin |
| 733 |
Destination.Seek( FVCOffset - 4, soFromBeginning ); |
| 734 |
end; |
| 735 |
end; |
| 736 |
|
| 737 |
// finally write vorbis block |
| 738 |
MetaDataBlockHeader[1] := META_VORBIS_COMMENT; |
| 739 |
MetaDataBlockHeader[2] := Byte(( VorbisBlock.Size shr 16 ) and 255 ); |
| 740 |
MetaDataBlockHeader[3] := Byte(( VorbisBlock.Size shr 8 ) and 255 ); |
| 741 |
MetaDataBlockHeader[4] := Byte( VorbisBlock.Size and 255 ); |
| 742 |
Destination.Write( MetaDataBlockHeader[ 1 ], SizeOf( MetaDataBlockHeader ) ); |
| 743 |
Destination.CopyFrom( VorbisBlock, VorbisBlock.Size ); |
| 744 |
|
| 745 |
// and add padding |
| 746 |
if FPaddingLast or bRearange then begin |
| 747 |
MetaDataBlockHeader[1] := META_PADDING or $80; |
| 748 |
end else begin |
| 749 |
MetaDataBlockHeader[1] := META_PADDING; |
| 750 |
end; |
| 751 |
if bRebuild then begin |
| 752 |
iNewPadding := 4096; // why not... |
| 753 |
end else begin |
| 754 |
if FTagSize > VorbisBlock.Size then begin // tag got smaller increase padding |
| 755 |
iNewPadding := (FPadding + FTagSize - VorbisBlock.Size) + iExtraPadding; |
| 756 |
end else begin // tag got bigger shrink padding |
| 757 |
iNewPadding := (FPadding - VorbisBlock.Size + FTagSize ) + iExtraPadding; |
| 758 |
end; |
| 759 |
end; |
| 760 |
MetaDataBlockHeader[2] := Byte(( iNewPadding shr 16 ) and 255 ); |
| 761 |
MetaDataBlockHeader[3] := Byte(( iNewPadding shr 8 ) and 255 ); |
| 762 |
MetaDataBlockHeader[4] := Byte( iNewPadding and 255 ); |
| 763 |
Destination.Write(MetaDataBlockHeader[ 1 ], 4); |
| 764 |
if (FPadding <> iNewPadding) or bRearange then begin // fill the block with zeros |
| 765 |
sTmp := DupeString( #0, iNewPadding ); |
| 766 |
Destination.Write( sTmp[1], iNewPadding ); |
| 767 |
end; |
| 768 |
|
| 769 |
// finish |
| 770 |
if bRebuild then begin // time to put back the audio data... |
| 771 |
Source.Seek( FAudioOffset, soFromBeginning ); |
| 772 |
Destination.CopyFrom( Source, Source.Size - FAudioOffset ); |
| 773 |
Source.Free; |
| 774 |
Destination.Free; |
| 775 |
if ( mbDeleteFile( FileName ) ) and ( mbRenameFile( BufferName, FileName ) ) then begin //Replace old file and delete temporary file |
| 776 |
result := true |
| 777 |
end else begin |
| 778 |
raise Exception.Create(''); |
| 779 |
end; |
| 780 |
end else begin |
| 781 |
result := true; |
| 782 |
Destination.Free; |
| 783 |
end; |
| 784 |
|
| 785 |
// post save tasks |
| 786 |
if bTAG_PreserveDate then mbFileSetTime( FileName, iFileAge ); |
| 787 |
if bRearange then FreeAndNil( MetaBlocks ); |
| 788 |
|
| 789 |
except |
| 790 |
// Access error |
| 791 |
if mbFileExists( BufferName ) then mbDeleteFile( BufferName ); |
| 792 |
end; |
| 793 |
end; |
| 794 |
|
| 795 |
(* -------------------------------------------------------------------------- *) |
| 796 |
|
| 797 |
end. |
| 798 |
|