File size: 893 Bytes
faf557e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np

def handler(in1):
    if in1 is None:
        msg='請先上傳或錄音!'
    try:
        sample_rate, audio_data=in1      
        duration=len(audio_data)/sample_rate # 計算音訊長度(秒)
        avg_amplitude=np.mean(np.abs(audio_data)) # 計算音訊平均振幅
        msg=(f'音訊類型: {type(in1)}\n'
             f'音訊資料: {in1}\n'
             f'取樣率: {sample_rate}\n'
             f'時長: {duration:.2f} 秒\n'
             f'平均振幅: {avg_amplitude:.4f}'
             )
    except Exception as e:
        msg=f'處理音訊時發生錯誤:{e}'
    return msg

in1=gr.Audio(label='音訊輸入')  # 預設 type='numpy'
out1=gr.Textbox(label='輸出資訊')
iface=gr.Interface(
    fn=handler,
    inputs=[in1], 
    outputs=out1,
    title='Audio 元件測試',
    flagging_mode='never',
    )
iface.launch()