Merge pull request #33 from Lazy-CZ/fix/export-audio-fix

fix: 修复批量导出 wav 音频语速变快、音调升高(采样率不匹配)问题
This commit is contained in:
qingmao
2026-09-04 16:14:35 +08:00
committed by GitHub
3 changed files with 47 additions and 11 deletions
+23
View File
@@ -2,6 +2,29 @@ import { describe, expect, it, vi } from 'vitest'
import { VoiceService } from '../../src/main/voice-service'
describe('VoiceService batch lookup', () => {
it('writes the decoded sample rate and duration into batch WAV output', async () => {
const getVoiceDataBatch = vi.fn().mockResolvedValue([{ success: true, hex: '0102' }])
const service = new VoiceService({ getVoiceDataBatch } as never)
const decoder = (service as unknown as {
decoderRegistry: { decode: ReturnType<typeof vi.fn> }
}).decoderRegistry
vi.spyOn(decoder, 'decode').mockResolvedValue({
pcm: Buffer.alloc(16_000 * 2),
sampleRate: 16_000,
channels: 1
})
const [result] = await service.resolveVoices([
{ sessionId: 'session', localId: 10, createTime: 100, svrId: '1000' }
])
expect(result.success).toBe(true)
const wav = Buffer.from(result.data!, 'base64')
expect(wav.readUInt32LE(24)).toBe(16_000)
expect(wav.readUInt16LE(22)).toBe(1)
expect((wav.length - 44) / (wav.readUInt32LE(24) * wav.readUInt16LE(22) * 2)).toBe(1)
})
it('retries failed batch entries with the compatible single-item lookup', async () => {
const getVoiceDataBatch = vi.fn().mockResolvedValue([
{ success: false, error: '获取语音数据失败' },