Skip to content

Commit

Permalink
Merge pull request #354 from cmusphinx/339-final-state
Browse files Browse the repository at this point in the history
Do not attempt to align phones to impossibly short durations (fixes #339)
  • Loading branch information
dhdaines authored Jul 20, 2023
2 parents f899dfe + 6252f26 commit 05cb832
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
51 changes: 50 additions & 1 deletion cython/test/alignment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import os
from pocketsphinx import Decoder
import unittest
import wave

DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data")


class TestAlignment(unittest.TestCase):
def _run_decode(self, decoder, expect_fail=False):
def _run_decode(self, decoder):
with open(os.path.join(DATADIR, "goforward.raw"), "rb") as fh:
buf = fh.read()
decoder.start_utt()
Expand Down Expand Up @@ -45,6 +46,54 @@ def test_default_lm(self):
self._run_decode(decoder)
self.assertEqual(decoder.hyp().hypstr, "go forward ten meters")

def _run_phone_align(self, decoder, buf):
decoder.start_utt()
decoder.process_raw(buf, no_search=False, full_utt=True)
decoder.end_utt()
decoder.set_alignment()
decoder.start_utt()
decoder.process_raw(buf, no_search=False, full_utt=True)
decoder.end_utt()

def test_align_forever(self):
decoder = Decoder(loglevel="INFO", backtrace=True, lm=None)
decoder.set_align_text("feels like these days go on forever")
with wave.open(
os.path.join(DATADIR, "forever", "input_2_16k.wav"), "r"
) as infh:
data = infh.readframes(infh.getnframes())
self._run_phone_align(decoder, data)
alignment = decoder.get_alignment()
phones = [entry.name for entry in alignment.phones()]
self.assertEqual(
phones,
[
"F",
"IY",
"L",
"Z",
"L",
"AY",
"K",
"DH",
"IY",
"Z",
"D",
"EY",
"Z",
"G",
"OW",
"AO",
"N",
"F",
"ER",
"EH",
"V",
"ER",
"SIL",
],
)


if __name__ == "__main__":
unittest.main()
12 changes: 10 additions & 2 deletions src/state_align_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,21 @@ state_align_search_init(const char *name,
i < sas->n_phones && itor;
++i, itor = ps_alignment_iter_next(itor)) {
ps_alignment_entry_t *ent = ps_alignment_iter_get(itor);
int min_nframes;

hmm_init(sas->hmmctx, &sas->hmms[i], FALSE,
ent->id.pid.ssid, ent->id.pid.tmatid);
if (ent-> start > 0)
/* Can't align less than the number of frames in an HMM! */
min_nframes = hmm_n_emit_state(&sas->hmms[i]);
if (ent->duration < min_nframes)
E_WARN("phone %d has impossible duration %d "
"(consider disabling bestpath search)\n",
i, ent->duration);
if (ent->start > 0 && ent->duration >= min_nframes)
sas->sf[i] = ent->start;
else
sas->sf[i] = 0; /* Always active */
if (ent->duration > 0)
if (ent->duration >= min_nframes)
sas->ef[i] = ent->start + ent->duration;
else
sas->ef[i] = INT_MAX; /* Always active */
Expand Down

0 comments on commit 05cb832

Please sign in to comment.