超级难题(100分)

  • 主题发起人 主题发起人 lfq
  • 开始时间 开始时间
L

lfq

Unregistered / Unconfirmed
GUEST, unregistred user!
请问在win95、98下如何让机器的喇叭发声?
我用api函数BOOL Beep(DWORD dwFreq,DWORD dwDuration)在NT下运行的很好,
但在WIN95、98下则不起作用,(似乎老一点机器可以),请问如何解决?
若用汇编,如何编码?
请高手教我!
 
对计时器编程就可以了
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure Play(Freq: Word; MSecs: Integer);
procedure SetPort(address, value: Word);
function GetPort(address: Word): Word;
procedure NoSound;
procedure Sound(Freq: Word);
procedure Delay(MSecs: Integer);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
play(3000,70); //改变两个参数可以改变音调
nosound;
end;

procedure TForm1.NoSound;
var
wValue: Word;
begin
wValue := GetPort($61);
wValue := wValue and $FC;
SetPort($61, wValue);
end;

procedure TForm1.Sound(Freq: Word);
var
B: Word;
begin
if Freq > 18 then begin
Freq := Word(1193181 div LongInt(Freq));
//B := GetPort($61);
B := GetPort($61);
if (B and 3) = 0 then begin
SetPort($61, B or 3);
SetPort($43, $B6);
end;
SetPort($42, Freq);
SetPort($42, (Freq SHR 8));
end;
end;

procedure TForm1.Delay(MSecs: Integer);
var
FirstTickCount : LongInt;
begin
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages;
until ((GetTickCount-FirstTickCount) >= LongInt(MSecs));
end;

procedure TForm1.Play(Freq: Word; MSecs: Integer);
begin
Sound(Freq);
Delay(MSecs);
end;

procedure TForm1.SetPort(address, value: Word);
var
bValue: Byte;
begin
bValue := trunc(value and 255);
asm
mov DX, address
mov AL, bValue
out DX, AL
end;
end;

function TForm1.GetPort(address: Word): Word;
var
bValue: Byte;
begin
asm
mov DX, address
in AL, DX
mov bValue, AL
end;
result := bValue;
end;

end.
 
多人接受答案了。
 
后退
顶部