Author: William Gerbert
Beep/Sound in Delphi
Answer:
The following assembler Routines implement sound output via port access and work
therefore only with Win3.x and Win95/98. Simply call Sound(hz) with hz as frequency
in Hz, and stop the sound output with NoSound().
If your application will run under Windows NT, you may use the operating system
routine:
1 Windows.Beep(Frequency, Duration);
2
3
4 function InPort(PortAddr: word): byte; assembler; stdcall;
5 asm
6 mov dx,PortAddr
7 in al,dx
8 end;
9
10 procedure OutPort(PortAddr: word; Databyte: byte); assembler; stdcall;
11 asm
12 mov al,Databyte
13 mov dx,PortAddr
14 out dx,al
15 end;
16
17 procedure Sound(Hz: Word);
18 var
19 TmpW: Word;
20 begin
21 OutPort($43, 182);
22 TmpW := InPort($61);
23 OutPort($61, TmpW or 3);
24 OutPort($42, lo(1193180 div hz));
25 OutPort($42, hi(1193180 div hz));
26 end;
27
28 procedure NoSound;
29 var
30 TmpW: Word;
31 begin
32 OutPort($43, 182);
33 TmpW := InPort($61);
34 OutPort($61, TmpW and 3);
35 end;
|