Can run from external directory. Outputs CSV. Works with TextGrids.

This commit is contained in:
Joseph Keshet
2016-06-29 21:20:34 -04:00
parent 4fb9cb39d5
commit 5940e013c5
7 changed files with 693 additions and 21 deletions
+46 -21
View File
@@ -1,25 +1,11 @@
import extract_features as features
from subprocess import call
import sys
import argparse
import tempfile
from helpers.textgrid import *
from helpers.utilities import *
import shutil
def easy_call(command, debug_mode=True):
try:
if debug_mode:
print >>sys.stderr, command
call(command, shell=True)
except Exception as exception:
print "Error: could not execute the following"
print ">>", command
print type(exception) # the exception instance
print exception.args # arguments stored in .args
exit(-1)
def main(wav_filename, preds_filename, begin, end):
def predict_from_times(wav_filename, preds_filename, begin, end):
tmp_features_filename = tempfile._get_default_tempdir() + "/" + next(tempfile._get_candidate_names()) + ".txt"
print tmp_features_filename
@@ -31,14 +17,53 @@ def main(wav_filename, preds_filename, begin, end):
easy_call("th load_tracking_model.lua " + tmp_features_filename + ' ' + preds_filename)
def predict_from_textgrid(wav_filename, preds_filename, textgrid_filename, textgrid_tier):
print wav_filename
if os.path.exists(preds_filename):
os.remove(preds_filename)
textgrid = TextGrid()
# read TextGrid
textgrid.read(textgrid_filename)
# extract tier names
tier_names = textgrid.tierNames()
if textgrid_tier in tier_names:
tier_index = tier_names.index(textgrid_tier)
# run over all intervals in the tier
for interval in textgrid[tier_index]:
if re.search(r'\S', interval.mark()):
tmp_features_filename = generate_tmp_filename()
tmp_preds = generate_tmp_filename()
features.create_features(wav_filename, tmp_features_filename, interval.xmin(), interval.xmax())
easy_call("th load_estimation_model.lua " + tmp_features_filename + ' ' + tmp_preds)
csv_append_row(tmp_preds, preds_filename)
else: # process first tier
for interval in textgrid[0]:
if re.search(r'\S', interval.mark()):
tmp_features_filename = generate_tmp_filename()
tmp_preds = generate_tmp_filename()
features.create_features(wav_filename, tmp_features_filename, interval.xmin(), interval.xmax())
easy_call("th load_estimation_model.lua " + tmp_features_filename + ' ' + tmp_preds)
csv_append_row(tmp_preds, preds_filename)
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser(description='Extract features for formants estimation.')
parser = argparse.ArgumentParser(description='Estimation and tracking of formants.')
parser.add_argument('wav_file', default='', help="WAV audio filename (single vowel or an whole utternace)")
parser.add_argument('formants_file', default='', help="output formant text file")
parser.add_argument('formants_file', default='', help="output formant CSV file")
parser.add_argument('--textgrid_filename', default='', help="get beginning and end times from a TextGrid file")
parser.add_argument('--textgrid_tier', default='', help="a tier name with portion to process (default first tier)")
parser.add_argument('--begin', help="beginning time in the WAV file", default=0.0, type=float)
parser.add_argument('--end', help="end time in the WAV file", default=-1.0, type=float)
args = parser.parse_args()
main(args.wav_file, args.formants_file, args.begin, args.end)
if args.textgrid_filename:
predict_from_textgrid(args.wav_file, args.formants_file, args.textgrid_filename, args.textgrid_tier)
else:
predict_from_times(args.wav_file, args.formants_file, args.begin, args.end)