/** * 音频处理器测试 */ import { describe, it, expect } from 'vitest'; import { normalizeAudio, resample, SimpleVAD, frameAudio } from '../src/core/audio-processor.js'; describe('audio-processor', () => { describe('normalizeAudio', () => { it('应该归一化 Int16Array 数据', () => { const input = new Int16Array([32767, -32768, 0, 16384]); const result = normalizeAudio(input, 16000); expect(result).toBeInstanceOf(Float32Array); expect(result.length).toBe(input.length); expect(result[0]).toBeCloseTo(1, 3); expect(result[1]).toBeCloseTo(-1, 3); expect(result[2]).toBe(0); }); it('应该处理 Float32Array 输入', () => { const input = new Float32Array([1, -1, 0, 0.5]); const result = normalizeAudio(input, 16000); expect(result).toBe(input); }); }); describe('resample', () => { it('应该保持相同采样率的数据不变', () => { const input = new Float32Array([1, 2, 3, 4]); const result = resample(input, 16000, 16000); expect(result).toBe(input); }); it('应该降低采样率', () => { const input = new Float32Array([1, 2, 3, 4, 5, 6, 7, 8]); const result = resample(input, 16000, 8000); expect(result.length).toBe(4); }); }); describe('SimpleVAD', () => { it('应该检测到高能量音频', () => { const vad = new SimpleVAD({ energyThreshold: 0.01 }); const loudFrame = new Float32Array([0.5, 0.6, 0.7, 0.8]); const result = vad.process(loudFrame, 16000); expect(result.isSpeaking).toBe(true); }); it('应该忽略低能量音频', () => { const vad = new SimpleVAD({ energyThreshold: 0.01 }); const quietFrame = new Float32Array([0.001, 0.002, 0.001, 0]); const result = vad.process(quietFrame, 16000); expect(result.isSpeaking).toBe(false); }); it('应该重置状态', () => { const vad = new SimpleVAD(); vad.process(new Float32Array([0.5, 0.6, 0.7]), 16000); vad.reset(); expect(vad.process(new Float32Array([0]), 16000).isSpeaking).toBe(false); }); }); describe('frameAudio', () => { it('应该正确分帧', () => { const input = new Float32Array([1, 2, 3, 4, 5, 6, 7, 8]); const frames = frameAudio(input, 4, 2); expect(frames.length).toBe(4); expect(frames[0]).toEqual(new Float32Array([1, 2, 3, 4])); expect(frames[1]).toEqual(new Float32Array([3, 4, 5, 6])); }); it('应该处理不足一帧的数据', () => { const input = new Float32Array([1, 2, 3]); const frames = frameAudio(input, 4, 2); expect(frames.length).toBe(0); }); }); });