1 /**
2  * @file   paramtypes.c
3  *
4  * <JA>
5  * @brief  ��ħ�ѥ�᡼������ʸ����ɽ���ȥХ��ʥ�ɽ��������Ѵ�
6  *
7  * ���Υե�����δؿ��ϡ���ħ�ѥ�᡼������ʸ����ɽ����"MFCC_E_D_Z" �ʤɡ�
8  * �� HTK �� short ����ɽ����������Х��ʥ�����Ȥ�����Ѵ���Ԥʤ��ޤ���
9  * </JA>
10  * <EN>
11  * @brief  Convert between string and binary expression of parameter type
12  *
13  * The functions in this file converts the expression of parameter type,
14  * between string (ex. "MFCC_E_D_Z") and internal binary format used in HTK.
15  * </EN>
16  *
17  * @author Akinobu LEE
18  * @date   Tue Feb 15 00:06:26 2005
19  *
20  * $Revision: 1.2 $
21  *
22  */
23 /*
24  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
25  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
26  * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
27  * All rights reserved
28  */
29 
30 #include <sent/stddefs.h>
31 #include <sent/htk_defs.h>
32 #include <sent/htk_param.h>
33 
34 
35 /// Database that relates base type strings to binary code and description string.
36 static OptionStr pbase[] = {
37   {"WAVEFORM", F_WAVEFORM, "sampled waveform", FALSE},
38   {"DISCRETE", F_DISCRETE, "Discrete", FALSE},
39   {"LPC", F_LPC, "LPC", TRUE},
40   {"LPCEPSTRA", F_LPCEPSTRA, "LPC cepstral", TRUE},
41   {"MFCC", F_MFCC, "mel-frequency cepstral", TRUE},
42   {"FBANK", F_FBANK, "log mel-filter bank", TRUE},
43   {"MELSPEC", F_MELSPEC, "linear mel-filter bank", TRUE},
44   {"LPREFC", F_LPREFC, "LPC(reflection)", TRUE},
45   {"LPDELCEP", F_LPDELCEP, "LPC+Delta", TRUE},
46   {"USER", F_USER, "user defined sample kind", TRUE},
47   {NULL,0,NULL,FALSE}
48 };
49 /// Database that relates qualifier type strings to binary code and description string.
50 static OptionStr pqual[] = {
51   {"_E", F_ENERGY, "log energy coef.", TRUE},
52   {"_N", F_ENERGY_SUP, "uppress absolute energy", TRUE},
53   {"_D", F_DELTA, "delta coef.", TRUE},
54   {"_A", F_ACCL, "acceleration coef.", TRUE},
55   {"_C", F_COMPRESS, "compressed", TRUE},
56   {"_Z", F_CEPNORM, "cepstral mean normalization", TRUE},
57   {"_K", F_CHECKSUM, "CRC checksum added", TRUE},
58   {"_0", F_ZEROTH, "0'th cepstral parameter", TRUE},
59   {NULL,0,NULL,FALSE}
60 };
61 
62 /**
63  * Convert a qualifier string to a binary type code.
64  *
65  * @param s [in] a string that contains qualifier strings like "_E_D_Z"
66  *
67  * @return the converted internal binary type code, F_ERR_INVALID if failed.
68  */
69 short
param_qualstr2code(char * s)70 param_qualstr2code(char *s)
71 {
72   int i, qlen;
73   char *p;
74   short qual_type;
75 
76   qual_type = 0;
77   p = s;
78 
79   /* parse qualifiers */
80   while (*p == '_') {
81     for (i=0;pqual[i].name!=NULL;i++) {
82       qlen = strlen(pqual[i].name);
83       if (strncasecmp(p, pqual[i].name, qlen) == 0) {
84 	qual_type |= pqual[i].type;
85 	break;
86       }
87     }
88     if (pqual[i].name == NULL) {	/* qualifier not found */
89       jlog("Error: paramtypes: unknown parameter qualifier: %2s\n", p);
90       return(F_ERR_INVALID);
91     }
92     p += 2;
93   }
94 
95   return(qual_type);
96 }
97 
98 /**
99  * Convert a type string that contains basename and qualifiers to a binary type code.
100  *
101  * @param s [in] a string that contains base and qualifier string like "MFCC_E_D_Z"
102  *
103  * @return the converted internal binary type code, F_ERR_INVALID if failed.
104  */
105 short
param_str2code(char * s)106 param_str2code(char *s)
107 {
108   int i;
109   short param_type, qual_type;
110   char *p, *buf;
111 
112   /* determine base type */
113   /* cutout base part to *buf */
114   buf = strcpy((char *)mymalloc(strlen(s)+1), s);
115   p = strchr(buf, '_');
116   if (p != NULL) *p = '\0';
117 
118   for (i=0;pbase[i].name!=NULL;i++) {
119     if (strcasecmp(buf, pbase[i].name) == 0) {
120       param_type = pbase[i].type;
121       /* qualifiers */
122       qual_type = param_qualstr2code(s + strlen(buf));
123       if (qual_type == F_ERR_INVALID) {
124 	free(buf);
125 	return(F_ERR_INVALID);
126       } else {
127 	param_type |= qual_type;
128 	free(buf);
129 	return(param_type);
130       }
131     }
132   }
133   /* base type not found */
134   free(buf);
135   return(F_ERR_INVALID);
136 }
137 
138 /**
139  * Convert the qualifier part of a binary type code to string.
140  *
141  * @param buf [out] buffer to store the resulting string (must have enough length)
142  * @param type [in] binary type code to convert.
143  * @param descflag [in] set to TRUE if you want result in description string
144  * instead of qualifier string.
145  *
146  * @return @a buf on success, NULL on failure.
147  */
148 char *
param_qualcode2str(char * buf,short type,boolean descflag)149 param_qualcode2str(char *buf, short type, boolean descflag)
150 {
151   int i;
152 
153   /* qualifier */
154   for (i=0;pqual[i].name!=NULL;i++) {
155     if (type & pqual[i].type) {
156       if (descflag) {
157 	sprintf(buf, " %s %s\n", pqual[i].desc,
158 		(pqual[i].supported ? "" : "(not supported)"));
159       } else {
160 	strcat(buf, pqual[i].name);
161       }
162     }
163   }
164   return(buf);
165 }
166 
167 /**
168  * Convert a binary type code to string.
169  *
170  * @param buf [out] buffer to store the resulting string (must have enough length)
171  * @param type [in] binary type code to convert.
172  * @param descflag [in] set to TRUE if you want result in description string
173  * instead of base and qualifier string.
174  *
175  * @return @a buf on success, NULL on failure.
176  */
177 char *
param_code2str(char * buf,short type,boolean descflag)178 param_code2str(char *buf, short type, boolean descflag)
179 {
180   int i;
181   short btype;
182 
183   /* basetype */
184   btype = type & F_BASEMASK;
185   for (i = 0; pbase[i].name != NULL; i++) {
186     if (pbase[i].type == btype) {
187       if (descflag) {
188 	sprintf(buf, "%s %s with:\n", pbase[i].desc,
189 		(pbase[i].supported ? "" : "(not supported)"));
190       } else {
191 	strcpy(buf, pbase[i].name);
192       }
193       break;
194     }
195   }
196   if (pbase[i].name  == NULL) {	/* not found */
197     sprintf(buf, "ERROR: unknown basetype ID: %d\n", btype);
198     return(buf);
199   }
200 
201   /* add qualifier string to buf */
202   param_qualcode2str(buf, type, descflag);
203 
204   return(buf);
205 }
206