游雁
2024-01-05 32905d8cdedd53dad26680b0bd41397aaf0e51ae
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
using Websocket.Client;
using System.Text.Json;
using System.Reactive.Linq;
using FunASRWSClient_Offline;
using System.Text.RegularExpressions;
 
namespace WebSocketSpace
{
    internal class  CWebSocketClient
    {
        private static readonly Uri serverUri = new Uri($"ws://{WSClient_Offline.host}:{WSClient_Offline.port}"); // 你要连接的WebSocket服务器地址
        private static WebsocketClient client = new WebsocketClient(serverUri);
        public async Task<string> ClientConnTest()
        {
            string commstatus = "WebSocket通信连接失败";
            try
            {
                client.Name = "funasr";
                client.ReconnectTimeout = null;
                client.ReconnectionHappened.Subscribe(info =>
                    Console.WriteLine($"Reconnection happened, type: {info.Type}, url: {client.Url}"));
                client.DisconnectionHappened.Subscribe(info =>
                    Console.WriteLine($"Disconnection happened, type: {info.Type}"));
 
                client
                    .MessageReceived
                    .Where(msg => msg.Text != null)
                    .Subscribe(msg =>
                    {
                        recmessage(msg.Text);
                    });
 
                await client.Start();
 
                if (client.IsRunning)
                    commstatus = "WebSocket通信连接成功";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                client.Dispose();
            }
 
            return commstatus;
        }
 
        public async Task<Task> ClientSendFileFunc(string file_name)//文件转录
        {
            string fileExtension = Path.GetExtension(file_name);
            fileExtension = fileExtension.Replace(".", "");
            if (!(fileExtension == "mp3" || fileExtension == "mp4" || fileExtension == "wav" || fileExtension == "pcm"))
                return Task.CompletedTask;
 
            try
            {
                if (client.IsRunning)
                {
                    if (fileExtension == "wav")
                    {
                        var exitEvent = new ManualResetEvent(false);
                        string path = Path.GetFileName(file_name);
                        string firstbuff = string.Format("{{\"mode\": \"offline\", \"wav_name\": \"{0}\", \"is_speaking\": true,\"hotwords\":\"{1}\"}}", Path.GetFileName(file_name), WSClient_Offline.hotword);
                        client.Send(firstbuff);
                        showWAVForm(client, file_name);
                    }
                    else
                    {
                        var exitEvent = new ManualResetEvent(false);
                        string path = Path.GetFileName(file_name);
                        string firstbuff = string.Format("{{\"mode\": \"offline\", \"wav_name\": \"{0}\", \"is_speaking\": true,\"hotwords\":\"{1}\", \"wav_format\":\"{2}\"}}", Path.GetFileName(file_name), WSClient_Offline.hotword, fileExtension);
                        client.Send(firstbuff);
                        showWAVForm_All(client, file_name);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return Task.CompletedTask;
        }
 
        public void recmessage(string message)
        {
            if (message != null)
            {
                try
                {
                    string timestamp = string.Empty;
                    JsonDocument jsonDoc = JsonDocument.Parse(message);
                    JsonElement root = jsonDoc.RootElement;
                    string mode = root.GetProperty("mode").GetString();
                    string text = root.GetProperty("text").GetString(); 
                    string name = root.GetProperty("wav_name").GetString();
                    if (message.IndexOf("timestamp") != -1)
                    {
                        Console.WriteLine($"文件名称:{name}");
                        //识别内容处理
                        text = text.Replace(",", "。");
                        text = text.Replace("?", "。");
                        List<string> sens = text.Split("。").ToList();
                        //时间戳处理
                        timestamp = root.GetProperty("timestamp").GetString();
                        List<List<int>> data = new List<List<int>>();
                        string pattern = @"\[(\d+),(\d+)\]";
                        foreach (Match match in Regex.Matches(timestamp, pattern))
                        {
                            int start = int.Parse(match.Groups[1].Value);
                            int end = int.Parse(match.Groups[2].Value);
                            data.Add(new List<int> { start, end });
                        }
                        int count = 0;
                        for (int i = 0; i< sens.Count;  i++)
                        {
                            if (sens[i].Length == 0)
                                continue;
                            Console.WriteLine(string.Format($"[{data[count][0]}-{data[count + sens[i].Length - 1][1]}]:{sens[i]}"));
                            count += sens[i].Length;
                        }
                    }
                    else
                    {
                        Console.WriteLine($"文件名称:{name} 文件转录内容: {text} 时间戳:{timestamp}");
                    }
                }
                catch (JsonException ex)
                {
                    Console.WriteLine("JSON 解析错误: " + ex.Message);
                }
            }
        }
 
        private void showWAVForm(WebsocketClient client, string file_name)
        {
            byte[] getbyte = FileToByte(file_name).Skip(44).ToArray();
 
            for (int i = 0; i < getbyte.Length; i += 1024000)
            {
                byte[] send = getbyte.Skip(i).Take(1024000).ToArray();
                client.Send(send);
                Thread.Sleep(5);
            }
            Thread.Sleep(10);
            client.Send("{\"is_speaking\": false}");
        }
 
        private void showWAVForm_All(WebsocketClient client, string file_name)
        {
            byte[] getbyte = FileToByte(file_name).ToArray();
            for (int i = 0; i < getbyte.Length; i += 1024000)
            {
                byte[] send = getbyte.Skip(i).Take(1024000).ToArray();
                client.Send(send);
                Thread.Sleep(5);
            }
            Thread.Sleep(10);
            client.Send("{\"is_speaking\": false}");
        }
 
        public byte[] FileToByte(string fileUrl)
        {
            try
            {
                using (FileStream fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read))
                {
                    byte[] byteArray = new byte[fs.Length];
                    fs.Read(byteArray, 0, byteArray.Length);
                    return byteArray;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}