-
Notifications
You must be signed in to change notification settings - Fork 57
/
preprocess.py
88 lines (65 loc) · 2.39 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
80
81
82
83
84
85
86
87
88
import yaml
import pathlib
import librosa as li
from ddsp.core import extract_loudness, extract_pitch
from effortless_config import Config
import numpy as np
from tqdm import tqdm
import numpy as np
from os import makedirs, path
import torch
from scipy.io import wavfile
def get_files(data_location, extension, **kwargs):
return list(pathlib.Path(data_location).rglob(f"*.{extension}"))
def preprocess(f, sampling_rate, block_size, signal_length, oneshot, **kwargs):
x, sr = li.load(f, sampling_rate)
N = (signal_length - len(x) % signal_length) % signal_length
x = np.pad(x, (0, N))
if oneshot:
x = x[..., :signal_length]
pitch = extract_pitch(x, sampling_rate, block_size)
loudness = extract_loudness(x, sampling_rate, block_size)
x = x.reshape(-1, signal_length)
pitch = pitch.reshape(x.shape[0], -1)
loudness = loudness.reshape(x.shape[0], -1)
return x, pitch, loudness
class Dataset(torch.utils.data.Dataset):
def __init__(self, out_dir):
super().__init__()
self.signals = np.load(path.join(out_dir, "signals.npy"))
self.pitchs = np.load(path.join(out_dir, "pitchs.npy"))
self.loudness = np.load(path.join(out_dir, "loudness.npy"))
def __len__(self):
return self.signals.shape[0]
def __getitem__(self, idx):
s = torch.from_numpy(self.signals[idx])
p = torch.from_numpy(self.pitchs[idx])
l = torch.from_numpy(self.loudness[idx])
return s, p, l
def main():
class args(Config):
CONFIG = "config.yaml"
args.parse_args()
with open(args.CONFIG, "r") as config:
config = yaml.safe_load(config)
files = get_files(**config["data"])
pb = tqdm(files)
signals = []
pitchs = []
loudness = []
for f in pb:
pb.set_description(str(f))
x, p, l = preprocess(f, **config["preprocess"])
signals.append(x)
pitchs.append(p)
loudness.append(l)
signals = np.concatenate(signals, 0).astype(np.float32)
pitchs = np.concatenate(pitchs, 0).astype(np.float32)
loudness = np.concatenate(loudness, 0).astype(np.float32)
out_dir = config["preprocess"]["out_dir"]
makedirs(out_dir, exist_ok=True)
np.save(path.join(out_dir, "signals.npy"), signals)
np.save(path.join(out_dir, "pitchs.npy"), pitchs)
np.save(path.join(out_dir, "loudness.npy"), loudness)
if __name__ == "__main__":
main()