1 /**
2 * @file rdhmmdef_var.c
3 *
4 * <JA>
5 * @brief HTK %HMM ����ե�������ɤ߹��ߡ�������ʬ�ۤ�ʬ���٥��ȥ�
6 * </JA>
7 *
8 * <EN>
9 * @brief Read HTK %HMM definition file: variance vector of Gaussian
10 * </EN>
11 *
12 * @author Akinobu LEE
13 * @date Wed Feb 16 04:01:38 2005
14 *
15 * $Revision: 1.3 $
16 *
17 */
18 /*
19 * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
20 * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
21 * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
22 * All rights reserved
23 */
24
25 /* currenty cannot treat other sub macros (~u,~i,~x) */
26
27 #include <sent/stddefs.h>
28 #include <sent/htk_hmm.h>
29
30 extern char *rdhmmdef_token; ///< Current token
31
32 /**
33 * Allocate a new data area and return it.
34 *
35 * @return pointer to newly allocated data.
36 */
37 static HTK_HMM_Var *
var_new(HTK_HMM_INFO * hmm)38 var_new(HTK_HMM_INFO *hmm)
39 {
40 HTK_HMM_Var *new;
41
42 new = (HTK_HMM_Var *)mybmalloc2(sizeof(HTK_HMM_Var), &(hmm->mroot));
43
44 new->name = NULL;
45 new->vec = NULL;
46 new->len = 0;
47 new->next = NULL;
48
49 return(new);
50 }
51
52 /**
53 * Add a new data to the global structure.
54 *
55 * @param hmm [i/o] %HMM definition data to store it
56 * @param new [in] new data to be added
57 */
58 void
var_add(HTK_HMM_INFO * hmm,HTK_HMM_Var * new)59 var_add(HTK_HMM_INFO *hmm, HTK_HMM_Var *new)
60 {
61 HTK_HMM_Var *match;
62
63 /* link data structure */
64 new->next = hmm->vrstart;
65 hmm->vrstart = new;
66
67 if (new->name != NULL) {
68 /* add index to search index tree */
69 if (hmm->vr_root == NULL) {
70 hmm->vr_root = aptree_make_root_node(new, &(hmm->mroot));
71 } else {
72 match = aptree_search_data(new->name, hmm->vr_root);
73 if (match != NULL && strmatch(match->name, new->name)) {
74 jlog("Error: rdhmmdef_var: ~v \"%s\" is already defined\n", new->name);
75 rderr(NULL);
76 } else {
77 aptree_add_entry(new->name, new, match->name, &(hmm->vr_root), &(hmm->mroot));
78 }
79 }
80 }
81
82 }
83
84 /**
85 * Look up a data macro by the name.
86 *
87 * @param hmm [in] %HMM definition data
88 * @param keyname [in] macro name to find
89 *
90 * @return pointer to the found data, or NULL if not found.
91 */
92 static HTK_HMM_Var *
var_lookup(HTK_HMM_INFO * hmm,char * keyname)93 var_lookup(HTK_HMM_INFO *hmm, char *keyname)
94 {
95 HTK_HMM_Var *v;
96
97 v = aptree_search_data(keyname, hmm->vr_root);
98 if (v != NULL && strmatch(v->name, keyname)) {
99 return v;
100 } else {
101 return NULL;
102 }
103 }
104
105 /**
106 * @brief Read one new data and returns the pointer
107 *
108 * If a sub-component of this data is directly defined at here, they
109 * will be read from here and assigned to this data. If a sub-component
110 * is not defined here but a macro name referencing to the component previously
111 * defined in other place, the data will be searched by the macro name and
112 * the pointer to the found component will be assigned to this model.
113 *
114 * @param fp [in] file pointer
115 * @param hmm [i/o] %HMM definition data to store it
116 *
117 * @return pointer to the newly read data.
118 */
119 static HTK_HMM_Var *
var_read(FILE * fp,HTK_HMM_INFO * hmm)120 var_read(FILE *fp, HTK_HMM_INFO *hmm)
121 {
122 HTK_HMM_Var *new;
123 int i;
124
125 new = var_new(hmm);
126
127 /* read covariance matrix (diagonal vector) */
128
129 if (!currentis("VARIANCE")) {
130 jlog("Error: rdhmmdef_var: variance matrix type \"%s\" not supported\n", rdhmmdef_token);
131 rderr(NULL);
132 } else {
133 read_token(fp);
134 NoTokErr("missing VARIANCE vector length");
135 new->len = atoi(rdhmmdef_token);
136 read_token(fp);
137 new->vec = (VECT *)mybmalloc2(sizeof(VECT) * new->len, &(hmm->mroot));
138 /* needs comversion if integerized */
139 for (i=0;i<new->len;i++) {
140 NoTokErr("missing some VARIANCE element");
141 new->vec[i] = (VECT)atof(rdhmmdef_token);
142 read_token(fp);
143 }
144 }
145
146 return (new);
147 }
148
149 /**
150 * @brief Return a pointer to the data located at the current point.
151 *
152 * If the current point is a macro reference, the pointer to the
153 * already defined data will be searched and returned.
154 * Otherwise, the definition of the data will be read from the current
155 * point and pointer to the newly allocated data will be returned.
156 *
157 * @param fp [in] file pointer
158 * @param hmm [i/o] %HMM definition data
159 *
160 * @return pointer to the data located at the current point.
161 */
162 HTK_HMM_Var *
get_var_data(FILE * fp,HTK_HMM_INFO * hmm)163 get_var_data(FILE *fp, HTK_HMM_INFO *hmm)
164 {
165 HTK_HMM_Var *tmp;
166
167 if (currentis("~v")) {
168 /* macro reference: lookup and return the pointer */
169 read_token(fp);
170 NoTokErr("missing VARIANCE macro name");
171 tmp = var_lookup(hmm, rdhmmdef_token);
172 if (tmp == NULL) {
173 jlog("Error: rdhmmdef_var: ~v \"%s\" not defined\n", rdhmmdef_token);
174 rderr(NULL);
175 }
176 read_token(fp);
177 return tmp;
178 } else if (currentis("VARIANCE")){
179 /* definition: define variance data, and return the pointer */
180 tmp = var_read(fp, hmm);
181 tmp->name = NULL; /* no name */
182 var_add(hmm, tmp);
183 return tmp;
184 } else {
185 rderr("no variance data");
186 return NULL;
187 }
188 }
189
190 /**
191 * Read a new data and store it as a macro.
192 *
193 * @param name [in] macro name
194 * @param fp [in] file pointer
195 * @param hmm [i/o] %HMM definition data
196 */
197 void
def_var_macro(char * name,FILE * fp,HTK_HMM_INFO * hmm)198 def_var_macro(char *name, FILE *fp, HTK_HMM_INFO *hmm)
199 {
200 HTK_HMM_Var *new;
201
202
203 /* read in data and return newly malloced data */
204 new = var_read(fp, hmm);
205
206 /* register it to the grobal HMM structure */
207 new->name = name;
208 var_add(hmm, new);
209 }
210