Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,108 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
from
|
| 7 |
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def main():
|
| 10 |
st.title("Audio Recorder App")
|
| 11 |
|
| 12 |
-
# Add
|
| 13 |
-
st.write("Click
|
| 14 |
|
| 15 |
# Create a placeholder for status messages
|
| 16 |
status_placeholder = st.empty()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
tmp_path = tmp_file.name
|
| 33 |
-
|
| 34 |
-
# Display the audio player
|
| 35 |
-
st.audio(audio_bytes, format='audio/wav')
|
| 36 |
-
|
| 37 |
-
# Add download button
|
| 38 |
-
st.download_button(
|
| 39 |
-
label="Download Recording",
|
| 40 |
-
data=audio_bytes,
|
| 41 |
-
file_name="recording.wav",
|
| 42 |
-
mime="audio/wav"
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
with col2:
|
| 55 |
-
st.metric("Channels", audio.channels)
|
| 56 |
-
with col3:
|
| 57 |
-
st.metric("Sample Rate", f"{audio.frame_rate} Hz")
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
|
| 62 |
-
# Add
|
| 63 |
-
|
| 64 |
-
if st.button("Normalize Audio"):
|
| 65 |
-
normalized_audio = audio.normalize()
|
| 66 |
-
normalized_bytes = normalized_audio.export(format='wav').read()
|
| 67 |
-
st.audio(normalized_bytes, format='audio/wav')
|
| 68 |
st.download_button(
|
| 69 |
-
label="Download
|
| 70 |
-
data=
|
| 71 |
-
file_name="
|
| 72 |
-
mime="
|
| 73 |
)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
status_placeholder.error(f"Error processing audio: {str(e)}")
|
| 77 |
-
st.stop()
|
| 78 |
|
| 79 |
# Add instructions
|
| 80 |
with st.expander("How to use"):
|
| 81 |
st.markdown("""
|
| 82 |
-
1.
|
| 83 |
-
2.
|
| 84 |
-
3.
|
| 85 |
-
4.
|
| 86 |
5. Use the download button to save your recording
|
| 87 |
-
|
|
|
|
| 88 |
""")
|
| 89 |
|
| 90 |
-
# Add
|
| 91 |
st.markdown("---")
|
| 92 |
-
st.caption("
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from streamlit_webrtc import webrtc_streamer, WebRtcMode
|
| 3 |
import numpy as np
|
| 4 |
+
import av
|
| 5 |
+
import queue
|
| 6 |
+
import threading
|
| 7 |
+
from typing import List, NamedTuple
|
| 8 |
import time
|
| 9 |
|
| 10 |
+
# Audio recording class
|
| 11 |
+
class AudioFrame(NamedTuple):
|
| 12 |
+
data: np.ndarray
|
| 13 |
+
timestamp: float
|
| 14 |
+
|
| 15 |
+
# Global variables for recording state
|
| 16 |
+
AUDIO_BUFFER: queue.Queue = queue.Queue()
|
| 17 |
+
RECORDING = False
|
| 18 |
+
AUDIO_FRAMES: List[AudioFrame] = []
|
| 19 |
+
|
| 20 |
def main():
|
| 21 |
st.title("Audio Recorder App")
|
| 22 |
|
| 23 |
+
# Add description
|
| 24 |
+
st.write("Click 'Start' to begin recording audio")
|
| 25 |
|
| 26 |
# Create a placeholder for status messages
|
| 27 |
status_placeholder = st.empty()
|
| 28 |
|
| 29 |
+
# Recording controls
|
| 30 |
+
col1, col2 = st.columns(2)
|
| 31 |
+
with col1:
|
| 32 |
+
start_button = st.button("Start Recording")
|
| 33 |
+
with col2:
|
| 34 |
+
stop_button = st.button("Stop Recording")
|
| 35 |
|
| 36 |
+
def audio_callback(frame: av.AudioFrame) -> av.AudioFrame:
|
| 37 |
+
global RECORDING, AUDIO_FRAMES
|
| 38 |
+
|
| 39 |
+
if RECORDING:
|
| 40 |
+
# Convert audio frame to numpy array
|
| 41 |
+
audio_data = frame.to_ndarray()
|
| 42 |
+
timestamp = time.time()
|
| 43 |
+
AUDIO_FRAMES.append(AudioFrame(audio_data, timestamp))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Update status
|
| 46 |
+
status_placeholder.info(f"Recording... Duration: {len(AUDIO_FRAMES) * 0.02:.1f}s")
|
| 47 |
+
|
| 48 |
+
return frame
|
| 49 |
+
|
| 50 |
+
# Initialize WebRTC component
|
| 51 |
+
ctx = webrtc_streamer(
|
| 52 |
+
key="audio-recorder",
|
| 53 |
+
mode=WebRtcMode.SENDONLY,
|
| 54 |
+
audio_receiver_size=256,
|
| 55 |
+
rtc_configuration={
|
| 56 |
+
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
|
| 57 |
+
},
|
| 58 |
+
media_stream_constraints={"video": False, "audio": True},
|
| 59 |
+
audio_callback=audio_callback,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Handle recording controls
|
| 63 |
+
if start_button:
|
| 64 |
+
RECORDING = True
|
| 65 |
+
AUDIO_FRAMES.clear()
|
| 66 |
+
status_placeholder.info("Starting recording...")
|
| 67 |
+
|
| 68 |
+
if stop_button:
|
| 69 |
+
RECORDING = False
|
| 70 |
+
if len(AUDIO_FRAMES) > 0:
|
| 71 |
+
# Combine all audio frames
|
| 72 |
+
all_audio = np.concatenate([frame.data for frame in AUDIO_FRAMES])
|
| 73 |
|
| 74 |
+
# Save as numpy array
|
| 75 |
+
np.save("recorded_audio.npy", all_audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# Show success message
|
| 78 |
+
status_placeholder.success(f"Recording saved! Duration: {len(AUDIO_FRAMES) * 0.02:.1f} seconds")
|
| 79 |
|
| 80 |
+
# Add download button for the numpy array
|
| 81 |
+
with open("recorded_audio.npy", "rb") as file:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
st.download_button(
|
| 83 |
+
label="Download Recording",
|
| 84 |
+
data=file,
|
| 85 |
+
file_name="recorded_audio.npy",
|
| 86 |
+
mime="application/octet-stream"
|
| 87 |
)
|
| 88 |
+
else:
|
| 89 |
+
status_placeholder.warning("No audio recorded")
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# Add instructions
|
| 92 |
with st.expander("How to use"):
|
| 93 |
st.markdown("""
|
| 94 |
+
1. Allow microphone access when prompted
|
| 95 |
+
2. Click 'Start Recording' to begin
|
| 96 |
+
3. Speak into your microphone
|
| 97 |
+
4. Click 'Stop Recording' when finished
|
| 98 |
5. Use the download button to save your recording
|
| 99 |
+
|
| 100 |
+
Note: You may need to click 'Start' in the WebRTC player above first.
|
| 101 |
""")
|
| 102 |
|
| 103 |
+
# Add requirements info
|
| 104 |
st.markdown("---")
|
| 105 |
+
st.caption("Required packages: streamlit-webrtc, numpy, av")
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
main()
|