Ich wollte mal auch so was schrieben, hab das folgender maßen, mit einem Objekt gemacht:
Delphi-Quellcode:
const
Mono : Word = $0001;
RiffId : string = 'RIFF';
WaveId : string = 'WAVE';
FmtId : string = 'fmt ';
DataId : string = 'data';
type
TWaveFile = class
WaveFormatEx : TWaveFormatEx;
MS : TMemoryStream;
TempInt,
DataCount,
RiffCount : integer;
SoundValue : byte;
w : double; // omega ( 2 * pi * frequency)
duration : integer;
samplerate : integer;
constructor Create;
procedure AddHeader(HSampleRate,HDuration:Integer);
procedure AddData (Freq,HDuration:Integer);
procedure SaveToFile(FileName:String);
procedure Play;
destructor Free;
end;
constructor TWaveFile.Create;
begin
inherited Create;
MS := TMemoryStream.Create;
samplerate := 11025;
end;
destructor TWaveFIle.Free;
begin
MS.Free;
end;
procedure TWaveFile.AddHeader(HSampleRate,HDuration:integer);
begin
duration := hduration;
samplerate := HSampleRate;
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;
with MS do begin
{Calculate length of sound data and of file data}
DataCount := (Duration * SampleRate) div 1000; // sound data
RiffCount := Length(WaveId)
+ Length(FmtId) + SizeOf(DWord)
+ SizeOf(TWaveFormatEx)
+ Length(DataId) + SizeOf(DWord)
+ DataCount; // file data
{write out the wave header}
Write(RiffId[1], 4); // 'RIFF'
Write(RiffCount, SizeOf(DWord)); // file data size
Write(WaveId[1], Length(WaveId)); // 'WAVE'
Write(FmtId[1], Length(FmtId)); // 'fmt '
TempInt := SizeOf(TWaveFormatEx);
Write(TempInt, SizeOf(DWord)); // TWaveFormat data size
Write(WaveFormatEx, SizeOf(TWaveFormatEx)); // WaveFormatEx record
Write(DataId[1], Length(DataId)); // 'data'
Write(DataCount, SizeOf(DWord)); // sound data size
end;
end;
procedure TWaveFile.AddData(Freq,HDuration:integer);
var i :integer;
begin
w := 2 * Pi * Freq; // omega
for i := 0 to hduration*samplerate div 1000 - 1 do begin
// wt = w *i /SampleRate
SoundValue := 127 + trunc(127 * sin(i * w / SampleRate));
MS.Write(SoundValue, SizeOf(Byte));
end;
end;
procedure TWavefile.SaveToFile(FileName:String);
begin
MS.Seek(0, soFromBeginning);
MS.SaveToFile(Filename);
end;
procedure TWaveFile.Play;
begin
sndPlaySound(MS.Memory, SND_MEMORY or SND_SYNC);
end;
Nun kannst musst du deinen Text, oder was auch immer erst einmal in "Morsecodes" umwandeln. Dann kanns du zu der Wave feile die entsprechenden Töne hinzufügen.
Ein Beispiel:
Du möchtest eine Frequenz erzeugen von 400 hz und 1 sekunde dann
Delphi-Quellcode:
wavefile := TWaveFile.Create;
wavefile.addheader(44100,1000);
wavefile.adddata(400,1000);
wavefile.play;
ich hoffe das ist was du meinst!
wenn du eine pause machen willst : wavefile.adddata(0,length)