How to Convert Text to Speech in Asp.Net
How to Convert Text to Speech in ASP.NET using System.Speech.Synthesis
In this article, we will explore how to implement text-to-speech functionality in an ASP.NET MVC application using the built-in System.Speech.Synthesis namespace in .NET. This method allows you to convert text to speech without relying on external services.
Implementation Steps
- Create or open your ASP.NET MVC project.
- In your controller, create an action to handle text-to-speech conversion.
- Adjust the speech rate using the `Rate` property of the `SpeechSynthesizer` object.
- Configure the voice using the `SelectVoiceByHints` method.
- Create a view to accept user input and trigger text-to-speech conversion.
- Make an HTTP request from your view to the controller action to convert the text to speech.
- Play the generated audio or handle it as needed in your application.
Code Example
We will add a controller and we will add one action on that which will take text as parameter which it will convert to audio file. Below is a sample code example for the controller action that converts text to speech, allowing users to adjust the speech rate and voice type:
using System; using System.Collections.Generic; using System.Linq; using System.Speech.Synthesis; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; using static System.Net.Mime.MediaTypeNames; namespace TextToSpeech.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public async Task<ActionResult> ConvertToSpeech(string textSpeech, int rate = 0, string voiceGender = "Female") { using (var synth = new SpeechSynthesizer()) { // Configure the voice and output format synth.SelectVoiceByHints(ParseVoiceGender(voiceGender), VoiceAge.Adult); synth.SetOutputToWaveFile(Server.MapPath("~/Content/speech.wav")); // Save the audio to a file // Adjust the speech rate synth.Rate = rate; // You can set the rate to a value like -10 (slower) or 10 (faster) // Convert the text to speech synth.Speak(textSpeech); } // You can then play the generated audio or handle it as needed return File("~/Content/speech.wav", "audio/wav"); } private VoiceGender ParseVoiceGender(string gender) { if (gender.Equals("Male", StringComparison.OrdinalIgnoreCase)) return VoiceGender.Male; else return VoiceGender.Female; } } }
On the view side we will add one input box and form to submit the text that we want to convert to speech
@{ ViewBag.Title = "Home Page"; } <main> <section class="row" aria-labelledby="aspnetTitle"> <h1 id="title">ASP.NET</h1> @using (Html.BeginForm("ConvertToSpeech", "Home", FormMethod.Post)) { <input type="text" name="textSpeech"/> <input type="submit" id="ConvertTextToSpeech" value="Convert To Speech" /> } </section> </main>
Explanation
In this code example, users can control the speech rate and voice type:
- Adjusting Speech Rate: By passing the `rate` parameter to the action, users can set the speech rate. A value of 0 represents the default rate, negative values make it slower, and positive values make it faster.
- Changing Voice Type: The `voiceGender` and `voiceAge` parameters allow users to select the gender and age of the voice. In this example, we use the `SelectVoiceByHints` method to configure the voice based on the provided gender and age parameters.
- You can check the output file in the Content folder as that is mentioned as the path
Conclusion
By following these steps and using the provided code, users can interact with your ASP.NET MVC application to convert text to speech with control over speech rate and voice type, providing a customizable and accessible user experience. So this is how we can convert text to speech in asp.net mvc.