0

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.

4

1 回答 1

0

事实证明,我一直在一个函数上设置 BPM,每当我对其初始化进行更改时,它都会被新的 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
}

现在,我将在测试时禁用该行代码。

于 2018-03-11T13:23:09.113 回答