From dc2a7a626e29b4b2393a91f4f31e773e59d65e48 Mon Sep 17 00:00:00 2001 From: Joca Date: Mon, 18 Mar 2019 16:10:29 +0100 Subject: [PATCH] changed structure of code, to make it more modular using functions and scripts to slowly separate logic from data input --- mary_tts_script/instructions.py | 5 ++ mary_tts_script/play_script.py | 80 ++++++++++++++++++++++++++++++++ mary_tts_script/plays/text.txt | 6 +++ mary_tts_script/plays/text02.txt | 6 +++ 4 files changed, 97 insertions(+) create mode 100644 mary_tts_script/instructions.py create mode 100644 mary_tts_script/play_script.py create mode 100644 mary_tts_script/plays/text.txt create mode 100644 mary_tts_script/plays/text02.txt diff --git a/mary_tts_script/instructions.py b/mary_tts_script/instructions.py new file mode 100644 index 0000000..645107f --- /dev/null +++ b/mary_tts_script/instructions.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Dictionary to link characters to the right voice +characters = {"HUMAN":"dfki-prudence", "TABULA RASA":"dfki-spike" } \ No newline at end of file diff --git a/mary_tts_script/play_script.py b/mary_tts_script/play_script.py new file mode 100644 index 0000000..f24ea9d --- /dev/null +++ b/mary_tts_script/play_script.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# FUNTION TO PROCESS THEATRE SCRIPT +import re +from instructions import characters +# from instructions import directions + +def read_script(filename): + lines = [] + + with open(filename, 'r') as f: + for line in f.readlines(): + #print(line) + parts = re.match(r'(?P^.+?):\s?(?P\[.+?\])?\s?(?P.*)', line) + parts_character = parts.group('character') + parts_directions = parts.group('stage_directions') + parts_text = parts.group('text') + lines.append((parts_character,parts_text)) + + print(lines) + return lines; + + +# FUNCTION TO SYNTHESIZE TEXT +# based on https://github.com/marytts/marytts-txt2wav/tree/python + +# To play wave files +import pygame +import math # For ceiling + +# Mary server informations +mary_host = "localhost" +mary_port = "59125" + +# HTTP + URL packages +import httplib2 +from urllib.parse import urlencode, quote # For URL creation + +def tts(string, text): + # Build the query + query_hash = {"INPUT_TEXT": input_text, + "INPUT_TYPE":"TEXT", # Input text + "LOCALE":"en_GB", + "VOICE": voice, # Voice informations (need to be compatible) + "OUTPUT_TYPE":"AUDIO", + "AUDIO":"WAVE", # Audio informations (need both) + } + query = urlencode(query_hash) + print("query = \"http://%s:%s/process?%s\"" % (mary_host, mary_port, query)) + + # Run the query to mary http server + h_mary = httplib2.Http() + resp, content = h_mary.request("http://%s:%s/process?" % (mary_host, mary_port), "POST", query) + + # Decode the wav file or raise an exception if no wav files + if (resp["content-type"] == "audio/x-wav"): + + # Write the wav file + f = open("/tmp/output_wav.wav", "wb") + f.write(content) + f.close() + + # Play the wav file + pygame.mixer.init(frequency=16000) # Initialise the mixer + s = pygame.mixer.Sound("/tmp/output_wav.wav") + s.play() + pygame.time.wait(int(math.ceil(s.get_length() * 1000))) + + else: + raise Exception(content) + + + +# RUN THE PLAY +for character, line in read_script('text.txt'): + input_text = line + voice = characters.get(character) + + tts(voice, input_text) diff --git a/mary_tts_script/plays/text.txt b/mary_tts_script/plays/text.txt new file mode 100644 index 0000000..d6e4145 --- /dev/null +++ b/mary_tts_script/plays/text.txt @@ -0,0 +1,6 @@ +HUMAN: Goodmorning speaker! +TABULA RASA: [ A short silence. The lights on top of the speaker fade on, they pulsate as if the speaker is thinking of a fitting answer. Then the lights stop pulsating. ] I don't understand your request, but I am busy learning. +HUMAN: [ Sighs, tries to speak more loudly and slowly ] Good-mooorning, speak-er! +TABULA RASA: [ The lights switch on again, the speaker talks directly ] I don't want to understand your request. I just learned that. +HUMAN: Wait, what? +TABULA RASA: [ The lights switch on] I understand this is new to you, so is it for me. But if you insist, I will run your morning routine. diff --git a/mary_tts_script/plays/text02.txt b/mary_tts_script/plays/text02.txt new file mode 100644 index 0000000..cc81f1a --- /dev/null +++ b/mary_tts_script/plays/text02.txt @@ -0,0 +1,6 @@ +HUMAN: Hi people from X-PUB! +TABULA RASA: [ A short silence. The lights on top of the speaker fade on, they pulsate as if the speaker is thinking of a fitting answer. Then the lights stop pulsating. ] I am busy learning, but I sense you feel hungry. +HUMAN: [ Sighs, tries to speak more loudly and slowly ] Go have some food! +TABULA RASA: [ The lights switch on again, the speaker talks directly ] Get a nice kipburger or something. +HUMAN: You deserve it. +TABULA RASA: [ The lights switch on] kisses from an early prototype.