我从基本的whiteenoise开始,我想创建一个延迟,让它像时钟一样,每隔一秒就会出现和关闭。然后我打算用不同的滤波器来改变噪音。
目前我用一个低通滤波器来改变噪音的频率声音。谁能帮帮我下一步,这是我第一次使用堆栈溢出,如果我没有提供足够的信息,对不起。
这是我的代码。
html
<html>
<input type="button" value="Start/Stop" id="StartStop">
<input type="number" min="1000" max="2000" value="1000" id="Filter">
<script>
let context= new AudioContext();
StartStop.onclick = function() {
if (context.state === 'suspended') context.resume();
else context.suspend();
}
context.audioWorklet.addModule('mySound.js').then(() => {
let myNoise = new AudioWorkletNode(context,'noise-generator');
let myFilter = new AudioWorkletNode(context,'lowpass-filter',{parameterData:{frequency:1000}});
Filter.oninput = function() {
myFilter.parameters.get('frequency').value=this.value;
FilterLabel.innerHTML = this.value ;
}
myNoise.connect(myFilter);
myFilter.connect(context.destination);
});
</script>
</html>
javascropt
registerProcessor('noise-generator',class extends AudioWorkletProcessor {
process(inputs, outputs) {
for (let i=0;i<outputs[0][0].length;++i) outputs[0][0][i]=2*Math.random()-1;
return true;
}
});
registerProcessor('gain-processor',class extends AudioWorkletProcessor {
// Custom AudioParams can be defined with this static getter.
static get parameterDescriptors() { return [{name:'gain',defaultValue:0.1}] }
// constructor() { super() } // The super constructor call is required
process(inputs, outputs, parameters) {
const input = inputs[0],output = outputs[0];
for (let channel=0;channel<inputs[0].length;++channel)
for (let i=0;i<input[channel].length;++i) output[channel][i] = input[channel][i] * parameters.gain[0];
return true;
}
});
registerProcessor('lowpass-filter', class extends AudioWorkletProcessor {
static get parameterDescriptors() { return [{name:'frequency',defaultValue:1000,minValue:0}]; }
constructor() {
super();
this.lastOut = 0;
}
process(inputs, outputs, parameters) {
let input = inputs[0],output = outputs[0],coeff;
let frequency = parameters.frequency;
for (let channel = 0; channel < output.length; ++channel) {
let inputChannel = input[channel],outputChannel = output[channel];
coeff = 2 * Math.PI * frequency[0] / sampleRate;
for (let i = 0; i < outputChannel.length; ++i) {
outputChannel[i]=inputChannel[i] * coeff +(1-coeff)*this.lastOut;
this.lastOut=outputChannel[i];
}
}
return true;
}
});
欢迎来到StackOverflow。
首先,我会通过使用内置的 GainNode
和 BiquadFilterNode
(或 IIRFilterNode
)而不是 AudioWorkletNode
来实现这些功能。
为了使噪音每秒都能关闭和开启,我会使用一个循环的AudioBufferSourceNode来创建常规的off-on点击,并将其反馈到 gain
的音频参数 GainNode
. 类似这样(完全没有测试过!)。
// Create buffer that's 1 sec long
let buffer = new AudioBuffer({sampleRate: context.sampleRate, length: context.sampleRate});
let data = buffer.getChannelData(0);
// Set the first few values of the buffer to 1. You'll have to decide how many.
// This is basically a square wave with a short duty cycle.
data[0] = 1;
// Create the source that loops forever.
let tick = new AudioBufferSourceNode(context, {buffer: buffer});
tick.loop = true;
let gain = new GainNode(context, {gain: 0});
tick.connect(gain.gain);
// Start the source now.
tick.start();
// A Biquad filter instead of your worklet. You'll have to choose the parameters.
let filt = new BiquadFilterNode(context);
// Connect up your noise source to the gain node, and connect the gain node
// to the destination:
myNoise.connect(filt).connect(gain).connect(context.destination);
滴答声在短时间内是1,然后就会被添加到 GainNode
. 因此,节点的增益为1位,其余时间为0。 这应用于你的噪声,所以你得到噪声输出的位,然后沉默。
我想这就是你要的。