Sign in to follow this  
DvDmanDT

PC speaker

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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)..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
I already have speakers.. I have an Audigy 2 ZS platinum pro card, with I-Trigue 5.1 speakers.. It's definitly not a project for soundquality or something, but rather educational (plus I think the PC speaker is cool)..

I accutually wrote a small 16-bit DOS program, which allowed you to play the piano (kinda) on your keyboard.. you could even record songs.. And best of it all, it ran on my friends lap (can't remember exact specs, but like 512kb ram, 20mb disk, DOS 3.x, 4-color monitor with like 320x200 res and so on).. Probably doesn't sound very cool, but it was.. :)

So yeah, it's a for fun project, just for the sake of very 1337 sounds..

Share this post


Link to post
Share on other sites
Maybe you can write a very small 16bit application that plays the PC speaker (you run it from command prompt).
Then, in the 32bit app you run this 16bit app hidden.
something like this:
int main (int argc,char* argv[])
{
sound(argv[0]);
delay(argv[1]);
return 0;
}

(untested, but you get the idea. right?)
or you can make the 16bit app to play more than 1 sound, so you won't have to open it for each sound (and you will for each song).
it remains me the PC speaker music player i programmed for my bad-design Pong game.

EDIT: oops, its not that original idea i see..what did you mean by stdin? i dont think you need it if the 16bit app is hidden.

pex.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this