|
import gradio as gr |
|
import soundfile as sf |
|
import datetime |
|
import numpy as np |
|
|
|
|
|
|
|
def save_audio(audio, label, sample_rate, bit_depth): |
|
|
|
filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{label}_{sample_rate}Hz_{bit_depth}bit.wav" |
|
|
|
|
|
audio_data = np.array(audio[1]) |
|
|
|
|
|
sf.write(filename, audio_data, int(sample_rate), subtype=f'PCM_{bit_depth}') |
|
|
|
return filename |
|
|
|
|
|
|
|
def create_interface(): |
|
sample_rate = gr.Dropdown(["44100", "48000"], label="選擇採樣率", value="44100") |
|
bit_depth = gr.Dropdown(["16", "24"], label="選擇位深度", value="16") |
|
label = gr.Textbox(label="輸入標記") |
|
audio = gr.Audio(sources="microphone", type="numpy", label="錄音", show_download_button=True,) |
|
|
|
interface = gr.Interface( |
|
fn=save_audio, |
|
inputs=[audio, label, sample_rate, bit_depth], |
|
outputs=["text"], |
|
title="錄音程式", |
|
description="錄音最大長度為 1 秒,儲存為 WAV 格式。", |
|
) |
|
return interface |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
interface = create_interface() |
|
interface.launch() |
|
|