📄 mmwaveio.pas
字号:
{========================================================================}
{= (c) 1995-98 SwiftSoft Ronald Dittrich =}
{========================================================================}
{= All Rights Reserved =}
{========================================================================}
{= D 01099 Dresden = Fax.: +49(0)351-8037944 =}
{= Loewenstr.7a = info@swiftsoft.de =}
{========================================================================}
{= Actual versions on http://www.swiftsoft.de/index.html =}
{========================================================================}
{= This code is for reference purposes only and may not be copied or =}
{= distributed in any format electronic or otherwise except one copy =}
{= for backup purposes. =}
{= =}
{= No Delphi Component Kit or Component individually or in a collection=}
{= subclassed or otherwise from the code in this unit, or associated =}
{= .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed =}
{= without express permission from SwiftSoft. =}
{= =}
{= For more licence informations please refer to the associated =}
{= HelpFile. =}
{========================================================================}
{= $Date: 23.11.98 - 13:42:36 $ =}
{========================================================================}
unit MMWaveIO;
{$I COMPILER.INC}
interface
uses
{$IFDEF WIN32}
Windows,
{$ELSE}
WinTypes,
WinProcs,
{$ENDIF}
SysUtils,
MMSystem,
MMRegs,
MMRiff,
MMUtils,
MMAbout;
const
{ file open flags for waveio functions }
RIFF_FILE = $0001;
RIFF_RESOURCE = $0002;
RIFF_MEMORY = $0004;
{ get the extra Info only }
RIFF_INFO_ONLY = $0008;
{ we have a RAW file }
RAW_FILE = $0010;
type
PWAVEIOCB = ^TWAVEIOCB;
TWAVEIOCB = packed record { do not change the first items !!!! }
dwSize : DWORD; { size for the structure }
dwFlags : DWORD; { flags used to open the file }
hmmio : THMMIO; { handle to open file }
ckRIFF,ckDATA : TMMCKINFO; { the current RIFF and data chunk }
hMem : THandle; { <> 0 if we have a res. file, free it !}
dwFileSize : DWORD; { the filesize for the actual file }
dwDataBytes : DWORD; { the data size for the actual file }
dwDataOffset : DWORD; { offset from data chunk }
dwDataSamples : DWORD; { how much samples in the file }
dwFirstSample : DWORD; { the first sample to read }
dwLastSample : DWORD; { the last Sample to read }
dwBytesLeft : DWORD; { how many bytes to play }
dwPosition : DWORD; { the current file position }
lpFilePath : PChar; { filename from the actual file }
lpDisp : PDispList; { pointer to display chunk's }
lpInfo : PInfoChunk; { pointer to info chunk }
fAvgBytesPerSec : Double;
{ this MUST be the last element in this structure--its length is }
{ not fixed; use ab[] to get at any extra bytes (note! the length }
{ of ab[] is in wfx.cbSize--this CAN be zero!) }
wfx : TWaveFormatEx;
ab : array[0..0] of Byte;
end;
const
{ error returns from waveio functions }
WIOERR_BASE = 100;
WIOERR_NOERROR = 0;
WIOERR_ERROR = WIOERR_BASE+1;
WIOERR_BADHANDLE = WIOERR_BASE+2;
WIOERR_BADFLAGS = WIOERR_BASE+3;
WIOERR_BADPARAM = WIOERR_BASE+4;
WIOERR_BADSIZE = WIOERR_BASE+5;
WIOERR_FILEERROR = WIOERR_BASE+6;
WIOERR_NOMEM = WIOERR_BASE+7;
WIOERR_BADFILE = WIOERR_BASE+8;
WIOERR_NODEVICE = WIOERR_BASE+9;
WIOERR_BADFORMAT = WIOERR_BASE+10;
WIOERR_ALLOCATED = WIOERR_BASE+11;
WIOERR_NOTSUPPORTED = WIOERR_BASE+12;
function wioBytesPerSample(pwfx: PWaveFormatEx): integer;
{ 32 bit functions }
function wioBytesToSamples(pwfx: PWaveFormatEx; dwBytes: DWORD): DWORD;
function wioBytesToTime(pwfx: PWaveFormatEx; dwBytes: DWORD): DWORD;
function wioSamplesToBytes(pwfx: PWaveFormatEx; dwSamples: DWORD): DWORD;
function wioSamplesToTime(pwfx: PWaveFormatEx; dwSamples: DWORD): DWORD;
function wioTimeToSamples(pwfx: PWaveFormatEx; dwTime: DWORD): DWORD;
function wioTimeToBytes(pwfx: PWaveFormatEx; dwTime: DWORD): DWORD;
{ 64 bit functions }
function wioBytesToSamples64(pwfx: PWaveFormatEx; dwBytes: int64): int64;
function wioBytesToTime64(pwfx: PWaveFormatEx; dwBytes: int64): int64;
function wioSamplesToBytes64(pwfx: PWaveFormatEx; dwSamples: int64): int64;
function wioSamplesToTime64(pwfx: PWaveFormatEx; dwSamples: int64): int64;
function wioTimeToSamples64(pwfx: PWaveFormatEx; dwTime: int64): int64;
function wioTimeToBytes64(pwfx: PWaveFormatEx; dwTime: int64): int64;
{ lpwio functions }
function wioBytesToSamplesEx(lpwio: PWaveIOCB; dwBytes: DWORD): DWORD;
function wioBytesToTimeEx(lpwio: PWaveIOCB; dwBytes: DWORD): DWORD;
function wioSamplesToBytesEx(lpwio: PWaveIOCB; dwSamples: DWORD): DWORD;
function wioSamplesToTimeEx(lpwio: PWaveIOCB; dwSamples: DWORD): DWORD;
function wioTimeToSamplesEx(lpwio: PWaveIOCB; dwTime: DWORD): DWORD;
function wioTimeToBytesEx(lpwio: PWaveIOCB; dwTime: DWORD): DWORD;
function wioSizeOfWaveFormat(pwfx: PWaveFormatEx): integer;
function wioCopyWaveFormat(pwfx: PWaveFormatEx): PWaveFormatEx;
procedure wioGetFormatName(pwfx: PWaveFormatEx; var FormatName: String);
procedure wioGetFormat(pwfx: PWaveFormatEx; var Format: String);
function wioIsWaveFile(FilePath: TFileName; dwFlags: DWORD): Boolean;
function wioGetFullPathName(lpFilePath: PChar): Boolean;
function wioFileExists(lpFilePath: PChar): Boolean;
function wioFileDelete(lpFilePath: PChar): Boolean;
procedure wioExtractPath(lpFilePath: PChar);
function wioFileCreateTemp(lpFilePath: PChar): Boolean;
function wioFileOpen(Var hmmio: THMMIO; Var ckRIFF: TMMCKINFO;
Var HMem: THandle; lpFilePath: PChar;
fccType: FourCC; dwFlags: DWORD): integer;
procedure wioFileClose(Var hmmio: THMMIO; Var hMem: THandle);
function wioCreateFileInfo(Var lpwio: PWAVEIOCB; pwfx: PWaveFormatEx): integer;
function wioCopyFileInfo(lpwioDst, lpwioSrc: PWAVEIOCB): integer;
function wioBuildFileInfoFromMem(Var lpwio: PWaveIOCB; Memory: Pointer; MemSize: DWORD): Word;
function wioBuildFileInfoFromResource(Var lpwio: PWaveIOCB; ResourceName: PChar): Word;
function wioBuildFileInfoFromRAWEx(Var lpwio: PWaveIOCB; lpFileName: PChar; DataOffset: DWORD;
pwfx: PWaveFormatEx): integer;
function wioBuildFileInfoFromRAW(Var lpwio: PWaveIOCB; lpFileName: PChar;
DataOffset, BitLength, Channels, SampleRate: DWORD): integer;
function wioReadFileInfo(Var lpwio: PWAVEIOCB; lpFilePath: PChar;
fccType: FourCC; dwFlags: DWORD): integer;
function wioWriteFileInfo(Var lpwio: PWAVEIOCB; lpFilePath: PChar): integer;
function wioFreeFileInfo(Var lpwio: PWAVEIOCB): integer;
function wioWaveCopyUselessChunks(lpwioSrc, lpwioDst: PWaveIOCB): integer;
function wioSetIOBufferSize(lpwio: PWaveIOCB; dwSize: integer): integer;
function wioWaveOpen(lpwio: PWaveIOCB): integer;
function wioWaveClose(lpwio: PWaveIOCB): integer;
function wioWaveLoadFile(lpFilePath: PChar; Var pwfx: PWaveFormatEx;
Var Buffer: PChar; Var Size: DWORD): integer;
function wioWaveSaveFile(lpFilePath: PChar; pwfx: PWaveFormatEx;
Buffer: PChar; Size: DWORD): integer;
function wioWaveSetFirstSample(lpwio: PWaveIOCB; dwSample: DWORD): integer;
function wioWaveSetLastSample(lpwio: PWaveIOCB; dwSample: DWORD): integer;
function wioWaveSetPosition(lpwio: PWaveIOCB; dwSample: DWORD): integer;
function wioWaveReadData(lpwio: PWaveIOCB; Buffer: PChar; nBytes: DWORD): DWORD;
function wioWaveReadDataDirect(lpwio: PWaveIOCB; Buffer: PChar; nBytes: DWORD): DWORD;
function wioWaveReadSamples(lpwio: PWaveIOCB; Buffer: PChar; nSamples: DWORD): DWORD;
function wioWaveWriteData(lpwio: PWaveIOCB; Buffer: PChar; nBytes: DWORD): DWORD;
function wioWaveWriteDataDirect(lpwio: PWaveIOCB; Buffer: PChar; nBytes: DWORD): DWORD;
function wioWaveWriteSamples(lpwio: PWaveIOCB; Buffer: PChar; nSamples: DWORD): DWORD;
implementation
Uses
MMMath,
MMMulDiv,
MMPCMSup;
{$DEFINE ROUND_UP}
{*************************************************************************}
{* *}
{* DESCRIPTION: *}
{* calculate the bytes per sample from the actual settings. *)
{* *}
{*************************************************************************}
function wioBytesPerSample(pwfx: PWaveFormatEx): integer;
begin
Result := wioSamplesToBytes(pwfx, 1);
end;
{*************************************************************************}
{* *}
{* DESCRIPTION: *}
{* convert a byte offset into a sample offset. *}
{* *}
{* dwSamples = (dwBytes / nAvgBytesPerSec) * nSamplesPerSec *}
{* *}
{*************************************************************************}
function wioBytesToSamples(pwfx: PWaveFormatEx; dwBytes: DWORD): DWORD;
begin
Result := muldiv32RN(dwBytes,
pwfx^.nSamplesPerSec,
pwfx^.nAvgBytesPerSec);
end;
{*************************************************************************}
{* *}
{* DESCRIPTION: *}
{* convert a byte offset into a time offset in milliseconds. *}
{* *}
(* dwTime = (dwBytes / nAvgBytesPerSec) * 1000 *)
{* *}
{*************************************************************************}
function wioBytesToTime(pwfx: PWaveFormatEx; dwBytes: DWORD): DWORD;
begin
Result := muldiv32RN(dwBytes, 1000, pwfx^.nAvgBytesPerSec);
end;
{*************************************************************************}
{* *}
{* DESCRIPTION: *}
{* convert a sample offset into a byte offset, with correct alignment*}
{* to nBlockAlign. *}
{* *}
{* dwBytes = (dwSamples / nSamplesPerSec) * nBytesPerSec *}
{* *}
{*************************************************************************}
function wioSamplesToBytes(pwfx: PWaveFormatEx; dwSamples: DWORD): DWORD;
begin
Result := muldiv32RN(dwSamples,
pwfx^.nAvgBytesPerSec,
pwfx^.nSamplesPerSec);
{ now align the byte offset to nBlockAlign }
{$IFDEF ROUND_UP}
Result := ((Result + pwfx^.nBlockAlign-1) div pwfx^.nBlockAlign) * pwfx^.nBlockAlign;
{$ELSE}
Result := (Result div pwfx^.nBlockAlign) * pwfx^.nBlockAlign;
{$ENDIF}
end;
{*************************************************************************}
{* *}
{* DESCRIPTION: *}
{* convert a sample offset into a time offset in milliseconds. *}
{* *}
{* dwTime = (dwSamples / nSamplesPerSec) * 1000 *}
{* *}
{*************************************************************************}
function wioSamplesToTime(pwfx: PWaveFormatEx; dwSamples: DWORD): DWORD;
begin
Result := muldiv32RN(dwSamples, 1000, pwfx^.nSamplesPerSec);
end;
{*************************************************************************}
{* *}
{* DESCRIPTION: *}
{* convert a time index in milliseconds to a sample offset. *}
{* *}
{* dwSamples = (dwTime / 1000) * nSamplesPerSec *}
{* *}
{*************************************************************************}
function wioTimeToSamples(pwfx: PWaveFormatEx; dwTime: DWORD): DWORD;
begin
Result := muldiv32RN(dwTime, pwfx^.nSamplesPerSec, 1000);
end;
{*************************************************************************}
{* *}
{* DESCRIPTION: *}
{* convert a time index in milliseconds to a byte offset, with *)
(* correct alignment to nBlockAlign. . *)
{* *}
{* dwBytes = ((dwTime / 1000) * nAvgBytesPerSec) *}
{* *}
{*************************************************************************}
function wioTimeToBytes(pwfx: PWaveFormatEx; dwTime: DWORD): DWORD;
begin
Result := muldiv32RN(dwTime, pwfx^.nAvgBytesPerSec, 1000);
{ now align the byte offset to nBlockAlign }
{$IFDEF ROUND_UP}
Result := ((Result + pwfx^.nBlockAlign-1) div pwfx^.nBlockAlign) * pwfx^.nBlockAlign;
{$ELSE}
Result := (Result div pwfx^.nBlockAlign) * pwfx^.nBlockAlign;
{$ENDIF}
end;
{*************************************************************************}
{* *}
{* DESCRIPTION: *}
{* convert a byte offset into a sample offset. (64 bit) *}
{* *}
{* dwSamples = (dwBytes / nAvgBytesPerSec) * nSamplesPerSec *}
{* *}
{*************************************************************************}
function wioBytesToSamples64(pwfx: PWaveFormatEx; dwBytes: int64): int64;
begin
Result := MulDiv64(dwBytes,
pwfx^.nSamplesPerSec,
pwfx^.nAvgBytesPerSec);
end;
{*************************************************************************}
{* *}
{* DESCRIPTION: *}
{* convert a byte offset into a time offset in milliseconds.(64 bit)*}
{* *}
(* dwTime = (dwBytes / nAvgBytesPerSec) * 1000 *)
{* *}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -