From 35951fd61a76f938fb052566b82c90306ac082d0 Mon Sep 17 00:00:00 2001
From: idanran <96647698+idanran@users.noreply.github.com>
Date: Wed, 20 Mar 2024 17:32:21 +0000
Subject: [PATCH] fix: unable to send voice in some cases

---
 package-lock.json        | 8 ++++----
 package.json             | 2 +-
 src/common/utils/file.ts | 9 +++------
 3 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index dc3d75e..38413f2 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -14,7 +14,7 @@
         "file-type": "^19.0.0",
         "fluent-ffmpeg": "^2.1.2",
         "level": "^8.0.1",
-        "silk-wasm": "^3.2.3",
+        "silk-wasm": "^3.2.4",
         "utf-8-validate": "^6.0.3",
         "uuid": "^9.0.1",
         "ws": "^8.16.0"
@@ -5895,9 +5895,9 @@
       }
     },
     "node_modules/silk-wasm": {
-      "version": "3.2.3",
-      "resolved": "https://registry.npmjs.org/silk-wasm/-/silk-wasm-3.2.3.tgz",
-      "integrity": "sha512-zZ3hgMpiPR6cFnKvCPgPpCwx6n5RoJCbEGIFlge2kAxAmgzBTf0b2F2xIPG5W4obUhQPQXXTTH074eGZJK01xw=="
+      "version": "3.2.4",
+      "resolved": "https://registry.npmjs.org/silk-wasm/-/silk-wasm-3.2.4.tgz",
+      "integrity": "sha512-oBkXmdIRl7cyzpoXEeEVN7v1M2yCnH1/bN8oANoYTvCqbYa5lM/CGJP47DYbpUFVO9PUpm58KP/HZaVzt4J6jw=="
     },
     "node_modules/slash": {
       "version": "4.0.0",
diff --git a/package.json b/package.json
index 1ef7e71..e56cea1 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,7 @@
     "file-type": "^19.0.0",
     "fluent-ffmpeg": "^2.1.2",
     "level": "^8.0.1",
-    "silk-wasm": "^3.2.3",
+    "silk-wasm": "^3.2.4",
     "utf-8-validate": "^6.0.3",
     "uuid": "^9.0.1",
     "ws": "^8.16.0"
diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts
index 6963191..10c3f5e 100644
--- a/src/common/utils/file.ts
+++ b/src/common/utils/file.ts
@@ -68,8 +68,6 @@ export async function file2base64(path: string) {
 }
 
 export async function encodeSilk(filePath: string) {
-    const fsp = require("fs").promises
-
     function getFileHeader(filePath: string) {
         // 定义要读取的字节数
         const bytesToRead = 7;
@@ -104,7 +102,6 @@ export async function encodeSilk(filePath: string) {
     // }
 
     try {
-        const fileName = path.basename(filePath);
         const pttPath = path.join(DATA_DIR, uuidv4());
         if (getFileHeader(filePath) !== "02232153494c4b") {
             log(`语音文件${filePath}需要转换成silk`)
@@ -118,7 +115,7 @@ export async function encodeSilk(filePath: string) {
                     if (ffmpegPath) {
                         ffmpeg.setFfmpegPath(ffmpegPath);
                     }
-                    ffmpeg(filePath).toFormat("wav").audioChannels(2).on('end', function () {
+                    ffmpeg(filePath).toFormat("wav").audioChannels(1).audioFrequency(24000).on('end', function () {
                         log('wav转换完成');
                     })
                         .on('error', function (err) {
@@ -146,10 +143,10 @@ export async function encodeSilk(filePath: string) {
                 duration: silk.duration,
             };
         } else {
-            const pcm = fs.readFileSync(filePath);
+            const silk = fs.readFileSync(filePath);
             let duration = 0;
             try {
-                duration = getDuration(pcm);
+                duration = getDuration(silk);
             } catch (e) {
                 log("获取语音文件时长失败", filePath, e.stack)
                 duration = fs.statSync(filePath).size / 1024 / 3  // 每3kb大约1s