Spaces:
Build error
Build error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,108 +0,0 @@
|
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|