File size: 1,234 Bytes
df17f59 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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"
# 將音頻數據轉換成 numpy 陣列
audio_data = np.array(audio[1])
# 將錄音儲存為 wav 檔案
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()
|