def classify_htk(audio_file): MIN_BREATH_DUR = 0.1 MIN_AC = 500 cwd = os.getcwd() os.chdir(os.path.dirname(os.path.realpath(__file__))) transcript = "breath.transcript" output = "breath-classify-output.json" results = "tmp/aligned.results" with open(transcript, 'w') as f: f.write("""[{"speaker": "speaker", "line": "{BR}"}]""") do_alignment(audio_file, transcript, output, json=True, textgrid=False) os.remove(transcript) # subprocess.call('python ../p2fa/align.py ../%s %s %s' % # (audio_file, transcript, output), shell=True) final_words = [{ "start": 0.0, "end": 0.0, "alignedWord": "sp", "word": "{p}" }] with open(results, 'r') as res: match = ac_re.search(res.read()) if match: ac = int(match.group(1)) print "Ac:", ac if ac > MIN_AC: print "Breath!" with open(output, 'r') as out: words = json.load(out)["words"] breath = filter( lambda x: x["alignedWord"] == "{BR}", words)[0] breath_dur = breath["end"] - breath["start"] print "breath len", breath_dur if breath_dur > MIN_BREATH_DUR: final_words = words final_words[0]["start"] = 0.0 for word in final_words: if word["alignedWord"] == "{BR}": word["likelihood"] = ac os.chdir(cwd) return final_words
def align_transcripts(as_ep_ids): """ Align the transcript segments in ./alignment_data/seg_json/ to episode audio using p2fa. If there are any errors during alignment, skip the episode on which they occur and move on. Parameters ---------- as_ep_ids : list The list of all audiosearch episode ids associated with a particular podcast Returns ------- as_ep_ids : list A list of episode ids that have been completely aligned. problem_episodes : list A list of episode ids that were not fully aligned """ problem_episodes = [] for ep_num in as_ep_ids: wavfile = './alignment_data/seg_audio/{}_seg*.wav'.format(ep_num) for ff in glob.glob(wavfile): file_name = os.path.split(ff)[-1].split('.')[0] trsfile = './alignment_data/seg_json/{}.json'.format(file_name) outfile = './alignment_data/alignments_json/' \ '{}_aligned.json'.format(file_name) if os.path.lexists(outfile): fn = os.path.split(outfile)[-1] print('\t{} already exists! Skipping'.format(fn)) continue try: print('\n\tAligning {}...'.format(file_name)) align.do_alignment(ff, trsfile, outfile, json=True, textgrid=False, phonemes=True, breaths=False) except: print('\n\tError Aligning {}'.format(file_name)) problem_episodes.append(ep_num) as_ep_ids.remove(ep_num) continue return as_ep_ids, problem_episodes