chore: import upstream snapshot with attribution
This commit is contained in:
@@ -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>
|
||||
@@ -0,0 +1,115 @@
|
||||
using 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";
|
||||
public static string hotword = null;
|
||||
private static CWebSocketClient m_websocketclient = new CWebSocketClient();
|
||||
[STAThread]
|
||||
public async void FunASR_Main()
|
||||
{
|
||||
loadconfig();
|
||||
loadhotword();
|
||||
//初始化通信连接
|
||||
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;
|
||||
// 解析键值对
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
static void loadhotword()
|
||||
{
|
||||
string filePath = "hotword.txt";
|
||||
try
|
||||
{
|
||||
// 使用 StreamReader 打开文本文件
|
||||
using (StreamReader sr = new StreamReader(filePath))
|
||||
{
|
||||
string line;
|
||||
// 逐行读取文件内容
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
hotword += line;
|
||||
hotword += " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("读取文件时发生错误:" + ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (hotword.Length > 0 && hotword[hotword.Length - 1] == ' ')
|
||||
hotword = hotword.Substring(0,hotword.Length - 1);
|
||||
}
|
||||
}
|
||||
private static string ClientConnTest()
|
||||
{
|
||||
//WebSocket连接状态监测
|
||||
Task<string> websocketstatus = m_websocketclient.ClientConnTest();
|
||||
if (websocketstatus != null && websocketstatus.Result.IndexOf("成功") == -1)
|
||||
return websocketstatus.Result;
|
||||
return "通信连接成功";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
# cshape-client-offline
|
||||
|
||||
这是一个基于FunASR-Websocket服务器的CShape客户端,用于转录本地音频文件。
|
||||
|
||||
将配置文件放在与程序相同目录下的config文件夹中,并在config.ini中配置服务器ip地址和端口号。
|
||||
|
||||
配置好服务端ip和端口号,在vs中打开需添加Websocket.Client的Nuget程序包后,可直接进行测试,按照控制台提示操作即可。
|
||||
|
||||
更新:支持热词和时间戳,热词需将config文件夹下的hotword.txt放置在执行路径下。
|
||||
|
||||
注:运行后台须注意热词和时间戳为不同模型,本客户端在win11下完成测试,编译环境VS2022。
|
||||
@@ -0,0 +1,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user