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;
|
|
Click here to visit editor's freeware, shareware page.