一键弹奏 tone.js 中的两个音符

Playing two notes in tone.js on one button click

我正在开发一款网页游戏,点击一个按钮可以连续播放两个音符。我可以使用上传的音频文件和 audio.onended 回调来做到这一点。

function playMusic(){
    audio.src = music_array[music_counter++];
    audio.play();
    audio.onended = function(){
        playMusic();
    };
}

我可以使用上面的代码块来播放整首歌曲。但是,使用这种方法我需要事先将所有音频文件上传到 html,我认为这不是一个好方法。所以,出于同样的目的,我正在研究 tone.js,但我无法一个接一个地演奏音符。我试过像这样使用 for 循环:

music = ["C4;4n", "D4;4n", ...... , "G5;8n"];
for(var i=0; i<range; i++){
    parts = music[music_counter++].split(';');
    synth.triggerAttackRelease(parts[0], parts[1]);
}

for(var i=0; i<range; i++){
    playNote();
}

playNote(){
    parts = music[music_counter++].split(';');
    synth.triggerAttackRelease(parts[0], parts[1]);
}

运气不好。我也查看了他们的文档和其他 Whosebug 帖子。我一直无法找出解决方案。有人知道如何实现吗?

终于找到方法了

var synth = new Tone.Synth().toMaster();

var music = [{"time": 0, "note": "A4", "duration": "16n"},
         ......
         {"time": 23.5, "note": "A4", "duration": "8n"},
         {"time": 24, "note": "G4", "duration": "4n"}];

function playMusic(){
    var part = new Tone.Part(function(time, note){
        //the notes given as the second element in the array
        //will be passed in as the second argument
        console.log(note);
        synth.triggerAttackRelease(note.note, note.duration, time);
    }, music).start(0);

    Tone.Transport.start();
}