[ Milos D @ 05.07.2005. 18:18 ] @
Zelim da PC svira tonove (frekvencije) na osnovu nekih podataka koji pristizu vremenom. Ima vise nacina da se to uradi, ali kod svakog je problem u "prelasku" sa jednog na drugi ton, naime ili dodje do kratkog prekida zvuka, ili do nekog "klik" zvuka izmedju dva tona tako da je "melodija" isprekidana. Probao sam MIDI ( http://www.alan-warriner.co.uk/ ), speaker (windows.beep) i ovo: Code: var msSound: TMemoryStream; procedure MakeSound(Frequency{Hz}, Duration{mSec}: Integer; Volume: TVolumeLevel); {writes tone to memory and plays it} var WaveFormatEx: TWaveFormatEx; i, TempInt, DataCount, RiffCount: integer; SoundValue: byte; w: double; // omega ( 2 * pi * frequency) const Mono: Word = $0001; // SampleRate: Integer = 11025; // 8000, 11025, 22050, or 44100 SampleRate: Integer = 44100; RiffId: string = 'RIFF'; WaveId: string = 'WAVE'; FmtId: string = 'fmt '; DataId: string = 'data'; begin // sndPlaySound (nil, 0); if Frequency > (0.6 * SampleRate) then begin ShowMessage(Format('Sample rate of %d is too Low to play a tone of %dHz', [SampleRate, Frequency])); Exit; end; 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; if msSound = nil then msSound := TMemoryStream.Create else msSound.Clear; with msSound 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 {calculate and write out the tone signal} // now the data values w := 2 * Pi * Frequency; // omega for i := 0 to DataCount - 1 do begin SoundValue := 127 + trunc(Volume * sin(i * w / SampleRate)); // wt = w * i / SampleRate Write(SoundValue, SizeOf(Byte)); end; {now play the sound} //sndPlaySound (nil, 0); sndPlaySound(msSound.Memory, SND_MEMORY or SND_ASYNC); end; end; procedure TForm1.Button2Click(Sender: TObject); var i : integer; begin for i := 0 to 100 do begin MakeSound (1000 + 100 * round (5*random), 150, 100); Application.ProcessMessages; sleep (150); end; end; Naravno, ne znam unapred koji ce biti sledeci zvuk, ni koliko ce trajati! |