2021-08-05 11:57:04 +00:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import torch.utils.data
|
2021-08-06 04:14:49 +00:00
|
|
|
import torchaudio
|
2021-08-05 11:57:04 +00:00
|
|
|
from tqdm import tqdm
|
|
|
|
|
2021-08-06 04:14:49 +00:00
|
|
|
from data.audio.wav_aug import WavAugmentor
|
2021-08-09 05:23:13 +00:00
|
|
|
from data.util import find_files_of_type, is_wav_file
|
2021-08-05 11:57:04 +00:00
|
|
|
from models.tacotron2.taco_utils import load_wav_to_torch
|
2021-08-06 04:14:49 +00:00
|
|
|
from utils.util import opt_get
|
2021-08-05 11:57:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WavfileDataset(torch.utils.data.Dataset):
|
|
|
|
|
|
|
|
def __init__(self, opt):
|
|
|
|
self.path = os.path.dirname(opt['path'])
|
|
|
|
cache_path = os.path.join(self.path, 'cache.pth')
|
|
|
|
if os.path.exists(cache_path):
|
|
|
|
self.audiopaths = torch.load(cache_path)
|
|
|
|
else:
|
|
|
|
print("Building cache..")
|
2021-08-09 05:23:13 +00:00
|
|
|
self.audiopaths = find_files_of_type('img', opt['path'], qualifier=is_wav_file)[0]
|
2021-08-05 11:57:04 +00:00
|
|
|
torch.save(self.audiopaths, cache_path)
|
2021-08-06 04:14:49 +00:00
|
|
|
|
|
|
|
# Parse options
|
|
|
|
self.sampling_rate = opt_get(opt, ['sampling_rate'], 24000)
|
|
|
|
self.augment = opt_get(opt, ['do_augmentation'], False)
|
2021-08-05 11:57:04 +00:00
|
|
|
self.max_wav_value = 32768.0
|
2021-08-06 04:14:49 +00:00
|
|
|
|
2021-08-05 11:57:04 +00:00
|
|
|
self.window = 2 * self.sampling_rate
|
2021-08-06 04:14:49 +00:00
|
|
|
if self.augment:
|
|
|
|
self.augmentor = WavAugmentor()
|
2021-08-05 11:57:04 +00:00
|
|
|
|
|
|
|
def get_audio_for_index(self, index):
|
|
|
|
audiopath = self.audiopaths[index]
|
|
|
|
filename = os.path.join(self.path, audiopath)
|
|
|
|
audio, sampling_rate = load_wav_to_torch(filename)
|
|
|
|
if sampling_rate != self.sampling_rate:
|
|
|
|
raise ValueError(f"Input sampling rate does not match specified rate {self.sampling_rate}")
|
|
|
|
audio_norm = audio / self.max_wav_value
|
2021-08-06 05:12:59 +00:00
|
|
|
audio_norm = audio_norm.unsqueeze(0)
|
2021-08-05 11:57:04 +00:00
|
|
|
return audio_norm, audiopath
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
clip1, clip2 = None, None
|
|
|
|
|
|
|
|
while clip1 is None and clip2 is None:
|
|
|
|
# Split audio_norm into two tensors of equal size.
|
|
|
|
audio_norm, filename = self.get_audio_for_index(index)
|
2021-08-06 05:12:59 +00:00
|
|
|
if audio_norm.shape[1] < self.window * 2:
|
2021-08-05 11:57:04 +00:00
|
|
|
# Try next index. This adds a bit of bias and ideally we'd filter the dataset rather than do this.
|
|
|
|
index = (index + 1) % len(self)
|
|
|
|
continue
|
2021-08-06 05:12:59 +00:00
|
|
|
j = random.randint(0, audio_norm.shape[1] - self.window)
|
|
|
|
clip1 = audio_norm[:, j:j+self.window]
|
2021-08-06 04:14:49 +00:00
|
|
|
if self.augment:
|
|
|
|
clip1 = self.augmentor.augment(clip1, self.sampling_rate)
|
2021-08-06 05:12:59 +00:00
|
|
|
j = random.randint(0, audio_norm.shape[1]-self.window)
|
|
|
|
clip2 = audio_norm[:, j:j+self.window]
|
2021-08-06 04:14:49 +00:00
|
|
|
if self.augment:
|
|
|
|
clip2 = self.augmentor.augment(clip2, self.sampling_rate)
|
2021-08-05 11:57:04 +00:00
|
|
|
|
|
|
|
return {
|
2021-08-06 05:22:44 +00:00
|
|
|
'clip1': clip1[0, :].unsqueeze(0),
|
|
|
|
'clip2': clip2[0, :].unsqueeze(0),
|
2021-08-05 11:57:04 +00:00
|
|
|
'path': filename,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.audiopaths)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
params = {
|
|
|
|
'mode': 'wavfile_clips',
|
|
|
|
'path': 'E:\\audio\\LibriTTS\\train-other-500',
|
|
|
|
'phase': 'train',
|
|
|
|
'n_workers': 0,
|
|
|
|
'batch_size': 16,
|
2021-08-06 04:14:49 +00:00
|
|
|
'do_augmentation': True,
|
2021-08-05 11:57:04 +00:00
|
|
|
}
|
|
|
|
from data import create_dataset, create_dataloader, util
|
|
|
|
|
2021-08-06 05:22:44 +00:00
|
|
|
ds = create_dataset(params, return_collate=True)
|
2021-08-05 11:57:04 +00:00
|
|
|
dl = create_dataloader(ds, params, collate_fn=c)
|
|
|
|
i = 0
|
|
|
|
for b in tqdm(dl):
|
2021-08-06 05:22:44 +00:00
|
|
|
for b_ in range(16):
|
|
|
|
torchaudio.save(f'{i}_clip1_{b_}.wav', b['clip1'][b_], ds.sampling_rate)
|
|
|
|
torchaudio.save(f'{i}_clip2_{b_}.wav', b['clip2'][b_], ds.sampling_rate)
|
|
|
|
i += 1
|