Festival TTS viseme file generation in bash / python - python

Am currently working on a project that uses Festival TTS engine on the RPi.
Have used it plenty before. I have also used pyfestival to do basic stuff. I am currently trying to generate the viseme file for a piece of text. This works perfectly if done in the Festival command interpreter, I need to do the exact same but in bash or python.
festival> (set! mytext (SayText "Hello word"))
festival> (utt.save.segs mytext "hw_viseme_file")
Have searched high and low for this info but have drawn a blank. Can someone give an example of how the 2 lines of code above can be called from either bash or Python?
Many thanks.
DP.

The festival command has a --script option, so you should be able to run from bash something like:
festival --script generate_visemes
Where 'generate-visemes' contains the Festival commands (above) that you want to execute.
From python you can call an external program like this:
os.system("festival --script generate_visemes")
Perhaps to create the script file, something like this:
import os
ttsMessage = "Hello World"
ttsVisemeFile = "hw_viseme_file"
f = open('generate_visemes','w')
textParam = '(set! mytext(SayText "{0}"))\n'.format(ttsMessage)
visemeParam = '(utt.save.segs mytext "{0}")\n'.format(ttsVisemeFile)
f.write(textParam)
f.write(visemeParam)
f.close()
os.execute("festival --script generate_visemes")

Finally worked this out - is fairly involved. The following runs in Python 2.7 on RPi when Festival TTS engine is installed.
import os
#Simple example
bashcommand = "echo 'Hello World' | festival --tts"
os.system(bashcommand)
# This bash command takes the entered phrase and returns an audio .wav file and a text file of the visemes
while True:
phrase = raw_input("Enter phrase:")
bashcommand = "festival -b '(set! mytext (Utterance Text " + '"' + phrase + '"))' + "' '(utt.synth mytext)' '(utt.save.wave mytext " + '"my_wav.wav")' + "' '(utt.save.segs mytext " + '"textfile"' + ")'"
os.system(bashcommand)
You can of course also just run the bash from the command line. Hope that may help someone in the future.

Related

ModuleNotFoundError when running a Python script from the command prompt/Run window (Windows 10)

I've been following the Automate the Boring Stuff with Python book and I'm stuck on chapter 6's chapter project - multi-clipboard automatic messages. Here is the code:
#! python3
# mclip.py - A multi-clipboard program.
import sys, pyperclip
TEXT = {"agree": """Yes I agree, that sounds fine to me.""",
"busy": """Sorry, can we do this next week or later this week?""",
"upsell": """Would you consider making this a monthly donation?"""}
if len(sys.argv) < 2:
print("Usage: python mclip.py [keyphrase] - copy phrase text")
sys.exit()
keyphrase = sys.argv[1]
if keyphrase in TEXT:
pyperclip.copy(TEXT[keyphrase])
print("Text for " + keyphrase + " copied to clipboard.")
else:
print("There is no text for " + keyphrase + ".")
Here is the notepad batch file I've created, it is located at C:\Windows\mclip.bat :
#py.exe C:\Users\ayram\PycharmProjects\ATBS\mclip.py %*
#pause
When I use "mclip.py agree" in the Run window or in the command prompt, it says that there is no command with this name. But when I type "mclip agree" instead, it says there is no module named pyperclip and I get the ModuleNotFoundError. When I use pyperclip.copy() and pyperclip.paste() in my IDE (python 3.10 - PyCharm), it works just fine though, so it seems that the IDE recognizes the module but not Windows? Any idea as to what the problem could be? Complete beginner here.
Thank you

Subprocess wont take the output of dxl script

Im using Python's subprocess module to run a dxl script. My Problem is when i try to catch the Output (In this example a print-statement or a error message) of my dxl script, it is shown in the command prompt, but when i try to catch it with stdout=subprocess.PIPE or subprocess.check_output it always returns an empty string. Is there a way to catch the output or how could I get the Error messages from Doors?
It's important that you dont see the GUI of DOORS.
Here is my quick example that shows my problem:
test.dxl
print "Hello World"
test.py
import subprocess
doorsPath = "C:\\Program Files (x86)\\IBM\\Rational\\DOORS\\9.5\\bin\\doors.exe"
userInfo = ' -user dude -password 1234 -d 127.0.0.1 -batch ".\\test.dxl"'
dxl = " -W"
output = subprocess.check_output(doorsPath+dxl+userInfo)
print(output)
Edit: Using Windows 7 , DOORS 9.5 and Python 2.7
I know this post is pretty old, but the solution to the problem is to use
cout << ... instead of print. You can override the print perms like shown here
DOORS Print Redirect Tutorial for print, cout and logfiles
I'm feeling lucky here,
change print "Hello World" to cout << "Hello World"
and userInfo = ' -user dude -password 1234 -d 127.0.0.1 -batch ".\\test.dxl > D:\output.txt"', as in cmd promt the text can be directly exported to a text file.
your script have many error try this link for example for subprocess
and try this :
import subprocess
import sys
path = "C:\\Program Files(x86)\\IBM\\Rational\\DOORS\\9.5\\bin\\doors.exe"
userInfo = "C:\\Program Files (x86)\\IBM\\Rational\\DOORS\\9.5\\bin\\doors.exe"
proc = subprocess.Popen([path,userInfo,"-W"])
proc.communicate()
i hape it work on your system!

Executing Code in Python Web

im creating a webpage that will show me the SSID's available in my Network
For this I have use this code:
nm-tool | grep "Infra" | cut -d " " -f5 > /home/nunukene/SSID3.txt
Im saving this into a file called SSID3, to later open it using the open() , read() and str.split
My problem is that the code I want to execute in the page, wont get executed, the file SSID3.txt wont be created
This is my website code so far:
#!/usr/bin/python
import os
import subprocess
import cgitb
cgitb.enable()
a=os.system("""nm-tool | grep "Infra" | cut -d " " -f5 > /home/nunukene/SSID3.txt""")
#SSIDStr = subprocess.check_output('nm-tool | grep "Infra" | cut -d " " -f5-6', shell=True)
#SSIDArray = str.split(SSIDStr)
ID = subprocess.check_output('ls', shell=True)
a='devilman'
print "Content-type:text/html\r\n\r\n"
print "<!DOCTYPE html>"
print "<html>"
print "<title> Not Hacking lol</title>"
print "<body>"
print "<h1> Join %s One of this networks <h1>" %(a)
print "</body>"
print "</html>"
I dont know how to get this process working before the rest!
I highly suggest using the logging module to help you diagnose where your problem is.
a reason your subprocess call didn't work is you would need to make a list out of all of the arguments to the command.
SSIDStr = subprocess.check_output(['nm-tool','|','grep','"Infra"','|','cut','-d','" "','-f5'])
(I'm not sure if you have to escape the double-quotes in this string)
using the subprocess call this way avoids using the text file and it's permission problems that the web server user may be experiencing writing files.
you were re-writing the "a" variable and you weren't using the text file in the output.
and I hope the cgitb.enable() line wasn't the problem. Haven't seen that before. Have you thought of using Flask?
If you are using python 3 then the print statements need to be functions.

Python 2.7 having multiple say commands on mac

I have tried many things to try to get text to speech to work in python 2.7 on a mac. I managed to write some simple codes using the system os such as:
from os import system
system('say Hello world')
This works alone:
from os import system
string2 = 'test'
string1 = 'hello world' + string2 + '.'
system("say %s" %(string1))
But if I do multiple say commands, like this:
system('say Please tell me your name.')
name = raw_input()
st = "Hello. Want pie" + name + "?"
system("say " + st)
I get this error after I enter my name:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
Am I currently making a mistake in concept, or does having two say commands not work? If having two say commands do not work this way, then how can I use text to speech multiple times in python 2.7 with macintosh?
Trying changing the format and see if you have any better luck.
system('say Please tell me your name.')
name = raw_input()
st = "Hello. Want pie" + name + "?"
system('say %s' %(st))

python 3 open terminal and run program

I made a small script in sublime that will extract commands from a json file that is on the user's computer and then it will open the terminal and run the settings/command. This works, except that it doesn't really open up the terminal. It only runs the command (and it works, as in my case it will run gcc to compile a simple C file), and pipes to STDOUT without opening up the terminal.
import json
import subprocess
import sublime_plugin
class CompilerCommand(sublime_plugin.TextCommand):
def get_dir(self, fullpath):
path = fullpath.split("\\")
path.pop()
path = "\\".join(path)
return path
def get_settings(self, path):
_settings_path = path + "\\compiler_settings.json"
return json.loads(open(_settings_path).read())
def run(self, edit):
_path = self.get_dir(self.view.file_name())
_settings = self.get_settings(_path)
_driver = _path.split("\\")[0]
_command = _driver + " && cd " + _path + " && " + _settings["compile"] + " && " + _settings["exec"]
proc = subprocess.Popen(_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
I'm not sure if using subprocess.Popen is the right way to go about it as I'm new to Python.
So to re-iterate; I want it to open up the terminal, run the command, and have the terminal stay open until the user presses ENTER or something. I'm running Windows 7 and Python 3, if that matters.
subprocess.Popen simply creates a subprocess with the given command. It is in no way related to opening a terminal window or any other windows for that matter.
You'll have to look into your platform specific UI automation solutions in order to achieve what you want. Or see if maybe the Sublime plugins mechanism can already do that.
NOTES:
Also, you should be using os.path.join/os.path.split/os.path.sep etc for your path operations—Sublime also runs on OS X for example, and OS X does not use backslashes. Also, file handles need to be closed, so use:
with open(...) as f:
return json.load(f) # also not that there is no nead to f.read()+json.loads()
# if you can just json.load() on the file handle
Furthermore, strings should usually be built using string interpolation:
_command = "{} && cd {} && {} && {}".format(_driver, _path, _settings["compile"], _settings["exec"])
...and, you should not be prefixing your local variables with _—it doesn't look nice and serves no purpose in Python either; and while we're at it, I might as well use the chance to recommend you to read PEP8: http://www.python.org/dev/peps/pep-0008/.

Categories

Resources