| 1 |
{ *************************************************************************** } |
| 2 |
{ } |
| 3 |
{ Audio Tools Library } |
| 4 |
{ Class TWAVfile - for manipulating with WAV files } |
| 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.5 (April 2005) by Gambit } |
| 13 |
{ - updated to unicode file access } |
| 14 |
{ } |
| 15 |
{ Version 1.44 (23 March 2005) by Gambit } |
| 16 |
{ - multichannel support } |
| 17 |
{ } |
| 18 |
{ Version 1.43 (27 August 2004) by Gambit } |
| 19 |
{ - added procedures: TrimFromEnd, TrimFromBeginning and FindSilence } |
| 20 |
{ - removed WriteNewLength procedure (replaced with TrimFromEnd) } |
| 21 |
{ - fixed some FormatSize/HeaderSize/SampleNumber related bugs } |
| 22 |
{ } |
| 23 |
{ Version 1.32 (05 June 2004) by Gambit } |
| 24 |
{ - WriteNewLength now properly truncates the file } |
| 25 |
{ } |
| 26 |
{ Version 1.31 (April 2004) by Gambit } |
| 27 |
{ - Added Ratio property } |
| 28 |
{ } |
| 29 |
{ Version 1.3 (22 February 2004) by Gambit } |
| 30 |
{ - SampleNumber is now read correctly } |
| 31 |
{ - added procedure to change the duration (SampleNumber and FileSize) } |
| 32 |
{ of the wav file (can be used for example to trim off the encoder } |
| 33 |
{ padding from decoded mp3 files) } |
| 34 |
{ } |
| 35 |
{ Version 1.2 (14 January 2002) } |
| 36 |
{ - Fixed bug with calculating of duration } |
| 37 |
{ - Some class properties added/changed } |
| 38 |
{ } |
| 39 |
{ Version 1.1 (9 October 2001) } |
| 40 |
{ - Fixed bug with WAV header detection } |
| 41 |
{ } |
| 42 |
{ Version 1.0 (31 July 2001) } |
| 43 |
{ - Info: channel mode, sample rate, bits per sample, file size, duration } |
| 44 |
{ } |
| 45 |
{ This library is free software; you can redistribute it and/or } |
| 46 |
{ modify it under the terms of the GNU Lesser General Public } |
| 47 |
{ License as published by the Free Software Foundation; either } |
| 48 |
{ version 2.1 of the License, or (at your option) any later version. } |
| 49 |
{ } |
| 50 |
{ This library is distributed in the hope that it will be useful, } |
| 51 |
{ but WITHOUT ANY WARRANTY; without even the implied warranty of } |
| 52 |
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU } |
| 53 |
{ Lesser General Public License for more details. } |
| 54 |
{ } |
| 55 |
{ You should have received a copy of the GNU Lesser General Public } |
| 56 |
{ License along with this library; if not, write to the Free Software } |
| 57 |
{ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA } |
| 58 |
{ } |
| 59 |
{ *************************************************************************** } |
| 60 |
|
| 61 |
|
| 62 |
unit WAVfile; |
| 63 |
|
| 64 |
interface |
| 65 |
|
| 66 |
uses |
| 67 |
Classes, SysUtils, DCClassesUtf8; |
| 68 |
|
| 69 |
const |
| 70 |
{ Format type names } |
| 71 |
WAV_FORMAT_UNKNOWN = 'Unknown'; |
| 72 |
WAV_FORMAT_PCM = 'Windows PCM'; |
| 73 |
WAV_FORMAT_ADPCM = 'Microsoft ADPCM'; |
| 74 |
WAV_FORMAT_ALAW = 'A-LAW'; |
| 75 |
WAV_FORMAT_MULAW = 'MU-LAW'; |
| 76 |
WAV_FORMAT_DVI_IMA_ADPCM = 'DVI/IMA ADPCM'; |
| 77 |
WAV_FORMAT_MP3 = 'MPEG Layer III'; |
| 78 |
|
| 79 |
{ Channel mode names } |
| 80 |
WAV_MODE: array [0..3] of string = ('Unknown', 'Mono', 'Stereo', 'Multichannel'); |
| 81 |
|
| 82 |
type |
| 83 |
{ Class TWAVfile } |
| 84 |
TWAVfile = class(TObject) |
| 85 |
private |
| 86 |
{ Private declarations } |
| 87 |
FValid: Boolean; |
| 88 |
FFormatSize: Cardinal; |
| 89 |
FFormatID: Word; |
| 90 |
FChannelNumber: Byte; |
| 91 |
FSampleRate: Cardinal; |
| 92 |
FBytesPerSecond: Cardinal; |
| 93 |
FBlockAlign: Word; |
| 94 |
FBitsPerSample: Byte; |
| 95 |
FSampleNumber: Cardinal; |
| 96 |
FHeaderSize: Cardinal; |
| 97 |
FFileSize: Cardinal; |
| 98 |
FFileName: String; |
| 99 |
FAmountTrimBegin: Cardinal; |
| 100 |
FAmountTrimEnd: Cardinal; |
| 101 |
FBitrate: Double; |
| 102 |
procedure FResetData; |
| 103 |
function FGetFormat: string; |
| 104 |
function FGetChannelMode: string; |
| 105 |
function FGetDuration: Double; |
| 106 |
function FGetRatio: Double; |
| 107 |
public |
| 108 |
{ Public declarations } |
| 109 |
constructor Create; { Create object } |
| 110 |
function ReadFromFile(const FileName: String): Boolean; { Load header } |
| 111 |
property Valid: Boolean read FValid; { True if header valid } |
| 112 |
property FormatSize: Cardinal read FFormatSize; |
| 113 |
property FormatID: Word read FFormatID; { Format type code } |
| 114 |
property Format: string read FGetFormat; { Format type name } |
| 115 |
property ChannelNumber: Byte read FChannelNumber; { Number of channels } |
| 116 |
property ChannelMode: string read FGetChannelMode; { Channel mode name } |
| 117 |
property SampleRate: Cardinal read FSampleRate; { Sample rate (hz) } |
| 118 |
property BytesPerSecond: Cardinal read FBytesPerSecond; { Bytes/second } |
| 119 |
property BlockAlign: Word read FBlockAlign; { Block alignment } |
| 120 |
property BitsPerSample: Byte read FBitsPerSample; { Bits/sample } |
| 121 |
property HeaderSize: Cardinal read FHeaderSize; { Header size (bytes) } |
| 122 |
property FileSize: Cardinal read FFileSize; { File size (bytes) } |
| 123 |
property Duration: Double read FGetDuration; { Duration (seconds) } |
| 124 |
property SampleNumber: Cardinal read FSampleNumber; |
| 125 |
procedure TrimFromBeginning(const Samples: Cardinal); |
| 126 |
procedure TrimFromEnd(const Samples: Cardinal); |
| 127 |
procedure FindSilence(const FromBeginning, FromEnd: Boolean); |
| 128 |
property Ratio: Double read FGetRatio; { Compression ratio (%) } |
| 129 |
property AmountTrimBegin: Cardinal read FAmountTrimBegin; |
| 130 |
property AmountTrimEnd: Cardinal read FAmountTrimEnd; |
| 131 |
property Bitrate: Double read FBitrate; |
| 132 |
end; |
| 133 |
|
| 134 |
implementation |
| 135 |
|
| 136 |
const |
| 137 |
DATA_CHUNK = 'data'; { Data chunk ID } |
| 138 |
|
| 139 |
type |
| 140 |
{ WAV file header data } |
| 141 |
WAVRecord = record |
| 142 |
{ RIFF file header } |
| 143 |
RIFFHeader: array [1..4] of Char; { Must be "RIFF" } |
| 144 |
FileSize: Integer; { Must be "RealFileSize - 8" } |
| 145 |
WAVEHeader: array [1..4] of Char; { Must be "WAVE" } |
| 146 |
{ Format information } |
| 147 |
FormatHeader: array [1..4] of Char; { Must be "fmt " } |
| 148 |
FormatSize: Cardinal; { Format size } |
| 149 |
FormatID: Word; { Format type code } |
| 150 |
ChannelNumber: Word; { Number of channels } |
| 151 |
SampleRate: Integer; { Sample rate (hz) } |
| 152 |
BytesPerSecond: Integer; { Bytes/second } |
| 153 |
BlockAlign: Word; { Block alignment } |
| 154 |
BitsPerSample: Word; { Bits/sample } |
| 155 |
DataHeader: array [1..4] of Char; { Can be "data" } |
| 156 |
SampleNumber: Cardinal; { Number of samples (optional) } |
| 157 |
end; |
| 158 |
|
| 159 |
{ ********************* Auxiliary functions & procedures ******************** } |
| 160 |
|
| 161 |
function ReadWAV(const FileName: String; var WAVData: WAVRecord): Boolean; |
| 162 |
var |
| 163 |
SourceFile: TFileStreamEx; |
| 164 |
begin |
| 165 |
try |
| 166 |
Result := true; |
| 167 |
{ Set read-access and open file } |
| 168 |
SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite); |
| 169 |
{ Read header } |
| 170 |
SourceFile.Read(WAVData, 36); |
| 171 |
|
| 172 |
{ Read number of samples } |
| 173 |
if SourceFile.Size > (WAVData.FormatSize + 24) then |
| 174 |
begin |
| 175 |
SourceFile.Seek(WAVData.FormatSize + 24, soFromBeginning); |
| 176 |
SourceFile.Read(WAVData.SampleNumber, 4); |
| 177 |
end; |
| 178 |
|
| 179 |
SourceFile.Free; |
| 180 |
except |
| 181 |
{ Error } |
| 182 |
Result := false; |
| 183 |
end; |
| 184 |
end; |
| 185 |
|
| 186 |
{ --------------------------------------------------------------------------- } |
| 187 |
|
| 188 |
function HeaderIsValid(const WAVData: WAVRecord): Boolean; |
| 189 |
begin |
| 190 |
Result := True; |
| 191 |
{ Header validation } |
| 192 |
if WAVData.RIFFHeader <> 'RIFF' then Result := False; |
| 193 |
if WAVData.WAVEHeader <> 'WAVE' then Result := False; |
| 194 |
if WAVData.FormatHeader <> 'fmt ' then Result := False; |
| 195 |
if WAVData.ChannelNumber = 0 then Result := False; |
| 196 |
end; |
| 197 |
|
| 198 |
{ ********************** Private functions & procedures ********************* } |
| 199 |
|
| 200 |
procedure TWAVfile.FResetData; |
| 201 |
begin |
| 202 |
{ Reset all data } |
| 203 |
FValid := false; |
| 204 |
FFormatSize := 0; |
| 205 |
FFormatID := 0; |
| 206 |
FChannelNumber := 0; |
| 207 |
FSampleRate := 0; |
| 208 |
FBytesPerSecond := 0; |
| 209 |
FBlockAlign := 0; |
| 210 |
FBitsPerSample := 0; |
| 211 |
FSampleNumber := 0; |
| 212 |
FHeaderSize := 0; |
| 213 |
FFileSize := 0; |
| 214 |
FFileName := ''; |
| 215 |
FAmountTrimBegin := 0; |
| 216 |
FAmountTrimEnd := 0; |
| 217 |
FBitrate := 0; |
| 218 |
end; |
| 219 |
|
| 220 |
{ --------------------------------------------------------------------------- } |
| 221 |
|
| 222 |
function TWAVfile.FGetFormat: string; |
| 223 |
begin |
| 224 |
{ Get format type name } |
| 225 |
case FFormatID of |
| 226 |
1: Result := WAV_FORMAT_PCM; |
| 227 |
2: Result := WAV_FORMAT_ADPCM; |
| 228 |
6: Result := WAV_FORMAT_ALAW; |
| 229 |
7: Result := WAV_FORMAT_MULAW; |
| 230 |
17: Result := WAV_FORMAT_DVI_IMA_ADPCM; |
| 231 |
85: Result := WAV_FORMAT_MP3; |
| 232 |
else |
| 233 |
Result := ''; |
| 234 |
end; |
| 235 |
end; |
| 236 |
|
| 237 |
{ --------------------------------------------------------------------------- } |
| 238 |
|
| 239 |
function TWAVfile.FGetChannelMode: string; |
| 240 |
begin |
| 241 |
{ Get channel mode name } //multichannel |
| 242 |
if FChannelNumber > 2 then Result := WAV_MODE[3] else |
| 243 |
Result := WAV_MODE[FChannelNumber]; |
| 244 |
end; |
| 245 |
|
| 246 |
{ --------------------------------------------------------------------------- } |
| 247 |
|
| 248 |
function TWAVfile.FGetDuration: Double; |
| 249 |
begin |
| 250 |
{ Get duration } |
| 251 |
Result := 0; |
| 252 |
if FValid then |
| 253 |
begin |
| 254 |
if (FSampleNumber = 0) and (FBytesPerSecond > 0) then |
| 255 |
Result := (FFileSize - FHeaderSize) / FBytesPerSecond; |
| 256 |
if (FSampleNumber > 0) and (FSampleRate > 0) then |
| 257 |
Result := FSampleNumber / FSampleRate; |
| 258 |
end; |
| 259 |
end; |
| 260 |
|
| 261 |
{ ********************** Public functions & procedures ********************** } |
| 262 |
|
| 263 |
constructor TWAVfile.Create; |
| 264 |
begin |
| 265 |
{ Create object } |
| 266 |
inherited; |
| 267 |
FResetData; |
| 268 |
end; |
| 269 |
|
| 270 |
{ --------------------------------------------------------------------------- } |
| 271 |
|
| 272 |
function TWAVfile.ReadFromFile(const FileName: String): Boolean; |
| 273 |
var |
| 274 |
WAVData: WAVRecord; |
| 275 |
begin |
| 276 |
{ Reset and load header data from file to variable } |
| 277 |
FResetData; |
| 278 |
FillChar(WAVData, SizeOf(WAVData), 0); |
| 279 |
Result := ReadWAV(FileName, WAVData); |
| 280 |
{ Process data if loaded and header valid } |
| 281 |
if (Result) and (HeaderIsValid(WAVData)) then |
| 282 |
begin |
| 283 |
FValid := true; |
| 284 |
{ Fill properties with header data } |
| 285 |
FFormatSize := WAVData.FormatSize; |
| 286 |
FFormatID := WAVData.FormatID; |
| 287 |
FChannelNumber := WAVData.ChannelNumber; |
| 288 |
FSampleRate := WAVData.SampleRate; |
| 289 |
FBytesPerSecond := WAVData.BytesPerSecond; |
| 290 |
FBlockAlign := WAVData.BlockAlign; |
| 291 |
FBitsPerSample := WAVData.BitsPerSample; |
| 292 |
FSampleNumber := WAVData.SampleNumber div FBlockAlign; |
| 293 |
if WAVData.DataHeader = DATA_CHUNK then FHeaderSize := 44 |
| 294 |
else FHeaderSize := WAVData.FormatSize + 28; |
| 295 |
FFileSize := WAVData.FileSize + 8; |
| 296 |
if FHeaderSize > FFileSize then FHeaderSize := FFileSize; |
| 297 |
FFileName := FileName; |
| 298 |
FBitrate := FBytesPerSecond * 8 / 1000; |
| 299 |
end; |
| 300 |
end; |
| 301 |
|
| 302 |
{ --------------------------------------------------------------------------- } |
| 303 |
|
| 304 |
function TWAVfile.FGetRatio: Double; |
| 305 |
begin |
| 306 |
{ Get compression ratio } |
| 307 |
if FValid then |
| 308 |
if FSampleNumber = 0 then |
| 309 |
Result := FFileSize / ((FFileSize - FHeaderSize) / FBytesPerSecond * FSampleRate * (FChannelNumber * FBitsPerSample / 8) + 44) * 100 |
| 310 |
else |
| 311 |
Result := FFileSize / (FSampleNumber * (FChannelNumber * FBitsPerSample / 8) + 44) * 100 |
| 312 |
else |
| 313 |
Result := 0; |
| 314 |
end; |
| 315 |
|
| 316 |
{ --------------------------------------------------------------------------- } |
| 317 |
|
| 318 |
procedure TWAVfile.TrimFromBeginning(const Samples: Cardinal); |
| 319 |
var |
| 320 |
SourceFile: TFileStreamEx; |
| 321 |
NewData, NewSamples, EraseOldData, NewFormatSize : Cardinal; |
| 322 |
begin |
| 323 |
try |
| 324 |
// blah, blah... should be self explanatory what happens here... |
| 325 |
SourceFile := TFileStreamEx.Create(FFileName, fmOpenReadWrite or fmShareDenyWrite); |
| 326 |
|
| 327 |
SourceFile.Seek(16, soFromBeginning); |
| 328 |
NewFormatSize := (Samples * FBlockAlign) + FFormatSize; |
| 329 |
SourceFile.Write(NewFormatSize, SizeOf(NewFormatSize)); |
| 330 |
|
| 331 |
SourceFile.Seek(FHeaderSize - 8, soFromBeginning); |
| 332 |
EraseOldData := 0; |
| 333 |
SourceFile.Write(EraseOldData, SizeOf(EraseOldData)); |
| 334 |
|
| 335 |
SourceFile.Seek(FHeaderSize + (Samples * FBlockAlign) - 8, soFromBeginning); |
| 336 |
NewData := 1635017060; // 'data' |
| 337 |
SourceFile.Write(NewData, SizeOf(NewData)); |
| 338 |
NewSamples := (FSampleNumber - Samples) * FBlockAlign; |
| 339 |
SourceFile.Write(NewSamples, SizeOf(NewSamples)); |
| 340 |
|
| 341 |
FFormatSize := NewFormatSize; |
| 342 |
FSampleNumber := FSampleNumber - Samples; |
| 343 |
FHeaderSize := FFormatSize + 28; |
| 344 |
|
| 345 |
SourceFile.Free; |
| 346 |
except |
| 347 |
{ Error } |
| 348 |
end; |
| 349 |
end; |
| 350 |
|
| 351 |
{ --------------------------------------------------------------------------- } |
| 352 |
|
| 353 |
procedure TWAVfile.TrimFromEnd(const Samples: Cardinal); |
| 354 |
var |
| 355 |
SourceFile: TFileStreamEx; |
| 356 |
NewSamples, NewSize : Cardinal; |
| 357 |
begin |
| 358 |
try |
| 359 |
SourceFile := TFileStreamEx.Create(FFileName, fmOpenReadWrite or fmShareDenyWrite); |
| 360 |
|
| 361 |
SourceFile.Seek(4, soFromBeginning); |
| 362 |
|
| 363 |
NewSamples := (FSampleNumber - Samples) * FBlockAlign; |
| 364 |
NewSize := NewSamples + FHeaderSize - 8; |
| 365 |
|
| 366 |
SourceFile.Write(NewSize, SizeOf(NewSize)); |
| 367 |
|
| 368 |
SourceFile.Seek(FHeaderSize - 4, soFromBeginning); |
| 369 |
SourceFile.Write(NewSamples, SizeOf(NewSamples)); |
| 370 |
|
| 371 |
SourceFile.Size := NewSamples + FHeaderSize; |
| 372 |
|
| 373 |
FSampleNumber := FSampleNumber - Samples; |
| 374 |
FFileSize := NewSamples + FHeaderSize; |
| 375 |
|
| 376 |
SourceFile.Free; |
| 377 |
except |
| 378 |
{ Error } |
| 379 |
end; |
| 380 |
end; |
| 381 |
|
| 382 |
{ --------------------------------------------------------------------------- } |
| 383 |
|
| 384 |
procedure TWAVfile.FindSilence(const FromBeginning, FromEnd: Boolean); |
| 385 |
var |
| 386 |
SourceFile: TFileStreamEx; |
| 387 |
ReadSample : Integer; |
| 388 |
AmountBegin, AmountEnd : Cardinal; |
| 389 |
begin |
| 390 |
try |
| 391 |
SourceFile := TFileStreamEx.Create(FFileName, fmOpenRead or fmShareDenyWrite); |
| 392 |
|
| 393 |
if FromBeginning then |
| 394 |
begin |
| 395 |
AmountBegin := 0; |
| 396 |
ReadSample := 0; |
| 397 |
SourceFile.Seek(FHeaderSize, soFromBeginning); |
| 398 |
// this assumes 16bit stereo |
| 399 |
repeat |
| 400 |
SourceFile.Read(ReadSample, SizeOf(ReadSample)); |
| 401 |
if ReadSample = 0 then |
| 402 |
Inc(AmountBegin); |
| 403 |
until (ReadSample <> 0) or (SourceFile.Position >= SourceFile.Size); |
| 404 |
FAmountTrimBegin := AmountBegin; |
| 405 |
end; |
| 406 |
|
| 407 |
if FromEnd then |
| 408 |
begin |
| 409 |
AmountEnd := 0; |
| 410 |
ReadSample := 0; |
| 411 |
repeat |
| 412 |
// this assumes 16bit stereo |
| 413 |
SourceFile.Seek(FFileSize - ((AmountEnd + 1) * 4), soFromBeginning); |
| 414 |
SourceFile.Read(ReadSample, SizeOf(ReadSample)); |
| 415 |
if ReadSample = 0 then |
| 416 |
Inc(AmountEnd); |
| 417 |
until ReadSample <> 0; |
| 418 |
FAmountTrimEnd := AmountEnd; |
| 419 |
end; |
| 420 |
|
| 421 |
SourceFile.Free; |
| 422 |
except |
| 423 |
{ Error } |
| 424 |
end; |
| 425 |
end; |
| 426 |
|
| 427 |
{ --------------------------------------------------------------------------- } |
| 428 |
|
| 429 |
end. |