防止 BPM 代码与真实节拍器慢慢不同步



我正在研究一个音乐生成器,它将BPM值作为输入,之后它将开始生成一些和弦,低音符,并使用MIDI信号触发鼓VSTi。

为了使一切以每分钟正确的节拍数运行,我使用了一个挂钟计时器,当您点击播放时,时钟从 0 开始,然后开始定期将 1/128 音符计数为"滴答声"。每次函数滴答结束时,我都会通过简单地计算自开始以来的时间契合的滴答数量来检查我们未来有多少滴答声:

class TrackManager {
constructor(BPM) {
this.tracks = ... 
this.v128 = 60000/(BPM*32);
}
...
play() {
this.tickCount = 0;
this.playing = true;
this.start = Date.now();
this.tick();
}
tick() {
if (!this.playing) return;
// Compute the number of ticks that fit in the
// amount of time passed since we started
let diff = Date.now() - this.start;
let tickCount = this.tickCount = (diff/this.v128)|0;
// Inform each track that there is a tick update,
// and then schedule the next tick.
this.tracks.forEach(t => t.tick(this.tickCount));
setTimeout(() => this.tick(), 2);
}
...
}

曲目根据Steps 生成音乐,这些 s 以刻度表示其预期的播放长度(使用.duration作为持久长度指示器,并在播放步数时设置为未来刻度值的.end),播放代码添加对要播放步长的刻度数的更正, 为了确保如果通过的即时报价比预期的多(例如由于复合舍入错误),下一步将播放,但需要更少的即时报价,以保持同步。

class Track {
...
tick(tickCount) {
if (this.step.end <= tickCount) {
this.playProgramStep(tickCount);
}
}
playProgramStep(tickCount) {
// Ticks are guaranteed monotonically increasing,
// but not guaranteed to be sequential, so if we
// find a gap of N ticks, we need to correct the
// play length of the next step by that many ticks:
let correction = this.stopPreviousStep(tickCount);
let step = this.setNextStep();
if (step) {
step.end = tickCount + step.duration - correction;
this.playStep(step);
}
}
stopPreviousStep(tickCount) {
this.step.stop();
return (tickCount - this.step.end);
}
...
}

这工作得相当好,但是在生成的轨道速度中仍然存在一些漂移,在运行单独的节拍器时尤其明显(在我的例子中,鼓模式VSTi,它被告知在哪个BPM下演奏哪种模式,然后做自己的事情)。虽然最初听起来还不错,但大约一分钟后,节拍器播放的 BPM 和生成器运行的 BPM 之间存在轻微但明显的不同步,我不确定这种不同步可能仍然来自哪里。

我本来期望在tick级别(对于120 BPM小于16ms)进行最微妙的不同步,这远低于明显的水平,但是代码中似乎留下了一个复合的不同步,我不确定它会在哪里。时钟周期是从系统时钟生成的,所以我不希望在 JS 遇到Date.now()的不稳定整数值之前开始不同步,我们不会再遇到 285 年左右的千年

什么可能仍然导致不同步?

事实证明,this.v128的计算仍然会导致引入漂移的值。例如,120 BPM 产生 15.625 毫秒的每个即时报价,这是相当可靠的,但 118 BPM 产生 15.889830508474576271186440677966[...]毫秒/分时,任何四舍五入(到任意数量的有效数字)最终都会产生越来越不正确的tickCount计算。

这里的解决方案是保留即时报价计算整数中涉及的所有值,方法是将this.v128值替换为this.tickFactor = BPM * 32;,然后将tick()函数更改为计算tickCount

tick() {
if (!this.playing) return;
// Compute the number of ticks that fit in the
// amount of time passed since we started
let diff = Date.now() - this.start;
// first form a large integer, which JS can cope with just fine,
// and only use division as the final operation.
let tickCount = this.tickCount = ((diff*this.tickFactor)/60000)|0;
// Inform each track that there is a tick update,
// and then schedule the next tick.
this.tracks.forEach(t => t.tick(this.tickCount));
setTimeout(() => this.tick(), 2);
}

最新更新