File size: 1,190 Bytes
f586543
270fe9f
f586543
7fffcc0
270fe9f
f586543
 
 
270fe9f
7fffcc0
 
 
447cda5
7fffcc0
 
 
 
270fe9f
447cda5
 
 
f586543
447cda5
f586543
270fe9f
f586543
270fe9f
f586543
 
 
7fffcc0
f586543
447cda5
 
270fe9f
 
f586543
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
import gradio as gr
import torch
import librosa
import numpy as np

def audio_to_pth(audio):
    # 音声ファイル(ファイルパス)を読み込む
    y, sr = librosa.load(audio, sr=None)

    # メルスペクトログラムに変換
    mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)

    # メルスペクトログラムを対数スケールに変換
    mel_spectrogram_db = librosa.power_to_db(mel_spectrogram, ref=np.max)

    # メルスペクトログラムをテンソルに変換
    tensor = torch.tensor(mel_spectrogram_db)

    # テンソルを5次元に変換
    tensor = tensor.unsqueeze(0).unsqueeze(0).unsqueeze(0)  # 5次元に拡張

    # テンソルを .pth ファイルに保存
    output_path = "audio_features_5d.pth"
    torch.save(tensor, output_path)

    return output_path

# Gradio インターフェースの設定
iface = gr.Interface(
    fn=audio_to_pth,
    inputs=gr.Audio(type="filepath"),
    outputs="file",
    title="Audio to 5D Tensor .PTH Converter",
    description="Upload an audio file to convert it to a .pth file containing a 5D tensor with audio features in mel spectrogram format."
)

iface.launch()