BHW commited on
Commit
df17f59
·
verified ·
1 Parent(s): 949207c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import soundfile as sf
3
+ import datetime
4
+ import numpy as np
5
+
6
+
7
+ # 錄音函數
8
+ def save_audio(audio, label, sample_rate, bit_depth):
9
+ # 產生檔案名稱
10
+ filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{label}_{sample_rate}Hz_{bit_depth}bit.wav"
11
+
12
+ # 將音頻數據轉換成 numpy 陣列
13
+ audio_data = np.array(audio[1])
14
+
15
+ # 將錄音儲存為 wav 檔案
16
+ sf.write(filename, audio_data, int(sample_rate), subtype=f'PCM_{bit_depth}')
17
+
18
+ return filename
19
+
20
+
21
+ # 介面設計
22
+ def create_interface():
23
+ sample_rate = gr.Dropdown(["44100", "48000"], label="選擇採樣率", value="44100")
24
+ bit_depth = gr.Dropdown(["16", "24"], label="選擇位深度", value="16")
25
+ label = gr.Textbox(label="輸入標記")
26
+ audio = gr.Audio(sources="microphone", type="numpy", label="錄音", show_download_button=True,)
27
+
28
+ interface = gr.Interface(
29
+ fn=save_audio,
30
+ inputs=[audio, label, sample_rate, bit_depth],
31
+ outputs=["text"],
32
+ title="錄音程式",
33
+ description="錄音最大長度為 1 秒,儲存為 WAV 格式。",
34
+ )
35
+ return interface
36
+
37
+
38
+ # 啟動介面
39
+ if __name__ == "__main__":
40
+ interface = create_interface()
41
+ interface.launch()