Problem with beeps in a Piano

Nov 22, 2011 at 9:34pm
Hey,
I am basically trying to make a Piano sort of thing in C++. I know how to play a certain sound using beeps and their frequencies. But what I want is that when I press a key, the beep goes on until the key is released, which means that I want to vary the time for beep considering the time I hold the key. like It would be something like Beep(261, x) where x would be the time as long as user holds the key...
hope u guyz would be of gr8 help... :)
Nov 22, 2011 at 10:00pm
Sounds like you must Beep for a virtual infinite amount of time when a key is pressed. While key is still pressed, you do nothing, while beeping in correct frequency continues.

Then when user stops pressing key, you should somehow send an interupt to the sound device to stop beeping. Not sure if such a c/c++ function exists, but would asume that such a thing should be possible on the assembler level.
Nov 22, 2011 at 10:00pm
For a console application, you need to do some special processing.

On Windows, use SetConsoleMode() to turn off line buffering.
Then use ReadConsoleInput() in a loop (see my "is key pressed" topic, linked below, for how to create a loop waiting on input).

There are two types of codes sent by the keyboard: the "make" key code when a key is pressed, and the "break" key code when a key is released. The KEY_EVENT_RECORD's bKeyDown field tells you whether it be a press ("make") or release ("break") code.

SetConsoleMode()
http://www.google.com/search?btnI=1&q=msdn+setconsolemode

ReadConsoleInput()
http://www.google.com/search?btnI=1&q=msdn+readconsoleinput

iskeypressed()
http://www.cplusplus.com/forum/beginner/5619/#msg25047

An example program demonstrating the use of these things
(This shows how to respond to the mouse, but you can ignore that stuff)
http://www.cplusplus.com/forum/beginner/5813/#msg25903

Hope this helps.
Topic archived. No new replies allowed.