From 55732c93977518f51b0c0fd0daf46e15d88cb48e Mon Sep 17 00:00:00 2001
From: GlocKieHuan <77883010+GlocKieHuan@users.noreply.github.com>
Date: 星期四, 17 八月 2023 13:47:58 +0800
Subject: [PATCH] Add cshape websocket client (#846)
---
funasr/runtime/wss-client/FunASRWSClient_Online/Program.cs | 255 +++++++++++++++++
funasr/runtime/wss-client/confg/config.ini | 2
funasr/runtime/wss-client/FunASRWSClient_Online/WaveCollect.cs | 106 +++++++
funasr/runtime/wss-client/FunASRClient_CShape.sln | 31 ++
funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs | 120 ++++++++
funasr/runtime/wss-client/FunASRWSClient_Online/README.md | 9
funasr/runtime/wss-client/FunASRWSClient_Offline/Program.cs | 85 +++++
funasr/runtime/wss-client/FunASRClient_CShape.suo | 0
funasr/runtime/wss-client/FunASRWSClient_Online/FunASRWSClient_Online.csproj | 15 +
funasr/runtime/wss-client/FunASRWSClient_Offline/README.md | 9
funasr/runtime/wss-client/FunASRWSClient_Online/WebScoketClient.cs | 219 ++++++++++++++
funasr/runtime/wss-client/confg/tmp.wav | 0
funasr/runtime/wss-client/FunASRWSClient_Offline/FunASRWSClient_Offline.csproj | 14
13 files changed, 865 insertions(+), 0 deletions(-)
diff --git a/funasr/runtime/wss-client/FunASRClient_CShape.sln b/funasr/runtime/wss-client/FunASRClient_CShape.sln
new file mode 100644
index 0000000..74eb6fa
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRClient_CShape.sln
@@ -0,0 +1,31 @@
+锘�
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.6.33829.357
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunASRWSClient_Offline", "FunASRWSClient_Offline\FunASRWSClient_Offline.csproj", "{E0986CC4-D443-44E2-96E8-F6E4B691CA57}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunASRWSClient_Online", "FunASRWSClient_Online\FunASRWSClient_Online.csproj", "{11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {E0986CC4-D443-44E2-96E8-F6E4B691CA57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {E0986CC4-D443-44E2-96E8-F6E4B691CA57}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {E0986CC4-D443-44E2-96E8-F6E4B691CA57}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {E0986CC4-D443-44E2-96E8-F6E4B691CA57}.Release|Any CPU.Build.0 = Release|Any CPU
+ {11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {E8483245-31D3-4C42-AAF9-B3195EAB97C2}
+ EndGlobalSection
+EndGlobal
diff --git a/funasr/runtime/wss-client/FunASRClient_CShape.suo b/funasr/runtime/wss-client/FunASRClient_CShape.suo
new file mode 100644
index 0000000..a2bf849
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRClient_CShape.suo
Binary files differ
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Offline/FunASRWSClient_Offline.csproj b/funasr/runtime/wss-client/FunASRWSClient_Offline/FunASRWSClient_Offline.csproj
new file mode 100644
index 0000000..0604316
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Offline/FunASRWSClient_Offline.csproj
@@ -0,0 +1,14 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net6.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Websocket.Client" Version="4.6.1" />
+ </ItemGroup>
+
+</Project>
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Offline/Program.cs b/funasr/runtime/wss-client/FunASRWSClient_Offline/Program.cs
new file mode 100644
index 0000000..a0039e7
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Offline/Program.cs
@@ -0,0 +1,85 @@
+锘縰sing System.Collections.Specialized;
+using WebSocketSpace;
+
+namespace FunASRWSClient_Offline
+{
+ /// <summary>
+ /// /涓荤▼搴忓叆鍙�
+ /// </summary>
+ public class Program
+ {
+ private static void Main()
+ {
+ WSClient_Offline m_funasrclient = new WSClient_Offline();
+ m_funasrclient.FunASR_Main();
+ }
+ }
+
+ public class WSClient_Offline
+ {
+ public static string host = "0.0.0.0";
+ public static string port = "10095";
+ private static CWebSocketClient m_websocketclient = new CWebSocketClient();
+ [STAThread]
+ public async void FunASR_Main()
+ {
+ loadconfig();
+ //鍒濆鍖栭�氫俊杩炴帴
+ string errorStatus = string.Empty;
+ string commstatus = ClientConnTest();
+ if (commstatus != "閫氫俊杩炴帴鎴愬姛")
+ errorStatus = commstatus;
+ //绋嬪簭鍒濆鐩戞祴寮傚父--鎶ラ敊銆侀��鍑�
+ if (errorStatus != string.Empty)
+ {
+ //鎶ラ敊鏂瑰紡寰呭姞
+ Environment.Exit(0);
+ }
+
+ //寰幆杈撳叆鎺ㄧ悊鏂囦欢
+ while (true)
+ {
+ Console.WriteLine("璇疯緭鍏ヨ浆褰曟枃浠惰矾寰勶細");
+ string filepath = Console.ReadLine();
+ if (filepath != string.Empty && filepath != null)
+ {
+ await m_websocketclient.ClientSendFileFunc(filepath);
+ }
+ }
+ }
+ private void loadconfig()
+ {
+ string filePath = "config.ini";
+ NameValueCollection settings = new NameValueCollection();
+ using (StreamReader reader = new StreamReader(filePath))
+ {
+ string line;
+ while ((line = reader.ReadLine()) != null)
+ {
+ // 蹇界暐绌鸿鍜屾敞閲�
+ if (string.IsNullOrEmpty(line) || line.StartsWith(";") || line.StartsWith("#"))
+ continue;
+ // 瑙f瀽閿�煎
+ int equalsIndex = line.IndexOf('=');
+ if (equalsIndex > 0)
+ {
+ string key = line.Substring(0, equalsIndex).Trim();
+ string value = line.Substring(equalsIndex + 1).Trim();
+ if (key == "host")
+ host = value;
+ else if (key == "port")
+ port = value;
+ }
+ }
+ }
+ }
+ private static string ClientConnTest()
+ {
+ //WebSocket杩炴帴鐘舵�佺洃娴�
+ Task<string> websocketstatus = m_websocketclient.ClientConnTest();
+ if (websocketstatus != null && websocketstatus.Result.IndexOf("鎴愬姛") == -1)
+ return websocketstatus.Result;
+ return "閫氫俊杩炴帴鎴愬姛";
+ }
+ }
+}
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Offline/README.md b/funasr/runtime/wss-client/FunASRWSClient_Offline/README.md
new file mode 100644
index 0000000..3563560
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Offline/README.md
@@ -0,0 +1,9 @@
+# cshape-client-offline
+
+杩欐槸涓�涓熀浜嶧unASR-Websocket鏈嶅姟鍣ㄧ殑CShape瀹㈡埛绔紝鐢ㄤ簬杞綍鏈湴闊抽鏂囦欢銆�
+
+灏嗛厤缃枃浠舵斁鍦ㄤ笌绋嬪簭鐩稿悓鐩綍涓嬬殑config鏂囦欢澶逛腑锛屽苟鍦╟onfig.ini涓厤缃湇鍔″櫒ip鍦板潃鍜岀鍙e彿銆�
+
+閰嶇疆濂芥湇鍔$ip鍜岀鍙e彿锛屽湪vs涓墦寮�闇�娣诲姞Websocket.Client鐨凬uget绋嬪簭鍖呭悗锛屽彲鐩存帴杩涜娴嬭瘯锛屾寜鐓ф帶鍒跺彴鎻愮ず鎿嶄綔鍗冲彲銆�
+
+娉細鏈鎴风鏆傛敮鎸亀av鏂囦欢锛屽湪win11涓嬪畬鎴愭祴璇曪紝缂栬瘧鐜VS2022銆�
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs b/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs
new file mode 100644
index 0000000..9208524
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs
@@ -0,0 +1,120 @@
+锘縰sing Websocket.Client;
+using System.Text.Json;
+using System.Reactive.Linq;
+using FunASRWSClient_Offline;
+
+namespace WebSocketSpace
+{
+ internal class CWebSocketClient
+ {
+ private static readonly Uri serverUri = new Uri($"ws://{WSClient_Offline.host}:{WSClient_Offline.port}"); // 浣犺杩炴帴鐨刉ebSocket鏈嶅姟鍣ㄥ湴鍧�
+ 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)//鏂囦欢杞綍
+ {
+ try
+ {
+ if (client.IsRunning)
+ {
+ var exitEvent = new ManualResetEvent(false);
+ string path = Path.GetFileName(file_name);
+ string firstbuff = string.Format("{{\"mode\": \"offline\", \"wav_name\": \"{0}\", \"is_speaking\": true}}", Path.GetFileName(file_name));
+ client.Send(firstbuff);
+ showWAVForm(client, file_name);
+ }
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.ToString());
+ }
+ return Task.CompletedTask;
+ }
+
+ public void recmessage(string message)
+ {
+ if (message != null)
+ {
+ try
+ {
+ 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(name == "asr_stream")
+ Console.WriteLine($"瀹炴椂璇嗗埆鍐呭: {text}");
+ else
+ Console.WriteLine($"鏂囦欢鍚嶇О:{name} 鏂囦欢杞綍鍐呭: {text}");
+ }
+ catch (JsonException ex)
+ {
+ Console.WriteLine("JSON 瑙f瀽閿欒: " + 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}");
+ }
+
+ 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;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/FunASRWSClient_Online.csproj b/funasr/runtime/wss-client/FunASRWSClient_Online/FunASRWSClient_Online.csproj
new file mode 100644
index 0000000..75fc029
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/FunASRWSClient_Online.csproj
@@ -0,0 +1,15 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net6.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="NAudio" Version="2.1.0" />
+ <PackageReference Include="Websocket.Client" Version="4.6.1" />
+ </ItemGroup>
+
+</Project>
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/Program.cs b/funasr/runtime/wss-client/FunASRWSClient_Online/Program.cs
new file mode 100644
index 0000000..ec0c5e4
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/Program.cs
@@ -0,0 +1,255 @@
+锘縰sing AliFsmnVadSharp;
+using NAudio.Wave;
+using System.Collections.Concurrent;
+using WebSocketSpace;
+using NAudio.CoreAudioApi;
+using System.IO;
+using System.Collections.Specialized;
+
+namespace FunASRWSClient_Online
+{
+ /// <summary>
+ /// /涓荤▼搴忓叆鍙�
+ /// </summary>
+ public class Program
+ {
+ private static void Main()
+ {
+ WSClient_Online m_funasrclient = new WSClient_Online();
+ m_funasrclient.FunASR_Main();
+ }
+ }
+ /// <summary>
+ /// /涓荤嚎绋嬪叆鍙o紝鍒濆鍖栧悗璇诲彇鏁版嵁
+ /// </summary>
+ public class WSClient_Online
+ {
+ /// <summary>
+ /// FunASR瀹㈡埛绔蒋浠惰繍琛岀姸鎬�
+ /// </summary>
+ ///
+ public static string host = "0.0.0.0";
+ public static string port = "10095";
+ public static string onlineasrmode = string.Empty;
+ private static WaveCollect m_wavecollect = new WaveCollect();
+ private static CWebSocketClient m_websocketclient = new CWebSocketClient();
+ public static readonly ConcurrentQueue<byte[]> ActiveAudioSet = new ConcurrentQueue<byte[]>();
+ public static readonly ConcurrentQueue<string> AudioFileQueue = new ConcurrentQueue<string>();
+ [STAThread]
+ public void FunASR_Main()
+ {
+ loadconfig();
+ //楹﹀厠椋庣姸鎬佺洃娴�
+ string errorStatus = string.Empty;
+ if (GetCurrentMicVolume() == -2)
+ errorStatus = "娉ㄦ剰锛氶害鍏嬮琚缃负闈欓煶锛�";
+ else if (GetCurrentMicVolume() == -1)
+ errorStatus = "娉ㄦ剰锛氶害鍏嬮鏈繛鎺ワ紒";
+ else if (GetCurrentMicVolume() == 0)
+ errorStatus = "娉ㄦ剰锛氶害鍏嬮澹伴煶璁剧疆涓�0锛�";
+
+ //鍒濆鍖栭�氫俊杩炴帴
+ string commstatus = ClientConnTest();
+ if (commstatus != "閫氫俊杩炴帴鎴愬姛")
+ errorStatus = commstatus;
+ //绋嬪簭鍒濆鐩戞祴寮傚父--鎶ラ敊銆侀��鍑�
+ if (errorStatus != string.Empty)
+ {
+ Environment.Exit(0);//鎶ラ敊鏂瑰紡寰呭姞
+ }
+
+ //鍚姩瀹㈡埛绔悜鏈嶅姟绔彂閫侀煶棰戞暟鎹嚎绋�
+ Thread SendAudioThread = new Thread(SendAudioToSeverAsync);
+ SendAudioThread.Start();
+
+ //鍚姩闊抽鏂囦欢杞綍绾跨▼
+ Thread AudioFileThread = new Thread(SendAudioFileToSeverAsync);
+ AudioFileThread.Start();
+
+ while (true)
+ {
+ Console.WriteLine("璇烽�夋嫨璇煶璇嗗埆鏂瑰紡锛�1.绂荤嚎鏂囦欢杞啓锛�2.瀹炴椂璇煶璇嗗埆");
+ string str = Console.ReadLine();
+ if (str != string.Empty)
+ {
+ if (str == "1")//绂荤嚎鏂囦欢杞啓
+ {
+ onlineasrmode = "offline";
+ Console.WriteLine("璇疯緭鍏ヨ浆褰曟枃浠惰矾寰�");
+ str = Console.ReadLine();
+ if (!string.IsNullOrEmpty(str))
+ AudioFileQueue.Enqueue(str);
+ }
+ else if (str == "2")//瀹炴椂璇煶璇嗗埆
+ {
+ Console.WriteLine("璇疯緭鍏ュ疄鏃惰闊宠瘑鍒ā寮忥細1.online锛�2.2pass");
+ str = Console.ReadLine();
+ OnlineASR(str);
+ }
+ }
+ }
+ }
+ private void loadconfig()
+ {
+ string filePath = "config.ini";
+ NameValueCollection settings = new NameValueCollection();
+ using (StreamReader reader = new StreamReader(filePath))
+ {
+ string line;
+ while ((line = reader.ReadLine()) != null)
+ {
+ // 蹇界暐绌鸿鍜屾敞閲�
+ if (string.IsNullOrEmpty(line) || line.StartsWith(";") || line.StartsWith("#"))
+ continue;
+ // 瑙f瀽閿�煎
+ int equalsIndex = line.IndexOf('=');
+ if (equalsIndex > 0)
+ {
+ string key = line.Substring(0, equalsIndex).Trim();
+ string value = line.Substring(equalsIndex + 1).Trim();
+ if (key == "host")
+ host = value;
+ else if (key == "port")
+ port = value;
+ }
+ }
+ }
+ }
+ private void OnlineASR(string str)
+ {
+ if (!string.IsNullOrEmpty(str))
+ {
+ if (str == "1")//瀹炴椂璇煶璇嗗埆
+ onlineasrmode = "online";
+ else if (str == "2")//瀹炴椂璇煶璇嗗埆-鍔ㄦ�佷慨姝�
+ onlineasrmode = "2pass";
+ }
+ //寮�濮嬪綍鍒跺0闊炽�佸彂閫佽瘑鍒�
+ if (onlineasrmode != string.Empty)
+ {
+ m_wavecollect.StartRec();
+ m_websocketclient.ClientFirstConnOnline(onlineasrmode);
+ try
+ {
+ while (true)
+ {
+ if (!WaveCollect.voicebuff.IsEmpty)
+ {
+ byte[] buff;
+ int buffcnt = WaveCollect.voicebuff.Count;
+ WaveCollect.voicebuff.TryDequeue(out buff);
+ if (buff != null)
+ ActiveAudioSet.Enqueue(buff);
+ }
+ else
+ {
+ if (Console.KeyAvailable)
+ {
+ var key = Console.ReadKey(true);
+
+ // 妫�娴嬪埌鎸変笅Ctrl+C
+ if ((key.Modifiers & ConsoleModifiers.Control) != 0 && key.Key == ConsoleKey.C)
+ {
+ // 鎵ц鐩稿簲鐨勬搷浣�
+ Console.WriteLine("Ctrl+C Pressed!");
+ // 閫�鍑哄惊鐜垨鎵ц鍏朵粬鎿嶄綔
+ break;
+ }
+ }
+ else
+ {
+ Thread.Sleep(10);
+ }
+ }
+ }
+ }
+ catch
+ {
+ Console.WriteLine("瀹炴椂璇嗗埆鍑虹幇寮傚父锛�");
+ }
+ finally
+ {
+ m_wavecollect.StopRec();
+ m_websocketclient.ClientLastConnOnline();
+ }
+ }
+ }
+
+ private string ClientConnTest()
+ {
+ //WebSocket杩炴帴鐘舵�佺洃娴�
+ Task<string> websocketstatus = m_websocketclient.ClientConnTest();
+ if (websocketstatus != null && websocketstatus.Result.IndexOf("鎴愬姛") == -1)
+ return websocketstatus.Result;
+ return "閫氫俊杩炴帴鎴愬姛";
+ }
+ private void SendAudioFileToSeverAsync()
+ {
+ while (true)
+ {
+ Thread.Sleep(1000);
+ if (AudioFileQueue.Count > 0)
+ {
+ string filepath = string.Empty;
+ AudioFileQueue.TryDequeue(out filepath);
+ if (filepath != string.Empty && filepath != null)
+ {
+ m_websocketclient.ClientSendFileFunc(filepath);
+ }
+ }
+ else
+ {
+ Thread.Sleep(100);
+ }
+ }
+ }
+ private void SendAudioToSeverAsync()
+ {
+ while (true)
+ {
+ if (ActiveAudioSet.Count > 0)
+ {
+ byte[] audio;
+ ActiveAudioSet.TryDequeue(out audio);
+ if (audio == null)
+ continue;
+
+ byte[] mArray = new byte[audio.Length];
+ Array.Copy(audio, 0, mArray, 0, audio.Length);
+ if (mArray != null)
+ m_websocketclient.ClientSendAudioFunc(mArray);
+ }
+ else
+ {
+ Thread.Sleep(10);
+ }
+ }
+ }
+
+ private void SaveAsWav(byte[] pcmData, string fileName, int sampleRate, int bitsPerSample, int channels)
+ {
+ using (var writer = new WaveFileWriter(fileName, new WaveFormat(sampleRate, bitsPerSample, channels)))
+ {
+ writer.Write(pcmData, 0, pcmData.Length);
+ }
+ }
+
+ private int GetCurrentMicVolume() //鑾峰彇楹﹀厠椋庤缃�
+ {
+ int volume = -1;
+ var enumerator = new MMDeviceEnumerator();
+
+ //鑾峰彇闊抽杈撳叆璁惧
+ IEnumerable<MMDevice> captureDevices = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active).ToArray();
+ if (captureDevices.Count() > 0)
+ {
+ MMDevice mMDevice = captureDevices.ToList()[0];
+ if (mMDevice.AudioEndpointVolume.Mute)
+ return -2;
+ volume = (int)(mMDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
+
+ }
+ return volume;
+ }
+ }
+}
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/README.md b/funasr/runtime/wss-client/FunASRWSClient_Online/README.md
new file mode 100644
index 0000000..c828c93
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/README.md
@@ -0,0 +1,9 @@
+# cshape-client-online
+
+杩欐槸涓�涓熀浜嶧unASR-Websocket鏈嶅姟鍣ㄧ殑CShape瀹㈡埛绔紝鐢ㄤ簬瀹炴椂璇煶璇嗗埆鍜岃浆褰曟湰鍦伴煶棰戞枃浠躲��
+
+灏嗛厤缃枃浠舵斁鍦ㄤ笌绋嬪簭鐩稿悓鐩綍涓嬬殑config鏂囦欢澶逛腑锛屽苟鍦╟onfig.ini涓厤缃湇鍔″櫒ip鍦板潃鍜岀鍙e彿銆�
+
+閰嶇疆濂芥湇鍔$ip鍜岀鍙e彿锛屽湪vs涓墦寮�闇�娣诲姞NAudio鍜學ebsocket.Client鐨凬uget绋嬪簭鍖呭悗锛屽彲鐩存帴杩涜娴嬭瘯锛屾寜鐓ф帶鍒跺彴鎻愮ず鎿嶄綔鍗冲彲銆�
+
+娉細瀹炴椂璇煶璇嗗埆浣跨敤online鎴�2pass锛岃浆褰曟枃浠堕粯璁や娇鐢╫ffline锛屽湪win11涓嬪畬鎴愭祴璇曪紝缂栬瘧鐜VS2022銆�
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/WaveCollect.cs b/funasr/runtime/wss-client/FunASRWSClient_Online/WaveCollect.cs
new file mode 100644
index 0000000..f927b5e
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/WaveCollect.cs
@@ -0,0 +1,106 @@
+锘縰sing System.Collections.Concurrent;
+using NAudio.Wave;
+using NAudio.CoreAudioApi;
+
+namespace AliFsmnVadSharp
+{
+ class WaveCollect
+ {
+ private string fileName = string.Empty;
+ private WaveInEvent? waveSource = null;
+ private WaveFileWriter? waveFile = null;
+ public static int wave_buffer_milliseconds = 600;
+ public static int wave_buffer_collectbits = 16;
+ public static int wave_buffer_collectchannels = 1;
+ public static int wave_buffer_collectfrequency = 16000;
+ public static readonly ConcurrentQueue<byte[]> voicebuff = new ConcurrentQueue<byte[]>();
+
+ public void StartRec()
+ {
+ // 鑾峰彇楹﹀厠椋庤澶�
+ var captureDevices = new MMDeviceEnumerator().EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
+ foreach (var device in captureDevices)
+ {
+ Console.WriteLine("Device Name: " + device.FriendlyName);
+ using (var capture = new WasapiLoopbackCapture(device))
+ {
+ // 鑾峰彇鏀寔鐨勯噰鏍风巼鍒楄〃
+ Console.WriteLine("Device Channels:" + capture.WaveFormat.Channels);
+ Console.WriteLine("Device SampleRate:" + capture.WaveFormat.SampleRate);
+ Console.WriteLine("Device BitsPerSample:" + capture.WaveFormat.BitsPerSample);
+ }
+ }
+ //娓呯┖缂撳瓨鏁版嵁
+ int buffnum = voicebuff.Count;
+ for (int i = 0; i < buffnum; i++)
+ voicebuff.TryDequeue(out byte[] buff);
+
+ waveSource = new WaveInEvent();
+ waveSource.BufferMilliseconds = wave_buffer_milliseconds;
+ waveSource.WaveFormat = new WaveFormat(wave_buffer_collectfrequency, wave_buffer_collectbits, wave_buffer_collectchannels); // 16bit,16KHz,Mono鐨勫綍闊虫牸寮�
+ waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
+ SetFileName(AppDomain.CurrentDomain.BaseDirectory + "tmp.wav");
+ waveFile = new WaveFileWriter(fileName, waveSource.WaveFormat);
+ waveSource.StartRecording();
+ }
+
+ public void StopRec()
+ {
+ if (waveSource != null)
+ {
+ waveSource.StopRecording();
+ if (waveSource != null)
+ {
+ waveSource.Dispose();
+ waveSource = null;
+ }
+ if (waveFile != null)
+ {
+ waveFile.Dispose();
+ waveFile = null;
+ }
+ }
+ }
+
+ public void SetFileName(string fileName)
+ {
+ this.fileName = fileName;
+ }
+
+ private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
+ {
+ if (waveFile != null)
+ {
+ if (e.Buffer != null && e.BytesRecorded > 0)
+ {
+ voicebuff.Enqueue(e.Buffer);
+ //waveFile.Write(e.Buffer, 0, e.BytesRecorded);
+ waveFile.Flush();
+ }
+
+ }
+ }
+
+ public static byte[] Wavedata_Dequeue()
+ {
+ byte[] datas;
+ voicebuff.TryDequeue(out datas);
+ return datas;
+ }
+
+ private void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
+ {
+ if (waveSource != null)
+ {
+ waveSource.Dispose();
+ waveSource = null;
+ }
+
+ if (waveFile != null)
+ {
+ waveFile.Dispose();
+ waveFile = null;
+ }
+ }
+ }
+}
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/WebScoketClient.cs b/funasr/runtime/wss-client/FunASRWSClient_Online/WebScoketClient.cs
new file mode 100644
index 0000000..69cbe4d
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/WebScoketClient.cs
@@ -0,0 +1,219 @@
+锘縰sing System.Net.WebSockets;
+using Websocket.Client;
+using System.Text.Json;
+using NAudio.Wave;
+using AliFsmnVadSharp;
+using System.Reactive.Linq;
+using FunASRWSClient_Online;
+
+namespace WebSocketSpace
+{
+ internal class CWebSocketClient
+ {
+ private static int chunk_interval = 10;
+ private static int[] chunk_size = new int[] { 5, 10, 5 };
+ private static readonly Uri serverUri = new Uri($"ws://{WSClient_Online.host}:{WSClient_Online.port}"); // 浣犺杩炴帴鐨刉ebSocket鏈嶅姟鍣ㄥ湴鍧�
+ 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 =>
+ {
+ rec_message(msg.Text, client);
+ });
+
+ await client.Start();
+
+ if (client.IsRunning)
+ commstatus = "WebSocket閫氫俊杩炴帴鎴愬姛";
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.ToString());
+ client.Dispose();
+ }
+
+ return commstatus;
+ }
+
+ public bool ClientFirstConnOnline(string asrmode)
+ {
+ if (client.IsRunning)
+ {
+ string firstbuff = string.Format("{{\"mode\": \"{0}\", \"chunk_size\": [{1},{2},{3}], \"chunk_interval\": {4}, \"wav_name\": \"microphone\", \"is_speaking\": true}}"
+ , asrmode, chunk_size[0], chunk_size[1], chunk_size[2], chunk_interval);
+ Task.Run(() => client.Send(firstbuff));
+ }
+ else
+ {
+ client.Reconnect();
+ return false;
+ }
+
+ return true;
+ }
+ public bool ClientSendAudioFunc(byte[] buff) //瀹炴椂璇嗗埆
+ {
+ if (client.IsRunning)
+ {
+ ////鍙戦�侀煶棰戞暟鎹�
+ int CHUNK = WaveCollect.wave_buffer_collectfrequency / 1000 * 60 * chunk_size[1] / chunk_interval;
+ for (int i = 0; i < buff.Length; i += CHUNK)
+ {
+ byte[] send = buff.Skip(i).Take(CHUNK).ToArray();
+ Task.Run(() => client.Send(send));
+ Thread.Sleep(1);
+ }
+ }
+ else
+ {
+ client.Reconnect();
+ return false;
+ }
+
+ return true;
+ }
+ public void ClientLastConnOnline()
+ {
+ Task.Run(() => client.Send("{\"is_speaking\": false}"));
+ }
+
+ public int ClientSendFileFunc(string file_name)//鏂囦欢杞綍 0:鍙戦�佹垚鍔� ret -1:鏂囦欢绫诲瀷涓嶆敮鎸� -2:閫氫俊鏂紑
+ {
+ string fileExtension = Path.GetExtension(file_name);
+ fileExtension = fileExtension.Replace(".", "");
+ if (!(fileExtension == "mp3" || fileExtension == "mp4" || fileExtension == "wav" || fileExtension == "pcm"))
+ return -1;
+
+ if (client.IsRunning)
+ {
+ if (fileExtension == "wav" || fileExtension == "pcm")
+ {
+ string firstbuff = string.Format("{{\"mode\": \"office\", \"chunk_size\": [{0},{1},{2}], \"chunk_interval\": {3}, \"wav_name\": \"{4}\", \"is_speaking\": true, \"wav_format\":\"pcm\"}}"
+ , chunk_size[0], chunk_size[1], chunk_size[2], chunk_interval, Path.GetFileName(file_name));
+ Task.Run(() => client.Send(firstbuff));
+ if (fileExtension == "wav")
+ showWAVForm(file_name);
+ else if (fileExtension == "pcm")
+ showWAVForm_All(file_name);
+ }
+ else if (fileExtension == "mp3" || fileExtension == "mp4")
+ {
+ string firstbuff = string.Format("{{\"mode\": \"offline\", \"chunk_size\": \"{0},{1},{2}\", \"chunk_interval\": {3}, \"wav_name\": \"{4}\", \"is_speaking\": true, \"wav_format\":\"{5}\"}}"
+ , chunk_size[0], chunk_size[1], chunk_size[2], chunk_interval, Path.GetFileName(file_name), fileExtension);
+ Task.Run(() => client.Send(firstbuff));
+ showWAVForm_All(file_name);
+ }
+ }
+ else
+ {
+ client.Reconnect();
+ return -2;
+ }
+
+ return 0;
+ }
+ private string recbuff = string.Empty;//鎺ユ敹绱缂撳瓨鍐呭
+ private string onlinebuff = string.Empty;//鎺ユ敹绱鍦ㄧ嚎缂撳瓨鍐呭
+ public void rec_message(string message, WebsocketClient client)
+ {
+ if (message != null)
+ {
+ try
+ {
+ string name = string.Empty;
+ JsonDocument jsonDoc = JsonDocument.Parse(message);
+ JsonElement root = jsonDoc.RootElement;
+ string mode = root.GetProperty("mode").GetString();
+ string text = root.GetProperty("text").GetString();
+ bool isfinal = root.GetProperty("is_final").GetBoolean();
+ if (message.IndexOf("wav_name ") != -1)
+ name = root.GetProperty("wav_name").GetString();
+
+ //if (name == "microphone")
+ // Console.WriteLine($"瀹炴椂璇嗗埆鍐呭: {text}");
+ //else
+ // Console.WriteLine($"鏂囦欢鍚嶇О:{name} 鏂囦欢杞綍鍐呭: {text}");
+
+ if (mode == "2pass-online" && WSClient_Online.onlineasrmode != "offline")
+ {
+ onlinebuff += text;
+ Console.WriteLine(recbuff + onlinebuff);
+ }
+ else if (mode == "2pass-offline")
+ {
+ recbuff += text;
+ onlinebuff = string.Empty;
+ Console.WriteLine(recbuff);
+ }
+
+ if (isfinal && WSClient_Online.onlineasrmode != "offline")//鏈粨鏉熷綋鍓嶈瘑鍒�
+ {
+ recbuff = string.Empty;
+ }
+ }
+ catch (JsonException ex)
+ {
+ Console.WriteLine("JSON 瑙f瀽閿欒: " + ex.Message);
+ }
+ }
+ }
+
+ private void showWAVForm(string file_name)
+ {
+ byte[] getbyte = FileToByte(file_name).Skip(44).ToArray();
+
+ for (int i = 0; i < getbyte.Length; i += 102400)
+ {
+ byte[] send = getbyte.Skip(i).Take(102400).ToArray();
+ Task.Run(() => client.Send(send));
+ Thread.Sleep(5);
+ }
+ Thread.Sleep(100);
+ Task.Run(() => client.Send("{\"is_speaking\": false}"));
+ }
+
+ private void showWAVForm_All(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();
+ Task.Run(() => client.Send(send));
+ Thread.Sleep(5);
+ }
+ Thread.Sleep(10);
+ Task.Run(() => 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;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/confg/config.ini b/funasr/runtime/wss-client/confg/config.ini
new file mode 100644
index 0000000..8de6518
--- /dev/null
+++ b/funasr/runtime/wss-client/confg/config.ini
@@ -0,0 +1,2 @@
+host=127.0.0.1
+port=10095
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/confg/tmp.wav b/funasr/runtime/wss-client/confg/tmp.wav
new file mode 100644
index 0000000..0f74f14
--- /dev/null
+++ b/funasr/runtime/wss-client/confg/tmp.wav
Binary files differ
--
Gitblit v1.9.1