KeyWave / app.py
BHW's picture
Create app.py
df17f59 verified
raw
history blame
1.23 kB
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()