【问题标题】:Javascript loop logic error with Tone.js Tone.Transport.scheduleRepeatTone.js Tone.Transport.scheduleRepeat 的 Javascript 循环逻辑错误
【发布时间】:2019-07-15 07:31:41
【问题描述】:

只是在玩Tone.js 并不太了解Tone.Transport.scheduleRepeat 的细微之处

当多次使用某个功能时,声音开始失真并且时间发生变化。

我正在关注这个tutorial,并尝试在Codepen 中复制。

尽管索引计数器被重置为零,但我的“playScore”函数每次运行时都会发生变化。

<!-- HTML -- >
<button onclick="playScore()">
  TEST SCORE
</button>



//js
function playScore() {
  console.clear();
  console.log("TEST SCORE");
  const synthScore = new Tone.Synth
  synthScore.oscillator.type = 'sine';
  synthScore.toMaster();
  //synth.triggerAttackRelease ('E4', '8n' );  
  const notes = [
    'C4', 'E4', 'G4',
    'C5', 'E5', 'G5' 
  ];

  let index = 0;

  Tone.Transport.scheduleRepeat(time => {
    repeat(time);
  }, "8n");

  function repeat(time) {
    console.log("index: " + index + " notes.length: " + notes.length);
    let note = notes[index % notes.length];
    synthScore.triggerAttackRelease(note, '8n', time);
    console.log("note:" + note + " time: " + time);
    index++;
  }

  Tone.Transport.start();

  setTimeout(() => {
    Tone.Transport.stop();
  }, 5000)

  // Tone.Transport.bpm.value = 120  

我希望以相同的方式以相同的顺序演奏相同的音符。

相反,我看到它在每次迭代中都会发生变化。

我发现这是因为显然,我有 2 个索引变量和逻辑,函数内有局部实例,函数外有全局实例。

【问题讨论】:

    标签: javascript web-audio-api


    【解决方案1】:

    找到了一个简单的解决方案。 按钮功能“playScore”只是启动和停止 Tone.Transport 音符会从它们发出的地方开始拾取,而不是从数组的顶部开始。

    console.clear();
    
      const synthScore = new Tone.Synth
      synthScore.oscillator.type = 'sine';
      synthScore.toMaster();
      //synth.triggerAttackRelease ('E4', '8n' );  
      const notes = [
        'C4', 'E4', 'G4',
        'C5', 'E5', 'G5' 
      ]; 
      let speed = '8n'
      let index = 0;
    
      Tone.Transport.scheduleRepeat(time => {
        repeat(time);
      }, "8n");
    
      let repeat = ( time ) => {
        console.log("index: " + index + " notes.length: " + notes.length);
        let note = notes[index % notes.length];
        //console.log(note)
        synthScore.triggerAttackRelease(note, '8n', time);
        addOutput(note, '8n')  
        index++;
      }  
    
    
    function playScore() {
      console.log("TEST SCORE");
      Tone.Transport.start();
    
      setTimeout(() => {
        Tone.Transport.stop();
      }, 5000);
      // Tone.Transport.bpm.value = 120  
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多