1 /**
2 * @file rdhmmdef_data.c
3 *
4 * <JA>
5 * @brief HTK %HMM ����ե�������ɤ߹��ߡ�%HMM ��ǥ�
6 * </JA>
7 *
8 * <EN>
9 * @brief Read HTK %HMM definition file: %HMM model
10 * </EN>
11 *
12 * @author Akinobu LEE
13 * @date Wed Feb 16 01:12:19 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 #include <sent/stddefs.h>
26 #include <sent/htk_hmm.h>
27
28 extern char *rdhmmdef_token; ///< Current token
29
30 /**
31 * Allocate a new data and return it.
32 *
33 * @return pointer to newly allocated data.
34 */
35 HTK_HMM_Data *
htk_hmmdata_new(HTK_HMM_INFO * hmminfo)36 htk_hmmdata_new(HTK_HMM_INFO *hmminfo)
37 {
38 HTK_HMM_Data *new;
39
40 new = (HTK_HMM_Data *)mybmalloc2(sizeof(HTK_HMM_Data), &(hmminfo->mroot));
41
42 new->name = NULL;
43 new->state_num = 0;
44 new->s = NULL;
45 new->tr = NULL;
46 new->next = NULL;
47
48 return(new);
49 }
50
51 /**
52 * Add a new data to the global structure.
53 *
54 * @param hmm [i/o] %HMM definition data to store it
55 * @param new [in] new data to be added
56 */
57 void
htk_hmmdata_add(HTK_HMM_INFO * hmm,HTK_HMM_Data * new)58 htk_hmmdata_add(HTK_HMM_INFO *hmm, HTK_HMM_Data *new)
59 {
60 HTK_HMM_Data *match;
61 /* link data structure */
62 new->next = hmm->start;
63 hmm->start = new;
64
65 if (new->name == NULL) {
66 /* HMM must have a name */
67 rderr("HMM has no name");
68 } else {
69 /* add index to search index tree */
70 if (hmm->physical_root == NULL) {
71 hmm->physical_root = aptree_make_root_node(new, &(hmm->mroot));
72 } else {
73 match = aptree_search_data(new->name, hmm->physical_root);
74 if (match != NULL && strmatch(match->name, new->name)) {
75 /* HMM of the same name should not be defined */
76 jlog("Error: rdhmmdef_data: HMM \"%s\" is defined more than twice\n", new->name);
77 rderr(NULL);
78 } else {
79 aptree_add_entry(new->name, new, match->name, &(hmm->physical_root), &(hmm->mroot));
80 }
81 }
82 }
83 }
84
85 /**
86 * @brief Read one new data and returns the pointer
87 *
88 * If a sub-component of this data is directly defined at here, they
89 * will be read from here and assigned to this data. If a sub-component
90 * is not defined here but a macro name referencing to the component previously
91 * defined in other place, the data will be searched by the macro name and
92 * the pointer to the found component will be assigned to this model.
93 *
94 * @param fp [in] file pointer
95 * @param hmm [in] %HMM definition data
96 *
97 * @return pointer to the newly read data.
98 */
99 static HTK_HMM_Data *
htk_hmmdata_read(FILE * fp,HTK_HMM_INFO * hmm)100 htk_hmmdata_read(FILE *fp, HTK_HMM_INFO *hmm)
101 {
102 HTK_HMM_Data *new;
103 int i;
104 short sid;
105
106 new = htk_hmmdata_new(hmm);
107
108 /* begin tag */
109 if (!currentis("BEGINHMM")) rderr("<BEGINHMM> not found");
110 read_token(fp);
111
112 /* read global opt if any */
113 /* read_global_opt(fp, &(new->opt)); */
114
115 /* num of state */
116 if (!currentis("NUMSTATES")) rderr("<NUMSTATES> not found");
117 read_token(fp);
118 NoTokErr("state num not found\n");
119 new->state_num = atoi(rdhmmdef_token);
120 read_token(fp);
121
122 /* malloc state */
123 new->s = (HTK_HMM_State **)mybmalloc2(sizeof(HTK_HMM_State *) * new->state_num, &(hmm->mroot));
124 for(i=0;i<new->state_num;i++) {
125 new->s[i] = NULL;
126 }
127
128 /* read/set each state info */
129 for (;;) {
130 if (!currentis("STATE")) break;
131 read_token(fp); NoTokErr("STATE id not found");
132 sid = atoi(rdhmmdef_token) - 1;
133 read_token(fp);
134 new->s[sid] = get_state_data(fp, hmm);
135 }
136
137 /* read/set transition info */
138 new->tr = get_trans_data(fp, hmm);
139 if ((new->tr)->statenum != new->state_num) {
140 rderr("# of transition != # of state");
141 }
142
143 /* read/set duration */
144
145 /* end tag */
146 if (!currentis("ENDHMM")) rderr("<ENDHMM> not found");
147 read_token(fp);
148
149 return(new);
150 }
151
152 /**
153 * Read a new data and store it as a macro.
154 *
155 * @param name [in] macro name
156 * @param fp [in] file pointer
157 * @param hmm [i/o] %HMM definition data
158 */
159 void
def_HMM(char * name,FILE * fp,HTK_HMM_INFO * hmm)160 def_HMM(char *name, FILE *fp, HTK_HMM_INFO *hmm)
161 {
162 HTK_HMM_Data *new;
163
164 /* read in HMM model data from fp, and return newly malloced HTK_HMM_Data */
165 new = htk_hmmdata_read(fp, hmm);
166
167 /* set name and add the new data to the main structure */
168 new->name = name;
169 htk_hmmdata_add(hmm, new);
170 }
171