|
1 | 1 | import t, { is, ok, almost } from 'tst' |
2 | | -import encode, { streamEncoder, fmt, channels, norm, merge } from './audio-encode.js' |
| 2 | +import encode from './audio-encode.js' |
3 | 3 | import decode from 'audio-decode' |
4 | | -// --- test utilities --- |
5 | | - |
6 | | -function sine(sampleRate = 44100, freq = 440, duration = 1) { |
7 | | - let n = sampleRate * duration |
8 | | - let data = new Float32Array(n) |
9 | | - for (let i = 0; i < n; i++) data[i] = Math.sin(2 * Math.PI * freq * i / sampleRate) |
10 | | - return [data] |
11 | | -} |
| 4 | +import AudioBuffer from 'audio-buffer' |
12 | 5 |
|
13 | 6 | function rms(arr) { |
14 | 7 | let sum = 0 |
15 | 8 | for (let i = 0; i < arr.length; i++) sum += arr[i] * arr[i] |
16 | 9 | return Math.sqrt(sum / arr.length) |
17 | 10 | } |
18 | 11 |
|
19 | | -// decode lena wav to get reference audio |
| 12 | +function sine(sr = 44100, freq = 440, dur = 1) { |
| 13 | + let n = sr * dur, d = new Float32Array(n) |
| 14 | + for (let i = 0; i < n; i++) d[i] = Math.sin(2 * Math.PI * freq * i / sr) |
| 15 | + return [d] |
| 16 | +} |
| 17 | + |
20 | 18 | let lenaPCM |
21 | 19 | async function getLena() { |
22 | | - if (!lenaPCM) { |
23 | | - let wav = (await import('audio-lena/wav')).default |
24 | | - lenaPCM = await decode(wav) |
25 | | - } |
| 20 | + if (!lenaPCM) lenaPCM = await decode((await import('audio-lena/wav')).default) |
26 | 21 | return lenaPCM |
27 | 22 | } |
28 | 23 |
|
29 | | -// --- core helpers --- |
30 | | - |
31 | | -t('channels', () => { |
32 | | - let mono = new Float32Array([1, 2, 3]) |
33 | | - let stereo = [new Float32Array([1, 2]), new Float32Array([3, 4])] |
34 | | - is(channels(null).length, 0) |
35 | | - is(channels(undefined).length, 0) |
36 | | - is(channels([]).length, 0) |
37 | | - is(channels(mono).length, 1) |
38 | | - is(channels(mono)[0], mono) |
39 | | - is(channels(stereo).length, 2) |
40 | | -}) |
41 | | - |
42 | | -t('norm', () => { |
43 | | - is(norm(null).length, 0) |
44 | | - let buf = new Uint8Array([1, 2, 3]) |
45 | | - is(norm(buf), buf) |
46 | | - ok(norm(new Int8Array([1, 2])) instanceof Uint8Array) |
47 | | -}) |
48 | | - |
49 | | -t('merge', () => { |
50 | | - let a = new Uint8Array([1, 2]), b = new Uint8Array([3, 4]) |
51 | | - is(merge(a, b).length, 4) |
52 | | - is(merge(a, null), a) |
53 | | - is(merge(null, b), b) |
54 | | -}) |
55 | | - |
56 | | -t('streamEncoder', async () => { |
57 | | - let enc = streamEncoder( |
58 | | - ch => new Uint8Array([ch[0].length]), |
59 | | - () => new Uint8Array([0xFF]), |
60 | | - () => {} |
61 | | - ) |
62 | | - let r1 = await enc.encode([new Float32Array([1, 2, 3])]) |
63 | | - is(r1[0], 3) |
64 | | - let r2 = await enc.encode() |
65 | | - is(r2[0], 0xFF) |
66 | | - is((await enc.encode()).length, 0) |
67 | | -}) |
68 | | - |
69 | | -t('fmt', async () => { |
70 | | - let encoder = fmt(async (opts) => streamEncoder( |
71 | | - ch => new Uint8Array(ch[0].length * 2), |
72 | | - () => new Uint8Array([0xFE]), |
73 | | - () => {} |
74 | | - )) |
75 | | - is(typeof encoder, 'function') |
76 | | - is(typeof encoder.stream, 'function') |
77 | | - let result = await encoder([new Float32Array(100)], { sampleRate: 44100 }) |
78 | | - is(result.length, 201) |
79 | | -}) |
80 | | - |
81 | 24 | // --- format round-trip tests with lena --- |
82 | 25 |
|
83 | 26 | t('wav round-trip (lena)', async () => { |
@@ -144,3 +87,14 @@ t('wav streaming', async () => { |
144 | 87 | let final = await enc.encode() |
145 | 88 | ok(c1.length > 0 || c2.length > 0 || final.length > 0) |
146 | 89 | }) |
| 90 | + |
| 91 | +t('AudioBuffer input', async () => { |
| 92 | + let ab = new AudioBuffer({ sampleRate: 44100, length: 44100 }) |
| 93 | + let ch = ab.getChannelData(0) |
| 94 | + for (let i = 0; i < ch.length; i++) ch[i] = Math.sin(2 * Math.PI * 440 * i / 44100) |
| 95 | + let buf = await encode.wav(ab, { sampleRate: 44100 }) |
| 96 | + ok(buf.length > 44, 'encodes AudioBuffer') |
| 97 | + let dec = await decode(buf) |
| 98 | + is(dec.sampleRate, 44100) |
| 99 | + almost(rms(dec.channelData[0]), rms(ch), 0.001, 'rms matches') |
| 100 | +}) |
0 commit comments