我用 Codepen 写了一个简单的合成器。它实际上可以正常工作,并且可以按照我的预期进行(在 Codepen 上和网站上的调试模式下)。不幸的是,它在我的本地机器上不起作用:如果我下载代码并尝试运行它,它会给我在问题标题中提到的错误。
如果我将代码“硬拷贝”到浏览器控制台并运行它,它会完美运行。
这是我的完整应用程序,只要您将鼠标悬停在带有相应音符的垫子上,它就会播放一些合成声音: https ://codepen.io/mattiasu96/pen/pxPXpB
//Simple definition of some filter and oscillator to create a composite sound
------------------------------------------
var filter = new Tone.AutoFilter(2,40,2.5).toMaster();
filter.depth=1;
filter.wet=1;
filter.filter.rolloff=-48;
filter.start();
var synth = new Tone.Synth({
oscillator: {
type: 'sawtooth',
},
envelope: {
attack: 0.001,
decay: 0.1,
sustain: 5,
release: 0.1
}
}).connect(filter);
var synthB = new Tone.Synth({
oscillator: {
type: 'sawtooth',
detune : -10 ,
},
envelope: {
attack: 0.001,
decay: 0.1,
sustain: 5,
release: 0.1
}
}).connect(filter);
var synthC = new Tone.Synth({
oscillator: {
type: 'sawtooth',
detune : +10 ,
},
envelope: {
attack: 0.001,
decay: 0.1,
sustain: 5,
release: 0.1
}
}).connect(filter);
var synthD = new Tone.Synth({
oscillator: {
type: 'sawtooth',
detune : -5 ,
},
envelope: {
attack: 0.001,
decay: 0.1,
sustain: 5,
release: 0.1
}
}).connect(filter);
var synthE = new Tone.Synth({
oscillator: {
type: 'sawtooth',
detune : -2 ,
},
envelope: {
attack: 0.001,
decay: 0.1,
sustain: 5,
release: 0.1
}
}).connect(filter);
var mode = 0; // 0 = modalità single note, 1 = modalità multiple notes
notes = document.querySelectorAll(".hex");
----------------------------------------
// Triggering the notes using the pads i have created
notes.forEach(function(note) {
note.addEventListener("mouseover", function(event){
x = event.target.title;
filter.baseFrequency=x;
synth.triggerAttack(x);
synthB.triggerAttack(x);
synthC.triggerAttack(x);
synthD.triggerAttack(x);
synthE.triggerAttack(x);
});
});
notes.forEach(function(note) {
note.addEventListener("mouseout", function(event){
x = event.target.title;
synth.triggerRelease();
synthB.triggerRelease();
synthC.triggerRelease();
synthD.triggerRelease();
synthE.triggerRelease();
});
});
正如我上面提到的,这一切都很好,但不是在本地。该代码实际上是正确的,因为它在 Codepen 上运行良好,并且如果我在浏览器调试控制台中硬拷贝它,它就可以运行。
我不知道为什么我会收到这个错误。