-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_desc_json_noisy.py
66 lines (56 loc) · 2.12 KB
/
create_desc_json_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
"""
Use this script to create JSON-Line description files that can be used to
train deep-speech models through this library.
This works with data directories that are organized like LibriSpeech:
data_directory/group/speaker/[file_id1.wav, file_id2.wav, ...,
speaker.trans.txt]
Where speaker.trans.txt has in each line, file_id transcription
"""
from __future__ import absolute_import, division, print_function
import argparse
import json
import os
import wave
from glob import glob
import pickle
from tqdm import tqdm
import numpy as np
def main(data_directory, ref_directory, output_file):
refs = []
durations = []
keys = []
noisetypes = []
snrs = []
for condition in tqdm(os.listdir(data_directory)):
condition_path = os.path.join(data_directory, condition)
noise_type, snr = condition.split('_')
snr = float(snr[:-2])
for f in glob(os.path.join(condition_path, '*.wav')):
file_id = os.path.basename(f)
reference = os.path.join(ref_directory, file_id)
audio = wave.open(f)
duration = float(audio.getnframes()) / audio.getframerate()
audio.close()
keys.append(f)
durations.append(duration)
refs.append(reference)
snrs.append(snr)
noisetypes.append(noise_type)
with open(output_file, 'w') as out_file:
for i in range(len(keys)):
line = json.dumps({'key': keys[i],
'duration': durations[i],
'ref': refs[i],
'snr': snrs[i],
'noisetype': noisetypes[i]})
out_file.write(line + '\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('data_directory', type=str,
help='Path to data directory')
parser.add_argument('ref_directory', type=str,
help='Path to ref directory')
parser.add_argument('output_file', type=str,
help='Path to output file')
args = parser.parse_args()
main(args.data_directory, args.ref_directory, args.output_file)