Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
from pydub import AudioSegment
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
def main():
|
| 10 |
+
st.title("Audio Recorder App")
|
| 11 |
+
|
| 12 |
+
# Add a description
|
| 13 |
+
st.write("Click the microphone button below to start recording")
|
| 14 |
+
|
| 15 |
+
# Create a placeholder for status messages
|
| 16 |
+
status_placeholder = st.empty()
|
| 17 |
+
|
| 18 |
+
# Add the audio recorder
|
| 19 |
+
audio_bytes = st.audio_recorder(
|
| 20 |
+
pause_threshold=2.0, # Automatically stops recording after 2 seconds of silence
|
| 21 |
+
sample_rate=44100
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
if audio_bytes:
|
| 25 |
+
try:
|
| 26 |
+
# Show success message
|
| 27 |
+
status_placeholder.success("Recording captured successfully!")
|
| 28 |
+
|
| 29 |
+
# Create a temporary file to store the audio
|
| 30 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
|
| 31 |
+
tmp_file.write(audio_bytes)
|
| 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 |
+
# Convert to AudioSegment for analysis
|
| 46 |
+
audio = AudioSegment.from_wav(tmp_path)
|
| 47 |
+
|
| 48 |
+
# Display audio information
|
| 49 |
+
st.subheader("Audio Information")
|
| 50 |
+
col1, col2, col3 = st.columns(3)
|
| 51 |
+
|
| 52 |
+
with col1:
|
| 53 |
+
st.metric("Duration", f"{len(audio)/1000:.2f} seconds")
|
| 54 |
+
with col2:
|
| 55 |
+
st.metric("Channels", audio.channels)
|
| 56 |
+
with col3:
|
| 57 |
+
st.metric("Sample Rate", f"{audio.frame_rate} Hz")
|
| 58 |
+
|
| 59 |
+
# Clean up temporary file
|
| 60 |
+
os.unlink(tmp_path)
|
| 61 |
+
|
| 62 |
+
# Add processing options
|
| 63 |
+
st.subheader("Audio Processing Options")
|
| 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 Normalized Audio",
|
| 70 |
+
data=normalized_bytes,
|
| 71 |
+
file_name="normalized_recording.wav",
|
| 72 |
+
mime="audio/wav"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 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. Click the microphone button to start recording
|
| 83 |
+
2. Speak into your microphone
|
| 84 |
+
3. Click the button again to stop recording
|
| 85 |
+
4. Your recording will appear above with playback controls
|
| 86 |
+
5. Use the download button to save your recording
|
| 87 |
+
6. Optional: Use the Normalize Audio button to improve audio quality
|
| 88 |
+
""")
|
| 89 |
+
|
| 90 |
+
# Add footer
|
| 91 |
+
st.markdown("---")
|
| 92 |
+
st.caption("Note: Recording quality depends on your microphone and browser settings")
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
main()
|