Skip to content

Commit

Permalink
wip: silence the test_force_align unit test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
joanise committed Nov 7, 2024
1 parent dfd63f0 commit 0fc85ee
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions test/test_force_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
Test force-alignment with SoundSwallower FSG search from Python API
"""

import contextlib
import io
import os
import shutil
import sys
import unittest
import wave
from tempfile import TemporaryDirectory
Expand All @@ -25,14 +28,34 @@
from readalongs.text.util import load_txt, load_xml, save_xml


@contextlib.contextmanager
def capture_stderr():
f = io.StringIO()
with contextlib.redirect_stderr(f):
yield f


@contextlib.contextmanager
def capture_c_stderr():
stderr_fileno = sys.stderr.fileno()
stderr_save = os.dup(stderr_fileno)
stderr_fd = os.open(os.devnull, os.O_RDWR)
os.dup2(stderr_fd, stderr_fileno)
yield
os.dup2(stderr_save, stderr_fileno)
os.close(stderr_save)
os.close(stderr_fd)


class TestForceAlignment(BasicTestCase):
"""Unit testing suite for forced-alignment with SoundSwallower"""

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 capture_stderr(), capture_c_stderr():
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 +73,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 capture_stderr():
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 +155,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 capture_stderr():
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 capture_stderr():
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 +189,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), capture_stderr():
_ = align_audio(xml_path, tf.name, unit="w")

def test_bad_align_mode(self):
with self.assertRaises(AssertionError):
with self.assertRaises(AssertionError), capture_stderr():
_ = 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 0fc85ee

Please sign in to comment.