File size: 2,858 Bytes
218a4f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Training data pipeline shared by model.py and load_balanced_model.py.

Simulates a low-resolution input for a random target sample rate: lowpass
filter the 48kHz ground truth, downsample, then cubic-spline upsample it
back to 48kHz so the generator sees fixed-size input/target pairs.
"""
import os
import random

import librosa
import torch
import torch.nn.functional as F
import torchaudio
import torchaudio.transforms as T
from ssr_eval.lowpass import lowpass_filter

from layers import cubic_upsampler

FILTER_TYPES = ["butter", "cheby1", "ellip", "bessel"]
INPUT_SAMPLE_RATES = [8000, 16000, 24000]
LOWPASS_FILTER_ORDER = 8


def _get_flist(folder):
    wav_files = []
    for root, dirs, files in os.walk(folder):
        for filename in files:
            if filename.endswith(('.wav', '.flac', '.mp4')):
                wav_files.append(os.path.join(root, filename))
    return wav_files


class VoiceDataset(torch.utils.data.Dataset):
    def __init__(self, root: str, segment_length: int, target_sample_rate: int) -> None:
        self._flist = _get_flist(root)
        self.segment_length = segment_length
        self.target_sample_rate = target_sample_rate

    def __getitem__(self, n: int):
        x, sr = torchaudio.load(self._flist[n])
        if sr > self.target_sample_rate:
            resample_transform_x = T.Resample(orig_freq=sr, new_freq=self.target_sample_rate)
            x = resample_transform_x(x)
        if x.size()[0] == 2:
            x = x[random.randint(0, 1)].unsqueeze(dim=0)
        assert x.shape[0] == 1
        x = torch.squeeze(x)
        assert x.dim() == 1
        if x.shape[0] < self.segment_length:
            x = F.pad(x, [0, self.segment_length - x.shape[0]], "constant")
        pos = random.randint(0, x.shape[0] - self.segment_length)
        x = x[pos:pos + self.segment_length]

        freq = random.choice(INPUT_SAMPLE_RATES)
        cutoff = freq * 0.5
        z = lowpass_filter(x, cutoff, self.target_sample_rate, LOWPASS_FILTER_ORDER, ftype=random.choice(FILTER_TYPES))
        z = librosa.resample(z, orig_sr=self.target_sample_rate, target_sr=freq)

        v = torch.tensor(z.copy(), dtype=torch.float32)
        v_ = cubic_upsampler(v, freq, self.target_sample_rate)
        y = torch.tensor(v_, dtype=torch.float32)
        return [x, y]

    def __len__(self) -> int:
        return len(self._flist)


def _collate(examples):
    x_stack = torch.stack([example[0] for example in examples])
    y_stack = torch.stack([example[1] for example in examples])
    return [x_stack, y_stack]


def make_dataloader(root: str, segment_length: int, target_sample_rate: int, batch_size: int) -> torch.utils.data.DataLoader:
    ds = VoiceDataset(root, segment_length, target_sample_rate)
    return torch.utils.data.DataLoader(
        ds, batch_size=batch_size, shuffle=True, collate_fn=_collate)