mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 14:01:42 +03:00
37 lines
778 B
JavaScript
37 lines
778 B
JavaScript
class SweepProcessor extends AudioWorkletProcessor {
|
|
phase = 0;
|
|
position = 0;
|
|
|
|
startFreq = 20;
|
|
endFreq = 20000;
|
|
duration = 10;
|
|
|
|
process(inputs, outputs) {
|
|
const output = outputs[0];
|
|
|
|
const totalSamples = sampleRate * this.duration;
|
|
|
|
for (const channel of output) {
|
|
for (let i = 0; i < channel.length; i++) {
|
|
const progress = this.position / totalSamples;
|
|
|
|
const freq = this.startFreq * Math.pow(this.endFreq / this.startFreq, progress);
|
|
|
|
channel[i] = Math.sin(this.phase) * 0.2;
|
|
|
|
this.phase += (2 * Math.PI * freq) / sampleRate;
|
|
|
|
this.position++;
|
|
}
|
|
}
|
|
|
|
if (this.position >= totalSamples) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
registerProcessor('sweep-processor', SweepProcessor);
|