-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
93 lines (73 loc) · 2.37 KB
/
test.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import t from 'https://unpkg.com/tst@6.1.1/index.js'
import createWriter from './dist/web-audio-write.min.js'
let interaction = new Promise((resolve) => {
console.log('Click to start tests')
document.addEventListener('click', resolve)
document.addEventListener('keydown', resolve)
document.addEventListener('touchstart', resolve)
})
t('basic', async (t) => {
await interaction
const context = new AudioContext()
const write = await createWriter(context.destination)
const FRAME = 1024, N = 20, CHANNELS = 2
let consumed = 0
let generators = [
createGenerator(context.sampleRate, t => Math.sin(440 * t * Math.PI * 2)),
createGenerator(context.sampleRate, t => Math.sin(440 * t * Math.PI * 2))
]
for (let i = 0; i < N; i++) {
let data = new Float32Array(FRAME * CHANNELS)
generators[0](data.subarray(0, FRAME))
generators[1](data.subarray(FRAME))
await write(data).then(e => (consumed += e.data))
}
await write(null)
t.is(consumed, FRAME * N, 'consumed number of samples is purrfæct')
t.throws(() => {
write([])
})
});
t('write AudioBuffer', async (t) => {
await interaction
var context = new AudioContext()
var write = createWriter(context.destination);
var buf = new AudioBuffer({ sampleRate: context.sampleRate, numberOfChannels: 2, length: 1024 * 10 });
for (let c = 0; c < buf.numberOfChannels; c++) for (let i = 0; i < buf.length; i++) { buf.getChannelData(c)[i] = Math.random() * 2 - 1 }
write(buf);
await write(null);
t.end()
});
t('chain of sound processing', async (t) => {
await interaction
var context = new AudioContext()
var panner = context.createStereoPanner();
panner.pan.value = -1;
panner.connect(context.destination);
var write = createWriter(panner);
const N = 20, FRAME = 1024, CHANNELS = context.destination.channelCount
let generators = [
createGenerator(context.sampleRate, t => Math.sin(440 * t * Math.PI * 2)),
createGenerator(context.sampleRate, t => Math.sin(440 * t * Math.PI * 2))
]
for (let i = 0; i < N; i++) {
let data = new Float32Array(FRAME * CHANNELS)
generators[0](data.subarray(0, FRAME))
generators[1](data.subarray(FRAME))
if (i > N/2) panner.pan.value = 1
await write(data)
}
await write(null)
t.end()
});
function createGenerator (sampleRate, fn) {
let c = 0
return (arr) => {
for (let i = 0; i < arr.length; i++) {
let t = c / sampleRate
arr[i] = fn(t)
c++
}
return arr
}
}