fbpx

Convert Text to Audio utilizing Azure and .NET 8

Convert Text to Audio utilizing Azure and .NET 8

Microsoft voices

In the ever-evolving panorama of expertise, accessibility stays a essential point of interest. As builders, it is our duty to make sure that our purposes are usable by people of all talents.

Text-to-audio conversion, powered by Azure Cognitive Services, presents a strong answer to this problem. In this text, we delve into the method of integrating text-to-audio performance into

.NET purposes, exploring its implementation, use circumstances, and the transformative affect it might have on accessibility.

Use Cases and Applications

  1. Narrating Content for Visually Impaired Users: By integrating text-to-audio performance, purposes can audibly convey web site content material, paperwork, or instructional supplies, thereby enhancing accessibility for visually impaired people.
  2. Interactive Voice Response Systems: Incorporating natural-sounding speech into IVR programs enriches consumer expertise, providing intuitive navigation via menus, prompts, and suggestions mechanisms.
  3. Instructions and Notifications: Text-to-audio conversion serves to alleviate cognitive pressure by audibly delivering directions, alerts, or notifications inside purposes, thereby decreasing reliance on visible interplay.
  4. Language Learning Applications: Text-to-audio capabilities facilitate pronunciation steering, textual content passage narration, and listening workout routines inside language studying purposes, fostering enhanced language acquisition.

Setting Up Azure Cognitive Services

The first step is to create an Azure Cognitive Services useful resource.

  1. Register for an Azure account if you have not already by signing up at no cost right here.
  2. Access the Azure portal and set up a brand new Speech Service useful resource.
    Speech Service
    After the useful resource is ready, make a remark of Key 1 and the Location/Region from the ‘Keys and Endpoint’ tab of the useful resource. These particulars are mandatory for linking your utility to the Text-to-Speech service.
    Keys and Endpoint

Integrating Azure Speech service with .NET

Create a .NET 8 Console App challenge in Visual Studio, after which set up Microsoft.CognitiveServices.Speech NuGet package deal.

Or

dotnet add package deal Microsoft.Azure.CognitiveServices.Speech

Sample Code Snippet of program.cs

utilizing Microsoft.CognitiveServices.Speech;
utilizing System.Media;

namespace TextToSpeech.Azure.NET8
{
    public class Program
    {
        public static async Task Main()
        {
            attempt
            {
                await SynthesizeAndPlayAudioAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }

        personal static async Task SynthesizeAndPlayAudioAsync()
        {
            // Load configuration from safe storage or app settings
            string key= "";
            string area = "";

            var speechConfig = SpeechConfig.FromSubscription(key, area);

            Console.WriteLine("Enter the textual content to synthesize:");
            string textual content = Console.ReadLine();

            Console.WriteLine("Choose a voice:");
            Console.WriteLine("1. en-US-GuyNeural");
            Console.WriteLine("2. en-US-JennyNeural");
            Console.WriteLine("3. en-US-AriaNeural");
            string voiceChoice = Console.ReadLine();

            string voiceName;
            swap (voiceChoice)
            {
                case "1":
                    voiceName = "en-US-GuyNeural";
                    break;
                case "2":
                    voiceName = "en-US-JennyNeural";
                    break;
                case "3":
                    voiceName = "en-US-AriaNeural";
                    break;
                default:
                    voiceName = "en-US-GuyNeural"; // Default to GuyNeural
                    break;
            }

            speechConfig.SetProperty(PropertyId.SpeechServiceConnection_SynthVoice, voiceName);

            utilizing var synthesizer = new SpeechSynthesizer(speechConfig);

            utilizing var memoryStream = new MemoryStream();
            synthesizer.SynthesisCompleted += (s, e) =>
            {
                if (e.Result.Reason == ResultReason.SynthesizingAudioAccomplished)
                {
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    utilizing var participant = new SoundPlayer(memoryStream);
                    participant.PlaySync();
                }
                else
                {
                    Console.WriteLine($"Speech synthesis failed: {e.Result.Reason}");
                }
            };

            await synthesizer.ConverseTextAsync(textual content);
        }
    }
}

Replace the key and area worth with your individual from ‘Keys and Endpoint’.

  1. Main Method: The entry level of this system the place asynchronous execution begins. It calls the SynthesizeAndPlayAudioAsync technique.
  2. SynthesizeAndPlayAudioAsync Method: This technique handles the speech synthesis and audio playback logic asynchronously.
    • It initializes the SpeechConfig object utilizing a subscription key and area.
    • It prompts the consumer to enter the textual content to synthesize and choose a voice from a predefined listing.
    • Based on the consumer’s voice selection, it units the suitable voice for synthesis.
    • It creates a SpeechSynthesizer object with the configured SpeechConfig.
    • It subscribes to the SynthesisCompleted occasion of the SpeechSynthesizer to deal with audio playback.
    • When synthesis completes, it performs the synthesized audio utilizing a SoundPlayer.
  3. Exception Handling: The program catches and shows any exceptions that happen throughout execution.

Testing the conversion

Once you’ve got constructed and launched the appliance, it can convert the supplied textual content enter into speech. It will play the synthesized speech again instantly or reserve it to an audio file for later use.

Build and Run the Application, as soon as the appliance is working, it can convert the enter textual content to speech and both play it again or reserve it to an audio file.

The following sentence is transformed from textual content to speech

Discover the enchanting fantastic thing about Tunisia! From pristine seashores to bustling souks and historic ruins, Tunisia provides an unforgettable expertise for each traveler. Explore the historic websites of Carthage, savor scrumptious Mediterranean delicacies, and immerse your self in Tunisian hospitality. Whether you are a historical past buff, a water sports activities fanatic, or just searching for leisure underneath the solar, Tunisia has one thing for everybody

 Tunisia offers

You can view the result on this video: https://vimeo.com/918035859

Conclusion

The integration of Azure Cognitive Services with .NET presents a strong answer for changing textual content to audio seamlessly. By leveraging Azure’s strong infrastructure and. NET’s flexibility, builders can improve accessibility and enrich consumer expertise throughout a variety of purposes.

This integration not solely facilitates accessibility for customers with visible impairments but in addition opens up new avenues for delivering content material in natural-sounding audio codecs, in the end broadening the attain and affect of digital purposes.

References: https://study.microsoft.com/en-us/azure/ai-services/speech-service/text-to-speech

Thank you for studying, please let me know your questions, ideas, or suggestions within the feedback part. I recognize your suggestions and encouragement.

Happy Documenting!

HI-FI News

by way of C-Sharpcorner Latest Articles https://ift.tt/nSg0sNc

March 1, 2024 at 01:04PM

Select your currency