Joooohan / audio-recorder-streamlit

MIT License
80 stars 16 forks source link

How to save audio_bytes as .wav file #11

Closed G1017 closed 4 months ago

G1017 commented 1 year ago

I used this method, but there was a problem with the audio I saved.

import streamlit as st import numpy as np import soundfile as sf from audio_recorder_streamlit import audio_recorder

def save_audio(audio_bytes, output_file):

保存为 WAV 文件

audio_array = np.frombuffer(audio_bytes, dtype=np.int16)
with sf.SoundFile(output_file, 'w', 44100, 1) as file:
        file.write(audio_array)

audio_bytes = audio_recorder(sample_rate=44_100) if audio_bytes: save_audio(audio_bytes,'output.wav') st.audio(audio_bytes, format="audio/wav")

mechnader commented 11 months ago

This worked for me : if audio_bytes: st.audio(audio_bytes, format="audio/wav") with open(path_myrecording, mode='bw') as f: f.write(audio_bytes) f.close()

I think the problem on your code came from the audio_bytes that is in stereo. I think you are hearing the audio in "slow frame rate". May be the np.frombuffer is not not adapted for stereo.