Merge pull request #260 from idranme/main

perf: audio
This commit is contained in:
linyuchen 2024-06-21 10:36:29 +08:00 committed by GitHub
commit 2f87e3818e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 6 deletions

View File

@ -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"

View File

@ -1,7 +1,7 @@
import fs from 'fs'
import fsAsync from 'fs/promises'
import fsPromise from 'fs/promises'
import { decode, encode, getDuration, getWavFileInfo, isWav } from 'silk-wasm'
import { decode, encode, getDuration, getWavFileInfo, isWav, isSilk } from 'silk-wasm'
import { log } from './log'
import path from 'node:path'
import { TEMP_DIR } from './index'
@ -61,10 +61,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 = () => {
@ -93,7 +94,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)
@ -110,7 +111,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