Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

Tone.Transport doesn't work on subsequent calls to function (Tone.js)

Is anybody here adept at using Transport with the Tone.js library? I have a table of melodies - each one has a play button. Using the Transport, I can have a melody play ONCE, but afterwards NONE of the melodies play. I've tried using Tone.Transport.cancel() after the last note, and that doesn't seem to do anything. I have code that doesn't use the Transport which works fine, but I had to refactor using the Transport because I want to use Tone.Draw since it coordinates DOM manipulation with the Audio Context. Here's the problematic function. Thanks in advance!

async function playMelody(melody) {  
    await Tone.start() 
    console.log("audio is ready") 
    Tone.Transport.bpm.value = 120;
    Tone.Transport.schedule((time) => {
        let t = time
        for (let i = 0; i < melody.length; i++) {
            let note = melody[i]
            if (note[0] !== "rest") {
                if (i == melody.length - 1) {
                    synth.triggerAttackRelease(note[0], Tone.Time(note[1]) - 0.1, t)
                    Tone.Transport.clear()
                    console.log("Transport cancelled")
                } else {
                synth.triggerAttackRelease(note[0], Tone.Time(note[1]) - 0.1, t)
                }
            }
            t += Tone.Time(note[1])       
        }
    }, 0)
    Tone.Transport.start()   
}
question from:https://stackoverflow.com/questions/65648850/tone-transport-doesnt-work-on-subsequent-calls-to-function-tone-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...