Javascript Web音频API相互播放注释
我对Javascript比较陌生,我尝试使用Web音频API播放笔记。音符确实在演奏,但不是我想要的。我正在试着让音符在彼此之后播放。我代码中的每个节点都有一个频率数组。然后我使用for循环遍历这个数组,并使用函数Javascript Web音频API相互播放注释,javascript,web-audio-api,Javascript,Web Audio Api,我对Javascript比较陌生,我尝试使用Web音频API播放笔记。音符确实在演奏,但不是我想要的。我正在试着让音符在彼此之后播放。我代码中的每个节点都有一个频率数组。然后我使用for循环遍历这个数组,并使用函数playNote播放频率。我尝试使用超时来在for循环中添加playNote调用的延迟,但这似乎不起作用 class Node { constructor(x, y, radius, color, frequency){ this.x = x t
playNote
播放频率。我尝试使用超时来在for循环中添加playNote调用的延迟,但这似乎不起作用
class Node {
constructor(x, y, radius, color, frequency){
this.x = x
this.y = y
this.radius = radius
this.color = color
this.frequency = frequency
}
}
const player1 = new Node(100, 100, 30, 'blue',[1047])
const player2 = new Node(200, 100, 30, 'red',[1047, 1047])
var listOfNodes = []
listOfNodes.push(player1)
listOfNodes.push(player2)
function playNote(freq) {
oscillator = audioCtx.createOscillator()
gain = audioCtx.createGain()
oscillator.type = 'sine'
oscillator.connect(gain)
oscillator.frequency.value = freq
gain.connect(audioCtx.destination)
oscillator.start(0)
gain.gain.exponentialRampToValueAtTime(0.00001, audioCtx.currentTime + 1)
}
listOfNodes.forEach(node => {
var i;
for (i = 0; i < node.frequency.length; i++) {
setTimeout(function() {
playNote(node.frequency[i])
}, 2000 * i);
}
}
);
类节点{
构造器(x、y、半径、颜色、频率){
这个。x=x
这个。y=y
这个半径=半径
这个颜色
这个频率=频率
}
}
const player1=新节点(10010030,'blue',[1047])
constplayer2=新节点(20010030,‘红色’,[10471047])
var listOfNodes=[]
listOfNodes.push(player1)
listOfNodes.push(player2)
功能播放说明(频率){
振荡器=audioCtx.create振荡器()
增益=audioCtx.createGain()
振荡器类型='sine'
振荡器。连接(增益)
振荡器.频率.值=频率
增益连接(audioCtx.目的地)
振荡器。启动(0)
增益。增益。指数RamptoValueAttime(0.00001,audioCtx.currentTime+1)
}
forEach(节点=>{
var i;
对于(i=0;i
有很多方法可以做到这一点。例如,您可以为每个playNote
设置不同的开始时间。像这样:
function playNote(freq, time) {
oscillator = audioCtx.createOscillator()
gain = audioCtx.createGain()
oscillator.type = 'sine'
oscillator.connect(gain)
oscillator.frequency.value = freq
gain.connect(audioCtx.destination)
oscillator.start(audioCtx.currentTime + time)
gain.gain.exponentialRampToValueAtTime(0.00001, audioCtx.currentTime + 1 + time)
}
for (i = 0; i < node.frequency.length; i++) {
playNote(node.frequency[i], i)
}
但我认为最好的方法是使用现代javascript特性:使用
const audioCtx=new AudioContext();
类节点{
构造器(x、y、半径、颜色、频率){
这个。x=x
这个。y=y
这个半径=半径
这个颜色
这个频率=频率
}
}
const player1=新节点(10010030,'blue',[1047])
constplayer2=新节点(20010030,‘红色’,[10471047])
var listOfNodes=[]
listOfNodes.push(player1)
listOfNodes.push(player2)
功能播放说明(频率){
返回新承诺((解决)=>{
振荡器=audioCtx.create振荡器()
增益=audioCtx.createGain()
振荡器类型='sine'
振荡器。连接(增益)
振荡器.频率.值=频率
增益连接(audioCtx.目的地)
振荡器启动(audioCtx.currentTime)
振荡器停止(audioCtx.currentTime+1)
addEventListener('end',resolve,{once:true});
})
}
异步函数播放(){
for(listOfNodes的常量节点){
for(节点的常量频率。频率){
log(`note with frequency${frequency}start`)
等待播放说明(频率);
log(`note with frequency${frequency}结束`)
}
}
}
play()代码>
function playNote(freq) {
oscillator = audioCtx.createOscillator()
gain = audioCtx.createGain()
oscillator.type = 'sine'
oscillator.connect(gain)
oscillator.frequency.value = freq
gain.connect(audioCtx.destination)
oscillator.start(audioCtx.currentTime)
oscillator.stop(audioCtx.currentTime + 1)
oscillator.addEventListener('ended', () => {playNote(nextFrequency)}, { once: true });
}