52 lines
1.2 KiB
Python
52 lines
1.2 KiB
Python
import qi
|
|
import zmq
|
|
|
|
from src.audio_streaming import AudioStreaming
|
|
from state import state
|
|
|
|
|
|
def say(session, message):
|
|
tts = session.service("ALTextToSpeech")
|
|
tts.say(message)
|
|
|
|
|
|
def listen_for_messages(session):
|
|
context = zmq.Context()
|
|
socket = context.socket(zmq.SUB)
|
|
socket.connect("tcp://localhost:5556")
|
|
socket.setsockopt_string(zmq.SUBSCRIBE, u"") # u because Python 2 shenanigans
|
|
|
|
poller = zmq.Poller()
|
|
poller.register(socket, zmq.POLLIN)
|
|
|
|
print("Listening for messages")
|
|
while not state.exit_event.is_set():
|
|
if not poller.poll(200): continue # At most 200 ms delay after CTRL+C
|
|
# We now know there's a message waiting for us
|
|
message = socket.recv_string()
|
|
print("Received message: {}".format(message))
|
|
|
|
if session: say(session, message)
|
|
|
|
|
|
def main():
|
|
try:
|
|
app = qi.Application()
|
|
app.start()
|
|
session = app.session
|
|
except RuntimeError:
|
|
session = None
|
|
|
|
audio_streamer = AudioStreaming()
|
|
audio_streamer.run()
|
|
|
|
listen_for_messages(session) # Runs indefinitely, until CTRL+C
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
state.initialize()
|
|
main()
|
|
finally:
|
|
state.deinitialize()
|