75 lines
3.3 KiB
JavaScript
75 lines
3.3 KiB
JavaScript
const { test } = require('node:test')
|
|
const assert = require('node:assert/strict')
|
|
const fs = require('node:fs')
|
|
const path = require('node:path')
|
|
const ts = require('typescript')
|
|
const target = { exports: {} }
|
|
const code = ts.transpileModule(fs.readFileSync(path.join(__dirname, '../src/local-pcm.ts'), 'utf8'), {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
|
|
}).outputText
|
|
new Function('module', 'exports', code)(target, target.exports)
|
|
const { PcmSpeechBuffer, PcmResampler } = target.exports
|
|
|
|
test('silent microphone never creates hallucination opportunities', () => {
|
|
const chunks = []
|
|
const buffer = new PcmSpeechBuffer((...chunk) => chunks.push(chunk))
|
|
for (let i = 0; i < 1000; i++) buffer.push(new Float32Array(1600), 100000 + i * 100)
|
|
buffer.flush()
|
|
assert.equal(chunks.length, 0)
|
|
})
|
|
test('speech preserves amplitude, boundaries and epoch timestamp', () => {
|
|
const chunks = []
|
|
const buffer = new PcmSpeechBuffer((pcm, timestamp) => chunks.push({ pcm, timestamp }))
|
|
buffer.push(new Float32Array(3200), 1793332800000)
|
|
buffer.push(new Float32Array(16000).fill(0.5), 1793332800200)
|
|
buffer.push(new Float32Array(11200), 1793332801200)
|
|
assert.equal(chunks.length, 1)
|
|
assert.equal(chunks[0].timestamp, 1793332800000)
|
|
assert.equal(chunks[0].pcm[4000], 16384)
|
|
buffer.flush()
|
|
assert.equal(chunks.length, 1)
|
|
})
|
|
test('continuous speech is bounded and stop drains final speech once', () => {
|
|
const chunks = []
|
|
const buffer = new PcmSpeechBuffer(pcm => chunks.push(pcm))
|
|
for (let i = 0; i < 100; i++) buffer.push(new Float32Array(1600).fill(-0.5), 100000 + i * 100)
|
|
buffer.flush()
|
|
buffer.flush()
|
|
assert.equal(chunks.length, 3)
|
|
assert.equal(chunks.reduce((n, c) => n + c.length, 0), 160000)
|
|
assert(chunks.every(c => c.length <= 64000))
|
|
assert.equal(chunks[0][0], -16384)
|
|
})
|
|
for (const rate of [16000, 44100, 48000]) {
|
|
test(`resampling ${rate}Hz maintains duration and a continuous signal across callbacks`, () => {
|
|
const resampler = new PcmResampler(rate)
|
|
let output = []
|
|
const input = Float32Array.from({ length: rate }, (_, i) => Math.sin(2 * Math.PI * 440 * i / rate))
|
|
for (let i = 0; i < input.length; i += 4096) output.push(...resampler.push(input.subarray(i, i + 4096)))
|
|
assert(Math.abs(output.length - 16000) <= 1)
|
|
const mse = output.reduce((sum, v, i) => sum + (v - Math.sin(2 * Math.PI * 440 * i / 16000)) ** 2, 0) / output.length
|
|
assert(mse < 0.00001)
|
|
})
|
|
}
|
|
test('invalid sample rate is rejected', () => {
|
|
for (const rate of [0, NaN, Infinity, 192001]) assert.throws(() => new PcmResampler(rate))
|
|
})
|
|
test('quiet and very short speech is preserved for model VAD, including a short negation', () => {
|
|
const chunks = []
|
|
const buffer = new PcmSpeechBuffer(pcm => chunks.push(pcm))
|
|
buffer.push(new Float32Array(800).fill(0.0005), 1793332800000)
|
|
buffer.flush()
|
|
assert.equal(chunks.length, 1)
|
|
assert.equal(chunks[0].length, 800)
|
|
assert(chunks[0].every(sample => sample > 0))
|
|
})
|
|
|
|
test('nonzero room noise cannot delay the first submission beyond four seconds', () => {
|
|
const chunks = []
|
|
const buffer = new PcmSpeechBuffer(pcm => chunks.push(pcm))
|
|
for (let i = 0; i < 39; i++) buffer.push(new Float32Array(1600).fill(0.0001), 100000 + i * 100)
|
|
assert.equal(chunks.length, 0)
|
|
buffer.push(new Float32Array(1600).fill(0.0001), 103900)
|
|
assert.equal(chunks[0].length, 64000)
|
|
})
|