Teams Bot机器人实时语音识别的多引擎的处理

之前开发的Teams的Bot 是使用微软的实时语音识别。现在增加了一个选项,可以在Azure 和 Soniox两引擎间切换。

Azure 的实时语音识别

Azure 的实时语音识别 使用 Microsoft.CognitiveServices.Speech.Transcription 进行处理。这个质量很不错,而且可以设置每个人的语音特征(wav audio file for creating voice signatures must be 16-bit, 16 kHz sample rate, in single channel (mono) format. The recommended length for each audio sample is between 30 seconds and two minutes. )。

cs 复制代码
            byte[] fileBytes = File.ReadAllBytes(fn);
            var content = new ByteArrayContent(fileBytes);
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
            var response = await client.PostAsync($"https://signature.{region}.cts.speech.microsoft.com/api/v1/Signature/GenerateVoiceSignatureFromByteArray", content);

            var jsonData = await response.Content.ReadAsStringAsync();
            var result = JsonConvert.DeserializeObject<VoiceSignature>(jsonData);
            return JsonConvert.SerializeObject(result.Signature);

设置了语音特征后就可以在实时语音识别中,自动识别说话者。一切都好,唯一的一个问题就是不便宜,目前一个月的费用有几万美元。

Soniox的实时语音识别

Soniox的实时语音识别引擎,是用WebSocket的,这一点和Azure的差别很大。下面的代码就是初始化识别引擎连接。

cs 复制代码
 ClientWebSocket ws= new ClientWebSocket();
 ws.ConnectAsync(new Uri(ConversationMgr.instance.SonioxUrl), CancellationToken.None).Wait();

 // Send start request with correct field names
 var startMessage = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new
 {
     api_key = ConversationMgr.instance.SonioxKey,
     audio_format = ConversationMgr.instance.SonioxAudioFormat,
     sample_rate = int.Parse(ConversationMgr.instance.SonioxSampleRate),
     num_channels = int.Parse(ConversationMgr.instance.SonioxNumChannels),
     model = ConversationMgr.instance.SonioxModel,
     enable_speaker_diarization=true,
     language_hints = ConversationMgr.instance.SonioxLanguageHints.Replace(" ", "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
 }));

 ws.SendAsync(new ArraySegment<byte>(startMessage), WebSocketMessageType.Text, true, CancellationToken.None).Wait();

初始化之后,当收到从机器人收到语音包后,就把包的时间发送到Soniox。

var audioChunk = new ArraySegment<byte>(item.Buff, 0, item.Buff.Length);

await ws.SendAsync(audioChunk, WebSocketMessageType.Binary, true, CancellationToken.None);

同时并行一个任务从 Soniox接受识别的结果:

result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);

返回的结果如下

javascript 复制代码
{"tokens":[{"text":"Wh","start_ms":540,"end_ms":540,"confidence":0.999,"is_final":false,"speaker":"1"},{"text":"at","start_ms":540,"end_ms":600,"confidence":1,"is_final":false,"speaker":"1"},{"text":" is","start_ms":660,"end_ms":720,"confidence":0.998,"is_final":false,"speaker":"1"},{"text":" y","start_ms":780,"end_ms":840,"confidence":1,"is_final":false,"speaker":"1"},{"text":"our","start_ms":840,"end_ms":900,"confidence":1,"is_final":false,"speaker":"1"},{"text":" best","start_ms":960,"end_ms":1020,"confidence":1,"is_final":false,"speaker":"1"}],"final_audio_proc_ms":0,"total_audio_proc_ms":1800}

{

"tokens": [

{

"text": "Wh",

"start_ms": 540,

"end_ms": 540,

"confidence": 0.999,

"is_final": false,

"speaker": "1"

},

{

"text": "at",

"start_ms": 540,

"end_ms": 600,

"confidence": 1,

"is_final": false,

"speaker": "1"

},

{

"text": " is",

"start_ms": 660,

"end_ms": 720,

"confidence": 0.998,

"is_final": false,

"speaker": "1"

},

{

"text": " y",

"start_ms": 780,

"end_ms": 840,

"confidence": 1,

"is_final": false,

"speaker": "1"

},

{

"text": "our",

"start_ms": 840,

"end_ms": 900,

"confidence": 1,

"is_final": false,

"speaker": "1"

},

{

"text": " best",

"start_ms": 960,

"end_ms": 1020,

"confidence": 1,

"is_final": false,

"speaker": "1"

}

],

"final_audio_proc_ms": 0,

"total_audio_proc_ms": 1800

}

相关推荐
慧都小妮子3 小时前
SciChart WPF v9.0 更新详解:矢量场图表、堆叠箱线图与 MVVM 轴同步来了
c#·.net·wpf·数据可视化·图表控件·图表
renhongxia15 小时前
Scaling Law撞墙了吗?大模型的“规模法则”走向何方
人工智能·深度学习·机器学习·架构·机器人
会的东西有点杂6 小时前
ROS 2的架构
机器人
yongshao87 小时前
通过iiQWorks.Sim软件配置kuka机械臂的外部轴
机器人·自动化
鱼听禅7 小时前
C#学习笔记-Entity Framework Core基础操作学习
c#·orm·ef core
2601_958352907 小时前
从智能安防到矿山预警:AR1105声源定位模组的行业应用与架构解析
人工智能·嵌入式硬件·架构·机器人·回音消除·声源定位
Z5998178418 小时前
c#软件开发学习笔记--分组、游标与临时表、分页
笔记·学习·c#
rick9778 小时前
用 C# 从零搭建本地知识库:RAG 系统完整实战
c#
沫儿笙8 小时前
弧焊机器人氩气省气装置
人工智能·机器人