Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Screen Resolution, Timer, MessageDlg and Sound - pitanja

[es] :: Pascal / Delphi / Kylix :: Screen Resolution, Timer, MessageDlg and Sound - pitanja

[ Pregleda: 2369 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

borovac
127.0.0.1

Član broj: 29278
Poruke: 220
*.as54.ze.bih.net.ba.



Profil

icon Screen Resolution, Timer, MessageDlg and Sound - pitanja05.10.2004. u 15:21 - pre 238 meseci
1. Kako pozvati neku funkciju za 5 sec.
Znam da može ovako:

Code:
Sleep(5000);
MyFuction;


Ali kako to uraditi bez upotrebe sleep?

2. Kako saznati koja je trenutna "screen resolution"?

3. Kod naredbe MessageDlg koristi se Dialogs ili QDialogs unit.
Kod QDialogs možeš navesti "message title" ili "default button"?
Da li se to može izvesti sa Dialogs?

4. Kako odsvirati zvuk određene frekvencije i tajanja?
Postoji naredba Beep koja bipne određenom frekvencijom i trajanjem.
Međutim "Beep" radi samo na WinNT platformama.
Da li postoji nešto drugo?
 
Odgovor na temu

reiser

Član broj: 7895
Poruke: 2314



+102 Profil

icon Re: Screen Resolution, Timer, MessageDlg and Sound - pitanja05.10.2004. u 16:01 - pre 238 meseci
1. Mozes pomocu Timer komponente. Setujes Timer.Interval na 5000 i prilikom prvog OnTimer event-a pozoves MyFunction.
Ili, koristi ovu funkciju :
Code:

// Delay * 100ns (e.g. PreciseDelay(10) will sleep 1000ns)
procedure PreciseDelay(const Delay : Int64);
var
  Timer    : THandle;
  LargeInt : Int64;
begin
  Timer := CreateWaitableTimer(nil, FALSE, 'MyPreciseTimer');
  If Timer <> 0 Then
  Begin
    Try
      LargeInt := -Delay;
      SetWaitableTimer(Timer, LargeInt, 0, nil, nil, FALSE);
      WaitForSingleObject(Timer, INFINITE);
    Finally
      CloseHandle(Timer);
    End;
  End;
end;

* by Rapaic Rajko

2.
Code:

type
  TSysMetrics = Record
                  Width    : Integer;
                  Height   : Integer;
                  RefRate  : Integer;
                  ColDepth : Integer;
                end;

var
  oScreenMode : TDeviceMode;
  SysMetrics   : TSysMetrics;

begin
// Uzima trenutne parametre za setting monitora
  EnumDisplaySettings(nil, $FFFFFFFE, oScreenMode);
  With SysMetrics Do
  Begin
    Width := oScreenMode.dmPelsWidth;
    Height := oScreenMode.dmPelsHeight;
    ColDepth := oScreenMode.dmBitsPerPel;
    RefRate := oScreenMode.dmDisplayFrequency;
  End;


3. Ne znam... U svakom slucaju ako neces da koristis MessageDlg, mozes da koristis Application.MessageBox ili MessageBox

4. Koliko vidim, ova procedura generise .wav fajl (zapisuje ga u MemoryStream), i pusta ga sa SndPlaySound (preuzeto sa torry.net) :
Code:

uses 
  MMSystem; 

type 
  TVolumeLevel = 0..127; 

procedure MakeSound(Frequency{Hz}, Duration{mSec}: Integer; Volume: TVolumeLevel); 
  {writes tone to memory and plays it} 
var 
  WaveFormatEx: TWaveFormatEx; 
  MS: TMemoryStream; 
  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 
  RiffId: string = 'RIFF'; 
  WaveId: string = 'WAVE'; 
  FmtId: string = 'fmt '; 
  DataId: string = 'data'; 
begin 
  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; 
  MS := TMemoryStream.Create; 
  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 
    {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(MS.Memory, SND_MEMORY or SND_SYNC); 
    MS.Free; 
  end; 
end;


poz
 
Odgovor na temu

bancika
Branislav Stojkovic

Član broj: 24844
Poruke: 631
195.252.103.*

Sajt: www.diy-fever.com


+1 Profil

icon Re: Screen Resolution, Timer, MessageDlg and Sound - pitanja05.10.2004. u 16:02 - pre 238 meseci
2) GetSystemMetrics(SM_CXSCREEN) i SM_CYSCREEN
procackaj malo Windows SDK help i videces za sta sve moze da se koristi GetSystemMetrics
3) ne, koliko ja znam, zar je bitno?
4)ovo ja koristim u te svrhe
Code:

uses 
  MMSystem; 

type 
  TVolumeLevel = 0..127; 

procedure MakeSound(Frequency{Hz}, Duration{mSec}: Integer; Volume: TVolumeLevel); 
  {writes tone to memory and plays it} 
var 
  WaveFormatEx: TWaveFormatEx; 
  MS: TMemoryStream; 
  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 
  RiffId: string = 'RIFF'; 
  WaveId: string = 'WAVE'; 
  FmtId: string = 'fmt '; 
  DataId: string = 'data'; 
begin 
  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; 
  MS := TMemoryStream.Create; 
  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 
    {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(MS.Memory, SND_MEMORY or SND_SYNC); 
    MS.Free; 
  end; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
  MakeSound(1200, 1000, 60); 
end; 



Ride the rainbow, crack the sky

DIY gitare, pojacala i efekti www.diy-fever.com
 
Odgovor na temu

bancika
Branislav Stojkovic

Član broj: 24844
Poruke: 631
195.252.103.*

Sajt: www.diy-fever.com


+1 Profil

icon Re: Screen Resolution, Timer, MessageDlg and Sound - pitanja05.10.2004. u 16:04 - pre 238 meseci
gle, u isto vreme smo mu napisali istu proceduru, bez da znamo jedan za drugog :) koje su sanse za to
Ride the rainbow, crack the sky

DIY gitare, pojacala i efekti www.diy-fever.com
 
Odgovor na temu

borovac
127.0.0.1

Član broj: 29278
Poruke: 220
*.as54.ze.bih.net.ba.



Profil

icon Re: Screen Resolution, Timer, MessageDlg and Sound - pitanja05.10.2004. u 19:45 - pre 238 meseci
Hvala momci. Lijepo ste to potrefili u isto vrijeme
 
Odgovor na temu

reiser

Član broj: 7895
Poruke: 2314



+102 Profil

icon Re: Screen Resolution, Timer, MessageDlg and Sound - pitanja05.10.2004. u 19:46 - pre 238 meseci
Hehe
 
Odgovor na temu

Rapaic Rajko
Bgd

Član broj: 4105
Poruke: 810
217.119.242.*



+62 Profil

icon Re: Screen Resolution, Timer, MessageDlg and Sound - pitanja07.10.2004. u 13:54 - pre 237 meseci
Ti rece u prvom postu da ne zelis da koristis Sleep(). Pretpostavljam da je to zato sto Sleep funkcija 'zamrzava' thread koji je poziva, u tvom slucaju MainThread aplikacije. Krajnji rezultat je da tih 5 sekundi app ne reaguje na tvoje komande; to izgleda prilicno neprofesionalno.
Ista stvar se desava i koriscenjem high precision timer-a (u ovom slucaju funkcija WaitForSingleObject() ). Zato je najbolje da koristis obican TTimer, evo i zasto. Delphi-jev TTimer je 'omotac' (wrapper) oko Windows-ovog timer-a, koji opet moze da se koristi na dva nacina. Prvi je da pozoves API funkciju SetTimer() i tu prosledis funkciju koju treba sistem da ti izvrsi, a drugi je da pozoves isto ali sa nil (NULL) parametrom za tu funkciju. U ovom drugom slucaju (koji koristi Delphi-jev TTimer) sistem samo posalje poruku WM_Timer aplikaciji koja je kreirala taj timer. Aplikacija obradjuje tu poruku i pozove - sta?... pa upravo metodu OnTimer tvog Delphi timer-a. Ono sto je tebi bitno je da NEMA zamrzavanja aplikacije u tom vremenskom intervalu, to jest dok stigne poruka WMTimer, MainThread je slobodan da obradjuje i druge poruke, znaci i neke tvoje akcije itd. itd. Doduse, treba reci da obrada WMTimer poruke moze malo i da 'kasni' jer ta poruka ima nizak prioritet, pa je novopristigle poruke 'viseg prioriteta' potiskuju na kraj message queue-a (npr. WMPaint; stalno se nesto iscrtava, zar ne).

Rajko

P.S. Ako ti treba precizno 5 sekundi, a da aplikacija ne zamrzava, javi - ovaj post je vec prevelik.
 
Odgovor na temu

[es] :: Pascal / Delphi / Kylix :: Screen Resolution, Timer, MessageDlg and Sound - pitanja

[ Pregleda: 2369 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.