From 1508dab7fe198646ffcf82249c2d02b3db4f1d79 Mon Sep 17 00:00:00 2001 From: idranme <96647698+idranme@users.noreply.github.com> Date: Tue, 18 Jun 2024 19:15:56 +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 9add729..e25533a 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "file-type": "^19.0.0", "fluent-ffmpeg": "^2.1.2", "level": "^8.0.1", - "silk-wasm": "^3.3.4", + "silk-wasm": "^3.6.0", "utf-8-validate": "^6.0.3", "uuid": "^9.0.1", "ws": "^8.16.0" diff --git a/src/common/utils/audio.ts b/src/common/utils/audio.ts index 3e14343..4eb2458 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 } from './log' import path from 'node:path' @@ -60,10 +60,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 = () => { @@ -91,7 +92,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) @@ -108,7 +109,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