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

186 lines
5.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# play_logic.py
# This script contains the logic to turn the play_script into instructions for the hardware
# ---
# 01 FUNTION TO PROCESS THEATRE SCRIPT
import re
from config import characters, directions
def read_script(filename):
lines = []
with open(filename, 'r') as f:
for line in f.readlines():
#print(line)
parts = re.match(r'(?P<character>^.+?):\s?(\[(?P<stage_directions>[^]]+)\])?\s?(?P<text>.*)', line)
parts_character = parts.group('character')
parts_text = parts.group('text')
parts_directions = str(parts.group('stage_directions')).split(".")
lines.append((parts_character,parts_text,parts_directions))
#print(lines)
return lines;
# 02 FUNCTION TO SYNTHESIZE TEXT
# based on https://github.com/marytts/marytts-txt2wav/tree/python
# To play wave files
from subprocess import call
from time import sleep
# Mary server informations
mary_host = "localhost"
mary_port = "59125"
# HTTP + URL packages
import httplib2
import os
from urllib.parse import urlencode, quote # For URL creation
def tts(voice, input_text, speaker):
# 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
fpath = os.path.join(os.path.dirname(__file__), '/tmp/output_wav.wav')
f = open(fpath, "wb")
f.write(content)
f.close()
# aplay -D mono3 /tmp/output_wav.wav
call(["aplay", fpath])
else:
raise Exception(content)
# 03 FUNCTIONS TO RUN THE PLAY ON THE SPEAKERS
import paho.mqtt.client as mqtt
import json
from time import sleep
HOST = 'localhost'
PORT = 1883
def on_connect(client, userdata, flags, rc):
print("Connected to {0} with result code {1}".format(HOST, rc))
# Subscribe to the text detected topic
client.subscribe("hermes/asr/textCaptured")
client.subscribe("hermes/nlu/intentNotRecognized")
client.subscribe('hermes/intent/jocavdh:ask')
client.subscribe('hermes/intent/jocavdh:answer_yes')
client.subprocess('hermes/dialogueManager/sessionEnded')
client.subprocess('hermes/dialogueManager/sessionStarted')
def on_message(client, userdata, msg):
print('Google Home is not speaking anymore')
client.connected_flag=True
# def on_waiting(client, userdata, msg):
# sessionId = json.loads(id.payload)
# print('delete mistaken intent')
# client.publish("hermes/dialogueManager/endSession", json.dumps({
# 'sessionId': sessionId,
# }))
client = mqtt.Client()
client.connect(HOST, PORT, 60)
client.on_connect = on_connect
client.connected_flag=False
listening = False
def play():
for character, line, direction in read_script('play_scripts/demo.txt'):
input_text = line
voice = characters.get(character)[0]
speaker = characters.get(character)[1]
#speaker = 'default'
# Some way to do something with the stage directions will come here
action = directions.get(direction[0])
tts(voice, input_text, speaker)
if action == 'listen_google_home':
print('Waiting for the Google Home to finish its talk')
# # start voice activity detection
# client.publish("hermes/asr/startListening", json.dumps({
# 'siteId': 'default',
# 'init': {
# 'type': 'action',
# 'canBeEnqueued': True
# }
# }))
client.publish("hermes/asr/startListening", json.dumps({
'siteId': 'default'
}))
# create callback
client.on_message = on_message
listening = True
while listening:
client.loop()
#client.on_message = on_message
client.message_callback_add('hermes/asr/textCaptured', on_message)
if client.connected_flag:
sleep(1)
print('Continue the play')
client.connected_flag = False
# client.message_callback_add('hermes/dialogueManager/sessionQueued', on_waiting)
break
if action == 'audio':
print('play audioclip')
playing = True
while playing:
call(["aplay", "-D", speaker, "/usr/share/snips/congress.wav"])
playing = False
if action == 'listen_audience':
print('ask the audience')
sleep(1)
print('This act is done.')
return