-
Notifications
You must be signed in to change notification settings - Fork 84
/
speech_gen_noisy.py
executable file
·285 lines (198 loc) · 6.91 KB
/
speech_gen_noisy.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2017, 2018 Guenter Bartsch
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# create corpus from an existing one by adding noise and echo effects to the
# recordings
#
# these additional, artifically created recordings should help with
# noise resistance when used in training
#
import os
import sys
import logging
import traceback
import locale
import codecs
import wave
import random
from optparse import OptionParser
from nltools import misc
from speech_transcripts import Transcripts
PROC_TITLE = 'speech_gen_noisy'
DEBUG_LIMIT = 0
FRAMERATE = 16000
MIN_QUALITY = 2
#
# init
#
misc.init_app(PROC_TITLE)
#
# command line
#
parser = OptionParser("usage: %prog [options] corpus")
parser.add_option ("-s", "--stride", dest="stride", type="int", default=4,
help="only generate noisy variant for every nth entry, default: 4")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
help="enable debug output")
(options, args) = parser.parse_args()
if options.verbose:
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("requests").setLevel(logging.WARNING)
else:
logging.basicConfig(level=logging.INFO)
if len(args) != 1:
parser.print_usage()
sys.exit(1)
corpus_in = args[0]
corpus_out = corpus_in + '_noisy'
#
# load transcripts
#
logging.info("loading transcripts...")
transcripts = Transcripts(corpus_name=corpus_in)
logging.info("loading transcripts...done.")
#
# config
#
config = misc.load_config('.speechrc')
corpora = config.get("speech", "speech_corpora")
noise_dir = config.get("speech", "noise_dir")
wav16_dir = config.get("speech", "wav16")
bg_dir = '%s/bg' % noise_dir
fg_dir = '%s/fg/16kHz' % noise_dir
out_dir = '%s/%s' % (corpora, corpus_out)
if os.path.exists(out_dir):
logging.error("%s already exists!" % out_dir)
sys.exit(1)
logging.info ("creating %s ..." % out_dir)
misc.mkdirs(out_dir)
#
# read fg file lengths
#
fg_lens = {}
for fgfn in os.listdir(fg_dir):
wav = wave.open('%s/%s' % (fg_dir, fgfn), 'r')
fr = wav.getframerate()
if fr == FRAMERATE:
fg_lens[fgfn] = float(wav.getnframes()) / float(FRAMERATE)
else:
logging.error('%s: wrong framerate %d' % (fgfn, fr))
wav.close()
#
# read bg file lengths
#
bg_lens = {}
max_bg_len = 0
cnt = 0
for bgfn in os.listdir(bg_dir):
if not bgfn.endswith('_16k.wav'):
continue
# print bgfn
wav = wave.open('%s/%s' % (bg_dir, bgfn), 'r')
fr = wav.getframerate()
if fr == FRAMERATE:
bg_lens[bgfn] = float(wav.getnframes()) / float(FRAMERATE)
if bg_lens[bgfn] > max_bg_len:
max_bg_len = bg_lens[bgfn]
else:
logging.error('%s: wrong framerate %d' % (bgfn, fr))
wav.close()
# print repr(bg_lens)
#
# count good transcripts
#
total_good = 0
for ts in transcripts:
if transcripts[ts]['quality']<MIN_QUALITY:
continue
total_good += 1
#
# main
#
cnt = 1
random.seed(42)
for ts in transcripts:
# print type(transcripts)
if DEBUG_LIMIT:
ts2 = random.choice(transcripts.keys())
cfn = transcripts[ts2]['cfn']
else:
cfn = transcripts[ts]['cfn']
entry = transcripts[cfn]
if entry['quality']<MIN_QUALITY:
continue
if cnt % options.stride == 0:
infn = '%s/%s/%s.wav' % (wav16_dir, corpus_in, cfn)
pkgdirfn = '%s/%s' % (out_dir, entry['dirfn'])
audiofn2 = entry['audiofn'] + '-noisy'
if not os.path.exists(pkgdirfn):
misc.mkdirs('%s/etc' % pkgdirfn)
misc.mkdirs('%s/wav' % pkgdirfn)
outfn = '%s/wav/%s.wav' % (pkgdirfn, audiofn2)
wav = wave.open(infn, 'r')
fr = wav.getframerate()
if fr == FRAMERATE:
in_len = float(wav.getnframes()) / float(FRAMERATE)
fg_level = random.uniform (-1.0, 0.0)
logging.info ('%5d/%5d %6.2fs lvl=%2.3f %s' % (cnt, total_good, in_len, fg_level, cfn))
logging.debug (' entry: %s' % repr(entry))
#
# forground noises
#
fgfn_1 = random.choice(fg_lens.keys())
fgfn_2 = random.choice(fg_lens.keys())
fg_len = fg_lens[fgfn_1] + fg_lens[fgfn_2] + in_len
logging.debug (' fg: len=%6.2fs fn1=%s fn2=%s' % (fg_len, fgfn_1, fgfn_2))
if fg_len < max_bg_len:
ts2 = 'nspc ' + entry['ts'] + ' nspc'
logging.debug (' ts2: %s' % ts2)
#
# background noise
#
bgfn = None
while not bgfn:
bgfn2 = random.choice(bg_lens.keys())
bgl = bg_lens[bgfn2]
if bgl > fg_len:
bgfn = bgfn2
bg_off = random.uniform (0, bgl - fg_len)
bg_level = random.uniform (-15.0, -10.0)
logging.debug (' bg: off=%6.2fs fn=%s' % (bg_off, bgfn))
# reverb [-w|--wet-only] [reverberance (50%) [HF-damping (50%)
# [room-scale (100%) [stereo-depth (100%)
# [pre-delay (0ms) [wet-gain (0dB)]]]]]]
reverb_level = random.uniform(0.0, 50.0)
# compand attack1,decay1{,attack2,decay2}
# [soft-knee-dB:]in-dB1[,out-dB1]{,in-dB2,out-dB2}
# [gain [initial-volume-dB [delay]]]
cmd = 'sox -b 16 -r 16000 -m "|sox --norm=%f %s/%s %s %s/%s -p compand 0.01,0.2 -90,-10 -5 reverb %f" "|sox --norm=%f %s/%s -p trim %f %f" %s' % \
(fg_level, fg_dir, fgfn_1, infn, fg_dir, fgfn_2, reverb_level, bg_level, bg_dir, bgfn, bg_off, fg_len, outfn)
logging.debug(' cmd: %s' % cmd)
os.system(cmd)
promptfn = '%s/etc/prompts-original' % pkgdirfn
with codecs.open(promptfn, 'a', 'utf8') as promptf:
promptf.write('%s %s\n' % (audiofn2, ts2))
else:
logging.error ('%s: too long %f' % (infn, fg_len))
else:
logging.error ('%s: wrong framerate %d' % (infn, fr))
wav.close()
cnt += 1
if DEBUG_LIMIT>0 and cnt>DEBUG_LIMIT:
break