stefanrmmr / streamlit-audio-recorder

Record Audio from the User's Microphone in Apps that are Deployed to the Web. (via Browser Media-API, REACT-based, Streamlit Custom Component)
MIT License
415 stars 75 forks source link

retrieve data in the streamlit app #3

Closed millawell closed 2 years ago

jingzhaoou commented 2 years ago

I am using the following solution. I hope to avoid using numpy. Of course, we don't have to save the data to a file if that is not required. A potential issue is that sending data from Javascript to Python seems quite slow.

      const blob = await fetch(data.url).then(r => r.blob());
      const blobData = await blob.arrayBuffer();
      const jsonBlobData = Buffer.from(blobData).toJSON()
      Streamlit.setComponentValue(jsonBlobData)
    audiorec_data = st_audiorec()
    if audiorec_data is not None and 'data' in audiorec_data.keys():
      wav_data = audiorec_data['data']
      with open(f1, 'w+b') as fh:
        fh.write(bytes(wav_data))
jingzhaoou commented 2 years ago

This solution is much faster and works. Just that there are some weird errors on the Python side.

      const blob = await fetch(data.url).then(r => r.blob());
      const blobData = await blob.arrayBuffer();
      Streamlit.setComponentValue(blobData)
    audiorec_data = st_audiorec()
    if audiorec_data is not None:
      wav_data = audiorec_data
      with open(f1, 'w+b') as fh:
        fh.write(wav_data)
millawell commented 2 years ago

I mean numpy is only used in that example which is totally not necessary, what one would do in the app is totally up to the user :)

jingzhaoou commented 2 years ago

@millawell Haha, totally agreed. I found that if we can pass the arrayBuffer directly to the Python side, the frontend handling seems be much faster.