回答得很晚,但有可能。 参看本节:。 https://www.twilio.com/docs/voice/twiml/stream#websocket-messages-to-twilio 。
基本上,你重新利用网页表发送整个链接的数据。 你们应该以文字/逐字方式回来。
The example JSON that was provided in the docs:
{
"event": "media",
"streamSid": "MZ18ad3ab5a668481ce02b83e7395059f0",
"media": {
"payload": "a3242sadfasfa423242... (a base64 encoded string of 8000/mulaw)"
}
}
I m 个人利用亚马孙波里用于TTS。 以下是如何使用波兰语的例子:
class Manager:
def __init__(self):
self._exit_stack = AsyncExitStack()
self._s3_client = None
async def __aenter__(self):
session = AioSession()
self._s3_client = await self._exit_stack.enter_async_context(
session.create_client("s3")
)
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._exit_stack.__aexit__(exc_type, exc_val, exc_tb)
async def create_client(service: str, session: AioSession, exit_stack: AsyncExitStack):
client = await exit_stack.enter_async_context(session.create_client(service))
return client
WORD = "<speak>"
async def synthesize_speech(text: str, voice_id: str = "Matthew"):
session = AioSession()
async with AsyncExitStack() as exit_stack:
polly = await create_client("polly", session, exit_stack)
try:
response = await polly.synthesize_speech(
Text=text,
TextType="ssml" if WORD in text else "text",
OutputFormat="pcm",
VoiceId=voice_id,
SampleRate="8000",
)
except (BotoCoreError, ClientError) as error:
logger.error(error)
raise HTTPException(500, "Failed to synthesize speech")
else:
mulaw_audio = await response["AudioStream"].read()
audio = audioop.lin2ulaw(mulaw_audio, 2)
base64_audio = base64.b64encode(audio).decode("utf-8")
return base64_audio
接着,这里举出了“快车牌”中如何发送网状数据的例子:
from fastapi import WebSocketDisconnect
@app.websocket("/stream")
async def websocket(ws: WebSocket):
await ws.accept()
stream_sid = None
try:
while True:
packet = await ws.receive_json()
if packet["event"] == "start":
# Save the stream SID for later use
# I would go as far as saving most of the start message
stream_sid = packet["streamSid"]
continue
# Send audio back:
await ws.send_json(
{
"event": "media",
"streamSid": stream_sid,
"media": {
"payload": await synthesize_speech("Hello world!")
}
}
)
# If you want to send multiple audio messages
# You should send a mark message. You ll receive
# a mark event back where you can send the next audio
except WebSocketDisconnect:
pass
在发出媒体信息之后,我建议你发出一个标志信息。 这使你知道你的录音是在什么时候进行的。 在此情况下,你可以向亚马孙波里提出音频要求,并依次发送。