1

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

  • sampler.releaseAll(Tone.now())
  • sampler.releaseAll()
  • sampler.triggerRelease(["A1"], Tone.now())

Tone.js 文档的相关部分在这里

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


我正在尝试使用按钮来停止播放当前正在播放的“采样器”音符。我认为触发发布阶段会这样做(如下面的代码所示),但这似乎没有任何效果。我尝试了很多方法。这个问题没有回答我的问题,部分原因是它是针对“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>

4

1 回答 1

1

替换triggerAttackRelease为 justtriggerAttack确实有效:

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”标记为已发布(if语句确保注释在攻击后仅发布一次)。

于 2022-02-08T21:56:48.230 回答