Computing.Net > Forums > Programming > Tone/Pitch/Beep in C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Tone/Pitch/Beep in C++

Reply to Message Icon

Name: Pianist
Date: June 14, 2002 at 11:14:19 Pacific
Comment:

Is there a way to get different pitches of the beep in C++ ?

I am a beginner and I want to write a sort of a piano program..

I want it to beep as long as I press and hold a key.

and i want each key to give a different tone. can you guys help me with the start ?



Sponsored Link
Ads by Google

Response Number 1
Name: cup
Date: June 14, 2002 at 11:17:55 Pacific
Reply:

What operating system/compiler are you using?

What you are planning may work if you are using external speakers but PC/Sun Workstation speakers go squeaky if they are sounded for too long.


0

Response Number 2
Name: Pianist
Date: June 14, 2002 at 11:27:37 Pacific
Reply:

I want to write a simple dos program.


0

Response Number 3
Name: Jim
Date: June 14, 2002 at 13:16:12 Pacific
Reply:

Doing this in C++ in DOS is going to be a little bit of a problem. Changing the frequency of the speaker is not very hard--you just "out" the right values to the right I/O ports. I'll go look them up later this afternoon if someone doesn't beat me to it. You might have to do some logarithmic math to calculate the right frequencies, unless you can look them up. The problem is going to be detecting when the key is released. In DOS, this is not a simple thing. You might have to capture the keyboard interrupt and check for key up codes. Not something I'd suggest for a beginner.

However, doing it in Windows is not such a problem. It sends you messages when the key is released.


0

Response Number 4
Name: Jim
Date: June 14, 2002 at 16:18:08 Pacific
Reply:

OK Here's the info on how to turn on/off the speaker, and change the frequency.

To turn on the speaker, you need to do an out to port 0x61, with the two most least significant bits equal to 1.

temp = inportb(0x61);
temp |= 3;
outportb(0x61, temp);

To turn off the speaker, reset the two bits.

temp = inportb(0x61);
temp &= 0xFC;
outportb(0x61, temp);


To change the frequency, you first out a 0xB6 to port 0x43, and then out the period, lsb first, to port 0x42. For example, to change the frequency to 440 hz (middle C), we use the period of 0x0A97.

outportb(0x43, 0xB6);
outportb(0x42, 0x97);
outportb(0x42, 0x0A);

To calculate the period from the frequency, divide 1193046 by the frequency. The frequency of the timer is 0x100000000 per hour. Divide 0x100000000 by 3600 (seconds in an hour) and you get the 1193046 constant. So the period of middle C is 1193046 / 440 which is 2711. Interestingly enough, 1193046 in hex is 0x123456.

For more information on programming the timer chip, go here.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Tone/Pitch/Beep in C++

how do u do a beep sound in c++ www.computing.net/answers/programming/how-do-u-do-a-beep-sound-in-c/3486.html

Piano Program in C++ www.computing.net/answers/programming/piano-program-in-c/14182.html

Pointers in C/C++ www.computing.net/answers/programming/pointers-in-cc/876.html