unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
MMSystem,
StdCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
WaveFormatEx : TWaveFormatEx;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
i, TempInt,
DataCount,
RiffCount : integer;
SoundValue : byte;
w : double;
MS : TMemoryStream;
const
Mono : Word = $0001;
SampleRate : integer = 11025;
RiffId :
string = '
RIFF';
WaveId :
string = '
WAVE';
FmtId :
string = '
fmt ';
DataId :
string = '
data';
Duration : integer=1500;
Frequency : integer=1500;
begin
with WaveFormatEx
do begin
wFormatTag := WAVE_FORMAT_PCM;
nChannels := Mono;
nSamplesPerSec := SampleRate;
wBitsPerSample := $0008;
nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
nBlockAlign := (nChannels * wBitsPerSample)
div 8;
cbSize := 0;
end;
MS := TMemoryStream.Create;
with MS
do begin
DataCount := (Duration * SampleRate)
div 1000;
// sound data
RiffCount := Length(WaveId)
+ Length(FmtId) + SizeOf(DWord)
+ SizeOf(TWaveFormatEx)
+ Length(DataId) + SizeOf(DWord)
+ DataCount;
// file data
Write(RiffId[1], 4);
Write(RiffCount, SizeOf(DWord));
Write(WaveId[1], Length(WaveId));
// 'WAVE'
Write(FmtId[1], Length(FmtId));
// 'fmt '
TempInt := SizeOf(TWaveFormatEx);
Write(TempInt, SizeOf(DWord));
Write(WaveFormatEx, SizeOf(TWaveFormatEx));
Write(DataId[1], Length(DataId));
// 'data'
Write(DataCount, SizeOf(DWord));
w := 2 * Pi * Frequency;
// omega
for i := 0
to DataCount - 1
do begin
SoundValue := 127 + trunc(127 * sin(i * w / SampleRate));
Write(SoundValue, SizeOf(Byte));
end;
sndPlaySound(MS.Memory, SND_MEMORY
or SND_SYNC);
MS.Free;
end;
end;
end.