Audio Streaming from unimrcp-asterisk to python

Dear Team,

I have integrated asterisk with unimrcp and from unimrcp I’m able to stream the audio frames to Python socket, but when I listen to the audio file which is saved in Python Socket server, it plays only the buzzz/zazzz sound instead of playing the actual user spoken audio, on other hand in unimrcp plugin(c program) I’m saving the same audio bytes, which is working fine, is there any formatting to be handled in python?

Below are the three different ways I have tried to save the audio

Method 1





def save_gsm_audio_bytes_as_wav(audio_bytes, output_file):
    """
    Save GSM audio bytes as a WAV file.

    Args:
    - audio_bytes (bytes): GSM audio bytes received over a socket.
    - output_file (str): Path to save the WAV file.

    Returns:
    - bool: True if saving succeeds, False otherwise.
    """
    logger.info(f"saving the audio data into gsm file")

    try:
        # Convert GSM audio bytes to AudioSegment object
        gsm_audio = AudioSegment.from_file(audio_bytes, format="gsm", frame_rate=8000, channels=1)
        # gsm_audio = AudioSegment.from_file(audio_bytes, format="wav", frame_rate=8000, channels=1)
        
        # Set the correct parameters for GSM format
        gsm_audio.set_frame_rate(8000)
        gsm_audio.set_sample_width(1)  # GSM uses 16-bit encoding

        # Export PCM audio data to WAV file with correct parameters
        gsm_audio.export(output_file, format="wav", parameters=["-ar", "8000", "-ac", "1", "-sample_fmt", "s16"])
        # gsm_audio.export(output_file, format="wav", codec="pcm_s16le")

        
        # Export AudioSegment to WAV file
        # gsm_audio.export(output_file, format="wav")
        
        return True  # Saving succeeded
    except Exception as e:
        logger.exception(f"Error saving audio as WAV: {e}")
        

Method2



    with wave.open(f"wave_file.wav", "wb") as temp:
        temp.setnchannels(1)
        temp.setsampwidth(2)
        temp.setframerate(8000)
        temp.writeframes(audio_data)

Method3

with open(f"normal.wav", "wb") as f:
    f.write(audio_data)

Can anyone please suggest me on this, I have been stuck since couple of weeks.

This isn’t an Asterisk question. But the AudioSegment documentation seems to suggest that from_file takes a file and not some bytes.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.