PocketSphinx
0.6
|
00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 00002 /* ==================================================================== 00003 * Copyright (c) 2008 Carnegie Mellon University. All rights 00004 * reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in 00015 * the documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * This work was supported in part by funding from the Defense Advanced 00019 * Research Projects Agency and the National Science Foundation of the 00020 * United States of America, and the CMU Sphinx Speech Consortium. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 00023 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 00024 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00025 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 00026 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00028 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00029 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00030 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * ==================================================================== 00035 * 00036 */ 00037 00043 #ifndef __ACMOD_H__ 00044 #define __ACMOD_H__ 00045 00046 /* System headers. */ 00047 #include <stdio.h> 00048 00049 /* SphinxBase headers. */ 00050 #include <sphinxbase/cmd_ln.h> 00051 #include <sphinxbase/logmath.h> 00052 #include <sphinxbase/fe.h> 00053 #include <sphinxbase/feat.h> 00054 #include <sphinxbase/bitvec.h> 00055 #include <sphinxbase/err.h> 00056 00057 /* Local headers. */ 00058 #include "ps_mllr.h" 00059 #include "bin_mdef.h" 00060 #include "tmat.h" 00061 #include "hmm.h" 00062 00066 typedef enum acmod_state_e { 00067 ACMOD_IDLE, 00068 ACMOD_STARTED, 00069 ACMOD_PROCESSING, 00070 ACMOD_ENDED 00071 } acmod_state_t; 00072 00076 #define SENSCR_DUMMY 0x7fff 00077 00081 struct ps_mllr_s { 00082 int refcnt; 00083 int n_class; 00084 int n_feat; 00085 int *veclen; 00086 float32 ****A; 00087 float32 ***b; 00088 float32 ***h; 00089 int32 *cb2mllr; 00090 }; 00091 00095 typedef struct ps_mgau_s ps_mgau_t; 00096 00097 typedef struct ps_mgaufuncs_s { 00098 char const *name; 00099 00100 int (*frame_eval)(ps_mgau_t *mgau, 00101 int16 *senscr, 00102 uint8 *senone_active, 00103 int32 n_senone_active, 00104 mfcc_t ** feat, 00105 int32 frame, 00106 int32 compallsen); 00107 int (*transform)(ps_mgau_t *mgau, 00108 ps_mllr_t *mllr); 00109 void (*free)(ps_mgau_t *mgau); 00110 } ps_mgaufuncs_t; 00111 00112 struct ps_mgau_s { 00113 ps_mgaufuncs_t *vt; 00114 int frame_idx; 00115 }; 00116 00117 #define ps_mgau_base(mg) ((ps_mgau_t *)(mg)) 00118 #define ps_mgau_frame_eval(mg,senscr,senone_active,n_senone_active,feat,frame,compallsen) \ 00119 (*ps_mgau_base(mg)->vt->frame_eval) \ 00120 (mg, senscr, senone_active, n_senone_active, feat, frame, compallsen) 00121 #define ps_mgau_transform(mg, mllr) \ 00122 (*ps_mgau_base(mg)->vt->transform)(mg, mllr) 00123 #define ps_mgau_free(mg) \ 00124 (*ps_mgau_base(mg)->vt->free)(mg) 00125 00147 struct acmod_s { 00148 /* Global objects, not retained. */ 00149 cmd_ln_t *config; 00150 logmath_t *lmath; 00151 glist_t strings; 00153 /* Feature computation: */ 00154 fe_t *fe; 00155 feat_t *fcb; 00157 /* Model parameters: */ 00158 bin_mdef_t *mdef; 00159 tmat_t *tmat; 00160 ps_mgau_t *mgau; 00161 ps_mllr_t *mllr; 00163 /* Senone scoring: */ 00164 int16 *senone_scores; 00165 bitvec_t *senone_active_vec; 00166 uint8 *senone_active; 00167 int senscr_frame; 00168 int n_senone_active; 00169 int log_zero; 00171 /* Utterance processing: */ 00172 mfcc_t **mfc_buf; 00173 mfcc_t ***feat_buf; 00174 FILE *rawfh; 00175 FILE *mfcfh; 00176 FILE *senfh; 00177 FILE *insenfh; 00178 long *framepos; 00180 /* A whole bunch of flags and counters: */ 00181 uint8 state; 00182 uint8 compallsen; 00183 uint8 grow_feat; 00184 uint8 insen_swap; 00185 int16 output_frame; 00186 int16 n_mfc_alloc; 00187 int16 n_mfc_frame; 00188 int16 mfc_outidx; 00189 int16 n_feat_alloc; 00190 int16 n_feat_frame; 00191 int16 feat_outidx; 00192 }; 00193 typedef struct acmod_s acmod_t; 00194 00211 acmod_t *acmod_init(cmd_ln_t *config, logmath_t *lmath, fe_t *fe, feat_t *fcb); 00212 00224 ps_mllr_t *acmod_update_mllr(acmod_t *acmod, ps_mllr_t *mllr); 00225 00233 int acmod_set_senfh(acmod_t *acmod, FILE *senfh); 00234 00242 int acmod_set_mfcfh(acmod_t *acmod, FILE *logfh); 00243 00251 int acmod_set_rawfh(acmod_t *acmod, FILE *logfh); 00252 00256 void acmod_free(acmod_t *acmod); 00257 00261 int acmod_start_utt(acmod_t *acmod); 00262 00266 int acmod_end_utt(acmod_t *acmod); 00267 00280 int acmod_rewind(acmod_t *acmod); 00281 00291 int acmod_advance(acmod_t *acmod); 00292 00301 int acmod_set_grow(acmod_t *acmod, int grow_feat); 00302 00321 int acmod_process_raw(acmod_t *acmod, 00322 int16 const **inout_raw, 00323 size_t *inout_n_samps, 00324 int full_utt); 00325 00337 int acmod_process_cep(acmod_t *acmod, 00338 mfcc_t ***inout_cep, 00339 int *inout_n_frames, 00340 int full_utt); 00341 00355 int acmod_process_feat(acmod_t *acmod, 00356 mfcc_t **feat); 00357 00364 int acmod_set_insenfh(acmod_t *acmod, FILE *insenfh); 00365 00371 int acmod_read_scores(acmod_t *acmod); 00372 00382 mfcc_t **acmod_get_frame(acmod_t *acmod, int *inout_frame_idx); 00383 00397 int16 const *acmod_score(acmod_t *acmod, 00398 int *inout_frame_idx); 00399 00403 int acmod_write_senfh_header(acmod_t *acmod, FILE *logfh); 00404 00408 int acmod_write_scores(acmod_t *acmod, int n_active, uint8 const *active, 00409 int16 const *senscr, FILE *senfh); 00410 00411 00415 int acmod_best_score(acmod_t *acmod, int *out_best_senid); 00416 00420 void acmod_clear_active(acmod_t *acmod); 00421 00425 void acmod_activate_hmm(acmod_t *acmod, hmm_t *hmm); 00426 00430 #define acmod_activate_sen(acmod, sen) bitvec_set((acmod)->senone_active_vec, sen) 00431 00435 int32 acmod_flags2list(acmod_t *acmod); 00436 00437 #endif /* __ACMOD_H__ */