C# Speech to Text

http://www.codeproject.com/Articles/380027/Csharp-Speech-to-Text


 

speech to text application

Introduction

The purpose of this article is it to give you a small insight of the capabilities of the System.Speech assembly. In detail, the usage of the SpeechRecognitionEngine class. The MSDN documentation of the class can be found here.

Background

I read several articles about how to use Text to Speech, but as I wanted to find out how to do it the opposite way, I realized that there is a lack of easily understandable articles covering this theme, so I decided to write a very basic one on my own and share my experiences with you.

The Solution

So now let's start. First of all you need to reference the System.Speech assembly in your application located in the GAC.

gac

This is the only reference needed containing the following namespaces and its classes. The System.Speech.Recognition namespace contains the Windows Desktop Speech technology types for implementing speech recognition.

  • System.Speech.AudioFormat
  • System.Speech.Recognition
  • System.Speech.Recognition.SrgsGrammar
  • System.Speech.Synthesis
  • System.Speech.Synthesis.TtsEngine

Before you can use SpeechRecognitionEngine, you have to set up several properties and invoke some methods: in this case I guess, code sometimes says more than words ...

// the recognition engine
SpeechRecognitionEngine speechRecognitionEngine = null;

// create the engine with a custom method (i will describe that later)
speechRecognitionEngine = createSpeechEngine("de-DE");

// hook to the needed events
speechRecognitionEngine.AudioLevelUpdated += 
  new EventHandler<AudioLevelUpdatedEventArgs>(engine_AudioLevelUpdated);
speechRecognitionEngine.SpeechRecognized += 
  new EventHandler<SpeechRecognizedEventArgs>(engine_SpeechRecognized);

// load a custom grammar, also described later
loadGrammarAndCommands();

// use the system's default microphone, you can also dynamically
// select audio input from devices, files, or streams.
speechRecognitionEngine.SetInputToDefaultAudioDevice();

// start listening in RecognizeMode.Multiple, that specifies
// that recognition does not terminate after completion.
speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

In detail now, the function createSpeechEngine(string preferredCulture). The standard constructor and its overloads are the following:

  • SpeechRecognitionEngine(): Initializes a new instance using the default speech recognizer for the system.
  • SpeechRecognitionEngine(CultureInfo): Initializes a new instance using the default speech recognizer for a specified locale.
  • SpeechRecognitionEngine(RecognizerInfo): Initializes a new instance using the information in a RecognizerInfo object to specify the recognizer to use.
  • SpeechRecognitionEngine(String): Initializes a new instance of the class with a string parameter that specifies the name of the recognizer to use.

The reason why I was creating a custom function for instantiating the class is that I wanted to add the possibility to choose the language that the engine is using. If the desired language is not installed, then the default language (Windows Desktop Language) is used. Preventing an exception while choosing a not installed package. Hint: You can install further language packs to choose a different CultureInfo that is used by the SpeechRecognitionEnginge but as far as I know, it is only supported on Win7 Ultimate/Enterprise.

private SpeechRecognitionEngine createSpeechEngine(string preferredCulture)
{
    foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers())
    {
        if (config.Culture.ToString() == preferredCulture)
        {
            speechRecognitionEngine = new SpeechRecognitionEngine(config);
            break;
        }
    }

    // if the desired culture is not installed, then load default
    if (speechRecognitionEngine == null)
    {
        MessageBox.Show("The desired culture is not installed " + 
            "on this machine, the speech-engine will continue using "
            + SpeechRecognitionEngine.InstalledRecognizers()[0].Culture.ToString() + 
            " as the default culture.", "Culture " + preferredCulture + " not found!");
        speechRecognitionEngine = new SpeechRecognitionEngine();
    }

    return speechRecognitionEngine;
}

The next step is it to set up the used Grammar that is loaded by the SpeechRecognitionEngine. In our case, we create a custom text file that contains key-value pairs of texts wrapped in the custom class SpeechToText.Word because I wanted to extend the usability of the program and give you a little showcase on what is possible with SAPI. That is interesting because in doing so, we are able to associate texts or even commands to a recognized word. Here is the wrapper class SpeechToText.Word.

namespace SpeechToText
{
   public class Word
   {           
       public Word() { }
       public string Text { get; set; }          // the word to be recognized by the engine
       public string AttachedText { get; set; }  // the text associated with the recognized word
       public bool IsShellCommand { get; set; }  // flag determining whether this word is an command or not
   }
}

Here is the method to set up the Choices used by the Grammar. In the foreach loop, we create and insert the Word classes and store them for later usage in a lookup List<Word>. Afterwards we insert the parsed words into the Choices class and finally build the Grammar by using a GrammarBuilder and load it synchronously with the SpeechRecognitionEngine. You could also simply add strings to the choices class by hand or load a predefined XML-file. Now our engine is ready to recognize the predefined words.

private void loadGrammarAndCommands()
{
    try
    {
        Choices texts = new Choices();
        string[] lines = File.ReadAllLines(Environment.CurrentDirectory + "\\example.txt");
        foreach (string line in lines)
        {
            // skip commentblocks and empty lines..
            if (line.StartsWith("--") || line == String.Empty) continue;

            // split the line
            var parts = line.Split(new char[] { '|' });

            // add word to the list for later lookup or execution
            words.Add(new Word() { Text = parts[0], AttachedText = parts[1], 
                      IsShellCommand = (parts[2] == "true") });

            // add the text to the known choices of the speech-engine
            texts.Add(parts[0]);
        }
        Grammar wordsList = new Grammar(new GrammarBuilder(texts));
        speechRecognitionEngine.LoadGrammar(wordsList);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

To start the SpeechRecognitionEngine, we call SpeechRecognitionEngine.StartRecognizeAsync(RecognizeMode.Multiple). This means that the recognizer continues performing asynchronous recognition operations until the RecognizeAsyncCancel() or RecognizeAsyncStop() method is called. To retrieve the result of an asynchronous recognition operation, attach an event handler to the recognizer's SpeechRecognized event. The recognizer raises this event whenever it successfully completes a synchronous or asynchronous recognition operation.

// attach eventhandler
speechRecognitionEngine.SpeechRecognized += 
  new EventHandler<SpeechRecognizedEventArgs>(engine_SpeechRecognized);

// start recognition
speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

// Recognized-event 
void engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    txtSpoken.Text += "\r" + getKnownTextOrExecute(e.Result.Text);
    scvText.ScrollToEnd();
}

And here comes the gimmick of this application, when the engine recognizes one of our predefined words, we decide whether to return the associated text, or to execute a shell command. This is done in the following function:

private string getKnownTextOrExecute(string command)
{
    try
    {   // use a little bit linq for our lookup list ...
        var cmd = words.Where(c => c.Text == command).First();

        if (cmd.IsShellCommand)
        {
            Process proc = new Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = cmd.AttachedText;
            proc.Start();
            return "you just started : " + cmd.AttachedText;
        }
        else
        {
            return cmd.AttachedText;
        }
    }
    catch (Exception)
    {
        return command;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 对于"C"这个字母,在日常生活中可能有很多不同的涵义和用途。 首先,C可以代表大写字母,它位于拉丁字母表的第三位。在英语中,C代表“See”(看到)、“Cold”(冷的)、“Cute”(可爱的)等词语的首字母。此外,在一些科学、医学和工程等领域,C也代表一些专业术语,比如“Carbon”(碳)、“Celsius”(摄氏度)等。 其次,C也是一个音符,在音乐中有重要的作用。在标准的西方音乐音阶中,C代表着音阶的第一音符,也是一个非常常见的音符。它在各种音乐作品中都能被听到,并经常被用来调整乐器和唱歌。此外,在音乐学中,C也是一个很重要的基准音,可以帮助其他音符和音阶进行相对比较。 此外,C在计算机编程中也有特殊的含义。C是一种程序设计语言,被广泛使用于软件开发和系统编程。这种语言简洁而强大,可以用来编写各种类型的软件。C语言不仅在计算机科学领域有很大影响力,而且在电子学、通信和嵌入式系统等领域也被广泛应用。 总的来说,字母"C"具有丰富的含义和用途。它不仅代表着一些词语的首字母,还在音乐和计算机编程中扮演了重要的角色。无论是文化、科学还是技术领域,C都是一个富有意义的字母。 ### 回答2: “C”是英语字母表中的第三个字母,也是拉丁字母表中的一部分。在英语中,它的读音是/siː/。它有多种用法和含义。 首先,在英语中, "C"是一个常用的字母,用于构成许多常见的单词。例如, "cat"(猫)和 "car"(车)中都包含了这个字母。它还可以作为单词的缩写,例如 "CEO"(首席执行官)和 "CFO"(首席财务官)。 其次,在数学中, "C"代表了一些重要的概念和符号。例如,"C"可以表示圆的周长或直径,也可以表示复数中的实部。在统计学中, "C"常常用来表示组合的数目。 此外, "C"还代表一些国家的代号。例如, "C"是中国的国家代码。在汽车的车牌号上, "C"常常代表“加利福尼亚州”(California)。 最后, "C"还是一种编程语言。C语言是一种通用的、高级的程序设计语言,广泛应用于软件开发和系统编程。 总的来说, "C"是一个多用途的字母,有着多种不同的用途和含义。无论是在日常生活中还是在学术领域,都可以看到它的存在。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值