Author Topic: Fun Stuff! - Text To Speech using Windows’ Powershell  (Read 6767 times)

0 Members and 1 Guest are viewing this topic.

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Fun Stuff! - Text To Speech using Windows’ Powershell
« 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]
« Last Edit: January 18, 2019, 02:32:13 am by Prithak »
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #1 on: January 17, 2019, 02:37:34 am »
Nice :-D
« Last Edit: January 17, 2019, 05:49:35 am by odin »

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #3 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.  
« Last Edit: January 17, 2019, 02:00:54 pm by bplus »

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #4 on: January 17, 2019, 02:54:52 pm »
Very impressive. A couple of lines only, and it works like a champ. Congrats, Prithak!!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #5 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
Logic is the beginning of wisdom.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #6 on: January 17, 2019, 04:19:46 pm »
Runs nicely in 32 bit, only get one go round in 64

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #7 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

FellippeHeitor

  • Guest
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #8 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

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #9 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #10 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.  
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #11 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #12 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? 

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #13 on: January 19, 2019, 11:50:37 am »
interesting... can this be reversed SPEECH -> TEXT?




I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Fun Stuff! - Text To Speech using Windows’ Powershell
« Reply #14 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








Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/