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

Lazarus (Delphi): OLDER VERSION OF... 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....


What's on the page you are reading is dated material. If you haven't seen my other page on Beeps yet, please go straight to the NEW VERSION of this tutorial on making sounds with Lazarus programs! (Or Delphi) (There are links from there to bring you back here, if, in the end, you need it.)

This version was started before 2001! And before my 11/21 "pruning" had not had any significant editing since 2008.

I have left this cut-down version online as an additional resource for anyone who might want it, to supplement my more recent page on the subject. I hope the more recent page is comprehensive and sufficient. (Basically, I just hate throwing anything away. Writing this was a lot of work!)

Make sure your sound system is working and turned on before embarking on the following! (Mine wasn't, and it took a little fooling about to get that right.) Go to YouTube or similar. Make sure the sounds you expect to hear are coming out of a "known good" app. (That may seem obvious. But TWICE when working on making sounds, I've wasted time because my sound was not turned on. The apps were sending sounds to the right places... but my sound was either muted, or turned off... which is almost never its state, in my case. Hence the time it took to consider if it was. Mr. Murphy never sleeps. The fact that my sound system is very rarely muted led me to assume things. Assumptions are, of course, often the source of unnecessary problems. But do I ever learn?

Beyond "Beep"

The earliest version of these pages began when I started trying to incorporate something a kind reader of my pages sent me, and things were going badly. Accessing the Win32 help pages supplied with Delphi 4 (you had to go into the start menu folder for Delphi to get at it) revealed 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

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... (The page documents the Windows PlaySound function, and there's also a link to it in the later version of the page you are reading.)

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 list 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 Delphi 1 days... more on this later.

But what's a poor Delphi 1, 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, Delphi 2 and Win95, use the better alternatives. ((Note from 2021: A citation of something in an ink-on-paper source! How quaint!... but you DID get coherent, well organized guides in that form, in those days. Sigh.))

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

In addition, 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!!

======================================================


Ha! It appears that I already "knew" about PlaySound, when I reworked this page in November 2021. I wish I'd seen this bit... from not later than 2008!... when I was struggling with all of this again n 2011! I haven't edited the following. And it's only here to give us both a chuckle...

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!

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


Test for valid HTML Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. It passes in some important ways, but still needs work to fully meet HTML 5 expectations. (If your browser hides your history, you may have to put the page's URL into the validator by hand. Check what page the validator looked at before becoming alarmed by a "not found" or "wrong doctype".)

AND tested for  Test for valid CSS


One last bit of advice: Be sure you know all you need to about spyware.

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