Get your computer to narrate a text file

Get your computer to narrate a text file

As alluded to in my article on the History of Windows PowerShell, one does not necessarily need to read everything the see. Instead, one can get the computer to read it for them.

There are many ways to approach this, and in the past a programmer would make use of the Windows COM object to access Microsoft's sapi.spvoice, which many of you will know as Microsoft David Desktop.

Windows PowerShell though, can make use of the SpeechSynthesizer Class to narrate text for you. This class comes with quite of lot of features a programmer can use, but I'll just be touching on a few for now to quickly demonstrate how you can have a little fun, and at the same time choose to get your computer to do the chore or reading.

For starters, let's open Windows PowerShell (Admin)
  • Depending on your personal settings, you can open the Windows PowerShell (Admin) a couple of ways:
  1. Right-click on Start and select Windows PowerShell (Admin), or
  2. In your search field type powershell then in the search results, locate and right-click on Windows Powershell and select Run as Administrator
Note: Whichever method you use to open the Windows PowerShell (Admin) you will receive a prompting from the User Account Control which you will need to click Yes to.

ps1.png

In order to make use of the SpeechSynthesizer Class you need to start your script with two key entries. The first line of your script will be as follows:

Code:
Add-Type -AssemblyName System.speech

Press Enter key

This first line of code, instructs PowerShell to add the System.speech assembly, which we need as this is not automatically loaded for us.

Next you want to create a New-Object of the SpeechSynthesizer Class. To do this we create a variable to store the object and in Windows PowerShell a variable begins with a $ symbol then we can name it whatever we like to. I'm going to name mine $Narrator as I'll be using it to narrate text to me.

So we'll type the following code onto the next line:

Code:
$Narrator = New-Object System.Speech.Synthesis.SpeechSynthesizer

Press Enter key

ps2.png


In the above image we have got Windows PowerShell to load the System.speech assembly and created a new SpeechSynthesizer object stored in the variable $Narrator

Now as I said before, this Object has a lot of options the programmer can make use of. To view the options you have type the following at the prompt:

Code:
$Narrator | Get-Member

Note: Make sure you use the object variable you created. in my case it is $Narrator but if you named it something else then use that in above command.

Press Enter key to execute

ps3.png


You can see above, I can make use of Events, Methods & Properties to control my object. You will notice though there are a few entries with . . . so to view all the details of that item, for example the Speak method type the following:

Code:
$Narrator | Get-Member Speak

ps3b.png


So let's try one of the Methods now:

Type the following command to make use of the Speak() method:

Code:
$Narrator.Speak("Hello, I'll do the reading for you.")

Press Enter key

ps4.png


Windows PowerShell will invoke the Speak() method and read the text you just typed between the speech marks.

Now you can also control how slow / fast it will speak.

The scale for the speed it will speak goes from -10 to 10

The default setting is 0 but you can change this to be slower or faster using the Property: Rate

So to make it speak extremely slowly type: $Narrator.Rate = -10
Or, to get it to speak extremely quickly type: $Narrator.Rate = 10

For example:

ps5.png


Each Windows edition comes with one or more built-in voices. You may be wondering how many you have. To check you can make use of the GetInstalledVoices() method by typing the following command:

Code:
$Narrator.GetInstalledVoices()

Press Enter key

ps6.png


My results show I have two choices. But which voices are they?

To find out, type the following command using the Voice Property option:

Code:
$Narrator.GetInstalledVoices().VoiceInfo

Press Enter key

ps7.png


You can see I have Microsoft David Desktop & Microsoft Zira Desktop

At the moment my settings default to David. But if I want to use Zira's voice instead, then I can type this command to invoke her.

Code:
$Narrator.SelectVoice('Microsoft Zira Desktop')

Press Enter key

This makes use of the SelectVoice() method

Example:

ps8.png


Another cool thing you can do is to record to file what you get it to narrate.

To do this you make use of the SetOutputToWaveFile() method and also the Dispose() method

Here's an example of the code you could type:

Code:
Add-Type -AssemblyName System.speech
$Narrator = New-Object System.Speech.Synthesis.SpeechSynthesizer
$Narrator.SelectVoice('Microsoft Zira Desktop')
$Narrator.SetOutputToWaveFile("$env:userprofile\Desktop\narration.wav")
$Narrator.Speak("Hello, I'm Zira and you apparently are recording me to your Desktop!")
$Narrator.Dispose()

You can select the entire code above, then copy & paste into your Administrator: Windows PowerShell console if you want.

ps9.png


Now press your Windows key + D to get to the desktop and you'll discover a new wave file named narration.wav — click it to play.

You'll be aware that in the Command Prompt console when you change directories your can use shortcuts like %userprofile% rather than typing the full path to C:\Users\Regedit32 in my case [ you'd replace Regedit32 with your username of course ].

Windows PowerShell does not like that shorthand method, so instead you need to make use of a special variable: $env which is the Environment variable

Then rather than use things like %userprofile% or %localappdata% you instead type userprofile or localappdata instead. You can see an example of this in the above code where I used $env:userprofile\Desktop which is short in my case for C:\Users\Regedit32\Desktop

Now in my previous article on the History of Windows PowerShell I mentioned a website storing the Monad Manifesto which is an interesting read. It is a long read though [ although it is worth reading as it has images too not just text. ]

That said, I've attached a text file named Monad-Manifesto.txt here which you can download to your Downloads folder.

This file is stripped of the images of course, but otherwise contains the full text content of the Manifesto. You don't need to read it though. Let's get the computer to read it to us instead. Here is how.

After downloading Monad-Manifesto to your Downloads folder, type or copy & paste the following code into your Administrator: Windows PowerShell console:

Code:
Add-Type -AssemblyName System.speech
$Narrator = New-Object System.Speech.Synthesis.SpeechSynthesizer
$Narrator.SelectVoice('Microsoft Zira Desktop')
$Narrator.Rate = 2
$Location = "$env:userprofile\Downloads\Monad-Manifesto.txt"
$Contents = Get-Content $Location
$Narrator.Speak($Contents)

Press Enter key

ps10.png


Now this is a lengthy file to read: So if you get bored just close the console!

Zira's voice will be silenced *lol*


Well that is it for now.


Regards,

Regedit32
  • Like
Reactions: JAY B. and Ian
Author
Regedit32
First release
Last update

More resources from Regedit32

Top