| 1 |
{ *************************************************************************** } |
| 2 |
{ } |
| 3 |
{ Audio Tools Library } |
| 4 |
{ Class TWAVPackFile - for manipulating with WAVPack Files } |
| 5 |
{ } |
| 6 |
{ http://mac.sourceforge.net/atl/ } |
| 7 |
{ e-mail: [email protected] } |
| 8 |
{ } |
| 9 |
{ Copyright (c) 2003-2005 by Mattias Dahlberg } |
| 10 |
{ } |
| 11 |
{ Version 1.2 (09 August 2004) by jtclipper } |
| 12 |
{ - updated to support WavPack version 4 files } |
| 13 |
{ - added encoder detection } |
| 14 |
{ } |
| 15 |
{ Version 1.1 (April 2004) by Gambit } |
| 16 |
{ - Added Ratio and Samples property } |
| 17 |
{ } |
| 18 |
{ Version 1.0 (August 2003) } |
| 19 |
{ } |
| 20 |
{ This library is free software; you can redistribute it and/or } |
| 21 |
{ modify it under the terms of the GNU Lesser General Public } |
| 22 |
{ License as published by the Free Software Foundation; either } |
| 23 |
{ version 2.1 of the License, or (at your option) any later version. } |
| 24 |
{ } |
| 25 |
{ This library is distributed in the hope that it will be useful, } |
| 26 |
{ but WITHOUT ANY WARRANTY; without even the implied warranty of } |
| 27 |
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU } |
| 28 |
{ Lesser General Public License for more details. } |
| 29 |
{ } |
| 30 |
{ You should have received a copy of the GNU Lesser General Public } |
| 31 |
{ License along with this library; if not, write to the Free Software } |
| 32 |
{ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA } |
| 33 |
{ } |
| 34 |
{ *************************************************************************** } |
| 35 |
|
| 36 |
unit WAVPackfile; |
| 37 |
|
| 38 |
interface |
| 39 |
|
| 40 |
uses |
| 41 |
Classes, SysUtils, APEtag, DCClassesUtf8; |
| 42 |
|
| 43 |
type |
| 44 |
TWAVPackfile = class(TObject) |
| 45 |
private |
| 46 |
FFileSize: int64; |
| 47 |
FValid: boolean; |
| 48 |
FFormatTag: integer; |
| 49 |
FVersion: integer; |
| 50 |
FChannels: integer; |
| 51 |
FSampleRate: integer; |
| 52 |
FBits: integer; |
| 53 |
FBitrate: double; |
| 54 |
FDuration: double; |
| 55 |
FEncoder: string; |
| 56 |
FAPEtag : TAPEtag; |
| 57 |
FTagSize: integer; |
| 58 |
|
| 59 |
FSamples: Int64; |
| 60 |
FBSamples: Int64; |
| 61 |
|
| 62 |
procedure FResetData; |
| 63 |
function FGetRatio: Double; |
| 64 |
function FGetChannelMode: string; |
| 65 |
|
| 66 |
public |
| 67 |
constructor Create; |
| 68 |
destructor Destroy; override; |
| 69 |
|
| 70 |
function ReadFromFile(const FileName: String): Boolean; |
| 71 |
function _ReadV3( f: TFileStreamEx ): boolean; |
| 72 |
function _ReadV4( f: TFileStreamEx ): boolean; |
| 73 |
|
| 74 |
property FileSize: int64 read FFileSize; |
| 75 |
property Valid: boolean read FValid; |
| 76 |
property FormatTag: integer read FFormatTag; |
| 77 |
property Version: integer read FVersion; |
| 78 |
property Channels: integer read FChannels; |
| 79 |
property ChannelMode: string read FGetChannelMode; |
| 80 |
property SampleRate: integer read FSamplerate; |
| 81 |
property Bits: integer read FBits; |
| 82 |
property Bitrate: double read FBitrate; |
| 83 |
property Duration: double read FDuration; |
| 84 |
property Samples: Int64 read FSamples; |
| 85 |
property BSamples: Int64 read FBSamples; |
| 86 |
property Ratio: Double read FGetRatio; |
| 87 |
property Encoder: string read FEncoder; |
| 88 |
|
| 89 |
property APEtag: TAPEtag read FAPEtag; |
| 90 |
end; |
| 91 |
|
| 92 |
implementation |
| 93 |
|
| 94 |
|
| 95 |
type |
| 96 |
|
| 97 |
wavpack_header3 = record |
| 98 |
ckID: array[0..3] of char; |
| 99 |
ckSize: longword; |
| 100 |
version: word; |
| 101 |
bits: word ; |
| 102 |
flags: word; |
| 103 |
shift: word; |
| 104 |
total_samples: longword; |
| 105 |
crc: longword; |
| 106 |
crc2: longword; |
| 107 |
extension: array[0..3] of char; |
| 108 |
extra_bc: byte; |
| 109 |
extras: array[0..2] of char; |
| 110 |
end; |
| 111 |
|
| 112 |
wavpack_header4 = record |
| 113 |
ckID: array[0..3] of char; |
| 114 |
ckSize: longword; |
| 115 |
version: word; |
| 116 |
track_no: byte; |
| 117 |
index_no: byte; |
| 118 |
total_samples: longword; |
| 119 |
block_index: longword; |
| 120 |
block_samples: longword; |
| 121 |
flags: longword; |
| 122 |
crc: longword; |
| 123 |
end; |
| 124 |
|
| 125 |
fmt_chunk = record |
| 126 |
wformattag: word; |
| 127 |
wchannels: word; |
| 128 |
dwsamplespersec: longword; |
| 129 |
dwavgbytespersec: longword; |
| 130 |
wblockalign: word; |
| 131 |
wbitspersample: word; |
| 132 |
end; |
| 133 |
|
| 134 |
riff_chunk = record |
| 135 |
id: array[0..3] of char; |
| 136 |
size: longword; |
| 137 |
end; |
| 138 |
|
| 139 |
const |
| 140 |
|
| 141 |
//version 3 flags |
| 142 |
MONO_FLAG_v3 = 1; // not stereo |
| 143 |
FAST_FLAG_v3 = 2; // non-adaptive predictor and stereo mode |
| 144 |
// RAW_FLAG_v3 = 4; // raw mode (no .wav header) |
| 145 |
// CALC_NOISE_v3 = 8; // calc noise in lossy mode (no longer stored) |
| 146 |
HIGH_FLAG_v3 = $10; // high quality mode (all modes) |
| 147 |
// BYTES_3_v3 = $20; // files have 3-byte samples |
| 148 |
// OVER_20_v3 = $40; // samples are over 20 bits |
| 149 |
WVC_FLAG_v3 = $80; // create/use .wvc (no longer stored) |
| 150 |
// LOSSY_SHAPE_v3 = $100; // noise shape (lossy mode only) |
| 151 |
// VERY_FAST_FLAG_v3 = $200; // double fast (no longer stored) |
| 152 |
NEW_HIGH_FLAG_v3 = $400; // new high quality mode (lossless only) |
| 153 |
// CANCEL_EXTREME_v3 = $800; // cancel EXTREME_DECORR |
| 154 |
// CROSS_DECORR_v3 = $1000; // decorrelate chans (with EXTREME_DECORR flag) |
| 155 |
// NEW_DECORR_FLAG_v3 = $2000; // new high-mode decorrelator |
| 156 |
// JOINT_STEREO_v3 = $4000; // joint stereo (lossy and high lossless) |
| 157 |
EXTREME_DECORR_v3 = $8000; // extra decorrelation (+ enables other flags) |
| 158 |
|
| 159 |
sample_rates: array[0..14] of integer = ( 6000, 8000, 9600, 11025, 12000, 16000, 22050, |
| 160 |
24000, 32000, 44100, 48000, 64000, 88200, 96000, 192000 ); |
| 161 |
|
| 162 |
{ --------------------------------------------------------------------------- } |
| 163 |
|
| 164 |
procedure TWAVPackfile.FResetData; |
| 165 |
begin |
| 166 |
FFileSize := 0; |
| 167 |
FTagSize := 0; |
| 168 |
FValid := false; |
| 169 |
FFormatTag := 0; |
| 170 |
FChannels := 0; |
| 171 |
FSampleRate := 0; |
| 172 |
FBits := 0; |
| 173 |
FBitrate := 0; |
| 174 |
FDuration := 0; |
| 175 |
FVersion := 0; |
| 176 |
FEncoder := ''; |
| 177 |
FSamples := 0; |
| 178 |
FBSamples := 0; |
| 179 |
FAPEtag.ResetData; |
| 180 |
end; |
| 181 |
|
| 182 |
{ --------------------------------------------------------------------------- } |
| 183 |
|
| 184 |
constructor TWAVPackfile.Create; |
| 185 |
begin |
| 186 |
inherited; |
| 187 |
FAPEtag := TAPEtag.Create; |
| 188 |
FResetData; |
| 189 |
end; |
| 190 |
|
| 191 |
destructor TWAVPackfile.Destroy; |
| 192 |
begin |
| 193 |
FAPEtag.Free; |
| 194 |
inherited; |
| 195 |
end; |
| 196 |
|
| 197 |
{ --------------------------------------------------------------------------- } |
| 198 |
|
| 199 |
function TWAVPackfile.FGetChannelMode: string; |
| 200 |
begin |
| 201 |
case FChannels of |
| 202 |
1: result := 'Mono'; |
| 203 |
2: result := 'Stereo'; |
| 204 |
else result := 'Surround'; |
| 205 |
end; |
| 206 |
end; |
| 207 |
|
| 208 |
{ --------------------------------------------------------------------------- } |
| 209 |
|
| 210 |
function TWAVPackfile.ReadFromFile(const FileName: String): Boolean; |
| 211 |
var |
| 212 |
f: TFileStreamEx; |
| 213 |
marker: array[0..3] of char; |
| 214 |
begin |
| 215 |
|
| 216 |
FResetData; |
| 217 |
FAPEtag.ReadFromFile(FileName); |
| 218 |
FTagSize := FAPEtag.Size; |
| 219 |
|
| 220 |
try |
| 221 |
f := TFileStreamEx.create(FileName, fmOpenRead or fmShareDenyWrite); |
| 222 |
FFileSize := f.Size; |
| 223 |
//read first bytes |
| 224 |
FillChar( marker, SizeOf( marker ), 0 ); |
| 225 |
f.Read( marker, SizeOf( marker) ); |
| 226 |
f.Seek( 0, soFromBeginning ); |
| 227 |
|
| 228 |
if marker = 'RIFF' then begin |
| 229 |
result := _ReadV3( f ); |
| 230 |
end else if marker = 'wvpk' then begin |
| 231 |
result := _ReadV4( f ); |
| 232 |
end else begin |
| 233 |
result := False; |
| 234 |
end; |
| 235 |
|
| 236 |
finally |
| 237 |
FreeAndNil( f ); |
| 238 |
end; |
| 239 |
end; |
| 240 |
|
| 241 |
{ --------------------------------------------------------------------------- } |
| 242 |
|
| 243 |
function TWAVPackfile._ReadV4( f: TFileStreamEx ): boolean; |
| 244 |
var |
| 245 |
wvh4: wavpack_header4; |
| 246 |
EncBuf : array[1..4096] of Byte; |
| 247 |
tempo : Integer; |
| 248 |
encoderbyte: Byte; |
| 249 |
begin |
| 250 |
result := false; |
| 251 |
FillChar( wvh4, SizeOf(wvh4) ,0); |
| 252 |
f.Read( wvh4, SizeOf(wvh4) ); |
| 253 |
if wvh4.ckID = 'wvpk' then // wavpack header found |
| 254 |
begin |
| 255 |
Result := true; |
| 256 |
FValid := true; |
| 257 |
FVersion := wvh4.version shr 8; |
| 258 |
FChannels := 2 - (wvh4.flags and 4); // mono flag |
| 259 |
|
| 260 |
FBits := ((wvh4.flags and 3) * 16); // bytes stored flag |
| 261 |
FSamples := wvh4.total_samples; |
| 262 |
FBSamples := wvh4.block_samples; |
| 263 |
FSampleRate := (wvh4.flags and ($1F shl 23)) shr 23; |
| 264 |
if (FSampleRate > 14) or (FSampleRate < 0) then |
| 265 |
begin |
| 266 |
FSampleRate := 44100; |
| 267 |
end |
| 268 |
else |
| 269 |
begin |
| 270 |
FSampleRate := sample_rates[ FSampleRate ]; |
| 271 |
end; |
| 272 |
|
| 273 |
if ((wvh4.flags and 8) = 8) then // hybrid flag |
| 274 |
begin |
| 275 |
FEncoder := 'hybrid lossy'; |
| 276 |
end |
| 277 |
else |
| 278 |
begin //if ((wvh4.flags and 2) = 2) then begin // lossless flag |
| 279 |
FEncoder := 'lossless'; |
| 280 |
end; |
| 281 |
|
| 282 |
{ |
| 283 |
if ((wvh4.flags and $20) > 0) then // MODE_HIGH |
| 284 |
begin |
| 285 |
FEncoder := FEncoder + ' (high)'; |
| 286 |
end |
| 287 |
else if ((wvh4.flags and $40) > 0) then // MODE_FAST |
| 288 |
begin |
| 289 |
FEncoder := FEncoder + ' (fast)'; |
| 290 |
end; |
| 291 |
} |
| 292 |
|
| 293 |
FDuration := wvh4.total_samples / FSampleRate; |
| 294 |
if FDuration > 0 then |
| 295 |
FBitrate := (FFileSize - int64( FTagSize ) ) * 8 / (FSamples / FSampleRate) / 1000; |
| 296 |
|
| 297 |
FillChar(EncBuf, SizeOf(EncBuf), 0); |
| 298 |
f.Read(EncBuf, SizeOf(EncBuf)); |
| 299 |
for tempo := 1 to 4094 do |
| 300 |
begin |
| 301 |
If EncBuf[tempo] = $65 then if |
| 302 |
EncBuf[tempo + 1] = $02 then |
| 303 |
begin |
| 304 |
encoderbyte := EncBuf[tempo + 2]; |
| 305 |
if encoderbyte = 8 then |
| 306 |
FEncoder := FEncoder + ' (high)' |
| 307 |
else if encoderbyte = 0 then |
| 308 |
FEncoder := FEncoder + ' (normal)' |
| 309 |
else if encoderbyte = 2 then |
| 310 |
FEncoder := FEncoder + ' (fast)' |
| 311 |
else if encoderbyte = 6 then |
| 312 |
FEncoder := FEncoder + ' (very fast)'; |
| 313 |
Break; |
| 314 |
end; |
| 315 |
end; |
| 316 |
|
| 317 |
end; |
| 318 |
end; |
| 319 |
|
| 320 |
{ --------------------------------------------------------------------------- } |
| 321 |
|
| 322 |
function TWAVPackfile._ReadV3( f: TFileStreamEx ): boolean; |
| 323 |
var |
| 324 |
chunk: riff_chunk; |
| 325 |
wavchunk: array[0..3] of char; |
| 326 |
fmt: fmt_chunk; |
| 327 |
hasfmt: boolean; |
| 328 |
fpos: int64; |
| 329 |
wvh3: wavpack_header3; |
| 330 |
begin |
| 331 |
|
| 332 |
result := false; |
| 333 |
hasfmt := false; |
| 334 |
|
| 335 |
// read and evaluate header |
| 336 |
FillChar( chunk, sizeof(chunk), 0 ); |
| 337 |
if (f.Read(chunk, sizeof(chunk)) <> SizeOf( chunk )) or |
| 338 |
(f.Read(wavchunk, sizeof(wavchunk)) <> SizeOf(wavchunk)) or (wavchunk <> 'WAVE') then exit; |
| 339 |
|
| 340 |
// start looking for chunks |
| 341 |
FillChar( chunk, SizeOf(chunk), 0 ); |
| 342 |
while (f.Position < f.Size) do begin |
| 343 |
|
| 344 |
if (f.read(chunk, sizeof(chunk)) < sizeof(chunk)) or (chunk.size <= 0) then break; |
| 345 |
fpos := f.Position; |
| 346 |
|
| 347 |
if chunk.id = 'fmt ' then begin // Format chunk found read it |
| 348 |
if (chunk.size >= sizeof(fmt)) and (f.Read(fmt, sizeof(fmt)) = sizeof(fmt)) then begin |
| 349 |
hasfmt := true; |
| 350 |
result := True; |
| 351 |
FValid := true; |
| 352 |
FFormatTag := fmt.wformattag; |
| 353 |
FChannels := fmt.wchannels; |
| 354 |
FSampleRate := fmt.dwsamplespersec; |
| 355 |
FBits := fmt.wbitspersample; |
| 356 |
FBitrate := fmt.dwavgbytespersec / 125.0; // 125 = 1/8*1000 |
| 357 |
end else begin |
| 358 |
break; |
| 359 |
end; |
| 360 |
end else if (chunk.id = 'data') and hasfmt then begin |
| 361 |
FillChar( wvh3, SizeOf(wvh3) ,0); |
| 362 |
f.Read( wvh3, SizeOf(wvh3) ); |
| 363 |
if wvh3.ckID = 'wvpk' then begin // wavpack header found |
| 364 |
result := true; |
| 365 |
FValid := true; |
| 366 |
FVersion := wvh3.version; |
| 367 |
FChannels := 2 - (wvh3.flags and 1); // mono flag |
| 368 |
FSamples := wvh3.total_samples; |
| 369 |
|
| 370 |
// Encoder guess |
| 371 |
if wvh3.bits > 0 then begin |
| 372 |
if (wvh3.flags and NEW_HIGH_FLAG_v3) > 0 then begin |
| 373 |
FEncoder := 'hybrid'; |
| 374 |
if (wvh3.flags and WVC_FLAG_v3) > 0 then begin |
| 375 |
FEncoder := FEncoder + ' lossless'; |
| 376 |
end else begin |
| 377 |
FEncoder := FEncoder + ' lossy'; |
| 378 |
end; |
| 379 |
if (wvh3.flags and EXTREME_DECORR_v3) > 0 then FEncoder := FEncoder + ' (high)'; |
| 380 |
end else if (wvh3.flags and (HIGH_FLAG_v3 or FAST_FLAG_v3)) = 0 then begin |
| 381 |
FEncoder := IntToStr( wvh3.bits + 3 ) + '-bit lossy'; |
| 382 |
end else begin |
| 383 |
FEncoder := IntToStr( wvh3.bits + 3 ) + '-bit lossy'; |
| 384 |
if (wvh3.flags and HIGH_FLAG_v3) > 0 then begin |
| 385 |
FEncoder := FEncoder + ' high'; |
| 386 |
end else begin |
| 387 |
FEncoder := FEncoder + ' fast'; |
| 388 |
end |
| 389 |
end; |
| 390 |
end else begin |
| 391 |
if (wvh3.flags and HIGH_FLAG_v3) = 0 then begin |
| 392 |
FEncoder := 'lossless (fast mode)'; |
| 393 |
end else if (wvh3.flags and EXTREME_DECORR_v3) > 0 then begin |
| 394 |
FEncoder := 'lossless (high mode)'; |
| 395 |
end else begin |
| 396 |
FEncoder := 'lossless'; |
| 397 |
end; |
| 398 |
end; |
| 399 |
|
| 400 |
if FSampleRate <= 0 then FSampleRate := 44100; |
| 401 |
FDuration := wvh3.total_samples / FSampleRate; |
| 402 |
if FDuration > 0 then FBitrate := 8.0*(FFileSize - int64( FTagSize ) - int64(wvh3.ckSize))/(FDuration*1000.0); |
| 403 |
end; |
| 404 |
break; |
| 405 |
end else begin // not a wv file |
| 406 |
break; |
| 407 |
end; |
| 408 |
f.seek( fpos + chunk.size, soFromBeginning ); |
| 409 |
end; // while |
| 410 |
|
| 411 |
end; |
| 412 |
|
| 413 |
{ --------------------------------------------------------------------------- } |
| 414 |
|
| 415 |
function TWAVPackfile.FGetRatio: Double; |
| 416 |
begin |
| 417 |
{ Get compression ratio } |
| 418 |
if FValid then |
| 419 |
Result := FFileSize / (FSamples * (FChannels * FBits / 8) + 44) * 100 |
| 420 |
else |
| 421 |
Result := 0; |
| 422 |
end; |
| 423 |
|
| 424 |
{ --------------------------------------------------------------------------- } |
| 425 |
|
| 426 |
end. |