[Python] Twilio Bidirectional stream along with STT to transcribe

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am receiving all the packets from twilio and also able to send it back and get a response the LLM is giving. I am using STT, TTS and LLM to do my processing. The problem is that when I am doing STT, the STT provider I am using requires the audio_data in wav/mp3 format in 8khz/16khz. Since twilio sends data in mulaw format at 8khz, we need to convert first this into a linear format and then convert it into wav format. I am using audioop.ulaw2lin() method to convert it first into linear then make it into wav format. Because of doing this process, the audio I am getting before sending it to STT is becoming sped up and some words become discernible and very difficult to translate. I have tried reducing sample rate but since twilio sends it at 8000 hz, if I decrease or increase it then its even worse. Can someone help me with this?

    elif data['event'] == 'media':
    # Decode base64 μ-law audio from Twilio
    audio_data = base64.b64decode(data['media']['payload'])
    # Convert μ-law to PCM
    pcm_audio = audioop.ulaw2lin(audio_data, 2)
    audio_data = self.convert_to_wav_8khz(pcm_audio)


    def convert_to_wav_8khz(self, audio_bytes):
    """Convert raw audio byte stream to 8kHz WAV (in-memory)."""
    # Load audio from bytes (pydub auto-detects format)
    audio = AudioSegment.from_raw(
    BytesIO(audio_bytes),
    sample_width=2,
    frame_rate=8000,
    channels=1
    )

    # Convert to mono and 8kHz
    audio = audio.set_channels(1).set_frame_rate(8000)

    # Save to an in-memory buffer as WAV
    wav_buffer = BytesIO()
    audio.export(wav_buffer, format="wav")
    return wav_buffer.getvalue()

    Continue reading...

Compartilhe esta Página