Spaces:
Sleeping
Sleeping
Ntabukiraniro
commited on
Commit
•
dea72bb
1
Parent(s):
3ef5d7a
Upload 2 files
Browse files- __init__.py +1 -0
- st_custom_components.py +33 -0
__init__.py
CHANGED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
st_custom_components.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import streamlit as st
|
4 |
+
from io import BytesIO
|
5 |
+
import streamlit.components.v1 as components
|
6 |
+
|
7 |
+
def st_audiorec():
|
8 |
+
|
9 |
+
# get parent directory relative to current directory
|
10 |
+
parent_dir = os.path.dirname(os.path.abspath(__file__))
|
11 |
+
# Custom REACT-based component for recording client audio in browser
|
12 |
+
build_dir = os.path.join(parent_dir, "frontend/build")
|
13 |
+
# specify directory and initialize st_audiorec object functionality
|
14 |
+
st_audiorec = components.declare_component("st_audiorec", path=build_dir)
|
15 |
+
|
16 |
+
# Create an instance of the component: STREAMLIT AUDIO RECORDER
|
17 |
+
raw_audio_data = st_audiorec() # raw_audio_data: stores all the data returned from the streamlit frontend
|
18 |
+
wav_bytes = None # wav_bytes: contains the recorded audio in .WAV format after conversion
|
19 |
+
|
20 |
+
# the frontend returns raw audio data in the form of arraybuffer
|
21 |
+
# (this arraybuffer is derived from web-media API WAV-blob data)
|
22 |
+
|
23 |
+
if isinstance(raw_audio_data, dict): # retrieve audio data
|
24 |
+
with st.spinner('retrieving audio-recording...'):
|
25 |
+
ind, raw_audio_data = zip(*raw_audio_data['arr'].items())
|
26 |
+
ind = np.array(ind, dtype=int) # convert to np array
|
27 |
+
raw_audio_data = np.array(raw_audio_data) # convert to np array
|
28 |
+
sorted_ints = raw_audio_data[ind]
|
29 |
+
stream = BytesIO(b"".join([int(v).to_bytes(1, "big") for v in sorted_ints]))
|
30 |
+
# wav_bytes contains audio data in byte format, ready to be processed further
|
31 |
+
wav_bytes = stream.read()
|
32 |
+
|
33 |
+
return wav_bytes
|