| 1 |
{ *************************************************************************** } |
| 2 |
{ } |
| 3 |
{ Audio Tools Library } |
| 4 |
{ Class TMonkey - for manipulating with Monkey's Audio file information } |
| 5 |
{ } |
| 6 |
{ http://mac.sourceforge.net/atl/ } |
| 7 |
{ e-mail: [email protected] } |
| 8 |
{ } |
| 9 |
{ Copyright (c) 2000-2002 by Jurgen Faul } |
| 10 |
{ Copyright (c) 2003-2005 by The MAC Team } |
| 11 |
{ } |
| 12 |
{ Version 1.7 (April 2005) by Gambit } |
| 13 |
{ - updated to unicode file access } |
| 14 |
{ } |
| 15 |
{ Version 1.6 (11 April 2004) by Gambit } |
| 16 |
{ - Added Ratio property again } |
| 17 |
{ } |
| 18 |
{ Version 1.5 (22 August 2003) by MaDah } |
| 19 |
{ - Added support for Monkey's Audio 3.98 } |
| 20 |
{ - Added/changed/removed some stuff } |
| 21 |
{ } |
| 22 |
{ Version 1.4 (29 July 2002) } |
| 23 |
{ - Correction for calculating of duration } |
| 24 |
{ } |
| 25 |
{ Version 1.1 (11 September 2001) } |
| 26 |
{ - Added property Samples } |
| 27 |
{ - Removed WAV header information } |
| 28 |
{ } |
| 29 |
{ Version 1.0 (7 September 2001) } |
| 30 |
{ - Support for Monkey's Audio files } |
| 31 |
{ - Class TID3v1: reading & writing support for ID3v1 tags } |
| 32 |
{ - Class TID3v2: reading & writing support for ID3v2 tags } |
| 33 |
{ - Class TAPEtag: reading & writing support for APE tags } |
| 34 |
{ } |
| 35 |
{ This library is free software; you can redistribute it and/or } |
| 36 |
{ modify it under the terms of the GNU Lesser General Public } |
| 37 |
{ License as published by the Free Software Foundation; either } |
| 38 |
{ version 2.1 of the License, or (at your option) any later version. } |
| 39 |
{ } |
| 40 |
{ This library is distributed in the hope that it will be useful, } |
| 41 |
{ but WITHOUT ANY WARRANTY; without even the implied warranty of } |
| 42 |
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU } |
| 43 |
{ Lesser General Public License for more details. } |
| 44 |
{ } |
| 45 |
{ You should have received a copy of the GNU Lesser General Public } |
| 46 |
{ License along with this library; if not, write to the Free Software } |
| 47 |
{ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA } |
| 48 |
{ } |
| 49 |
{ *************************************************************************** } |
| 50 |
|
| 51 |
unit Monkey; |
| 52 |
|
| 53 |
interface |
| 54 |
|
| 55 |
uses |
| 56 |
Classes, SysUtils, ID3v1, ID3v2, APEtag, DCClassesUtf8; |
| 57 |
|
| 58 |
const |
| 59 |
{ Compression level codes } |
| 60 |
MONKEY_COMPRESSION_FAST = 1000; { Fast (poor) } |
| 61 |
MONKEY_COMPRESSION_NORMAL = 2000; { Normal (good) } |
| 62 |
MONKEY_COMPRESSION_HIGH = 3000; { High (very good) } |
| 63 |
MONKEY_COMPRESSION_EXTRA_HIGH = 4000; { Extra high (best) } |
| 64 |
MONKEY_COMPRESSION_INSANE = 5000; { Insane } |
| 65 |
MONKEY_COMPRESSION_BRAINDEAD = 6000; { BrainDead } |
| 66 |
{ Compression level names } |
| 67 |
MONKEY_COMPRESSION: array [0..6] of string = |
| 68 |
('Unknown', 'Fast', 'Normal', 'High', 'Extra High', 'Insane', 'BrainDead'); |
| 69 |
|
| 70 |
{ Format flags, only for Monkey's Audio <= 3.97 } |
| 71 |
MONKEY_FLAG_8_BIT = 1; // Audio 8-bit |
| 72 |
MONKEY_FLAG_CRC = 2; // New CRC32 error detection |
| 73 |
MONKEY_FLAG_PEAK_LEVEL = 4; // Peak level stored |
| 74 |
MONKEY_FLAG_24_BIT = 8; // Audio 24-bit |
| 75 |
MONKEY_FLAG_SEEK_ELEMENTS = 16; // Number of seek elements stored |
| 76 |
MONKEY_FLAG_WAV_NOT_STORED = 32; // WAV header not stored |
| 77 |
|
| 78 |
{ Channel mode names } |
| 79 |
MONKEY_MODE: array [0..2] of string = |
| 80 |
('Unknown', 'Mono', 'Stereo'); |
| 81 |
|
| 82 |
type |
| 83 |
{ Class TMonkey } |
| 84 |
TMonkey = class(TObject) |
| 85 |
private |
| 86 |
{ Private declarations } |
| 87 |
FValid : boolean; |
| 88 |
// Stuff loaded from the header: |
| 89 |
FVersion : integer; |
| 90 |
FVersionStr : string; |
| 91 |
FChannels : integer; |
| 92 |
FSampleRate : integer; |
| 93 |
FBits : integer; |
| 94 |
FPeakLevel : longword; |
| 95 |
FPeakLevelRatio : double; |
| 96 |
FTotalSamples : int64; |
| 97 |
FBitrate : double; |
| 98 |
FDuration : double; |
| 99 |
FCompressionMode : integer; |
| 100 |
FCompressionModeStr : string; |
| 101 |
// FormatFlags, only used with Monkey's <= 3.97 |
| 102 |
FFormatFlags : integer; |
| 103 |
FHasPeakLevel : boolean; |
| 104 |
FHasSeekElements : boolean; |
| 105 |
FWavNotStored : boolean; |
| 106 |
// Tagging |
| 107 |
FID3v1 : TID3v1; |
| 108 |
FID3v2 : TID3v2; |
| 109 |
FAPEtag : TAPEtag; |
| 110 |
// |
| 111 |
FFileSize : int64; |
| 112 |
|
| 113 |
procedure FResetData; |
| 114 |
|
| 115 |
function FGetRatio: Double; |
| 116 |
function FGetChannelMode: string; |
| 117 |
|
| 118 |
public |
| 119 |
{ Public declarations } |
| 120 |
constructor Create; { Create object } |
| 121 |
destructor Destroy; override; { Destroy object } |
| 122 |
|
| 123 |
function ReadFromFile(const FileName: String): Boolean; { Load header } |
| 124 |
|
| 125 |
property FileSize : int64 read FFileSize; |
| 126 |
property Valid : boolean read FValid; |
| 127 |
property Version : integer read FVersion; |
| 128 |
property VersionStr : string read FVersionStr; |
| 129 |
property Channels : integer read FChannels; |
| 130 |
property SampleRate : integer read FSamplerate; |
| 131 |
property Bits : integer read FBits; |
| 132 |
property Bitrate : double read FBitrate; |
| 133 |
property Duration : double read FDuration; |
| 134 |
property PeakLevel : longword read FPeakLevel; |
| 135 |
property PeakLevelRatio : double read FPeakLevelRatio; |
| 136 |
property TotalSamples : int64 read FTotalSamples; |
| 137 |
property CompressionMode : integer read FCompressionMode; |
| 138 |
property CompressionModeStr: string read FCompressionModeStr; |
| 139 |
property ChannelMode: string read FGetChannelMode; { Channel mode name } |
| 140 |
// FormatFlags, only used with Monkey's <= 3.97 |
| 141 |
property FormatFlags : integer read FFormatFlags; |
| 142 |
property HasPeakLevel : boolean read FHasPeakLevel; |
| 143 |
property HasSeekElements : boolean read FHasSeekElements; |
| 144 |
property WavNotStored : boolean read FWavNotStored; |
| 145 |
// Tagging |
| 146 |
property ID3v1: TID3v1 read FID3v1; { ID3v1 tag data } |
| 147 |
property ID3v2: TID3v2 read FID3v2; { ID3v2 tag data } |
| 148 |
property APEtag: TAPEtag read FAPEtag; { APE tag data } |
| 149 |
|
| 150 |
property Ratio: Double read FGetRatio; { Compression ratio (%) } |
| 151 |
end; |
| 152 |
|
| 153 |
implementation |
| 154 |
|
| 155 |
type |
| 156 |
{ Real structure of Monkey's Audio header } |
| 157 |
// common header for all versions |
| 158 |
APE_HEADER = packed record |
| 159 |
cID: array[0..3] of byte; // should equal 'MAC ' |
| 160 |
nVersion : WORD; // version number * 1000 (3.81 = 3810) |
| 161 |
end; |
| 162 |
// old header for <= 3.97 |
| 163 |
APE_HEADER_OLD = packed record |
| 164 |
nCompressionLevel, // the compression level |
| 165 |
nFormatFlags, // any format flags (for future use) |
| 166 |
nChannels: word; // the number of channels (1 or 2) |
| 167 |
nSampleRate, // the sample rate (typically 44100) |
| 168 |
nHeaderBytes, // the bytes after the MAC header that compose the WAV header |
| 169 |
nTerminatingBytes, // the bytes after that raw data (for extended info) |
| 170 |
nTotalFrames, // the number of frames in the file |
| 171 |
nFinalFrameBlocks: longword; // the number of samples in the final frame |
| 172 |
nInt : integer; |
| 173 |
end; |
| 174 |
// new header for >= 3.98 |
| 175 |
APE_HEADER_NEW = packed record |
| 176 |
nCompressionLevel : word; // the compression level (see defines I.E. COMPRESSION_LEVEL_FAST) |
| 177 |
nFormatFlags : word; // any format flags (for future use) Note: NOT the same flags as the old header! |
| 178 |
nBlocksPerFrame : longword; // the number of audio blocks in one frame |
| 179 |
nFinalFrameBlocks : longword; // the number of audio blocks in the final frame |
| 180 |
nTotalFrames : longword; // the total number of frames |
| 181 |
nBitsPerSample : word; // the bits per sample (typically 16) |
| 182 |
nChannels : word; // the number of channels (1 or 2) |
| 183 |
nSampleRate : longword; // the sample rate (typically 44100) |
| 184 |
end; |
| 185 |
// data descriptor for >= 3.98 |
| 186 |
APE_DESCRIPTOR = packed record |
| 187 |
padded : Word; // padding/reserved (always empty) |
| 188 |
nDescriptorBytes, // the number of descriptor bytes (allows later expansion of this header) |
| 189 |
nHeaderBytes, // the number of header APE_HEADER bytes |
| 190 |
nSeekTableBytes, // the number of bytes of the seek table |
| 191 |
nHeaderDataBytes, // the number of header data bytes (from original file) |
| 192 |
nAPEFrameDataBytes, // the number of bytes of APE frame data |
| 193 |
nAPEFrameDataBytesHigh, // the high order number of APE frame data bytes |
| 194 |
nTerminatingDataBytes : longword;// the terminating data of the file (not including tag data) |
| 195 |
cFileMD5 : array[0..15] of Byte; // the MD5 hash of the file (see notes for usage... it's a littly tricky) |
| 196 |
end; |
| 197 |
|
| 198 |
{ ********************** Private functions & procedures ********************* } |
| 199 |
|
| 200 |
procedure TMonkey.FResetData; |
| 201 |
begin |
| 202 |
{ Reset data } |
| 203 |
FValid := false; |
| 204 |
FVersion := 0; |
| 205 |
FVersionStr := ''; |
| 206 |
FChannels := 0; |
| 207 |
FSampleRate := 0; |
| 208 |
FBits := 0; |
| 209 |
FPeakLevel := 0; |
| 210 |
FPeakLevelRatio := 0.0; |
| 211 |
FTotalSamples := 0; |
| 212 |
FBitrate := 0.0; |
| 213 |
FDuration := 0.0; |
| 214 |
FCompressionMode := 0; |
| 215 |
FCompressionModeStr := ''; |
| 216 |
FFormatFlags := 0; |
| 217 |
FHasPeakLevel := false; |
| 218 |
FHasSeekElements := false; |
| 219 |
FWavNotStored := false; |
| 220 |
FFileSize := 0; |
| 221 |
FID3v1.ResetData; |
| 222 |
FID3v2.ResetData; |
| 223 |
FAPEtag.ResetData; |
| 224 |
end; |
| 225 |
|
| 226 |
{ ********************** Public functions & procedures ********************** } |
| 227 |
|
| 228 |
constructor TMonkey.Create; |
| 229 |
begin |
| 230 |
{ Create object } |
| 231 |
inherited; |
| 232 |
FID3v1 := TID3v1.Create; |
| 233 |
FID3v2 := TID3v2.Create; |
| 234 |
FAPEtag := TAPEtag.Create; |
| 235 |
FResetData; |
| 236 |
end; |
| 237 |
|
| 238 |
{ --------------------------------------------------------------------------- } |
| 239 |
|
| 240 |
destructor TMonkey.Destroy; |
| 241 |
begin |
| 242 |
{ Destroy object } |
| 243 |
FID3v1.Free; |
| 244 |
FID3v2.Free; |
| 245 |
FAPEtag.Free; |
| 246 |
inherited; |
| 247 |
end; |
| 248 |
|
| 249 |
{ --------------------------------------------------------------------------- } |
| 250 |
|
| 251 |
function TMonkey.ReadFromFile(const FileName: String): Boolean; |
| 252 |
var |
| 253 |
f : TFileStreamEx; |
| 254 |
APE : APE_HEADER; // common header |
| 255 |
APE_OLD : APE_HEADER_OLD; // old header <= 3.97 |
| 256 |
APE_NEW : APE_HEADER_NEW; // new header >= 3.98 |
| 257 |
APE_DESC : APE_DESCRIPTOR; // extra header >= 3.98 |
| 258 |
BlocksPerFrame : integer; |
| 259 |
LoadSuccess : boolean; |
| 260 |
TagSize : integer; |
| 261 |
begin |
| 262 |
Result := FALSE; |
| 263 |
FResetData; |
| 264 |
// load tags first |
| 265 |
FID3v2.ReadFromFile(FileName); |
| 266 |
FID3v1.ReadFromFile(FileName); |
| 267 |
FAPEtag.ReadFromFile(FileName); |
| 268 |
// calculate total tag size |
| 269 |
TagSize := 0; |
| 270 |
if FID3v1.Exists then inc(TagSize, 128); |
| 271 |
if FID3v2.Exists then inc(TagSize, FID3v2.Size); |
| 272 |
if FAPEtag.Exists then inc(TagSize, FAPETag.Size); |
| 273 |
// begin reading data from file |
| 274 |
LoadSuccess := FALSE; |
| 275 |
f:=nil; |
| 276 |
try |
| 277 |
try |
| 278 |
f := TFileStreamEx.create(FileName, fmOpenRead or fmShareDenyWrite); |
| 279 |
FFileSize := f.Size; |
| 280 |
// seek past id3v2-tag |
| 281 |
if FID3v2.Exists then begin |
| 282 |
f.Seek(FID3v2.Size, soFromBeginning); |
| 283 |
end; |
| 284 |
// Read APE Format Header |
| 285 |
fillchar(APE, sizeof(APE), 0); |
| 286 |
if (f.Read(APE, sizeof(APE)) = sizeof(APE)) and ( StrLComp(@APE.cID[0],'MAC ',4)=0) then begin |
| 287 |
FVersion := APE.nVersion; |
| 288 |
Str(FVersion / 1000 : 4 : 2, FVersionStr); |
| 289 |
// Load New Monkey's Audio Header for version >= 3.98 |
| 290 |
if APE.nVersion >= 3980 then begin |
| 291 |
fillchar(APE_DESC, sizeof(APE_DESC), 0); |
| 292 |
if (f.Read(APE_DESC, sizeof(APE_DESC)) = sizeof(APE_DESC)) then begin |
| 293 |
// seek past description header |
| 294 |
if APE_DESC.nDescriptorBytes <> 52 then f.Seek(APE_DESC.nDescriptorBytes - 52, soFromCurrent); |
| 295 |
// load new ape_header |
| 296 |
if APE_DESC.nHeaderBytes > sizeof(APE_NEW) then APE_DESC.nHeaderBytes := sizeof(APE_NEW); |
| 297 |
fillchar(APE_NEW, sizeof(APE_NEW), 0); |
| 298 |
if (longword(f.Read(APE_NEW, APE_DESC.nHeaderBytes)) = APE_DESC.nHeaderBytes ) then begin |
| 299 |
// based on MAC SDK 3.98a1 (APEinfo.h) |
| 300 |
FSampleRate := APE_NEW.nSampleRate; |
| 301 |
FChannels := APE_NEW.nChannels; |
| 302 |
FFormatFlags := APE_NEW.nFormatFlags; |
| 303 |
FBits := APE_NEW.nBitsPerSample; |
| 304 |
FCompressionMode := APE_NEW.nCompressionLevel; |
| 305 |
// calculate total uncompressed samples |
| 306 |
if APE_NEW.nTotalFrames>0 then begin |
| 307 |
FTotalSamples := Int64(APE_NEW.nBlocksPerFrame) * |
| 308 |
Int64(APE_NEW.nTotalFrames-1) + |
| 309 |
Int64(APE_NEW.nFinalFrameBlocks); |
| 310 |
end; |
| 311 |
LoadSuccess := TRUE; |
| 312 |
end; |
| 313 |
end; |
| 314 |
end else begin |
| 315 |
// Old Monkey <= 3.97 |
| 316 |
fillchar(APE_OLD, sizeof(APE_OLD), 0); |
| 317 |
if (f.Read(APE_OLD, sizeof(APE_OLD)) = sizeof(APE_OLD) ) then begin |
| 318 |
FCompressionMode := APE_OLD.nCompressionLevel; |
| 319 |
FSampleRate := APE_OLD.nSampleRate; |
| 320 |
FChannels := APE_OLD.nChannels; |
| 321 |
FFormatFlags := APE_OLD.nFormatFlags; |
| 322 |
FBits := 16; |
| 323 |
if APE_OLD.nFormatFlags and MONKEY_FLAG_8_BIT <>0 then FBits := 8; |
| 324 |
if APE_OLD.nFormatFlags and MONKEY_FLAG_24_BIT <>0 then FBits := 24; |
| 325 |
FHasSeekElements := APE_OLD.nFormatFlags and MONKEY_FLAG_PEAK_LEVEL <>0; |
| 326 |
FWavNotStored := APE_OLD.nFormatFlags and MONKEY_FLAG_SEEK_ELEMENTS <>0; |
| 327 |
FHasPeakLevel := APE_OLD.nFormatFlags and MONKEY_FLAG_WAV_NOT_STORED<>0; |
| 328 |
if FHasPeakLevel then begin |
| 329 |
FPeakLevel := APE_OLD.nInt; |
| 330 |
FPeakLevelRatio := (FPeakLevel / (1 shl FBits) / 2.0) * 100.0; |
| 331 |
end; |
| 332 |
// based on MAC_SDK_397 (APEinfo.cpp) |
| 333 |
if (FVersion >= 3950) then |
| 334 |
BlocksPerFrame := 73728 * 4 |
| 335 |
else if (FVersion >= 3900) or ((FVersion >= 3800) and (APE_OLD.nCompressionLevel = MONKEY_COMPRESSION_EXTRA_HIGH)) then |
| 336 |
BlocksPerFrame := 73728 |
| 337 |
else |
| 338 |
BlocksPerFrame := 9216; |
| 339 |
// calculate total uncompressed samples |
| 340 |
if APE_OLD.nTotalFrames>0 then begin |
| 341 |
FTotalSamples := Int64(APE_OLD.nTotalFrames-1) * |
| 342 |
Int64(BlocksPerFrame) + |
| 343 |
Int64(APE_OLD.nFinalFrameBlocks); |
| 344 |
end; |
| 345 |
LoadSuccess := TRUE; |
| 346 |
end; |
| 347 |
end; |
| 348 |
if LoadSuccess then begin |
| 349 |
// compression profile name |
| 350 |
if ((FCompressionMode mod 1000) = 0) and (FCompressionMode<=6000) then begin |
| 351 |
FCompressionModeStr := MONKEY_COMPRESSION[FCompressionMode div 1000]; |
| 352 |
end else begin |
| 353 |
FCompressionModeStr := IntToStr(FCompressionMode); |
| 354 |
end; |
| 355 |
// length |
| 356 |
if FSampleRate>0 then FDuration := FTotalSamples / FSampleRate; |
| 357 |
// average bitrate |
| 358 |
if FDuration>0 then FBitrate := (FFileSize - Int64(TagSize))*8.0 / (FDuration/1000.0); |
| 359 |
// some extra sanity checks |
| 360 |
FValid := (FBits>0) and (FSampleRate>0) and (FTotalSamples>0) and (FChannels>0); |
| 361 |
Result := FValid; |
| 362 |
end; |
| 363 |
end; |
| 364 |
finally |
| 365 |
f.free; |
| 366 |
end; |
| 367 |
except |
| 368 |
end; |
| 369 |
end; |
| 370 |
|
| 371 |
{ --------------------------------------------------------------------------- } |
| 372 |
|
| 373 |
function TMonkey.FGetRatio: Double; |
| 374 |
begin |
| 375 |
{ Get compression ratio } |
| 376 |
if FValid then |
| 377 |
Result := FFileSize / (FTotalSamples * (FChannels * FBits / 8) + 44) * 100 |
| 378 |
else |
| 379 |
Result := 0; |
| 380 |
end; |
| 381 |
|
| 382 |
{ --------------------------------------------------------------------------- } |
| 383 |
|
| 384 |
function TMonkey.FGetChannelMode: string; |
| 385 |
begin |
| 386 |
if FChannels < Length(MONKEY_MODE) then |
| 387 |
Result:= MONKEY_MODE[FChannels] |
| 388 |
else begin |
| 389 |
Result:= EmptyStr; |
| 390 |
end; |
| 391 |
end; |
| 392 |
|
| 393 |
{ --------------------------------------------------------------------------- } |
| 394 |
|
| 395 |
end. |