Learn PowerShell to talk

In many fantastic movies, people interact with computers using voice commands. For example, a man comes into the room and it's dark. "Computer, lights 50%!" he says. "There is a light 50%!" — speaks a pleasant female contralto a computer and takes under the hood, including a dim light. Someone did not like? But if the idea of such cooperation was born for a very long time, wandering ever since in the pages of science fiction books and films frames, but now everything has changed: the availability of hand high technology has made many things possible. In the novel by Robert Heinlein "Moon is a harsh mistress" (1966.) the supercomputer Mike could speak freely, but he took a huge amount of space and, of course, was not mobile. The hero of my story can not boast of perfect pronunciation or the ability to maintain a dialogue, but for its size it is very capable. Of course, I'm talking about netbook Asus EeePC 1000.

Not long ago I installed a new operating system MS Windows 7 RC and could not find what is being offered in the kit. My greatest interest was caused a command prompt PowerShell and PowerShell ISE. Somewhere on the Internet there are a variety of cmdlets to implement almost any task, including for speech. But I wanted to implement everything using a normal function, not to be dependent on the installed cmdlets. They are, after all, may not be on a specific computer, and the profile copy — a matter of minutes. In addition, the recent story of the disappearance in the new Assembly, for example, the cmdlet Get-Clipboard to work with the clipboard and worrying concerning the excessive use of rare cmdlets. But classes .NET is not going anywhere, although access to them can also be not the most simple: in this case you have to run the console mode-sta to have the opportunity to work with the clipboard.

Prefix the function name I use from one of its nicknames, to be able to auto-completion to quickly get to the in-house function by typing in just three characters: they are anywhere else in the PowerShell commands does not occur.

the Well, here's the implementation:

astSpeak function([string]$inputString, [int]$speed = -2
[int]$engine = 0, [switch]$file
[switch]$list, [switch]$buffer
[int]$volume = 85)
{
# Create object
$oVoice = New-Object -com "SAPI.spvoice"

# If you want to display a list of votes
if($list)
{
Write-Output "Installed: "
$i = 0
Foreach ($Token in $oVoice.getvoices())
{
Write-Host $i - $Token.getdescription()
$i++
}
}
# If you need to speak
else
{
# Get the text from a file, if you specify switch
if($file){ $toSpeechText = Get-Content $inputString}
# Recite the text from the clipboard (requires sta mode)
elseif($buffer){
$null = [reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$toSpeechText = [Windows.Forms.Clipboard]::GetText() }
# Use this line if switch is not set
else{ $toSpeechText = $inputString}

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


* This source code was highlighted with Source Code Highlighter.

the What you can do with a function?

You can list the installed voices. There are quite a number of different voices, including those who can read Russian text. I have installed as the primary voice of Alain, which is successfully passed the inspection and reading of Wikipedia articles and fiction. As the latter was used ironic short story by Harry Harrison "the ultimate Weapon" from the Library Moshkova. Thanks to the voice immediately managed to find a few typos in the text and to understand the necessity of placing the letters "e", otherwise, the reading was impressive. So, to get a list of the voice engine, you need to call:
the astSpeak -l

image

You can read any given text, well, it's the simplest.

the astSpeak "hi! My name is Alyona, I am the voice of your computer!"

You can read a text file. To do this, you must specify the appropriate key and the path to the desired file. For example:

the astSpeak -f "D:\Библиотека\Редъярд Киплинг\Заповедь.txt"

Yes... perhaps to trust Allen's poems will no longer :)

You can read the text contained in the clipboard. With the realization that there are some difficulties, as a special cmdlet has been removed from the package PowerShell, and to access the buffer means .NET only in sta mode (Single Threading Apartment), and the PowerShell console runs in mta by default. In the mta running by default ISE. But it's not a problem enough in the shortcut properties console to prescribe the appropriate key:

image

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

the astSpeak -b

The worst thing — if there was a URL.

When you call any mode that involves playing (I mean in addition to listing the voice engines), you can also set the speech speed, voice volume and number of the sound engine. For example, to hear the word "Hello" performed by Catherine (engine number 1), the volume mounted on and 85/100 with a slightly increased speed, enter the following command.

the astSpeak -e 1 -v 85 -s 3 "Hello"

In General, it really is that easy! If you have any comments, tips or you know how to improve it all, I will be very glad to hear from you. And... the first topic on habré me! :)
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

A Bunch Of MODx Revolution + LiveStreet

Monitoring PostgreSQL with Zabbix

PostgreSQL load testing using JMeter, Yandex.Tank and Overload