我的更新运行速度比它应该运行的要慢



我正在尝试创建一个程序,在播放时编写乐谱。我的第一个目标是以每拍64次的速度进行更新(这是我所需要的所有准确性(。然而,当我设置代码时,更新的运行速度比它应该运行的要慢。

我的代码设置为每分钟140拍,但听起来只有每分钟50拍。

我有一个理论,这是一个计算更新之间延迟的问题,但我不知道我做错了什么。以下是我的全部代码:

#include <raylib.h>
#include <iostream>
#include <chrono>
#include <string>
#include "filestuff.h"
Sound metVoices[1];
int metSelectedVoice = 0;
float bpm = 140;
void initSounds() {
using str = std::string;
str v1p = getExeDir() + "\sounds\met voice 1.wav";
metVoices[0] = LoadSound(v1p.data());
}
void update64() {
std::cout << "64th note" << std::endl;
}
void updateBeat() {
//PlaySound is asynchronous
PlaySound(metVoices[metSelectedVoice]);
}
int main() {
InitAudioDevice();
initSounds();
long double delay64;
//bps and beat will clear from memory because they go out of scope
{
long double bps = bpm / 60;
long double beat = 1 / bps;
delay64 = beat / 64;
}
long double update64Timer = delay64;
long double beatTimer = 64;
long double dt = 0;
while (true) {
std::chrono::steady_clock::time_point frameStart = std::chrono::steady_clock::now();
update64Timer -= dt;
if (update64Timer <= 0) {
update64();
beatTimer--;
update64Timer = delay64;
}
if (beatTimer <= 0) {
updateBeat();
beatTimer = 64;
}
dt = std::chrono::duration<long double>(std::chrono::steady_clock::now() - frameStart).count();
}
CloseAudioDevice();
return 0;
}

有人指出,我的代码不安全,而且里面有一些神奇的数字。我计划改进这些东西,但我需要先做点什么。

如果需要更多的信息,我很乐意分享。提前感谢任何帮助我的人!

我发现了我的问题。该代码效率低下,可以跟上每拍64次的脉冲。

最新更新