Click here if you want to know more about the source and format of these pages.
Making a beep....
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.))
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;
Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
....... P a g e . . . E n d s .....