节拍器播放时数音符

Counting notes while metronome is playing

以下音频以每分钟 100 节拍和 4/4 拍号播放。

记录的秒数和滴答声不断增加。我如何确定:

(function() {
  var isPlaying = false;

  document.getElementById('togglePlayback').addEventListener('click', function() {
    isPlaying = !isPlaying;
    Tone.Transport.bpm.value = 100;
    Tone.Transport.timeSignature = "4/4";
    console.log('Start playing');
    console.log(Tone.Transport.bpm.value);
    console.log(Tone.Transport.timeSignature);

    if(isPlaying) {
      Tone.Transport.scheduleRepeat(function(time){
        console.log('Seconds: ' + Tone.Transport.getSecondsAtTime(time));
        console.log('Ticks: ' + Tone.Transport.getTicksAtTime(time));
      }, "4n");
      Tone.Transport.start();
    }
  });
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/13.7.1/Tone.js"></script>
<div>
  <button id="togglePlayback">Start</button>
</div>
<div id="results"></div>

由于四分音符是 192 个节拍,您可以使用它来确定其他事件和条件的时间:

(function() {
  Tone.Transport.bpm.value = 100;
  Tone.Transport.timeSignature = "4/4";

  Tone.Transport.scheduleRepeat(function(time){
    console.log('Quarter Notes:', Tone.Transport.getTicksAtTime(time) / 192);
  }, "4n");

  var isPlaying = false;

  document.getElementById('togglePlayback').addEventListener('click', function() {
    if (isPlaying) {
      Tone.Transport.pause();
    } else {
      Tone.Transport.start();
    }

    isPlaying = !isPlaying;
  });
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/13.7.1/Tone.js"></script>
<div>
  <button id="togglePlayback">Start</button>
</div>
<div id="results"></div>

我还修改了您播放/暂停的逻辑,而不是安排新的重复播放,并在每按两下按钮时从头开始播放。