/
mixMrtBubbleNoiseDir.m
71 lines (61 loc) · 3.2 KB
/
mixMrtBubbleNoiseDir.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
function outDir = mixMrtBubbleNoiseDir(inDir, outDir, nMixes, bubblesPerSec, snr_db, dur_s, normalize, noiseShape, randomness, speechFiles)
% Generate mixtures of bubble noise with clean files from a directory
%
% mixMrtBubbleNoiseDir(inDir, outDir, nMixes, bubblesPerSec, snr_db, dur_s, normalize, noiseShape, speechFiles)
%
% Mixture files will have the same name as the original files with a
% mixture number appended. It is safe to run this function multiple times
% with the same arguments, it will only create new mixtures if there are
% not enough in outDir already.
%
% Inputs:
% inDir directory in which to find clean input files
% outDir directory in which to write wav file for each mixture.
% Files will be written in a subdirectory named "bpsNN"
% where NN is the bubbles-per-second value.
% nMixes maximum number of mixes per clean file, if some mixes
% already exist, enough will be added to reach this many
% bublesPerSec number of bubbles in noise per second of mix file
% snr_db scaling parameter for the clean files, in dB from an RMS of 0.1
% dur_s desired duration of mix files, clean files will be
% zero-padded or truncated from both ends to be this length
% normalize normalize each clean file to have an RMS of 0.1 before scaling
% noiseShape numeric specifier of noise shape passed to speechProfile()
% speechFiles list of files (relative to inDir) or regexp of files to
% find in inDir to use as clean files. If blank, use all
if ~exist('bubblesPerSec', 'var') || isempty(bubblesPerSec), bubblesPerSec = 15; end
if ~exist('snr_db', 'var') || isempty(snr_db), snr_db = -30; end
if ~exist('nMixes', 'var') || isempty(nMixes), nMixes = 1; end
if ~exist('dur_s', 'var') || isempty(dur_s), dur_s = 2; end
if ~exist('normalize', 'var') || isempty(normalize), normalize = 1; end
if ~exist('noiseShape', 'var') || isempty(noiseShape), noiseShape = 0; end
if ~exist('randomness', 'var') || isempty(randomness), randomness = 1; end
if ~exist('speechFiles', 'var') || isempty(speechFiles), speechFiles = findFiles(inDir, '\.wav'); end
outDir = fullfile(outDir, sprintf('bps%g', bubblesPerSec));
if ischar(speechFiles) % Can supply a pattern
speechFiles = findFiles(inDir, speechFiles, 1);
end
useHoles = true;
snr = db2mag(snr_db);
for i = 1:length(speechFiles)
cleanFile = fullfile(inDir, speechFiles{i});
[~,sr] = audioread(cleanFile);
num = -1;
while true
[d f e] = fileparts(speechFiles{i});
% Find next available file name
[outFile num] = nextAvailableFile(fullfile(outDir, d), ...
'%s_bps%g_snr%+d_%03d', {f, bubblesPerSec, snr_db}, num, e);
if num >= nMixes
break
end
fprintf('%d %d: %s\n', i, num, outFile)
if randomness > 1
randomSeed = randomness + num;
else
randomSeed = randomness;
end
[mix sr] = mixBubbleNoise(cleanFile, sr, useHoles, bubblesPerSec, snr, dur_s, normalize, noiseShape, randomSeed);
wavWriteBetter(mix, sr, outFile);
end
end