pranab
08-16-2005, 10:23 AM
I made an alarm SW last month which could play alarm through PC speaker, show DayOfWeek and Date at system tray, etc. I don't like to keep my multimedia speaker turned on whole night and I don't like to hover mouse on the system clock everytime I want to see date/day. I didn't found any commercial alarm software with those functionality so I made it myself. But unfortunetly I lost all code due to hard drive crash :( .
I used the beep API. So it could not sound beep at specified frequencies in Win9x. Now I want to rewrite the code. But this time I want to make it Win9x compatible. So I Googled and found nothing except the Sound() function of Turbo C. Then I've found this Delphi code at
http://delphi.about.com/cs/adptips2003/a/bltip0303_3.htm
I've tried to convert it to VB (with a Delphi manual - I don't know Delphi at all) but failed. It would be great help if anyone can convert it to VB. Or atleast Win32 C++, so that it can be compiled in a Win32 dll and can be used as an API.
~~~~~~~~~~~~~~~~~~~~~~~~~
procedure 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 GetPort(address: Word): Word;
var
bValue: Byte;
begin
asm
mov dx, address
in al, dx
mov bValue, al
end;
GetPort := bValue;
end;
procedure Sound(aFreq, aDelay: Integer) ;
procedure DoSound(Freq: Word) ;
var
B: Byte;
begin
if Freq > 18 then
begin
Freq := Word(1193181 div Longint(Freq)) ;
B := Byte(GetPort($61)) ;
if (B and 3) = 0 then
begin
SetPort($61, Word(B or 3)) ;
SetPort($43, $B6) ;
end;
SetPort($42, Freq) ;
SetPort($42, Freq shr 8) ;
end;
end;
procedure Delay(MSecs: Integer) ;
var
FirstTickCount: LongInt;
begin
FirstTickCount := GetTickCount;
repeat
Sleep(1) ;
//or use Application.ProcessMessages instead of Sleep
until ((GetTickCount - FirstTickCount) >= Longint(MSecs)) ;
end;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Windows.Beep(aFreq, aDelay) ;
end
else
begin
DoSound(aFreq) ;
Delay(aDelay) ;
end;
end;
procedure NoSound;
var
Value: Word;
begin
if not (Win32Platform = VER_PLATFORM_WIN32_NT) then
begin
Value := GetPort($61) and $FC;
SetPort($61, Value) ;
end;
end;
{
// Example:
procedure TForm1.Button1Click(Sender: TObject) ;
begin
Sound(500, 1000) ;
Sound(700, 1000) ;
Sound(900, 1000) ;
NoSound;
end;
}
~~~~~~~~~~~~~~~~~~~~~~~~~
I used the beep API. So it could not sound beep at specified frequencies in Win9x. Now I want to rewrite the code. But this time I want to make it Win9x compatible. So I Googled and found nothing except the Sound() function of Turbo C. Then I've found this Delphi code at
http://delphi.about.com/cs/adptips2003/a/bltip0303_3.htm
I've tried to convert it to VB (with a Delphi manual - I don't know Delphi at all) but failed. It would be great help if anyone can convert it to VB. Or atleast Win32 C++, so that it can be compiled in a Win32 dll and can be used as an API.
~~~~~~~~~~~~~~~~~~~~~~~~~
procedure 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 GetPort(address: Word): Word;
var
bValue: Byte;
begin
asm
mov dx, address
in al, dx
mov bValue, al
end;
GetPort := bValue;
end;
procedure Sound(aFreq, aDelay: Integer) ;
procedure DoSound(Freq: Word) ;
var
B: Byte;
begin
if Freq > 18 then
begin
Freq := Word(1193181 div Longint(Freq)) ;
B := Byte(GetPort($61)) ;
if (B and 3) = 0 then
begin
SetPort($61, Word(B or 3)) ;
SetPort($43, $B6) ;
end;
SetPort($42, Freq) ;
SetPort($42, Freq shr 8) ;
end;
end;
procedure Delay(MSecs: Integer) ;
var
FirstTickCount: LongInt;
begin
FirstTickCount := GetTickCount;
repeat
Sleep(1) ;
//or use Application.ProcessMessages instead of Sleep
until ((GetTickCount - FirstTickCount) >= Longint(MSecs)) ;
end;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Windows.Beep(aFreq, aDelay) ;
end
else
begin
DoSound(aFreq) ;
Delay(aDelay) ;
end;
end;
procedure NoSound;
var
Value: Word;
begin
if not (Win32Platform = VER_PLATFORM_WIN32_NT) then
begin
Value := GetPort($61) and $FC;
SetPort($61, Value) ;
end;
end;
{
// Example:
procedure TForm1.Button1Click(Sender: TObject) ;
begin
Sound(500, 1000) ;
Sound(700, 1000) ;
Sound(900, 1000) ;
NoSound;
end;
}
~~~~~~~~~~~~~~~~~~~~~~~~~