From ab2148ec182db2b5d18d38f2dd690922d2cf56b8 Mon Sep 17 00:00:00 2001
From: Logan <52682203+ding-ding666@users.noreply.github.com>
Date: 星期一, 26 五月 2025 14:34:47 +0800
Subject: [PATCH] 更新go client 的原生实现 (#2532)
---
/dev/null | 27 -------------
runtime/golang/websocket/go_ws_client.go | 78 +++++++++++++++++++++++++++++++--------
2 files changed, 62 insertions(+), 43 deletions(-)
diff --git a/runtime/golang/websocket/go_ws_client.go b/runtime/golang/websocket/go_ws_client.go
index 5e0444a..d209221 100644
--- a/runtime/golang/websocket/go_ws_client.go
+++ b/runtime/golang/websocket/go_ws_client.go
@@ -2,19 +2,18 @@
import (
"bufio"
- "bytes"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/url"
"os"
- "os/exec"
"strconv"
"strings"
"sync"
"time"
+ "github.com/go-audio/wav"
"github.com/gorilla/websocket"
)
@@ -47,6 +46,63 @@
Stride int `json:"stride"`
ChunkNum int `json:"chunk_num"`
AudioBytes string `json:"audio_bytes"`
+}
+
+// 瀹屽叏妯℃嫙Python wave搴撶殑琛屼负璇诲彇WAV鏂囦欢
+func readWAVFile(filePath string, chunkSize []int, chunkInterval int) (*AudioData, error) {
+ file, err := os.Open(filePath)
+ if err != nil {
+ return nil, fmt.Errorf("failed to open WAV file: %v", err)
+ }
+ defer file.Close()
+
+ // 浣跨敤wav搴撹В鐮乄AV鏂囦欢锛屼絾鍙幏鍙栧熀鏈俊鎭�
+ decoder := wav.NewDecoder(file)
+ if !decoder.IsValidFile() {
+ return nil, fmt.Errorf("invalid WAV file format")
+ }
+
+ // 鑾峰彇WAV鏂囦欢鏍煎紡淇℃伅
+ format := decoder.Format()
+ sampleRate := int(format.SampleRate)
+
+ // 璇诲彇鎵�鏈夐煶棰戞暟鎹�
+ buf, err := decoder.FullPCMBuffer()
+ if err != nil {
+ return nil, fmt.Errorf("failed to read full PCM data: %v", err)
+ }
+
+ // 鑾峰彇鍘熷闊抽瀛楄妭鏁版嵁锛堢瓑鍚屼簬Python鐨刡ytes(frames)锛�
+ audioBytes := make([]byte, len(buf.Data)*2)
+ for i, sample := range buf.Data {
+ // 纭繚涓嶱ython wave搴撶殑瀛楄妭搴忎竴鑷达紙灏忕搴�16浣嶏級
+ sample16 := int16(sample)
+ audioBytes[i*2] = byte(sample16 & 0xFF)
+ audioBytes[i*2+1] = byte((sample16 >> 8) & 0xFF)
+ }
+
+ // 瀹屽叏鎸夌収Python鐨勮绠楁柟寮忚绠楀弬鏁�
+ // Python: stride = int(60 * chunk_size[1] / chunk_interval / 1000 * sample_rate * 2)
+ // 娉ㄦ剰锛歅ython涓殑闄ゆ硶鏄诞鐐归櫎娉曪紝Go涓渶瑕佹樉寮忚浆鎹㈤伩鍏嶆暣鏁伴櫎娉曟埅鏂�
+ stride := int(float64(60*chunkSize[1]) / float64(chunkInterval) / 1000.0 * float64(sampleRate) * 2.0)
+
+ // 娣诲姞瀹夊叏妫�鏌ラ槻姝㈤櫎闆�
+ if stride <= 0 {
+ return nil, fmt.Errorf("calculated stride is zero or negative: %d", stride)
+ }
+
+ // Python: chunk_num = (len(audio_bytes) - 1) // stride + 1
+ chunkNum := (len(audioBytes)-1)/stride + 1
+
+ // 缂栫爜涓築ase64锛堜笌Python鐨刡ase64.b64encode().decode('utf-8')涓�鑷达級
+ audioBase64 := base64.StdEncoding.EncodeToString(audioBytes)
+
+ return &AudioData{
+ SampleRate: sampleRate,
+ Stride: stride,
+ ChunkNum: chunkNum,
+ AudioBytes: audioBase64,
+ }, nil
}
func IntSlicetoString(nums []int) string {
@@ -109,20 +165,10 @@
}
if strings.HasSuffix(wav_path, ".wav") {
- cmd := exec.Command("python", "wavhandler.py", wav_path, IntSlicetoString(args.chunk_size), strconv.Itoa(args.chunk_interval))
-
- var out bytes.Buffer
- cmd.Stdout = &out
- err := cmd.Run()
+ // 浣跨敤Go鍘熺敓瀹炵幇鏇挎崲Python璋冪敤
+ audioData, err := readWAVFile(wav_path, args.chunk_size, args.chunk_interval)
if err != nil {
- fmt.Println("Error running Python script:", err)
- return
- }
-
- var audioData AudioData
- err = json.Unmarshal(out.Bytes(), &audioData)
- if err != nil {
- fmt.Println("Error parsing JSON:", err)
+ fmt.Println("Error reading WAV file:", err)
return
}
@@ -222,7 +268,7 @@
var ibestWriter *os.File
var err error
if args.output_dir != "" {
- filePath := fmt.Sprintf("%s/text.%d", args.output_dir, id)
+ filePath := fmt.Sprintf("%s/text.%s", args.output_dir, id)
ibestWriter, err = os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("failed to open file: %v", err)
diff --git a/runtime/golang/websocket/wavhandler.py b/runtime/golang/websocket/wavhandler.py
deleted file mode 100644
index e6b8e2f..0000000
--- a/runtime/golang/websocket/wavhandler.py
+++ /dev/null
@@ -1,27 +0,0 @@
-import sys
-import wave
-import json
-import base64
-
-if __name__ == "__main__":
- wav_path = sys.argv[1]
- chunk_size = [int(x) for x in sys.argv[2].split(",")]
- chunk_interval = int(sys.argv[3])
-
- with wave.open(wav_path, "rb") as wav_file:
- params = wav_file.getparams()
- sample_rate = wav_file.getframerate()
- frames = wav_file.readframes(wav_file.getnframes())
- audio_bytes = bytes(frames)
-
- stride = int(60 * chunk_size[1] / chunk_interval / 1000 * sample_rate * 2)
- chunk_num = (len(audio_bytes) - 1) // stride + 1
-
- result = {
- "sample_rate": sample_rate,
- "stride": stride,
- "chunk_num": chunk_num,
- "audio_bytes": base64.b64encode(audio_bytes).decode('utf-8')
- }
-
- print(json.dumps(result))
\ No newline at end of file
--
Gitblit v1.9.1