-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathrender-to-buffer.js
More file actions
24 lines (18 loc) · 888 Bytes
/
Copy pathrender-to-buffer.js
File metadata and controls
24 lines (18 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Render audio offline to a buffer — no speakers, no I/O.
// Run: node examples/render-to-buffer.js
import { OfflineAudioContext } from 'web-audio-api'
import { init as buildTone } from './graphs/render-to-buffer.js'
import { help } from './utils.js'
help({
description: 'render audio offline into an AudioBuffer',
usage: [''],
notes: ['Renders a two-second stereo 440 Hz tone and prints buffer metadata and samples.'],
})
const sr = 44100
const ctx = new OfflineAudioContext(2, sr * 2, sr) // 2 seconds, stereo
buildTone(ctx, { frequency: 440, duration: 2, gain: 1, when: 0 })
const buffer = await ctx.startRendering()
console.log('Channels:', buffer.numberOfChannels)
console.log('Length:', buffer.length, 'samples')
console.log('Duration:', buffer.duration, 'seconds')
console.log('First 8 samples:', Array.from(buffer.getChannelData(0).slice(0, 8), v => v.toFixed(4)))