//返回秒数
function ReadWaveTime(FileName: string): Integer;
type
TWaveFileHeader = packed record
FType: Integer;
Size: Longint;
RType: Integer;
end;
TWaveChunkHeader = packed record
CType: Longint;
Size: Longint;
end;
const
ID_RIFF = Ord('R') + Ord('I') * $100 + Ord('F') * $10000 + Ord('F') * $1000000;
ID_WAVE = Ord('W') + Ord('A') * $100 + Ord('V') * $10000 + Ord('E') * $1000000;
ID_FMT = Ord('f') + Ord('m') * $100 + Ord('t') * $10000 + Ord(' ') * $1000000;
ID_FACT = Ord('f') + Ord('a') * $100 + Ord('c') * $10000 + Ord('t') * $1000000;
ID_DATA = Ord('d') + Ord('a') * $100 + Ord('t') * $10000 + Ord('a') * $1000000;
var
DataSize: Integer;
Format: TWaveFormatEx;
Stream: TFileStream;
Header: TWaveFileHeader;
Chunk: TWaveChunkHeader;
begin
Result := 0;
DataSize := 0;
FilLChar(Format, SizeOf(Format), 0);
Stream := TFileStream.Create(FileName, fmOpenRead);
with Streamdo
try
ReadBuffer(Header, Sizeof(Header));
if (Header.FType <>
ID_RIFF) and (Header.RType <>
ID_WAVE) then
raise Exception.Create('invalid wave file format');
Stream.ReadBuffer(Chunk, Sizeof(Chunk));
while Chunk.CType <>
0do
begin
if Chunk.CType = ID_FMT then
Stream.ReadBuffer(Format, Chunk.Size) else
if Chunk.CType = ID_DATA then
begin
DataSize := Stream.Size - Stream.Position;
break;
end else
if Chunk.CType = ID_FACT then
Stream.Seek(Chunk.Size, soFromCurrent);
FillChar(Chunk, SizeOf(Chunk), 0);
Stream.ReadBuffer(Chunk, Sizeof(Chunk));
end;
if (DataSize <>
0) and (Format.nAvgBytesPerSec <>
0) then
Result := DataSize div Format.nAvgBytesPerSec;
finally
Free;
end;
end;
TWaveFormatEx :
nAvgBytesPerSec: 声音文件中声音频率每秒钟产生出的字节数 ;