Coder Social home page Coder Social logo

streamlit-audiorecorder's Introduction

Streamlit App

streamlit-audiorecorder

An audio Recorder for streamlit

Description

Audio recorder component for streamlit.
It creates a button to start the recording and takes three arguments: the start button text, the stop button text, and the pause button text.
If the pause button text is not specified, the pause button is not displayed.

Example with buttons

If all prompts are given as empty strings, the component will use the react-audio-recorder visualizer:

Example with the visualiser

Parameters

The signature of the component is:

audiorecorder(start_prompt="Start recording", stop_prompt="Stop recording", pause_prompt="", show_visualizer=True, key=None):

The prompt parameters are self-explanatory.
The optional key parameter is used internally by Streamlit to properly distinguish multiple audiorecorders on the page.
The show_visualizer parameter is a boolean that determines whether to show live audio visualization while recording. If set to False, the text "recording" is displayed. It is used only when all prompts are empty strings.

Return value

The component's return value is a pydub AudioSegment.
All AudioSegment methods are available. In particular, you can:

  • Play the audio in the frontend with st.audio(audio.export().read())
  • Save the audio to a file with audio.export("audio.wav", format="wav")

Installation:

pip install streamlit-audiorecorder

Note: This package uses ffmpeg, so it should be installed for this audiorecorder to work properly.

On Ubuntu/Debian: sudo apt update && sudo apt install ffmpeg
On Mac: brew install ffmpeg

Usage:

import streamlit as st
from audiorecorder import audiorecorder

st.title("Audio Recorder")
audio = audiorecorder("Click to record", "Click to stop recording")

if len(audio) > 0:
    # To play audio in frontend:
    st.audio(audio.export().read())  

    # To save audio to a file, use pydub export method:
    audio.export("audio.wav", format="wav")

    # To get audio properties, use pydub AudioSegment properties:
    st.write(f"Frame rate: {audio.frame_rate}, Frame width: {audio.frame_width}, Duration: {audio.duration_seconds} seconds")

Troubleshooting:

Error: No record button is shown and you get the following error message in the console:

Component Error
Cannot read properties of undefined (reading 'getUserMedia')

Reason: To record the audio, this component uses the MediaDevices`` interface. For security reasons, the getUserMedia()` method is available only in secure contexts (HTTPS), as explained in the MDM documentation :

As an API that may involve significant privacy concerns, getUserMedia()'s specification lays out a wide array of privacy and security requirements that browsers are obligated to meet.

getUserMedia() is a powerful feature that can only be used in secure contexts; in insecure contexts, navigator.mediaDevices is undefined, preventing access to getUserMedia(). A secure context is, in short, a page loaded using HTTPS or the file:/// URL scheme, or a page loaded from localhost.

Solution: Serve your website using HTTPS. If you are serving your website locally, make sure to access it using localhost, not an IP address.

streamlit-audiorecorder's People

Contributors

theevann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

streamlit-audiorecorder's Issues

Key: Missing Parameter

I guess your key parameter is internally tied to the label, and is therefore not exposed for end use. This causes an error in streamlit programs where there are multiple audio recorder widgets with either the same label (Eg. "Speak here") or blank label (because you only want to use the icon with no label)

Cheers

ffmpg error

Can I define ffmpeg through yml or requirement.txt, and how, knowing that I am working on the web app , streamlit?
Capture

[Query] Is there a way to associate an animation/gif while recording.

Hi,

Currently the only indication of recording is the change of text. Is there a way we can perhaps also add animation (like lottiefiles or blinking red circle icon or microphone) which is more suggestive of the recording being in progress.

Great work by the way on having such a clean implementation.

JSONDecodeError with audiorecorder function

Hey,

I have a problem using the audiorecorder function. After it has been working on the same system (MacOS), now I get the error message below. I cooked everything down to these two lines of code in a clean virtual environment with only streamlit audiorecorder.

'''from audiorecorder import audiorecorder
audio = audiorecorder("Click to record something", "Click to stop recording")'''

  • Also tried un/reinstalling ffmpeg

Any ideas?

Thank you

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Traceback:
File "localpath/.venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
exec(code, module.dict)
File "localpath/app/other_pages/app_transkription.py", line 24, in
audio = audiorecorder("Click to record something", "Click to stop recording")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "localpath/.venv/lib/python3.11/site-packages/audiorecorder/init.py", line 27, in audiorecorder
audio_segment = AudioSegment.from_file(BytesIO(b64decode(base64_audio)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "localpath/.venv/lib/python3.11/site-packages/pydub/audio_segment.py", line 728, in from_file
info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "localpath/.venv/lib/python3.11/site-packages/pydub/utils.py", line 279, in mediainfo_json
info = json.loads(output)
^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/init.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None

Pause/resume

Hi,

Thank you for your work on this. I have added the component into a streamlit application that records audio and then transcribes it via OpenAI's Whisper, then improves the grammar and formatting via GPT-3.5-turbo. It works very well.

Would it be possible to add in a second button to pause/resume the recording?

Whilst I can dabble with python, I have no knowledge of JS/TS.

No sound recorded

Hey,
This component is exactly what I'm looking for so, thanks!
But, I can't make it work.
I tried the following on my Mac:

audio = audiorecorder("Click to record", "Recording...")
if len(audio) > 0:
st.audio(audio, sample_rate = 16000)

I am required to specify the sample_rate.
The app does require access to my microphone - good.
And whatever rate I try, I get only some white noise when trying to play the record.

Anything I'm not doing right? Thanks in advance!

Only one recorder in two tabs after recording

Simple code for two recorders (each in different tab):

    tab1, tab2 = st.tabs(["TAB1", "TAB2",])
    with tab1:
        audio1 = audiorecorder("RECORDER 1", "Recording...", key="recorder1")
        if len(audio1) > 0:
            st.write(f"Audio1 len: {len(audio1)}")

    with tab2:
        audio2 = audiorecorder("RECORDER 2", "Recording...", key="recorder2")
        if len(audio2) > 0:
            st.write(f"Audio2 len: {len(audio2)}")

After using one recorder other recorder in different tab will be hidden.
Video: streamlit-audiorecorder_tabs_bug.webm

streamlit==1.15.2
streamlit-audiorecorder==0.0.2

[Query] Inconsistent performance on devices on deployment.

Hi,

When deploying the component using Streamlit via github., I am using the audio recorder as an alternate input in chat, so it can be used for more than once for recording. Following are the issues I have observed:

  1. On iOS device, for each interaction a pop up shows up asking for allowing the permission to the microphone. Is there a way to minimise this to just once per session? On android devices it usually allows to save the selection for the website.
  2. For some reason on certain android devices on Chrome the permission pop up doesn't show up at all and in such case the component shows both the "Start" and "Stop" buttons and obviously nothing works since the permission doesn't get granted.
  3. In some cases on both androids and iphones even though the component works properly for some time. After several recordings, all of a sudden, the record bottons starts and stops as expected but no audio is captured.

I have tried it across several android and iphones and one issue or the other usually pops up after a few tries. I don't know if these are device specific issues or streamlit component related. Since I am quite new to the whole streamlit stack hence seeking some insight or pointers on how to debug these or to know if these are some known issues with components or streamlit in general.

Thanks

Does this work on mobile device?

Hello,

Great plugin, thanks for your hard work.
I have a question, does this work on mobile devices?

I click on record audio on my poco android and nothing happens. No authorization prompt for the microphone, no nothing.

Perhaps that's a know issue?

Thanks

Error message

Hello,

I get :

Component Error
Cannot read properties of undefined (reading 'getUserMedia')

No recorder is shown, just a blank player.

Thanks

Recorded audio can't be read as an .mp3 file

Hi! (again)

In your example you suggest writing audio.tobytes() to an mp3 file.

I tested this, the mp3 file doesn't play in vlc. But if written in a .wav file, the wav file reads without problem.

This suggests that the bytes data outputted by your audiorecorder is intrinsically encoded in wav format.

Yet, when trying to convert the wav audio file (or bytes) to an AudioSegment, pydub crashes, warning me that the file isn't in proper wav format (even though it reads fine in vlc!). This is probably because the file doesn't have the proper header a wav file should have.

So I checked the source code of your react component. I'm not a front-end specialist, but when you process the data output of the MediaRecorder object, you write :

 recorder.ondataavailable = async ({data}) => {
        const audioData = new Blob([data], {type: "audio/mp3"});
        const audioData_str = (await BlobToDataURL(audioData)).replace(/^data:.+?base64,/, "");
        Streamlit.setComponentValue(audioData_str);
      }

When creating the blob, you force its type to "audio/mp3". This will result in setting the header of the blob as the one of an mp3 file.
But the data itself coming out of the recorder is likely not in mp3 format (I guess it is in wav format), and just forcing the type of the blob won't achieve any conversion. You just put wav bytes data into a blob with mp3 header. This results in crashes when having to convert the content with other audio tools, such as pydub, the wave module, speech_recognition etc...

Some audio tools such as vlc or st.audio will sometimes ignore the header to process only the bytes content. That's why reading the data still works in some cases : st.audio thinks you input wav bytes into it so it plays it as a wav. But it will make more advanced audio tools depending on the header crash.

To solve this issue, you probably just have to change "audio/mp3" to "audio/wav" when creating the blob.

This should do the trick !

FileNotFoundError: [WinError 2] The system cannot find the file specified

I get a problem when hitting the stop button "FileNotFoundError: [WinError 2] The system cannot find the file specified"

This is when testing app in visual studio code (and I run visual studio code software first from my Command prompt after activating my conda env, because if I first run visual studio code even if I set the python interpreter to my conda env, it never recognize streamlit or conda).

I have all dependencies installed and I get the following error

C:\Users\user\.conda\envs\streamlit\Lib\site-packages\pydub\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning) C:\Users\user\.conda\envs\streamlit\Lib\site-packages\pydub\utils.py:198: RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning) 2024-02-22 09:48:03.486 Uncaught app exception Traceback (most recent call last): File "C:\Users\user\.conda\envs\streamlit\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 535, in _run_script exec(code, module.__dict__) File "C:\Users\user\Documents\Python_projects\Streamlit_projects\Test1_Streamlit_App_v1\pages\2_๐Ÿš€_page 2.py", line 8, in <module> audio = audiorecorder("Click to record", "Click to stop recording") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\.conda\envs\streamlit\Lib\site-packages\audiorecorder\__init__.py", line 27, in audiorecorder audio_segment = AudioSegment.from_file(BytesIO(b64decode(base64_audio))) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\.conda\envs\streamlit\Lib\site-packages\pydub\audio_segment.py", line 728, in from_file info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\.conda\envs\streamlit\Lib\site-packages\pydub\utils.py", line 274, in mediainfo_json res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\.conda\envs\streamlit\Lib\subprocess.py", line 1026, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\user\.conda\envs\streamlit\Lib\subprocess.py", line 1538, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [WinError 2] The system cannot find the file specified

missing map file for bootstrap

Hi,

Thanks a lot for this package. I'm using it very often. One non minor issue that I started seeing recently is an error in the console that package is missing bootstrap.min.css.map. Could you please include it in the package. Or remove line reffering to it from bootstrap.min.css (/*# sourceMappingURL=bootstrap.min.css.map */).

Thanks

raise StreamlitAPIException(f"No such component directory: '{abspath}'")

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/data/disk1/ybZhang/voice-assistant-chatgpt/streamlit-audiorecorder/audiorecorder/__init__.py", line 17, in <module>
    _component_func = components.declare_component("audiorecorder", path=build_dir)
  File "/home/ybZhang/miniconda3/lib/python3.8/site-packages/streamlit-1.15.0-py3.8.egg/streamlit/components/v1/components.py", line 301, in declare_component
    ComponentRegistry.instance().register_component(component)
  File "/home/ybZhang/miniconda3/lib/python3.8/site-packages/streamlit-1.15.0-py3.8.egg/streamlit/components/v1/components.py", line 341, in register_component
    raise StreamlitAPIException(f"No such component directory: '{abspath}'")
streamlit.errors.StreamlitAPIException: No such component directory: '/data/disk1/ybZhang/voice-assistant-chatgpt/streamlit-audiorecorder/audiorecorder/frontend/build'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.