Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#

How To Create CLI To Announce Logged On User With Speech API (tts)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Apr 2016CPOL2 min read 8.3K   2  
A simple command line based app to announce the currently logged on user using the Speech API

Introduction

This code is a simple command line program to announce the currently logged in user Display Name. The concept is fairly simple, but could easily be extended to announce dynamically passed in data and more complex logic. Many examples show using TTS in a Windowed app, but few showing CLI pattern.

Using the Code

I created this as follows using Visual Studio 2013 Community Edition on WIndows 10.

In Visual Studio:

  • Create a New Project called SayIt
  • In Solution Explorer, Add References for:
    • System.Speech
    • System.DirectoryServices.AccountManagement
  • Add the contents below to SayIt.cs file.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.DirectoryServices.AccountManagement;

namespace SayIt
{
    class SayIt
    {
       static void Main(string[] args)
       {
           const string voiceArg = "v:";
           const string loudArg = "l:";
           const string rateArg = "r:";

           IEnumerable<string> parsedArgsVoice = from str in args
                                                 where str.IndexOf(voiceArg) >= 0
                                                 select str.Substring(2);

           IEnumerable<int> parsedArgsLoud = from str in args
                                             where str.IndexOf(loudArg) >= 0
                                             select Int32.Parse(str.Substring(2));

           IEnumerable<int> parsedArgsRate = from str in args
                                             where str.IndexOf(rateArg) >= 0
                                             select Int32.Parse(str.Substring(2));

           SpeechSynthesizer synth = new SpeechSynthesizer();

           List<string> voiceList = new List<string>();
           foreach (var voice in synth.GetInstalledVoices())
           {
               voiceList.Add(voice.VoiceInfo.Name);
           }            

           synth.SelectVoice(parsedArgsVoice.Count() > 0
                             ? parsedArgsVoice.ElementAt(0)
                             : voiceList.ElementAt(0));

           synth.Volume = parsedArgsLoud.Count() > 0
                          ? parsedArgsLoud.ElementAt(0)
                          : 100;

           synth.Rate = parsedArgsRate.Count() > 0
                        ? parsedArgsRate.ElementAt(0)
                        : 1;

           synth.Speak("Hello " + UserPrincipal.Current.DisplayName);
       }
   }
}

The code above is fairly straight forward and broken down in a few basic steps:

  1. Set up known CLI argument prefixes as const strings
  2. Use Linq to parse known command line arguments in a manner that allows the ordering of them to be specific. There is a lot of type validation code left out here to focus on the main logic!
  3. Create a new SpeechSynthesizer instance
  4. Use the SpeechSynthesizer instance to obtain installed voices and store them in a list
  5. Select a Voice, set the volume, and set the rate on the SpeechSynthesizer instance.
    • Use the conditional ternary operator to set the voice, rate, and loudness to either a passed in CLI argument or a default if not passed as an argument.
    • Passed in Voice must match an installed voice exactly - TTS settings in Control Panel will list installed voices
    • Volume/Loudness valid range is 0 to 100
    • Rate valid range is 0 to 10
  6. Call the Speak method on the SpeechSynthesizer instance passing the string Hello with the UserPrincipal.Current.DisplayName (currently logged in user's DisplayName) appended.
    • If wishing to use just the username, then append Environment.UserName instead of UserPrincipal.Current.DisplayName.
      • Also the System.DirectoryServices.AccountManagement reference and using statement can be removed.
      • Using Environment.UserName also seems to be much faster.

After building the solution, open a command prompt and change directory to where the SayIt.exe is output and type some of the following commands:

  • SayIt - Announces user name in default voice, rate, and loudness
  • SayIt l:60 - Announces user name in default voice, rate, and loudness of 60
  • SayIt r:5 l:70 - Announces user name in default voice, rate of 5, and loudness of 70
  • SayIt l:90 r:2 "v:Microsoft Zira Desktop" - Announces user name in using Microsoft Zira Desktop voice, rate of 2, and loudness of 90

Points of Interest

I am using this with a Kinect V2 on Windows setup with the Windows Hello face recognition for login. I setup a Task that executes this program upon login. So my kids sit down in front of the computer, Windows Hello recognizes them and automatically logs them in, and when logged in, the computer TTS voice announces Hello Robert. They get a kick out of it. When using the Environment.UserName, which is faster, it usually announces while the login screen is still in progress, which is cool too.

History

  • 2016-04-09 - Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Cerner Corp.
United States United States
I have been developing software for 30+ years as a hobbyist and 20+ as an employed professional. I bought my first computer, a Tandy CocCo3, back 1987 to teach myself computer game and music programming. I have since been employed in startups, the D.O.D., and several large Fortune 500s across the telecomm, financial, and health services sectors. At this point I have forgotten as much, or more, as I've learned, but I still love to learn.

Comments and Discussions

 
-- There are no messages in this forum --