install-package Microsoft.CognitiveServices.Speech
install-package NAudio
install-package System.Speech
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using NAudio.CoreAudioApi;
using System;
using System.Speech.Recognition;
namespace ConsoleApp1
{
class Program
{
static SpeechConfig speechConfig = SpeechConfig.FromSubscription("<paste-your-subscription-key>", "<paste-your-region>");
static string id = "";
static AudioConfig audioConfig = AudioConfig.FromMicrophoneInput(id);
static SpeechSynthesizer sy = new SpeechSynthesizer(speechConfig, audioConfig);
static void Main(string[] args)
{
var enumerator = new MMDeviceEnumerator();
var id = "";
foreach (var endpoint in
enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active))
{
Console.WriteLine("{0} ({1})", endpoint.FriendlyName, endpoint.ID);
id = endpoint.ID;
}
//创建中午识别器
using (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);
//实例化命令词管理
System.Speech.Recognition.Grammar grammar = new System.Speech.Recognition.Grammar(gBuilder);
//-----------------
//创建并加载听写语法(添加命令词汇识别的比较精准)
recognizer.LoadGrammar(grammar);
//为语音识别事件添加处理程序。
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRRecongized);
//将输入配置到语音识别器。
recognizer.SetInputToDefaultAudioDevice();
//启动异步,连续语音识别。
recognizer.RecognizeAsync(RecognizeMode.Multiple);
//保持控制台窗口打开。
while (true)
{
Console.ReadLine();
}
}
}
//speechrecognized事件处理
static void recognizer_SpeechRRecongized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("识别结果:" + e.Result.Text);
}
}
}
[HttpPost]
public object Send(IFormFile file)
{
var stream = file.OpenReadStream();
var sr = new StreamReader($"{Environment.CurrentDirectory}\\words.txt");
var txt = sr.ReadToEnd();
var Words = txt.Split(Environment.NewLine);
sr.Dispose();
using (var recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-CN")))
{
//----------------
//初始化命令词
var conmmonds = new Choices();
//添加命令词
conmmonds.Add(Words);
//初始化命令词管理
var gBuilder = new GrammarBuilder();
//将命令词添加到管理中
gBuilder.Append(conmmonds);
//实例化命令词管理
var grammar = new System.Speech.Recognition.Grammar(gBuilder);
//-----------------
//创建并加载听写语法(添加命令词汇识别的比较精准)
recognizer.LoadGrammar(grammar);
recognizer.SetInputToWaveStream(stream);
//为语音识别事件添加处理程序。
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);
//将输入配置到语音识别器。
//recognizer.SetInputToDefaultAudioDevice();
//启动异步,连续语音识别。
//recognizer.RecognizeAsync(RecognizeMode.Single);
recognizer.InitialSilenceTimeout = TimeSpan.FromSeconds(10);
completed = false;
var result = recognizer.Recognize(recognizer.InitialSilenceTimeout);
//while (!completed)
//{
// Thread.Sleep(300);
//}
return new { Success = true, Data = result?.Text };
}
}