This Delphi code will allow you to read the header of wav files.
//Description of the Wav Header
type
TWaveHeader = record
idRiff: array[0..3]
of
char;
RiffLen: longint;
idWave: array[0..3]
of
char;
idFmt: array[0..3]
of
char;
InfoLen: longint;
WaveType: smallint;
Ch:
smallint;
Freq:
longint;
BytesPerSec: longint;
align: smallint;
Bits:
smallint;
end;
TDataHeader = record
idData: array[0..3]
of
char;
DataLen: longint;
end;
procedure ReadWaveHeader(Stream: TStream;
var
SampleCount, SamplesPerSec: integer;
var
BitsPerSample, Channeles: smallint);
var
WaveHeader: TWaveHeader;
DataHeader: TDataHeader;
begin
Stream.Read(WaveHeader,
sizeof(TWaveHeader));
with
WaveHeader do
begin
if
idRiff <> 'RIFF' then
raise EReadError.Create('Wrong idRIFF');
if
idWave <> 'WAVE' then
raise EReadError.Create('Wrong idWAVE');
if
idFmt <> 'fmt
' then raise EReadError.Create('Wrong idFmt');
if
WaveType <> 1
then
raise EReadError.Create('Unknown format');
Channeles := Ch;
SamplesPerSec := Freq;
BitsPerSample := Bits;
Stream.Seek(InfoLen - 16,
soFromCurrent);
end;
Stream.Read(DataHeader,
sizeof(TDataHeader));
if
DataHeader.idData = 'fact' then
begin
Stream.Seek(4,
soFromCurrent);
Stream.Read(DataHeader, sizeof(TDataHeader));
end;
with
DataHeader do
begin
if
idData <> 'data' then
raise EReadError.Create('Wrong idData');
SampleCount := DataLen div
(Channeles * BitsPerSample div 8)
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
OpenDialog1.Filter := 'Wave files|*.wav';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
F:
TFileStream;
SampleCount, SamplesPerSec: integer;
BitsPerSample, Channeles: smallint;
begin
if
not
OpenDialog1.Execute then
Exit;
try
F :=
TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
ReadWaveHeader(F, SampleCount, SamplesPerSec,
BitsPerSample, Channeles);
F.Free;
Memo1.Clear;
Memo1.Lines.Add('SampleCount: ' + IntToStr(SampleCount));
Memo1.Lines.Add(Format('Length: %5.3f sec', [SampleCount / SamplesPerSec]));
Memo1.Lines.Add('Channeles: ' + IntToStr(Channeles));
Memo1.Lines.Add('Freq:
' +
IntToStr(SamplesPerSec));
Memo1.Lines.Add('Bits:
' +
IntToStr(BitsPerSample));
except
raise Exception.Create('Error: Find problems with file reading');
end;
end;
Other method to read the wav-header
if you want a large FX collection wav files with Fantastic Fx Sounds Library. Over 2300 Wav Sound Effects, Ambient Effects, Club-Trance Effects, Dark Sounds, Hit Effects, Laser Sounds, Reverse Fx, Scratch Sounds, Voices Effects press here
Source code: http://delphiworld.narod.ru/base/read_write_sound.html
Other method to read the wav-header
function GetWaveHeader(FileName: TFilename): TWaveHeader;
const
riff =
'RIFF';
wave =
'WAVE';
var
f:
TFileStream;
w:
TWaveHeader;
begin
if
not
FileExists(Filename) then
exit;
try
f :=
TFileStream.create(Filename, fmOpenRead);
f.Read(w, Sizeof(w));
if
w.ident1 <> riff then
begin
Showmessage('This is not
a RIFF File');
exit;
end;
if
w.ident2 <> wave then
begin
Showmessage('This is not
a valid wave file');
exit;
end;
finally
f.free;
end;
Result
:= w;
end;
EXPLAIN THE TYPE WAV:
type
TWaveHeader = record
ident1: array[0..3]
of
Char; // Must be
"RIFF"
len:
DWORD; // remaining length after this header
ident2: array[0..3]
of
Char; // Must be
"WAVE"
ident3: array[0..3]
of
Char; // Must be
"fmt "
reserv: DWORD; // Reserved Size
wFormatTag: Word; // format type
nChannels: Word; // number of channels (i.e. mono, stereo, etc.)
nSamplesPerSec: DWORD; //sample rate
nAvgBytesPerSec: DWORD; //for buffer estimation
nBlockAlign: Word; //block size of data
wBitsPerSample: Word; //number of bits per sample of mono data
cbSize: Word; //the count in bytes of the size of
ident4: array[0..3]
of
Char; //Must be
"data"
end;
if you want a large FX collection wav files with Fantastic Fx Sounds Library. Over 2300 Wav Sound Effects, Ambient Effects, Club-Trance Effects, Dark Sounds, Hit Effects, Laser Sounds, Reverse Fx, Scratch Sounds, Voices Effects press here
Source code: http://delphiworld.narod.ru/base/read_write_sound.html