Exercise - Create a text to speech application using a text file

Completed

In this exercise, you'll create a text file that you'll use to create an audio file by using the Azure AI speech synthesizer.

Create your text to speech application

  1. In the Cloud Shell on the right, create a directory for your application, then switch folders to your new folder:

    mkdir text-to-speech
    cd text-to-speech
    
  2. Create a new .NET Core application:

    dotnet new console
    

    This command should take a few seconds to complete.

  3. When your .NET Core application has been created, add the Speech SDK package to your application:

    dotnet add package Microsoft.CognitiveServices.Speech
    

    This command should take a few seconds to complete.

Add the code for your text to speech application

  1. In the Cloud Shell on the right, open the Program.cs file:

    code Program.cs
    
  2. Replace the existing code with the following using statements, which enable the Azure AI Speech APIs for your application:

    using System.Text;
    using Microsoft.CognitiveServices.Speech;
    using Microsoft.CognitiveServices.Speech.Audio;
    
  3. Below the using statements, add the following code, which uses Azure AI Speech APIs to convert the contents of the text file that you'll create to create a WAV file with the synthesized voice. Replace the azureKey and azureLocation values with the ones you copied in the last exercise.

    string azureKey = "ENTER YOUR KEY FROM THE FIRST EXERCISE";
    string azureLocation = "ENTER YOUR LOCATION FROM THE FIRST EXERCISE";
    string textFile = "Shakespeare.txt";
    string waveFile = "Shakespeare.wav";
    
    try
    {
        FileInfo fileInfo = new FileInfo(textFile);
        if (fileInfo.Exists)
        {
            string textContent = File.ReadAllText(fileInfo.FullName);
            var speechConfig = SpeechConfig.FromSubscription(azureKey, azureLocation);
            using var speechSynthesizer = new SpeechSynthesizer(speechConfig, null);
            var speechResult = await speechSynthesizer.SpeakTextAsync(textContent);
            using var audioDataStream = AudioDataStream.FromResult(speechResult);
            await audioDataStream.SaveToWaveFileAsync(waveFile);       
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    
    }
    

    This code uses your key and location to initialize a connection to Azure AI services, then reads the contents of the text file you'll create, then uses the SpeakTextAsync() method of the speech synthesizer to convert the text to audio, then uses an audio stream to save the results to an audio file.

  4. When you've finished adding all of the code, your file should resemble the following example:

    using System.Text;
    using Microsoft.CognitiveServices.Speech;
    using Microsoft.CognitiveServices.Speech.Audio;
    
    string azureKey = "ENTER YOUR KEY FROM THE FIRST EXERCISE";
    string azureLocation = "ENTER YOUR LOCATION FROM THE FIRST EXERCISE";
    string textFile = "Shakespeare.txt";
    string waveFile = "Shakespeare.wav";
    
    try
    {
        FileInfo fileInfo = new FileInfo(textFile);
        if (fileInfo.Exists)
        {
            string textContent = File.ReadAllText(fileInfo.FullName);
            var speechConfig = SpeechConfig.FromSubscription(azureKey, azureLocation);
            using var speechSynthesizer = new SpeechSynthesizer(speechConfig, null);
            var speechResult = await speechSynthesizer.SpeakTextAsync(textContent);
            using var audioDataStream = AudioDataStream.FromResult(speechResult);
            await audioDataStream.SaveToWaveFileAsync(waveFile);       
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);                
    }
    

    Make sure that you update the values for the azureKey and azureLocation variables with your key and location from the previous exercise.

  5. To save your changes, press Ctrl+S to save the file, and then press Ctrl+Q to exit the editor.

Create a text file for your application to read

  1. In the Cloud Shell on the right, create a new text file that your application will read:

    code Shakespeare.txt
    
  2. When the code editor appears, enter the following text.

    The following quotes are from act 2, scene 7, of William Shakespeare's play "As You Like It."
    
    Thou seest we are not all alone unhappy:
    This wide and universal theatre
    Presents more woeful pageants than the scene
    Wherein we play in.
    
    All the world's a stage,
    And all the men and women merely players:
    They have their exits and their entrances;
    And one man in his time plays many parts,
    His acts being seven ages.
    
  3. To save your changes, press Ctrl+S to save the file, and then press Ctrl+Q to exit the editor.

Run your application

  1. To run your application, use the following command in the Cloud Shell on the right:

    dotnet run
    
  2. If you don't see any errors, your application has run successfully. To verify, run the following command to get a list of files in the directory.

    ls -l
    

    You should get a response like the following example, and you should have the Shakespeare.wav file in the list of files:

    drwxr-xr-x 3 user   user     4096 Oct  1 11:11 bin
    drwxr-xr-x 3 user   user     4096 Oct  1 11:11 obj
    -rw-r--r-- 1 user   user     1328 Oct  1 11:11 Program.cs
    -rw-r--r-- 1 user   user      413 Oct  1 11:11 Shakespeare.txt
    -rw-r--r-- 1 user   user   955282 Oct  1 11:11 Shakespeare.wav
    -rw-r--r-- 1 user   user      348 Oct  1 11:11 text to speech.csproj
    

Optional: Listen to your WAV file

In order to listen to the WAV file that your application created, you'll first need to download it. To do so, use the following steps.

  1. In the Cloud Shell on the right, use the following command to copy the WAV file to your temporary cloud drive:

    cp Shakespeare.wav ~/clouddrive
    
  2. Sign in to the Azure portal with the same account you used to activate the sandbox.

  3. Select All resources on the Azure portal home page.

  4. When the All resources page is displayed, select your cloudshellNNNNN storage account, then select File shares, then select your cloudshellfilesNNNNN file share.

    Screenshot showing the Cloud Shell file shares.

  5. When your cloudshellfilesNNNNN file shares page is displayed, select Browse, then select the Shakespeare.wav file, then select the Download icon.

    Screenshot showing the file download link.

  6. Download the Shakespeare.wav file to your computer, where you can listen to it with your operating system's audio player.

Optional: Change the voice

The SpeechConfig class has a SpeechSynthesisVoiceName property you can use to specify a voice other than the default. You can find a list of voices you can use in the Text to speech section of the Language and voice support for the Speech service article.

To specify the voice, use the following steps.

  1. In the Cloud Shell on the right, open the Program.cs file:

    code Program.cs
    
  2. Locate the following line of code:

    var speechConfig = SpeechConfig.FromSubscription(azureKey, azureLocation);
    
  3. Add the following line after the previous line:

    speechConfig.SpeechSynthesisVoiceName = "en-SG-WayneNeural";
    
  4. The resulting section of code should look like the following example:

    string textContent = File.ReadAllText(fileInfo.FullName);
    var speechConfig = SpeechConfig.FromSubscription(azureKey, azureLocation);
    speechConfig.SpeechSynthesisVoiceName = "en-SG-WayneNeural";
    using var speechSynthesizer = new SpeechSynthesizer(speechConfig, null);
    

    When you make this change and recompile your application, the text to speech synthesis will use the new voice.