-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest.js
More file actions
40 lines (36 loc) · 1.35 KB
/
Copy pathtest.js
File metadata and controls
40 lines (36 loc) · 1.35 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
34
35
36
37
38
39
40
import decode, { decoder } from './decode-mp3.js'
import mp3 from 'audio-lena/mp3'
let pass = 0, fail = 0
function ok(cond, msg) {
if (cond) { pass++; console.log(' ok', msg) }
else { fail++; console.log(' FAIL', msg) }
}
function near(a, b, tol = 0.02) { return Math.abs(a - b) < tol }
function rms(f32) { let s = 0; for (let i = 0; i < f32.length; i++) s += f32[i] * f32[i]; return Math.sqrt(s / f32.length) }
// whole-file decode
console.log('MP3 whole-file')
{
let r = await decode(mp3)
ok(r.channelData.length >= 1, 'has channels')
ok(r.sampleRate === 44100, 'sampleRate 44100')
ok(near(r.channelData[0].length / r.sampleRate, 12.27), 'duration ~12.27s')
ok(rms(r.channelData[0]) > 0.05, 'has audio content')
}
// streaming decoder
console.log('MP3 streaming')
{
let dec = await decoder()
let buf = new Uint8Array(mp3)
let mid = Math.floor(buf.length / 2)
let a = dec.decode(buf.slice(0, mid).buffer) // ArrayBuffer chunk
let b = dec.decode(buf.subarray(mid))
ok(!(a instanceof Promise) && !(b instanceof Promise), 'decode returns values')
let total = (a.channelData[0]?.length || 0) + (b.channelData[0]?.length || 0)
ok(total > 0, 'decoded samples: ' + total)
dec.free()
let threw = false
try { dec.decode(buf) } catch { threw = true }
ok(threw, 'decode after free throws')
}
console.log(`\n${pass} passed, ${fail} failed`)
if (fail) process.exit(1)