how to use speech in asp.net core



Question: How do you use Speech to Text in Asp.Net Core 3.1?

Answer: As far as I know the library isn't available in Asp.Net Core 3.1 Web API, however, you can accomplish the same by using Powershell code and then execute the Powershell process in your Asp.Net Core 3.1 C# Code.

Step 1. Create a Static class that will hold the C# Code for speech to Text

Step 2. Write your Powershell Script and test it using "Windows Powershell ISE" Powershell IDE.

Step 3. Call your Static class from any class in your Application. See the Code Below:

Asp.Net Core 3.1 Static Class C-Sharp Code:

 public static class SpeakingModuleRepository
    {
        public static void TextToSpeak(string myTextToSpeek, bool wait = false)
        {
            void Execute(string command)
            {
                var myPowerShellScriptFile = "C:\\Users\\ThePCUser\\Documents\\PCUser\\MyCustomPowershellScript.ps1";
                // Setup the PS  
                var start = new System.Diagnostics.ProcessStartInfo()
                    {
                        FileName = "C:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe",                    
                        LoadUserProfile = false,
                        UseShellExecute = false,
                        CreateNoWindow = true,
                        Arguments = $"-executionpolicy bypass -File {myPowerShellScriptFile}  -argument '{myTextToSpeek}'",
                        WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
                    };

                //Init the Process  
                var p = System.Diagnostics.Process.Start(start);
                if (wait) p.WaitForExit();
            }
        }

    }

Create a Powershell Code, and test it:

param($argument) ##This will allow this code to expect parameters
Add-Type -AssemblyName System.speech
$MyVariable = New-Object System.Speech.Synthesis.SpeechSynthesizer
$TextToSpeak = '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US">
    <voice name="en-US-AriaRUS" gender="female">
      "'+ $argument +'"
    </voice>
</speak>'
$MyVariable.SpeakSsml($TextToSpeak)