You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
talker/talker.py

47 lines
1.8 KiB

#programs importing this file as a library should load() the phenomes then use talk() to speak
import os
import sys
import requests
from pydub import AudioSegment
from pydub.playback import play
def talk(word, dict):
all = ["AA", "AE", "AH", "AO", "AW", "AY", "B", "CH", "D", "DH", "EH", "ER", "EY", "F", "G", "HH", "IH", "IY", "JH", "K", "L", "M", "N", "NG", "OW", "OY", "P", "R", "S", "SH", "T", "TH", "UH", "UW", "V", "W", "Y", "Z", "ZH"]
if not os.path.exists("phenome-samples"): os.mkdir("phenome-samples")
for phenome in all:
if not os.path.isfile("phenome-samples/"+phenome+".wav"):
print("missing phenome audio:", phenome)
new = []
for phenome in dict[word.upper()]:
if phenome[-1].isdigit(): new.append(phenome[0:-1])
else: new.append(phenome)
print("<", new)
word = AudioSegment.empty()
for p in new:
word += AudioSegment.from_file("phenome-samples/"+p+".wav")
play(word)
def load():
ps = {}
if not os.path.isfile("cmudict.txt"):
print("downloading phenomes...")
f = open("cmudict.txt", "w", encoding="utf-8")
f.write(requests.get("http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b").text)
f.close()
print("loading phenomes...")
f = open("cmudict.txt", "r", encoding="utf-8")
for line in f.read().split("\n"):
if line.startswith(";") or line == "": continue
sp1 = line.split(" ")
ps[sp1[0]] = sp1[1].split(" ")
print("succesfully loaded phenomes")
return ps
def main():
ps = load()
while True:
inp = input("> ")
try:
for word in inp.split(" "): talk(word, ps)
except:
print("word(s) not in phenome dictionary or error playing phenome audio")
if __name__ == "__main__":
main()