PC speaker

Started by
10 comments, last by pex22 18 years, 7 months ago
Hello everone... How can I use the PC speaker (in a 32 bit program)? The sound() function seems to have dissapeared years ago (no big surprise here).. I suspect I'll have to use assembly to use it, and that's ok, but I have no idea what to do in order to use it.. All I can find is VERY old delphi code to use it.. So, any help? [edit] I posted in the wrong forum before, I intended to post it in general programming
Advertisement
The PC speaker lives at port 0x80 (or is that 0x81 ? it's been so long ago).

The problem is that the port space is protected, and can't be accessed by a 32-bit program that's not running in kernel mode. Thus, you have to write a device driver that lets you twiddle this port -- or perhaps use one that's already written.

The specifics of that depend on what OS you're targeting, of course.
enum Bool { True, False, FileNotFound };
Damn, that was what I suspected.. It is avaible to 16 bit programs though.. So that 16-bit emulator functionality of the cpu must be able to use it.. Do you know if I by any chance can enter that mode in a code block inside my exe? Probably not.. Maybe I can create a 16-bit exe file and pipe it from my windows exe, and send everything needed to stdin.. :p
You can use the Beep function if you're using windows.
Quote:Original post by Kamikaze15
You can use the Beep function if you're using windows.


"The function is synchronous; it performs an alertable wait and does not return control to its caller until the sound finishes."

Don't think thats what he wants.
-Scott
Yeah, I know that... But I couldn't find any better when I needed it too...
I just did a trick, everytime I wanted to Beep away I spawned a new thread that would self-destruct when unneeded.

kinda like:
struct MYBEEPS{   DWORD dwFreq;   DWORD dwDur;   HANDLE hThread;};DWORD WINAPI myBeepThread( void* pPrivateData ){   MYBEEPS *pData = (MYBEEPS*)pPrivateData;   Beep( pData->dwFreq, pData->dwDur );   CloseHandle( pData->hThread );   delete pPrivateData;   ExitThread( 0 );   return 0;}void myBeep( DWORD dwFreq, DWORD dwDur ){   MYBEEPS *pData = new MYBEEPS;   if( !pData )      return;   pData->dwFreq = dwFreq;   pData->dwDur = dwDur;   pData->hThread = CreateThread( NULL, 0, myBeepThread, (void*)pData, 0L, NULL );   if( !(pData->hThread) )      delete pData;}


I know it's kinda ugly, but it works [smile]

[edit] Corrected some stuff
[edit2] hacked a little to fix memory leaks (hopefully it should work ok)
In 32-bit mode you will need to write a driver to access the PC speaker. Maybe you can find a driver online that makes the PC Speaker look like a sound cards...?

I would guess that the 16-bit virtual machine in windows (2000/XP atleast) would either emulate port 0x80 with your sound card or just ignore it...
Trust me, it doesn't.. Download borlands Turbo C(++), and try the sound function.. It'll use real PC speaker.. Guess I'll have to forget about that though (I wanted to create a ringtone/midi player, that didn't require a soundcard/speakers)..
I wrote a PC speaker driver for Xenix/286... about 15 years ago.. Ahh good times! Sorry I had to just share that I apologize for it being non-relevant.
Why do you want to use the PC speaker instead of, say, the audio system? Many PCs these days just redirect the 'PC Speaker' to the audio system anyways.

Edit: NVM didn't see your last post. Really, I can't see why you'd want to do such a thing when you could just go to the nearest dollar store and get some speakers for $1. I guess the novelty might be cool, especially if you didn't limit it to midi (it isn't difficult to simulate PCM sounds on the PC speaker - look at all the DOS games that did it)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement