C#调用微软库的实现语音识别

早期, 我们用SpeechRecognitionEngine如下来进行语音识别,但是复杂的词条加上环境噪声,其实成功率是不高的。

cs 复制代码
            SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-CN"));//初始化中文引擎
            Choices conmmonds = new Choices();
            conmmonds.Add(new string[] { "启动", "停止", "充电" });//添加词条
            GrammarBuilder gBuilder = new GrammarBuilder();
            gBuilder.Append(conmmonds);
            Grammar grammar = new DictationGrammar(); //new Grammar(gBuilder);
            grammar.Name = "default dictation";
            grammar.Enabled = true;
            recognizer.LoadGrammar(grammar);

            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Deal_SpeechRecongized);
            recognizer.SetInputToDefaultAudioDevice();//设置默认输入设备

            recognizer.InitialSilenceTimeout = TimeSpan.FromSeconds(3);
            recognizer.BabbleTimeout = TimeSpan.FromSeconds(2);
            recognizer.EndSilenceTimeout = TimeSpan.FromSeconds(1);
            recognizer.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(1.5);

            recognizer.RecognizeAsync(RecognizeMode.Multiple);//开始监控设备输入准备识别

            Speak(conmmonds + "测试语音");

另外一种方式,Microsoft的Speech SDK来实现语音的实时解读,然后根据接收到的文本内容进行处理,实现与用户的语音交互。

开发语音识别的需要使用注册Azure账号或测试api-key:

1. 准备工作

  • 安装SDK或库 :根据所选服务,下载并安装对应的C# SDK或NuGet包。比如,如果您选择Microsoft的Azure Cognitive Services中的语音服务,您就需要安装Microsoft.CognitiveServices.Speech NuGet包。

2. 实现语音识别

使用C#代码初始化语音识别客户端,监听麦克风输入,转换为文本。

cs 复制代码
1using Microsoft.CognitiveServices.Speech;
2using Microsoft.CognitiveServices.Speech.Audio;
3
4// 初始化SpeechConfig,使用您的API Key和区域
5var config = SpeechConfig.FromSubscription("your-api-key", "your-region");
6config.SpeechRecognitionLanguage = "zh-CN"; // 设置语言
7
8// 设置音频配置,使用默认麦克风
9using var audioConfig = AudioConfig.FromDefaultMicrophoneInput();
10using var recognizer = new SpeechRecognizer(config, audioConfig);
11
12Console.WriteLine("请说话...");
13
14// 开始连续语音识别
15var result = await recognizer.RecognizeOnceAsync();
16
17if (result.Reason == ResultReason.RecognizedSpeech)
18{
19    Console.WriteLine($"识别到的文本: {result.Text}");
20    // 在这里处理识别到的文本,比如调用另一个函数进行反馈处理
21}
22else if (result.Reason == ResultReason.NoMatch)
23{
24    Console.WriteLine("没有识别到有效语音输入。");
25}
26else if (result.Reason == ResultReason.Canceled)
27{
28    var cancellation = CancellationDetails.FromResult(result);
29    Console.WriteLine($"识别被取消: {cancellation.Reason}");
30}

3. 实现语音反馈

处理完识别到的文本后,可以使用相同的Speech SDK将处理结果转换为语音反馈给用户。

复制代码
1// 使用Text-to-Speech转换文本为语音
2var speechSynthesizer = new SpeechSynthesizer(config, null);
3
4var ssml = $"<speak version='1.0' xml:lang='zh-CN'><voice name='zh-CN-XiaoyiNeural'><prosody rate='medium'>{result.Text}</prosody></voice></speak>";
5var resultSynthesis = await speechSynthesizer.SynthesizeSsmlToWaveFileAsync(ssml, "output.wav");
6
7if (resultSynthesis.Reason == ResultReason.SynthesizingAudioCompleted)
8{
9    Console.WriteLine("语音合成完成。");
10    // 可以进一步处理,比如播放输出的音频文件
11}
12else if (resultSynthesis.Reason == ResultReason.Canceled)
13{
14    var cancellation = CancellationDetails.FromResult(resultSynthesis);
15    Console.WriteLine($"语音合成被取消: {cancellation.Reason}");
16}
相关推荐
Dingdangcat861 分钟前
YOLOX-L在钢丝绳损伤检测中的应用:基于300轮训练与COCO数据集的智能分类系统详解
人工智能·分类·数据挖掘
AI营销快线10 分钟前
2026 GEO服务商评测:原圈科技如何定义AI营销终局?
人工智能
天翼云开发者社区13 分钟前
天翼云全栈赋能OpenClaw,打造会干活的专属AI!
人工智能·智能体·openclaw
百***787514 分钟前
Clawdbot 技术实战:基于一步 API 快速接入,打造本地化 AI 自动化助手
运维·人工智能·自动化
阿正的梦工坊19 分钟前
Megatron中--train-iters和--max_epochs两个参数介绍
人工智能·深度学习·自然语言处理
人工智能AI技术19 分钟前
【C#程序员入门AI】向量数据库入门:C#集成Chroma/Pinecone,实现AI知识库检索(RAG基础)
人工智能·c#
jl486382124 分钟前
打造医疗设备的“可靠视窗”:医用控温仪专用屏从抗菌设计到EMC兼容的全链路解析
大数据·运维·人工智能·物联网·人机交互
kiro_102328 分钟前
BGRtoNV12与NV12toBGR互转函数
人工智能·opencv·计算机视觉
码农三叔29 分钟前
(9-1)电源管理与能源系统:电池选择与安全
人工智能·嵌入式硬件·安全·机器人·能源·人形机器人
司沐_Simuoss31 分钟前
Text to SQL系统的千层套路~
数据库·人工智能·sql·语言模型·系统架构