我的循环似乎随机停止。可能是什么问题?
我尝试使用循环的Tone.Event,发生同样的事情。也许,我对时间的理解是通过Tone.Loop和内部Transport.scheduleOnce来解释的。循环可能随时停止,因此如果经过约10秒,请刷新并重新运行该函数,或者让其运行直到Tone.Loop最终停止,但可能会停止。我可以保证。您可以在做其他事情时让它播放,您会知道当声音的音量不再浸入和浸出时循环已经停止。通过将Loop的本质直接绑定到Tone.Context的“ tick”事件,我解决了这个问题。那时一切似乎都按预期运行。不过,我想知道循环失败的原因。
function testTone(freq) {
let interval = 0.5,
maindip = 0.7;
let osc = new Tone.Oscillator(freq || 100,'sine').sync().start(0),
gain = new Tone.Gain(),
postgain = new Tone.Gain(),
panner = new Tone.Panner3D(-3,-3,-7),
freeverb = new Tone.Freeverb(1),
loop = new Tone.Loop((time)=> {
let div = Math.random(0,1),
dip = 1-div*maindip;
gain.gain.value = dip;
postgain.gain.value = dip;
console.log(time);
Tone.Transport.scheduleOnce((time)=> {
console.log('once upon', time);
gain.gain.value = 1;
postgain.gain.value = 1;
}, '+'+(interval*div/2));
}, interval);
freeverb.wet.value = 0.2;
freeverb.dampening.value = 500;
osc.chain(gain, panner, freeverb, postgain, Tone.Master);
loop.start(0);
Tone.Transport.start();
this.osc = osc;
this.panner = panner;
this.gain = gain;
this.loop = loop;
this.postgain = postgain;
this.reverb = freeverb;
}
循环应该永远运行。