PocketSphinx  0.6
src/libpocketsphinx/bin_mdef.h
Go to the documentation of this file.
00001 /* -*- c-file-style: "linux" -*- */
00002 /* ====================================================================
00003  * Copyright (c) 2005 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  */
00045 #ifndef __BIN_MDEF_H__
00046 #define __BIN_MDEF_H__
00047 
00048 #ifdef __cplusplus
00049 extern "C" {
00050 #if 0
00051 }; /* Fool Emacs */
00052 #endif
00053 #endif /* __cplusplus */
00054 
00055 /* SphinxBase headers. */
00056 #include <sphinxbase/mmio.h>
00057 #include <sphinxbase/cmd_ln.h>
00058 
00059 #include "mdef.h"
00060 
00061 #define BIN_MDEF_FORMAT_VERSION 1
00062 /* Little-endian machines will write "BMDF" to disk, big-endian ones "FDMB". */
00063 #define BIN_MDEF_NATIVE_ENDIAN 0x46444d42 /* 'BMDF' in little-endian order */
00064 #define BIN_MDEF_OTHER_ENDIAN 0x424d4446  /* 'BMDF' in big-endian order */
00065 #ifdef __GNUC__
00066 #define ATTRIBUTE_PACKED __attribute__((packed))
00067 #else
00068 #define ATTRIBUTE_PACKED
00069 #endif
00070 
00074 typedef struct mdef_entry_s mdef_entry_t;
00075 struct mdef_entry_s {
00076         int32 ssid; 
00077         int32 tmat; 
00078         /* FIXME: is any of this actually necessary? */
00079         union {
00081                 struct {
00082                         int8 filler;
00083                         int8 reserved[3];
00084                 } ci;
00086                 struct {
00087                         int8 wpos;
00088                         int8 ctx[3]; 
00089                 } cd;
00090         } info;
00091 } ATTRIBUTE_PACKED;
00092 
00096 #define BAD_SSID 0xffff
00097 
00100 #define BAD_SENID 0xffff
00101 
00105 typedef struct cd_tree_s cd_tree_t;
00106 struct cd_tree_s {
00107         int16 ctx; 
00108         int16 n_down; 
00109         union {
00110                 int32 pid; 
00111                 int32 down; 
00112         } c;
00113 };
00114 
00118 typedef struct bin_mdef_s bin_mdef_t;
00119 struct bin_mdef_s {
00120         int refcnt;
00121         int32 n_ciphone;    
00122         int32 n_phone;      
00123         int32 n_emit_state; 
00124         int32 n_ci_sen;     
00125         int32 n_sen;        
00126         int32 n_tmat;       
00127         int32 n_sseq;       
00128         int32 n_ctx;        
00129         int32 n_cd_tree;    
00130         int16 sil;          
00132         mmio_file_t *filemap;
00133         char **ciname;       
00134         cd_tree_t *cd_tree;  
00135         mdef_entry_t *phone; 
00136         uint16 **sseq;       
00137         uint8 *sseq_len;     
00139         /* These two are not stored on disk, but are generated at load time. */
00140         int16 *cd2cisen;        
00141         int16 *sen2cimap;       
00144         enum { BIN_MDEF_FROM_TEXT, BIN_MDEF_IN_MEMORY, BIN_MDEF_ON_DISK } alloc_mode;
00145 };
00146 
00147 #define bin_mdef_is_fillerphone(m,p)    (((p) < (m)->n_ciphone) \
00148                                          ? (m)->phone[p].info.ci.filler \
00149                                          : (m)->phone[(m)->phone[p].info.cd.ctx[0]].info.ci.filler)
00150 #define bin_mdef_is_ciphone(m,p)        ((p) < (m)->n_ciphone)
00151 #define bin_mdef_n_ciphone(m)           ((m)->n_ciphone)
00152 #define bin_mdef_n_phone(m)             ((m)->n_phone)
00153 #define bin_mdef_n_sseq(m)              ((m)->n_sseq)
00154 #define bin_mdef_n_emit_state(m)        ((m)->n_emit_state)
00155 #define bin_mdef_n_emit_state_phone(m,p) ((m)->n_emit_state ? (m)->n_emit_state \
00156                                           : (m)->sseq_len[(m)->phone[p].ssid])
00157 #define bin_mdef_n_sen(m)               ((m)->n_sen)
00158 #define bin_mdef_n_tmat(m)              ((m)->n_tmat)
00159 #define bin_mdef_pid2ssid(m,p)          ((m)->phone[p].ssid)
00160 #define bin_mdef_pid2tmatid(m,p)        ((m)->phone[p].tmat)
00161 #define bin_mdef_silphone(m)            ((m)->sil)
00162 #define bin_mdef_sen2cimap(m,s)         ((m)->sen2cimap[s])
00163 #define bin_mdef_sseq2sen(m,ss,pos)     ((m)->sseq[ss][pos])
00164 #define bin_mdef_pid2ci(m,p)            (((p) < (m)->n_ciphone) ? (p) \
00165                                          : (m)->phone[p].info.cd.ctx[0])
00166 
00170 bin_mdef_t *bin_mdef_read(cmd_ln_t *config, const char *filename);
00174 bin_mdef_t *bin_mdef_read_text(cmd_ln_t *config, const char *filename);
00178 int bin_mdef_write(bin_mdef_t *m, const char *filename);
00182 int bin_mdef_write_text(bin_mdef_t *m, const char *filename);
00186 bin_mdef_t *bin_mdef_retain(bin_mdef_t *m);
00190 int bin_mdef_free(bin_mdef_t *m);
00191 
00196 int bin_mdef_ciphone_id(bin_mdef_t *m,         
00197                         const char *ciphone);  
00203 int bin_mdef_ciphone_id_nocase(bin_mdef_t *m,        
00204                                const char *ciphone); 
00206 /* Return value: READ-ONLY ciphone string name for the given ciphone id */
00207 const char *bin_mdef_ciphone_str(bin_mdef_t *m, 
00208                                  int32 ci);     
00210 /* Return value: phone id for the given constituents if found, else -1 */
00211 int bin_mdef_phone_id(bin_mdef_t *m,    
00212                       int32 b,          
00213                       int32 l,          
00214                       int32 r,          
00215                       int32 pos);       
00217 /* Look up a phone id, backing off to other word positions. */
00218 int bin_mdef_phone_id_nearest(bin_mdef_t * m, int32 b,
00219                               int32 l, int32 r, int32 pos);
00220 
00226 int bin_mdef_phone_str(bin_mdef_t *m,   
00227                        int pid,         
00228                        char *buf);      
00230 #ifdef __cplusplus
00231 }; /* extern "C" */
00232 #endif /* __cplusplus */
00233 
00234 #endif /* __BIN_MDEF_H__ */