Beep vs MessageBeep vs Windows.Beep procedure in Delphi

Beep 

Beep procedure call generates a very short and quiet sound through the internal speaker of our system. It is used to inform the user that something has gone wring. It is declared in SysUtils unit. and on Windows OS, Beep procedure call Windows API MessageBeep procedure internally.

How to use Beep?

Start a project. Put a button on it. Make the button's OnClick handler do.....

Uses
      SysUtils;
..........………....
procedure TForm1.Button1Click(Sender: TObject);
begin
  Beep;
end;

That’s it, we can hear a sound on button click after application run.

MessageBeep

The MessageBeep function plays a waveform sound. The waveform sound for each sound type is identified by an entry in the sounds section of the registry. After queuing the sound, the MessageBeep function returns control to the calling function and plays the sound asynchronously. It is declared in Windows unit.

  MessageBeep(uType: Cardinal);

Parameter uType specifies the sound type, as identified by an entry in the [sounds] section of the registry. This parameter can be one of the following values:

Value                              Meaning
MB_OK                             Plays default beep sound
MB_ICONINFORMATION     Plays information message sound
MB_ICONWARNING             Plays warning message sound
MB_ICONERROR             Plays error message sound
MB_ICONEXCLAMATION     Plays warning message sound
MB_ICONASTERISK             Plays informaction message sound
MB_ICONHAND                     Plays error message soung
MB_ICONSTOP                     Plays error message sound

If it cannot play the specified alert sound, MessageBeep attempts to play the system default sound. If it cannot play the system default sound, the function produces a standard beep sound through the computer speaker. The user can disable the warning beep by using the Control Panel Sound application.

How to use MessageBeep?

Start a project. Put a button on it. Make the button's OnClick handler do.

Uses
    Windows; 
....................
procedure TForm1.Button1Click(Sender: TObject);
begin
    MessageBeep(MB_OK);
    MessageBeep(MB_ICONINFORMATION);
    MessageBeep(MB_ICONWARNING);
    MessageBeep(MB_ICONERROR);
    MessageBeep(MB_ICONASTERISK);
    MessageBeep(MB_ICONEXCLAMATION);
    MessageBeep(MB_ICONHAND);
    MessageBeep(MB_ICONSTOP);
End;

Beep vs MessageBeep

Beep can play only default wave sound but MessageBeep can play different kind of wave sound based on utype paramater value. And Beep internally calls MessageBeep with utype=0.

Windows.Beep

We checked a Beep procedure with no parameters in Sysutils unit which indirectly calls MessageBeep Windows API procedure. Now we will see another Beep procedure with parameters and is a winows api procedure. This Beep procedure can play sound for a specific duration with specific frequency.

Windows.Beep(dwFreq:cardinal; dwDuration:cardinal);

How to use the Windows.Beep?

Start a project. Put a button on it. Make the button's OnClick handler do.

Uses
    Windows; 
…………............
procedure TForm1.Button1Click(Sender: TObject);
begin
     Windows.Beep(300,500);
End;

The first parameter (300) sets the frequency of the beep. It can be anything from 37 to 32,767. The second sets the duration of the beep in milliseconds. N.B.: The function is synchronous; it does not return control to its caller until the sound finishes. So don't use this to make long sounds, e.g. more than a second.

SysUtils.Beep vs Windows.Beep

SysUtils.Beep is asynchronous so after queuing the sound, the MessageBeep function returns control to the calling function and plays the sound asynchronously. Where Windows.Beep function is synchronous that generates simple tones on the speaker. The function is synchronous; it performs an wait and does not return control to its caller until the sound finishes.


Comments

  1. Majority of the issues regarding windows 10 are associated with serious damage in system files this lead to potential issues, hence a Factory reset windows is highly recommended.

    ReplyDelete
  2. Thank you for it.
    Now it's easy to use it and select the adequate option.

    ReplyDelete

Post a Comment

Popular posts from this blog

MS Excel Automation in Delphi

ShellExecute in Delphi

Drawing Shapes in Delphi