perf: audio

This commit is contained in:
idranme 2024-06-18 19:15:56 +00:00
parent 52a065542e
commit 1508dab7fe
2 changed files with 7 additions and 6 deletions

View File

@ -23,7 +23,7 @@
"file-type": "^19.0.0", "file-type": "^19.0.0",
"fluent-ffmpeg": "^2.1.2", "fluent-ffmpeg": "^2.1.2",
"level": "^8.0.1", "level": "^8.0.1",
"silk-wasm": "^3.3.4", "silk-wasm": "^3.6.0",
"utf-8-validate": "^6.0.3", "utf-8-validate": "^6.0.3",
"uuid": "^9.0.1", "uuid": "^9.0.1",
"ws": "^8.16.0" "ws": "^8.16.0"

View File

@ -1,5 +1,5 @@
import fs from 'fs' 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 fsPromise from 'fs/promises'
import { log } from './log' import { log } from './log'
import path from 'node:path' import path from 'node:path'
@ -60,10 +60,11 @@ export async function encodeSilk(filePath: string) {
// } // }
try { try {
const file = await fsPromise.readFile(filePath)
const pttPath = path.join(TEMP_DIR, uuidv4()) const pttPath = path.join(TEMP_DIR, uuidv4())
if (getFileHeader(filePath) !== '02232153494c4b') { if (!isSilk(file)) {
log(`语音文件${filePath}需要转换成silk`) log(`语音文件${filePath}需要转换成silk`)
const _isWav = await isWavFile(filePath) const _isWav = isWav(file)
const pcmPath = pttPath + '.pcm' const pcmPath = pttPath + '.pcm'
let sampleRate = 0 let sampleRate = 0
const convert = () => { const convert = () => {
@ -91,7 +92,7 @@ export async function encodeSilk(filePath: string) {
if (!_isWav) { if (!_isWav) {
input = await convert() input = await convert()
} else { } else {
input = fs.readFileSync(filePath) input = file
const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000] const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000]
const { fmt } = getWavFileInfo(input) const { fmt } = getWavFileInfo(input)
// log(`wav文件信息`, fmt) // log(`wav文件信息`, fmt)
@ -108,7 +109,7 @@ export async function encodeSilk(filePath: string) {
duration: silk.duration / 1000, duration: silk.duration / 1000,
} }
} else { } else {
const silk = fs.readFileSync(filePath) const silk = file
let duration = 0 let duration = 0
try { try {
duration = getDuration(silk) / 1000 duration = getDuration(silk) / 1000