-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_audio.py
executable file
·153 lines (140 loc) · 6.17 KB
/
prepare_audio.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
#!/usr/bin/env python3
"""audio.py
This program takes a spreadsheet in .ods format containing linguistic
information on the N|uu language (collected through field work). It
checks the audio columns, identifies the correct wav files and copies
them to a specified audio directory.
"""
import argparse
import filecmp
import glob
import logging
import re
from pandas_ods_reader import read_ods
def read_input(filename):
"""Read input .ods file found at filename. Return a list of
entries that contain the information on the word and the
recordings.
"""
logging.debug("Reading in file " + filename)
spreadsheet = read_ods(filename , 1)
data = []
for index, row in spreadsheet.iterrows():
entry = {}
entry["tw"] = row["Dictionary Recording (target word only)"]
entry["tw in s"] = row["Recordings (target word in sentence)"]
entry["word"] = row["Orthography 1"]
data.append(entry)
return data
def write_output(file, data, base, target):
"""For each word in the column "word" in data, this function
searches the names of the audio files (in "tw") and globs
this filename in the base directory (recursively). It then writes
a bash copy function to the output. The target is the target
directory where the audio files should go.
"""
logging.debug("Writing app output to " + file)
output = open(file, "w")
output.write("mkdir -p " + target + "\n")
for i in data:
if not i["tw"]:
output.write("# " + i["word"] + " does not have dictionary recording\n")
else:
# handle target word (tw) audio
output.write("# " + i["word"] + "\n")
files = re.split("[ ;,]+", i["tw"])
for f in files:
if f == "--" or f == "": # We can skip the -- or empty strings
break
logging.debug("Handling " + f)
if "." in f:
logging.error("Found period in " + f)
locations_lc = glob.glob(base + "/**/" + f + ".wav", recursive = True)
locations_uc = glob.glob(base + "/**/" + f + ".WAV", recursive = True)
locations_nc = glob.glob(base + "/**/" + f, recursive = True)
nr_files_lc = len(locations_lc)
nr_files_uc = len(locations_uc)
nr_files_nc = len(locations_nc)
if nr_files_lc + nr_files_uc + nr_files_nc == 1:
output.write("cp \"")
if nr_files_lc == 1:
output.write(locations_lc[0])
elif nr_files_uc == 1:
output.write(locations_uc[0])
else:
output.write(locations_nc[0])
output.write("\" " + target + "/" + f + ".wav\n")
elif nr_files_lc + nr_files_uc + nr_files_nc == 0:
logging.warning("Did not find " + f)
output.write("# Did not find " + f + "\n")
else:
logging.warning("Found multiple " + f)
# Check for duplicates
locations = locations_lc + locations_uc + locations_nc
same = True
for i in range(len(locations)):
for j in range(i + 1, len(locations)):
same &= filecmp.cmp(locations[i], locations[j])
if same:
output.write("cp \"")
output.write(locations[0] + "\" ")
output.write(target + "/" + f + ".wav\n")
else:
logging.error("Found multiple different " + f)
output.write("# Found multiple different " + f + "\n")
output.close()
def main():
"""Commandline arguments are parsed and handled. Next, the input
is read from the input filename. Validation of the data is
performed. Next, the data is converted into the format usable for
the dictionary portal and dictionary app. Finally, the data is
written to output files. The argument provides the base of the output files
and the system generates a .txt containing the dictionary portal/app format.
"""
parser = argparse.ArgumentParser(description="This program checks the N|uu spreadsheet to identify audio files that are used. It provides a script that copies the used audio files.")
parser.add_argument("-i", "--input",
help = "name of ods spreadsheet file",
action = "store",
metavar = "FILE")
parser.add_argument("-o", "--output",
help = "name of output filename",
action = "store",
metavar = "FILE")
parser.add_argument("-t", "--target",
help = "target directory",
action = "store",
metavar = "DIR")
parser.add_argument("-b", "--base",
help = "name of directory that contains all audio files",
action = "store",
metavar = "FILE")
parser.add_argument("-l", "--log",
help = "name of logging filename",
action = "store",
metavar = "FILE")
parser.add_argument("-d", "--debug",
help = "provide debugging information",
action = "store_const",
dest = "loglevel",
const = logging.DEBUG,
default = logging.WARNING,
)
args = parser.parse_args()
if args.log:
logging.basicConfig(filename = args.log, filemode='w', format = '%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', datefmt = '%H:%M:%S', level = args.loglevel)
else:
logging.basicConfig(level = args.loglevel)
# Perform checks on arguments
if args.input == None:
parser.error("An input filename is required.")
if args.base == None:
parser.error("A base directory is required.")
if args.output == None:
parser.error("An output filename is required.")
if args.target == None:
parser.error("A target directory is required.")
# Handle the data
data = read_input(args.input)
write_output(args.output, data, args.base, args.target)
if __name__ == '__main__':
main()