-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathworklet.js
More file actions
33 lines (26 loc) · 1.12 KB
/
Copy pathworklet.js
File metadata and controls
33 lines (26 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// AudioWorklet: custom processor that generates white noise.
// Run: node examples/worklet.js
import { OfflineAudioContext, AudioWorkletNode } from 'web-audio-api'
import { init } from './graphs/worklet.js'
import { args, num, help } from './utils.js'
help({
description: 'render white noise from a custom AudioWorkletProcessor',
usage: ['', 'gain=0.5'],
options: [['gain=<0..1>', 'peak of the amplitude parameter automation (default: 0.18)']],
notes: ['Renders one second offline and prints RMS measurements through an automated fade.'],
})
let { $ } = args()
let gain = num($('gain', 0.18))
const sr = 44100
const duration = 1
const ctx = new OfflineAudioContext(1, sr * duration, sr)
await init(ctx, { duration, gain, when: 0, AudioWorkletNodeClass: AudioWorkletNode })
let buf = await ctx.startRendering()
let data = buf.getChannelData(0)
// Show amplitude at different points
for (let t of [0, 0.15, 0.5, 0.85, 0.99]) {
let i = Math.floor(t * sr)
let block = data.slice(i, i + 256)
let rms = Math.sqrt(block.reduce((s, v) => s + v * v, 0) / block.length)
console.log(`t=${t.toFixed(2)}s RMS=${rms.toFixed(3)}`)
}