-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresample-all.py
executable file
·133 lines (112 loc) · 3.94 KB
/
resample-all.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
#!/usr/bin/python
"""Resample and collate corpora, storing the result in corpora/16k
./resample-all.py [--force] [--append] [--help] [[<corpus>]...]
If corpora are listed on the command line, only those subdirectories
of ./corpora/ will be used. The default is to use all corpora except
the already resampled ones.
If --force is given, already resampled files will be overwritten.
--apend adds to the existing metadata rather than writing it afresh.
"""
from voxutils.resample import convert_one
from errno import EEXIST
import sys, os
from os.path import join
from voxutils.resample import convert_one
from voxutils.paths import CORPORA_DIR, RESAMPLED_16K_DIR, IGNORED_CORPORA
def log(*msgs):
for m in msgs:
print >> sys.stderr, m
def read_prompt_file(path):
ids = []
f = open(path)
for line in f:
fn, transcript = line.strip().split(None, 1)
if '/' in fn:
base_id = fn.rsplit('/', 1)[1]
else:
base_id = fn
uid = fn.replace('/', '-')
ids.append([base_id, uid, transcript])
f.close()
return ids
def prompt_file_gen(corpus):
allfiles = []
for root, dirs, files in os.walk(corpus, followlinks=True):
if 'etc' in dirs:
try:
ids = read_prompt_file(join(root, 'etc', 'PROMPTS'))
dirs.remove('etc')
except (IOError, OSError), e:
log(e)
continue
elif 'PROMPTS' in files:
ids = read_prompt_file(join(root, 'PROMPTS'))
else:
ids = []
for subdir in ('wav', 'flac'):
if subdir in dirs:
dirs.remove(subdir)
wavdir = join(root, subdir)
wavfiles = {x.rsplit('.', 1)[0] : x for x in os.listdir(wavdir)}
for x in ids:
base_id = x[0]
if base_id in wavfiles:
x.append(join(wavdir, wavfiles[base_id]))
allfiles.append(x)
for x in allfiles:
yield x
def resample_corpus(corpus_dir, resample_dir, force=False):
etc_dir = join(resample_dir, 'etc')
wav_dir = join(resample_dir, 'wav')
try:
os.makedirs(etc_dir)
os.makedirs(wav_dir)
except OSError, e:
if e.errno != EEXIST:
raise
transcription = open(join(etc_dir, 'transcription'), 'a')
fileids = open(join(etc_dir, 'fileids'), 'a')
prompts = open(join(etc_dir, 'PROMPTS'), 'a')
words = set()
for base_id, pid, transcript, wavfile in prompt_file_gen(corpus_dir):
pid = pid.replace('/', '-')
transcript = transcript.upper()
newfn = os.path.join(wav_dir, pid + '.wav')
if force or not os.path.exists(newfn):
convert_one(wavfile, newfn)
print >> prompts, "%s %s" % (pid, transcript)
print >> fileids, "%s" % (pid,)
print >> transcription, "<s> %s </s> (%s)" % (transcript, pid)
words.update(transcript.split())
prompts.close()
fileids.close()
transcription.close()
vocab = open(join(etc_dir, 'vocab'), 'a')
for word in sorted(words):
print >> vocab, word
vocab.close()
def main(target=RESAMPLED_16K_DIR):
if '--help' in sys.argv or '-h' in sys.argv:
print __doc__
sys.exit()
if '--force' in sys.argv:
sys.argv.remove('--force')
force = True
else:
force = False
if '--append' in sys.argv:
sys.argv.remove('--append')
else:
for fn in ('fileids', 'PROMPTS', 'transcription', 'vocab'):
fn = os.path.join(target, 'etc', fn)
try:
os.rename(fn, fn + '~')
except OSError, e:
log("%s does not exist" % fn)
corpora = sys.argv[1:]
if corpora == []:
corpora = [x for x in os.listdir(CORPORA_DIR) if x not in IGNORED_CORPORA]
for c in corpora:
d = join(CORPORA_DIR, c)
resample_corpus(d, target, force=force)
main()