-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdetect_breaths.py
139 lines (109 loc) · 4.13 KB
/
detect_breaths.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
try:
import simplejson as json
except:
import json
import os
import re
import subprocess
import click
from radiotool.composer import Speech, Segment, Composition
from align import do_alignment
ac_re = re.compile(r"\[Ac=(-?\d+)")
def alignment_with_breaths(speech_file, alignment_file, out_alignment_file=None):
pause_idx = 0
subprocess.call('rm tmpaudio/*.wav', shell=True)
with open(alignment_file, 'r') as af:
alignment = json.load(af)["words"]
new_alignment = []
for x in alignment:
if x["alignedWord"] == "sp":
comp = Composition(channels=1)
speech = Speech(speech_file, "p")
comp.add_track(speech)
start = x["start"]
end = x["end"]
# ignore super-short pauses
if end - start <= .05:
new_alignment.append(x)
pause_idx += 1
continue
# print "pause", pause_idx-1, "start:", start, "end", end
# print "len", end - start
print "Creating pause", start, end - start
seg = Segment(speech, 0.0, start, end - start)
comp.add_segment(seg)
comp.export(
adjust_dynamics=False,
filename="tmpaudio/p%06d" % pause_idx,
channels=1,
filetype='wav',
samplerate=speech.samplerate,
separate_tracks=False)
print "# classifying p%06d.wav" % pause_idx
print "# segment length:", x["end"] - x["start"]
cls = classify_htk(
'tmpaudio/p%06d.wav' % pause_idx)
# cls = breath_classifier.classify(
# 'tmp/pauses/p%06d.wav' % pause_idx)
for word in cls:
word["start"] = round(word["start"] + x["start"], 5)
word["end"] = round(word["end"] + x["start"], 5)
cls[-1]["end"] = x["end"]
new_alignment.extend(cls)
pause_idx += 1
else:
new_alignment.append(x)
if out_alignment_file is None:
out_alignment_file = os.path.splitext(alignment_file)[0] + "-breaths.json"
with open(out_alignment_file, 'w') as new_af:
json.dump({"words": new_alignment}, new_af, indent=4)
return 0
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
@click.command()
@click.argument("wavfile")
@click.argument("alignment_json")
def do_detect_breaths(wavfile, alignment_json):
return alignment_with_breaths(wavfile, alignment_json)
if __name__ == '__main__':
do_detect_breaths()