In elevenlabs you can now use the stream feature also with input stream. However, a socket connection must be established for this.
Unfortunately I have to implement the websocket connection myself instead of using the elevenlabs python client, because i have to use the websocket-client library and the elevenlabs client is using a different websocket library.
The problem is, however, that
data = json.loads(ws.recv())
blocks forever and nothing ever seems to arrive. what did I do wrong?
here is my code(BOS and EOS are exactly the same as in the documentations code of elevenlabs python client and rest of it is also nearly the same code):
with closing(create_connection(url, header={"xi-api-key":"mykey")) as ws:
# Send beginning of stream
ws.send(BOS)
# Stream text chunks and receive audio
for text_chunk in text_chunker(text):
data = dict(text=text_chunk, try_trigger_generation=True)
ws.send(json.dumps(data))
try:
data = json.loads(ws.recv()) -> blocking here, never gets over it
if data["audio"]:
yield base64.b64decode(data["audio"]) # type: ignore
except TimeoutError:
pass
# Send end of stream
ws.send(EOS)
# Receive remaining audio
while True:
try:
data = json.loads(ws.recv())
if data["audio"]:
yield base64.b64decode(data["audio"]) # type: ignore
except ws.exceptions.ConnectionClosed:
break
with the original code from the elevenlabs client , which uses the websockets.sync.client library, it works as expected
found a solution by using WebSocketApp in a thread.