Files
pepperplus-ri/main.py
Twirre Meulenbelt afae6fc331 feat: stream audio to CB
Uses PyAudio and ZeroMQ to publish audio chunks.

ref: N25B-119
2025-10-01 10:50:53 +02:00

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()