function TPCSpeaker.GetPort(address: Word): Word;
var
bValue: Byte;
begin
asm
mov DX, address
in AL, DX
mov bValue, AL
end;
result := bValue;
end;
procedure Tpcspeaker.Button1Click(Sender: TObject);
begin
play(1800,100);//由这两个参数控制音调
end;
procedure Tpcspeaker.Button2Click(Sender: TObject);
begin
NoSound;
end;
end.
来自:whb_li 时间:00-10-26 0:17:21 ID:376428
to terry_lzs: 打扰,我用你的代码调试了一下,发现
asm
mov DX, address
in AL, DX /////这一行出错
mov bValue, AL
end;
--------------------------------------------------------------------------------
来自:terry_lzs 时间:00-10-26 8:27:07 ID:376539
to whb_li:我在我的电脑上用delphi 4和delphi 5都试过了,没有问题。
如果你是把网页上的直接拷贝到unit上请注意删除一些不可见的字符。
我以前有遇到这种问题的。
--------------------------------------------------------------------------------
来自:pino 时间:00-11-21 9:49:23 ID:399668
在nt下不能直接对端口操作,可以用api messagebeep
而在win9x下就不能用api,可以在代码里面根据系统选择实现方式
来自:xjktthq 时间:00-11-21 9:57:09 ID:399678
for i:=9 to 20 do
windows.beep(i*130,20);
procedure pcSound(dFreq:Double;wLong:Word);export;
var
wFreq:word;
begin
wFreq := round(1192576/dFreq);
asm
push ax
push bx
push cx
push dx
push di
mov al,0B6H
out 43H,al
mov dx,12H
mov ax,wFreq
out 42H,al
mov al,ah
out 42H,al
in al,61H
mov ah,al
or al,3
out 61H,al
@wait1:
mov cx,wLong
@delay:
dec cx
jnz @delay
dec bx
jnz @wait1
mov al,0B6H
out 43H,al
mov al,ah
out 61H,al
pop di
pop dx
pop cx
pop bx
pop ax
end;
end;
exports
pcSound;
begin
end.
* ------------------------------ 调用的例子 ------------------
unit Unit1;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
DLLName:string;
Buffer:array[0..127] of Char;
LIB:THandle;
pcSound:procedure(dFreq:Double;wLong:Word);
begin
DLLName := 'SpeakSound.dll';
StrPCopy(Buffer,DLLName);
Lib := LoadLibrary(Buffer);
if not(Lib=0) then
try
pcSound := GetProcAddress(Lib,'pcSound');
pcsound(440.0,1000);
Finally
FreeLibrary(Lib);
end
else
Showmessage('failed');
end;
end.
{
261.7 c
293.7 d
329.6 e
349.2 f
392.0 g
440.0 a
493.9 b
}
--------------------------------------------------------------------------------
来自:悲酥清风 时间:01-2-6 16:23:18 ID:450522
AHM2000 Freeware 中有个PcSpeaker控件,带源码的,源码在AHM2000的主叶上有!
来自:Iknow 时间:01-2-6 17:36:31 ID:450601
^_^ 连cdefgab都出来了. 音乐功底不错嘛.
{* Program : PCSpk.Pas
Purpose : TPCSpeaker Component
Author Version Last Changed Comments
------ ------- ------------ --------
Song Weng Sam 1.01 Aug. 28, 96 Initial Release (Version set to 1.01
to match the 16bit version which is
currently 1.01 too.)
*}
interface
uses
Classes, WinProcs, Forms;
type
TPCSpeaker = class(TComponent)
private
{ Private declarations }
procedure NoSound;
procedure Sound(Freq: Word);
procedure SetPort(address, value: Word);
function GetPort(address: Word): Word;
protected
{ Protected declarations }
public
{ Public declarations }
procedure Delay(MSecs: Integer);
procedure Play(Freq: Word; MSecs: Integer);
procedure Stop;
published
{ Published declarations }
end;
procedure Register;
implementation
procedure TPCSpeaker.NoSound;
var
wValue: Word;
begin
wValue := GetPort($61);
wValue := wValue and $FC;
SetPort($61, wValue);
end;
procedure TPCSpeaker.Sound(Freq: Word);
var
B: Word;
begin
if Freq > 18 then begin
Freq := Word(1193181 div LongInt(Freq));
B := GetPort($61);
if (B and 3) = 0 then begin
SetPort($61, B or 3);
SetPort($43, $B6);
end;
procedure TPCSpeaker.Delay(MSecs: Integer);
var
FirstTickCount : LongInt;
begin
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages; {allowing access to other controls, etc.}
until ((GetTickCount-FirstTickCount) >= LongInt(MSecs));
end;
procedure TPCSpeaker.Play(Freq: Word; MSecs: Integer);
begin
Sound(Freq);
Delay(MSecs);
NoSound;
end;
procedure TPCSpeaker.Stop;
begin
NoSound;
end;
procedure TPCSpeaker.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 TPCSpeaker.GetPort(address: Word): Word;
var
bValue: Byte;
begin
asm
mov DX, address
in AL, DX
mov bValue, AL
end;
result := bValue;
end;
procedure Register;
begin
RegisterComponents('Sun', [TPCSpeaker]);
end;
procedure TPCSpeaker.Delay(MSecs: Integer);
var
FirstTickCount : LongInt;
begin
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages; {allowing access to other controls,
etc.}
until ((GetTickCount-FirstTickCount) >= LongInt(MSecs));
end;
procedure TPCSpeaker.Play(Freq: Word; MSecs: Integer);
begin
Sound(Freq);
Delay(MSecs);
//NoSound;
end;
procedure TPCSpeaker.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;
procedure Sound(Freq:Integer);
var
p:Integer;
begin
P:=1190000 div Freq;
asm
mov al,10110110B
out 43h,al
mov ax,P
out 42h,al
mov al,ah
out 42,al
in al,61h
mov ah,al
or al,03h
out 61h,al
end;
end;