Windows中有一个很酷的功能,可让您让您的计算机说话或说(computer talk or speak)任何话!此方法利用安装在Windows XP、Vista、7、8 和 10上的内置API ,称为(API)SAPI(语音应用程序编程接口(Speech Application Programming Interface))。
Microsoft Speech API用于 Windows中内置的文本到语音辅助功能(accessibility feature)。您还可以编写调用API的程序,这将允许您让您的应用程序说话,但这是为开发人员准备的。
实际上,让您的计算机说出您想要的内容非常容易!在本文中,我将向您展示如何创建一个VBS 脚本(VBS script),该脚本将接收来自用户的文本输入(text input),然后说出它。我还将提到如何硬编码您想要说出的文本,然后安排该脚本在特定事件上运行,例如启动Windows或注销。
输入文字,说出讯息
让我们从一个简单的脚本开始,它会弹出一个对话框(dialog box),您可以在其中输入您希望 Windows 说出的内容。首先(First),继续打开记事本(Notepad)。
第 1 步:(Step 1:) 将以下文本粘贴到新文档中:
Dim message, sapi
message=InputBox(“Enter the text you want spoken”,”Speak This”)
Set sapi=CreateObject(“sapi.spvoice”)
sapi.Speak message
请注意,当您从Web 浏览器复制文本并将其粘贴(web browser and paste)到记事本(Notepad)中时,引号会引起问题。在保存文件之前,您需要检查并删除每个引号 (") 并重新输入引号。在下面的示例中,有六个引号。它应该类似于下图(image below)。
现在,当您保存文件时,给它起任何名称,但请确保您还在名称后键入.VBS。接下来,对于Save as type框,选择All files而不是默认的Text Documents。
现在双击VBS 文件(VBS file),您应该会看到一个输入框(input box),您可以在其中输入您想要朗读的文本!输入一些内容(Type something),然后单击“确定”。
如果你做的一切都正确(everything right),你应该听到男性或女性的声音说出你的句子。如果您收到任何类型的错误消息(error message),请确保再次复制并粘贴文本并替换这些引号。
配置 SAPI 语音设置
这很有趣,但我们也可以使用不同的设置来配置我们的小型会说话的计算机。我们可以改变音量,声音说话的快慢,改变男性和女性的性别。这是一些示例代码(example code),我在其中添加了几行额外的行。
Dim message, sapi
message=InputBox(“Enter the text you want spoken”,”Speak This”)
Set sapi=CreateObject(“sapi.spvoice”)
Set sapi.Voice = sapi.GetVoices.Item(1)
sapi.Rate = 0
sapi.volume = 100
sapi.Speak message
默认速率(default rate)为 0 ,范围为 -10 到 10。-10 表示声音超级慢,10 表示声音超级快。音量默认为 100,范围为 0 到 100。以Set sapi.Voice 开头(Set sapi.Voice)的行将允许您更改为另一个声音,如果安装在您的系统上。
请注意,您可以选择的声音数量取决于操作系统(operating system)。在Windows 7中,只有一种声音,Microsoft Anna。
在Windows 10中,有两种声音:Microsoft David 和 Microsoft Zira(Microsoft David and Microsoft Zira),如下图所示。
如果您的系统上只安装了一个语音,您必须将sapi.GetVoices.Item(1)更改为sapi.GetVoices.Item(0) ,否则当您尝试运行脚本时会收到错误消息。(error message)您还可以创建另一个记事本文档并将下面的代码粘贴(Notepad document and paste)到其中,它会告诉您安装了哪些声音。将其另存为 .VBS 文件,如上所示并运行它。
Set VObj = CreateObject(“SAPI.SpVoice”)
For Each Voice In VObj.getvoices
I = I + 1
msgbox “” & (I – 1) & ” – ” & Voice.GetDescription
Next
到目前为止,我们一直在使用这个弹出对话框(popup dialog)来输入文本,但您也可以简单地将您的消息输入到脚本文件(script file)中。如果您想安排脚本自动运行,这将很有用。显然,这可以用来恶作剧你的朋友和家人,而且效果很好。
想象一下有人打开电脑,听到电脑告诉他们“你好约翰(Hello John),我很困,今天请不要打扰我!” 当它启动时!我可以从经验中向您保证,这是歇斯底里的,您绝对应该尝试一下。
为了对消息进行硬编码,只需将最后一行更改为如下所示:
sapi.Speak “Hello John, I am very tired today!”
计划脚本文件
现在您已经为您的预期目标硬编码了您的消息,您只需在需要时运行它。如果您精通技术,则可以使用AutoIt之类的程序,它可以让您做一些极端的事情,例如在打开某个程序或最大化或最小化窗口时运行脚本文件。(script file)
我没有时间在这篇文章中深入探讨,但幸运的是, Microsoft有一个称为(Microsoft)任务计划程序(Task Scheduler)的内置功能,可以让您轻松地使用脚本完成一些非常复杂的事情。
我之前的帖子深入探讨了如何安排脚本文件在(script file)Windows中发生某些事件时运行。如果您在使脚本正常工作时遇到任何问题,请随时发表评论,我会尽力提供帮助。享受!
How to Make Your Computer Talk/Speak What You Type
There is сool feature in Windоws that allows you to make your computer talk or speak whatever you tell it! This method takes advantage of a built-in API installed on Windows XP, Vista, 7, 8 and 10 called SAPI (Speech Application Programming Interface).
The Microsoft Speech API is what is used for the text-to-speech accessibility feature built into Windows. You can also write programs that call the API, which would allow you to let your applications speak, but that’s for developers.
Just making your computer say what you want is pretty easy actually! In this article, I’ll show you how to create a VBS script that will take a text input from a user and then speak it. I’ll also mention how to hard code the text you want spoken and then schedule that script to run on specific events like starting Windows or logging off.
Input Text, Speak Message
Let’s start off with a simple script that will popup a dialog box where you can type in what you want Windows to speak. First, go ahead and open Notepad.
Step 1: Paste the following text into a new document:
Dim message, sapi
message=InputBox(“Enter the text you want spoken”,”Speak This”)
Set sapi=CreateObject(“sapi.spvoice”)
sapi.Speak message
Note that when you copy text from your web browser and paste it into Notepad, the quotes will cause problems. Before you save the file, you need to go through and delete each quote (“) and retype the quote. In the example below, there are six quotes. It should look something like the image below.
Now when you go to save the file, give it any name, but make sure you also type .VBS after the name. Next, for the Save as type box, choose All files instead of the default Text Documents.
Now double-click on the VBS file and you should see an input box where you can type in the text you want spoken! Type something and click OK.
If you did everything right, you should hear either a male or female voice speak out your sentence. If you get any kind of error message, make sure to copy and paste the text again and replace those quotes.
Configure SAPI Voice Settings
This is fun, but we can also configure our little talking computer with different settings. We can change the volume, how fast or slow the voice talks and change the gender between male and female. Here is some example code where I added a couple of extra lines.
Dim message, sapi
message=InputBox(“Enter the text you want spoken”,”Speak This”)
Set sapi=CreateObject(“sapi.spvoice”)
Set sapi.Voice = sapi.GetVoices.Item(1)
sapi.Rate = 0
sapi.volume = 100
sapi.Speak message
The default rate is 0 and the range is -10 to 10. -10 will have the voice speak super slowly and 10 will speak super fast. The volume is defaulted at 100 and the range is 0 to 100. The line that starts with Set sapi.Voice will allow you to change to another voice, if installed on your system.
Note that the number of voices you can choose from depends on the operating system. In Windows 7, there is only one voice, Microsoft Anna.
In Windows 10, there are two voices: Microsoft David and Microsoft Zira as shown below.
If there is only one voice installed on your system, you have to change that says sapi.GetVoices.Item(1) to sapi.GetVoices.Item(0), otherwise you will get an error message when you try to run the script. You can also create another Notepad document and paste the code below in, which will tell you what voices are installed. Save it as a .VBS file like shown above and run it.
Set VObj = CreateObject(“SAPI.SpVoice”)
For Each Voice In VObj.getvoices
I = I + 1
msgbox “” & (I – 1) & ” – ” & Voice.GetDescription
Next
Up until now, we have been using this popup dialog to enter text, but you could also simply type your message into the script file. This would be useful if you wanted to then schedule the script to run automatically. Obviously, this can be used to prank your friends and family and it works really well.
Just imagine someone turning on their computer and hearing the computer tell them “Hello John, I’m very sleepy, please don’t bother me today!” when it boots up! I can promise you from experience, it’s hysterical and you should definitely try it.
In order to hardcode the message, simply change the last line to something like this:
sapi.Speak “Hello John, I am very tired today!”
Schedule Script File
Now that you have hardcoded your message for your intended target, you simply have to run it when you want. If you are tech-savvy, you could use a program like AutoIt, which will allow you to do extreme stuff like run your script file when a certain program is opened or when a windows is maximized or minimized.
I don’t have time to get into all that in this post, but luckily Microsoft has a built-in feature called the Task Scheduler that lets you do some pretty complicated stuff with scripts easily.
My previous post goes in depth on how to schedule a script file to run when certain events occur in Windows. If you have any trouble getting your script to work, feel free to post a comment and I’ll try to help. Enjoy!