Skip to content

Commit

Permalink
refactor(silence): change syntax for adding silence and allow output …
Browse files Browse the repository at this point in the history
…to variety of audio formats
  • Loading branch information
roedoejet committed Oct 19, 2021
1 parent e5a7d52 commit a75dca2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
14 changes: 8 additions & 6 deletions readalongs/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,11 @@ def frames_to_time(frames):
}
silence_offsets = defaultdict(int)
silence = 0
if results["tokenized"].xpath("//*[@silence]"):
if results["tokenized"].xpath("//silence"):
endpoint = 0
for el in results["tokenized"].xpath("//*"):
if "silence" in el.attrib:
silence_ms = parse_time(el.attrib["silence"])
if el.tag == "silence" and "dur" in el.attrib:
silence_ms = parse_time(el.attrib["dur"])
silence_segment = AudioSegment.silent(
duration=silence_ms
) # create silence segment
Expand Down Expand Up @@ -518,11 +518,13 @@ def save_readalong(
save_xml(tokenized_xml_path, align_results["tokenized"])

_, audio_ext = os.path.splitext(audiofile)
audio_path = output_base + audio_ext
audio_format = audio_ext[1:]
if audiosegment:
audio_path = output_base + ".wav"
audiosegment.export(audio_path, format="wav")
if audio_format in ["m4a", "aac"]:
audio_format = "ipod"
audiosegment.export(audio_path, format=audio_format)
else:
audio_path = output_base + audio_ext
shutil.copy(audiofile, audio_path)

smil_path = output_base + ".smil"
Expand Down
4 changes: 2 additions & 2 deletions test/data/ej-fra-silence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<body>
<div type="page">
<p>
<s><span silence="1"/>Bonjour.</s>
<s><silence dur="1"/>Bonjour.</s>
<s>Je m'appelle Éric Joanis.</s>
<s>Je suis <span silence="1.382s"></span> programmeur au sein <span silence="500ms"></span> de l'équipe des technologies pour les langues autochtones au CNRC.</s>
<s>Je suis <silence dur="1.382s"></silence> programmeur au sein <silence dur="500ms"></silence> de l'équipe des technologies pour les langues autochtones au CNRC.</s>
</p>
</div>
<div type="page">
Expand Down
15 changes: 11 additions & 4 deletions test/test_silence.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,26 @@ def test_basic_silence_insertion(self):
],
)
self.assertEqual(results.exit_code, 0)
self.assertTrue(os.path.exists(os.path.join(output, "silence.wav")))
self.assertTrue(os.path.exists(os.path.join(output, "silence.m4a")))
# test silence spans in output xml
with open(os.path.join(output, "silence.xml"), "rb") as f:
xml_bytes = f.read()
root = etree.fromstring(xml_bytes)
silence_spans = root.xpath("//*[@silence]")
silence_spans = root.xpath("//silence")
self.assertEqual(len(silence_spans), 3)
# test audio has correct amount of silence added
original_audio = AudioSegment.from_file(
os.path.join(self.data_dir, "ej-fra.m4a")
)
new_audio = AudioSegment.from_wav(os.path.join(output, "silence.wav"))
self.assertEqual(len(new_audio) - len(original_audio), 2882)
new_audio = AudioSegment.from_file(
os.path.join(output, "silence.m4a"), format="m4a"
)
self.assertAlmostEqual(
len(new_audio) - len(original_audio),
2882,
msg="silence-added audio file is more than 50ms shorter or longer",
delta=50,
)


if __name__ == "__main__":
Expand Down

0 comments on commit a75dca2

Please sign in to comment.