| 1 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2 |
;;; ;;; |
| 3 |
;;; Language Technologies Institute ;;; |
| 4 |
;;; Carnegie Mellon University ;;; |
| 5 |
;;; Copyright (c) 2007-2017 ;;; |
| 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: November 2007 ;;; |
| 35 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 36 |
;;; ;;; |
| 37 |
;;; Convert a clustergen voice to flite ;;; |
| 38 |
;;; (Oct 2014) support for random forests ;;; |
| 39 |
;;; (Jun 2017) support for quantized params ;;; |
| 40 |
;;; ;;; |
| 41 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 42 |
|
| 43 |
;; Used for getting smaller models, if non-zero this will reduce the |
| 44 |
;; order of the dumped models from whatever it is (probably 24) to this |
| 45 |
;; It does the right thing with statics and dynamics and stddev |
| 46 |
(defvar cg:relevant_params nil) ;; a list of param ranges to dump |
| 47 |
(defvar cg_reduced_order 0) |
| 48 |
(defvar cg:quantized_params t) ;; 8 bit quantized table |
| 49 |
(if (> cg_reduced_order 0) ;; just to remind me |
| 50 |
(format t "\n***** CG: note reducing order to %d *****\n\n" |
| 51 |
cg_reduced_order)) |
| 52 |
(defvar F0MEAN 0.0) |
| 53 |
(defvar F0STD 1.0) |
| 54 |
(defvar num_channels_additive_constant 4) |
| 55 |
(defvar new_min_range nil) |
| 56 |
|
| 57 |
(define (cg_convert name festvoxdir odir) |
| 58 |
"(cg_convert name festvoxdir odir) |
| 59 |
Convert a festvox cg voice into a C file." |
| 60 |
|
| 61 |
(load (format nil "%s/festvox/%s_cg.scm" festvoxdir name)) |
| 62 |
(eval (list (intern (format nil "voice_%s_cg" name)))) |
| 63 |
|
| 64 |
(if cg:quantized_params |
| 65 |
(if cg:rfs_models |
| 66 |
(system (format nil "$FLITEDIR/tools/quantize_params quantize_rf_models")) |
| 67 |
(system (format nil "$FLITEDIR/tools/quantize_params find_segments_quant festival/trees/%s_mcep.params" name)))) |
| 68 |
|
| 69 |
(set! ofd (fopen (path-append odir (string-append name "_cg.c")) "w")) |
| 70 |
(format ofd "/*****************************************************/\n") |
| 71 |
(format ofd "/** Autogenerated clustergen voice for %s */\n" name) |
| 72 |
(format ofd "/*****************************************************/\n") |
| 73 |
(format ofd "\n") |
| 74 |
(format ofd "#include \"cst_string.h\"\n") |
| 75 |
(format ofd "#include \"cst_cg.h\"\n") |
| 76 |
(format ofd "#include \"cst_cart.h\"\n") |
| 77 |
|
| 78 |
(format ofd "extern const cst_phoneset %s_phoneset;\n\n" name) |
| 79 |
|
| 80 |
(format t "cg_convert: converting F0 trees\n") |
| 81 |
;; F0 trees |
| 82 |
(if (and cg:rfs_models (probe_file "rf_models/mlistf0")) |
| 83 |
(set! f0ms (load "rf_models/mlistf0" t)) |
| 84 |
(set! f0ms (list '01))) |
| 85 |
|
| 86 |
(if (and cg:rfs_models (probe_file "rf_models/mlistf0")) |
| 87 |
(begin ;; Random Forest F0 Models |
| 88 |
(format t "cg_convert: converting rf F0 trees\n") |
| 89 |
(mapcar |
| 90 |
(lambda (f0m) |
| 91 |
(format t "cg_convert: converting model_%02d f0 params\n" f0m) |
| 92 |
(set! val_table nil) |
| 93 |
(cg_convert_carts |
| 94 |
(load (format nil "rf_models/trees_%02d/%s_f0.tree" f0m name) t) |
| 95 |
(format nil "%02d_f0" f0m) name odir) |
| 96 |
(format ofd "extern const cst_cart * const %s_%02d_f0_carts[];\n" name f0m)) |
| 97 |
f0ms)) |
| 98 |
(begin ;; No-random Forest F0 Models (just one model) |
| 99 |
(set! val_table nil) ;; different val number over the two sets of carts |
| 100 |
(cg_convert_carts |
| 101 |
(load (format nil "festival/trees/%s_f0.tree" name) t) |
| 102 |
"01_f0" name odir) |
| 103 |
(format ofd "\n") |
| 104 |
(format ofd "extern const cst_cart * const %s_01_f0_carts[];\n" name ))) |
| 105 |
|
| 106 |
(if cg:spamf0 |
| 107 |
(begin |
| 108 |
(set! acctrack (track.load "festival/trees/cb.params")) |
| 109 |
(format ofd "extern const cst_cart %s_spamf0_phrase_cart;\n" name) |
| 110 |
(format ofd "extern const cst_cart %s_spamf0_accent_cart;\n" name) |
| 111 |
(format ofd "extern const float * const %s_spamf0_accent_vectors[];\n" name) |
| 112 |
(format ofd "#define %s_spamf0_accent_num_channels %d\n" name (track.num_channels acctrack)) |
| 113 |
(format ofd "#define %s_spamf0_accent_num_frames %d\n" name (track.num_frames acctrack)) |
| 114 |
)) |
| 115 |
|
| 116 |
;; spectral trees |
| 117 |
(set! val_table nil) ;; different val number over the two sets of carts |
| 118 |
|
| 119 |
(if cg:rfs_models |
| 120 |
(set! pms (load "rf_models/mlist" t)) |
| 121 |
(set! pms (list '01))) |
| 122 |
|
| 123 |
(if cg:rfs_models |
| 124 |
(begin ;; Random Forest Spectral Models |
| 125 |
(format t "cg_convert: converting rf spectral trees\n") |
| 126 |
(mapcar |
| 127 |
(lambda (pm) |
| 128 |
(set! old_carttoC_extract_answer carttoC_extract_answer) |
| 129 |
(set! carttoC_extract_answer carttoC_extract_spectral_frame) |
| 130 |
(set! val_table nil) |
| 131 |
(cg_convert_carts |
| 132 |
(load (format nil "rf_models/trees_%02d/%s_mcep.tree" pm name) t) |
| 133 |
(format nil "%02d_mcep" pm) name odir) |
| 134 |
(set! carttoC_extract_answer old_carttoC_extract_answer) |
| 135 |
(format ofd "\n") |
| 136 |
(format ofd "extern const cst_cart * const %s_%02d_mcep_carts[];\n" name pm) |
| 137 |
|
| 138 |
;; spectral params |
| 139 |
(if cg:quantized_params |
| 140 |
;; Quantized params use 8 bit indexs -- you |
| 141 |
;; must externally create quantized params first |
| 142 |
(cg_convert_params_quantized |
| 143 |
(format nil "rf_models/trees_%02d/%s_mcep.params.q_params" pm name) |
| 144 |
(format nil "rf_models/trees_%02d/%s_mcep.params.q_table" pm name) |
| 145 |
name (format nil "%02d" pm) odir ofd) |
| 146 |
(cg_convert_params |
| 147 |
(format nil "rf_models/trees_%02d/%s_mcep.params" pm name) |
| 148 |
(format nil "festival/trees/%s_min_range.scm" name) |
| 149 |
name (format nil "%02d" pm) odir ofd)) |
| 150 |
(format ofd "extern const unsigned short * const %s_%02d_model_vectors[];\n" name pm )) |
| 151 |
pms)) |
| 152 |
(begin ;; Non-random forest spectral models (one model) |
| 153 |
(format t "cg_convert: converting single spectral trees\n") |
| 154 |
(set! old_carttoC_extract_answer carttoC_extract_answer) |
| 155 |
(set! carttoC_extract_answer carttoC_extract_spectral_frame) |
| 156 |
(set! val_table nil) |
| 157 |
(cg_convert_carts |
| 158 |
(load (format nil "festival/trees/%s_mcep.tree" name) t) |
| 159 |
"01_mcep" name odir) |
| 160 |
(set! carttoC_extract_answer old_carttoC_extract_answer) |
| 161 |
(format ofd "\n") |
| 162 |
(format ofd "extern const cst_cart * const %s_01_mcep_carts[];\n" name ) |
| 163 |
;; spectral params |
| 164 |
(if cg:quantized_params |
| 165 |
(cg_convert_params_quantized |
| 166 |
(format nil "festival/trees/%s_mcep.params.q_params" name) |
| 167 |
(format nil "festival/trees/%s_mcep.params.q_table" name) |
| 168 |
name "01" odir ofd) |
| 169 |
(cg_convert_params |
| 170 |
(format nil "festival/trees/%s_mcep.params" name) |
| 171 |
(format nil "festival/trees/%s_min_range.scm" name) |
| 172 |
name "01" odir ofd)) |
| 173 |
(format ofd "extern const unsigned short * const %s_01_model_vectors[];\n" name ) |
| 174 |
)) |
| 175 |
|
| 176 |
(if (probe_file (format nil "festvox/%s_char_phone_map.scm" name)) |
| 177 |
(begin |
| 178 |
(setq cpm (load (format nil "festvox/%s_char_phone_map.scm" name) t)) |
| 179 |
(format ofd "\nstatic char * const %s_char_phone_map[%s][2] =\n" |
| 180 |
name (+ 1 (length cpm))) |
| 181 |
(format ofd "{\n") |
| 182 |
(mapcar |
| 183 |
(lambda (x) |
| 184 |
(format ofd " { %l, \"%s\" },\n" (car x) (cadr x))) |
| 185 |
cpm) |
| 186 |
(format ofd " { NULL, NULL }\n") |
| 187 |
(format ofd "};\n\n") |
| 188 |
)) |
| 189 |
|
| 190 |
(format ofd "#define %s_num_f0_models %d\n" name (length f0ms)) |
| 191 |
(format ofd "const cst_cart **%s_f0_carts_table[] = {\n" name) |
| 192 |
(mapcar |
| 193 |
(lambda (f0m) |
| 194 |
(format ofd " (const cst_cart **)%s_%02d_f0_carts,\n" name f0m)) |
| 195 |
f0ms) |
| 196 |
(format ofd "NULL};\n") |
| 197 |
|
| 198 |
(format ofd "#define %s_num_param_models %d\n" name (length pms)) |
| 199 |
(format ofd "int %s_num_channels_table[] = {\n" name) |
| 200 |
(mapcar |
| 201 |
(lambda (pm) |
| 202 |
(format ofd " %s_%02d_num_channels,\n" name pm)) |
| 203 |
pms) |
| 204 |
(format ofd "0};\n") |
| 205 |
(format ofd "int %s_num_frames_table[] = {\n" name) |
| 206 |
(mapcar |
| 207 |
(lambda (pm) |
| 208 |
(format ofd " %s_%02d_num_frames,\n" name pm)) |
| 209 |
pms) |
| 210 |
(format ofd "0};\n") |
| 211 |
(format ofd "const unsigned short **%s_model_vectors_table[] = {\n" name) |
| 212 |
(mapcar |
| 213 |
(lambda (pm) |
| 214 |
(format ofd " (const unsigned short **)%s_%02d_model_vectors,\n" name pm)) |
| 215 |
pms) |
| 216 |
(format ofd "NULL};\n") |
| 217 |
(if cg:quantized_params |
| 218 |
(begin |
| 219 |
(format ofd "const float **%s_model_qtable[] = {\n" name) |
| 220 |
(mapcar |
| 221 |
(lambda (pm) |
| 222 |
(format ofd " (const float **)%s_%02d_qtable,\n" name pm)) |
| 223 |
pms) |
| 224 |
(format ofd "NULL};\n")) |
| 225 |
(begin |
| 226 |
(format ofd "const float **%s_model_qtable[] = {NULL}; /* not used */ \n" name) |
| 227 |
)) |
| 228 |
(format ofd "const cst_cart **%s_mcep_carts_table[] = {\n" name) |
| 229 |
(mapcar |
| 230 |
(lambda (pm) |
| 231 |
(format ofd " (const cst_cart **)%s_%02d_mcep_carts,\n" name pm)) |
| 232 |
pms) |
| 233 |
(format ofd "NULL};\n") |
| 234 |
|
| 235 |
;; duration model (cart conversion) |
| 236 |
(if cg:rfs_dur_models |
| 237 |
(set! dms (load "dur_rf_models/mlist" t)) |
| 238 |
(set! dms '(01))) |
| 239 |
|
| 240 |
(if cg:rfs_dur_models |
| 241 |
(begin |
| 242 |
(format t "cg_convert: converting rf duration models\n") |
| 243 |
(mapcar |
| 244 |
(lambda (dm) |
| 245 |
(format t "cg_convert: converting %02d duration model\n" dm) |
| 246 |
(set! val_table nil) |
| 247 |
(cg_convert_durmodel |
| 248 |
(format nil "dur_rf_models/dur_%02d/%s_durdata_cg.scm" dm name) |
| 249 |
(format nil "%s_cg_%02d_" name dm) odir) |
| 250 |
(format ofd "extern const dur_stat * const %s_cg_%02d_dur_stats[];\n" name dm) |
| 251 |
(format ofd "extern const cst_cart %s_cg_%02d_dur_cart;\n" name dm)) |
| 252 |
dms)) |
| 253 |
(begin |
| 254 |
(format t "cg_convert: converting single duration model\n") |
| 255 |
(format t "cg_convert: converting 01 duration model\n") |
| 256 |
(cg_convert_durmodel |
| 257 |
(format nil "festvox/%s_durdata_cg.scm" name) |
| 258 |
(format nil "%s_cg_%02d_" name 01) odir) |
| 259 |
(format ofd "extern const dur_stat * const %s_cg_%02d_dur_stats[];\n" name 01) |
| 260 |
(format ofd "extern const cst_cart %s_cg_%02d_dur_cart;\n" name 01) |
| 261 |
)) |
| 262 |
|
| 263 |
(format ofd "#define %s_num_dur_models %d\n" name (length dms)) |
| 264 |
(format ofd "const dur_stat **%s_dur_stats_table[] = {\n" name) |
| 265 |
(mapcar |
| 266 |
(lambda (dm) |
| 267 |
(format ofd " (const dur_stat **)%s_cg_%02d_dur_stats,\n" name dm)) |
| 268 |
dms) |
| 269 |
(format ofd "NULL};\n") |
| 270 |
(format ofd "const cst_cart *%s_dur_cart_table[] = {\n" name) |
| 271 |
(mapcar |
| 272 |
(lambda (dm) |
| 273 |
(format ofd " &%s_cg_%02d_dur_cart,\n" name dm)) |
| 274 |
dms) |
| 275 |
(format ofd "NULL};\n") |
| 276 |
|
| 277 |
;; phone to states |
| 278 |
(format t "cg_convert: converting phone to state map\n") |
| 279 |
(cg_phone_to_states |
| 280 |
(format nil "festvox/%s_statenames.scm" name) |
| 281 |
name odir) |
| 282 |
(format ofd "extern const char * const *%s_phone_states[];\n" name) |
| 283 |
|
| 284 |
(format ofd "\n") |
| 285 |
(format ofd "const char * const %s_types[] = {\n" name) |
| 286 |
(mapcar |
| 287 |
(lambda (cart) |
| 288 |
(format ofd " \"%s\",\n" (car cart))) |
| 289 |
(load (format nil "festival/trees/%s_f0.tree" name) t)) |
| 290 |
(format ofd " NULL};\n") |
| 291 |
(format ofd "#define %s_num_types %d\n\n" |
| 292 |
name |
| 293 |
(length (load (format nil "festival/trees/%s_f0.tree" name) t))) |
| 294 |
|
| 295 |
(format ofd "const float %s_model_min[] = { \n" name) |
| 296 |
(mapcar |
| 297 |
(lambda (p) |
| 298 |
(format ofd " %f,\n" (car p))) |
| 299 |
(reverse new_min_range)) |
| 300 |
(format ofd "};\n") |
| 301 |
(format ofd "const float %s_model_range[] = { \n" name) |
| 302 |
(mapcar |
| 303 |
(lambda (p) |
| 304 |
(format ofd " %f,\n" (cadr p))) |
| 305 |
(reverse new_min_range)) |
| 306 |
(format ofd "};\n") |
| 307 |
|
| 308 |
(format ofd "float %s_dynwin[] = { -0.5, 0.0, 0.5 };\n" name) |
| 309 |
(format ofd "#define %s_dynwinsize 3\n" name) |
| 310 |
|
| 311 |
(if cg:mixed_excitation |
| 312 |
(begin |
| 313 |
;; Uses filters in festvox/mef.track (from Jan 2013) |
| 314 |
(set! n 0) |
| 315 |
(while (< n 5) |
| 316 |
(format ofd "const double %s_me_filter_%d[] = {\n" name n) |
| 317 |
(set! o 0) |
| 318 |
(while (< o 46) |
| 319 |
(format ofd "%f, " (track.get me_filter_track n o)) |
| 320 |
(set! o (+ o 1))) |
| 321 |
(format ofd "%f\n};\n" (track.get me_filter_track n o)) |
| 322 |
(set! n (+ n 1)) |
| 323 |
) |
| 324 |
(format ofd "const double * const %s_me_h[] = {\n" name) |
| 325 |
(format ofd " %s_me_filter_0,\n" name) |
| 326 |
(format ofd " %s_me_filter_1,\n" name) |
| 327 |
(format ofd " %s_me_filter_2,\n" name) |
| 328 |
(format ofd " %s_me_filter_3,\n" name) |
| 329 |
(format ofd " %s_me_filter_4\n" name) |
| 330 |
(format ofd "};\n\n") |
| 331 |
)) |
| 332 |
|
| 333 |
(format ofd "const cst_cg_db %s_cg_db = {\n" name) |
| 334 |
(format ofd " \"%s\",\n" name) |
| 335 |
(format ofd " %s_types,\n" name) |
| 336 |
(format ofd " %s_num_types,\n" name) |
| 337 |
(if (boundp 'framerate) |
| 338 |
(format ofd " %d,\n" framerate) ;; sample rate |
| 339 |
(format ofd " 16000,\n")) ;; sample rate |
| 340 |
|
| 341 |
(format ofd " %f,%f,\n" F0MEAN F0STD) |
| 342 |
|
| 343 |
(format ofd " %s_num_f0_models,\n" name) |
| 344 |
(format ofd " %s_f0_carts_table,\n" name) |
| 345 |
(format ofd " %s_num_param_models,\n" name) |
| 346 |
(format ofd " %s_mcep_carts_table,\n" name) |
| 347 |
(if cg:spamf0 |
| 348 |
(begin |
| 349 |
(set! mfd (fopen (path-append odir "paramfiles.mak") "a")) |
| 350 |
(format mfd "SPAMF0=true\n") |
| 351 |
(fclose mfd) |
| 352 |
(format ofd " &%s_spamf0_accent_cart,\n" name) |
| 353 |
(format ofd " &%s_spamf0_phrase_cart,\n" name) |
| 354 |
) |
| 355 |
(begin |
| 356 |
(set! mfd (fopen (path-append odir "paramfiles.mak") "a")) |
| 357 |
(format mfd "SPAMF0=false\n") |
| 358 |
(fclose mfd) |
| 359 |
(format ofd " NULL,NULL,\n") |
| 360 |
) |
| 361 |
) |
| 362 |
(format ofd " %s_num_channels_table,\n" name) |
| 363 |
(format ofd " %s_num_frames_table,\n" name) |
| 364 |
(format ofd " %s_model_vectors_table,\n" name) |
| 365 |
|
| 366 |
(if cg:spamf0 |
| 367 |
(begin |
| 368 |
(format ofd " %s_spamf0_accent_num_channels,\n" name) |
| 369 |
(format ofd " %s_spamf0_accent_num_frames,\n" name) |
| 370 |
(format ofd " %s_spamf0_accent_vectors,\n" name) |
| 371 |
) |
| 372 |
(format ofd " 0,0,NULL,\n") |
| 373 |
) |
| 374 |
(format ofd " %s_model_min,\n" name) |
| 375 |
(format ofd " %s_model_range,\n" name) |
| 376 |
|
| 377 |
(cond |
| 378 |
((not cg:quantized_params) |
| 379 |
;; Simple 2 values per short |
| 380 |
(format ofd " NULL, /* no quantization table(s) */\n") |
| 381 |
(format ofd " CST_CG_MODEL_SHAPE_BASE_MINRANGE,\n") |
| 382 |
) |
| 383 |
((eq 41 cg_model_num_channels) |
| 384 |
(format ofd " %s_model_qtable,\n" name) |
| 385 |
(format ofd " CST_CG_MODEL_SHAPE_QUANTIZED_PARAMS_41,\n")) |
| 386 |
(t |
| 387 |
(format ofd " %s_model_qtable,\n" name) |
| 388 |
(format ofd " CST_CG_MODEL_SHAPE_QUANTIZED_PARAMS,\n"))) |
| 389 |
|
| 390 |
(format ofd " %f, /* frame_advance */\n" cg:frame_shift) |
| 391 |
|
| 392 |
(format ofd " %s_num_dur_models,\n" name) |
| 393 |
(format ofd " %s_dur_stats_table,\n" name) |
| 394 |
(format ofd " %s_dur_cart_table,\n" name) |
| 395 |
(format ofd " %s_phone_states,\n" name) |
| 396 |
|
| 397 |
(format ofd " 1, /* 1 if mlpg required */\n") |
| 398 |
(format ofd " %s_dynwin,\n" name) |
| 399 |
(format ofd " %s_dynwinsize,\n" name) |
| 400 |
|
| 401 |
(format ofd " %f, /* mlsa_alpha */\n" mlsa_alpha_param) |
| 402 |
(format ofd " %f, /* mlsa_beta */\n" 0.4) |
| 403 |
|
| 404 |
(if cg:multimodel |
| 405 |
(format ofd " 1, /* cg:multimodel */\n") |
| 406 |
(format ofd " 0, /* cg:multimodel */\n")) |
| 407 |
|
| 408 |
(if cg:mixed_excitation |
| 409 |
(begin |
| 410 |
(format ofd " 1, /* cg:mixed_excitation */\n") |
| 411 |
(format ofd " 5,47, /* filter sizes */\n") |
| 412 |
(format ofd " %s_me_h, \n" name)) |
| 413 |
(begin |
| 414 |
(format ofd " 0, /* cg:mixed_excitation */\n") |
| 415 |
(format ofd " 0,0, /* cg:mixed_excitation */\n") |
| 416 |
(format ofd " NULL, \n"))) |
| 417 |
(if cg:spamf0 |
| 418 |
(format ofd " 1, /* cg:spamf0 */\n") |
| 419 |
(format ofd " 0, /* cg:spamf0 */\n")) |
| 420 |
(format ofd " 1.5 /* gain */\n") |
| 421 |
|
| 422 |
;; If a grapheme language, add phoneset and char_phone_map |
| 423 |
(if (probe_file (format nil "festvox/%s_char_phone_map.scm" name)) |
| 424 |
(begin |
| 425 |
(format ofd " ,\n") |
| 426 |
(format ofd " &%s_phoneset,\n" name) |
| 427 |
(format ofd " &%s_char_phone_map\n" name) |
| 428 |
)) |
| 429 |
|
| 430 |
(format ofd "};\n") |
| 431 |
|
| 432 |
(fclose ofd) |
| 433 |
) |
| 434 |
|
| 435 |
(define (unit_type u) |
| 436 |
(apply |
| 437 |
string-append |
| 438 |
(reverse |
| 439 |
(symbolexplode |
| 440 |
(string-after |
| 441 |
(apply |
| 442 |
string-append |
| 443 |
(reverse (symbolexplode u))) |
| 444 |
"_"))))) |
| 445 |
|
| 446 |
(define (unit_occur u) |
| 447 |
(apply |
| 448 |
string-append |
| 449 |
(reverse |
| 450 |
(symbolexplode |
| 451 |
(string-before |
| 452 |
(apply |
| 453 |
string-append |
| 454 |
(reverse (symbolexplode u))) |
| 455 |
"_"))))) |
| 456 |
|
| 457 |
(define (cg_convert_durmodel durmodelfn name odir) |
| 458 |
|
| 459 |
(set! durmodel (load durmodelfn t)) |
| 460 |
(set! phonedurs (cadr (car (cddr (car durmodel))))) |
| 461 |
(set! zdurtree (cadr (car (cddr (cadr durmodel))))) |
| 462 |
|
| 463 |
(set! dfd (fopen (path-append odir (string-append name "durmodel.c")) "w")) |
| 464 |
(set! dfdh (fopen (path-append odir (string-append name "durmodel.h")) "w")) |
| 465 |
(format dfd "/*****************************************************/\n") |
| 466 |
(format dfd "/** Autogenerated durmodel_cg for %s */\n" name) |
| 467 |
(format dfd "/*****************************************************/\n") |
| 468 |
|
| 469 |
(format dfd "#include \"cst_synth.h\"\n") |
| 470 |
(format dfd "#include \"cst_string.h\"\n") |
| 471 |
(format dfd "#include \"cst_cart.h\"\n") |
| 472 |
(format dfd "#include \"%sdurmodel.h\"\n\n" name) |
| 473 |
|
| 474 |
(mapcar |
| 475 |
(lambda (s) |
| 476 |
(format dfd "static const dur_stat dur_state_%s = { \"%s\", %f, %f };\n" |
| 477 |
(cg_normal_phone_name (car s)) |
| 478 |
(car s) (car (cdr s)) (car (cddr s))) |
| 479 |
) |
| 480 |
phonedurs) |
| 481 |
(format dfd "\n") |
| 482 |
|
| 483 |
(format dfd "const dur_stat * const %sdur_stats[] = {\n" name) |
| 484 |
(mapcar |
| 485 |
(lambda (s) |
| 486 |
(format dfd " &dur_state_%s,\n" (cg_normal_phone_name (car s)))) |
| 487 |
phonedurs) |
| 488 |
(format dfd " NULL\n};\n") |
| 489 |
|
| 490 |
(set! val_table nil) |
| 491 |
(set! current_node -1) |
| 492 |
(set! feat_nums nil) |
| 493 |
(do_carttoC dfd dfdh |
| 494 |
(format nil "%s%s" name "dur") |
| 495 |
zdurtree) |
| 496 |
|
| 497 |
(fclose dfd) |
| 498 |
(fclose dfdh) |
| 499 |
) |
| 500 |
|
| 501 |
(define (cg_phone_to_states phonestatefn name odir) |
| 502 |
|
| 503 |
(set! dfd (fopen (path-append odir (string-append name "_cg_phonestate.c")) "w")) |
| 504 |
(format dfd "/*****************************************************/\n") |
| 505 |
(format dfd "/** Autogenerated phonestate_cg for %s */\n" name) |
| 506 |
(format dfd "/*****************************************************/\n") |
| 507 |
|
| 508 |
(set! phonestates (load phonestatefn t)) |
| 509 |
|
| 510 |
(mapcar |
| 511 |
(lambda (x) |
| 512 |
(format dfd "const char * const %s_%s_ps[] = { " name |
| 513 |
(cg_normal_phone_name (car x))) |
| 514 |
(mapcar |
| 515 |
(lambda (y) (format dfd "\"%s\", " y)) |
| 516 |
x) |
| 517 |
(format dfd " 0};\n")) |
| 518 |
(cadr (caddr (car phonestates)))) |
| 519 |
|
| 520 |
(format dfd "const char * const * const %s_phone_states[] = {\n" name) |
| 521 |
(mapcar |
| 522 |
(lambda (x) |
| 523 |
(format dfd " %s_%s_ps,\n" name |
| 524 |
(cg_normal_phone_name (car x)))) |
| 525 |
(cadr (caddr (car phonestates)))) |
| 526 |
(format dfd " 0};\n") |
| 527 |
|
| 528 |
(fclose dfd) |
| 529 |
) |
| 530 |
|
| 531 |
(define (cg_convert_params mcepfn mcepminrangefn name type odir cofd) |
| 532 |
(let ((param.track (track.load mcepfn)) |
| 533 |
(i 0) (mfd)) |
| 534 |
(format t "cg_convert: converting model_%s spectral params\n" type) |
| 535 |
|
| 536 |
(set! mfd (fopen (path-append odir (string-append name "_cg_" type "_params.c")) "w")) |
| 537 |
(format mfd "/*****************************************************/\n") |
| 538 |
(format mfd "/** Autogenerated model_vectors for %s */\n" name) |
| 539 |
(format mfd "/*****************************************************/\n") |
| 540 |
(set! num_channels (track.num_channels param.track)) |
| 541 |
(set! num_frames (track.num_frames param.track)) |
| 542 |
(set! cg_model_num_channels num_channels) |
| 543 |
(format mfd "/** Size: %d */\n" cg_model_num_channels) |
| 544 |
;; Output each frame |
| 545 |
(set! mcep_min_range (load mcepminrangefn t)) |
| 546 |
(while (< i num_frames) |
| 547 |
(output_param_frame name type param.track i mfd) |
| 548 |
(set! i (+ 1 i))) |
| 549 |
(format mfd "\n\n") |
| 550 |
;; Output each frame |
| 551 |
(format mfd "const unsigned short * const %s_%s_model_vectors[] = {\n" name type) |
| 552 |
(set! i 0) |
| 553 |
(while (< i num_frames) |
| 554 |
(format mfd " %s_%s_param_frame_%d,\n" name type i) |
| 555 |
(set! i (+ 1 i))) |
| 556 |
(format mfd "};\n\n") |
| 557 |
|
| 558 |
(if cg:mixed_excitation |
| 559 |
(begin |
| 560 |
(set! num_channels_additive_constant 14) |
| 561 |
)) |
| 562 |
|
| 563 |
(if (> cg_reduced_order 0) |
| 564 |
(format cofd "#define %s_%s_num_channels %d\n" |
| 565 |
name type (+ num_channels_additive_constant (* 4 cg_reduced_order))) |
| 566 |
(format cofd "#define %s_%s_num_channels %d\n" name type num_channels)) |
| 567 |
|
| 568 |
(format cofd "#define %s_%s_num_frames %d\n" name type num_frames) |
| 569 |
|
| 570 |
(fclose mfd) |
| 571 |
|
| 572 |
)) |
| 573 |
|
| 574 |
(define (cg_convert_params_quantized mcepfn mcepqtable name type odir cofd) |
| 575 |
(let ((param.track (track.load mcepfn)) |
| 576 |
(qtable.track (track.load mcepqtable)) |
| 577 |
(i 0) (mfd)) |
| 578 |
(format t "cg_convert: converting model_%s quantized spectral params\n" type) |
| 579 |
(set! mfd (fopen (path-append odir (string-append name "_cg_" type "_params.c")) "w")) |
| 580 |
(format mfd "/*****************************************************/\n") |
| 581 |
(format mfd "/** Autogenerated model_vectors (quantized) for %s */\n" name) |
| 582 |
(format mfd "/*****************************************************/\n") |
| 583 |
;; This will be half the actual number of channels |
| 584 |
;; as two vals are encoded per (16 bit) entry |
| 585 |
(set! num_channels (track.num_channels param.track)) |
| 586 |
(set! num_frames (track.num_frames param.track)) |
| 587 |
(set! cg_model_num_channels num_channels) |
| 588 |
(format mfd "/** Size: %d channels */\n" cg_model_num_channels) |
| 589 |
;; Output each frame |
| 590 |
(while (< i num_frames) |
| 591 |
;; output vals without normalization -- its already happened |
| 592 |
(output_param_frame_asis name type param.track i mfd) |
| 593 |
(set! i (+ 1 i))) |
| 594 |
(format mfd "\n\n") |
| 595 |
;; Output each frame |
| 596 |
(format mfd "const unsigned short * const %s_%s_model_vectors[] = {\n" name type) |
| 597 |
(set! i 0) |
| 598 |
(while (< i num_frames) |
| 599 |
(format mfd " %s_%s_param_frame_%d,\n" name type i) |
| 600 |
(set! i (+ 1 i))) |
| 601 |
(format mfd "};\n\n") |
| 602 |
|
| 603 |
(if cg:mixed_excitation |
| 604 |
(begin |
| 605 |
(set! num_channels_additive_constant 14) |
| 606 |
)) |
| 607 |
|
| 608 |
;; LIE about num channels (put in model number of channels not |
| 609 |
;; num of channels in compressed/quantized track |
| 610 |
(format cofd "#define %s_%s_num_channels %d\n" name type |
| 611 |
(cond |
| 612 |
((and cg:quantized_params (equal? 41 num_channels)) |
| 613 |
114) ;; naively assume this is the special compression |
| 614 |
(cg:quantized_params |
| 615 |
(* 2 num_channels)) |
| 616 |
(t |
| 617 |
num_channels))) |
| 618 |
|
| 619 |
(format cofd "#define %s_%s_num_frames %d\n" name type num_frames) |
| 620 |
|
| 621 |
;; Dump the q_table too, that gives the lookup table to map values back |
| 622 |
(set! num_channels (track.num_channels qtable.track)) |
| 623 |
(set! num_frames (track.num_frames qtable.track)) |
| 624 |
(set! i 0) |
| 625 |
;; Output each frame |
| 626 |
(while (< i num_frames) |
| 627 |
(format mfd "static const float %s_%s_qtable_frame_%d[] = { \n" name type i) |
| 628 |
(set! j 0) |
| 629 |
(while (< j num_channels) |
| 630 |
(format mfd " %f," (track.get qtable.track i j)) |
| 631 |
(set! j (+ 1 j))) |
| 632 |
(format mfd " };\n") |
| 633 |
(set! i (+ 1 i))) |
| 634 |
(format mfd "\n\n") |
| 635 |
;; Output each frame |
| 636 |
(format mfd "const float * const %s_%s_qtable[] = {\n" name type) |
| 637 |
(set! i 0) |
| 638 |
(while (< i num_frames) |
| 639 |
(format mfd " %s_%s_qtable_frame_%d,\n" name type i) |
| 640 |
(set! i (+ 1 i))) |
| 641 |
(format mfd "};\n\n") |
| 642 |
;; add extern reference to the qtable to main file |
| 643 |
(format cofd "extern const float * const %s_%s_qtable[];\n" name type) |
| 644 |
(fclose mfd) |
| 645 |
|
| 646 |
)) |
| 647 |
|
| 648 |
(define (mcepcoeff_norm c min range) |
| 649 |
(let ((x (* (/ (- c min) range) 65535))) |
| 650 |
(cond |
| 651 |
((< x 0) 0.0) |
| 652 |
((> x 65535) 65535) |
| 653 |
(t x)))) |
| 654 |
|
| 655 |
(define (output_accent_frame name track f ofd) |
| 656 |
"(output_accent_frame name track frame ofd) |
| 657 |
Ouput this accent params." |
| 658 |
(let ((i 0) (nc (track.num_channels track))) |
| 659 |
;(format ofd "static const unsigned short %s_spamf0_accent_frame_%d[] = { \n" name f) |
| 660 |
(format ofd "static const float %s_spamf0_accent_frame_%d[] = { \n" name f) |
| 661 |
(while (< i nc) |
| 662 |
(format ofd " %f," (track.get track f i)) |
| 663 |
(set! i (+ 1 i))) |
| 664 |
(format ofd " };\n") |
| 665 |
) |
| 666 |
) |
| 667 |
|
| 668 |
(define (output_param_frame name type track f ofd) |
| 669 |
"(output_param_frame name track frame ofd) |
| 670 |
Ouput this frame." |
| 671 |
(let ((i 0) (nc (track.num_channels track))) |
| 672 |
(format ofd "static const unsigned short %s_%s_param_frame_%d[] = { \n" name type f) |
| 673 |
(set! min_range mcep_min_range) |
| 674 |
(set! real_order (/ (- nc 4) 4)) |
| 675 |
(set! new_min_range nil) |
| 676 |
|
| 677 |
(if cg:relevant_params |
| 678 |
(begin ;; specified number of parameters |
| 679 |
) |
| 680 |
(if cg:mixed_excitation |
| 681 |
(begin |
| 682 |
|
| 683 |
(while (< i nc) |
| 684 |
(if (or (eq cg_reduced_order 0) |
| 685 |
(< i (* 2 (+ 1 cg_reduced_order))) ;; static and static_stddev |
| 686 |
(and (> i (- (/ (- nc 10) 2) 1)) ;; deltas and delta_stddev |
| 687 |
(< i (+ (/ (- nc 10) 2) (* 2 cg_reduced_order)))) |
| 688 |
(> i (- nc 13))) |
| 689 |
(begin |
| 690 |
; (format t "i is %d %d\n" i (+ (/ nc 2) (* 2 cg_reduced_order))) |
| 691 |
(format ofd " %d," |
| 692 |
(mcepcoeff_norm |
| 693 |
(track.get track f i) |
| 694 |
(caar min_range) |
| 695 |
(cadr (car min_range)))) |
| 696 |
(set! new_min_range (cons (car min_range) new_min_range)) |
| 697 |
)) |
| 698 |
(set! min_range (cdr min_range)) |
| 699 |
(set! i (+ 1 i))) |
| 700 |
(format ofd " };\n") |
| 701 |
) |
| 702 |
(begin |
| 703 |
(while (< i nc) |
| 704 |
(if (or (eq cg_reduced_order 0) |
| 705 |
(< i (* 2 (+ 1 cg_reduced_order))) ;; static and static_stddev |
| 706 |
(and (> i (- (/ nc 2) 1)) ;; deltas and delta_stddev |
| 707 |
(< i (+ (/ nc 2) (* 2 cg_reduced_order)))) |
| 708 |
(> i (- nc 3))) |
| 709 |
(begin |
| 710 |
; (format t "i is %d %d\n" i (+ (/ nc 2) (* 2 cg_reduced_order))) |
| 711 |
(format ofd " %d," |
| 712 |
(mcepcoeff_norm |
| 713 |
(track.get track f i) |
| 714 |
(caar min_range) |
| 715 |
(cadr (car min_range)))) |
| 716 |
(set! new_min_range (cons (car min_range) new_min_range)) |
| 717 |
)) |
| 718 |
(set! min_range (cdr min_range)) |
| 719 |
(set! i (+ 1 i))) |
| 720 |
(format ofd " };\n") |
| 721 |
))) |
| 722 |
) |
| 723 |
) |
| 724 |
|
| 725 |
(define (output_param_frame_asis name type track f ofd) |
| 726 |
"(output_param_frame_asis name track frame ofd) |
| 727 |
Ouput this frame." |
| 728 |
;; This is (maybe) hardcoded for rf3 builds which are statics, deltas, me. |
| 729 |
;; It assumes any fancy coding has externally been done so just dumps |
| 730 |
;; what is there asis. |
| 731 |
(let ((i 0) (nc (track.num_channels track))) |
| 732 |
(format ofd "static const unsigned short %s_%s_param_frame_%d[] = { \n" name type f) |
| 733 |
(while (< i nc) |
| 734 |
(format ofd " %d," (track.get track f i)) |
| 735 |
(set! i (+ 1 i))) |
| 736 |
(format ofd " };\n") |
| 737 |
)) |
| 738 |
|
| 739 |
(define (carttoC_extract_spectral_frame ofdh tree) |
| 740 |
"(carttoC_extract_spectral_frame tree) |
| 741 |
Get list of answers from leaf node." |
| 742 |
(carttoC_val_table ofdh |
| 743 |
(car (car tree)) |
| 744 |
'none)) |
| 745 |
|
| 746 |
(define (cg_convert_carts carts prefix name odir) |
| 747 |
"(define cg_convert_carts cartfn name) |
| 748 |
Output cg selection carts into odir/name_carts.c" |
| 749 |
(let (ofd ofdh) |
| 750 |
;; Set up to dump full list of things at leafs |
| 751 |
;; default processing of leaf (int or float) is fine |
| 752 |
|
| 753 |
(set! ofd (fopen (format nil "%s/%s_cg_%s_trees.c" odir name prefix) "w")) |
| 754 |
(set! ofdh (fopen (format nil "%s/%s_cg_%s_trees.h" odir name prefix) "w")) |
| 755 |
(format ofd "/*****************************************************/\n") |
| 756 |
(format ofd "/** Autogenerated %s %s carts */\n" name prefix) |
| 757 |
(format ofd "/*****************************************************/\n") |
| 758 |
(format ofd "\n") |
| 759 |
(format ofd "#include \"cst_string.h\"\n") |
| 760 |
(format ofd "#include \"cst_cart.h\"\n") |
| 761 |
(format ofd "#include \"%s_cg_%s_trees.h\"\n" name prefix) |
| 762 |
|
| 763 |
(mapcar |
| 764 |
(lambda (cart) |
| 765 |
(if (string-equal "string" (typeof (car cart))) |
| 766 |
(begin |
| 767 |
(set! current_node -1) |
| 768 |
(set! feat_nums nil) |
| 769 |
(do_carttoC ofd ofdh |
| 770 |
(format nil "%s_%s_%s" name prefix |
| 771 |
(cg_normal_phone_name (car cart))) |
| 772 |
(cadr cart))))) |
| 773 |
carts) |
| 774 |
|
| 775 |
(format ofd "\n\n") |
| 776 |
(format ofd "const cst_cart * const %s_%s_carts[] = {\n" name prefix) |
| 777 |
(mapcar |
| 778 |
(lambda (cart) |
| 779 |
(if (string-equal "string" (typeof (car cart))) |
| 780 |
(format ofd " &%s_%s_%s_cart,\n" name prefix |
| 781 |
(cg_normal_phone_name (car cart)))) |
| 782 |
) |
| 783 |
carts) |
| 784 |
(format ofd " 0 };\n") |
| 785 |
|
| 786 |
(fclose ofd) |
| 787 |
(fclose ofdh) |
| 788 |
|
| 789 |
) |
| 790 |
) |
| 791 |
|
| 792 |
(define (cg_normal_phone_name x) |
| 793 |
(cg_normal_phone_name_base |
| 794 |
(cg_normal_phone_name_base |
| 795 |
(cg_normal_phone_name_base x)))) |
| 796 |
|
| 797 |
(define (cg_normal_phone_name_base x) |
| 798 |
;; Some phone names aren't valid C labels |
| 799 |
(cond |
| 800 |
((string-matches x ".*@.*" x) |
| 801 |
(intern |
| 802 |
(string-append |
| 803 |
(string-before x "@") |
| 804 |
"atsign" |
| 805 |
(string-after x "@")))) |
| 806 |
((string-matches x ".*:.*") |
| 807 |
(intern |
| 808 |
(string-append |
| 809 |
(string-before x ":") |
| 810 |
"sc" |
| 811 |
(string-after x ":")))) |
| 812 |
((string-matches x ".*=.*") |
| 813 |
(intern |
| 814 |
(string-append |
| 815 |
(string-before x "=") |
| 816 |
"eq" |
| 817 |
(string-after x "=")))) |
| 818 |
((string-matches x ".*>.*") |
| 819 |
(intern |
| 820 |
(string-append |
| 821 |
(string-before x ">") |
| 822 |
"gt" |
| 823 |
(string-after x ">")))) |
| 824 |
((string-matches x ".*}.*") |
| 825 |
(intern |
| 826 |
(string-append |
| 827 |
(string-before x "}") |
| 828 |
"rb" |
| 829 |
(string-after x "}")))) |
| 830 |
((string-matches x ".*].*") |
| 831 |
(intern |
| 832 |
(string-append |
| 833 |
(string-before x "]") |
| 834 |
"rbk" |
| 835 |
(string-after x "]")))) |
| 836 |
((string-matches x ".*-.*") |
| 837 |
(intern |
| 838 |
(string-append |
| 839 |
(string-before x "-") |
| 840 |
"hyp" |
| 841 |
(string-after x "-")))) |
| 842 |
((string-matches x ".*\\*.*") |
| 843 |
(intern |
| 844 |
(string-append |
| 845 |
(string-before x "*") |
| 846 |
"star" |
| 847 |
(string-after x "*")))) |
| 848 |
((string-matches x ".*\\^.*") |
| 849 |
(intern |
| 850 |
(string-append |
| 851 |
(string-before x "^") |
| 852 |
"caret" |
| 853 |
(string-after x "^")))) |
| 854 |
((string-matches x ".*~.*") |
| 855 |
(intern |
| 856 |
(string-append |
| 857 |
(string-before x "~") |
| 858 |
"tilde" |
| 859 |
(string-after x "~")))) |
| 860 |
(t x))) |
| 861 |
|
| 862 |
(provide 'make_cg) |