-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocess.py
79 lines (63 loc) · 2.03 KB
/
preprocess.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Preprocess mel-spectrogram of size (80, 128) from VCC 2020 dataset
"""
import numpy as np
import os
import glob
import random
from sklearn.model_selection import train_test_split
from utils import Audio
def get_wav_paths(dirs):
wav_paths = {}
for dir in dirs:
for spk in os.listdir(dir):
if '.' in spk: continue
wav_paths[spk] = glob.glob(os.path.join(dir, spk, '*.wav'))
return wav_paths
def load_wav_audio(paths):
data = {}
mels = {}
audio = Audio()
for spk in paths:
print('loading:', spk)
data[spk] = []
mels[spk] = []
for path in paths[spk]:
mel = audio.audio_to_mel(path)
mels[spk].append(mel)
samples = audio.mel_sample(mel, width=128, k=5)
if samples is not None:
data[spk].append(samples)
data[spk] = np.concatenate(data[spk], axis=0)
return data, mels
def create_dataset(data):
os.makedirs('./data/train', exist_ok=True)
os.makedirs('./data/test', exist_ok=True)
for spk in data:
train, test = train_test_split(data[spk], test_size=0.1, random_state=101)
np.save(f'./data/train/{spk}.npy', train)
np.save(f'./data/test/{spk}.npy', test)
def save_embeddings(mels):
os.makedirs('./embeddings/', exist_ok=True)
audio = Audio()
for spk in mels:
avg_embed = np.zeros(256, dtype=np.float32)
for mel in mels[spk][:5]:
embed = audio.mel_to_embed(mel)
avg_embed += embed
avg_embed = avg_embed / 5
np.save(f'./embeddings/{spk}.npy', avg_embed)
if __name__ == "__main__":
# 1. get data paths
data_dirs = [
'/home/jirayu.b/sing-me-light/data/source',
'/home/jirayu.b/sing-me-light/data/target_task1',
'/home/jirayu.b/sing-me-light/data/target_task2'
]
paths = get_wav_paths(data_dirs)
# 2. load wav audio
data, mels = load_wav_audio(paths)
# 3. create dataset
create_dataset(data)
# 4. save embeddings
save_embeddings(mels)