📜 ⬆️ ⬇️

Learn PowerShell to talk

In many science fiction films, people interact with computers using voice commands. For example, a person enters a room and it is dark there. “Computer, light 50%!” He says. “There is a light of 50%!” - responds with a pleasant female contralto computer and takes under the visor, including dim light. Who did not want this? But if the very idea of ​​such an interaction was born a long time ago, wandering through the pages of fantastic books and frames of films, now everything has changed: the availability of high technologies at hand has made a lot possible. In Robert Heinlein's novel The Moon is a Severe Mistress (1966), Mike could speak freely, but he occupied a huge amount of space and, of course, was not mobile. The hero of my story can not boast the perfect pronunciation or the ability to maintain a dialogue, but, for its size, it is very capable. Of course, I'm talking about the Asus EeePC 1000 netbook.

Not so long ago I installed a new MS Windows 7 RC operating system on it and could not get acquainted with what was offered in the kit. My biggest interest was the PowerShell command line and PowerShell ISE. Somewhere on the Internet there are a variety of cmdlets to perform almost any task, including speech reproduction. But I wanted to implement everything through a normal function, so as not to be dependent on the installed cmdlets. They, in the end, may not be on a specific computer, and copying a profile is a matter of minutes. In addition, the recent disappearance story in the new build, for example, the Get-Clipboard cmdlet for working with the clipboard, is alarming about the excessive use of rare cmdlets. But the .NET classes are not going anywhere, although access to them may not be the easiest either: in this case you have to start the console in -sta mode in order to be able to work with the clipboard.

I use the function name prefix from one of my nicknames in order to be able to auto-complete quickly to get to the self-written functions by entering just three characters: they are not found anywhere else in PowerShell commands.

Well, actually, the implementation itself:
')
function astSpeak([ string ]$inputString, [ int ]$speed = -2,
[ int ]$engine = 0, [ switch ]$file,
[ switch ]$list, [ switch ]$buffer,
[ int ]$volume = 85)
{
#
$oVoice = New-Object -com "SAPI.spvoice"

#
if ($list)
{
Write-Output " : "
$i = 0
Foreach ($Token in $oVoice.getvoices())
{
Write-Host $i - $Token.getdescription()
$i++
}
}
#
else
{
# ,
if ($file){ $toSpeechText = Get-Content $inputString}
# ( sta)
elseif($buffer){
$ null = [reflection. Assembly ]::LoadWithPartialName( "System.Windows.Forms" )
$toSpeechText = [Windows.Forms.Clipboard]::GetText() }
# ,
else { $toSpeechText = $inputString}

#
$oVoice.rate = $speed
$oVoice.volume = $volume
$oVoice.voice = $oVoice.getvoices().item($engine)
$oVoice.Speak($toSpeechText)
}
}


* This source code was highlighted with Source Code Highlighter .

What can function?

You can display a list of votes set in the system. There is a fairly large number of various voices, including those who can read the Russian text. I have the voice of Alena as the main one, who quite successfully passed the test by reading articles from Wikipedia and fiction. For the latter, Harry Harrison’s short, ironic story Absolute Weapon, taken at the Moshkov Library, was used. Thanks to the voice, it was immediately possible to find a few typos in the text and understand the necessity of placing the letter “e”, otherwise the reading was impressive. So, to get a list of voice engines, you need to call:

astSpeak -l

image

You can read any given text, well, this is the simplest.

astSpeak "Hi! My name is Alena, I am the voice of your computer! ”

Can read text file. To do this, specify the appropriate key and the path to the desired file. For example:

astSpeak -f "D: \ Library \ Redyard Kipling \ Commandment.txt"

Yes ... I guess, we won't trust Alena anymore :)

You can read the text contained in the clipboard. With the implementation of this, there are some difficulties, since the special cmdlet was excluded from the PowerShell delivery package, and you can access the buffer using .NET only in the sta (Single Threading Apartment) mode, and the PowerShell console starts in mta mode by default. In mta mode, the default ise. But this is not a problem, it is enough to register the corresponding key in the properties of the console shortcut:

image

So, if the console mode allows, it will look like this:

astSpeak -b

The worst thing is if there was a URL.

When you call any mode that involves the reproduction of speech (well, that is, in addition to the enumeration of voice engines), you can also set the speed of speech, voice volume and the number of the sound engine. For example, to hear the word “hello” by Katerina (engine number 1), the volume set to 85/100 and with a slightly increased speed, you need to enter the following command.

astSpeak -e 1 -v 85 -s 3 "Hi"

In general, everything is really simple! If you have any comments, tips or you know how to improve it all, I will be very happy to hear from you. And ... with the first topic on me! :)

Source: https://habr.com/ru/post/60965/


All Articles