Tone.js release/stop 采样器

Tone.js release/stop sampler

编辑:更新其他尝试。下面问题的关键是行 sampler.triggerRelease(["A1"], 'now') 不起作用。我尝试过的其他事情包括:

Tone.js 文档的相关部分是 here

不起作用的是 sampler.dispose(),因为它基本上断开了采样器乐器与输出的连接。但是我需要继续使用采样器在关闭后不久重新触发相同的音符。


我正在尝试使用按钮来停止正在播放的 'Sampler' 音符。我认为触发发布阶段会执行此操作(如我下面的代码所示),但这似乎没有任何效果。我试过很多方法。 没有回答我的问题,部分原因是它是针对 'Polysynth' 而不是 'Sampler'。提前致谢!

const readyOverlay = document.querySelector('#readyOverlay')
readyOverlay.addEventListener('click', async () => {
    readyOverlay.style.display = 'none'
    await Tone.start()

    const sampler = new Tone.Sampler({
        urls: {
            A1: "A1.mp3"
        },
        baseUrl: "https://tonejs.github.io/audio/casio/",
        onload: () => {
            sampler.triggerAttackRelease(["A1"], 5);
        }
    }).toDestination();
    sampler.release = 0
    document.querySelector('#stop').addEventListener('click', () => {
        sampler.triggerRelease(["A1"], 'now')
    })
})
        #readyOverlay {
    position: absolute;
    z-index: 9;
    width: 100%;
    height: 100%;
    background: white;
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: center;
}
<script src="https://unpkg.com/tone@14.7.77/build/Tone.js"></script>
    <div id="readyOverlay">Press anywhere to start!</div>
    <button id="stop" >stop</button>

仅用 triggerAttack 替换 triggerAttackRelease 确实有效:

const readyOverlay = document.querySelector('#readyOverlay')
readyOverlay.addEventListener('click', async () => {
    readyOverlay.style.display = 'none'
    await Tone.start()

    const sampler = new Tone.Sampler({
        urls: {
            A1: "A1.mp3"
        },
        baseUrl: "https://tonejs.github.io/audio/casio/",
        onload: () => {
            sampler.triggerAttack(["A1"]);
        }
    }).toDestination();
    sampler.release = 0;
    document.querySelector('#stop').addEventListener('click', () => {
        sampler.triggerRelease(["A1"]);
    })
})
        #readyOverlay {
    position: absolute;
    z-index: 9;
    width: 100%;
    height: 100%;
    background: white;
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: center;
}
<script src="https://unpkg.com/tone@14.7.77/build/Tone.js"></script>
    <div id="readyOverlay">Press anywhere to start!</div>
    <button id="stop" >stop</button>

triggerAttackRelease 只是一系列 triggerAttacktriggerRelease 调用。这意味着您已经在初始示例的 onload 回调中安排了注释发布。

Tone.js 忽略第二次发布调用,因为 sampler 已经将“A1”标记为已发布 (this if statement makes sure note is released only once after the attack)。