From c2e4e3c2e9be855277d9f4fa9cd0544892ff829a Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期三, 30 八月 2023 09:57:30 +0800
Subject: [PATCH] Merge branch 'main' of github.com:alibaba-damo-academy/FunASR add
---
funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs | 75 +++++++++++++++++++++++++++++++++----
1 files changed, 66 insertions(+), 9 deletions(-)
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs b/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs
index 9208524..350aa20 100644
--- a/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs
+++ b/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs
@@ -2,6 +2,7 @@
using System.Text.Json;
using System.Reactive.Linq;
using FunASRWSClient_Offline;
+using System.Text.RegularExpressions;
namespace WebSocketSpace
{
@@ -45,15 +46,31 @@
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)
{
- 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);
+ 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)
@@ -69,15 +86,42 @@
{
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 text = root.GetProperty("text").GetString();
string name = root.GetProperty("wav_name").GetString();
- if(name == "asr_stream")
- Console.WriteLine($"瀹炴椂璇嗗埆鍐呭: {text}");
+ 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}");
+ {
+ Console.WriteLine($"鏂囦欢鍚嶇О:{name} 鏂囦欢杞綍鍐呭: {text} 鏃堕棿鎴筹細{timestamp}");
+ }
}
catch (JsonException ex)
{
@@ -100,6 +144,19 @@
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
--
Gitblit v1.9.1