Skip to content

Commit

Permalink
V0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
theevann committed Apr 29, 2024
1 parent 0c7c60e commit e8479c9
Show file tree
Hide file tree
Showing 10 changed files with 3,556 additions and 1,938 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ __pycache__/
node_modules/
build/
dist/
*.egg-info/
*.egg-info/
.DS_Store
.vscode/
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ 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](images/buttons.gif)

If all prompts are given as empty strings, the component will use the [react-audio-recorder](https://github.com/samhirtarif/react-audio-recorder) visualizer:

![Example with the visualiser](images/visualiser.gif)

#### Parameters
The signature of the component is:
```python
audiorecorder(start_prompt="Start recording", stop_prompt="Stop recording", pause_prompt="", key=None):
audiorecorder(start_prompt="Start recording", stop_prompt="Stop recording", pause_prompt="", show_visualizer=True, key=None):
```
The prompt parameters are self-explanatory, and the optionnal `key` parameter is used internally by streamlit to properly distinguish multiple audiorecorders on the page.
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](https://github.com/jiaaro/pydub/) [`AudioSegment`](https://github.com/jiaaro/pydub/blob/master/API.markdown#audiosegment).
All `AudioSegment` methods are available, in particular you can:
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")`

Expand All @@ -27,8 +35,8 @@ 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`
On Ubuntu/Debian: `sudo apt update && sudo apt install ffmpeg`
On Mac: `brew install ffmpeg`

### Usage:
```python
Expand Down
17 changes: 9 additions & 8 deletions audiorecorder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@


_RELEASE = True
_LOAD_LOCAL = False


if not _RELEASE:
if _LOAD_LOCAL and not _RELEASE:
_component_func = components.declare_component(
"audiorecorder",
url="http://localhost:3001",
Expand All @@ -20,33 +21,33 @@
_component_func = components.declare_component("audiorecorder", path=build_dir)


def audiorecorder(start_prompt="Start recording", stop_prompt="Stop recording", pause_prompt="", show_visualizer=False, key=None) -> AudioSegment:
def audiorecorder(start_prompt="Start recording", stop_prompt="Stop recording", pause_prompt="", show_visualizer=True, key=None) -> AudioSegment:
base64_audio = _component_func(start_prompt=start_prompt, stop_prompt=stop_prompt, pause_prompt=pause_prompt, show_visualizer=show_visualizer, key=key, default=b"")
audio_segment = AudioSegment.empty()
if len(base64_audio) > 0:
# Firefox and Chrome handle webm but Safari doesn't, so we let pydub/ffmpeg figure out the format
audio_segment = AudioSegment.from_file(BytesIO(b64decode(base64_audio)))
# audio_segment = AudioSegment.from_file(BytesIO(b64decode(base64_audio)), format="webm")
audio_segment = AudioSegment.from_file(BytesIO(b64decode(base64_audio)))
return audio_segment


if not _RELEASE:
import streamlit as st

st.subheader("Audio Recorder Test")
audio_1 = audiorecorder("Click to record", "Click to stop recording", "Click to pause", key="audio_1")
audio_2 = audiorecorder("", "" , show_visualizer=True, key="audio_2")
audio_1 = audiorecorder("Click to record", "Click to stop recording", "Click to pause", key="audio_1"), st.container()
audio_2 = audiorecorder("", "" , show_visualizer=True, key="audio_2"), st.container()

for audio in [audio_1, audio_2]:
for audio, container in [audio_1, audio_2]:
if len(audio) > 0:
# To play the audio in the frontend
st.audio(audio.export().read())
container.audio(audio.export().read())

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

# To save the audio
# audio.export("audio.wav", format="wav")
Loading

0 comments on commit e8479c9

Please sign in to comment.