English 中文(简体)
通过网络链接表向Twilio提供电传
原标题:Stream audio back to Twilio via websocket connection
最佳回答

回答得很晚,但有可能。 参看本节:。 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

在发出媒体信息之后,我建议你发出一个标志信息。 这使你知道你的录音是在什么时候进行的。 在此情况下,你可以向亚马孙波里提出音频要求,并依次发送。

问题回答

回答还很晚,因为我也正在奋力回击,因此,我要分享一些示范守则,使用 no子和ure笑话。 下面的法典是假定你遵循了从网上和网上查阅的传闻文件。

下面是一份基本的网络袖珍守则,你可以利用 no在网上找到许多实例。 The transcriber in the following Code I use AssemblyAI streaming api which You can found here: 。 在我把音响转换为呼声之后,我使用斜话服务,将文字转换成言语,并用网页上传回。

 this.wss.on( connection , (ws) => {
        console.log( A new client connected. );
        ws.on( message , async (message) => {
            try {
                let data = JSON.parse(message);
                switch (data.event) {
                    case  connected :
                        console.info( Twilio media stream connected );
                        break;
                    case  start :
                        console.info( Twilio media stream started );
                    case  media :
                        await transcriberConnectionPromise;
                        transcriber.sendAudio(Buffer.from(data.media.payload,  base64 ));
                        let userSpeech = transcriber.getFinalText();

                        let audioOutput = await this.azure.convertTextToSpeechStream( how are you );
                        ws.send(JSON.stringify({
                           "event": "media",
                           "streamSid": data.streamSid,
                           "media": {
                           "payload": audioOutput
                           }
                        }));
                        break;
                    case  mark :
                    case  stop :
                        await transcriber.close();
                        console.info( Twilio media stream stopped );
                        break;
            } catch (error) {
                console.log(error);
                ws.terminate();
            }
        });

        ws.on( close , () => {
            console.log( Client has disconnected. );
        });
    });
}

演讲:

    async convertTextToSpeechStream(speechInput: string): Promise<string> {
    return new Promise((resolve, reject) => {
        let audioData = null;
        let speechConfig = sdk.SpeechConfig.fromSubscription(process.env[ azure.speech.apim.key ]!, process.env[ azure.speech.region ]);
        speechConfig.speechSynthesisOutputFormat = sdk.SpeechSynthesisOutputFormat.Raw8Khz8BitMonoMULaw;

        let xmlData = `
        <speak version= 1.0  xml:lang= en-US >
        <voice name= en-US-AvaNeural  effect="eq_telecomhp8k">
        <prosody rate="+8.00%" volume="soft" styledegree="2" role="YoungAdultFemale"> 
        ${speechInput}
        </prosody>
        </voice>
        </speak>`;

        const speechSynthesizer = new sdk.SpeechSynthesizer(speechConfig, null);
        speechSynthesizer.speakSsmlAsync(
            xmlData,
            result => {
                audioData = result.audioData;
                speechSynthesizer.close();
                resolve(Buffer.from(audioData!).toString( base64 ))
            },
            error => {
                console.log(error);
                speechSynthesizer.close();
                reject(new Error( Text to speech issue ));
            });
    });
}




相关问题
Rails 3 XML Builder / Twilio API

This Twilio API Sample code isn t working in Rails 3: #voice_controller.rb def reminder @postto = BASE_URL + /directions respond_to do |format| format.xml { @postto } end ...

Using twimlets in a Twilio Play verb

I thought it I could specify any URL - including the twimlets used in conference calls - in the Play verb of a TwiML file. However, the following does not work... http://twimlets.com/holdmusic?Bucket=...

Twilio - how do they send/receive SMS

I m interested to find out how twilio s SMS sending/receiving feature works, on long codes, or local numbers Outside of the states, mobile originated SMS terminated on a long number is very common, ...

400 bad request from twilio s REST API using twilio-ruby

I m trying to get started with Twilio s REST API using the rubygem twilio-ruby, and I ve hit a snag. Here s my code: h = {:From => "123-123-1234", :To => "123-123-1234", :Body => "hey"} ...

How does the Twilio website work?

There is a good pitch from Twilio here. I just don t get how they can do that with a website. How can you control a land line with a web browser?

Django | twilio to send SMS

I m using twilio as for a mobile verification mechanism, I have no prior experience in using twilio but looking at the sample PHP code I used this one in my code but apparently it s giving me an 400 ...

IVR vs Asp.net MVC: How can I stop reinventing the browser?

I m making an IVR system for a project, and have decided on Twilio to handle the phone portion (making and receiving calls, sending and receiving SMS messages). This will surface a website with an IVR ...

热门标签