Joooohan / audio-recorder-streamlit

MIT License
74 stars 15 forks source link

what's the sample width of returned audio bytes? #5

Closed gustavz closed 1 year ago

gustavz commented 1 year ago

What's the sample width of returned audio bytes? I want to convert the data into a speech_recognition.AudioData object which needs sample_rate and sample_width defined. lib: https://github.com/Uberi/speech_recognition

Joooohan commented 1 year ago

Hi @gustavz, you can get the sample_width using a third party library like wave. I tried this and it seems to work:

    import io
    import wave
    import streamlit as st

    base_audio_bytes = audio_recorder(key="base")
    if base_audio_bytes:
        st.audio(base_audio_bytes, format="audio/wav")
        buffer = io.BytesIO(base_audio_bytes)
        with wave.open(buffer) as f:
            st.write(f.getsampwidth())