How to Convert Text to Speech in Asp.Net | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

How to Convert Text to Speech in Asp.Net

Date- Nov 06,2023

3666

Free Download Pay & Download
SpeechSynthesis AspNet

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

  1. Create or open your ASP.NET MVC project.
  2. In your controller, create an action to handle text-to-speech conversion.
  3. Adjust the speech rate using the `Rate` property of the `SpeechSynthesizer` object.
  4. Configure the voice using the `SelectVoiceByHints` method.
  5. Create a view to accept user input and trigger text-to-speech conversion.
  6. Make an HTTP request from your view to the controller action to convert the text to speech.
  7. 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.

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1210
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts