游雁
2024-12-23 1e5ef6ed9a6f64ecca7b9ef9481519b271f793a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
 
import (
    "bufio"
    "bytes"
    "encoding/base64"
    "encoding/json"
    "fmt"
    "log"
    "net/url"
    "os"
    "os/exec"
    "strconv"
    "strings"
    "sync"
    "time"
 
    "github.com/gorilla/websocket"
)
 
var args struct {
    audio_in       string
    thread_num     int
    host           string
    port           string
    output_dir     string
    hotword        string
    audio_fs       int
    use_itn        int
    mode           string
    chunk_size     []int
    chunk_interval int
}
 
var websocketConn *websocket.Conn
var offline_msg_done = false
 
type Message struct {
    WavName   string `json:"wav_name"`
    Text      string `json:"text"`
    TimeStamp string `json:"timestamp"`
    Mode      string `json:"mode"`
}
 
type AudioData struct {
    SampleRate int    `json:"sample_rate"`
    Stride     int    `json:"stride"`
    ChunkNum   int    `json:"chunk_num"`
    AudioBytes string `json:"audio_bytes"`
}
 
func IntSlicetoString(nums []int) string {
    strNums := make([]string, len(nums))
    for i, num := range nums {
        strNums[i] = strconv.Itoa(num)
    }
    result := strings.Join(strNums, ",")
    return result
}
 
func recordFromScp(chunk_begin, chunk_size int) {
    wavs := []string{args.audio_in}
    sample_rate := args.audio_fs
    wav_format := "pcm"
    use_itn := true
 
    wav_name := "demo"
    wav_path := wavs[0]
    var audio_bytes []byte
    var stride, chunk_num int
 
    fst_dict := make(map[string]int)
    hotword_msg := ""
    if args.hotword != "" {
        file, err := os.Open(args.hotword)
        if err != nil {
            log.Fatalf("failed to open file: %v", err)
        }
        defer file.Close()
 
        scanner := bufio.NewScanner(file)
        for scanner.Scan() {
            words := strings.Fields(scanner.Text())
 
            if len(words) < 2 {
                fmt.Println("Please checkout format of hotwords")
                continue
            }
            weight, err := strconv.Atoi(words[1])
            if err != nil {
                fmt.Println("The weight of hotwords must be Int!")
            }
            fst_dict[words[0]] = weight
        }
        if err := scanner.Err(); err != nil {
            log.Fatalf("error reading file: %v", err)
        }
        bytes, _ := json.Marshal(fst_dict)
        hotword_msg = string(bytes)
        fmt.Println("HotWord: ", hotword_msg)
    }
 
    if args.use_itn == 0 {
        use_itn = false
    }
 
    if chunk_size > 0 {
        wavs = wavs[chunk_begin : chunk_begin+chunk_size]
    }
 
    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()
        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)
            return
        }
 
        stride = audioData.Stride
        chunk_num = audioData.ChunkNum
        sample_rate = audioData.SampleRate
        audio_bytes, err = base64.StdEncoding.DecodeString(audioData.AudioBytes)
        if err != nil {
            fmt.Println("Error decoding Base64:", err)
            return
        }
    } else {
        fmt.Println("Currently, only the WAV format is supported")
        return
    }
 
    first_message := make(map[string]interface{})
    first_message["mode"] = args.mode
    first_message["chunk_size"] = args.chunk_size
    first_message["chunk_interval"] = args.chunk_interval
    first_message["audio_fs"] = sample_rate
    first_message["wav_name"] = wav_name
    first_message["wav_format"] = wav_format
    first_message["is_speaking"] = true
    first_message["hotwords"] = hotword_msg
    first_message["itn"] = use_itn
 
    bytes, _ := json.Marshal(first_message)
    message := string(bytes)
 
    // fmt.Println(audio_bytes)
    // fmt.Println(stride)
    // fmt.Println(chunk_num)
    // fmt.Println(message)
 
    err := websocketConn.WriteMessage(websocket.TextMessage, []byte(message))
    if err != nil {
        log.Println("Failed to send the message:", err)
        return
    }
 
    is_speaking := true
    for i := 0; i < chunk_num; i++ {
        beg := i * stride
        var data []byte
        if i == chunk_num-1 {
            data = audio_bytes[beg:]
        } else {
            data = audio_bytes[beg : beg+stride]
        }
 
        err = websocketConn.WriteMessage(websocket.BinaryMessage, data)
        if err != nil {
            fmt.Println("Failed to send audio data:", err)
            return
        }
 
        if i == chunk_num-1 {
            is_speaking = false
            endMsg := map[string]bool{"is_speaking": is_speaking}
            endMsgBytes, err := json.Marshal(endMsg)
            if err != nil {
                fmt.Println("JSON serialization failed:", err)
                return
            }
            err = websocketConn.WriteMessage(websocket.TextMessage, endMsgBytes)
            if err != nil {
                fmt.Println("Failed to send the termination message:", err)
                return
            }
        }
        var sleepDuration time.Duration
        if args.mode == "offline" {
            sleepDuration = time.Millisecond
        } else {
            fmt.Println("timesleep:  Currently, only offline mode is supported.")
            // sleepDuration = time.Duration(60*float64(args.chunk_size[1])/float64(args.chunk_interval)) * time.Millisecond
            return
        }
        time.Sleep(sleepDuration)
    }
 
    if args.mode != "offline" {
        fmt.Println("Currently, only offline mode is supported.")
        return
    }
    if args.mode == "offline" {
        for !offline_msg_done {
            time.Sleep(1 * time.Second)
        }
    }
    websocketConn.Close()
}
 
func message(id string) {
    text_print := ""
    var ibestWriter *os.File
    var err error
    if args.output_dir != "" {
        filePath := fmt.Sprintf("%s/text.%d", 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)
        }
    } else {
        ibestWriter = nil
    }
 
    for {
        _, message, err := websocketConn.ReadMessage()
        if err != nil {
            log.Println("read:", err)
            break
        }
        var meg Message
        var wav_name string
        timestamp := ""
 
        err = json.Unmarshal(message, &meg)
        if err != nil {
            log.Println("unmarshal:", err)
            continue
        }
 
        if meg.WavName != "" {
            wav_name = meg.WavName
        } else {
            wav_name = "demo"
        }
 
        text := meg.Text
 
        if meg.TimeStamp != "" {
            timestamp = meg.TimeStamp
        }
 
        if ibestWriter != nil {
            var text_write_line string
            if timestamp != "" {
                text_write_line = fmt.Sprintf("%s\t%s\t%s\n", wav_name, text, timestamp)
            } else {
                text_write_line = fmt.Sprintf("%s\t%s\n", wav_name, text)
            }
            _, err = ibestWriter.WriteString(text_write_line)
            if err != nil {
                log.Fatalf("Failed to write to file: %v", err)
            }
        }
 
        if meg.Mode != "offline" {
            fmt.Println("Currently, only offline mode is supported.")
            return
        }
 
        if meg.Mode == "offline" {
            if timestamp != "" {
                text_print += fmt.Sprintf("%s timestamp: %s", text, timestamp)
            } else {
                text_print += fmt.Sprintf("%s ", text)
            }
            fmt.Println("\rpid" + id + ": " + wav_name + ": " + text_print)
            offline_msg_done = true
        }
    }
}
 
func wsClient(id, chunk_begin, chunk_size int, done chan bool) {
    for i := chunk_begin; i < chunk_begin+chunk_size; i++ {
        offline_msg_done = false
 
        u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%s", args.host, args.port), Path: "/"}
        fmt.Printf("Thread %d: Connecting to %s\n", id, u.String())
        var err error
        websocketConn, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
        if err != nil {
            log.Fatal("dial:", err)
        }
        defer websocketConn.Close()
 
        var wg sync.WaitGroup
        wg.Add(2)
 
        go func() {
            defer wg.Done()
            recordFromScp(i, 1)
        }()
 
        go func() {
            defer wg.Done()
            id_str := strconv.Itoa(id)
            i_str := strconv.Itoa(i)
            message(id_str + "_" + i_str)
        }()
 
        wg.Wait()
    }
 
    done <- true
}
 
func oneThread(id, chunk_begin, chunk_size int, wg *sync.WaitGroup) {
    defer wg.Done()
    done := make(chan bool)
 
    go wsClient(id, chunk_begin, chunk_size, done)
 
    select {
    case <-done:
        fmt.Printf("Thread %d: Task completed\n", id)
    }
}
 
func main() {
    args.audio_in = "../audio/asr_example.wav"
    args.thread_num = 1
    args.host = "127.0.0.1"
    args.port = "10095"
    args.output_dir = "/workspace/models/Outputs"
    args.hotword = "/workspace/models/hotword.txt"
    args.chunk_size = []int{5, 10, 5}
    args.chunk_interval = 10
    args.mode = "offline"
    args.audio_fs = 16000
    args.use_itn = 1
    var chunk_size, remain_wavs int
 
    wavs := []string{args.audio_in}
 
    total_len := len(wavs)
    if total_len >= args.thread_num {
        chunk_size = total_len / args.thread_num
        remain_wavs = total_len - chunk_size*args.thread_num
    } else {
        chunk_size = 1
        remain_wavs = 0
    }
 
    var wg sync.WaitGroup
    chunk_begin := 0
    for i := 0; i < args.thread_num; i++ {
        wg.Add(1)
        now_chunk_size := chunk_size
        if remain_wavs > 0 {
            now_chunk_size = chunk_size + 1
            remain_wavs = remain_wavs - 1
        }
        go oneThread(i, chunk_begin, now_chunk_size, &wg)
 
        chunk_begin = chunk_begin + now_chunk_size
    }
    wg.Wait()
}