Dela via


Testing System.Speech.Synthesis

Before testing SWI-Speech, I thought I pay the System.Speech (SAPI .NET managed code) a little visit. What I wanted to achieve is really simple. Use System.Speech to say “Hello World” or any other user-defined string.

In order to achieve this highly ambitious goal, one needs to …

  1. Start the Speech Synthesis Engine
  2. Configure the Speech Synthesis Engine according to one’s needs
  3. Say what one wants to say

With this in mind, I wrote a little C# command line app, which may not be the “nicest”, however, does the job. Please note that the actual System.Speech code is quite little and the majority of code is boilerplate.

Before coding starts, add the “System.Speech” assembly to the referenced assemblies. The following C# code with inline comments will achieve what mentioned above.

 

    1:  using System;
    2:  using System.Collections.Generic;
    3:  using System.Collections.ObjectModel;
    4:  using System.Speech.Synthesis;
    5:   
    6:  namespace Speech1
    7:  {
    8:      class Program
    9:      {
   10:          static void Main(string[] args)
   11:          {
   12:              Console.WriteLine("Speech Synthesis 1");
   13:              /*
   14:               * create the speech synthesizer, which will later say something 
   15:               * to the interested audience
   16:              */
   17:              using (SpeechSynthesizer sSynth = new SpeechSynthesizer())
   18:              {
   19:                  /*
   20:                   * which are installed voices (male, female) and what are their 
   21:                   * characteristics
   22:                  */
   23:                  ReadOnlyCollection<InstalledVoice> installedVoices = sSynth.GetInstalledVoices();
   24:   
   25:                  if (installedVoices == null ||
   26:                      installedVoices.Count < 1)
   27:                  {
   28:                      Console.WriteLine("no installed voices ... ");
   29:                      return;
   30:                  }
   31:   
   32:                  List<string> vVoiceNames = new List<string>(installedVoices.Count);
   33:                  
   34:                  /*
   35:                   * allow the user to choose an arbitrary voice, 
   36:                   * which has to be in the list of installed voices
   37:                  */
   38:                  Console.WriteLine("installed voices:");
   39:                  foreach (InstalledVoice installedVoice in installedVoices)
   40:                  {
   41:                      Console.WriteLine("==> " + installedVoice.VoiceInfo.Name);
   42:   
   43:                      vVoiceNames.Add(installedVoice.VoiceInfo.Name);
   44:                  }
   45:   
   46:                  Console.WriteLine("which voice do you want to use: ");
   47:                  string useVoice = Console.ReadLine();
   48:   
   49:                  /*
   50:                   * if user did not choose a voice or the chosen one is some bogus, 
   51:                   * then choose the first voice 
   52:                   * from the list of installed vocies
   53:                   */
   54:                  if (string.IsNullOrEmpty(useVoice) ||
   55:                      vVoiceNames.Contains(useVoice) == false)
   56:                  {
   57:                      Console.WriteLine("voice must not be null, empty and have a valid name");
   58:   
   59:                      useVoice = installedVoices[0].VoiceInfo.Name;
   60:                  }
   61:   
   62:                  Console.WriteLine("using ... " + useVoice);
   63:   
   64:                  /*
   65:                   * allow user to choose what to say
   66:                   */
   67:                  Console.WriteLine("Enter your text: ");
   68:                  string textToSpeak = Console.ReadLine();
   69:   
   70:                  if (string.IsNullOrEmpty(textToSpeak))
   71:                      textToSpeak = "Hello World";
   72:   
   73:                  /*
   74:                   * prepare saying something
   75:                   * 
   76:                   * select the chosen voice
   77:                   * set the output device to the default one
   78:                   * set volume up
   79:                   */
   80:                  sSynth.SelectVoice(useVoice);
   81:                  sSynth.SetOutputToDefaultAudioDevice();
   82:                  sSynth.Volume = 100;
   83:                  
   84:                  Console.WriteLine("saying ... " + textToSpeak);
   85:   
   86:                  /*
   87:                   * say what we want to say
   88:                   */
   89:                  sSynth.Speak(textToSpeak);
   90:              }
   91:   
   92:              Console.WriteLine("Press <key> to exit");
   93:              Console.ReadLine();
   94:          }
   95:      }
   96:  }

Happy talking gibberish :).