• Advertisement

• ### Popular Now

• 10
• 9
• 13
• 10
• 18
• Advertisement
• Advertisement
• Advertisement

# Background sound support in C for PC Speaker

This topic is 4733 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

##### 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

##### 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

##### Share on other sites
Quote:
 Original post by tppramodWhether 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

##### 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

##### Share on other sites
Quote:
 Original post by tppramodthankx.... 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

##### Share on other sites
that code does not compile.

#### Share this post

##### Share on other sites
Quote:
 Original post by try_catch_thisthat 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

##### 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

##### Share on other sites
Is there a standard c++ version of that code that will work?
Who even has turbo c?

#### Share this post

##### Share on other sites

• Advertisement