记录c#实现语音转文字的方式(无网络接口) whisper.net C#使用微软的Speech.Recognition库实现语音识别转文字
using System;
using System.Speech.Recognition;
class Program
{
static void Main()
{
// 创建语音识别引擎
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
// 配置语法
Choices commands = new Choices();
commands.Add(new string[] { "hello", "open notepad", "close notepad", "exit" });
GrammarBuilder grammarBuilder = new GrammarBuilder(commands);
Grammar grammar = new Grammar(grammarBuilder);
// 将语法添加到识别引擎
recognizer.LoadGrammar(grammar);
// 设置事件处理程序来处理识别结果
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// 启动语音识别
recognizer.SetInputToDefaultAudioDevice();
recognizer.RecognizeAsync(RecognizeMode.Multiple);
Console.WriteLine("Listening for commands. Press Enter to exit.");
Console.ReadLine();
// 停止识别并释放资源
recognizer.RecognizeAsyncStop();
recognizer.Dispose();
}
private static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result != null && e.Result.Confidence > 0.6)
{
string recognizedText = e.Result.Text;
Console.WriteLine("Recognized: " + recognizedText);
// 在此处可以根据 recognizedText 执行相应的操作
// 例如,执行命令、触发事件等
}
}
}
using System.Speech.Recognition;
static SpeechRecognitionEngine speechRecognition = new SpeechRecognitionEngine();//创建语言识别对象
static void Main(string[] args)
{
Choices choices = new Choices();//创建选择
choices.Add(new string[] { "你好", "谢谢" });//添加要监听、识别的词组
GrammarBuilder gb = new GrammarBuilder();//语法构建
gb.Append(choices);//加载选择
Grammar gr = new Grammar(gb);//创建语法
speechRecognition.LoadGrammarAsync(gr);
speechRecognition.SetInputToDefaultAudioDevice();
speechRecognition.SpeechRecognized += speechRecognition_response;
speechRecognition.RecognizeAsync(RecognizeMode.Multiple);//启动
Console.ReadLine();
}
public static void speechRecognition_response(object sender, SpeechRecognizedEventArgs e)
{
//识别到语法里的词组、则进入函数
switch (e.Result.Text)//判断识别到的词组是哪个
{
case "你好":
Console.WriteLine("你好");
break;
case "谢谢":
Console.WriteLine("谢谢");
break;
}
}
在C#中创建一个简单的本地语音转文字API,可以使用Windows自带的语音识别功能。以下是一个基于Windows Speech API 3.0的示例代码。
首先,确保安装了语音识别引擎,在控制面板的“语言”选项中安装。
然后,在项目中添加对 System.Speech 的引用。
以下是一个简单的语音识别器类:
using System;
using System.Speech.Recognition;
using System.Speech.Synthesis;
public class SpeechToTextConverter
{
public event Action<string> TextReceived;
private SpeechRecognizer recognizer;
private SpeechSynthesizer synthesizer;
public SpeechToTextConverter()
{
recognizer = new SpeechRecognizer();
recognizer.SetInputToDefaultAudioDevice();
recognizer.RecognizeAsync(RecognizeMode.Synchronous);
recognizer.SpeechRecognized += Recognizer_SpeechRecognized;
synthesizer = new SpeechSynthesizer();
}
private void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string text = e.Result.Text;
TextReceived?.Invoke(text);
}
public void StartListening()
{
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
public void StopListening()
{
recognizer.UnloadAllGrammars();
recognizer.Dispose();
}
public void Speak(string text)
{
synthesizer.Speak(text);
}
}
SpeechToTextConverter converter = new SpeechToTextConverter();
// 启动监听
converter.StartListening();
// 当有文字时触发
converter.TextReceived += (text) =>
{
Console.WriteLine("You said: " + text);
};
// 合成语音
converter.Speak("Hello, how are you?");
// 停止监听
converter.StopListening();
【Paddle实战】基于PaddleSpeech搭建私人录音转文字服务 FunASR语音识别快速上手指南 FunASR Windows本地部署(语音转文字)