Skip to main content

Streamlit component that allows to record mono audio from the user's microphone, and/or perform speech recognition directly.

Project description

streamlit-mic-recorder

Streamlit component that allows to record mono audio from the user's microphone, and/or perform speech recognition directly.

Installation instructions

$ pip install streamlit-mic-recorder

Usage instructions

Two functions are provided (with the same front-end):

audio=mic_recorder(
    start_prompt="Start recording",
    stop_prompt="Stop recording", 
    just_once=False,
    use_container_width=False,
    callback=None,
    args=(),
    kwargs={},
    key=None
)

Renders a button. Click to start recording, click to stop. Returns None or a dictionary with the following structure:

{
    "bytes":audio_bytes, # wav audio bytes mono signal, can be processed directly by st.audio
    "sample_rate":sample_rate, # depends on your browser's audio configuration
    "sample_width":sample_width, # 2
    "id": id # A unique timestamp identifier of the audio
}

sample_rate and sample_width are provided in case you need them for further audio processing.

Arguments:

  • 'start/stop_prompt', the prompts appearing on the button depending on its recording state.
  • 'just_once' determines if the widget returns the audio only once just after it has been recorded (and then None), or on every rerun of the app. Useful to avoid reprocessing the same audio twice.
  • 'use_container_width' just like for st.button, determines if the button fills its container width or not.
  • 'callback': an optional callback being called when a new audio is received
  • 'args/kwargs': optional args and kwargs passed to the callback when triggered

Remark: When using a key for the widget, due to how streamlit's component API works, the associated state variable will only contain the raw unprocessed output from the React frontend, which was not very practical. For convenience I added a special state variable to be able to access the output in the expected format (the dictionary described above) more easily. If key is the key you gave to the widget, you can acces the properly formatted output via key+'_output' in the session state. Here is an example on how it can be used within a callback:

def callback()
    if st.session_state.my_recorder_output:
        audio_bytes=st.session_state.my_recorder_output['bytes']
        st.audio(audio_bytes)

mic_recorder(key='my_recorder', callback=callback)
text=speech_to_text(
    language='en',
    start_prompt="Start recording",
    stop_prompt="Stop recording", 
    just_once=False,
    use_container_width=False,
    callback=None,
    args=(),
    kwargs={},
    key=None
)

Renders a button. Click to start recording, click to stop. Returns None or a text transcription of the recorded speech in the chosen language. Similarly to the mic_recorder function, you can pass a callback that will trigger when a new text transcription is received, and access this transcription directly in the session state by adding an '_output' suffix to the key you chose for the widget.

def callback()
    if st.session_state.my_stt_output:
        st.write(st.session_state.my_stt_output)

speech_to_text(key='my_stt', callback=callback)

Example

import streamlit as st
from streamlit_mic_recorder import mic_recorder,speech_to_text

state=st.session_state

if 'text_received' not in state:
    state.text_received=[]

c1,c2=st.columns(2)
with c1:
    st.write("Convert speech to text:")
with c2:
    text=speech_to_text(language='en',use_container_width=True,just_once=True,key='STT')

if text:       
    state.text_received.append(text)

for text in state.text_received:
    st.text(text)

st.write("Record your voice, and play the recorded audio:")
audio=mic_recorder(start_prompt="⏺️",stop_prompt="⏹️",key='recorder')

if audio:       
    st.audio(audio['bytes'])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_mic_recorder-0.0.4.tar.gz (471.9 kB view details)

Uploaded Source

Built Distribution

streamlit_mic_recorder-0.0.4-py3-none-any.whl (475.6 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_mic_recorder-0.0.4.tar.gz.

File metadata

  • Download URL: streamlit_mic_recorder-0.0.4.tar.gz
  • Upload date:
  • Size: 471.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for streamlit_mic_recorder-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a723e3ed1463c4991bc6098f28fc482481a9973f9c754485cc95b48cb21e86c7
MD5 6b0f0a2c4bcc5dcf0bfea53877b9269c
BLAKE2b-256 55c30e36343885302b90175134eedd13428726c2600ed45d242b1e5ff03cdb5a

See more details on using hashes here.

File details

Details for the file streamlit_mic_recorder-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_mic_recorder-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8167523d1b10712a911659a51fd1cedf39ea112af10e5db267679f0734ee59a1
MD5 b15addbd7aa3a060136c67fb9f2035a2
BLAKE2b-256 f542cfb479db7dcd00af9e093899a96b24915662d5dd917203458284abb1c381

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page