Mirror

Beep/Sound in Delphi (Views: 756)


Problem/Question/Abstract:

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:

Windows.Beep(Frequency, Duration);


function InPort(PortAddr: word): byte; assembler; stdcall;
asm
  mov dx,PortAddr
  in al,dx
end;

procedure OutPort(PortAddr: word; Databyte: byte); assembler; stdcall;
asm
  mov al,Databyte
  mov dx,PortAddr
  out dx,al
end;

procedure Sound(Hz: Word);
var
  TmpW: Word;
begin
  OutPort($43, 182);
  TmpW := InPort($61);
  OutPort($61, TmpW or 3);
  OutPort($42, lo(1193180 div hz));
  OutPort($42, hi(1193180 div hz));
end;

procedure NoSound;
var
  TmpW: Word;
begin
  OutPort($43, 182);
  TmpW := InPort($61);
  OutPort($61, TmpW and 3);
end;

<< Back to main page