PocketSphinx  0.6
bin_mdef.h
Go to the documentation of this file.
1 /* -*- c-file-style: "linux" -*- */
2 /* ====================================================================
3  * Copyright (c) 2005 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
45 #ifndef __BIN_MDEF_H__
46 #define __BIN_MDEF_H__
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #if 0
51 }; /* Fool Emacs */
52 #endif
53 #endif /* __cplusplus */
54 
55 /* SphinxBase headers. */
56 #include <sphinxbase/mmio.h>
57 #include <sphinxbase/cmd_ln.h>
58 #include <pocketsphinx_export.h>
59 
60 #include "mdef.h"
61 
62 #define BIN_MDEF_FORMAT_VERSION 1
63 /* Little-endian machines will write "BMDF" to disk, big-endian ones "FDMB". */
64 #define BIN_MDEF_NATIVE_ENDIAN 0x46444d42 /* 'BMDF' in little-endian order */
65 #define BIN_MDEF_OTHER_ENDIAN 0x424d4446 /* 'BMDF' in big-endian order */
66 #ifdef __GNUC__
67 #define ATTRIBUTE_PACKED __attribute__((packed))
68 #else
69 #define ATTRIBUTE_PACKED
70 #endif
71 
75 typedef struct mdef_entry_s mdef_entry_t;
76 struct mdef_entry_s {
77  int32 ssid;
78  int32 tmat;
79  /* FIXME: is any of this actually necessary? */
80  union {
82  struct {
83  uint8 filler;
84  uint8 reserved[3];
85  } ci;
87  struct {
88  uint8 wpos;
89  uint8 ctx[3];
90  } cd;
91  } info;
92 } ATTRIBUTE_PACKED;
93 
97 #define BAD_SSID 0xffff
98 
101 #define BAD_SENID 0xffff
102 
106 typedef struct cd_tree_s cd_tree_t;
107 struct cd_tree_s {
108  int16 ctx;
109  int16 n_down;
110  union {
111  int32 pid;
112  int32 down;
113  } c;
114 };
115 
119 typedef struct bin_mdef_s bin_mdef_t;
120 struct bin_mdef_s {
121  int refcnt;
122  int32 n_ciphone;
123  int32 n_phone;
124  int32 n_emit_state;
125  int32 n_ci_sen;
126  int32 n_sen;
127  int32 n_tmat;
128  int32 n_sseq;
129  int32 n_ctx;
130  int32 n_cd_tree;
131  int16 sil;
133  mmio_file_t *filemap;
134  char **ciname;
137  uint16 **sseq;
138  uint8 *sseq_len;
140  /* These two are not stored on disk, but are generated at load time. */
141  int16 *cd2cisen;
142  int16 *sen2cimap;
145  enum { BIN_MDEF_FROM_TEXT, BIN_MDEF_IN_MEMORY, BIN_MDEF_ON_DISK } alloc_mode;
146 };
147 
148 #define bin_mdef_is_fillerphone(m,p) (((p) < (m)->n_ciphone) \
149  ? (m)->phone[p].info.ci.filler \
150  : (m)->phone[(m)->phone[p].info.cd.ctx[0]].info.ci.filler)
151 #define bin_mdef_is_ciphone(m,p) ((p) < (m)->n_ciphone)
152 #define bin_mdef_n_ciphone(m) ((m)->n_ciphone)
153 #define bin_mdef_n_phone(m) ((m)->n_phone)
154 #define bin_mdef_n_sseq(m) ((m)->n_sseq)
155 #define bin_mdef_n_emit_state(m) ((m)->n_emit_state)
156 #define bin_mdef_n_emit_state_phone(m,p) ((m)->n_emit_state ? (m)->n_emit_state \
157  : (m)->sseq_len[(m)->phone[p].ssid])
158 #define bin_mdef_n_sen(m) ((m)->n_sen)
159 #define bin_mdef_n_tmat(m) ((m)->n_tmat)
160 #define bin_mdef_pid2ssid(m,p) ((m)->phone[p].ssid)
161 #define bin_mdef_pid2tmatid(m,p) ((m)->phone[p].tmat)
162 #define bin_mdef_silphone(m) ((m)->sil)
163 #define bin_mdef_sen2cimap(m,s) ((m)->sen2cimap[s])
164 #define bin_mdef_sseq2sen(m,ss,pos) ((m)->sseq[ss][pos])
165 #define bin_mdef_pid2ci(m,p) (((p) < (m)->n_ciphone) ? (p) \
166  : (m)->phone[p].info.cd.ctx[0])
167 
171 POCKETSPHINX_EXPORT
172 bin_mdef_t *bin_mdef_read(cmd_ln_t *config, const char *filename);
176 POCKETSPHINX_EXPORT
177 bin_mdef_t *bin_mdef_read_text(cmd_ln_t *config, const char *filename);
181 POCKETSPHINX_EXPORT
182 int bin_mdef_write(bin_mdef_t *m, const char *filename);
186 POCKETSPHINX_EXPORT
187 int bin_mdef_write_text(bin_mdef_t *m, const char *filename);
191 bin_mdef_t *bin_mdef_retain(bin_mdef_t *m);
195 int bin_mdef_free(bin_mdef_t *m);
196 
201 int bin_mdef_ciphone_id(bin_mdef_t *m,
202  const char *ciphone);
208 int bin_mdef_ciphone_id_nocase(bin_mdef_t *m,
209  const char *ciphone);
211 /* Return value: READ-ONLY ciphone string name for the given ciphone id */
212 const char *bin_mdef_ciphone_str(bin_mdef_t *m,
213  int32 ci);
215 /* Return value: phone id for the given constituents if found, else -1 */
216 int bin_mdef_phone_id(bin_mdef_t *m,
217  int32 b,
218  int32 l,
219  int32 r,
220  int32 pos);
222 /* Look up a phone id, backing off to other word positions. */
223 int bin_mdef_phone_id_nearest(bin_mdef_t * m, int32 b,
224  int32 l, int32 r, int32 pos);
225 
231 int bin_mdef_phone_str(bin_mdef_t *m,
232  int pid,
233  char *buf);
235 #ifdef __cplusplus
236 }; /* extern "C" */
237 #endif /* __cplusplus */
238 
239 #endif /* __BIN_MDEF_H__ */