Sign in to follow this  
Robinhood

How to play a .wav File through beep function?

Recommended Posts

If you want to play wav files using the PC speaker you have to reprogram the 8254 timer chip (present on all PCs). You will need to hook to the timer interrupt (int 8), which runs at a basee frequency of 18.2Hz. Usig some counters you can trick it into ticking at higher frquencies (required for sampling), though.
I don't remember how it was done exactly (you somehow need to fiddle with the interrupt controller at port 20h and such), but it is not possible without special kernel driver anyway.
Someone wrote a linux driver for that - maybe that will help you.

Good luck,
pat.

Share this post


Link to post
Share on other sites
I mean the API:
BOOL Beep(
DWORD dwFreq,
DWORD dwDuration
);

I want to know the algorithm(maybe DSP)?
I don't expect someone to explain the detail at great length and a link or an article is appreciated.

Share this post


Link to post
Share on other sites
I think the answer is that you don't normally play .wav files that way.

That API tells the speaker to play a sound for a certain time and duration. A .wav file essentially tells the speaker what position it's diaphragm should be at.

You can make beeps easily with a digital sampled (.wav method) using something like:


for(j=0; j<256; ++j) {
for(i=0; i<256; ++i)
write(256);
for(i=0; i<256; ++i)
write(0);
}



Going the other way is much harder - it requires high timing accuracy and probably a nice chunk of processor time (not that much on modern systems though). Unless you're looking at games on a CNC machine, I think you can assume a sound card that can play back digital audio.

Without interrupt level code, you're not going to get this to work reliably.

Share this post


Link to post
Share on other sites
Quote:
Original post by LucidIon
Without interrupt level code, you're not going to get this to work reliably.

Seconded. You won't even get it play something you want to hear at all - the sampling rate is at best 1KHz. The other thing is, that Beep() is not asynchronous, that is it doesn't return the control to the caller unless it finished. While it might be possible to call Beep() in a seperate thread, the required FFT calculations would surely make realtime playback impossible (since Windows isn't a real-time OS).
Just use PlaySound() instead.

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