Question:
How to make a program on C++ that plays a sound each time i press any button?
2008-05-02 14:13:12 UTC
Hello i need help on C++ programing.
I have to make C++ program witch plays a sound each time someone press any letter on keyboard. Does anybody knows how to do that?
Sounds should be different for example : if you press "A" you hear sound "A" if you press "B" you hear sound "B".
is it better to make it console program or with C++ builder? Can you give me some advices how i should do that and what to start with?

Appreciate you help.
Thank you.
Three answers:
2008-05-02 14:28:40 UTC
Well, I know there is a way, one of my programs have it but its not with me. Umm... well for now try:





void sound( void)

{

//turn the computer speaker on at 500HTz

sound(500);

//delay 400ms

delay(400);

//turn speaker off

nosound();

{
jplatt39
2008-05-02 15:01:01 UTC
Look, the reason standard C/C++ doesn't deal with this sort of thing is because it's so hardware-dependent. And C/C++ tries to discourage that. Darl McBride, who used to head the company that sued among others IBM was quoted in the media today as saying "Linux is Unix. You don't see any separate programming guides for it do you?" (Aside from that any quick search of Amazon.com will show you do see separate programming guides for Linux) the point is that when they are being used as an adjunct to computer science they are supposed to be cross-platform and the ideal is to run the same code on a Playstation 3, a Macintel or Wintel, or a mini or mainframe whit an absolute minimum of recoding.



Nevertheless, and I'm sure this will surprise you, I am not writing this to scold you. I can't give you specific details because you haven't told me what your hardware is -- not least, what your operating system is. At least that and your compiler are essential for a specific answer, because there are hardware-dependent libraries out there for "real coders".



For Windows input (the keyboard) conio.h gives you direct access. Unix/Linux/Mac OS X have ncurses, and there is a clone for Windows called PDCurses. I'm going to assume you're using Windows, therefore directX and link you to a couple of tutorials and a forum thread about programming DirectX sound in sources. Please note. For the most part you will have to download libraries to do it. I'm not on Windows, and I don't write for DirectX at all, so I can't tell you how trustworthy these programs are, but if you want to learn how to do sound I guess you'll have to find out for yourself.
pugyking
2008-05-02 14:38:32 UTC
Easiest is probably to use:



System::Console::Beep(frequency, time);



Frequency is the tone in Hz of the sound,

Time is the duration in miliseconds of the beep.



This uses the beep on your Motherboard rather than playing through your speakers. This way you can beep for a second or so on each keystroke and change the frequency for each key.



If you need help on how to make the sound actually play when you hit a key you'll need to be a bit more specific on what you're doing (is it a WIndows Form or console app, are you entering text into a textbox etc.).



For kicks I put this in a timer once and counted up the frequency, had a little frequency sweep that way.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...