using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using SpeechLib;//NET2.0 引用 Speech sdk 5.1 在COM选项卡里面的Microsoft Speech object library引用 已经有11.0版本
using System.Speech;
using System.Speech.Recognition;
using System.Speech.Synthesis; namespace Speech
{
/// <summary>
/// 20140427
/// 涂聚文
///
/// </summary>
public partial class Form1 : Form
{
private enum State
{
Idle = 0,
Accepting = 1,
Off = 2,
} private State RecogState = State.Off;
private SpeechRecognitionEngine recognizer;
private SpeechSynthesizer synthesizer = null;
private int Hypothesized = 0;
private int Recognized = 0;
/// <summary>
///
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
// SpVoice voice = new SpVoice();//SAPI 5.4
//SpeechLib.ISpeechObjectTokens obj = voice.GetVoices(string.Empty, string.Empty);
//int count = obj.Count;//获取语音库总数
//bool result = false;
//for (int i = 0; i < count; i++)
//{
// string desc = obj.Item(i).GetDescription(i);//.GetDescription(); //遍历语音库 // comboBox1.Items.Add(desc);
//} //SpVoiceClass voice = new SpVoiceClass();//SAPI 5.1
////voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);
////voice.Speak("你要说的话",SpeechVoiceSpeakFlags.SVSFlagsAsync); //SpVoice voice1 = new SpVoice();//SAPI 5.4
//voice1.Volume = 100;//音量
//voice1.Voice = voice1.GetVoices(string.Empty, string.Empty).Item(0); //voice1.Rate = 2;//速度语音朗读速度
// voice1.Speak("你要说的话", SpeechVoiceSpeakFlags.SVSFlagsAsync);
//voice1.Speak("speech sdk 5.1", SpeechVoiceSpeakFlags.SVSFlagsAsync);
//SpeechSynthesizer syn = new SpeechSynthesizer();
//syn.SelectVoice("Microsoft Lili"); //initialize recognizer and synthesizer
InitializeRecognizerSynthesizer(); //if input device found then proceed
if (SelectInputDevice())
{
LoadDictationGrammar(); ReadAloud("中华人民共和国"); //中文方式Speech Engine Ready for Input
} }
/// <summary>
/// 中文
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
SpVoiceClass voice = new SpVoiceClass();
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0); //0,为系统默认,中文 voice.Speak(this.textBox1.Text.Trim(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
}
/// <summary>
/// 英文
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
SpVoiceClass voice = new SpVoiceClass();
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(1);//
voice.Speak(this.textBox2.Text.Trim(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
}
/// <summary>
/// 输入中文语音输出中文文字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
switch (RecogState)
{
case State.Off:
RecogState = State.Accepting;
button3.Text = "Stop";
recognizer.RecognizeAsync(RecognizeMode.Multiple);
break;
case State.Accepting:
RecogState = State.Off;
button3.Text = "Start";
recognizer.RecognizeAsyncStop();
break;
}
}
/// <summary>
/// pause recognition and speak the text sent
/// </summary>
/// <param name="speakText"></param>
public void ReadAloud(string speakText)
{
try
{
recognizer.RecognizeAsyncCancel();
synthesizer.SpeakAsync(speakText);
}
catch { }
} /// <summary>
/// initialize recognizer and synthesizer along with their events
/// /// </summary>
private void InitializeRecognizerSynthesizer()
{
var selectedRecognizer = (from e in SpeechRecognitionEngine.InstalledRecognizers()
where e.Culture.Equals(Thread.CurrentThread.CurrentCulture)
select e).FirstOrDefault();
recognizer = new SpeechRecognitionEngine(selectedRecognizer);
recognizer.AudioStateChanged += new EventHandler<AudioStateChangedEventArgs>(recognizer_AudioStateChanged);
recognizer.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); synthesizer = new SpeechSynthesizer();
}
#region Recognizer events
private void recognizer_AudioStateChanged(object sender, AudioStateChangedEventArgs e)
{
switch (e.AudioState)
{
case AudioState.Speech:
LabelStatus.Text = "Listening";
break;
case AudioState.Silence:
LabelStatus.Text = "Idle";
break;
case AudioState.Stopped:
LabelStatus.Text = "Stopped";
break;
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
Hypothesized++;
LabelHypothesized.Text = "Hypothesized: " + Hypothesized.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Recognized++;
string s = "Recognized: " + Recognized.ToString(); if (RecogState == State.Off)
return;
float accuracy = (float)e.Result.Confidence;
string phrase = e.Result.Text;
{
if (phrase == "End Dictate")
{
RecogState = State.Off;
recognizer.RecognizeAsyncStop();
ReadAloud("Dictation Ended");
return;
}
textBox1.AppendText(" " + e.Result.Text);
}
}
#endregion
/// <summary>
/// select input device if available
/// </summary>
/// <returns></returns>
private bool SelectInputDevice()
{
bool proceedLoading = true;
//if OS is above XP
if (IsOscompatible())
{
try
{
recognizer.SetInputToDefaultAudioDevice();
}
catch
{
proceedLoading = false; //no audio input device
}
}
//if OS is XP or below
else
ThreadPool.QueueUserWorkItem(InitSpeechRecogniser);
return proceedLoading;
} /// <summary>
/// Findout if OS is compatible.
/// </summary>
/// <returns>true if greater than XP otherwise false</returns>
private bool IsOscompatible()
{
OperatingSystem osInfo = Environment.OSVersion;
if (osInfo.Version > new Version("6.0"))
return true;
else
return false;
}
/// <summary>
///
/// </summary>
/// <param name="o"></param>
private void InitSpeechRecogniser(object o)
{
recognizer.SetInputToDefaultAudioDevice();
} /// <summary>
/// Load grammars, one for command and other for dictation
/// </summary>
private void LoadDictationGrammar()
{
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(new Choices("End Dictate"));
Grammar commandGrammar = new Grammar(grammarBuilder);
commandGrammar.Name = "main command grammar";
recognizer.LoadGrammar(commandGrammar); DictationGrammar dictationGrammar = new DictationGrammar();
dictationGrammar.Name = "dictation";
recognizer.LoadGrammar(dictationGrammar);
}
}
}

Csharp: speech to text, text to speech in win的更多相关文章

  1. csharp:Google TTS API text to speech

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. Text Converted into Speech in Pi

    Step 1: Convert any text into uint8 type in matlab : Step 2: Add models in matlab : copy the uint8 n ...

  3. 利用Google Speech API实现Speech To Text

    很久很久以前, 网上流传着一个免费的,识别率暴高的,稳定的 Speech To Text API, 那就是Google Speech API. 但是最近再使用的时候,总是返回500 Error. 后来 ...

  4. xe5 android tts(Text To Speech)

    xe5 android  tts(Text To Speech) TTS是Text To Speech的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话. 以下代码实现xe5 开发的文本转 ...

  5. 各大厂的语音识别Speech To Text API使用体验

    最近发现有声读物能极大促进我的睡眠,但每个前面都有一段开场语,想把它剪掉,但是有多个开场语,所以就要用到语音识别判断一下再剪. 前两年在本地搭建过识别的环境,奈何识别准确率不行,只能找找API了,后面 ...

  6. C#中的System.Speech命名空间初探

    本程序是口算两位数乘法,随机生成两个两位数,用语音读出来.然后开启语音识别,接受用户输入,知道答案正确关闭语音识别.用户说答案时,可以说“再说一遍”重复题目. 关键是GrammarBuilder和Ch ...

  7. Speech两种使用方法

    COM组件使用speech: public class Speach { private static Speach _Instance = null ; private SpeechLib.SpVo ...

  8. 重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML

    [源码下载] 重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML 作者:webabcd 介绍重新想象 Windows 8.1 ...

  9. System.Speech.Synthesis 添加暂停、继续功能

    为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...

随机推荐

  1. Ionic2使用百度地图API(JS)出现白屏解决方案

    最近自学ionic2,写了一个内嵌百度地图JS的demo,实际跑起来之后出现了大家常见的白屏问题.. 最初的实现是这样的: 首先主页内嵌了一个百度地图插件 <div id="Bmap& ...

  2. 经典排序的python实现

    具体原理我这里就不解释了,可以查看数据结构课本或者百度百科 这里只给出相应的代码(很简洁) 1 __author__ = "WSX" class sort: def __init_ ...

  3. Kettle 值映射

    在费用转换里面做了两个值映射.一个是编码.一个是名称.其中两个值映射设置不一样效果不一样. 第一个编码映射 目标字段名不为空,则表示会新增字段.其中复核源值条件的都会转换为目标值,不符合条件的会用[不 ...

  4. system命令

    服务查看 查看所有服务运行状态: service --status-all chkconfig --list 查看单个服务的运行状态 service sshd status 查看启动状态,是否开机自动 ...

  5. AXI协议(一)

    最近弄Zynq,不懂AXI协议Zynq很难玩儿的转.这些笔记主要攻克AXI中的一些难题. 所有的AXI4包含了5个不同的通道:     (1)读/写地址通道(Read/Write address ch ...

  6. CAS总结

    n++的问题不能保证原子操作. 因为被编译后拆分成了3个指令,先获取值,然后加一,然后写回内存.把变量声明为volatile,volatile只能保证内存可见性,但是不能保证原子性,在多线程并发下,无 ...

  7. appium解决无法通过name属性识别元素org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session

    执行代码.: public AndroidDriver<AndroidElement> appiumDriver; appiumDriver.findElement(By.name(&qu ...

  8. (转)Linux服务器SNMP常用OID

    原文:https://www.haiyun.me/archives/linux-snmp-oid.html 收集整理一些Linux下snmp常用的OID,用做服务器监控很不错.服务器负载: 1 2 3 ...

  9. Java学习之路(一):日常第一课,认识JAVA

    Java的介绍 语言的起源 Java是SUN(Stanford University Network 斯坦福大学网络公司) 1995年推出的一门高级编程语言. Java名称的来源: Java最初是被命 ...

  10. 案例20-页面使用redis缓存显示类别菜单

    1 准备工作 1  需要导入所需要的jar包. 2 启动windows版本的redis服务端 3 准备JedisUtils工具类的配置文件redis.properties redis.maxIdle= ...