uses MMSystem, Math
//
procedure TForm1.MakeSound(Frequency, Duration: Integer; Volume: Integer);
var
WaveFormatEx: TWaveFormatEx;
MS: TMemoryStream;
i, TempInt, DataCount, RiffCount: integer;
SoundValue: byte;
t: double;
const
Mono: Word = $0001;
SampleRate: Integer = 44100;
RiffId: string = 'RIFF';
WaveId: string = 'WAVE';
FmtId: string = 'fmt ';
DataId: string = 'data';
begin
with WaveFormatEx do
begin
wFormatTag := WAVE_FORMAT_PCM;
nChannels := Mono;
nSamplesPerSec := SampleRate;
wBitsPerSample := $0008;
nBlockAlign := (nChannels * wBitsPerSample) div 8;
nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
cbSize := 0;
end;
MS := TMemoryStream.Create;
with MS do
begin
DataCount := (Duration * SampleRate) div 1000;
RiffCount := Length(WaveId) + Length(FmtId) + SizeOf(DWORD) +
SizeOf(TWaveFormatEx) + Length(DataId) + SizeOf(DWORD) + DataCount;
Write(RiffId[1], 4);
Write(RiffCount, SizeOf(DWORD));
Write(WaveId[1], Length(WaveId));
Write(FmtId[1], Length(FmtId));
TempInt := SizeOf(TWaveFormatEx);
Write(TempInt, SizeOf(DWORD));
Write(WaveFormatEx, SizeOf(TWaveFormatEx));
Write(DataId[1], Length(DataId));
Write(DataCount, SizeOf(DWORD));
for i := 0 to DataCount - 1 do
begin
t := (i * Frequency / SampleRate);
// Sinus
if RadioButton1.Checked then SoundValue := 127 + trunc(Volume * sin(2 * pi * t));
// square
if RadioButton2.Checked then SoundValue := 127 + trunc(Volume * sign(sin(2 * pi * t)));
// triangle
if RadioButton3.Checked then SoundValue := 127 + trunc(Volume * (2 * abs(2 * t - 2 * floor(t) - 1 ) - 1));
// thoot
if RadioButton4.Checked then SoundValue := 127 + trunc(Volume * (2 * (t - floor(t + 0.5))));
Write(SoundValue, SizeOf(Byte));
end;
sndPlaySound(MS.Memory, SND_MEMORY or SND_SYNC);
MS.Free;
end;
end;
Beispiel :
procedure TForm1.Button1Click(Sender: TObject);
begin
MakeSound(440, 1000, 25);
end;
Keine Kommentare:
Kommentar veröffentlichen