| 1 | /*************************************************************************/ |
| 2 | /* */ |
| 3 | /* Language Technologies Institute */ |
| 4 | /* Carnegie Mellon University */ |
| 5 | /* Copyright (c) 1999 */ |
| 6 | /* All Rights Reserved. */ |
| 7 | /* */ |
| 8 | /* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 | /* this software and its documentation without restriction, including */ |
| 10 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 | /* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 | /* permit persons to whom this work is furnished to do so, subject to */ |
| 13 | /* the following conditions: */ |
| 14 | /* 1. The code must retain the above copyright notice, this list of */ |
| 15 | /* conditions and the following disclaimer. */ |
| 16 | /* 2. Any modifications must be clearly marked as such. */ |
| 17 | /* 3. Original authors' names are not deleted. */ |
| 18 | /* 4. The authors' names are not used to endorse or promote products */ |
| 19 | /* derived from this software without specific prior written */ |
| 20 | /* permission. */ |
| 21 | /* */ |
| 22 | /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 | /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 | /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 | /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 | /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 | /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 | /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 | /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 | /* THIS SOFTWARE. */ |
| 31 | /* */ |
| 32 | /*************************************************************************/ |
| 33 | /* Author: Alan W Black ([email protected]) */ |
| 34 | /* Date: December 1999 */ |
| 35 | /*************************************************************************/ |
| 36 | /* */ |
| 37 | /* Letter to sound rule support */ |
| 38 | /* */ |
| 39 | /*************************************************************************/ |
| 40 | |
| 41 | /* ----------------------------------------------------------------- */ |
| 42 | /* The English TTS System "Flite+hts_engine" */ |
| 43 | /* developed by HTS Working Group */ |
| 44 | /* http://hts-engine.sourceforge.net/ */ |
| 45 | /* ----------------------------------------------------------------- */ |
| 46 | /* */ |
| 47 | /* Copyright (c) 2005-2013 Nagoya Institute of Technology */ |
| 48 | /* Department of Computer Science */ |
| 49 | /* */ |
| 50 | /* 2005-2008 Tokyo Institute of Technology */ |
| 51 | /* Interdisciplinary Graduate School of */ |
| 52 | /* Science and Engineering */ |
| 53 | /* */ |
| 54 | /* All rights reserved. */ |
| 55 | /* */ |
| 56 | /* Redistribution and use in source and binary forms, with or */ |
| 57 | /* without modification, are permitted provided that the following */ |
| 58 | /* conditions are met: */ |
| 59 | /* */ |
| 60 | /* - Redistributions of source code must retain the above copyright */ |
| 61 | /* notice, this list of conditions and the following disclaimer. */ |
| 62 | /* - Redistributions in binary form must reproduce the above */ |
| 63 | /* copyright notice, this list of conditions and the following */ |
| 64 | /* disclaimer in the documentation and/or other materials provided */ |
| 65 | /* with the distribution. */ |
| 66 | /* - Neither the name of the HTS working group nor the names of its */ |
| 67 | /* contributors may be used to endorse or promote products derived */ |
| 68 | /* from this software without specific prior written permission. */ |
| 69 | /* */ |
| 70 | /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */ |
| 71 | /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */ |
| 72 | /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ |
| 73 | /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ |
| 74 | /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */ |
| 75 | /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */ |
| 76 | /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */ |
| 77 | /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */ |
| 78 | /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ |
| 79 | /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */ |
| 80 | /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */ |
| 81 | /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */ |
| 82 | /* POSSIBILITY OF SUCH DAMAGE. */ |
| 83 | /* ----------------------------------------------------------------- */ |
| 84 | |
| 85 | #include "cst_features.h" |
| 86 | #include "cst_lts.h" |
| 87 | #include "cst_endian.h" |
| 88 | |
| 89 | static cst_lts_phone apply_model(cst_lts_letter *vals, |
| 90 | cst_lts_addr start, |
| 91 | const cst_lts_model *model); |
| 92 | |
| 93 | #ifndef FLITE_PLUS_HTS_ENGINE |
| 94 | cst_lts_rules *new_lts_rules() |
| 95 | { |
| 96 | cst_lts_rules *lt = cst_alloc(cst_lts_rules,1); |
| 97 | lt->name = 0; |
| 98 | lt->letter_index = 0; |
| 99 | lt->models = 0; |
| 100 | lt->phone_table = 0; |
| 101 | lt->context_window_size = 0; |
| 102 | lt->context_extra_feats = 0; |
| 103 | lt->letter_table = 0; |
| 104 | return lt; |
| 105 | } |
| 106 | |
| 107 | cst_val *lts_apply_val(const cst_val *wlist,const char *feats,const cst_lts_rules *r) |
| 108 | { |
| 109 | /* for symbol to symbol mapping */ |
| 110 | const cst_val *v; |
| 111 | cst_val *p; |
| 112 | char *word; |
| 113 | int i,j; |
| 114 | |
| 115 | word = cst_alloc(char,val_length(wlist)+1); |
| 116 | |
| 117 | for (v=wlist,i=0; v; v=val_cdr(v),i++) |
| 118 | { |
| 119 | for (j=0; r->letter_table[j]; j++) |
| 120 | if (cst_streq(val_string(val_car(v)),r->letter_table[j])) |
| 121 | { |
| 122 | word[i] = j; |
| 123 | break; |
| 124 | } |
| 125 | if (!r->letter_table[j]) |
| 126 | { |
| 127 | #if 0 |
| 128 | printf("awb_debug unknown letter >%s<\n",val_string(val_car(v))); |
| 129 | #endif |
| 130 | i--; /* can't find this letter so skip it */ |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | p = lts_apply(word,feats,r); |
| 135 | cst_free(word); |
| 136 | |
| 137 | return p; |
| 138 | } |
| 139 | #endif /* !FLITE_PLUS_HTS_ENGINE */ |
| 140 | |
| 141 | cst_val *lts_apply(const char *word,const char *feats,const cst_lts_rules *r) |
| 142 | { |
| 143 | int pos, index, i; |
| 144 | cst_val *phones=0; |
| 145 | cst_lts_letter *fval_buff; |
| 146 | cst_lts_letter *full_buff; |
| 147 | cst_lts_phone phone; |
| 148 | char *left, *right, *p; |
| 149 | char hash; |
| 150 | char zeros[8]; |
| 151 | |
| 152 | /* For feature vals for each letter */ |
| 153 | fval_buff = cst_alloc(cst_lts_letter, |
| 154 | (r->context_window_size*2)+ |
| 155 | r->context_extra_feats); |
| 156 | /* Buffer with added contexts */ |
| 157 | full_buff = cst_alloc(cst_lts_letter, |
| 158 | (r->context_window_size*2)+ |
| 159 | cst_strlen(word)+1); /* TBD assumes single POS feat */ |
| 160 | if (r->letter_table) |
| 161 | { |
| 162 | for (i=0; i<8; i++) zeros[i] = 2; |
| 163 | cst_sprintf((char *)full_buff, |
| 164 | "%.*s%c%s%c%.*s", |
| 165 | r->context_window_size-1, zeros, |
| 166 | 1, |
| 167 | word, |
| 168 | 1, |
| 169 | r->context_window_size-1, zeros); |
| 170 | hash = 1; |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | /* Assumes l_letter is a char and context < 8 */ |
| 175 | cst_sprintf((char *)full_buff, |
| 176 | "%.*s#%s#%.*s", |
| 177 | r->context_window_size-1, "00000000", |
| 178 | word, |
| 179 | r->context_window_size-1, "00000000"); |
| 180 | hash = '#'; |
| 181 | } |
| 182 | |
| 183 | /* Do the prediction backwards so we don't need to reverse the answer */ |
| 184 | for (pos = r->context_window_size + cst_strlen(word) - 1; |
| 185 | full_buff[pos] != hash; |
| 186 | pos--) |
| 187 | { |
| 188 | /* Fill the features buffer for the predictor */ |
| 189 | cst_sprintf((char *)fval_buff, |
| 190 | "%.*s%.*s%s", |
| 191 | r->context_window_size, |
| 192 | full_buff+pos-r->context_window_size, |
| 193 | r->context_window_size, |
| 194 | full_buff+pos+1, |
| 195 | feats); |
| 196 | if ((!r->letter_table |
| 197 | && ((full_buff[pos] < 'a') || (full_buff[pos] > 'z')))) |
| 198 | { |
| 199 | #ifdef EXCESSIVELY_CHATTY |
| 200 | cst_errmsg("lts:skipping unknown char \"%c\"\n", |
| 201 | full_buff[pos]); |
| 202 | #endif |
| 203 | continue; |
| 204 | } |
| 205 | if (r->letter_table) |
| 206 | index = full_buff[pos] - 3; |
| 207 | else |
| 208 | index = (full_buff[pos]-'a')%26; |
| 209 | phone = apply_model(fval_buff, |
| 210 | r->letter_index[index], |
| 211 | r->models); |
| 212 | /* delete epsilons and split dual-phones */ |
| 213 | if (cst_streq("epsilon",r->phone_table[phone])) |
| 214 | continue; |
| 215 | else if ((p=strchr(r->phone_table[phone],'-')) != NULL) |
| 216 | { |
| 217 | left = cst_substr(r->phone_table[phone],0, |
| 218 | cst_strlen(r->phone_table[phone])-cst_strlen(p)); |
| 219 | right = cst_substr(r->phone_table[phone], |
| 220 | (cst_strlen(r->phone_table[phone])-cst_strlen(p))+1, |
| 221 | (cst_strlen(p)-1)); |
| 222 | phones = cons_val(string_val(left), |
| 223 | cons_val(string_val(right),phones)); |
| 224 | cst_free(left); |
| 225 | cst_free(right); |
| 226 | } |
| 227 | else |
| 228 | phones = cons_val(string_val(r->phone_table[phone]),phones); |
| 229 | } |
| 230 | |
| 231 | cst_free(full_buff); |
| 232 | cst_free(fval_buff); |
| 233 | |
| 234 | return phones; |
| 235 | } |
| 236 | |
| 237 | static void cst_lts_get_state(cst_lts_rule *state, |
| 238 | const cst_lts_model *model, |
| 239 | unsigned short n, |
| 240 | int rule_size) |
| 241 | { /* As some OS's require a more elaborate access than a simple lookup */ |
| 242 | memmove(state,&model[n*rule_size],rule_size); |
| 243 | } |
| 244 | |
| 245 | static cst_lts_phone apply_model(cst_lts_letter *vals,cst_lts_addr start, |
| 246 | const cst_lts_model *model) |
| 247 | { |
| 248 | /* because some machines (ipaq/mips) can't deal with addrs not on */ |
| 249 | /* word boundaries we use a static and copy the rule values each time */ |
| 250 | /* so we know its properly aligned */ |
| 251 | /* Hmm this still might be wrong on some machines that align the */ |
| 252 | /* structure cst_lts_rules differently */ |
| 253 | cst_lts_rule state; |
| 254 | unsigned short nstate; |
| 255 | static const int sizeof_cst_lts_rule = 6; |
| 256 | |
| 257 | cst_lts_get_state(&state,model,start,sizeof_cst_lts_rule); |
| 258 | for ( ; |
| 259 | state.feat != CST_LTS_EOR; |
| 260 | ) |
| 261 | { |
| 262 | /* printf("%s %c %c %d\n",vals,vals[state.feat],state.val, |
| 263 | (vals[state.feat] == state.val) ? 1 : 0); */ |
| 264 | if (vals[state.feat] == state.val) |
| 265 | nstate = state.qtrue; |
| 266 | else |
| 267 | nstate = state.qfalse; |
| 268 | /* This should really happen at compilation time */ |
| 269 | #ifndef FLITE_PLUS_HTS_ENGINE |
| 270 | if (CST_BIG_ENDIAN) |
| 271 | nstate = SWAPSHORT(nstate); |
| 272 | #endif /* !FLITE_PLUS_HTS_ENGINE */ |
| 273 | |
| 274 | cst_lts_get_state(&state,model,nstate,sizeof_cst_lts_rule); |
| 275 | } |
| 276 | |
| 277 | return (cst_lts_phone)state.val; |
| 278 | } |