def test(self): url = "%s/list" % self.base_url log.debug("LEXICON URL: %s" % url) #print("LEXICON URL: %s" % url) try: r = requests.get(url) response = r.text response_json = json.loads(response) #print(response_json) exists = False #print(response_json) for lex in response_json: if lex['name'] == self.lexicon_name: exists = True if not exists: msg = "Lexicon does not exist: %s" % (self.lexicon_name) log.error(msg) raise LexiconException(msg) except json.JSONDecodeError: msg = "Unable to create lexicon client for %s. Response was: %s" % ( self.lexicon_name, response) log.error(msg) raise LexiconException(msg) except Exception as e: msg = "Unable to create lexicon client for %s at url %s. Reason: %s" % ( self.lexicon_name, url, e) log.warning(msg) raise LexiconException(msg)
def loadVoice(voice_config): try: v = Voice(voice_config) voices.append(v) except VoiceException as e: log.error("Failed to load voice from %s. Reason:\n%s" % (voice_config, e))
def mary2ws(maryxml, voice): (lang, maryxml) = dropHeader(maryxml) #lang = "sv" #log.debug(maryxml) root = ET.fromstring(maryxml.encode('utf-8')) #root = ET.fromstring(maryxml) paragraphs = [] utterance = { "lang": lang, "paragraphs": paragraphs } paragraph_elements = root.findall(".//p") for paragraph_element in paragraph_elements: sentences = [] paragraphs.append({"sentences": sentences}) sentence_elements = paragraph_element.findall("s") for sentence_element in sentence_elements: phrases = [] sentence = {"phrases": phrases} sentences.append(sentence) for sentence_child in sentence_element: if sentence_child.tag == "phrase": phrase_element = sentence_child phrase = buildPhrase(phrase_element, lang, voice) phrases.append(phrase) elif sentence_child.tag == "prosody": prosody_element = sentence_child #can 'prosody' only contain exactly one 'phrase'? phrase_element = prosody_element[0] phrase = buildPhrase(phrase_element, lang, voice) phrase = addIfExists(phrase, prosody_element, "pitch", prefix="prosody_") phrase = addIfExists(phrase, prosody_element, "range", prefix="prosody_") phrases.append(phrase) elif sentence_child.tag == "t": #This is a special case that happens (sometimes..) with single-word sentences #doesn't work, produces error in synthesis #TODO look at this again #It only happens with the word "Hon." ... phrase_element = ET.Element("phrase") phrase_element.append(sentence_child) phrase = buildPhrase(phrase_element, lang, voice) phrases.append(phrase) else: log.error("sentence child should not have tag %s" % sentence_child.tag) return utterance
def loadTextprocessor(tp_config): try: tp = Textprocessor(tp_config) textprocessors.append(tp) except TextprocessorException as e: log.error("Failed to load textprocessor from %s. Reason:\n%s" % (tp_config, e))
def test(self): url = "%s/list" % self.base_url log.debug("LEXICON URL: %s" % url) print("LEXICON URL: %s" % url) try: r = requests.get(url) response = r.text response_json = json.loads(response) print(response_json) exists = False print(response_json) for lex in response_json: if lex['name'] == self.lexicon_name: exists = True if not exists: msg = "Lexicon does not exist: %s" % (self.lexicon_name) log.error(msg) raise LexiconException(msg) except json.JSONDecodeError: msg = "Unable to create lexicon client for %s. Response was: %s" % (self.lexicon_name, response) log.error(msg) raise LexiconException(msg) except Exception as e: msg = "Unable to create lexicon client for %s at url %s. Reason: %s" % (self.lexicon_name, url, e) log.warning(msg) raise LexiconException(msg)
def test_config(): log.debug("\nTEST CONFIG\n") log.debug("Testing that audio_tmpdir exists and is writeable") try: tmpdir = config.config.get("Audio settings","audio_tmpdir") log.debug("TMPDIR: %s" % tmpdir) fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) tmpfile = fh.name fh.write("test".encode("utf-8")) fh.close() except: log.error("audio_tmpdir does not exist or is not writeable") raise log.debug("Testing to make sure that config file contains url to lexicon server:") try: assert ( config.config.has_option("Services", "lexicon") == True ) log.debug("Services|lexicon = %s" % config.config.get("Services", "lexicon")) log.debug("ok") except: log.error("Services|lexicon not found in config file\n") raise log.debug("\nEND TEST CONFIG\n")
def test_config(): log.debug("\nTEST CONFIG\n") log.debug("Testing that audio_tmpdir exists and is writeable") try: tmpdir = config.config.get("Audio settings", "audio_tmpdir") log.debug("TMPDIR: %s" % tmpdir) fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) tmpfile = fh.name fh.write("test".encode("utf-8")) fh.close() except: log.error("audio_tmpdir does not exist or is not writeable") raise log.debug( "Testing to make sure that config file contains url to lexicon server:" ) try: assert (config.config.has_option("Services", "lexicon") == True) log.debug("Services|lexicon = %s" % config.config.get("Services", "lexicon")) log.debug("ok") except: log.error("Services|lexicon not found in config file\n") raise log.debug("\nEND TEST CONFIG\n")
def mapperMapFromMary(trans, lang, voice): log.info("mapperMapFromMary( %s , %s , %s )" % (trans, lang, voice)) if "mapper" in voice: #Bad names.. It should be perhaps "external" and "internal" instead of "from" and "to" to_symbol_set = voice["mapper"]["from"] from_symbol_set = voice["mapper"]["to"] else: log.info("No marytts mapper defined for language %s" % lang) return trans ## hl remove quote_plus 20170613 ## hb "" b j A: rn . % 9 j doesn't work, testing to put quote back in 20170626 ##url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, quote_plus(trans)) url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, quote(trans)) ##url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, trans) log.debug("MAPPER URL before requests: %s" % url) r = requests.get(url) log.debug("MAPPER URL: "+r.url) response = r.text #log.debug("RESPONSE: %s" % response) try: response_json = json.loads(response) #log.debug("RESPONSE_JSON: %s" % response_json) new_trans = response_json["Result"] except: log.error("unable to map %s, from %s to %s. response was %s" % (trans, from_symbol_set, to_symbol_set, response)) raise #log.debug("NEW TRANS: %s" % new_trans) return new_trans
def buildPhrase(phrase_element, lang, voice): if phrase_element.tag != "phrase": log.error("wrong type of element: %s\n%s" % (phrase_element.tag, phrase_element)) sys.exit(1) tokens = [] phrase = {"tokens": tokens} for phrase_child in phrase_element: #log.debug("phrase_child: %s" % phrase_child.tag) #no 'mtu' if phrase_child.tag == "t": token_element = phrase_child words = [] token = { "words": words } tokens.append(token) #HB 171109 #This is the case with arabic words vocalised with mishkal if "orig" in token_element.attrib: orth = token_element.attrib["orig"] else: orth = token_element.text token["token_orth"] = orth word = buildWord(token_element, lang, voice) words.append(word) #END no 'mtu' #'mtu' elif phrase_child.tag == "mtu": mtu_element = phrase_child words = [] token = { "mtu": True, "words": words } tokens.append(token) token_orth = mtu_element.attrib["orig"] token["token_orth"] = token_orth token_elements = mtu_element.findall("t") for token_element in token_elements: word = buildWord(token_element, lang, voice) words.append(word) #END 'mtu' elif phrase_child.tag == "boundary": boundary = {} boundary = addIfExists(boundary, phrase_child, "breakindex") boundary = addIfExists(boundary, phrase_child, "tone") phrase["boundary"] = boundary else: log.warn("phrase child should not have tag %s" % phrase_child.tag) return phrase
def saveAndConvertAudio(audio_url): global config tmpdir = config.config.get("Audio settings","audio_tmpdir") log.debug("TMPDIR: %s" % tmpdir) if not os.path.isdir(tmpdir): os.system("mkdir -p %s" % tmpdir) fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) tmpwav = fh.name log.debug("audio_url:\n%s" % audio_url) r = requests.get(audio_url) log.debug(r.headers['content-type']) audio_data = r.content fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) tmpwav = fh.name fh.write(audio_data) fh.close() #tmpwav is now the synthesised wav file #tmpopus = "%s/%s.opus" % (tmpdir, tmpfilename) tmpopus = "%s.opus" % tmpwav convertcmd = "opusenc %s %s" % (tmpwav, tmpopus) log.debug("convertcmd: %s" % convertcmd) if log.log_level != "debug": convertcmd = "opusenc --quiet %s %s" % (tmpwav, tmpopus) retval = os.system(convertcmd) if retval != 0: log.error("ERROR: opusenc was not found. You should probably run something like\nsudo apt install opus-tools\n") #remove everything before the tmpdir, to build the external url #HB problem with wikimedia usage? #opus_url_suffix = re.sub("^.*/%s/" % tmpdir, "%s/" % tmpdir, tmpopus) opus_url_suffix = re.sub("^.*/%s/" % tmpdir, "", tmpopus) log.debug("opus_url_suffix: %s" % opus_url_suffix) #return tmpopus return_audio_data = True if return_audio_data: #audio_data = "data:audio/wav;base64,%s" % encode_audio(re.sub("^.*/", "wikispeech_server/tmp/", audio_file)).decode() audio_data = "%s" % encode_audio(tmpwav).decode() else: audio_data = "" return (opus_url_suffix, audio_data)
def saveAndConvertAudio(audio_url, presynth=False): global config log.debug("PRESYNTH: %s, type: %s" % (presynth, type(presynth))) tmpdir = config.config.get("Audio settings", "audio_tmpdir") log.debug("TMPDIR: %s" % tmpdir) fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) tmpwav = fh.name if presynth: fh.close() #The "url" is actually a filename at this point cmd = "mv %s %s" % (audio_url, tmpwav) log.debug(cmd) os.system(cmd) else: log.debug("audio_url:\n%s" % audio_url) r = requests.get(audio_url) log.debug(r.headers['content-type']) audio_data = r.content fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) tmpwav = fh.name fh.write(audio_data) fh.close() #tmpwav is now the synthesised wav file #tmpopus = "%s/%s.opus" % (tmpdir, tmpfilename) tmpopus = "%s.opus" % tmpwav convertcmd = "opusenc %s %s" % (tmpwav, tmpopus) log.debug("convertcmd: %s" % convertcmd) if log.log_level != "debug": convertcmd = convertcmd # +" &> /dev/null" retval = os.system(convertcmd) if retval != 0: log.error( "ERROR: opusenc was not found. You should probably run something like\nsudo apt install opus-tools\n" ) #remove everything before the tmpdir, to build the external url #HB problem with wikimedia usage? #opus_url_suffix = re.sub("^.*/%s/" % tmpdir, "%s/" % tmpdir, tmpopus) opus_url_suffix = re.sub("^.*/%s/" % tmpdir, "", tmpopus) log.debug("opus_url_suffix: %s" % opus_url_suffix) #return tmpopus return opus_url_suffix
def buildPhrase(phrase_element, lang, voice): if phrase_element.tag != "phrase": log.error("wrong type of element: %s\n%s" % (phrase_element.tag, phrase_element)) sys.exit(1) tokens = [] phrase = {"tokens": tokens} for phrase_child in phrase_element: #log.debug("phrase_child: %s" % phrase_child.tag) #no 'mtu' if phrase_child.tag == "t": token_element = phrase_child words = [] token = {"words": words} tokens.append(token) #HB 171109 #This is the case with arabic words vocalised with mishkal if "orig" in token_element.attrib: orth = token_element.attrib["orig"] else: orth = token_element.text token["token_orth"] = orth word = buildWord(token_element, lang, voice) words.append(word) #END no 'mtu' #'mtu' elif phrase_child.tag == "mtu": mtu_element = phrase_child words = [] token = {"mtu": True, "words": words} tokens.append(token) token_orth = mtu_element.attrib["orig"] token["token_orth"] = token_orth token_elements = mtu_element.findall("t") for token_element in token_elements: word = buildWord(token_element, lang, voice) words.append(word) #END 'mtu' elif phrase_child.tag == "boundary": boundary = {} boundary = addIfExists(boundary, phrase_child, "breakindex") boundary = addIfExists(boundary, phrase_child, "tone") phrase["boundary"] = boundary else: log.warn("phrase child should not have tag %s" % phrase_child.tag) return phrase
def saveAndConvertAudio(audio_url,presynth=False): global config log.debug("PRESYNTH: %s, type: %s" % (presynth, type(presynth)) ) tmpdir = config.config.get("Audio settings","audio_tmpdir") log.debug("TMPDIR: %s" % tmpdir) fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) tmpwav = fh.name if presynth: fh.close() #The "url" is actually a filename at this point cmd = "mv %s %s" % (audio_url, tmpwav) log.debug(cmd) os.system(cmd) else: log.debug("audio_url:\n%s" % audio_url) r = requests.get(audio_url) log.debug(r.headers['content-type']) audio_data = r.content fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) tmpwav = fh.name fh.write(audio_data) fh.close() #tmpwav is now the synthesised wav file #tmpopus = "%s/%s.opus" % (tmpdir, tmpfilename) tmpopus = "%s.opus" % tmpwav convertcmd = "opusenc %s %s" % (tmpwav, tmpopus) log.debug("convertcmd: %s" % convertcmd) if log.log_level != "debug": convertcmd = "opusenc --quiet %s %s" % (tmpwav, tmpopus) retval = os.system(convertcmd) if retval != 0: log.error("ERROR: opusenc was not found. You should probably run something like\nsudo apt install opus-tools\n") #remove everything before the tmpdir, to build the external url #HB problem with wikimedia usage? #opus_url_suffix = re.sub("^.*/%s/" % tmpdir, "%s/" % tmpdir, tmpopus) opus_url_suffix = re.sub("^.*/%s/" % tmpdir, "", tmpopus) log.debug("opus_url_suffix: %s" % opus_url_suffix) #return tmpopus return opus_url_suffix
def testVoice(config): voice = config["espeak_voice"] espeak = "espeak-ng" #espeak = "espeak" try: retval = os.system("%s -v %s -q test" % (espeak, voice)) assert retval == 0 log.debug("Test successful for voice %s" % config["name"]) return True except: msg = "Failed command: '%s -v %s'" % (espeak, voice) log.error(msg) raise VoiceException(msg)
def map(self, string): url = "%s/%s/%s/%s/%s" % (self.base_url, "map", self.from_symbol_set, self.to_symbol_set, string) r = requests.get(url) log.debug(r.url) response = r.text try: response_json = json.loads(response) new_string = response_json["Result"] return new_string except: log.error("unable to map string '%s'from %s to %s. response was %s" % (string,self.from_symbol_set, self.to_symbol_set, response)) raise MapperException
def saveAndConvertAudio(audio_url): global config tmpdir = config.config.get("Audio settings", "audio_tmpdir") log.debug("TMPDIR: %s" % tmpdir) if not os.path.isdir(tmpdir): os.system("mkdir -p %s" % tmpdir) fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) tmpwav = fh.name log.debug("audio_url:\n%s" % audio_url) r = requests.get(audio_url) log.debug(r.headers['content-type']) audio_data = r.content #fh = NamedTemporaryFile(mode='w+b', dir=tmpdir, delete=False) #tmpwav = fh.name fh.write(audio_data) fh.close() tmpopus = "%s.opus" % tmpwav convertcmd = "opusenc %s %s" % (tmpwav, tmpopus) log.debug("convertcmd: %s" % convertcmd) if log.log_level != "debug": convertcmd = "opusenc --quiet %s %s" % (tmpwav, tmpopus) retval = os.system(convertcmd) if retval != 0: log.error( "ERROR: opusenc was not found. You should probably run something like\nsudo apt install opus-tools\n" ) opus_url_suffix = re.sub("^.*/%s/" % tmpdir, "", tmpopus) log.debug("opus_url_suffix: %s" % opus_url_suffix) return_audio_data = True if return_audio_data: audio_data = "%s" % encode_audio(tmpopus).decode() else: audio_data = "" #Removing any remaining files in the tmpdir tmpfiles = glob.glob("%s/*" % tmpdir) for f in tmpfiles: os.unlink(f) return (opus_url_suffix, audio_data)
def buildWord(token_element, lang, voice): if token_element.tag != "t": log.error("wrong type of element: %s\n%s" % (token_element.tag, token_element)) sys.exit(1) orth = token_element.text word = { "orth": orth } word = addIfExists(word, token_element, "accent") #g2p_method can be rules, lexicon, or not there if there is sampa in input #change to a tag for input_ssml_transcription that can be used by the lexicon_client if token_element.attrib["pos"] not in ["$PUNCT","."] and "g2p_method" not in token_element.attrib: word["input_ssml_transcription"] = True word = addIfExists(word, token_element, "pos") #word = addIfExists(word, token_element, "ph") if "ph" in token_element.attrib: mary_trans = token_element.attrib.get("ph") ws_trans = mapperMapFromMary(mary_trans, lang, voice) word["trans"] = ws_trans #In the new version with output_type INTONATION from marytts the following is no longer used syllable_elements = token_element.findall("syllable") if len(syllable_elements) > 0: syllables = [] word["syllables"] = syllables for syllable_element in syllable_elements: phonemes = [] syllable = { "phonemes": phonemes } syllable = addIfExists(syllable, syllable_element, "accent") syllable = addIfExists(syllable, syllable_element, "ph") syllable = addIfExists(syllable, syllable_element, "stress") syllables.append(syllable) phoneme_elements = syllable_element.findall("ph") for phoneme_element in phoneme_elements: symbol = phoneme_element.attrib["p"] phonemes.append({"symbol": symbol}) return word
def test(self): url = "%s/%s/%s/%s" % (self.base_url, "maptable", self.from_symbol_set, self.to_symbol_set) log.debug(url) try: r = requests.get(url) response = r.text response_json = json.loads(response) except json.JSONDecodeError: msg = "Unable to create mapper from %s to %s. Response was: %s" % (self.from_symbol_set, self.to_symbol_set, response) log.error(msg) raise MapperException(msg) except Exception as e: msg = "Unable to create mapper at url %s. Reason: %s" % (url, e) log.error(msg) raise MapperException(msg)
def map(self, string): url = "%s/%s/%s/%s/%s" % (self.base_url, "map", self.from_symbol_set, self.to_symbol_set, string) r = requests.get(url) log.debug(r.url) response = r.text try: response_json = json.loads(response) new_string = response_json["Result"] return new_string except: log.error( "unable to map string '%s'from %s to %s. response was %s" % (string, self.from_symbol_set, self.to_symbol_set, response)) raise MapperException
def mapperMapToMary(trans, lang, voice): log.debug("mapperMapToMary( %s, %s, %s)" % (trans, lang, voice)) if "mapper" in voice: to_symbol_set = voice["mapper"]["to"] from_symbol_set = voice["mapper"]["from"] log.info("marytts mapper defined for language %s\nFrom: %s\nTo: %s" % (lang, from_symbol_set, to_symbol_set)) else: log.info("No marytts mapper defined for language %s" % lang) return trans ## hl remove quote_plus 20170613 ## hb "" b j A: rn . % 9 j doesn't work, using quote 20170626 ## url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, quote_plus(trans)) url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, quote(trans)) ##url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, trans) log.debug("MAPPER URL before requests: %s" % url) r = requests.get(url) log.debug("MAPPER URL: %s" % r.url) response = r.text log.debug("MAPPER RESPONSE: %s" % response) try: response_json = json.loads(response) except json.JSONDecodeError: log.error("JSONDecodeError:") log.error("RESPONSE: %s" % response) raise new_trans = response_json["Result"] #Special cases for Swedish pre-r allophones that are not handled by the mapper (because mary uses an old version of the phoneme set that desn't distinguish between normal and r-coloured E/{ (always E) and 2/9 (always 9). This should change in mary later on. if lang == "sv": new_trans = re.sub("{ - ",r"E - ", new_trans) new_trans = re.sub("{ ",r"E ", new_trans) new_trans = re.sub("2(:? -) r? ",r"9\1 r", new_trans) log.debug("NEW TRANS: %s" % new_trans) return new_trans
def mapperMapToMary(trans, lang, voice): log.debug("mapperMapToMary( %s, %s, %s)" % (trans, lang, voice)) if "mapper" in voice: to_symbol_set = voice["mapper"]["to"] from_symbol_set = voice["mapper"]["from"] log.debug("marytts mapper defined for language %s\nFrom: %s\nTo: %s" % (lang, from_symbol_set, to_symbol_set)) else: log.debug("No marytts mapper defined for language %s" % lang) return trans ## hl remove quote_plus 20170613 ## hb "" b j A: rn . % 9 j doesn't work, using quote 20170626 ## url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, quote_plus(trans)) url = mapper_url + "/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, quote(trans)) ##url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, trans) log.debug("MAPPER URL before requests: %s" % url) r = requests.get(url) log.debug("MAPPER URL: %s" % r.url) response = r.text log.debug("MAPPER RESPONSE: %s" % response) try: response_json = json.loads(response) except json.JSONDecodeError: log.error("JSONDecodeError:") log.error("RESPONSE: %s" % response) raise new_trans = response_json["Result"] #Special cases for Swedish pre-r allophones that are not handled by the mapper (because mary uses an old version of the phoneme set that desn't distinguish between normal and r-coloured E/{ (always E) and 2/9 (always 9). This should change in mary later on. if lang == "sv": new_trans = re.sub("{ - ", r"E - ", new_trans) new_trans = re.sub("{ ", r"E ", new_trans) new_trans = re.sub("2(:? -) r? ", r"9\1 r", new_trans) log.debug("NEW TRANS: %s" % new_trans) return new_trans
def test(self): url = "%s/%s/%s/%s" % (self.base_url, "maptable", self.from_symbol_set, self.to_symbol_set) log.debug(url) try: r = requests.get(url) response = r.text response_json = json.loads(response) except json.JSONDecodeError: msg = "Unable to create mapper from %s to %s. Response was: %s" % ( self.from_symbol_set, self.to_symbol_set, response) log.error(msg) raise MapperException(msg) except Exception as e: msg = "Unable to create mapper at url %s. Reason: %s" % (url, e) log.error(msg) raise MapperException(msg)
def buildWord(token_element, lang, voice): if token_element.tag != "t": log.error("wrong type of element: %s\n%s" % (token_element.tag, token_element)) sys.exit(1) orth = token_element.text word = {"orth": orth} word = addIfExists(word, token_element, "accent") #g2p_method can be rules, lexicon, or not there if there is sampa in input #change to a tag for input_ssml_transcription that can be used by the lexicon_client if token_element.attrib["pos"] not in [ "$PUNCT", "." ] and "g2p_method" not in token_element.attrib: word["input_ssml_transcription"] = True word = addIfExists(word, token_element, "pos") #word = addIfExists(word, token_element, "ph") if "ph" in token_element.attrib: mary_trans = token_element.attrib.get("ph") ws_trans = mapperMapFromMary(mary_trans, lang, voice) word["trans"] = ws_trans #In the new version with output_type INTONATION from marytts the following is no longer used syllable_elements = token_element.findall("syllable") if len(syllable_elements) > 0: syllables = [] word["syllables"] = syllables for syllable_element in syllable_elements: phonemes = [] syllable = {"phonemes": phonemes} syllable = addIfExists(syllable, syllable_element, "accent") syllable = addIfExists(syllable, syllable_element, "ph") syllable = addIfExists(syllable, syllable_element, "stress") syllables.append(syllable) phoneme_elements = syllable_element.findall("ph") for phoneme_element in phoneme_elements: symbol = phoneme_element.attrib["p"] phonemes.append({"symbol": symbol}) return word
def lookup(self, string): if string.strip() == "": log.warning("LEXICON LOOKUP STRING IS EMPTY!") return {} encString = urllib.parse.quote(string) url = "%s/%s?lexicons=%s&words=%s" % (self.base_url, "lookup", self.lexicon_name, encString) r = requests.get(url) log.debug("LEXICON LOOKUP URL: %s" % r.url) response = r.text try: response_json = json.loads(response) log.debug(response_json) return response_json except: log.error("unable to lookup '%s' in %s. response was %s" % (string, self.lexicon_name, response)) raise LexiconException(response)
def lookup(self, string): if string.strip() == "": log.warning("LEXICON LOOKUP STRING IS EMPTY!") return {} encString = urllib.parse.quote(string) url = "%s/%s?lexicons=%s&words=%s" % (self.base_url, "lookup", self.lexicon_name, encString) r = requests.get(url) log.debug("LEXICON LOOKUP URL: %s" % r.url) response = r.text try: response_json = json.loads(response) log.debug(response_json) return response_json except: log.error("unable to lookup '%s' in %s. response was %s" % (string, self.lexicon_name, response)) raise LexiconException(response)
def testVoice(voice_config): #from wikispeech_server.adapters.ahotts_adapter import socket_write_filelength_file,socket_read_filelength_file #cwdir = os.getcwd() #tmpdir = config.config.get("Audio settings","audio_tmpdir") #ahotts_server_ip = config.config.get("Services", "ahotts_server_ip") #ahotts_server_port = config.config.get("Services", "ahotts_server_port") #ahotts_speed = config.config.get("Services", "ahotts_speed") lang = voice_config["lang"] name = voice_config["name"] try: response = requests.post("http://" + ahotts_server_ip + ":" + ahotts_server_port + "/ahotts_getaudio", data={ 'text': "Hasierako proba".encode('latin-1'), 'lang': lang, 'voice': name, 'speed': ahotts_speed }) except: msg = "Ahotts server not found at %s:%s" % (ahotts_server_ip, ahotts_server_port) log.error(msg) raise VoiceException(msg) if response.status_code == 200: files = response.json() wavfile = files['wav'] wrdfile = files['wrd'] response2 = requests.get("http://" + ahotts_server_ip + ":" + ahotts_server_port + "/ahotts_downloadfile?file=" + wavfile) if response2.status_code == 200: response3 = requests.get("http://" + ahotts_server_ip + ":" + ahotts_server_port + "/ahotts_downloadfile?file=" + wrdfile) if response3.status_code != 200: msg = "AhoTTS server error" log.error(msg) raise VoiceException(msg) else: msg = "AhoTTS server error" log.error(msg) raise VoiceException(msg) else: msg = "AhoTTS server error" log.error(msg) raise VoiceException(msg)
def testVoice(self): log.info("Testing voice %s" % self.name) if self.engine == "marytts": voice_host = config.config.get("Services", "marytts") url = re.sub("process","voices",voice_host) log.debug("Calling url: %s" % url) try: r = requests.get(url) except: msg = "Marytts server not found at url %s" % (url) log.error(msg) raise VoiceException(msg) response = r.text log.debug("Response:\n%s" % response) marytts_voicenames = self.getMaryttsVoicenames(response) if not self.name in marytts_voicenames: msg = "Voice %s not found at url %s" % (self.name, url) log.error(msg) raise VoiceException(msg) else: log.info("Voice found at url %s" % url)
def testVoice(voice_config): voice_host = config.config.get("Services", "marytts") url = re.sub("process", "voices", voice_host) name = voice_config["name"] log.debug("Calling url: %s" % url) try: r = requests.get(url) except: msg = "Marytts server not found at url %s" % (url) log.error(msg) raise VoiceException(msg) response = r.text log.debug("Response:\n%s" % response) marytts_voicenames = getMaryttsVoicenames(response) log.debug("marytts_voicenames: %s" % marytts_voicenames) if not name in marytts_voicenames: msg = "Voice %s not found at url %s" % (name, url) log.error(msg) raise VoiceException(msg) else: log.info("Voice found at url %s" % url)
def mapperMapFromMary(trans, lang, voice): log.debug("mapperMapFromMary( %s , %s , %s )" % (trans, lang, voice)) if "mapper" in voice: #Bad names.. It should be perhaps "external" and "internal" instead of "from" and "to" to_symbol_set = voice["mapper"]["from"] from_symbol_set = voice["mapper"]["to"] else: log.debug("No marytts mapper defined for language %s" % lang) return trans ## hl remove quote_plus 20170613 ## hb "" b j A: rn . % 9 j doesn't work, testing to put quote back in 20170626 ##url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, quote_plus(trans)) url = mapper_url + "/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, quote(trans)) ##url = mapper_url+"/mapper/map/%s/%s/%s" % (from_symbol_set, to_symbol_set, trans) log.debug("MAPPER URL before requests: %s" % url) r = requests.get(url) log.debug("MAPPER URL: " + r.url) response = r.text #log.debug("RESPONSE: %s" % response) try: response_json = json.loads(response) #log.debug("RESPONSE_JSON: %s" % response_json) new_trans = response_json["Result"] except: log.error("unable to map %s, from %s to %s. response was %s" % (trans, from_symbol_set, to_symbol_set, response)) raise #log.debug("NEW TRANS: %s" % new_trans) return new_trans
def testVoice(self): log.info("Testing voice %s" % self.name) if self.engine == "marytts": voice_host = config.config.get("Services", "marytts") url = re.sub("process","voices",voice_host) log.debug("Calling url: %s" % url) try: r = requests.get(url) except: msg = "Marytts server not found at url %s" % (url) log.error(msg) raise VoiceException(msg) response = r.text log.debug("Response:\n%s" % response) marytts_voicenames = self.getMaryttsVoicenames(response) if not self.name in marytts_voicenames: msg = "Voice %s not found at url %s" % (self.name, url) log.error(msg) raise VoiceException(msg) else: log.info("Voice found at url %s" % url) elif self.engine == "ahotts": cwdir = os.getcwd() tmpdir = config.config.get("Audio settings","audio_tmpdir") ahotts_dir = config.config.get("Services", "ahotts_dir") ahotts_server_ip = config.config.get("Services", "ahotts_server_ip") ahotts_server_port = config.config.get("Services", "ahotts_server_port") ahotts_command = "cd %s/bin; echo \"Hasierako proba\" > ahotts_test.txt; ./tts_client -IP=%s -Port=%s -InputFile=ahotts_test.txt -OutputFile=ahotts_test.wav ; mv ahotts_test.wav %s/%s/ahotts_test.wav ; rm ahotts_test.txt" % (ahotts_dir, ahotts_server_ip, ahotts_server_port, cwdir, tmpdir) os.system(ahotts_command) try: wavfile=open('%s/%s/ahotts_test.wav'%(cwdir, tmpdir),'r') wavfile.close() os.remove('%s/%s/ahotts_test.wav'%(cwdir, tmpdir)) except: msg = "AhoTTS server not found at IP %s and Port %s" % (ahotts_server_ip,ahotts_server_port) log.error(msg) raise VoiceException(msg)
if __name__ == "__main__": log.log_level = "debug" #debug, info, warning, error tp_config = { "name":"wikitextproc_sv", "lang":"sv", "components":[ { "module":"adapters.marytts_adapter", "call":"marytts_preproc", "mapper": { "from":"sv-se_ws-sampa", "to":"sv-se_sampa_mary" }, }, { "module":"adapters.lexicon_client", "call":"lexLookup", "lexicon":"wikispeech_testdb:sv" } ] } try: tp = Textprocessor(tp_config) log.info("Created textprocessor %s from %s" % (tp, tp_config)) except TextprocessorException as e: log.error("Failed to create textprocessor for %s\nException message was:\n%s" % (tp_config, e))
def synthesise(lang, voice, utterance, hostname=None): log.debug("Utterance: %s" % utterance) input = utterance['original_text'] log.debug("Text: %s" % input) words = get_orth(utterance) log.debug("Words: %s" % words) hashstring = input + '&Lang=' + lang + '&Voice=' + voice['name'] try: hash_object = hashlib.md5(hashstring.encode('latin-1')) except: try: hash_object = hashlib.md5(hashstring.encode('utf-8')) except: hash_object = hashlib.md5(hashstring.encode()) hashnumber = hash_object.hexdigest() """ Call to tts_client, only works if ahotts is installed in same server as wikispeech-server. Better to make socket calls over the network. inputfile=open("%s/bin/tts_%s.txt" % (ahotts_dir, hashnumber),"wb") inputfile.write(input.encode('latin-1')+'\n'.encode('latin-1')) inputfile.close() ahotts_command = "cd %s/bin ; ./tts_client -SetDur=y -Speed=%s -IP=%s -Port=%s -InputFile=tts_%s.txt -OutputFile=tts_%s.wav -WordFile=tts_%s.wrd -PhoFile=tts_%s.pho ; mv tts_%s.wav %s/%s/tts_%s.wav ; rm tts_%s.txt" % (ahotts_dir, ahotts_speed, ahotts_server_ip, ahotts_server_port, hashnumber, hashnumber, hashnumber, hashnumber, hashnumber, cwdir, tmpdir, hashnumber, hashnumber) log.info("Ahotts command: %s" % ahotts_command) os.system(ahotts_command) audio_url = "%s%s/%s" % (hostname, "audio",'tts_%s.wav' % hashnumber) words_times_file=open(ahotts_dir+'/tts_'+hashnumber+'.wrd','r') words_times=words_times_file.readlines() words_times_file.close() os.remove(ahotts_dir+'/tts_'+hashnumber+'.wrd') os.remove(ahotts_dir+'/tts_'+hashnumber+'.pho') log.info(str(words)) audio_url = "%s%s/%s" % (hostname, "audio",'tts_%s.wav' % hashnumber) words_times_file=open(wrdfilename,'r') words_times=words_times_file.readlines() words_times_file.close() os.remove(inputfilename) os.remove(wrdfilename) """ """ Call to socket, does not work properly in docker compose environment # Write text to file inputfilename="%s/%s/tts_%s.txt" % (cwdir,tmpdir,hashnumber) inputfile=open(inputfilename,"wb") inputfile.write(input.encode('latin-1')+'\n'.encode('latin-1')) inputfile.close() # Open socket socketa=socket.socket(socket.AF_INET,socket.SOCK_STREAM) ipa=socket.gethostbyname(ahotts_server_ip) socketa.connect((ipa,int(ahotts_server_port))) # Write options options_struct=struct.Struct('4s 4s 4s 1024s 1024s 1024s ?') options_struct_packed=options_struct.pack(lang.encode('utf-8'),"".encode('utf-8'),ahotts_speed.encode('utf-8'),"".encode('utf-8'),("wav/tts_%s.pho" % hashnumber).encode('utf-8'),("wav/tts_%s.wrd" % hashnumber).encode('utf-8'),True) totalsent=0 while totalsent<len(options_struct_packed): sent=socketa.send(options_struct_packed[totalsent:]) if sent==0: raise RuntimeError("socket connection broken") totalsent=totalsent+sent # Write text file length + text file socket_write_filelength_file(socketa,inputfilename) # Read wav file wavfilename="%s/%s/tts_%s.wav" % (cwdir,tmpdir,hashnumber) wavfile=open(wavfilename,"wb") wavfile.write(socket_read_filelength_file(socketa)) wavfile.close() # Read pho file socket_read_filelength_file(socketa) # Read wrd file wrdfilename="%s/%s/tts_%s.wrd" % (cwdir,tmpdir,hashnumber) wrdfile=open(wrdfilename,"wb") wrdfile.write(socket_read_filelength_file(socketa)) wrdfile.close() # Close socket socketa.close() audio_url = "%s%s/%s" % (hostname, "audio",'tts_%s.wav' % hashnumber) words_times_file=open(wrdfilename,'r') words_times=words_times_file.readlines() words_times_file.close() os.remove(inputfilename) os.remove(wrdfilename) """ response = requests.post("http://" + ahotts_server_ip + ":" + ahotts_server_port + "/ahotts_getaudio", data={ 'text': input.encode('latin-1') + '\n'.encode('latin-1'), 'lang': lang, 'voice': voice, 'speed': ahotts_speed }) url = "http://" + ahotts_server_ip + ":" + ahotts_server_port + "/ahotts_getaudio" data = { 'text': input.encode('latin-1') + '\n'.encode('latin-1'), 'lang': lang, 'voice': voice, 'speed': ahotts_speed } if response.status_code == 200: files = response.json() wavfile = files['wav'] wrdfile = files['wrd'] response2 = requests.get("http://" + ahotts_server_ip + ":" + ahotts_server_port + "/ahotts_downloadfile?file=" + wavfile) if response2.status_code == 200: wavfilename = "%s/%s/tts_%s.wav" % (cwdir, tmpdir, hashnumber) wavfile = open(wavfilename, "wb") for chunk in response2.iter_content(1024): wavfile.write(chunk) wavfile.close() response3 = requests.get("http://" + ahotts_server_ip + ":" + ahotts_server_port + "/ahotts_downloadfile?file=" + wrdfile) if response3.status_code == 200: wrdfilename = "%s/%s/tts_%s.wrd" % (cwdir, tmpdir, hashnumber) wrdfile = open(wrdfilename, "wb") for chunk in response3.iter_content(1024): wrdfile.write(chunk) wrdfile.close() else: msg = "AhoTTS server error" log.error(msg) raise VoiceException(msg) else: msg = "AhoTTS server error" log.error(msg) raise VoiceException(msg) else: msg = "AhoTTS server error" log.error(msg) raise VoiceException(msg) audio_url = "%s%s/%s" % (hostname, "audio", 'tts_%s.wav' % hashnumber) words_times_file = open(wrdfilename, 'r') words_times = words_times_file.readlines() words_times_file.close() os.remove(wrdfilename) words_times = list(map(lambda x: x[:-1].split(' ')[1], words_times)) tokens = [] starttime = 0.0 lastendtime = 0.0 for word_ind in range(len(words)): word = words[word_ind] if word_ind > len(words_times) - 1: endtime = lastendtime else: endtime = float(words_times[word_ind]) / 1000 tokens.append({ "orth": word, "starttime": starttime, "endtime": endtime }) starttime = endtime lastendtime = endtime return (audio_url, tokens)
def maryxml2tokensET(maryxmlstring): try: root = ET.fromstring(maryxmlstring) except: log.error("ERROR IN PARSING XML:\n%s" % maryxmlstring) #raise with no argument re-raises the last exception raise #root = doc.getroot() lang = root.attrib['{http://www.w3.org/XML/1998/namespace}lang'] #lang = utt2["maryxml"]["@xml:lang"] ps = root.findall(".//{http://mary.dfki.de/2002/MaryXML}p") utt = [] output_tokens = [] endtime = 0 for p in ps: ss = p.findall(".//{http://mary.dfki.de/2002/MaryXML}s") paragraph = [] utt.append(paragraph) for s in ss: sentence = [] paragraph.append(sentence) #log.debug "S:",s phrases = s.findall(".//{http://mary.dfki.de/2002/MaryXML}phrase") for phrase in phrases: #log.debug "PHRASE:", phrase for child in phrase: #log.debug "CHILD:", child.tag expanded = None if child.tag == "{http://mary.dfki.de/2002/MaryXML}t": #orth = child.text.strip() orth = "".join(child.itertext()).strip() #log.debug "ORTH:", orth tokendur = 0 for ph in child.findall( ".//{http://mary.dfki.de/2002/MaryXML}ph"): #log.debug ph.attrib #tokendur += ph.attrib['{http://mary.dfki.de/2002/MaryXML}d'] tokendur += int(ph.attrib['d']) endtime += tokendur #endtime_seconds = endtime/1000.0 #token = (orth,endtime_seconds) elif child.tag == "{http://mary.dfki.de/2002/MaryXML}mtu": log.debug("Found mtu token") log.debug(child.attrib) #The expanded words of the mtu expanded = "".join(child.itertext()).strip() log.debug("expanded: %s" % expanded) #The original orthography #TODO return both orth = child.attrib["orig"] log.debug("orig orth: %s" % orth) tokendur = 0 for ph in child.findall( ".//{http://mary.dfki.de/2002/MaryXML}ph"): #log.debug ph.attrib #tokendur += ph.attrib['{http://mary.dfki.de/2002/MaryXML}d'] tokendur += int(ph.attrib['d']) endtime += tokendur #endtime_seconds = endtime/1000.0 #token = (orth,endtime_seconds) elif child.tag == "{http://mary.dfki.de/2002/MaryXML}boundary": orth = "PAUSE" orth = "" #log.debug "ORTH:", orth #log.debug child.attrib #endtime += child.attrib['{http://mary.dfki.de/2002/MaryXML}duration'] if "duration" in child.attrib: endtime += int(child.attrib['duration']) #endtime_seconds = endtime/1000.0 #token = (orth,endtime_seconds) orth = re.sub("\s+", " ", orth) endtime_seconds = endtime / 1000.0 token = {"orth": orth, "endtime": endtime_seconds} if expanded: expanded = re.sub("\s+", " ", expanded) token["expanded"] = expanded sentence.append(token) output_tokens.append(token) #log.debug utt #return (output_tokens, lang) return output_tokens
def test_wikispeech(): sent = "apa" trans = {} trans["apa"] = '" A: - p a' lang = "sv" try: tmp = textproc(lang,"default_textprocessor", sent) res = synthesise(lang,"default_voice",tmp,"markup","json") except FileNotFoundError: log.error("Failed to do wikispeech test.\nError type: %s\nError info:%s" % (sys.exc_info()[0], sys.exc_info()[1])) import traceback log.debug("Stacktrace:") traceback.print_tb(sys.exc_info()[2]) log.debug("END stacktrace") log.error("wikispeech test failure") log.error("Is the audio_tmpdir %s correctly configured?" % config.config.get("Audio settings", "audio_tmpdir")) raise except: log.error("Failed to do wikispeech test.\nError type: %s\nError info:%s" % (sys.exc_info()[0], sys.exc_info()[1])) import traceback log.debug("Stacktrace:") traceback.print_tb(sys.exc_info()[2]) log.debug("END stacktrace") log.error("wikispeech test failure") log.error("No running marytts server found at %s" % config.config.get("Services","marytts")) raise #TODO Better with exception than return value if type(res) == type("") and res.startswith("No voice available"): log.error("Failed to do wikispeech test") log.error(res) log.error("wikispeech test failure") raise log.debug("%s --> %s" % (sent,res)) log.debug("SUCCESS: wikispeech test")
def test_textproc(): sent = "apa" try: res = textproc("sv","default_textprocessor", sent) except: log.error("Failed to do textprocessing.\nError type: %s\nError info:%s" % (sys.exc_info()[0], sys.exc_info()[1])) import traceback log.debug("Stacktrace:") traceback.print_tb(sys.exc_info()[2]) log.debug("END stacktrace") log.error("textprocessing test failure") log.error("No running marytts server found at %s" % config.config.get("Services","marytts")) raise #TODO Better with exception than return value if type(res) == type("") and res.startswith("ERROR:"): log.error("Failed to do textprocessing") log.error(res) log.error("textprocessing test failure") raise log.debug("%s --> %s" % (sent,res)) log.debug("SUCCESS: textprocessing test")
def test_lexicon_client(): lexicon = "wikispeech_testdb:sv" sent = "apa hund färöarna" trans = {} trans["apa"] = '"" A: . p a' trans["hund"] = '" h u0 n d' trans["färöarna"] = '"" f {: . % r 2: . a . rn a' try: lexicon_client.loadLexicon(lexicon) lex = lexicon_client.getLookupBySentence(sent, lexicon) log.debug("LEX: %s" % lex) except: log.error("Failed to do lexicon lookup.\nError type: %s\nError info:%s" % (sys.exc_info()[0], sys.exc_info()[1])) import traceback log.debug("Stacktrace:") if log.log_level == "debug": traceback.print_tb(sys.exc_info()[2]) log.debug("END stacktrace") log.error("lexicon lookup test failure") log.error("No running lexserver found at %s" % config.config.get("Services","lexicon")) raise for word in sent.split(" "): try: if lex[word] != trans[word]: log.error("lexicon lookup test failure") log.error("word %s, found %s, expected %s" % (word, lex[word], trans[word])) raise except KeyError: log.error("Lexicon lookup test failure: Word %s not found in lexicon %s" % (word, lexicon)) raise log.debug("SUCCESS: lexicon lookup test")
import wikispeech_server.log as log from wikispeech_server.textprocessor import Textprocessor, TextprocessorException from wikispeech_server.voice import Voice, VoiceException ################# # # Test opusenc before anything else # ################ log.debug("\nOPUSENC\n\nChecking that opusenc is installed on your system..") retval = os.system("opusenc -V") if retval != 0: os.system("opusenc -V") log.error( "ERROR: opusenc was not found. You should probably run something like\nsudo apt install opus-tools\n" ) sys.exit(1) else: log.debug("opusenc found.\n\nEND OPUSENC\n") ############### # # Load textprocessors and voices # ############### def loadTextprocessor(tp_config): try: tp = Textprocessor(tp_config)
import pytz from pytz import timezone import subprocess ################# # # Test opusenc before anything else # ################ log.info("\nOPUSENC\n\nChecking that opusenc is installed on your system..") retval = os.system("opusenc -V") if retval != 0: os.system("opusenc -V") log.error("ERROR: opusenc was not found. You should probably run something like\nsudo apt install opus-tools\n") sys.exit(1) else: log.info("opusenc found.\n\nEND OPUSENC\n") ############### # # Load textprocessors and voices # ############### textprocessors = [] def loadTextprocessor(tp_config): try:
def maryxml2tokensET(maryxmlstring): try: root = ET.fromstring(maryxmlstring) except: log.error("ERROR IN PARSING XML:\n%s" % maryxmlstring) #raise with no argument re-raises the last exception raise #root = doc.getroot() lang = root.attrib['{http://www.w3.org/XML/1998/namespace}lang'] #lang = utt2["maryxml"]["@xml:lang"] ps = root.findall(".//{http://mary.dfki.de/2002/MaryXML}p") utt = [] output_tokens = [] endtime = 0 for p in ps: ss = p.findall(".//{http://mary.dfki.de/2002/MaryXML}s") paragraph = [] utt.append(paragraph) for s in ss: sentence = [] paragraph.append(sentence) #log.debug "S:",s phrases = s.findall(".//{http://mary.dfki.de/2002/MaryXML}phrase") for phrase in phrases: #log.debug "PHRASE:", phrase for child in phrase: #log.debug "CHILD:", child.tag expanded = None if child.tag == "{http://mary.dfki.de/2002/MaryXML}t": #orth = child.text.strip() orth = "".join(child.itertext()).strip() #log.debug "ORTH:", orth tokendur = 0 for ph in child.findall(".//{http://mary.dfki.de/2002/MaryXML}ph"): #log.debug ph.attrib #tokendur += ph.attrib['{http://mary.dfki.de/2002/MaryXML}d'] tokendur += int(ph.attrib['d']) endtime += tokendur #endtime_seconds = endtime/1000.0 #token = (orth,endtime_seconds) elif child.tag == "{http://mary.dfki.de/2002/MaryXML}mtu": log.debug("Found mtu token") log.debug(child.attrib) #The expanded words of the mtu expanded = "".join(child.itertext()).strip() log.debug("expanded: %s" % expanded) #The original orthography #TODO return both orth = child.attrib["orig"] log.debug("orig orth: %s" % orth) tokendur = 0 for ph in child.findall(".//{http://mary.dfki.de/2002/MaryXML}ph"): #log.debug ph.attrib #tokendur += ph.attrib['{http://mary.dfki.de/2002/MaryXML}d'] tokendur += int(ph.attrib['d']) endtime += tokendur #endtime_seconds = endtime/1000.0 #token = (orth,endtime_seconds) elif child.tag == "{http://mary.dfki.de/2002/MaryXML}boundary": orth = "PAUSE" orth = "" #log.debug "ORTH:", orth #log.debug child.attrib #endtime += child.attrib['{http://mary.dfki.de/2002/MaryXML}duration'] if "duration" in child.attrib: endtime += int(child.attrib['duration']) #endtime_seconds = endtime/1000.0 #token = (orth,endtime_seconds) orth = re.sub("\s+", " ", orth) endtime_seconds = endtime/1000.0 token = {"orth":orth,"endtime":endtime_seconds} if expanded: expanded = re.sub("\s+", " ", expanded) token["expanded"] = expanded sentence.append(token) output_tokens.append(token) #log.debug utt #return (output_tokens, lang) return output_tokens
def test_lexicon_client(): lexicon = "wikispeech_testdb:sv" sent = "apa hund färöarna" trans = {} trans["apa"] = '"" A: . p a' trans["hund"] = '" h u0 n d' trans["färöarna"] = '"" f {: . % r 2: . a . rn a' try: lexicon_client.loadLexicon(lexicon) lex = lexicon_client.getLookupBySentence(sent, lexicon) log.debug("LEX: %s" % lex) except: log.error( "Failed to do lexicon lookup.\nError type: %s\nError info:%s" % (sys.exc_info()[0], sys.exc_info()[1])) import traceback log.debug("Stacktrace:") if log.log_level == "debug": traceback.print_tb(sys.exc_info()[2]) log.debug("END stacktrace") log.error("lexicon lookup test failure") log.error("No running lexserver found at %s" % config.config.get("Services", "lexicon")) raise for word in sent.split(" "): try: if lex[word] != trans[word]: log.error("lexicon lookup test failure") log.error("word %s, found %s, expected %s" % (word, lex[word], trans[word])) raise except KeyError: log.error( "Lexicon lookup test failure: Word %s not found in lexicon %s" % (word, lexicon)) raise log.debug("SUCCESS: lexicon lookup test")
def mary2ws(maryxml, voice): (lang, maryxml) = dropHeader(maryxml) #lang = "sv" #log.debug(maryxml) root = ET.fromstring(maryxml.encode('utf-8')) #root = ET.fromstring(maryxml) paragraphs = [] utterance = {"lang": lang, "paragraphs": paragraphs} paragraph_elements = root.findall(".//p") for paragraph_element in paragraph_elements: sentences = [] paragraphs.append({"sentences": sentences}) sentence_elements = paragraph_element.findall("s") for sentence_element in sentence_elements: phrases = [] sentence = {"phrases": phrases} sentences.append(sentence) for sentence_child in sentence_element: if sentence_child.tag == "phrase": phrase_element = sentence_child phrase = buildPhrase(phrase_element, lang, voice) phrases.append(phrase) elif sentence_child.tag == "prosody": prosody_element = sentence_child #can 'prosody' only contain exactly one 'phrase'? phrase_element = prosody_element[0] phrase = buildPhrase(phrase_element, lang, voice) phrase = addIfExists(phrase, prosody_element, "pitch", prefix="prosody_") phrase = addIfExists(phrase, prosody_element, "range", prefix="prosody_") phrases.append(phrase) elif sentence_child.tag == "t": #This is a special case that happens (sometimes..) with single-word sentences #doesn't work, produces error in synthesis #TODO look at this again #It only happens with the word "Hon." ... phrase_element = ET.Element("phrase") phrase_element.append(sentence_child) phrase = buildPhrase(phrase_element, lang, voice) phrases.append(phrase) else: log.error("sentence child should not have tag %s" % sentence_child.tag) return utterance
def test_textproc(): sent = "apa" try: res = textproc("sv", "default_textprocessor", sent) except: log.error( "Failed to do textprocessing.\nError type: %s\nError info:%s" % (sys.exc_info()[0], sys.exc_info()[1])) import traceback log.debug("Stacktrace:") traceback.print_tb(sys.exc_info()[2]) log.debug("END stacktrace") log.error("textprocessing test failure") log.error("No running marytts server found at %s" % config.config.get("Services", "marytts")) raise #TODO Better with exception than return value if type(res) == type("") and res.startswith("ERROR:"): log.error("Failed to do textprocessing") log.error(res) log.error("textprocessing test failure") raise log.debug("%s --> %s" % (sent, res)) log.debug("SUCCESS: textprocessing test")
def test_wikispeech(): sent = "apa" trans = {} trans["apa"] = '" A: - p a' lang = "sv" try: tmp = textproc(lang, "default_textprocessor", sent) res = synthesise(lang, "default_voice", tmp, "markup", "json") except FileNotFoundError: log.error( "Failed to do wikispeech test.\nError type: %s\nError info:%s" % (sys.exc_info()[0], sys.exc_info()[1])) import traceback log.debug("Stacktrace:") traceback.print_tb(sys.exc_info()[2]) log.debug("END stacktrace") log.error("wikispeech test failure") log.error("Is the audio_tmpdir %s correctly configured?" % config.config.get("Audio settings", "audio_tmpdir")) raise except: log.error( "Failed to do wikispeech test.\nError type: %s\nError info:%s" % (sys.exc_info()[0], sys.exc_info()[1])) import traceback log.debug("Stacktrace:") traceback.print_tb(sys.exc_info()[2]) log.debug("END stacktrace") log.error("wikispeech test failure") log.error("No running marytts server found at %s" % config.config.get("Services", "marytts")) raise #TODO Better with exception than return value if type(res) == type("") and res.startswith("No voice available"): log.error("Failed to do wikispeech test") log.error(res) log.error("wikispeech test failure") raise log.debug("%s --> %s" % (sent, res)) log.debug("SUCCESS: wikispeech test")
return "{name:%s, lang:%s}" % (self.name, self.lang) def __str__(self): return {"name":self.name, "lang":self.lang} #return "{name:%s, lang:%s}" % (self.name, self.lang) if __name__ == "__main__": log.log_level = "debug" #debug, info, warning, error voice_config = { "lang":"sv", "name":"stts_sv_nst-hsmm", "engine":"marytts", "adapter":"adapters.marytts_adapter", "mapper": { "from":"sv-se_ws-sampa", "to":"sv-se_sampa_mary" } } try: v = Voice(voice_config) log.info("Created voice %s from %s" % (v, voice_config)) except VoiceException as e: log.error("Failed to create voice for %s\nException message was:\n%s" % (voice_config, e))
l.append("config_file: %s" % (self.config["config_file"])) return "{%s}" % ", ".join(l) def __str__(self): return self.__repr__() if __name__ == "__main__": log.log_level = "debug" #debug, info, warning, error voice_config = { "lang":"sv", "name":"stts_sv_nst-hsmm", "engine":"marytts", "adapter":"adapters.marytts_adapter", "mapper": { "from":"sv-se_ws-sampa", "to":"sv-se_sampa_mary" } } try: v = Voice(voice_config) log.info("Created voice %s from %s" % (v, voice_config)) except VoiceException as e: log.error("Failed to create voice for %s\nException message was:\n%s" % (voice_config, e))