Normalize audio using pydub and load it on streamlit audio player

Hi guys,

I am trying to normalize audio data and then load it on streamlit audio player. I am using pydub for this. The following code is not working for me and I cannot figure out why!

from pydub.effects import normalize
from pydub import AudioSegment

sound = AudioSegment.from_wav(filename)
norm = normalize(sound)
st.audio(norm.raw_data)

When I run this code, the audio player does not function. “.raw_data” basically passes the data in bytes to st.audio() method, which is the same format that it requires! Can anyone help me with this? I want to normalize each audio before it plays on the app.

Hi.

Not an expert…BUT:

I was trying to do something very similar and ran into this pydub issue from 2017.

Pydub’s creator says that " The reason you may want to use this sound.export() method is because the raw audio data does not include any headers that would normally be in an audio file (the meta data is stored in the audio segment separately from the audio samples)".

In my use case I didn’t want to store the AudioSegment before using in Streamlit, so I “saved the AudioSegment” exported without a name to a variable, and then read that variable to st.audio :

example = audio.export()
st.audio(example.read()

And it worked.

Hope it helps. If not, interested to know how you solve this issue.

3 Likes

Hi @munozbravo thanks for sharing this info. I have created a separate copy of normalised audios for now and I’m loading those audio files in the player. Earlier, I had thought of exporting audios and then loading them back but that was something I wanted to avoid as multiple users are accessing audios in my application, so it was going to be a little hard to track. I will try out what you have posted and will get back to you!