QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Prithak on January 17, 2019, 01:51:28 am

Title: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Prithak on January 17, 2019, 01:51:28 am

I created this code just for fun!
Code: QB64: [Select]
  1.     INPUT "Enter text: "; text$
  2.     SHELL _HIDE "Powershell -Command " + CHR$(34) + "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('" + text$ + "');" + CHR$(34)
  3.  

-Prithak
[/font]
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Petr on January 17, 2019, 02:37:34 am
Nice :-D
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Prithak on January 17, 2019, 06:19:12 am
Nice :-D
:)
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: bplus on January 17, 2019, 12:30:44 pm
Yeah fun! Here is one liner robot comedian (for Windows users)
https://www.qb64.org/forum/index.php?topic=215.msg1080#msg1080

Perhaps your method is more successful than my code in different Windows systems.

EDIT Update:
Yep! substitution of Prithak's code works fine in my windows system with the code I was playing with.
Code: QB64: [Select]
  1. SUB speak (message AS STRING)
  2.     'modifed from "Code" at JB forum
  3.     'OPEN "sound.vbs" FOR OUTPUT AS #1
  4.     'PRINT #1, "Dim message, sapi"
  5.     'PRINT #1, "message=" + CHR$(34) + message + CHR$(34) + " "
  6.     'PRINT #1, "Set sapi=CreateObject(" + CHR$(34) + "sapi.spvoice" + CHR$(34) + ")"
  7.     'PRINT #1, "sapi.Speak message"
  8.     'CLOSE #1
  9.     'SHELL "wscript sound.vbs"
  10.     SHELL _HIDE "Powershell -Command " + CHR$(34) + "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('" + message + "');" + CHR$(34)
  11.  
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Bert22306 on January 17, 2019, 02:54:52 pm
Very impressive. A couple of lines only, and it works like a champ. Congrats, Prithak!!
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: johnno56 on January 17, 2019, 04:06:39 pm
A few days ago I installed Google Speech on my Linux machine and used the Shell command in qb64 to add speech. Very cool. Could be used for interactive fiction or even as a compliment to message boxes. Cannot find out how to change to a male voice. But Google Speech comes with Sox Effects which can make the voice 'almost' sound like a male.

What are you guy going to use speech for? Just curious.

J
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Dimster on January 17, 2019, 04:19:46 pm
Runs nicely in 32 bit, only get one go round in 64
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Pete on January 17, 2019, 05:48:59 pm
It makes a nice clipboard reader for my Windows 10.

Code: QB64: [Select]
  1. WIDTH 80, 3
  2. LOCATE 2, 1
  3. PRINT " Copy text to clipboard to have it read back by computer. Press [Esc] to quit.";
  4.     _LIMIT 10
  5.     text$ = _CLIPBOARD$
  6.     b$ = INKEY$
  7.     IF b$ <> "" THEN
  8.         IF b$ = CHR$(27) THEN SYSTEM
  9.     END IF
  10.     IF text$ <> "" THEN
  11.         _CLIPBOARD$ = ""
  12.         SHELL _HIDE "Powershell -Command " + CHR$(34) + "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('" + text$ + "');" + CHR$(34)
  13.     END IF
  14.  

Very cool. Thanks!

Pete
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: FellippeHeitor on January 17, 2019, 06:46:14 pm
You don't need to erase the clipboard every run:

Code: QB64: [Select]
  1. WIDTH 80, 3
  2. LOCATE 2, 1
  3. PRINT " Copy text to clipboard to have it read back by computer. Press [Esc] to quit.";
  4.     _LIMIT 10
  5.     text$ = _CLIPBOARD$
  6.     b$ = INKEY$
  7.     IF b$ <> "" THEN
  8.         IF b$ = CHR$(27) THEN SYSTEM
  9.     END IF
  10.     IF text$ <> previousText$ THEN
  11.         previousText = text$
  12.         SHELL _HIDE "Powershell -Command " + CHR$(34) + "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('" + text$ + "');" + CHR$(34)
  13.     END IF
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Pete on January 17, 2019, 07:02:53 pm
Dammit, I'm old and I have to have stuff repeated constantly. Your code doesn't allow that!

Pete and Re-Pete :D
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Prithak on January 18, 2019, 01:30:37 am
You don't need to erase the clipboard every run:

Code: QB64: [Select]
  1. WIDTH 80, 3
  2. LOCATE 2, 1
  3. PRINT " Copy text to clipboard to have it read back by computer. Press [Esc] to quit.";
  4.     _LIMIT 10
  5.     text$ = _CLIPBOARD$
  6.     b$ = INKEY$
  7.     IF b$ <> "" THEN
  8.         IF b$ = CHR$(27) THEN SYSTEM
  9.     END IF
  10.     IF text$ <> previousText$ THEN
  11.         previousText = text$
  12.         SHELL _HIDE "Powershell -Command " + CHR$(34) + "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('" + text$ + "');" + CHR$(34)
  13.     END IF
Found a simple little problem with the code.
Code: QB64: [Select]
  1. WIDTH 80, 3
  2. LOCATE 2, 1
  3. PRINT " Copy text to clipboard to have it read back by computer. Press [Esc] to quit.";
  4.     _LIMIT 10
  5.     text$ = _CLIPBOARD$
  6.     b$ = INKEY$
  7.     IF b$ <> "" THEN
  8.         IF b$ = CHR$(27) THEN SYSTEM
  9.     END IF
  10.     IF text$ <> previousText$ THEN
  11.         previousText$ = text$ 'You forgot to put $ in previousText xD
  12.         SHELL _HIDE "Powershell -Command " + CHR$(34) + "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('" + text$ + "');" + CHR$(34)
  13.     END IF
  14.  
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Pete on January 18, 2019, 10:17:02 am
Hey lay off... because... apparently Fell's getting old, too! :D

Kidding aside, I was bummed out a bit yesterday when I discovered the pause key wasn't able to pause the text reader. I've thought about playing around with feeding the shell command sentences and making a pause function that way. At least that way you could pause at the end of a sentence with a mouse click or key input in the program window. I haven't tried going word for word, but wow, that would probably pile up so many shell calls that the system would start to slow down and the user would think his PC was having a stroke... No Bill, not a keystroke.

Pete
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Dimster on January 19, 2019, 11:02:44 am
Hello Prithak - I've been having a lot of fun converting some of my menus to voice. Thank you very much for this piece of code. I was wrong about the 64 bit only giving one go round on the do loop, seems I just need to add a CLS to it whereas the 34 bit works great without. I am thinking Powershell has some limitations to the characters it can and cannot convert to voice. I'm having no problem with it talking over a period (.) to get to the next sentence but  a comma or apostrophe do not do well. The sentence "How's life" doesn't seem to work but "How is life" works fine. I'm trying to find out more on what letters or symbols are problematic but so far no luck. Thinking I could just try them all one at a time but would you know of a website that may have a dictionary on Powershell syntax? 
Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: xra7en on January 19, 2019, 11:50:37 am
interesting... can this be reversed SPEECH -> TEXT?




Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Pete on January 19, 2019, 02:03:52 pm
Code: QB64: [Select]
  1.     END IF
  2.         SHELL _HIDE "Powershell -Command " + CHR$(34) + "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('" + text$ + "');" + CHR$(34)
  3.         _CLIPBOARD$ = ""
  4.     IF text$ <> "" THEN
  5.     END IF
  6.         IF b$ = CHR$(27) THEN SYSTEM
  7.     IF b$ <> "" THEN
  8.     b$ = INKEY$
  9.     text$ = _CLIPBOARD$
  10.     _LIMIT 10
  11. PRINT " Copy text to clipboard to have it read back by computer. Press [Esc] to quit.";
  12. LOCATE 2, 1
  13. WIDTH 80, 3
  14.  

Well it was worth a try!

Pete :D








Title: Re: Fun Stuff! - Text To Speech using Windows’ Powershell
Post by: Prithak on January 19, 2019, 09:26:38 pm
interesting... can this be reversed SPEECH -> TEXT?
Who knows? Let's give it a try! Although I don't really know how to get input from microphone in QB64 yet, but I'll figure it out... Somehow...