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

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.
   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search
Please click on this to rate this tutorial....

.. and click here, too, if you're feeling really 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.


Why does this site cause a script to run? I have the traffic to this page monitored for me by Jazar Top 200 Delphi, and they provide promotional services, too. Click the "Help get this site publicity" link above for more information.