获取WAV文件信息
unit W32Waves;
interface
uses SysUtils, Windows, MMSystem, Dialogs;
type
PWaveInformation = ^tWaveInformation;
TWaveInformation = record
WaveFormat: Word;
Channels: Word;
SampleRate: Longint;
BitsPerSample: Word;
SamplesNumber: Longint;
Length: Extended;
ValidWave: bool;
end;
const
WAVE_FORMAT_PCM = $0001;
WAVE_FORMAT_G723_ADPCM = $0014;
WAVE_FORMAT_ANTEX_ADPCME = $0033;
WAVE_FORMAT_G721_ADPCM = $0040;
WAVE_FORMAT_APTX = $0025;
WAVE_FORMAT_AUDIOFILE_AF36 = $0024;
WAVE_FORMAT_AUDIOFILE_AF10 = $0026;
WAVE_FORMAT_CONTROL_RES_VQLPC = $0034;
WAVE_FORMAT_CONTROL_RES_CR10 = $0037;
WAVE_FORMAT_CREATIVE_ADPCM = $0200;
WAVE_FORMAT_DOLBY_AC2 = $0030;
WAVE_FORMAT_DSPGROUP_TRUESPEECH = $0022;
WAVE_FORMAT_DIGISTD = $0015;
WAVE_FORMAT_DIGIFIX = $0016;
WAVE_FORMAT_DIGIREAL = $0035;
WAVE_FORMAT_DIGIADPCM = $0036;
WAVE_FORMAT_ECHOSC1 = $0023;
WAVE_FORMAT_FM_TOWNS_SND = $0300;
WAVE_FORMAT_IBM_CVSD = $0005;
WAVE_FORMAT_OLIGSM = $1000;
WAVE_FORMAT_OLIADPCM = $1001;
WAVE_FORMAT_OLICELP = $1002;
WAVE_FORMAT_OLISBC = $1003;
WAVE_FORMAT_OLIOPR = $1004;
WAVE_FORMAT_IMA_ADPCM = $0011;
WAVE_FORMAT_DVI_ADPCM = $0011;
WAVE_FORMAT_UNKNOWN = $0000;
WAVE_FORMAT_ADPCM = $0002;
WAVE_FORMAT_ALAW = $0006;
WAVE_FORMAT_MULAW = $0007;
WAVE_FORMAT_GSM610 = $0031;
WAVE_FORMAT_MPEG = $0050;
WAVE_FORMAT_NMS_VBXADPCM = $0038;
WAVE_FORMAT_OKI_ADPCM = $0010;
WAVE_FORMAT_SIERRA_ADPCM = $0013;
WAVE_FORMAT_SONARC = $0021;
WAVE_FORMAT_MEDIASPACE_ADPCM = $0012;
WAVE_FORMAT_YAMAHA_ADPCM = $0020;
function GetWaveInformationFromFile(FileName: string; Info: pWaveInformation): bool;
implementation
type
TCommWaveFmtHeader = record
wFormatTag: Word;
nChannels: Word;
nSamplesPerSec: Longint;
nAvgBytesPerSec: Longint;
nBlockAlign: Word;
nBitsPerSample: Word;
cbSize: Longint;
end;
function GetWaveInformationFromFile(FileName: string; Info: pWaveInformation): bool;
var
hdmmio: HMMIO;
mmckinfoParent: TMMCKInfo;
mmckinfoSubchunk: TMMCKInfo;
Fmt: TCommWaveFmtHeader;
Samples: Longint;
begin
Result := False;
FillChar(Info^, SizeOf(TWaveInformation), #0);
hdmmio := mmioOpen(PChar(FileName), nil, MMIO_READ);
if (hdmmio = 0) then
Exit;
mmckinfoParent.fccType := mmioStringToFOURCC('WAVE', MMIO_TOUPPER);
if (mmioDescend(hdmmio, PMMCKINFO(@mmckinfoParent), nil, MMIO_FINDRIFF) <> 0) then
Exit;
mmckinfoSubchunk.ckid := mmioStringToFOURCC('fmt ', 0);
if (mmioDescend(hdmmio, @mmckinfoSubchunk, @mmckinfoParent, MMIO_FINDCHUNK) <> 0) then
Exit;
if (mmioRead(hdmmio, PChar(@fmt), Longint(SizeOf(TCommWaveFmtHeader))) <>
Longint(SizeOf(TCommWaveFmtHeader))) then
Exit;
Info^.WaveFormat := fmt.wFormatTag;
Info^.Channels := fmt.nChannels;
Info^.SampleRate := fmt.nSamplesPerSec;
Info^.BitsPerSample := fmt.nBitsPerSample;
mmioAscend(hdmmio, @mmckinfoSubchunk, 0);
mmckinfoSubchunk.ckid := mmioStringToFOURCC('data', 0);
if (mmioDescend(hdmmio, @mmckinfoSubchunk, @mmckinfoParent, MMIO_FINDCHUNK) <> 0) then
Exit;
Info^.SamplesNumber := mmckinfoSubchunk.cksize;
Samples := (Info^.SamplesNumber * 8 ) div Info^.BitsPerSample div Info^.Channels;
Info^.Length := Samples / Info^.Samplerate;
mmioClose(hdmmio, 0);
Info^.ValidWave := True;
Result := True;
end;
end.