hmm.h
Go to the documentation of this file.
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ====================================================================
* Copyright (c) 1999-2004 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* This work was supported in part by funding from the Defense Advanced
* Research Projects Agency and the National Science Foundation of the
* United States of America, and the CMU Sphinx Speech Consortium.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
*/
#ifndef __HMM_H__
#define __HMM_H__
/* System headers. */
#include <stdio.h>
/* SphinxBase headers. */
#include <sphinxbase/fixpoint.h>
#include <sphinxbase/listelem_alloc.h>
/* PocketSphinx headers. */
#include "bin_mdef.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef int32 frame_idx_t;
#define MAX_N_FRAMES MAX_INT32
#define SENSCR_SHIFT 10
#define WORST_SCORE ((int)0xE0000000)
#define TMAT_WORST_SCORE (-255)
#define BETTER_THAN >
#define WORSE_THAN <
typedef struct hmm_context_s {
int32 n_emit_state;
uint8 ** const *tp;
int16 const *senscore;
uint16 * const *sseq;
int32 *st_sen_scr;
listelem_alloc_t *mpx_ssid_alloc;
void *udata;
} hmm_context_t;
#define HMM_MAX_NSTATE 5
typedef struct hmm_s {
hmm_context_t *ctx;
int32 score[HMM_MAX_NSTATE];
int32 history[HMM_MAX_NSTATE];
int32 out_score;
int32 out_history;
uint16 ssid;
uint16 senid[HMM_MAX_NSTATE];
int32 bestscore;
int16 tmatid;
frame_idx_t frame;
uint8 mpx;
uint8 n_emit_state;
} hmm_t;
#define hmm_context(h) (h)->ctx
#define hmm_is_mpx(h) (h)->mpx
#define hmm_in_score(h) (h)->score[0]
#define hmm_score(h,st) (h)->score[st]
#define hmm_out_score(h) (h)->out_score
#define hmm_in_history(h) (h)->history[0]
#define hmm_history(h,st) (h)->history[st]
#define hmm_out_history(h) (h)->out_history
#define hmm_bestscore(h) (h)->bestscore
#define hmm_frame(h) (h)->frame
#define hmm_mpx_ssid(h,st) (h)->senid[st]
#define hmm_nonmpx_ssid(h) (h)->ssid
#define hmm_ssid(h,st) (hmm_is_mpx(h) \
? hmm_mpx_ssid(h,st) : hmm_nonmpx_ssid(h))
#define hmm_mpx_senid(h,st) (hmm_mpx_ssid(h,st) == BAD_SENID \
? BAD_SENID : (h)->ctx->sseq[hmm_mpx_ssid(h,st)][st])
#define hmm_nonmpx_senid(h,st) ((h)->senid[st])
#define hmm_senid(h,st) (hmm_is_mpx(h) \
? hmm_mpx_senid(h,st) : hmm_nonmpx_senid(h,st))
#define hmm_senscr(h,st) (hmm_senid(h,st) == BAD_SENID \
? WORST_SCORE \
: -(h)->ctx->senscore[hmm_senid(h,st)])
#define hmm_tmatid(h) (h)->tmatid
#define hmm_tprob(h,i,j) (-(h)->ctx->tp[hmm_tmatid(h)][i][j])
#define hmm_n_emit_state(h) ((h)->n_emit_state)
#define hmm_n_state(h) ((h)->n_emit_state + 1)
hmm_context_t *hmm_context_init(int32 n_emit_state,
uint8 ** const *tp,
int16 const *senscore,
uint16 * const *sseq);
#define hmm_context_set_senscore(ctx, senscr) ((ctx)->senscore = (senscr))
void hmm_context_free(hmm_context_t *ctx);
void hmm_init(hmm_context_t *ctx, hmm_t *hmm, int mpx, int ssid, int tmatid);
void hmm_deinit(hmm_t *hmm);
void hmm_clear(hmm_t *h);
void hmm_clear_scores(hmm_t *h);
void hmm_normalize(hmm_t *h, int32 bestscr);
void hmm_enter(hmm_t *h, int32 score,
int32 histid, int frame);
int32 hmm_vit_eval(hmm_t *hmm);
int32 hmm_dump_vit_eval(hmm_t *hmm,
FILE *fp
);
void hmm_dump(hmm_t *h,
FILE *fp
);
#ifdef __cplusplus
}
#endif
#endif /* __HMM_H__ */
bin_mdef.h
Binary format model definition files, with support for heterogeneous topologies and variable-size N-p...
hmm_normalize
void hmm_normalize(hmm_t *h, int32 bestscr)
Renormalize the scores in this HMM based on the given best score.
Definition: hmm.c:209
HMM_MAX_NSTATE
#define HMM_MAX_NSTATE
Hard-coded limit on the number of emitting states.
Definition: hmm.h:160
hmm_vit_eval
int32 hmm_vit_eval(hmm_t *hmm)
Viterbi evaluation of given HMM.
Definition: hmm.c:789
hmm_context_init
hmm_context_t * hmm_context_init(int32 n_emit_state, uint8 **const *tp, int16 const *senscore, uint16 *const *sseq)
Create an HMM context.
Definition: hmm.c:56
hmm_enter
void hmm_enter(hmm_t *h, int32 score, int32 histid, int frame)
Enter an HMM with the given path score and history ID.
Definition: hmm.c:201
hmm_deinit
void hmm_deinit(hmm_t *hmm)
Free an HMM structure, releasing internal data (but not the HMM structure itself).
Definition: hmm.c:111
hmm_dump_vit_eval
int32 hmm_dump_vit_eval(hmm_t *hmm, FILE *fp)
Like hmm_vit_eval, but dump HMM state and relevant senscr to fp first, for debugging;.
Definition: hmm.c:810
hmm_clear_scores
void hmm_clear_scores(hmm_t *h)
Reset the scores of the HMM.
Definition: hmm.c:170
hmm_init
void hmm_init(hmm_context_t *ctx, hmm_t *hmm, int mpx, int ssid, int tmatid)
Populate a previously-allocated HMM structure, allocating internal data.
Definition: hmm.c:89
hmm_context_free
void hmm_context_free(hmm_context_t *ctx)
Free an HMM context.
Definition: hmm.c:80
frame_idx_t
int32 frame_idx_t
Type for frame index values.
Definition: hmm.h:64
hmm_dump
void hmm_dump(hmm_t *h, FILE *fp)
For debugging, dump the whole HMM out.
Definition: hmm.c:116
hmm_clear
void hmm_clear(hmm_t *h)
Reset the states of the HMM to the invalid condition.
Definition: hmm.c:183
hmm_context_s
Definition: hmm.h:146
hmm_context_s::senscore
int16 const * senscore
State emission scores senscore[senid] (negated scaled logs3 values).
Definition: hmm.h:149
hmm_context_s::udata
void * udata
Whatever you feel like, gosh.
Definition: hmm.h:154
hmm_context_s::n_emit_state
int32 n_emit_state
Number of emitting states in this set of HMMs.
Definition: hmm.h:147
hmm_context_s::mpx_ssid_alloc
listelem_alloc_t * mpx_ssid_alloc
Allocator for senone sequence ID arrays.
Definition: hmm.h:153
hmm_context_s::st_sen_scr
int32 * st_sen_scr
Temporary array of senone scores (for some topologies).
Definition: hmm.h:152
hmm_context_s::tp
uint8 **const * tp
State transition scores tp[id][from][to] (logs3 values).
Definition: hmm.h:148
hmm_context_s::sseq
uint16 *const * sseq
Senone sequence mapping.
Definition: hmm.h:151
hmm_context_t
Shared information between a set of HMMs.
hmm_s
Definition: hmm.h:170
hmm_s::tmatid
int16 tmatid
Transition matrix ID (see hmm_context_t).
Definition: hmm.h:179
hmm_s::mpx
uint8 mpx
Is this HMM multiplex? (hoisted for speed)
Definition: hmm.h:181
hmm_s::bestscore
int32 bestscore
Best [emitting] state score in current frame (for pruning).
Definition: hmm.h:178
hmm_s::score
int32 score[HMM_MAX_NSTATE]
State scores for emitting states.
Definition: hmm.h:172
hmm_s::n_emit_state
uint8 n_emit_state
Number of emitting states (hoisted for speed)
Definition: hmm.h:182
hmm_s::out_history
int32 out_history
History index for non-emitting exit state.
Definition: hmm.h:175
hmm_s::out_score
int32 out_score
Score for non-emitting exit state.
Definition: hmm.h:174
hmm_s::ctx
hmm_context_t * ctx
Shared context data for this HMM.
Definition: hmm.h:171
hmm_s::history
int32 history[HMM_MAX_NSTATE]
History indices for emitting states.
Definition: hmm.h:173
hmm_s::frame
frame_idx_t frame
Frame in which this HMM was last active; <0 if inactive.
Definition: hmm.h:180
hmm_s::senid
uint16 senid[HMM_MAX_NSTATE]
Senone IDs (non-MPX) or sequence IDs (MPX)
Definition: hmm.h:177
hmm_s::ssid
uint16 ssid
Senone sequence ID (for non-MPX)
Definition: hmm.h:176
hmm_t
An individual HMM among the HMM search space.