HOME - - - - - Delphi Tutorials TOC - - - - - - Other material for programmers

Delphi: How To: Make 'beep' or other sound

This has good information, and a search button at the bottom of the page

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages.

Making a beep....


New and improved!

The next few inches of this page were added in August 08. It gives you a really easy way to get a beep of the frequency and lenght you need. It even gives you an even easier way to get a bog standart beep. (When I added the new improved information, I did not go through the rest of the page and edit it.) The messagebeep stuff at the bottom of the new material is pretty useful, too.

By the way.... make sure your sound system is working before embarking on the following! (Mine wasn't, and it took a little fooling about to get that right.)

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

windows.beep(300,500);

And you're done! (Tested with Delphi 4, likely to work with anything above Delphi 1)

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.)

If you just want a simple, standard, get- your- attention "beep", you can make the handler just say "beep;" No "windows.", no parameters.

What's the "windows" object? I don't know. Haven't seen it before. But it Just Works. You don't haev to "do anything" to use it. My thanks to....

http://www.xtremevbtalk.com/archive/index.php/t-234669.html

... for giving an example of "windows.beep..." which I have re-cycled to bring you the above.

When I started trying to incorporate something a kind reader of my pages sent me, things were going badly. Accessing the Win32 help pages supplied with Delphi 4 (you have to go into the start menu folder for Delphi to get at it) revealled a Windows API call ("beep") that looked like it might be what I'd need. I'd forgotten how to do API calls, and found the following which might be useful to you, should you need to do an API call someday....

http://www.blong.com/Conferences/BorConUK97/WinAPI/Api.htm

... but, for now, if you just want a beep, I think you have what you need? If you are a glutton for web-trawling, and you're still looking for frills of sound-making, perhaps some of the following will be of interest to you....

Main Microsoft Playsound information page...

http://msdn2.microsoft.com/en-us/library/ms712879.aspx

Playsound for System sounds..

http://msdn2.microsoft.com/en-us/library/ms713502.aspx

Using PlaySound to Play Waveform-Audio Files

http://msdn2.microsoft.com/en-us/library/ms713502.aspx

- - - - - - - - - - - - - - - -

Also in my notes for updating this tutorial when I turned to the task in August 08....

I found what may be another answer for Delphi 2, and probably higher. Put....

messagebeep(MB_ICONQuestion);

... into a program, and when the code is executed you'll get whatever sound you have configured your machine to play when it finds something to question. There are other pre-defined sound names that let you get various "standard" sounds. Those settings (Win98) are accessed via Settings | Sounds. (Still around in XP, and probably later, but probably you get to them by a different route.)

Put your cursor on messagebeep when you are in Delphi (an instance of the word in your sourcecode) and hit F1 for a ist of the allowed arguments. The information is in the Win32 Programmer's Reference.


(Below here is the pre-August 08 material... more complicated than the above, but may have useful stuff in the undergrowth! The simple "beep" idea (with opensound) may only work with Delphi 1, as presented. It might be possible to update it to work with the later Delphis. (Delphi 1 was for 16 bit Windows.))



It was so easy in pre-Windows days!

It is easy (within the Windows meaning of the term) again in post Delphi1 days... more on this later.

But what's a poor Delphi1, no soundcard, user to do? Fear not... help is at hand!

I obtained what I needed for this from the excellent 'Waite Group Borland Delphi, How To' book, ISBN 1-57169-019-0. It clearly states that this 'solution' is not very robust. If it doesn't work on your computer don't be too surprised, or waste too much time on it. You may want to check that DEVICE=SOUND.DRV (or similar) is in your CONFIG.SYS file. If you have a soundcard, Delphi2 and Win95, use the better alternatives.

Making a sound on such a system is almost easy....

You can almost just do..

iTmp:=OpenSound;
SetVoiceSound(1,9800000,400);
StartSound;
CloseSound;

Unfortunately, you should...

a) Test iTmp after the first line, and deal with problems.
... and you must

b) Arrange for CloseSound NOT to happen immediately, but TO happen eventually.

These needs are met in the following code. Once it is entered into a program, calling 'Beep' will make a beep on the speaker.

_________________________
Extracted essential bits....

(The 'uses' clause needs nothing special, apart from the usual SysUtils, etc)

Somewhere in the 'type TForm1 = class(TForm)...' declaration, you need to add

procedure Beep;

I put it down at the bottom of the Delphi created declarations. In my case that was after...

procedure FormClose(Sender: TObject; var Action: TCloseAction);

... and after

implementation

{$R *.DFM}
.... you need....
var boSoundVoiceInUse:boolean;

procedure TForm1.FormCreate(Sender: TObject);
begin
boSoundVoiceInUse:=false;
end;

procedure TForm1.Beep;
var iVoice:integer;
const freq=150;(*Sets pitch of tone. MIGHT be APPROX Hertz*)
      dur=350;(*Sets duration of sound. MIGHT be APPROX milliseconds.
         The tiBeep mechanism for calling SoundClose will ensure
         that the sound does not last longer than dur ms. If you
         are getting too short sounds, multiply the reference to
         dur in the call of SetSoundVoice by something suitable.*)
begin
iVoice:=OpenSound;
if iVoice<1 then
  showmessage('Unable to do sounds at this time. '+
  'Either some other program is using the relevant '+
  'resource, or it is not available. (Making it '+
  'available might be more of a pain than it is worth!)');
if iVoice>0 then begin
  SetVoiceSound(iVoice,freq*655360,dur);
  StartSound;
  (*If you simply put CloseSound here, the
  sound will cease virtually immediately... too
  quickly to even hear it start. In 'the old days',
  you could put a loop to waste time here and then
  CloseSound... but in the Windows environment that
  is A Very Bad Idea. MAKING the sound is easy....
  dealing with letting it continue, and then tidying
  up afterwards is the messy bit! The CloseSound will
  happen if..
  i) We close the main form, or
  ii) The tiBeep timer says that the beep's allotted
  time has passed*)
  boSoundVoiceInUse:=true;(*This is needed so that
  we can check in Form1.OnClose that a CloseSound
  isn't needed.*)
  with Form1 do begin
    tiBeep.interval:=dur;
    tiBeep.enabled:=false;
    tiBeep.enabled:=true;
    end;(*with*)
  end; (*iVoice was >0*)
end;

procedure TForm1.tiBeepTimer(Sender: TObject);
(*'How to..' (ISBN1-57169-019-0, pg.546) says
that calling CloseSound won't hurt, even if
sound not open... but such sloppiness is
if the timer is only enabled after an OpenSpound
occurs.*)
begin
CloseSound;
boSoundVoiceInUse:=false;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if boSoundVoiceInUse then CloseSound;
end;

... and that should do it!!

======================================================
On a system with Win98 and Delphi 2 and a soundcard.....

The following worked on the system just described. I expect it would also work in a Win95 machine. This information comes to you from the combined efforts of....

...the also excellent 'Teach Yourself Delphi Two in 21 Days', ISBN 0-672-30863-0
...help from an internet newsgroup, found with www.deja.com
...and from about an hour's exploration of the issues. (Not hastened by several crashes of my machine as I tried trivial things!!!)

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

playsound('C:\windows\media\tada.wav',0,snd_sync)

(The C:\windows\media\tada.wav must describe a file present on your system.)

AND (the book and help file forgot to mention this!!) add mmsystem to the unit's 'Uses' clause. (Without it, I got 'playsound: unknown identifier'.. even though pressing f1 gave me lots of information about this 'unknown'... but the f1 information didn't mention that mmsystem was needed, either!!) Oh well, got there in the end!

If you run the program click the button, the .wav file should play. No joy? Be sure your speaker is switched on, etc! How many hours have we all lost to simple things like that? The Win95+ volume controls never cease to confuse me. Try using some other .wav player to check out that all SHOULD be well, and that any problem is in your Delphi program.

The book also said that

playsound('SystemStart', 0, snd_async or snd_nodefault)

was worth knowing about. Well.. it did work.. sort of. Unfortunately, I closed the form before the sound finished playing, and that froze the system. It wouldn't even respond to ctrl-alt-del, but it did persist in playing and replaying a very short segment of 'The Windows Sound'. Also, I couldn't discover a list of the 'known' sounds that I could use where 'SystemStart' is, but I got indications that they would be highly specific to individual machines... not very useful if you want others to use your software.
To search THIS site....
Click this to search this site without using forms, or just use......
powered by FreeFind
Site search Web search
Be sure to spell the words you are searching for correctly!
The search engine doesn't understand English. Searching for "How do I use repeat" will just return pages with "how", "do", "I", "use", or "repeat".
(Go to my other sites (see bottom of page) and use their search buttons if you want to search them.)...


Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's freeware, shareware page.


Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.


Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....