| 1 | { *************************************************************************** }
|
|---|
| 2 | { }
|
|---|
| 3 | { Audio Tools Library (Freeware) }
|
|---|
| 4 | { Class TWAVFile - for extracting information from WAV file header }
|
|---|
| 5 | { }
|
|---|
| 6 | { Copyright (c) 2001 by Jurgen Faul }
|
|---|
| 7 | { E-mail: jfaul@gmx.de }
|
|---|
| 8 | { http://jfaul.de/atl }
|
|---|
| 9 | { }
|
|---|
| 10 | { Version 1.0 (31th July 2001) }
|
|---|
| 11 | { - Info: channel mode, sample rate, bits per sample, file size, duration }
|
|---|
| 12 | { }
|
|---|
| 13 | { *************************************************************************** }
|
|---|
| 14 |
|
|---|
| 15 | unit WavFile;
|
|---|
| 16 |
|
|---|
| 17 | interface
|
|---|
| 18 |
|
|---|
| 19 | uses
|
|---|
| 20 | Classes, SysUtils;
|
|---|
| 21 |
|
|---|
| 22 | const
|
|---|
| 23 | { Used with ChannelMode property }
|
|---|
| 24 | CHANNEL_MODE_MONO = 1; { Index for mono mode }
|
|---|
| 25 | CHANNEL_MODE_STEREO = 2; { Index for stereo mode }
|
|---|
| 26 |
|
|---|
| 27 | { Channel mode names }
|
|---|
| 28 | CHANNEL_MODE: array [0..2] of string = ('Unknown', 'Mono', 'Stereo');
|
|---|
| 29 |
|
|---|
| 30 | type
|
|---|
| 31 | { Real structure of WAV file header }
|
|---|
| 32 | TWAVRecord = record
|
|---|
| 33 | { RIFF file header }
|
|---|
| 34 | RIFFHeader: array [1..4] of Char; { Must be "RIFF" }
|
|---|
| 35 | FileSize: Integer; { Must be "RealFileSize - 8" }
|
|---|
| 36 | WAVEHeader: array [1..4] of Char; { Must be "WAVE" }
|
|---|
| 37 | { Format information }
|
|---|
| 38 | FormatHeader: array [1..4] of Char; { Must be "fmt " }
|
|---|
| 39 | FormatSize: Integer; { Must be 16 (decimal) }
|
|---|
| 40 | FormatCode: Word; { Must be 1 }
|
|---|
| 41 | ChannelNumber: Word; { Number of channels }
|
|---|
| 42 | SampleRate: Integer; { Sample rate (hz) }
|
|---|
| 43 | BytesPerSecond: Integer; { Bytes per second }
|
|---|
| 44 | BytesPerSample: Word; { Bytes per Sample }
|
|---|
| 45 | BitsPerSample: Word; { Bits per sample }
|
|---|
| 46 | { Data area }
|
|---|
| 47 | DataHeader: array [1..4] of Char; { Must be "data" }
|
|---|
| 48 | DataSize: Integer; { Data size }
|
|---|
| 49 | end;
|
|---|
| 50 |
|
|---|
| 51 | { TWAVFile }
|
|---|
| 52 |
|
|---|
| 53 | TWAVFile = class
|
|---|
| 54 | private
|
|---|
| 55 | FValid: Boolean;
|
|---|
| 56 | FChannelModeID: Byte;
|
|---|
| 57 | FSampleRate: Word;
|
|---|
| 58 | FBitsPerSample: Byte;
|
|---|
| 59 | FFileSize: Cardinal;
|
|---|
| 60 | procedure ResetData;
|
|---|
| 61 | function GetChannelMode: string;
|
|---|
| 62 | function GetDuration: Double;
|
|---|
| 63 | function HeaderIsValid: Boolean;
|
|---|
| 64 | public
|
|---|
| 65 | SourceFile: TFileStream;
|
|---|
| 66 | WAVData: TWAVRecord;
|
|---|
| 67 | constructor Create; { Create object }
|
|---|
| 68 | function OpenFile(const FileName: string): Boolean; { Load header }
|
|---|
| 69 | procedure CloseFile;
|
|---|
| 70 | property Valid: Boolean read FValid; { True if header valid }
|
|---|
| 71 | property ChannelModeID: Byte read FChannelModeID; { Channel mode code }
|
|---|
| 72 | property ChannelMode: string read GetChannelMode; { Channel mode name }
|
|---|
| 73 | property SampleRate: Word read FSampleRate; { Sample rate (hz) }
|
|---|
| 74 | property BitsPerSample: Byte read FBitsPerSample; { Bits per sample }
|
|---|
| 75 | property FileSize: Cardinal read FFileSize; { File size (bytes) }
|
|---|
| 76 | property Duration: Double read GetDuration; { Duration (seconds) }
|
|---|
| 77 | end;
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | implementation
|
|---|
| 81 |
|
|---|
| 82 | { ********************* Auxiliary functions & procedures ******************** }
|
|---|
| 83 |
|
|---|
| 84 | function TWAVFile.HeaderIsValid: Boolean;
|
|---|
| 85 | begin
|
|---|
| 86 | Result := True;
|
|---|
| 87 | if WAVData.RIFFHeader <> 'RIFF' then Result := False;
|
|---|
| 88 | if WAVData.WAVEHeader <> 'WAVE' then Result := False;
|
|---|
| 89 | if WAVData.FormatHeader <> 'fmt ' then Result := False;
|
|---|
| 90 | if WAVData.FormatSize <> 16 then Result := False;
|
|---|
| 91 | if WAVData.FormatCode <> 1 then Result := False;
|
|---|
| 92 | if WAVData.DataHeader <> 'data' then Result := False;
|
|---|
| 93 | if (WAVData.ChannelNumber <> CHANNEL_MODE_MONO) and
|
|---|
| 94 | (WAVData.ChannelNumber <> CHANNEL_MODE_STEREO) then Result := False;
|
|---|
| 95 | end;
|
|---|
| 96 |
|
|---|
| 97 | { ********************** Private functions & procedures ********************* }
|
|---|
| 98 |
|
|---|
| 99 | procedure TWAVFile.ResetData;
|
|---|
| 100 | begin
|
|---|
| 101 | FValid := false;
|
|---|
| 102 | FChannelModeID := 0;
|
|---|
| 103 | FSampleRate := 0;
|
|---|
| 104 | FBitsPerSample := 0;
|
|---|
| 105 | FFileSize := 0;
|
|---|
| 106 | end;
|
|---|
| 107 |
|
|---|
| 108 | function TWAVFile.GetChannelMode: string;
|
|---|
| 109 | begin
|
|---|
| 110 | Result := CHANNEL_MODE[FChannelModeID];
|
|---|
| 111 | end;
|
|---|
| 112 |
|
|---|
| 113 | function TWAVFile.GetDuration: Double;
|
|---|
| 114 | begin
|
|---|
| 115 | if FValid then
|
|---|
| 116 | Result := (FFileSize - 44) * 8 /
|
|---|
| 117 | FSampleRate / FBitsPerSample / FChannelModeID
|
|---|
| 118 | else
|
|---|
| 119 | Result := 0;
|
|---|
| 120 | end;
|
|---|
| 121 |
|
|---|
| 122 | constructor TWAVFile.Create;
|
|---|
| 123 | begin
|
|---|
| 124 | inherited;
|
|---|
| 125 | ResetData;
|
|---|
| 126 | end;
|
|---|
| 127 |
|
|---|
| 128 | function TWAVFile.OpenFile(const FileName: string): Boolean;
|
|---|
| 129 | var
|
|---|
| 130 | Transferred: Integer;
|
|---|
| 131 | begin
|
|---|
| 132 | Result := True;
|
|---|
| 133 | SourceFile := TFileStream.Create(FileName, fmOpenRead);
|
|---|
| 134 | Transferred := SourceFile.Read(WAVData, 44);
|
|---|
| 135 | if Transferred < 44 then Result := False;
|
|---|
| 136 |
|
|---|
| 137 | ResetData;
|
|---|
| 138 | if Result and HeaderIsValid then
|
|---|
| 139 | begin
|
|---|
| 140 | FValid := True;
|
|---|
| 141 | FChannelModeID := WAVData.ChannelNumber;
|
|---|
| 142 | FSampleRate := WAVData.SampleRate;
|
|---|
| 143 | FBitsPerSample := WAVData.BitsPerSample;
|
|---|
| 144 | FFileSize := WAVData.FileSize + 8;
|
|---|
| 145 | end;
|
|---|
| 146 | end;
|
|---|
| 147 |
|
|---|
| 148 | procedure TWAVFile.CloseFile;
|
|---|
| 149 | begin
|
|---|
| 150 | ResetData;
|
|---|
| 151 | FreeAndNil(SourceFile);
|
|---|
| 152 | end;
|
|---|
| 153 |
|
|---|
| 154 | end.
|
|---|