From 80c3356c8ffa59ad1e9ad0467ddd8af7438d37ee Mon Sep 17 00:00:00 2001 From: idranme <96647698+idranme@users.noreply.github.com> Date: Wed, 10 Jul 2024 17:44:17 +0000 Subject: [PATCH] perf: audio --- package.json | 2 +- src/common/utils/audio.ts | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 3c97cea8..72b14653 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "json-schema-to-ts": "^3.1.0", "log4js": "^6.9.1", "qrcode-terminal": "^0.12.0", - "silk-wasm": "^3.3.4", + "silk-wasm": "^3.6.1", "sqlite3": "^5.1.7", "uuid": "^10.0.0", "ws": "^8.16.0" diff --git a/src/common/utils/audio.ts b/src/common/utils/audio.ts index f1b6d8da..e9a33c91 100644 --- a/src/common/utils/audio.ts +++ b/src/common/utils/audio.ts @@ -1,5 +1,5 @@ import fs from 'fs'; -import { encode, getDuration, getWavFileInfo, isWav } from 'silk-wasm'; +import { encode, getDuration, getWavFileInfo, isWav, isSilk } from 'silk-wasm'; import fsPromise from 'fs/promises'; import { log, logError } from './log'; import path from 'node:path'; @@ -63,10 +63,11 @@ export async function encodeSilk(filePath: string) { // } try { + const file = await fsPromise.readFile(filePath); const pttPath = path.join(TEMP_DIR, uuidv4()); - if (getFileHeader(filePath) !== '02232153494c4b') { + if (!isSilk(file)) { log(`语音文件${filePath}需要转换成silk`); - const _isWav = await isWavFile(filePath); + const _isWav = isWav(file); const pcmPath = pttPath + '.pcm'; let sampleRate = 0; const convert = () => { @@ -96,7 +97,7 @@ export async function encodeSilk(filePath: string) { if (!_isWav) { input = await convert(); } else { - input = fs.readFileSync(filePath); + input = file; const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000]; const { fmt } = getWavFileInfo(input); // log(`wav文件信息`, fmt) @@ -113,7 +114,7 @@ export async function encodeSilk(filePath: string) { duration: silk.duration / 1000 }; } else { - const silk = fs.readFileSync(filePath); + const silk = file; let duration = 0; try { duration = getDuration(silk) / 1000;