节拍器演奏时计数音符

时间:2019-02-04 15:51:21

标签: javascript web-audio tone.js

以下音频以100每分钟的节拍和4/4的时间签名播放。

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

  • 第一个措施完成时?即第二次出现1 ..通常音乐会被计算为1, 2, 3, 4, 1, 2, 3, 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>

1 个答案:

答案 0 :(得分:1)

由于四分音符是192滴答声,因此您可以使用它来确定其他事件和情况的发生时间:

for file in os.listdir('directory'):
    filename = os.fsdecode(file)
    if filename.startswith('k_0_'):
       continue
(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;
  });
})();

我还修改了播放/暂停的逻辑,而不是安排新的重复,而是从每按一次按钮开始播放就开始。