-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGenerate_mcTrainDataMix2.m
208 lines (175 loc) · 6.65 KB
/
Generate_mcTrainDataMix2.m
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
197
198
199
200
201
202
203
204
205
206
207
208
function Generate_mcTrainDataMix2(overwrite, outDir)
% Re-write of official Reverb data generation script to use two
% simultaneous talkers at different locations in the same room.
if ~exist('outDir', 'var') || isempty(outDir), outDir = '/export/ws15-ffs-data2/mmandel/data/reverb/mixes/'; end
if ~exist('overwrite', 'var') || isempty(overwrite), overwrite = false; end
baseDir = '/export/ws15-ffs-data2/mmandel/data/reverb';
wsjDir = fullfile(baseDir, 'wsjcam0');
rirAndNoiseDir = fullfile(baseDir, 'reverb_tools_for_Generate_mcTrainData');
saveDir = fullfile(outDir, 'data/mc_train/');
% Parameters related to acoustic conditions
SNRdB = 20;
gain = 0.25;
fs = 16000;
otherFStride = 2267; % Prime to pseudo-randomize other utterance
if ~exist(fullfile(wsjDir,'data'), 'file')
error(['Could not find wsjcam0 corpus : Please confirm if %s is ' ...
'a correct path to your clean WSJCAM0 corpus'], wsjDir);
end
% List of WSJ speech data
flist = fullfile(rirAndNoiseDir, 'etc/audio_si_tr.lst');
% List of RIRs
rooms = {'SmallRoom1', 'MediumRoom1', 'LargeRoom1', 'SmallRoom2', ...
'MediumRoom2', 'LargeRoom2'};
dists = {'near', 'far'};
angs = {'AnglA', 'AnglB'};
noises = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '10'};
% RIR_sim1='./RIR/RIR_SmallRoom1_near_AnglA.wav';
% noise_sim1='./NOISE/Noise_SmallRoom1';
disp('Loading RIRs...')
for r = 1:length(rooms)
for d = 1:length(dists)
for a = 1:length(angs)
rirFile{r,d,a} = fullfile(rirAndNoiseDir, 'RIR', ['RIR_' rooms{r} '_' ...
dists{d} '_' angs{a} '.wav']);
rir{r,d,a} = wavread(rirFile{r,d,a});
[~,delay{r,d,a}] = max(rir{r,d,a}(:,1));
before_impulse = floor(fs*0.001);
after_impulse = floor(fs*0.05);
inds = delay{r,d,a}-before_impulse : delay{r,d,a}+after_impulse;
rirDirect{r,d,a} = rir{r,d,a}(inds,1);
end
end
end
files = readLines(flist);
lastNoiseFile = '';
otherF = round(length(files)/2);
for f = 1:length(files)
fprintf('%d: %s\n', f, files{f});
baseOutFile = fullfile(saveDir, files{f});
doneFile = [baseOutFile '.done'];
if exist(doneFile, 'file') && ~overwrite
fprintf('\b <-- Skipping\n');
continue
end
% Figure out what RIR and noise to use to be consistent
% with original REVERB data
a = mod(f-1, 2) + 1;
d = floor(mod(f-1, 4)/2) + 1;
r = floor(mod(f-1, 24)/4) + 1;
n = floor(mod(f-1, 240)/24) + 1;
noiseFile = fullfile(rirAndNoiseDir, 'NOISE', ['Noise_' rooms{r}, ...
'_' noises{n} '.wav']);
if ~strcmp(noiseFile, lastNoiseFile)
% fprintf('Loading noise: %s\n', noiseFile);
noise = wavread(noiseFile);
lastNoiseFile = noiseFile;
end
sphFile = fullfile(wsjDir, 'data', [files{f} '.wv1']);
[x Px] = loadAndSpatialize(sphFile, rir{r,d,a}, rirDirect{r,d,a});
scaledNoise = scaleNoise(noise, size(x,1), Px, SNRdB);
saveWav(x, gain, delay{r,d,a}, fs, [baseOutFile '_src1.wav']);
saveWav(x + scaledNoise, gain, delay{r,d,a}, fs, [baseOutFile '_src1n.wav']);
% Put utterances from different talkers at other positions in the same room
for od = 1:length(dists)
for oa = 1:length(angs)
if (od == d) && (oa == a)
% Skip same exact location
continue;
end
otherF = mod(otherF + otherFStride - 1, length(files)) + 1;
while speakerName(files{f}) == speakerName(files{otherF})
otherF = mod(otherF + otherFStride - 1, length(files)) + 1;
end
sphFile2 = fullfile(wsjDir, 'data', [files{otherF} '.wv1']);
[x2 Px2] = loadAndSpatialize(sphFile2, rir{r,od,oa}, rirDirect{r,od,oa});
x2 = zeroPadOrTrim(x2, size(x,1));
mixNum = oa-1+2*(od-1);
mixFile = sprintf('%s_mix%d', baseOutFile, mixNum);
saveWav(x + x2 + scaledNoise, gain, delay{r,d,a}, fs, ...
[mixFile '.wav']);
saveMat([mixFile '.mat'], sphFile, sphFile2, noiseFile, ...
r,d,a, delay, rirFile);
end
end
% Touch doneFile
fclose(fopen(doneFile, 'w'));
end
function [y Px] = loadAndSpatialize(sphFile, rir, rirDirect)
x = sph2wav(sphFile);
y = fconv(x, rir);
direct_signal = fconv(x, rirDirect);
Px = diag(mean(direct_signal.^2,1));
function scaledNoise = scaleNoise(noise, lenX, Px, SNRdB)
noise = noise(1:lenX,:);
noiseRef = noise(:,1);
iPn = diag(1./mean(noiseRef.^2,1));
Msnr = sqrt(10^(-SNRdB/10)*iPn*Px);
scaledNoise = noise*Msnr;
function spk = speakerName(sphFile)
% Speaker name is first three characters of file name without any directories
bn = basename(sphFile);
spk = bn(1:3);
function x = zeroPadOrTrim(x, len)
if size(x,1) > len
x = x(1:len,:);
elseif size(x,1) < len
x = [x; zeros(len-size(x,1), size(x,2))];
end
function fnames = readLines(flist)
fid = fopen(flist);
fnames = {};
while 1
fnameTmp = fgetl(fid);
if ~ischar(fnameTmp), break; end
fnames{end+1} = fnameTmp;
end
fclose(fid);
function saveWav(x, gain, delay, fs, outFile)
y = gain * x(delay:end,:);
fprintf('Writing: %s\n', outFile);
ensureDirExists(outFile);
wavwrite(y, fs, outFile);
function saveMat(matFile, sphFile, sphFile2, noiseFile, r,d,a, delay, rirFile)
delay = delay{r,d,a};
rirFile = rirFile{r,d,a};
save(matFile, 'sphFile', 'sphFile2', 'noiseFile', 'r', 'd', 'a', ...
'delay', 'rirFile');
%%%%
function [y]=fconv(x, h)
%FCONV Fast Convolution
% [y] = FCONV(x, h) convolves x and h
%
% x = input vector
% h = input vector
%
% See also CONV
%
% NOTES:
%
% 1) I have a short article explaining what a convolution is. It
% is available at http://stevem.us/fconv.html.
%
%
%Version 1.0
%Coded by: Stephen G. McGovern, 2003-2004.
%
%Copyright (c) 2003, Stephen McGovern
%All rights reserved.
%
%THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
%AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
%IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
%LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
%CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
%SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
%CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
%ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%POSSIBILITY OF SUCH DAMAGE.
Ly=length(x)+length(h)-1;
X=fft(x, Ly);
H=fft(h, Ly);
Y=bsxfun(@times, X, H);
y=real(ifft(Y));