From 401b0e2bd0647b816ee72e416e7d447b347df18e Mon Sep 17 00:00:00 2001 From: Alen <33656288+cnxysoft@users.noreply.github.com> Date: Sat, 14 Sep 2024 17:50:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=83=A8=E5=88=86=E8=AF=AD=E9=9F=B3?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E9=80=9F=E7=8E=87=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/audio.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/common/audio.ts b/src/common/audio.ts index 4dd82afd..388c0cfd 100644 --- a/src/common/audio.ts +++ b/src/common/audio.ts @@ -40,12 +40,14 @@ async function convert(filePath: string, pcmPath: string, logger: LogWrapper): P }); } -async function handleWavFile(file: Buffer, filePath: string, pcmPath: string, logger: LogWrapper): Promise { +async function handleWavFile( + file: Buffer, filePath: string, pcmPath: string, logger: LogWrapper +): Promise<{input: Buffer, sampleRate: number}> { const { fmt } = getWavFileInfo(file); if (!ALLOW_SAMPLE_RATE.includes(fmt.sampleRate)) { - return await convert(filePath, pcmPath, logger); + return {input: await convert(filePath, pcmPath, logger), sampleRate: 24000}; } - return file; + return {input: file, sampleRate: fmt.sampleRate}; } export async function encodeSilk(filePath: string, TEMP_DIR: string, logger: LogWrapper) { @@ -55,8 +57,10 @@ export async function encodeSilk(filePath: string, TEMP_DIR: string, logger: Log if (!isSilk(file)) { logger.log(`语音文件${filePath}需要转换成silk`); const pcmPath = `${pttPath}.pcm`; - const input = isWav(file) ? await handleWavFile(file, filePath, pcmPath, logger) : await convert(filePath, pcmPath, logger); - const silk = await encode(input, 24000); + const { input, sampleRate } = isWav(file) + ? (await handleWavFile(file, filePath, pcmPath, logger)) + : {input: await convert(filePath, pcmPath, logger), sampleRate: 24000}; + const silk = await encode(input, sampleRate); await fsPromise.writeFile(pttPath, silk.data); logger.log(`语音文件${filePath}转换成功!`, pttPath, '时长:', silk.duration); return {