Skip to content

Commit

Permalink
fix(tests): silence test_anchors and test_force_align suites
Browse files Browse the repository at this point in the history
includes using silence_c_stderr to hide SoundSwallower logs
  • Loading branch information
joanise committed Nov 7, 2024
1 parent 8d7be24 commit 99da050
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
33 changes: 19 additions & 14 deletions test/test_anchors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"""Unit testing for the anchors functionality in readalongs align"""

import os
from contextlib import redirect_stderr
from io import StringIO
from unittest import main

from basic_test_case import BasicTestCase
from basic_test_case import BasicTestCase, silence_c_stderr

from readalongs.align import align_audio
from readalongs.log import LOGGER
Expand All @@ -18,10 +20,11 @@ def test_anchors_inner_only(self):
"""Test aligning with anchors only between existing text"""

# ej-fra-anchors has anchors between words/sentences only
results = align_audio(
os.path.join(self.data_dir, "ej-fra-anchors.readalong"),
os.path.join(self.data_dir, "ej-fra.m4a"),
)
with redirect_stderr(StringIO()):
results = align_audio(
os.path.join(self.data_dir, "ej-fra-anchors.readalong"),
os.path.join(self.data_dir, "ej-fra.m4a"),
)
words = results["words"]
# The input text file has 99 words, so should the aligned segments.
self.assertEqual(len(words), 99)
Expand All @@ -39,11 +42,12 @@ def test_anchors_outer_too(self):

# ej-fra-anchors2 also has anchors before the first word and after the last word
save_temps_prefix = os.path.join(self.tempdir, "anchors2-temps")
results = align_audio(
os.path.join(self.data_dir, "ej-fra-anchors2.readalong"),
os.path.join(self.data_dir, "ej-fra.m4a"),
save_temps=save_temps_prefix,
)
with redirect_stderr(StringIO()):
results = align_audio(
os.path.join(self.data_dir, "ej-fra-anchors2.readalong"),
os.path.join(self.data_dir, "ej-fra.m4a"),
save_temps=save_temps_prefix,
)
words = results["words"]
# The input text file has 99 words, so should the aligned segments.
self.assertEqual(len(words), 99)
Expand Down Expand Up @@ -83,10 +87,11 @@ def test_anchors_align_modes(self):
with open(xml_file, "wt", encoding="utf8") as f:
print(xml_with_anchors, file=f)
with self.assertLogs(LOGGER, level="INFO") as cm:
results = align_audio(
xml_file,
os.path.join(self.data_dir, "noise.mp3"),
)
with silence_c_stderr(), redirect_stderr(StringIO()):
results = align_audio(
xml_file,
os.path.join(self.data_dir, "noise.mp3"),
)
words = results["words"]
self.assertEqual(len(words), 10)
logger_output = "\n".join(cm.output)
Expand Down
34 changes: 23 additions & 11 deletions test/test_force_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import shutil
import unittest
import wave
from contextlib import redirect_stderr
from io import StringIO
from tempfile import TemporaryDirectory

from basic_test_case import BasicTestCase
from basic_test_case import BasicTestCase, silence_c_stderr
from lxml import etree
from soundswallower import get_model_path

Expand All @@ -32,7 +34,8 @@ def test_align(self):
"""Basic alignment test case with XML input"""
xml_path = os.path.join(self.data_dir, "ej-fra.readalong")
wav_path = os.path.join(self.data_dir, "ej-fra.m4a")
results = align_audio(xml_path, wav_path, unit="w", debug_aligner=True)
with silence_c_stderr(), redirect_stderr(StringIO()):
results = align_audio(xml_path, wav_path, unit="w", debug_aligner=True)

# Verify that the same IDs are in the output
converted_path = os.path.join(self.data_dir, "ej-fra-converted.readalong")
Expand All @@ -50,7 +53,8 @@ def test_align_text(self):
_, temp_fn = create_input_ras(
input_file_name=txt_path, text_languages=("fra",), save_temps=None
)
results = align_audio(temp_fn, wav_path, unit="w", save_temps=None)
with redirect_stderr(StringIO()):
results = align_audio(temp_fn, wav_path, unit="w", save_temps=None)

# Verify that the same IDs are in the output
converted_path = os.path.join(self.data_dir, "ej-fra-converted.readalong")
Expand Down Expand Up @@ -131,14 +135,22 @@ def test_align_switch_am(self):
with open(os.path.join(custom_am_path, "noisedict"), "at") as fh:
fh.write(";; here is a comment\n")
fh.write("[BOGUS] SIL\n")
results = align_audio(
xml_path, wav_path, unit="w", config={"acoustic_model": custom_am_path}
)
with redirect_stderr(StringIO()):
results = align_audio(
xml_path,
wav_path,
unit="w",
config={"acoustic_model": custom_am_path},
)
# Try with no noisedict
os.remove(os.path.join(custom_am_path, "noisedict"))
results = align_audio(
xml_path, wav_path, unit="w", config={"acoustic_model": custom_am_path}
)
with redirect_stderr(StringIO()):
results = align_audio(
xml_path,
wav_path,
unit="w",
config={"acoustic_model": custom_am_path},
)
# Verify that the same IDs are in the output
converted_path = os.path.join(self.data_dir, "ej-fra-converted.readalong")
xml = load_xml(converted_path)
Expand All @@ -157,11 +169,11 @@ def test_align_fail(self):
writer.setsampwidth(2)
writer.setframerate(16000)
writer.writeframes(b"\x00\x00")
with self.assertRaises(RuntimeError):
with self.assertRaises(RuntimeError), redirect_stderr(StringIO()):
_ = align_audio(xml_path, tf.name, unit="w")

def test_bad_align_mode(self):
with self.assertRaises(AssertionError):
with self.assertRaises(AssertionError), redirect_stderr(StringIO()):
_ = align_audio(
os.path.join(self.data_dir, "ej-fra.readalong"),
os.path.join(self.data_dir, "noise.mp3"),
Expand Down

0 comments on commit 99da050

Please sign in to comment.