I’ve been implementing a file upload functionality. I now want to extend this to
file_uploader = st.sidebar.file_uploader(label="", type=".wav") if file_uploader is not None: st.write(file_uploader) y, sr = handle_uploaded_audio_file(file_uploader)
where the called method is
def handle_uploaded_audio_file(uploaded_file): a = pydub.AudioSegment.from_wav(uploaded_file) st.write(a.sample_width) samples = a.get_array_of_samples() fp_arr = np.array(samples).T.astype(np.float32) fp_arr /= np.iinfo(samples.typecode).max st.write(fp_arr.shape) return fp_arr, 22050
I can successfully play back the audio–but it does not sound the same. I guess that it’s some vital information missing here, e.g. the sample rate, bit rate, and so on.
My question therefore is:
I see the problem with the last part, reading the vital information. Since the sample rate determines the length, a higher sample rate makes the file shorter, and vice versa. It’s therefore important to determine the right parameters. This I’d like to read directly from the file, i.e. not manually inserting them.
How can I upload an audio file, convert it to the same wav file internally (as if loading from disk), and not manually adding sample rate and similar information?