Author Topic: Powershell Text To Speech  (Read 5229 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Powershell Text To Speech
« on: January 25, 2022, 05:04:08 pm »
Code: QB64: [Select]
  1. _Title "Steve's Powershell Speech Script"
  2.  
  3.  
  4. SaP "Hello World. This is a normal speed demo of David's voice", "David", 0
  5. SaP "Hello again.  This is a normal speed demo of Ziva's voice.", "Ziva", 0
  6. SaP "And now I'm speaking as David, but I'm speaking veeery slow.", "David", -10
  7. SaP "And now I'm a very hyper Ziva!", "Ziva", 5
  8. SaP "And now I'm done with my demo!", "", 0
  9.  
  10. Sub SaP (text$, who$, speed)
  11.     Print text$
  12.     If UCase$(who$) = "ZIVA" Then Speaker = 1
  13.     speak text$, Speaker, speed
  14.  
  15. Sub speak (text As String, Speaker As Integer, Speed)
  16.     Dim message As String
  17.     message = text
  18.     'some symbols and such can't be used with Powershell like this, as they're command symbols
  19.     'we need to strip them out of our text.  (Like apostrophes!)
  20.     remove$ = "'" + Chr$(34) 'add to remove$ here, if more symbols need to be removed as future testing showcases problems
  21.     For j = 1 To Len(remove$)
  22.         Do
  23.             i = InStr(message, Mid$(remove$, j, 1))
  24.             If i Then message = Left$(message, i - 1) + Mid$(message, i + 1)
  25.         Loop Until i = 0
  26.     Next
  27.     out$ = "Powershell -Command " + Chr$(34)
  28.     out$ = out$ + "Add-Type -AssemblyName System.Speech; "
  29.     out$ = out$ + "$Speech = New-Object System.Speech.Synthesis.SpeechSynthesizer; "
  30.     If Speaker = 0 Then out$ = out$ + "$Speech.SelectVoice('Microsoft David Desktop'); "
  31.     If Speaker = 1 Then out$ = out$ + "$Speech.SelectVoice('Microsoft Zira Desktop'); "
  32.     If Speed Then out$ = out$ + "$Speech.Rate =" + Str$(Speed) + "; "
  33.     out$ = out$ + "$Speech.Speak('" + message + "');" + Chr$(34)
  34.     Shell _Hide out$
  35.  

I've shared a few examples of this before in the past, but the project I was working on today was one which I thought could benefit from some decent Text-to-Speech capabilities, so I wrote up this little routine to give me a little more command over the output.

Choose from the voice you like -- male or female.  Set a speed for how fast they talk (valid range is -10 for slowest to 10 for fastest).  Send it some text and let it do it's thing.  It's that simple!

There's also a lazy man's SaP sub included for those who want to Speak And Print both at the same time. :P

Enjoy!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Powershell Text To Speech
« Reply #1 on: January 25, 2022, 09:40:14 pm »
Cool! I didn't know they could do different voices.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Powershell Text To Speech
« Reply #2 on: January 25, 2022, 10:06:04 pm »
Cool! I didn't know they could do different voices.

There's a lot you can do with them if you want.  There's volume options, pause, resume, stop...  There's a whole language you can use with it, much like html.  You just need to decide exactly what you want it to do for you.  Basically, if you turn on Narrator with Windows, you can reproduce those effects with powershell commands.  Read up on the windows speech synthesizer sometime if you're interested in expanding functionality.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Powershell Text To Speech
« Reply #3 on: January 26, 2022, 04:58:11 am »
As I do not use Windows, could someone please post a sample? Thank you.
Logic is the beginning of wisdom.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Powershell Text To Speech
« Reply #4 on: January 26, 2022, 05:58:46 am »
As I do not use Windows, could someone please post a sample? Thank you.

Post a sample of my PC speaking the text on the screen?  How do you suggest I do that?  Video capture the moment?  Be assured, if you never use Windows and Powershell, you can skip this demo as it has absolutely *zero* relation to you and your programming.  😘
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Powershell Text To Speech
« Reply #5 on: January 26, 2022, 08:27:09 am »
Steve,

I have little to no care about Windows, Powershell or the Demo. None of them relate to my needs. What I *am* interested in is, TTS and its *quality* of voice. Just to hear a few words is all that I would like. If the Demo is not capable of producing a sample audio file, then so be it, ignore my request and we will just move on... ;)
Logic is the beginning of wisdom.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Powershell Text To Speech
« Reply #6 on: January 26, 2022, 10:04:32 am »
Steve,

I have little to no care about Windows, Powershell or the Demo. None of them relate to my needs. What I *am* interested in is, TTS and its *quality* of voice. Just to hear a few words is all that I would like. If the Demo is not capable of producing a sample audio file, then so be it, ignore my request and we will just move on... ;)

There's an option to SendOutputToFile, but I've never played around with that before.  I'll have to experiment some and see how to specify location and format and such, and if I can get it working, I'll share the output (and code for others) later this evening sometime.  👍
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Powershell Text To Speech
« Reply #7 on: January 26, 2022, 01:03:06 pm »
Steve,

If it can be done, that would be great, if not then it is no big deal. After all... just curious... In any event... thank you for trying...

J
Logic is the beginning of wisdom.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Powershell Text To Speech
« Reply #8 on: January 26, 2022, 02:22:47 pm »
@johnno56 You're going to owe me two sodas AND a cheeseburger for this one!  Getting the syntax JUST right was a complete and utter PITA.  LOL!!

Code: QB64: [Select]
  1. _Title "Steve's Powershell Speech Script"
  2.  
  3. SaP "Hello World, This is a normal speed demo of David's voice", "David", 0
  4. SaP "Hello again.  This is a normal speed demo of Ziva's voice.", "Ziva", 0
  5. SaP "And now I'm speaking as David, but I'm speaking veeery slow.", "David", -10
  6. SaP "And now I'm a very hyper Ziva!", "Ziva", 5
  7. SaP "And now I'm done with my demo!", "", 0
  8.  
  9. Sub SaP (text$, who$, speed)
  10.     Print text$
  11.     If UCase$(who$) = "ZIVA" Then Speaker = 1
  12.     speak text$, Speaker, speed
  13.  
  14. Sub speak (text As String, Speaker As Integer, Speed)
  15.     Dim message As String
  16.     Static n: n = n + 1 'This sets an incremental counter for our output file  (Test1, Test2, Test3, ect)
  17.     message = text
  18.     'some symbols and such can't be used with Powershell like this, as they're command symbols
  19.     'we need to strip them out of our text.  (Like apostrophes!)
  20.     remove$ = "'" + Chr$(34) 'add to remove$ here, if more symbols need to be removed as future testing showcases problems
  21.     For j = 1 To Len(remove$)
  22.         Do
  23.             i = InStr(message, Mid$(remove$, j, 1))
  24.             If i Then message = Left$(message, i - 1) + Mid$(message, i + 1)
  25.         Loop Until i = 0
  26.     Next
  27.     out$ = "Powershell -Command " + Chr$(34)
  28.     out$ = out$ + "Add-Type -AssemblyName System.Speech; "
  29.     out$ = out$ + "$Speech = New-Object System.Speech.Synthesis.SpeechSynthesizer; "
  30.     out$ = out$ + "$Speech.SetOutputToWaveFile({" + Chr$(34) + "D:\Repo\qb64\Test" + _Trim$(Str$(n)) + ".wav" + Chr$(34) + "}); " 'The command to send the output to a file and not the speakers.
  31.     If Speaker = 0 Then out$ = out$ + "$Speech.SelectVoice('Microsoft David Desktop'); "
  32.     If Speaker = 1 Then out$ = out$ + "$Speech.SelectVoice('Microsoft Zira Desktop'); "
  33.     If Speed Then out$ = out$ + "$Speech.Rate =" + Str$(Speed) + "; "
  34.     out$ = out$ + "$Speech.Speak('" + message + "');" + Chr$(34)
  35.     Shell out$
  36.     _Echo out$
  37.  

 
 
 
 
 

As you can see, these are actually some rather nice sounding voices.  I truly wouldn't be ashamed to make use of them for any of my personal projects in the future!  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Powershell Text To Speech
« Reply #9 on: January 26, 2022, 03:39:47 pm »
OR Run this and just jump to about 2 mins into demo and hear the sound of the Speech:

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Powershell Text To Speech
« Reply #10 on: January 26, 2022, 06:02:12 pm »
Steve,

Nicely done. The quality of these voices a way better that Linux's eSpeak and Festival. The female voice is very good. Thank you for taking the time and effort in creating the files. Much appreciated. As they say here in Australia, "Your blood's worth bottling". (bit of cultural home work... lol)

Mmm... Cheeseburger...
Logic is the beginning of wisdom.