[英]Tone.js release/stop sampler
EDIT: Update on other attempts.编辑:更新其他尝试。 The key bit of the question below is the line
sampler.triggerRelease(["A1"], 'now')
which is not working.下面问题的关键是行
sampler.triggerRelease(["A1"], 'now')
不起作用。 Other things I've tried include:我尝试过的其他事情包括:
sampler.releaseAll(Tone.now())
sampler.releaseAll()
sampler.triggerRelease(["A1"], Tone.now())
The relevant part of the Tone.js docs is here . Tone.js 文档的相关部分在此处。
What won't work is sampler.dispose()
, because it essentially disconnects the sampler instrument from the output. But I need to continue using the sampler to retrigger the same note shortly after turning it off.不起作用的是
sampler.dispose()
,因为它基本上断开采样器乐器与 output 的连接。但我需要继续使用采样器在关闭后不久重新触发相同的音符。
I'm trying to use a button press to stop the playing of a 'Sampler' note while it's currently playing.我正在尝试使用按钮按下来停止当前正在播放的“采样器”音符的播放。 I thought that triggering the release phase would do this (as in my code below) but this doesn't seem to have any effect.
我认为触发发布阶段会执行此操作(如我下面的代码所示),但这似乎没有任何效果。 I've tried lots of methods.
我试过很多方法。 This question doesn't answer mine, partly because it's for the 'Polysynth' not the 'Sampler'.
这个问题没有回答我的问题,部分原因是它是针对“Polysynth”而不是“Sampler”的。 Thanks in advance!
提前致谢!
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>
Replacing triggerAttackRelease
with just triggerAttack
does work:仅用
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
is just a sequence of triggerAttack
and triggerRelease
calls. triggerAttackRelease
只是一系列triggerAttack
和triggerRelease
调用。 It means you have already scheduled a note release in the onload
callback in your initial example.这意味着您已经在初始示例的
onload
回调中安排了注释发布。
Tone.js ignores the second release call because sampler
has already marked "A1" as released ( this if
statement makes sure note is released only once after the attack ). Tone.js 忽略了第二次发布调用,因为
sampler
已经将“A1”标记为已发布( 此if
语句确保音符仅在攻击后发布一次)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.