예제 #1
0
        public static int bio_fread_1d(BoxedValue <Pointer <byte> > buf, uint el_sz, BoxedValue <uint> n_el, FILE fp, int sw, BoxedValue <uint> ck)
        {
            /* Read 1-d array size */
            Pointer <byte> array_size = PointerHelpers.Malloc <byte>(4);

            if (bio_fread(array_size, 4, 1, fp, sw, ck) != 1)
            {
                err.E_FATAL("fread(arraysize) failed\n");
            }

            n_el.Val = array_size.ReinterpretCast <uint>().Deref;
            if (n_el.Val <= 0)
            {
                err.E_FATAL(string.Format("Bad arraysize: {0}\n", n_el.Val));
            }

            /* Allocate memory for array data */
            buf.Val = ckd_alloc.ckd_calloc <byte>(n_el.Val * el_sz);

            /* Read array data */
            if (bio_fread(buf.Val, (int)el_sz, (int)n_el.Val, fp, sw, ck) != n_el.Val)
            {
                err.E_FATAL("fread(arraydata) failed\n");
            }

            return((int)(n_el.Val));
        }
예제 #2
0
        public static int swap_check(FILE fp)
        {
            Pointer <byte> magic_buf = PointerHelpers.Malloc <byte>(4);
            Pointer <uint> magic     = magic_buf.ReinterpretCast <uint>();

            if (fp.fread(magic_buf, 4, 1) != 1)
            {
                err.E_ERROR("Cannot read BYTEORDER MAGIC NO.\n");
                return(-1);
            }

            if (+magic != BYTE_ORDER_MAGIC)
            {
                /* either need to swap or got bogus magic number */
                byteorder.SWAP_INT32(magic);

                if (+magic == BYTE_ORDER_MAGIC)
                {
                    return(1);
                }

                byteorder.SWAP_INT32(magic);
                err.E_ERROR(string.Format("Bad BYTEORDER MAGIC NO: {0:x8}, expecting {1:x8}\n",
                                          magic, BYTE_ORDER_MAGIC));
                return(-1);
            }

            return(0);
        }
예제 #3
0
        public static trigger_adapter trigger_create(string modelDir, string dictionaryFile, bool verboseLogging)
        {
            //printf("            creating recognizer\n");

            Pointer <ps_decoder_t> ps     = PointerHelpers.NULL <ps_decoder_t>();
            Pointer <cmd_ln_t>     config = PointerHelpers.NULL <cmd_ln_t>();

            config = cmd_ln.cmd_ln_init(config, pocketsphinx.ps_args(), 1,
                                        "-hmm", modelDir,
                                        "-dict", dictionaryFile,
                                        "-verbose", "y");

            ps = pocketsphinx.ps_init(config);

            cmd_ln.cmd_ln_free_r(config);

            trigger_adapter adapter = new trigger_adapter();

            adapter.ps = ps;
            adapter.user_is_speaking = false;
            adapter.last_hyp         = PointerHelpers.Malloc <byte>(512);
            adapter.last_hyp[0]      = 0;

            return(adapter);
        }
예제 #4
0
        public void TestVoidPointerByteToULong()
        {
            Pointer <byte>  pointer     = PointerHelpers.Malloc <byte>(80);
            Pointer <ulong> voidPointer = pointer.Point(20).ReinterpretCast <ulong>();

            pointer[20] = 80;
            pointer[21] = 226;
            pointer[22] = 182;
            pointer[23] = 173;
            pointer[24] = 76;
            pointer[25] = 82;
            pointer[26] = 0;
            pointer[27] = 0;
            Assert.AreEqual(90489285435984UL, voidPointer[0]);
            Assert.AreEqual(0UL, voidPointer[1]);
            voidPointer[2] = 67823904875254UL;
            Assert.AreEqual(246, pointer[36]);
            Assert.AreEqual(226, pointer[37]);
            Assert.AreEqual(193, pointer[38]);
            Assert.AreEqual(123, pointer[39]);
            Assert.AreEqual(175, pointer[40]);
            Assert.AreEqual(61, pointer[41]);
            Assert.AreEqual(0, pointer[42]);
            Assert.AreEqual(0, pointer[43]);
        }
예제 #5
0
        /* Parse tmat and state.Deref.senone mappings for phone p and fill in structure */
        public static void parse_tmat_senmap(Pointer <mdef_t> m, Pointer <byte> line, long off, int p)
        {
            int            wlen, n, s;
            Pointer <byte> lp;
            Pointer <byte> word = PointerHelpers.Malloc <byte>(1024);

            lp = line + (int)off;

            /* Read transition matrix id */
            if ((stdio.sscanf_d_n(lp, out n, out wlen) != 1) || (n < 0))
            {
                err.E_FATAL(string.Format("Missing or bad transition matrix id: {0}\n", cstring.FromCString(line)));
            }
            m.Deref.phone[p].tmat = n;
            if (m.Deref.n_tmat <= n)
            {
                err.E_FATAL(string.Format("tmat-id({0}) > #tmat in header({1}): {2}\n", n, m.Deref.n_tmat,
                                          cstring.FromCString(line)));
            }
            lp += wlen;

            /* Read senone mappings for each emitting state */
            for (n = 0; n < m.Deref.n_emit_state; n++)
            {
                if ((stdio.sscanf_d_n(lp, out s, out wlen) != 1) || (s < 0))
                {
                    err.E_FATAL(string.Format("Missing or bad state[{0}].Deref.senone mapping: {1}\n", n,
                                              cstring.FromCString(line)));
                }

                if ((p < m.Deref.n_ciphone) && (m.Deref.n_ci_sen <= s))
                {
                    err.E_FATAL(string.Format("CI-senone-id({0}) > #CI-senones({1}): {2}\n", s,
                                              m.Deref.n_ci_sen, cstring.FromCString(line)));
                }
                if (m.Deref.n_sen <= s)
                {
                    err.E_FATAL(string.Format("Senone-id({0}) > #senones({1}): {2}\n", s, m.Deref.n_sen,
                                              cstring.FromCString(line)));
                }

                Pointer <ushort> tmp = m.Deref.sseq[p];
                tmp[n] = (ushort)s;
                lp    += wlen;
            }

            /* Check for the last non-emitting state N */
            if ((stdio.sscanf_s_n(lp, word, out wlen) != 1) || (cstring.strcmp(word, cstring.ToCString("N")) != 0))
            {
                err.E_FATAL(string.Format("Missing non-emitting state spec: {0}\n", cstring.FromCString(line)));
            }
            lp += wlen;

            /* Check for end of line */
            if (stdio.sscanf_s_n(lp, word, out wlen) == 1)
            {
                err.E_FATAL(string.Format("Non-empty beyond non-emitting final state: {0}\n", cstring.FromCString(line)));
            }
        }
예제 #6
0
        public void TestPointerToBoolean()
        {
            Pointer <int> ptr = default(Pointer <int>);

            Assert.IsFalse(ptr.IsNonNull);
            ptr = PointerHelpers.Malloc <int>(10);
            Assert.IsTrue(ptr.IsNonNull);
            ptr.Free();
            Assert.IsFalse(ptr.IsNonNull);
        }
예제 #7
0
        public void TestPointerIncrement()
        {
            Pointer <int> ptr  = PointerHelpers.Malloc <int>(10);
            Pointer <int> ptr2 = ptr;

            for (int c = 0; c < 10; c++, ptr2++)
            {
                ptr[c] = c;
                Assert.AreEqual(c, ptr2.Deref);
            }
        }
예제 #8
0
        public void TestPointerDeref()
        {
            Pointer <int> ptr = PointerHelpers.Malloc <int>(10);

            Assert.AreEqual(0, +ptr);
            ptr = PointerHelpers.Malloc <int>(0);
            try
            {
                int x = +ptr;
                Assert.Fail("Should have thrown exception");
            }
            catch (Exception) { }
        }
예제 #9
0
        public static void bcomment_read(FILE fp)
        {
            Pointer <byte> iline = PointerHelpers.Malloc <byte>(16384);

            while (fp.fgets(iline, 16384).IsNonNull)
            {
                if (cstring.strcmp(iline, END_COMMENT) == 0)
                {
                    return;
                }
            }

            err.E_FATAL(string.Format("Missing {0} marker\n", cstring.FromCString(END_COMMENT)));
        }
예제 #10
0
        public void TestVoidPointerByteToSbyte()
        {
            Pointer <byte>  pointer     = PointerHelpers.Malloc <byte>(80);
            Pointer <sbyte> voidPointer = pointer.ReinterpretCast <sbyte>();

            pointer[0] = 99;
            pointer[1] = 128;
            Assert.AreEqual((sbyte)99, voidPointer[0]);
            Assert.AreEqual((sbyte)-128, voidPointer[1]);
            pointer[20] = 53;
            pointer[21] = 240;
            voidPointer = voidPointer + 20;
            Assert.AreEqual((sbyte)53, voidPointer[0]);
            Assert.AreEqual((sbyte)-16, voidPointer[1]);
        }
예제 #11
0
        public void TestPointerReallocSmaller()
        {
            Pointer <int> pointer = PointerHelpers.Malloc <int>(8);

            for (int c = 0; c < 8; c++)
            {
                pointer[c] = c;
            }

            pointer = pointer.Realloc(4);

            for (int c = 0; c < 4; c++)
            {
                Assert.AreEqual(c, pointer[c]);
            }
        }
예제 #12
0
        public static Pointer <byte> arg_resolve_env(Pointer <byte> str)
        {
            BoxedValue <Pointer <byte> > resolved_str = new BoxedValue <Pointer <byte> >(PointerHelpers.NULL <byte>());
            Pointer <byte>    env_name = PointerHelpers.Malloc <byte>(100);
            Pointer <byte>    env_val;
            BoxedValue <uint> alloced = new BoxedValue <uint>(0);
            Pointer <byte>    i       = str;
            Pointer <byte>    j;

            /* calculate required resolved_str size */
            do
            {
                j = cstring.strstr(i, cstring.ToCString("$("));
                if (j.IsNonNull)
                {
                    if (j != i)
                    {
                        strnappend(resolved_str, alloced, i, checked ((uint)(j - i)));
                        i = j;
                    }
                    j = cstring.strchr(i + 2, (byte)')');
                    if (j.IsNonNull)
                    {
                        if (j - (i + 2) < 100)
                        {
                            cstring.strncpy(env_name, i + 2, checked ((uint)(j - (i + 2))));
                            env_name[j - (i + 2)] = (byte)'\0';
                            env_val = PointerHelpers.NULL <byte>();
                        }
                        i = j + 1;
                    }
                    else
                    {
                        /* unclosed, copy and skip */
                        j = i + 2;
                        strnappend(resolved_str, alloced, i, checked ((uint)(j - i)));
                        i = j;
                    }
                }
                else
                {
                    strappend(resolved_str, alloced, i);
                }
            } while (j.IsNonNull);

            return(resolved_str.Val);
        }
예제 #13
0
        public void TestPointerReallocFree()
        {
            Pointer <int> pointer = PointerHelpers.Malloc <int>(8);

            for (int c = 0; c < 8; c++)
            {
                pointer[c] = c;
            }

            pointer = pointer.Realloc(0);

            try
            {
                pointer[0].GetHashCode();
                Assert.Fail();
            }
            catch (NullReferenceException) { }
        }
예제 #14
0
        public static void fe_warp_affine_set_parameters(Pointer <byte> param_str, float sampling_rate)
        {
            Pointer <byte> tok;
            Pointer <byte> seps           = cstring.ToCString(" \t");
            Pointer <byte> temp_param_str = PointerHelpers.Malloc <byte>(256);
            int            param_index    = 0;

            nyquist_frequency = sampling_rate / 2;
            if (param_str.IsNull)
            {
                is_neutral = 1;
                return;
            }
            /* The new parameters are the same as the current ones, so do nothing. */
            if (cstring.strcmp(param_str, p_str) == 0)
            {
                return;
            }
            is_neutral = 0;
            cstring.strcpy(temp_param_str, param_str);
            parameters.MemSet(0, N_PARAM);
            cstring.strcpy(p_str, param_str);
            /* FIXME: strtok() is not re-entrant... */
            tok = cstring.strtok(temp_param_str, seps);
            while (tok.IsNonNull)
            {
                parameters[param_index++] = (float)strfuncs.atof_c(tok);
                tok = cstring.strtok(PointerHelpers.NULL <byte>(), seps);
                if (param_index >= N_PARAM)
                {
                    break;
                }
            }
            if (tok.IsNonNull)
            {
                err.E_INFO(string.Format("Affine warping takes up to two arguments, {0} ignored.\n", cstring.FromCString(tok)));
            }
            if (parameters[0] == 0)
            {
                is_neutral = 1;
                err.E_INFO
                    ("Affine warping cannot have slope zero, warping not applied.\n");
            }
        }
예제 #15
0
        public void TestPointerPoint()
        {
            Pointer <int> ptr = PointerHelpers.Malloc <int>(10);

            for (int c = 0; c < 10; c++)
            {
                ptr[c] = c;
            }
            Pointer <int> ptr2 = ptr + 6;

            Assert.AreEqual(6, +ptr2);
            ptr2 = ptr.Point(6);
            Assert.AreEqual(6, +ptr2);
            ptr2 = ptr2 - 3;
            Assert.AreEqual(3, +ptr2);
            Pointer <int> ptr3 = ptr + 3;

            Assert.AreEqual(ptr2, ptr3);
        }
예제 #16
0
        public void TestScanfSN()
        {
            Pointer <byte> d = PointerHelpers.Malloc <byte>(50);
            int            len;
            int            r;

            r = stdio.sscanf_s_n(cstring.ToCString("  Three  "), d, out len);
            Assert.AreEqual("Three", cstring.FromCString(d));
            Assert.AreEqual(7, len);
            Assert.AreEqual(1, r);

            r = stdio.sscanf_s_n(cstring.ToCString("      "), d, out len);
            Assert.AreEqual(0, r);

            r = stdio.sscanf_s_n(cstring.ToCString("1"), d, out len);
            Assert.AreEqual("1", cstring.FromCString(d));
            Assert.AreEqual(1, len);
            Assert.AreEqual(1, r);
        }
예제 #17
0
        public static void bio_verify_chksum(FILE fp, int byteswap, uint chksum)
        {
            Pointer <byte> file_chksum_array = PointerHelpers.Malloc <byte>(4);
            Pointer <uint> file_chksum       = file_chksum_array.ReinterpretCast <uint>();

            if (fp.fread(file_chksum_array, 4, 1) != 1)
            {
                err.E_FATAL("fread(chksum) failed\n");
            }

            if (byteswap != 0)
            {
                byteorder.SWAP_INT32(file_chksum);
            }
            if (+file_chksum != chksum)
            {
                err.E_FATAL
                    (string.Format("Checksum error; file-checksum {0:x8}, computed {1:x8}\n", file_chksum, chksum));
            }
        }
예제 #18
0
        public static int FREAD_SWAP32_CHK(Pointer <bin_mdef_t> m, FILE fh, int swap, out int dest, Pointer <byte> filename)
        {
            Pointer <byte> buf     = PointerHelpers.Malloc <byte>(4);
            Pointer <int>  buf_int = buf.ReinterpretCast <int>();

            if (fh.fread(buf, 4, 1) != 1)
            {
                dest = 0;
                fh.fclose();
                ckd_alloc.ckd_free(m);
                err.E_ERROR_SYSTEM(string.Format("Failed to read {0} from {1}\n", dest, cstring.FromCString(filename)));
                return(-1);
            }

            dest = +buf_int;
            if (swap != 0)
            {
                dest = byteorder.SWAP_INT32(dest);
            }
            return(0);
        }
예제 #19
0
        public static void triphone_add(Pointer <mdef_t> m,
                                        short ci, short lc, short rc, int wpos,
                                        int p)
        {
            Pointer <ph_lc_t> lcptr;
            Pointer <ph_rc_t> rcptr;

            SphinxAssert.assert(p < m.Deref.n_phone);

            /* Fill in phone[p] information (state and tmat mappings added later) */
            m.Deref.phone[p].ci   = ci;
            m.Deref.phone[p].lc   = lc;
            m.Deref.phone[p].rc   = rc;
            m.Deref.phone[p].wpos = wpos;

            /* Create <ci,lc,rc,wpos> .Deref. p mapping if not a CI phone */
            if (p >= m.Deref.n_ciphone)
            {
                if ((lcptr = find_ph_lc(m.Deref.wpos_ci_lclist[wpos][(int)ci], lc)).IsNull)
                {
                    lcptr            = ckd_alloc.ckd_calloc_struct <ph_lc_t>(1); /* freed at mdef_free, I believe */
                    lcptr.Deref.lc   = lc;
                    lcptr.Deref.next = m.Deref.wpos_ci_lclist[wpos][(int)ci];
                    Pointer <Pointer <ph_lc_t> > tmp = m.Deref.wpos_ci_lclist[wpos];
                    tmp[(int)ci] = lcptr;  /* This is what needs to be freed */
                }
                if ((rcptr = find_ph_rc(lcptr.Deref.rclist, rc)).IsNonNull)
                {
                    Pointer <byte> buf = PointerHelpers.Malloc <byte>(4096);
                    mdef_phone_str(m, rcptr.Deref.pid, buf);
                    err.E_FATAL(string.Format("Duplicate triphone: {0}\n", cstring.FromCString(buf)));
                }

                rcptr              = ckd_alloc.ckd_calloc_struct <ph_rc_t>(1); /* freed in mdef_free, I believe */
                rcptr.Deref.rc     = rc;
                rcptr.Deref.pid    = p;
                rcptr.Deref.next   = lcptr.Deref.rclist;
                lcptr.Deref.rclist = rcptr;
            }
        }
예제 #20
0
        public static int bio_readhdr(FILE fp, BoxedValue <Pointer <Pointer <byte> > > argname, BoxedValue <Pointer <Pointer <byte> > > argval, out int swap)
        {
            Pointer <byte> line = PointerHelpers.Malloc <byte>(16384);
            Pointer <byte> word = PointerHelpers.Malloc <byte>(4096);
            int            i, l;
            int            lineno;

            argname.Val = ckd_alloc.ckd_calloc <Pointer <byte> >(BIO_HDRARG_MAX + 1);
            argval.Val  = ckd_alloc.ckd_calloc <Pointer <byte> >(BIO_HDRARG_MAX);

            lineno = 0;
            if (fp.fgets(line, 16384).IsNull)
            {
                err.E_ERROR(string.Format("Premature EOF, line {0}\n", lineno));
                goto error_out;
            }
            lineno++;

            if ((line[0] == 's') && (line[1] == '3') && (line[2] == '\n'))
            {
                /* New format (post Dec-1996, including checksums); read argument-value pairs */
                for (i = 0; ;)
                {
                    if (fp.fgets(line, 16384).IsNull)
                    {
                        err.E_ERROR(string.Format("Premature EOF, line {0}\n", lineno));
                        goto error_out;
                    }
                    lineno++;

                    if (stdio.sscanf_s_n(line, word, out l) != 1)
                    {
                        err.E_ERROR(string.Format("Header format error, line {0}\n", lineno));
                        goto error_out;
                    }
                    if (cstring.strcmp(word, cstring.ToCString("endhdr")) == 0)
                    {
                        break;
                    }
                    if (word[0] == '#') /* Skip comments */
                    {
                        continue;
                    }

                    if (i >= BIO_HDRARG_MAX)
                    {
                        err.E_ERROR
                            (string.Format("Max arg-value limit({0}) exceeded; increase BIO_HDRARG_MAX\n",
                                           BIO_HDRARG_MAX));
                        goto error_out;
                    }

                    argname.Val[i] = ckd_alloc.ckd_salloc(word);
                    if (stdio.sscanf_s(line + l, word) != 1)
                    {      /* Multi-word values not allowed */
                        err.E_ERROR(string.Format("Header format error, line {0}\n", lineno));
                        goto error_out;
                    }
                    argval.Val[i] = ckd_alloc.ckd_salloc(word);
                    i++;
                }
            }
            else
            {
                /* Old format (without checksums); the first entry must be the version# */
                if (stdio.sscanf_s(line, word) != 1)
                {
                    err.E_ERROR(string.Format("Header format error, line {0}\n", lineno));
                    goto error_out;
                }

                argname.Val[0] = ckd_alloc.ckd_salloc(cstring.ToCString("version"));
                argval.Val[0]  = ckd_alloc.ckd_salloc(word);
                i = 1;

                bcomment_read(fp);
            }
            argname.Val[i] = PointerHelpers.NULL <byte>();

            if ((swap = swap_check(fp)) < 0)
            {
                err.E_ERROR("swap_check failed\n");
                goto error_out;
            }

            return(0);

error_out:
            bio_hdrarg_free(argname.Val, argval.Val);
            argname.Val = argval.Val = PointerHelpers.NULL <Pointer <byte> >();
            swap        = 0;
            return(-1);
        }
예제 #21
0
        public static int ps_start_utt(Pointer <ps_decoder_t> ps)
        {
            int            rv;
            Pointer <byte> uttid = PointerHelpers.Malloc <byte>(16);

            if (ps.Deref.acmod.Deref.state == acmod_state_e.ACMOD_STARTED || ps.Deref.acmod.Deref.state == acmod_state_e.ACMOD_PROCESSING)
            {
                err.E_ERROR("Utterance already started\n");
                return(-1);
            }

            if (ps.Deref.search == null)
            {
                err.E_ERROR("No search module is selected, did you forget to specify a language model or grammar?\n");
                return(-1);
            }

            profile.ptmr_reset(ps.Deref.perf);
            profile.ptmr_start(ps.Deref.perf);

            stdio.sprintf(uttid, string.Format("{0}", ps.Deref.uttno));
            ++ps.Deref.uttno;

            /* Remove any residual word lattice and hypothesis. */
            ps_lattice.ps_lattice_free(ps.Deref.search.dag);
            ps.Deref.search.dag       = PointerHelpers.NULL <ps_lattice_t>();
            ps.Deref.search.last_link = PointerHelpers.NULL <ps_latlink_t>();
            ps.Deref.search.post      = 0;
            ckd_alloc.ckd_free(ps.Deref.search.hyp_str);
            ps.Deref.search.hyp_str = PointerHelpers.NULL <byte>();
            if ((rv = acmod.acmod_start_utt(ps.Deref.acmod)) < 0)
            {
                return(rv);
            }

            /* Start logging features and audio if requested. */
            // LOGAN cut out logging

            /*if (ps.Deref.mfclogdir) {
             *  char *logfn = string_join(ps.Deref.mfclogdir, "/",
             *                            uttid, ".mfc", NULL);
             *  FILE *mfcfh;
             *  E_INFO("Writing MFCC file: %s\n", logfn);
             *  if ((mfcfh = fopen(logfn, "wb")) == NULL) {
             *      E_ERROR_SYSTEM("Failed to open MFCC file %s", logfn);
             *      ckd_alloc.ckd_free(logfn);
             *      return -1;
             *  }
             *  ckd_alloc.ckd_free(logfn);
             *  acmod_set_mfcfh(ps.Deref.acmod, mfcfh);
             * }
             * if (ps.Deref.rawlogdir) {
             *  char *logfn = string_join(ps.Deref.rawlogdir, "/",
             *                            uttid, ".raw", NULL);
             *  FILE *rawfh;
             *  E_INFO("Writing raw audio file: %s\n", logfn);
             *  if ((rawfh = fopen(logfn, "wb")) == NULL) {
             *      E_ERROR_SYSTEM("Failed to open raw audio file %s", logfn);
             *      ckd_alloc.ckd_free(logfn);
             *      return -1;
             *  }
             *  ckd_alloc.ckd_free(logfn);
             *  acmod_set_rawfh(ps.Deref.acmod, rawfh);
             * }
             * if (ps.Deref.senlogdir) {
             *  char *logfn = string_join(ps.Deref.senlogdir, "/",
             *                            uttid, ".sen", NULL);
             *  FILE *senfh;
             *  E_INFO("Writing senone score file: %s\n", logfn);
             *  if ((senfh = fopen(logfn, "wb")) == NULL) {
             *      E_ERROR_SYSTEM("Failed to open senone score file %s", logfn);
             *      ckd_alloc.ckd_free(logfn);
             *      return -1;
             *  }
             *  ckd_alloc.ckd_free(logfn);
             *  acmod_set_senfh(ps.Deref.acmod, senfh);
             * }*/

            /* Start auxiliary phone loop search. */
            if (ps.Deref.phone_loop != null)
            {
                ps_search_start(ps.Deref.phone_loop);
            }

            return(ps_search_start(ps.Deref.search));
        }
예제 #22
0
        public static int senone_mixw_read(Pointer <senone_t> s, Pointer <byte> file_name, Pointer <logmath_t> lmath)
        {
            FILE fp;
            int  byteswap, chksum_present;
            BoxedValue <uint> chksum = new BoxedValue <uint>();
            Pointer <float>   pdf;
            int i, f, c, p, n_err;
            Pointer <Pointer <byte> > argname;
            Pointer <Pointer <byte> > argval;

            err.E_INFO(string.Format("Reading senone mixture weights: {0}\n", cstring.FromCString(file_name)));

            if ((fp = FILE.fopen(file_name, "rb")) == null)
            {
                err.E_FATAL_SYSTEM(string.Format("Failed to open mixture weights file '{0}' for reading", cstring.FromCString(file_name)));
            }

            /* Read header, including argument-value info and 32-bit byteorder magic */
            BoxedValue <Pointer <Pointer <byte> > > boxed_argname = new BoxedValue <Pointer <Pointer <byte> > >();
            BoxedValue <Pointer <Pointer <byte> > > boxed_argval  = new BoxedValue <Pointer <Pointer <byte> > >();

            if (bio.bio_readhdr(fp, boxed_argname, boxed_argval, out byteswap) < 0)
            {
                err.E_FATAL(string.Format("Failed to read header from file '{0}'\n", cstring.FromCString(file_name)));
            }
            argname = boxed_argname.Val;
            argval  = boxed_argval.Val;

            /* Parse argument-value list */
            chksum_present = 0;
            for (i = 0; argname[i].IsNonNull; i++)
            {
                if (cstring.strcmp(argname[i], cstring.ToCString("version")) == 0)
                {
                    if (cstring.strcmp(argval[i], MIXW_PARAM_VERSION) != 0)
                    {
                        err.E_WARN(string.Format("Version mismatch({0}): {1}, expecting {2}\n",
                                                 cstring.FromCString(file_name), cstring.FromCString(argval[i]), cstring.FromCString(MIXW_PARAM_VERSION)));
                    }
                }
                else if (cstring.strcmp(argname[i], cstring.ToCString("chksum0")) == 0)
                {
                    chksum_present = 1; /* Ignore the associated value */
                }
            }
            bio.bio_hdrarg_free(argname, argval);
            argname = argval = PointerHelpers.NULL <Pointer <byte> >();

            chksum.Val = 0;

            /* Read #senones, #features, #codewords, arraysize */
            byte[]         temp_read_buf      = new byte[4];
            Pointer <byte> temp_read_ptr_byte = new Pointer <byte>(temp_read_buf);
            Pointer <uint> temp_read_ptr_uint = temp_read_ptr_byte.ReinterpretCast <uint>();

            if ((bio.bio_fread(temp_read_ptr_byte, 4, 1, fp, byteswap, chksum) != 1))
            {
                err.E_FATAL(string.Format("bio_fread({0}) (arraysize) failed\n", cstring.FromCString(file_name)));
            }
            s.Deref.n_sen = temp_read_ptr_uint[0];

            if ((bio.bio_fread(temp_read_ptr_byte, 4, 1, fp, byteswap, chksum) != 1))
            {
                err.E_FATAL(string.Format("bio_fread({0}) (arraysize) failed\n", cstring.FromCString(file_name)));
            }
            s.Deref.n_feat = temp_read_ptr_uint[0];

            if ((bio.bio_fread(temp_read_ptr_byte, 4, 1, fp, byteswap, chksum) != 1))
            {
                err.E_FATAL(string.Format("bio_fread({0}) (arraysize) failed\n", cstring.FromCString(file_name)));
            }
            s.Deref.n_cw = temp_read_ptr_uint[0];

            if ((bio.bio_fread(temp_read_ptr_byte, 4, 1, fp, byteswap, chksum) != 1))
            {
                err.E_FATAL(string.Format("bio_fread({0}) (arraysize) failed\n", cstring.FromCString(file_name)));
            }
            i = (int)temp_read_ptr_uint[0];

            if (i != s.Deref.n_sen * s.Deref.n_feat * s.Deref.n_cw)
            {
                err.E_FATAL
                    (string.Format("{0}: #float32s({1}) doesn't match dimensions: {2} x {3} x {4}\n",
                                   cstring.FromCString(file_name), i, s.Deref.n_sen, s.Deref.n_feat, s.Deref.n_cw));
            }

            /*
             * Compute #LSB bits to be dropped to represent mixwfloor with 8 bits.
             * All PDF values will be truncated (in the LSB positions) by these many bits.
             */
            if ((s.Deref.mixwfloor <= 0.0) || (s.Deref.mixwfloor >= 1.0))
            {
                err.E_FATAL(string.Format("mixwfloor ({0}) not in range (0, 1)\n", s.Deref.mixwfloor));
            }

            /* Use a fixed shift for compatibility with everything else. */
            err.E_INFO(string.Format("Truncating senone logs3(pdf) values by {0} bits\n", hmm.SENSCR_SHIFT));

            /*
             * Allocate memory for senone PDF data.  Organize normally or transposed depending on
             * s.Deref.n_gauden.
             */
            if (s.Deref.n_gauden > 1)
            {
                err.E_INFO("Not transposing mixture weights in memory\n");
                s.Deref.pdf = ckd_alloc.ckd_calloc_3d <byte>(s.Deref.n_sen, s.Deref.n_feat, s.Deref.n_cw);
            }
            else
            {
                err.E_INFO("Transposing mixture weights in memory\n");
                s.Deref.pdf = ckd_alloc.ckd_calloc_3d <byte>(s.Deref.n_feat, s.Deref.n_cw, s.Deref.n_sen);
            }

            /* Temporary structure to read in floats */
            pdf = ckd_alloc.ckd_calloc <float>(s.Deref.n_cw);
            Pointer <byte>  bytebuf  = PointerHelpers.Malloc <byte>(4 * s.Deref.n_cw);
            Pointer <float> floatbuf = bytebuf.ReinterpretCast <float>();

            /* Read senone probs data, normalize, floor, convert to logs3, truncate to 8 bits */
            n_err = 0;
            for (i = 0; i < s.Deref.n_sen; i++)
            {
                for (f = 0; f < s.Deref.n_feat; f++)
                {
                    if (bio.bio_fread(bytebuf, 4, (int)s.Deref.n_cw, fp, byteswap, chksum) != s.Deref.n_cw)
                    {
                        err.E_FATAL(string.Format("bio_fread({0}) (arraydata) failed\n", file_name));
                    }
                    floatbuf.MemCopyTo(pdf, (int)s.Deref.n_cw);

                    /* Normalize and floor */
                    if (vector.vector_sum_norm(pdf, checked ((int)s.Deref.n_cw)) <= 0.0)
                    {
                        n_err++;
                    }
                    vector.vector_floor(pdf, checked ((int)s.Deref.n_cw), s.Deref.mixwfloor);
                    vector.vector_sum_norm(pdf, checked ((int)s.Deref.n_cw));

                    /* Convert to logs3, truncate to 8 bits, and store in s.Deref.pdf */
                    for (c = 0; c < s.Deref.n_cw; c++)
                    {
                        p  = -(logmath.logmath_log(lmath, pdf[c]));
                        p += (1 << (hmm.SENSCR_SHIFT - 1)) - 1; /* Rounding before truncation */

                        if (s.Deref.n_gauden > 1)
                        {
                            s.Deref.pdf[i][f].Set(c, checked ((byte)((p < (255 << hmm.SENSCR_SHIFT)) ? (p >> hmm.SENSCR_SHIFT) : 255)));
                        }
                        else
                        {
                            s.Deref.pdf[f][c].Set(i, checked ((byte)((p < (255 << hmm.SENSCR_SHIFT)) ? (p >> hmm.SENSCR_SHIFT) : 255)));
                        }
                    }
                }
            }
            if (n_err > 0)
            {
                err.E_WARN(string.Format("Weight normalization failed for %d mixture weights components\n", n_err));
            }

            ckd_alloc.ckd_free(pdf);

            if (chksum_present != 0)
            {
                bio.bio_verify_chksum(fp, byteswap, chksum.Val);
            }

            if (fp.fread(temp_read_ptr_byte, 1, 1) == 1)
            {
                err.E_FATAL(string.Format("More data than expected in {0}\n", cstring.FromCString(file_name)));
            }

            fp.fclose();

            err.E_INFO
                (string.Format("Read mixture weights for {0} senones: {1} features x {2} codewords\n",
                               s.Deref.n_sen, s.Deref.n_feat, s.Deref.n_cw));

            return(1);
        }
예제 #23
0
        public static int bio_fread_3d(BoxedValue <Pointer <Pointer <Pointer <float> > > > arr,
                                       BoxedValue <uint> d1,
                                       BoxedValue <uint> d2,
                                       BoxedValue <uint> d3,
                                       FILE fp,
                                       uint swap,
                                       BoxedValue <uint> chksum)
        {
            MemoryBlock <byte> length_buf = new MemoryBlock <byte>(12);
            Pointer <byte>     length     = new Pointer <byte>(new BasicMemoryBlockAccess <byte>(length_buf), 0);
            Pointer <uint>     l_d1       = new Pointer <uint>(new UpcastingMemoryBlockAccess <uint>(length_buf), 0);
            Pointer <uint>     l_d2       = new Pointer <uint>(new UpcastingMemoryBlockAccess <uint>(length_buf), 4);
            Pointer <uint>     l_d3       = new Pointer <uint>(new UpcastingMemoryBlockAccess <uint>(length_buf), 8);
            uint           n   = 0;
            Pointer <byte> raw = PointerHelpers.NULL <byte>();
            uint           ret;

            ret = (uint)bio_fread(length.Point(0), 4, 1, fp, (int)swap, chksum);
            if (ret != 1)
            {
                if (ret == 0)
                {
                    err.E_ERROR_SYSTEM("Unable to read complete data");
                }
                else
                {
                    err.E_ERROR_SYSTEM("OS error in bio_fread_3d");
                }
                return(-1);
            }
            ret = (uint)bio_fread(length.Point(4), 4, 1, fp, (int)swap, chksum);
            if (ret != 1)
            {
                if (ret == 0)
                {
                    err.E_ERROR_SYSTEM("Unable to read complete data");
                }
                else
                {
                    err.E_ERROR_SYSTEM("OS error in bio_fread_3d");
                }
                return(-1);
            }
            ret = (uint)bio_fread(length.Point(8), 4, 1, fp, (int)swap, chksum);
            if (ret != 1)
            {
                if (ret == 0)
                {
                    err.E_ERROR_SYSTEM("Unable to read complete data");
                }
                else
                {
                    err.E_ERROR_SYSTEM("OS error in bio_fread_3d");
                }
                return(-1);
            }

            BoxedValue <Pointer <byte> > boxed_raw = new BoxedValue <Pointer <byte> >(raw);
            BoxedValue <uint>            boxed_n   = new BoxedValue <uint>(n);

            if (bio_fread_1d(boxed_raw, 4, boxed_n, fp, (int)swap, chksum) != n)
            {
                return(-1);
            }
            n   = boxed_n.Val;
            raw = boxed_raw.Val;

            SphinxAssert.assert(n == +l_d1 * +l_d2 * +l_d3);

            // LOGAN changed
            // Convert byte data to float
            Pointer <float> float_upcast_buf = raw.ReinterpretCast <float>();
            Pointer <float> float_copy_buf   = PointerHelpers.Malloc <float>(n);

            float_upcast_buf.MemCopyTo(float_copy_buf, (int)n);

            arr.Val = ckd_alloc.ckd_alloc_3d_ptr <float>(+l_d1, +l_d2, +l_d3, float_copy_buf);
            d1.Val  = +l_d1;
            d2.Val  = +l_d2;
            d3.Val  = +l_d3;

            return((int)n);
        }
예제 #24
0
        public static int senone_mgau_map_read(Pointer <senone_t> s, Pointer <byte> file_name)
        {
            FILE fp;
            int  byteswap, chksum_present, n_gauden_present;
            BoxedValue <uint> chksum = new BoxedValue <uint>();
            int i;
            Pointer <Pointer <byte> > argname;
            Pointer <Pointer <byte> > argval;
            object ptr;
            double v;

            err.E_INFO(string.Format("Reading senone gauden-codebook map file: {0}\n", cstring.FromCString(file_name)));

            if ((fp = FILE.fopen(file_name, "rb")) == null)
            {
                err.E_FATAL_SYSTEM(string.Format("Failed to open map file '{0}' for reading", cstring.FromCString(file_name)));
            }

            /* Read header, including argument-value info and 32-bit byteorder magic */
            BoxedValue <Pointer <Pointer <byte> > > boxed_argname = new BoxedValue <Pointer <Pointer <byte> > >();
            BoxedValue <Pointer <Pointer <byte> > > boxed_argval  = new BoxedValue <Pointer <Pointer <byte> > >();

            if (bio.bio_readhdr(fp, boxed_argname, boxed_argval, out byteswap) < 0)
            {
                err.E_FATAL(string.Format("Failed to read header from file '{0}'\n", cstring.FromCString(file_name)));
            }
            argname = boxed_argname.Val;
            argval  = boxed_argval.Val;

            /* Parse argument-value list */
            chksum_present   = 0;
            n_gauden_present = 0;
            for (i = 0; argname[i].IsNonNull; i++)
            {
                if (cstring.strcmp(argname[i], cstring.ToCString("version")) == 0)
                {
                    if (cstring.strcmp(argval[i], SPDEF_PARAM_VERSION) != 0)
                    {
                        err.E_WARN(string.Format("Version mismatch({0}): {1}, expecting {2}\n", file_name, argval[i], SPDEF_PARAM_VERSION));
                    }

                    /* HACK!! Convert version# to float32 and take appropriate action */
                    if (stdio.sscanf_f(argval[i], out v) != 1)
                    {
                        err.E_FATAL(string.Format("{0}: Bad version no. string: {1}\n", cstring.FromCString(file_name),
                                                  cstring.FromCString(argval[i])));
                    }

                    n_gauden_present = (v > 1.1) ? 1 : 0;
                }
                else if (cstring.strcmp(argname[i], cstring.ToCString("chksum0")) == 0)
                {
                    chksum_present = 1; /* Ignore the associated value */
                }
            }
            bio.bio_hdrarg_free(argname, argval);
            argname = argval = PointerHelpers.NULL <Pointer <byte> >();

            chksum.Val = 0;

            /* Read #gauden (if version matches) */
            byte[]         read_single_buf = new byte[4];
            Pointer <byte> read_byte_ptr   = new Pointer <byte>(read_single_buf);
            Pointer <uint> read_uint_ptr   = read_byte_ptr.ReinterpretCast <uint>();

            if (n_gauden_present != 0)
            {
                err.E_INFO(string.Format("Reading number of codebooks from {0}\n", cstring.FromCString(file_name)));
                if (bio.bio_fread(read_byte_ptr, 4, 1, fp, byteswap, chksum) != 1)
                {
                    err.E_FATAL(string.Format("fread({0}) (#gauden) failed\n", cstring.FromCString(file_name)));
                }
                s.Deref.n_gauden = read_uint_ptr[0];
            }

            /* Read 1d array data */
            BoxedValue <uint>            n_el      = new BoxedValue <uint>(s.Deref.n_sen);
            BoxedValue <Pointer <byte> > boxed_ptr = new BoxedValue <Pointer <byte> >();

            if (bio.bio_fread_1d(boxed_ptr, 4, n_el, fp, byteswap, chksum) < 0)
            {
                err.E_FATAL(string.Format("bio_fread_1d({0}) failed\n", cstring.FromCString(file_name)));
            }
            s.Deref.n_sen = n_el.Val;
            Pointer <uint> data_as_uint     = boxed_ptr.Val.ReinterpretCast <uint>();
            Pointer <uint> native_uint_data = PointerHelpers.Malloc <uint>(s.Deref.n_sen);

            data_as_uint.MemCopyTo(native_uint_data, (int)s.Deref.n_sen);
            s.Deref.mgau = native_uint_data;

            err.E_INFO(string.Format("Mapping {0} senones to {1} codebooks\n", s.Deref.n_sen, s.Deref.n_gauden));

            /* Infer n_gauden if not present in this version */
            if (n_gauden_present == 0)
            {
                s.Deref.n_gauden = 1;
                for (i = 0; i < s.Deref.n_sen; i++)
                {
                    if (s.Deref.mgau[i] >= s.Deref.n_gauden)
                    {
                        s.Deref.n_gauden = s.Deref.mgau[i] + 1;
                    }
                }
            }

            if (chksum_present != 0)
            {
                bio.bio_verify_chksum(fp, byteswap, chksum.Val);
            }

            if (fp.fread(read_byte_ptr, 1, 1) == 1)
            {
                err.E_FATAL(string.Format("More data than expected in {0}: {1}\n", cstring.FromCString(file_name), read_single_buf[0]));
            }

            fp.fclose();

            err.E_INFO(string.Format("Read {0}->{1} senone-codebook mappings\n", s.Deref.n_sen,
                                     s.Deref.n_gauden));

            return(1);
        }
예제 #25
0
        public static void parse_base_line(Pointer <mdef_t> m, Pointer <byte> line, int p)
        {
            int            wlen, n;
            Pointer <byte> word = PointerHelpers.Malloc <byte>(1024);
            Pointer <byte> lp;
            int            ci;

            lp = line;

            /* Read base phone name */
            if (stdio.sscanf_s_n(lp, word, out wlen) != 1)
            {
                err.E_FATAL(string.Format("Missing base phone name: {0}\n", cstring.FromCString(line)));
            }
            lp += wlen;

            /* Make sure it's not a duplicate */
            ci = mdef_ciphone_id(m, word);
            if (ci >= 0)
            {
                err.E_FATAL(string.Format("Duplicate base phone: {0}\n", cstring.FromCString(line)));
            }

            /* Add ciphone to ciphone table with id p */
            ciphone_add(m, word, p);
            ci = (int)p;

            /* Read and skip "-" for lc, rc, wpos */
            for (n = 0; n < 3; n++)
            {
                if ((stdio.sscanf_s_n(lp, word, out wlen) != 1) ||
                    (cstring.strcmp(word, cstring.ToCString("-")) != 0))
                {
                    err.E_FATAL(string.Format("Bad context info for base phone: {0}\n", cstring.FromCString(line)));
                }
                lp += wlen;
            }

            /* Read filler attribute, if present */
            if (stdio.sscanf_s_n(lp, word, out wlen) != 1)
            {
                err.E_FATAL(string.Format("Missing filler attribute field: {0}\n", cstring.FromCString(line)));
            }
            lp += wlen;
            if (cstring.strcmp(word, cstring.ToCString("filler")) == 0)
            {
                m.Deref.ciphone[(int)ci].filler = 1;
            }
            else if (cstring.strcmp(word, cstring.ToCString("n/a")) == 0)
            {
                m.Deref.ciphone[(int)ci].filler = 0;
            }
            else
            {
                err.E_FATAL(string.Format("Bad filler attribute field: {0}\n", cstring.FromCString(line)));
            }

            triphone_add(m, (short)ci, -1, -1, word_posn_t.WORD_POSN_UNDEFINED, p);

            /* Parse remainder of line: transition matrix and state.Deref.senone mappings */
            parse_tmat_senmap(m, line, lp - line, p);
        }
예제 #26
0
        public static Pointer <dict_t> dict_init(Pointer <cmd_ln_t> config, Pointer <bin_mdef_t> mdef)
        {
            FILE fp, fp2;
            int  n;
            Pointer <lineiter_t> li;
            Pointer <dict_t>     d;
            Pointer <short>      sil        = PointerHelpers.Malloc <short>(1);
            Pointer <byte>       dictfile   = PointerHelpers.NULL <byte>();
            Pointer <byte>       fillerfile = PointerHelpers.NULL <byte>();

            if (config.IsNonNull)
            {
                dictfile   = cmd_ln.cmd_ln_str_r(config, cstring.ToCString("-dict"));
                fillerfile = cmd_ln.cmd_ln_str_r(config, cstring.ToCString("_fdict"));
            }

            /*
             * First obtain #words in dictionary (for hash table allocation).
             * Reason: The PC NT system doesn't like to grow memory gradually.  Better to allocate
             * all the required memory in one go.
             */
            fp = null;
            n  = 0;
            if (dictfile.IsNonNull)
            {
                if ((fp = FILE.fopen(dictfile, "r")) == null)
                {
                    err.E_ERROR_SYSTEM(string.Format("Failed to open dictionary file '{0}' for reading", cstring.FromCString(dictfile)));
                    return(PointerHelpers.NULL <dict_t>());
                }
                for (li = pio.lineiter_start(fp); li.IsNonNull; li = pio.lineiter_next(li))
                {
                    if (0 != cstring.strncmp(li.Deref.buf, HASHES, 2) &&
                        0 != cstring.strncmp(li.Deref.buf, SEMICOLONS, 2))
                    {
                        n++;
                    }
                }

                fp.fseek(0L, FILE.SEEK_SET);
            }

            fp2 = null;
            if (fillerfile.IsNonNull)
            {
                if ((fp2 = FILE.fopen(fillerfile, "r")) == null)
                {
                    err.E_ERROR_SYSTEM(string.Format("Failed to open filler dictionary file '{0}' for reading", cstring.FromCString(fillerfile)));
                    fp.fclose();
                    return(PointerHelpers.NULL <dict_t>());
                }
                for (li = pio.lineiter_start(fp2); li.IsNonNull; li = pio.lineiter_next(li))
                {
                    if (0 != cstring.strncmp(li.Deref.buf, HASHES, 2) &&
                        0 != cstring.strncmp(li.Deref.buf, SEMICOLONS, 2))
                    {
                        n++;
                    }
                }

                fp2.fseek(0L, FILE.SEEK_SET);
            }

            /*
             * Allocate dict entries.  HACK!!  Allow some extra entries for words not in file.
             * Also check for type size restrictions.
             */
            d = ckd_alloc.ckd_calloc_struct <dict_t>(1);       /* freed in dict_free() */
            d.Deref.refcnt    = 1;
            d.Deref.max_words =
                (n + S3DICT_INC_SZ < s3types.MAX_S3WID) ? n + S3DICT_INC_SZ : s3types.MAX_S3WID;
            if (n >= s3types.MAX_S3WID)
            {
                err.E_ERROR(string.Format("Number of words in dictionaries ({0}) exceeds limit (%d)\n", n,
                                          s3types.MAX_S3WID));
                if (fp != null)
                {
                    fp.fclose();
                }
                if (fp2 != null)
                {
                    fp2.fclose();
                }
                ckd_alloc.ckd_free(d);
                return(PointerHelpers.NULL <dict_t>());
            }

            err.E_INFO(string.Format("Allocating {0} * {1} bytes ({2} KiB) for word entries\n",
                                     d.Deref.max_words, 28,
                                     d.Deref.max_words * 28 / 1024));
            d.Deref.word   = ckd_alloc.ckd_calloc_struct <dictword_t>(d.Deref.max_words);   /* freed in dict_free() */
            d.Deref.n_word = 0;
            if (mdef.IsNonNull)
            {
                d.Deref.mdef = bin_mdef.bin_mdef_retain(mdef);
            }

            /* Create new hash table for word strings; case-insensitive word strings */
            if (config.IsNonNull && cmd_ln.cmd_ln_exists_r(config, cstring.ToCString("-dictcase")) != 0)
            {
                d.Deref.nocase = cmd_ln.cmd_ln_boolean_r(config, cstring.ToCString("-dictcase"));
            }
            d.Deref.ht = hash_table.hash_table_new(d.Deref.max_words, d.Deref.nocase);

            /* Digest main dictionary file */
            if (fp != null)
            {
                err.E_INFO(string.Format("Reading main dictionary: {0}\n", cstring.FromCString(dictfile)));
                dict_read(fp, d);
                fp.fclose();
                err.E_INFO(string.Format("{0} words read\n", d.Deref.n_word));
            }

            if (dict_wordid(d, S3_START_WORD) != s3types.BAD_S3WID)
            {
                err.E_ERROR("Remove sentence start word '<s>' from the dictionary\n");
                dict_free(d);
                return(PointerHelpers.NULL <dict_t>());
            }
            if (dict_wordid(d, S3_FINISH_WORD) != s3types.BAD_S3WID)
            {
                err.E_ERROR("Remove sentence start word '</s>' from the dictionary\n");
                dict_free(d);
                return(PointerHelpers.NULL <dict_t>());
            }
            if (dict_wordid(d, S3_SILENCE_WORD) != s3types.BAD_S3WID)
            {
                err.E_ERROR("Remove silence word '<sil>' from the dictionary\n");
                dict_free(d);
                return(PointerHelpers.NULL <dict_t>());
            }

            /* Now the filler dictionary file, if it exists */
            d.Deref.filler_start = d.Deref.n_word;
            if (fp2 != null)
            {
                err.E_INFO(string.Format("Reading filler dictionary: {0}\n", cstring.FromCString(fillerfile)));
                dict_read(fp2, d);
                fp2.fclose();
                err.E_INFO(string.Format("{0} words read\n", d.Deref.n_word - d.Deref.filler_start));
            }
            if (mdef.IsNonNull)
            {
                sil.Deref = checked ((short)bin_mdef.bin_mdef_silphone(mdef));
            }
            else
            {
                sil.Deref = 0;
            }
            if (dict_wordid(d, S3_START_WORD) == s3types.BAD_S3WID)
            {
                dict_add_word(d, S3_START_WORD, sil, 1);
            }
            if (dict_wordid(d, S3_FINISH_WORD) == s3types.BAD_S3WID)
            {
                dict_add_word(d, S3_FINISH_WORD, sil, 1);
            }
            if (dict_wordid(d, S3_SILENCE_WORD) == s3types.BAD_S3WID)
            {
                dict_add_word(d, S3_SILENCE_WORD, sil, 1);
            }

            d.Deref.filler_end = d.Deref.n_word - 1;

            /* Initialize distinguished word-ids */
            d.Deref.startwid  = dict_wordid(d, S3_START_WORD);
            d.Deref.finishwid = dict_wordid(d, S3_FINISH_WORD);
            d.Deref.silwid    = dict_wordid(d, S3_SILENCE_WORD);

            if ((d.Deref.filler_start > d.Deref.filler_end) ||
                (dict_filler_word(d, d.Deref.silwid) == 0))
            {
                err.E_ERROR(string.Format("Word '{0}' must occur (only) in filler dictionary\n",
                                          cstring.FromCString(S3_SILENCE_WORD)));
                dict_free(d);
                return(PointerHelpers.NULL <dict_t>());
            }

            /* No check that alternative pronunciations for filler words are in filler range!! */

            return(d);
        }
예제 #27
0
 public static Pointer <T> ckd_malloc <T>(int num_elements) where T : struct
 {
     return(PointerHelpers.Malloc <T>(num_elements));
 }
예제 #28
0
        public static Pointer <tmat_t> tmat_init(Pointer <byte> file_name, Pointer <logmath_t> lmath, double tpfloor, int breport)
        {
            int  n_src, n_dst, n_tmat;
            FILE fp;
            int  byteswap, chksum_present;
            BoxedValue <uint>          chksum = new BoxedValue <uint>();
            Pointer <Pointer <float> > tp;
            int i, j, k, tp_per_tmat;
            Pointer <Pointer <byte> > argname;
            Pointer <Pointer <byte> > argval;
            Pointer <tmat_t>          t;

            if (breport != 0)
            {
                err.E_INFO(string.Format("Reading HMM transition probability matrices: {0}\n",
                                         cstring.FromCString(file_name)));
            }

            t = ckd_alloc.ckd_calloc_struct <tmat_t>(1);

            if ((fp = FILE.fopen(file_name, "rb")) == null)
            {
                err.E_FATAL_SYSTEM(string.Format("Failed to open transition file '{0}' for reading", cstring.FromCString(file_name)));
            }

            /* Read header, including argument-value info and 32-bit byteorder magic */
            BoxedValue <Pointer <Pointer <byte> > > boxed_argname = new BoxedValue <Pointer <Pointer <byte> > >();
            BoxedValue <Pointer <Pointer <byte> > > boxed_argval  = new BoxedValue <Pointer <Pointer <byte> > >();

            if (bio.bio_readhdr(fp, boxed_argname, boxed_argval, out byteswap) < 0)
            {
                err.E_FATAL(string.Format("Failed to read header from file '{0}'\n", cstring.FromCString(file_name)));
            }
            argname = boxed_argname.Val;
            argval  = boxed_argval.Val;

            /* Parse argument-value list */
            chksum_present = 0;
            for (i = 0; argname[i].IsNonNull; i++)
            {
                if (cstring.strcmp(argname[i], cstring.ToCString("version")) == 0)
                {
                    if (cstring.strcmp(argval[i], TMAT_PARAM_VERSION) != 0)
                    {
                        err.E_WARN(string.Format("Version mismatch({0}): }1}, expecting {2}\n",
                                                 cstring.FromCString(file_name), cstring.FromCString(argval[i]), cstring.FromCString(TMAT_PARAM_VERSION)));
                    }
                }
                else if (cstring.strcmp(argname[i], cstring.ToCString("chksum0")) == 0)
                {
                    chksum_present = 1; /* Ignore the associated value */
                }
            }
            bio.bio_hdrarg_free(argname, argval);
            argname = argval = PointerHelpers.NULL <Pointer <byte> >();

            chksum.Val = 0;

            Pointer <byte> fread_buf     = PointerHelpers.Malloc <byte>(4);
            Pointer <int>  fread_buf_int = fread_buf.ReinterpretCast <int>();

            /* Read #tmat, #from-states, #to-states, arraysize */
            if ((bio.bio_fread(fread_buf, 4, 1, fp, byteswap, chksum) != 1))
            {
                err.E_FATAL(string.Format("Failed to read header from '{0}'\n", cstring.FromCString(file_name)));
            }
            n_tmat = fread_buf_int.Deref;

            if ((bio.bio_fread(fread_buf, 4, 1, fp, byteswap, chksum) != 1))
            {
                err.E_FATAL(string.Format("Failed to read header from '{0}'\n", cstring.FromCString(file_name)));
            }
            n_src = fread_buf_int.Deref;

            if ((bio.bio_fread(fread_buf, 4, 1, fp, byteswap, chksum) != 1))
            {
                err.E_FATAL(string.Format("Failed to read header from '{0}'\n", cstring.FromCString(file_name)));
            }
            n_dst = fread_buf_int.Deref;

            if ((bio.bio_fread(fread_buf, 4, 1, fp, byteswap, chksum) != 1))
            {
                err.E_FATAL(string.Format("Failed to read header from '{0}'\n", cstring.FromCString(file_name)));
            }
            i = fread_buf_int.Deref;

            if (n_tmat >= short.MaxValue)
            {
                err.E_FATAL(string.Format("{0}: Number of transition matrices ({1}) exceeds limit ({2})\n", cstring.FromCString(file_name),
                                          n_tmat, short.MaxValue));
            }
            t.Deref.n_tmat = checked ((short)n_tmat);

            if (n_dst != n_src + 1)
            {
                err.E_FATAL(string.Format("{0}: Unsupported transition matrix. Number of source states ({1}) != number of target states ({2})-1\n", cstring.FromCString(file_name), n_src, n_dst));
            }
            t.Deref.n_state = checked ((short)n_src);

            if (i != t.Deref.n_tmat * n_src * n_dst)
            {
                err.E_FATAL(string.Format("{0}: Invalid transitions. Number of coefficients ({1}) doesn't match expected array dimension: {2} x {3} x {4}\n", cstring.FromCString(file_name), i, t.Deref.n_tmat, n_src, n_dst));
            }

            /* Allocate memory for tmat data */
            t.Deref.tp = ckd_alloc.ckd_calloc_3d <byte>((uint)t.Deref.n_tmat, (uint)n_src, (uint)n_dst);

            /* Temporary structure to read in the float data */
            tp = ckd_alloc.ckd_calloc_2d <float>((uint)n_src, (uint)n_dst);

            /* Read transition matrices, normalize and floor them, and convert to log domain */
            tp_per_tmat = n_src * n_dst;
            for (i = 0; i < t.Deref.n_tmat; i++)
            {
                byte[]         rawData     = new byte[tp_per_tmat * 4];
                Pointer <byte> rawData_ptr = new Pointer <byte>(rawData);
                if (bio.bio_fread(rawData_ptr, 4, tp_per_tmat, fp,
                                  byteswap, chksum) != tp_per_tmat)
                {
                    err.E_FATAL(string.Format("Failed to read transition matrix {0} from '{1}'\n", i, cstring.FromCString(file_name)));
                }

                // LOGAN modified - need to convert byte array to float
                Pointer <float> tp_tmp = tp[0];
                for (int c = 0; c < tp_per_tmat; c++)
                {
                    tp_tmp[c] = BitConverter.ToSingle(rawData, c * 4);
                }

                /* Normalize and floor */
                for (j = 0; j < n_src; j++)
                {
                    if (vector.vector_sum_norm(tp[j], n_dst) == 0.0)
                    {
                        err.E_WARN(string.Format("Normalization failed for transition matrix {0} from state {1}\n",
                                                 i, j));
                    }
                    vector.vector_nz_floor(tp[j], n_dst, tpfloor);
                    vector.vector_sum_norm(tp[j], n_dst);

                    /* Convert to logs3. */
                    for (k = 0; k < n_dst; k++)
                    {
                        int ltp;
                        /* Log and quantize them. */
                        ltp = -logmath.logmath_log(lmath, tp[j][k]) >> hmm.SENSCR_SHIFT;
                        if (ltp > 255)
                        {
                            ltp = 255;
                        }
                        t.Deref.tp[i][j].Set(k, (byte)ltp);
                    }
                }
            }

            ckd_alloc.ckd_free_2d(tp);

            if (chksum_present != 0)
            {
                bio.bio_verify_chksum(fp, byteswap, chksum.Val);
            }

            if (fp.fread(fread_buf, 1, 1) == 1)
            {
                err.E_ERROR("Non-empty file beyond end of data\n");
            }

            fp.fclose();

            if (tmat_chk_uppertri(t, lmath) < 0)
            {
                err.E_FATAL("Tmat not upper triangular\n");
            }
            if (tmat_chk_1skip(t, lmath) < 0)
            {
                err.E_FATAL("Topology not Left-to-Right or Bakis\n");
            }

            return(t);
        }
예제 #29
0
        /*
         * Initialize phones (ci and triphones) and state.Deref.senone mappings from .mdef file.
         */
        public static Pointer <mdef_t> mdef_init(Pointer <byte> mdeffile, int breport)
        {
            FILE           fp;
            int            n_ci, n_tri, n_map, n;
            Pointer <byte> tag = PointerHelpers.Malloc <byte>(1024);
            Pointer <byte> buf = PointerHelpers.Malloc <byte>(1024);
            Pointer <Pointer <ushort> > senmap;
            int p;
            int s, ci, cd;
            Pointer <mdef_t> m;

            if (mdeffile.IsNull)
            {
                err.E_FATAL("No mdef-file\n");
            }

            if (breport != 0)
            {
                err.E_INFO(string.Format("Reading model definition: {0}\n", cstring.FromCString(mdeffile)));
            }

            m = ckd_alloc.ckd_calloc_struct <mdef_t>(1);       /* freed in mdef_free */

            if ((fp = FILE.fopen(mdeffile, "r")) == null)
            {
                err.E_FATAL_SYSTEM(string.Format("Failed to open mdef file '{0} for reading", cstring.FromCString(mdeffile)));
            }

            if (noncomment_line(buf, 1024, fp) < 0)
            {
                err.E_FATAL(string.Format("Empty file: {0}\n", cstring.FromCString(mdeffile)));
            }

            if (cstring.strncmp(buf, cstring.ToCString("BMDF"), 4) == 0 || cstring.strncmp(buf, cstring.ToCString("FDMB"), 4) == 0)
            {
                err.E_INFO(string.Format("Found byte-order mark {0:x4}, assuming this is a binary mdef file\n", buf));
                fp.fclose();
                ckd_alloc.ckd_free(m);
                return(PointerHelpers.NULL <mdef_t>());
            }

            if (cstring.strncmp(buf, MODEL_DEF_VERSION, cstring.strlen(MODEL_DEF_VERSION)) != 0)
            {
                err.E_FATAL(string.Format("Version error: Expecing {0}, but read {1}\n",
                                          cstring.FromCString(MODEL_DEF_VERSION), cstring.FromCString(buf)));
            }

            /* Read #base phones, #triphones, #senone mappings defined in header */
            n_ci             = -1;
            n_tri            = -1;
            n_map            = -1;
            m.Deref.n_ci_sen = -1;
            m.Deref.n_sen    = -1;
            m.Deref.n_tmat   = -1;
            do
            {
                if (noncomment_line(buf, 1024, fp) < 0)
                {
                    err.E_FATAL("Incomplete header\n");
                }

                if ((stdio.sscanf_d_s(buf, out n, tag) != 2) || (n < 0))
                {
                    err.E_FATAL(string.Format("Error in header: %s\n", cstring.FromCString(buf)));
                }

                if (cstring.strcmp(tag, cstring.ToCString("n_base")) == 0)
                {
                    n_ci = n;
                }
                else if (cstring.strcmp(tag, cstring.ToCString("n_tri")) == 0)
                {
                    n_tri = n;
                }
                else if (cstring.strcmp(tag, cstring.ToCString("n_state_map")) == 0)
                {
                    n_map = n;
                }
                else if (cstring.strcmp(tag, cstring.ToCString("n_tied_ci_state")) == 0)
                {
                    m.Deref.n_ci_sen = n;
                }
                else if (cstring.strcmp(tag, cstring.ToCString("n_tied_state")) == 0)
                {
                    m.Deref.n_sen = n;
                }
                else if (cstring.strcmp(tag, cstring.ToCString("n_tied_tmat")) == 0)
                {
                    m.Deref.n_tmat = n;
                }
                else
                {
                    err.E_FATAL(string.Format("Unknown header line: {0}\n", cstring.FromCString(buf)));
                }
            } while ((n_ci < 0) || (n_tri < 0) || (n_map < 0) ||
                     (m.Deref.n_ci_sen < 0) || (m.Deref.n_sen < 0) || (m.Deref.n_tmat < 0));

            if ((n_ci == 0) || (m.Deref.n_ci_sen == 0) || (m.Deref.n_tmat == 0) ||
                (m.Deref.n_ci_sen > m.Deref.n_sen))
            {
                err.E_FATAL(string.Format("{0}: Error in header\n", cstring.FromCString(mdeffile)));
            }

            /* Check typesize limits */
            if (n_ci >= short.MaxValue)
            {
                err.E_FATAL(string.Format("{0}: #CI phones ({1}) exceeds limit ({2})\n", cstring.FromCString(mdeffile), n_ci,
                                          short.MaxValue));
            }
            if (n_ci + n_tri >= int.MaxValue) /* Comparison is always false... */
            {
                err.E_FATAL(string.Format("{0}: #Phones ({1}) exceeds limit ({2})\n", cstring.FromCString(mdeffile),
                                          n_ci + n_tri, int.MaxValue));
            }
            if (m.Deref.n_sen >= short.MaxValue)
            {
                err.E_FATAL(string.Format("{0}: #senones ({1}) exceeds limit ({2})\n", cstring.FromCString(mdeffile),
                                          m.Deref.n_sen, short.MaxValue));
            }
            if (m.Deref.n_tmat >= int.MaxValue) /* Comparison is always false... */
            {
                err.E_FATAL(string.Format("{0}: #tmats ({1}) exceeds limit ({2})\n", cstring.FromCString(mdeffile),
                                          m.Deref.n_tmat, int.MaxValue));
            }

            m.Deref.n_emit_state = (n_map / (n_ci + n_tri)) - 1;
            if ((m.Deref.n_emit_state + 1) * (n_ci + n_tri) != n_map)
            {
                err.E_FATAL
                    ("Header error: n_state_map not a multiple of n_ci*n_tri\n");
            }

            /* Initialize ciphone info */
            m.Deref.n_ciphone  = n_ci;
            m.Deref.ciphone_ht = hash_table.hash_table_new(n_ci, hash_table.HASH_CASE_YES); /* With case-insensitive string names *//* freed in mdef_free */
            m.Deref.ciphone    = ckd_alloc.ckd_calloc_struct <ciphone_t>(n_ci);             /* freed in mdef_free */

            /* Initialize phones info (ciphones + triphones) */
            m.Deref.n_phone = n_ci + n_tri;
            m.Deref.phone   = ckd_alloc.ckd_calloc_struct <phone_t>(m.Deref.n_phone);  /* freed in mdef_free */

            /* Allocate space for state.Deref.senone map for each phone */
            senmap       = ckd_alloc.ckd_calloc_2d <ushort>((uint)m.Deref.n_phone, (uint)m.Deref.n_emit_state); /* freed in mdef_free */
            m.Deref.sseq = senmap;                                                                              /* TEMPORARY; until it is compressed into just the unique ones */

            /* Allocate initial space for <ci,lc,rc,wpos> .Deref. pid mapping */
            m.Deref.wpos_ci_lclist = ckd_alloc.ckd_calloc_2d <Pointer <ph_lc_t> >((uint)N_WORD_POSN, (uint)m.Deref.n_ciphone);      /* freed in mdef_free */

            /*
             * Read base phones and triphones.  They'll simply be assigned a running sequence
             * number as their "phone-id".  If the phone-id < n_ci, it's a ciphone.
             */

            /* Read base phones */
            for (p = 0; p < n_ci; p++)
            {
                if (noncomment_line(buf, 1024, fp) < 0)
                {
                    err.E_FATAL(string.Format("Premature EOF reading CIphone {0}\n", p));
                }
                parse_base_line(m, buf, p);
            }
            m.Deref.sil = (short)mdef_ciphone_id(m, S3_SILENCE_CIPHONE);

            /* Read triphones, if any */
            for (; p < m.Deref.n_phone; p++)
            {
                if (noncomment_line(buf, 1024, fp) < 0)
                {
                    err.E_FATAL(string.Format("Premature EOF reading phone {0}\n", p));
                }
                parse_tri_line(m, buf, p);
            }

            if (noncomment_line(buf, 1024, fp) >= 0)
            {
                err.E_ERROR(string.Format("Non-empty file beyond expected #phones ({0})\n",
                                          m.Deref.n_phone));
            }

            /* Build CD senones to CI senones map */
            if (m.Deref.n_ciphone * m.Deref.n_emit_state != m.Deref.n_ci_sen)
            {
                err.E_FATAL
                    (string.Format("#CI-senones({0}) != #CI-phone({1}) x #emitting-states({2})\n",
                                   m.Deref.n_ci_sen, m.Deref.n_ciphone, m.Deref.n_emit_state));
            }
            m.Deref.cd2cisen = ckd_alloc.ckd_calloc <short>(m.Deref.n_sen);  /* freed in mdef_free */

            m.Deref.sen2cimap = ckd_alloc.ckd_calloc <short>(m.Deref.n_sen); /* freed in mdef_free */

            for (s = 0; s < m.Deref.n_sen; s++)
            {
                m.Deref.sen2cimap[s] = -1;
            }
            for (s = 0; s < m.Deref.n_ci_sen; s++)
            { /* CI senones */
                m.Deref.cd2cisen[s]  = (short)s;
                m.Deref.sen2cimap[s] = (short)(s / m.Deref.n_emit_state);
            }
            for (p = n_ci; p < m.Deref.n_phone; p++)
            {       /* CD senones */
                for (s = 0; s < m.Deref.n_emit_state; s++)
                {
                    cd = m.Deref.sseq[p][s];
                    ci = m.Deref.sseq[m.Deref.phone[p].ci][s];
                    m.Deref.cd2cisen[cd]  = (short)ci;
                    m.Deref.sen2cimap[cd] = m.Deref.phone[p].ci;
                }
            }

            sseq_compress(m);
            fp.fclose();

            return(m);
        }
예제 #30
0
        public static void parse_tri_line(Pointer <mdef_t> m, Pointer <byte> line, int p)
        {
            int            wlen;
            Pointer <byte> word = PointerHelpers.Malloc <byte>(1024);
            Pointer <byte> lp;
            int            ci, lc, rc;
            int            wpos = word_posn_t.WORD_POSN_BEGIN;

            lp = line;

            /* Read base phone name */
            if (stdio.sscanf_s_n(lp, word, out wlen) != 1)
            {
                err.E_FATAL(string.Format("Missing base phone name: {0}\n", cstring.FromCString(line)));
            }
            lp += wlen;

            ci = mdef_ciphone_id(m, word);
            if (ci < 0)
            {
                err.E_FATAL(string.Format("Unknown base phone: {0}\n", cstring.FromCString(line)));
            }

            /* Read lc */
            if (stdio.sscanf_s_n(lp, word, out wlen) != 1)
            {
                err.E_FATAL(string.Format("Missing left context: {0}\n", cstring.FromCString(line)));
            }
            lp += wlen;
            lc  = mdef_ciphone_id(m, word);
            if (lc < 0)
            {
                err.E_FATAL(string.Format("Unknown left context: {0}\n", cstring.FromCString(line)));
            }

            /* Read rc */
            if (stdio.sscanf_s_n(lp, word, out wlen) != 1)
            {
                err.E_FATAL(string.Format("Missing right context: {0}\n", cstring.FromCString(line)));
            }
            lp += wlen;
            rc  = mdef_ciphone_id(m, word);
            if (rc < 0)
            {
                err.E_FATAL(string.Format("Unknown right  context: {0}\n", cstring.FromCString(line)));
            }

            /* Read tripone word-position within word */
            if ((stdio.sscanf_s_n(lp, word, out wlen) != 1) || (word[1] != '\0'))
            {
                err.E_FATAL(string.Format("Missing or bad word-position spec: {0}\n", cstring.FromCString(line)));
            }
            lp += wlen;
            switch (word[0])
            {
            case (byte)'b':
                wpos = word_posn_t.WORD_POSN_BEGIN;
                break;

            case (byte)'e':
                wpos = word_posn_t.WORD_POSN_END;
                break;

            case (byte)'s':
                wpos = word_posn_t.WORD_POSN_SINGLE;
                break;

            case (byte)'i':
                wpos = word_posn_t.WORD_POSN_INTERNAL;
                break;

            default:
                err.E_FATAL(string.Format("Bad word-position spec: {0}\n", cstring.FromCString(line)));
                break;
            }

            /* Read filler attribute, if present.  Must match base phone attribute */
            if (stdio.sscanf_s_n(lp, word, out wlen) != 1)
            {
                err.E_FATAL(string.Format("Missing filler attribute field: {0}\n", cstring.FromCString(line)));
            }
            lp += wlen;
            if (((cstring.strcmp(word, cstring.ToCString("filler")) == 0) && (m.Deref.ciphone[(int)ci].filler != 0)) ||
                ((cstring.strcmp(word, cstring.ToCString("n/a")) == 0) && (m.Deref.ciphone[(int)ci].filler == 0)))
            {
                /* Everything is fine */
            }
            else
            {
                err.E_FATAL(string.Format("Bad filler attribute field: {0}\n", cstring.FromCString(line)));
            }

            triphone_add(m, (short)ci, (short)lc, (short)rc, wpos, p);

            /* Parse remainder of line: transition matrix and state.Deref.senone mappings */
            parse_tmat_senmap(m, line, lp - line, p);
        }