Reputation: 395
I'm trying to build a steady metronome that ticks every beat, but it seems like there is a problem.
How long the metronome waits for each beat is determined by this formula: 60000 / BPM
But, the value seems to return a specific number no matter what value you plug into BPM.
I have a property that returns the value of this formula, along with a bpm integer:
static private int bpm = 125;
static private int BeatLength
{
get
{
return 60000 / bpm;
}
}
static public int beat = 0;
And here is the function that's responsible for the metronome (it runs on a dedicated thread):
public static void RhythmUpdate()
{
lock (threadLock)
{
while (true)
{
Thread.Sleep(BeatLength); // Sleep until the next beat
AddRequest(BeatLength * beat);
beat++;
DefaultSounds.def_CowBell.Play();
OnBeat?.Invoke();
}
}
}
When breakpointing the def_CowBell.Play();
, Visual Studio notes that it takes around 600ms to loop. This is how I know the value.
Extra Info:
I'm using OpenGL
Maybe some more if asked...
I appreciate your help in advance.
Upvotes: 0
Views: 100
Reputation: 395
It turns out that I've been setting BPM on a function, and whenever I made a change to it's initialization it would be overwritten by that new BPM.
public static void StartBeat(int startingBPM)
{
rhythmThread = new Thread(new ThreadStart(RhythmUpdate)); // Initialize new thread
bpm = startingBPM; // < This piece of codde was the source.
rhythmThread.Name = "BeatThread";
rhythmThread.Start(); // Start the thread
}
For now, I'll disable that line of code when testing.
Upvotes: 0