Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| import scipy.io.wavfile as wavfile | |
| from datetime import datetime | |
| def process_audio(sample_rate, audio_data): | |
| """Resample to 16kHz and save as .wav file""" | |
| if audio_data is None: | |
| return "No audio recorded", None | |
| target_sample_rate = 16000 | |
| # Resample if needed | |
| if sample_rate != target_sample_rate: | |
| duration = len(audio_data) / sample_rate | |
| num_samples = int(duration * target_sample_rate) | |
| audio_data = np.interp( | |
| np.linspace(0, len(audio_data), num_samples), | |
| np.arange(len(audio_data)), | |
| audio_data | |
| ) | |
| audio_data = audio_data.astype(np.float32) | |
| # Save to file with timestamp | |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| output_path = f"recording_{timestamp}.wav" | |
| audio_int16 = (audio_data * 32767).astype(np.int16) | |
| wavfile.write(output_path, target_sample_rate, audio_int16) | |
| status = f"Saved: {output_path} (16kHz, {len(audio_int16)/target_sample_rate:.2f}s)" | |
| return status, output_path | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Voice Recorder") | |
| gr.Markdown("Record audio from microphone and save as 16kHz .wav file") | |
| audio_input = gr.Audio(sources=["microphone"], type="numpy") | |
| record_btn = gr.Button("Record") | |
| output_text = gr.Textbox(label="Status") | |
| output_file = gr.File(label="Download Recording", visible=True) | |
| record_btn.click( | |
| fn=lambda x: process_audio(16000, x) if x is not None else ("No audio", None), | |
| inputs=[audio_input], | |
| outputs=[output_text, output_file] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |