Is it possible to make a metronome in C? If possible, then how would I do it?
Is it possible to make a metronome in C? If possible, then how would I do it?
Code:for(;;) { // make a sound // sleep for a certain amount of time based on tempo }
If you understand what you're doing, you're not learning anything.
Yes.... maybe....
You can time down to one-millisecond using <ctime> (aka time.h).
There is no sound in standard C++, but assuming you're on a Windows system, PlaySound() is probably the easiest way to make a sound.
There are a couple of issues with Windows. Although you can read time with millisecond precision, you do not get millisecond accuracy. The time_t structure only gets updated about every 30 ms (I forget the exact number). And the Windows multitasking also adds to the problem... The CPU may be doing something else just when you need it to make a "tick".
I think these timing errors may be enough to cause problems, but I'm not sure. You can hear a 50ms delay.... For example, if you delay the sound to one speaker it will sound like an echo. But, a 50ms error between beats may not be as noticable.
If the Windows timing issues are a problem, DirectX (DirectSound) or MIDI may provide the solution. (These techniques might be able to hand-off the timing chores to the soundcard/soundchip.)
Thanks for the replies. I'll try to do some work on it.
This is how I would be getting the delay between each ticks.Code:int ms = (60.0f / (float)bpm) * 1000;