-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathaugment.py
197 lines (136 loc) · 7.36 KB
/
augment.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Author: Francois Grondin
# Date: October 19, 2020
# Affiliation: Universite de Sherbrooke
# Contact: [email protected]
from mix import MIX
from bird import BIRD
import torchaudio
import torch
class SSL:
# This dataset uses a room impulse response and a speech datasets, to create augmented
# data to perform sound source localization.
#
# rir Room impulse response dataset (e.g. BIRD).
# speech Speech dataset (e.g. LibriSpeech).
# samples_count Number of augmented segments to generate.
def __init__(self, rir, speech, samples_count):
self.mix = MIX(rir=rir, speech=speech, count=[2,2], duration=80000, samples_count=samples_count)
# Return the number of samples.
def __len__(self):
return len(self.mix)
# Return the item at index idx. This returns the STFTs of microphones 1 and 2, and the
# TDOAs of sources 1 and 2
def __getitem__(self, idx):
ys, meta, _, _ = self.mix[idx]
ys = torch.from_numpy(ys)
Ys = torchaudio.functional.spectrogram(waveform=torch.transpose(ys, 0, 1),
pad=0,
window=torch.hann_window(400),
n_fft=512,
hop_length=256,
win_length=400,
power=None,
normalized=False)
taus = BIRD.getTDOA(meta)
taus = torch.tensor(taus[0:2])
return Ys, taus
class RT60:
# This dataset uses a room impulse response and a speech datasets, to create augmented
# data to estimate the reverberation time RT60.
#
# rir Room impulse response dataset (e.g. BIRD).
# speech Speech dataset (e.g. LibriSpeech).
# samples_count Number of augmented segments to generate.
def __init__(self, rir, speech, samples_count):
self.mix = MIX(rir=rir, speech=speech, count=[1,1], duration=80000, samples_count=samples_count)
# Return the number of samples.
def __len__(self):
return len(self.mix)
# Return the item at index idx. This returns the STFTs of microphones 1 and 2, and the
# RT60 value for the room
def __getitem__(self, idx):
ys, meta, _, _ = self.mix[idx]
ys = torch.from_numpy(ys)
Ys = torchaudio.functional.spectrogram(waveform=torch.transpose(ys, 0, 1),
pad=0,
window=torch.hann_window(400),
n_fft=512,
hop_length=256,
win_length=400,
power=None,
normalized=False)
rt60 = BIRD.getRT60(meta)
return Ys, rt60
class CNT:
# This dataset uses a room impulse response and a speech datasets, to create augmented
# data to count the number of sources (between 1 and 4).
#
# rir Room impulse response dataset (e.g. BIRD)
# speech Speech dataset (e.g. LibriSpeech)
# samples_count Number of augmented segments to generate
def __init__(self, rir, speech, samples_count):
self.mix = MIX(rir=rir, speech=speech, count=[1,4], duration=80000, samples_count=samples_count)
# Return the number of samples.
def __len__(self):
return len(self.mix)
# Return the item at index idx. This returns the STFTs of microphones 1 and 2, and the
# number of active sources (between 1 and 4)
def __getitem__(self, idx):
ys, _, count, _ = self.mix[idx]
ys = torch.from_numpy(ys)
Ys = torchaudio.functional.spectrogram(waveform=torch.transpose(ys, 0, 1),
pad=0,
window=torch.hann_window(400),
n_fft=512,
hop_length=256,
win_length=400,
power=None,
normalized=False)
return Ys, count
class IRM:
# This dataset uses a room impulse response and a speech datasets, to create augmented
# data to estimate an ideal ratio mask for the target sound source.
#
# rir Room impulse response dataset (e.g. BIRD)
# speech Speech dataset (e.g. LibriSpeech)
# samples_count Number of augmented segments to generate
def __init__(self, rir, speech, samples_count):
self.mix = MIX(rir=rir, speech=speech, count=[2,2], duration=80000, samples_count=samples_count)
# Return the number of samples.
def __len__(self):
return len(self.mix)
# Return the item at index idx. This returns the STFTs of microphones 1 and 2, the
# ideal ratio masks for microphones 1 and 2, and the TDOA of source 1 (the target source)
def __getitem__(self, idx):
ys, meta, _, xs = self.mix[idx]
xs = torch.from_numpy(xs)
ys = torch.from_numpy(ys)
X1s = torchaudio.functional.spectrogram(waveform=torch.transpose(xs[0,:,:], 0, 1),
pad=0,
window=torch.hann_window(400),
n_fft=512,
hop_length=256,
win_length=400,
power=None,
normalized=False)
X2s = torchaudio.functional.spectrogram(waveform=torch.transpose(xs[1,:,:], 0, 1),
pad=0,
window=torch.hann_window(400),
n_fft=512,
hop_length=256,
win_length=400,
power=None,
normalized=False)
Ys = torchaudio.functional.spectrogram(waveform=torch.transpose(ys, 0, 1),
pad=0,
window=torch.hann_window(400),
n_fft=512,
hop_length=256,
win_length=400,
power=None,
normalized=False)
M1s = (X1s[0,:,:,0] ** 2 + X1s[0,:,:,1] ** 2) / (X1s[0,:,:,0] ** 2 + X1s[0,:,:,1] ** 2 + X2s[0,:,:,0] ** 2 + X2s[0,:,:,1] ** 2)
M2s = (X1s[1,:,:,0] ** 2 + X1s[1,:,:,1] ** 2) / (X1s[1,:,:,0] ** 2 + X1s[1,:,:,1] ** 2 + X2s[1,:,:,0] ** 2 + X2s[1,:,:,1] ** 2)
Ms = torch.cat((torch.unsqueeze(M1s, dim=0), torch.unsqueeze(M2s, dim=0)), 0)
tau = BIRD.getTDOA(meta)[0]
return Ys, Ms, tau