我从基本的白噪声开始,我想创建一个延迟,让它像时钟一样在每一秒之后开始和关闭。然后,我将使用不同的滤波器来调整噪声以改变噪声。
目前我有一个低通滤波器的噪声,以改变噪声的频率声音。有人能帮我完成下一步吗?这是我第一次使用堆栈溢出,如果我没有给出足够的信息,很抱歉。
下面是我的代码:
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;
}
});发布于 2020-05-01 23:43:17
欢迎使用StackOverflow。
首先,我会使用内置的GainNode和BiquadFilterNode (或IIRFilterNode)而不是AudioWorkletNode来实现它们,从而降低复杂性。
为了每秒关闭和打开噪音,我会使用循环AudioBufferSourceNode来创建常规的关闭/打开点击,并将其输入到GainNode的gain AudioParam中。类似这样的东西(完全未经测试!):
// 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。这是应用于你的噪声,所以你得到噪声输出一小段时间,然后静默。
我想这就是你想要的。
https://stackoverflow.com/questions/61529487
复制相似问题