Jump to content
  • Advertisement
Sign in to follow this  
tppramod

Background sound support in C for PC Speaker

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am just finishing a pacman game written in C (not C++). I need to provide some sound support to the game using PC Speaker for which i want to know `how to play a sound in the background' so that the my sprites moves smoothly without waiting for the sound to play at the foreground and then continue. i need some simple code only in C for background sound support for PC speaker. As an example i liked the simple sound supports used in PCMAN by Greg Kuperberg. I am using Turbo C. Please post any codes available. I would be grateful if someone can help. Thanks Pramod Kumar INDIA

Share this post


Link to post
Share on other sites
Advertisement
This seems like a decent tutorial on using the PC-speaker.

Instead of the BASIC-ish IN/OUT functions refered to in the tutorial you need to use TurboC's inportb/outportb functions instead to program IO ports.
This is a direct translation of some of the tutorial's code:
int value;

outportb(0x43, 0xb6);

outportb(0x42, 54);
outportb(ox42, 124);

value = inportb(0x61);
value |= 3;
outportb(0x61, value);
You'll only be able to play simple tones with this method, playing digital samples is a lot more work.

Share this post


Link to post
Share on other sites
thanks, doynax.

Whether the code will work for playing the tones in the background? i am more concerned about this. what methods were used in classic games like paratrooper, pcman etc. any idea? Any source code availble for such games or any detailed documentation for this with sample codes.

thanks

pramod

Share this post


Link to post
Share on other sites
Quote:
Original post by tppramod
Whether the code will work for playing the tones in the background? i am more concerned about this. what methods were used in classic games like paratrooper, pcman etc. any idea? Any source code availble for such games or any detailed documentation for this with sample codes.
That shouldn't a problem.

The tutorial theaches you to begin playing a note at a specific frequency. Your program will regain control without any delay and the tone will continue to play in the background until you stop it manually.

I can't guarantee that it'll work but the following code should play tones at the frequency:
#include <dos.h>

void playTone(unsigned int freq) {
unsigned char reg;
unsigned int period;

period = 1193180UL / freq;

outportb(0x43, 0xb6);
outportb(0x42, period);
outportb(0x42, period >> 8);

reg = inportb(0x61);
reg |= 3;
outportb(0x61, reg);
}

void stopTone(void) {
unsigned char reg;

reg = inportb(0x61);
reg &= ~3;
outportb(0x61, reg);
}


[Edited by - doynax on March 28, 2005 4:50:04 AM]

Share this post


Link to post
Share on other sites
thankx.... i will try this out. but i have not understood the line `reg &= ~3;' what is `~3'?. also have you ever tried the methods given in the said tutorial?

pramod

Share this post


Link to post
Share on other sites
Quote:
Original post by tppramod
thankx.... i will try this out. but i have not understood the line `reg &= ~3;' what is `~3'?. also have you ever tried the methods given in the said tutorial?
reg &= ~3 is equivalent to "VALUE = VALUE AND 252" in the tutorial.

I've used this method before but's been a long time ago since I've last used it.

Share this post


Link to post
Share on other sites
Quote:
Original post by try_catch_this
that code does not compile.
Well, that wouldn't surprise me..
Could you give me any more specific information on the error?

I haven't got Turbo C to compile it with so I can't test it myself.

edit: I found at least one bug. I had managed to use a 0 instead of an O in the prefix of a hexadecimal constant.

Share this post


Link to post
Share on other sites
thanks for the nice help. right now i am in my office. i will incorporate the code and see if it works tomorrow. catch u later. thanks once again.

pramod

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!