feat: stream audio to CB
Uses PyAudio and ZeroMQ to publish audio chunks. ref: N25B-119
This commit is contained in:
44
main.py
44
main.py
@@ -1,27 +1,51 @@
|
||||
import qi
|
||||
import zmq
|
||||
import time
|
||||
|
||||
from src.audio_streaming import AudioStreaming
|
||||
from state import state
|
||||
|
||||
|
||||
def say(session, message):
|
||||
tts = session.service("ALTextToSpeech")
|
||||
tts.say(message)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = qi.Application()
|
||||
app.start()
|
||||
session = app.session
|
||||
|
||||
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
|
||||
socket.setsockopt_string(zmq.SUBSCRIBE, u"") # u because Python 2 shenanigans
|
||||
|
||||
while True:
|
||||
print("Listening for message")
|
||||
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))
|
||||
|
||||
say(session, message)
|
||||
if session: say(session, message)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user