-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaugmentations.py
31 lines (22 loc) · 889 Bytes
/
augmentations.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
import torch
from torch import nn
import torchaudio
youtube_noise, _ = torchaudio.load('Cafe sounds ~ Ambient noise-i9a6ReFTHiw.wav')
youtube_noise = youtube_noise.sum(dim=0)
class GaussianNoise(nn.Module):
def __init__(self, mean=0, std=0.05):
super(GaussianNoise, self).__init__()
self.noiser = torch.distributions.Normal(mean, std)
def forward(self, wav):
wav = wav + self.noiser.sample(wav.size())
wav = wav.clamp(-1, 1)
return wav
class YoutubeNoise(nn.Module):
def __init__(self, alpha=0.05):
super(YoutubeNoise, self).__init__()
self.alpha = alpha
self.noise_wav = youtube_noise
def forward(self, wav):
wav = wav + self.alpha * self.noise_wav[:wav.shape[-1]]
wav = wav.clamp(-1, 1)
return wav