make_clunits.scm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;; ;;;
  3. ;;; Language Technologies Institute ;;;
  4. ;;; Carnegie Mellon University ;;;
  5. ;;; Copyright (c) 2000 ;;;
  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 (awb@cs.cmu.edu) ;;;
  34. ;;; Date: April 2001 ;;;
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;; ;;;
  37. ;;; Convert festvox voice to flite ;;;
  38. ;;; ;;;
  39. ;;; clunits: catalogue, carts and param ;;;
  40. ;;; ;;;
  41. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  42. (defvar lpc_min -2.709040)
  43. (defvar lpc_max 2.328840)
  44. (defvar mcep_min -5.404620)
  45. (defvar mcep_max 4.540220)
  46. (defvar page_size 500) ;; number of frames per page
  47. ;;(set! page_size 1000000) ;; 1^6 means we do mmap
  48. (define (clunits_convert name clcatfnfileordered clcatfnunitordered
  49. cltreesfn festvoxdir odir)
  50. "(clunits_convert name clcatfn clcatfnordered cltreesfn festvoxdir odir)
  51. Convert a festvox clunits (processed) voice into a C file."
  52. (clunitstoC clcatfnfileordered clcatfnunitordered name
  53. (path-append festvoxdir "sts")
  54. (path-append festvoxdir "mcep")
  55. odir)
  56. (set! ofd (fopen (path-append odir (string-append name "_clunits.c")) "a"))
  57. (clunits_make_carts cltreesfn clcatfnunitordered name odir ofd)
  58. (format ofd "\n\n")
  59. (format ofd "static const int %s_join_weights[] = \n" name)
  60. (format ofd " { 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768,\n")
  61. (format ofd " 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768 };\n\n")
  62. (format ofd "extern const cst_cart * const %s_carts[];\n" name )
  63. (format ofd "extern cst_sts_list %s_sts, %s_mcep;\n\n" name name )
  64. (format ofd "cst_clunit_db %s_db = {\n" name)
  65. (format ofd " \"%s\",\n\n" name)
  66. (format ofd " %s_unit_types,\n" name)
  67. (format ofd " %s_carts,\n" name)
  68. (format ofd " %s_units,\n\n" name)
  69. (format ofd " %s_num_unit_types,\n" name)
  70. (format ofd " %s_num_units,\n\n" name)
  71. (format ofd " &%s_sts,\n\n" name)
  72. (format ofd " &%s_mcep,\n\n" name)
  73. (format ofd " %s_join_weights,\n\n" name)
  74. (format ofd " 1, /* optimal coupling */\n")
  75. (format ofd " 5, /* extend selections */\n")
  76. (format ofd " 100, /* f0 weight */\n")
  77. (format ofd " 0 /* unit_name function */\n")
  78. (format ofd "};\n")
  79. (fclose ofd)
  80. ;; Duration model
  81. (clunits_convert_durmodel
  82. (format nil "%s/festvox/%s_durdata.scm" festvoxdir name)
  83. name odir)
  84. )
  85. (define (unit_type u)
  86. (apply
  87. string-append
  88. (reverse
  89. (symbolexplode
  90. (string-after
  91. (apply
  92. string-append
  93. (reverse (symbolexplode u)))
  94. "_")))))
  95. (define (unit_occur u)
  96. (apply
  97. string-append
  98. (reverse
  99. (symbolexplode
  100. (string-before
  101. (apply
  102. string-append
  103. (reverse (symbolexplode u)))
  104. "_")))))
  105. (define (sort_clentries entries clcatfnunitorder)
  106. (let ((neworder nil) (unittype nil) (q nil))
  107. (mapcar
  108. (lambda (unit)
  109. (set! unittype (string-before (car unit) "_"))
  110. (set! q (assoc_string unittype entries))
  111. ;; only keep this unit if there is an actually tree --
  112. ;; in build3 (or other methods) it might have been pruned
  113. (if q
  114. (begin
  115. (set! nnentry (assoc_string
  116. (car unit)
  117. (cdr (assoc_string unittype entries))))
  118. ; (format t "%s %l %l\n" unittype nnentry (assoc_string unittype entries))
  119. (set! neworder
  120. (cons
  121. nnentry
  122. neworder)))))
  123. (load clcatfnunitorder t))
  124. (format t "new order %d\n" (length neworder))
  125. (reverse neworder))
  126. )
  127. (define (clunitstoC clcatfnfileordered clcatfnunitordered
  128. name stsdir mcepdir odir)
  129. "(clunitstoC clcatfnfileordered clcatfnunitordered name mcepdir stsdir odir)
  130. Convert a clunits catalogue and its sts representations into a
  131. compilable single C file."
  132. (let
  133. ((clindex (load clcatfnfileordered t))
  134. (lofdidx (fopen (path-append odir (string-append name "_lpc.c")) "w"))
  135. (lofdh (fopen (path-append odir (string-append name "_lpc.h")) "w"))
  136. (cofdidx (fopen (path-append odir (string-append name "_clunits.c")) "w"))
  137. (cofdh (fopen (path-append odir (string-append name "_clunits.h")) "w"))
  138. (mofdidx (fopen (path-append odir (string-append name "_mcep.c")) "w"))
  139. (mofdh (fopen (path-append odir (string-append name "_mcep.h")) "w")))
  140. (format lofdidx "/*****************************************************/\n")
  141. (format lofdidx "/** Autogenerated Clunits index for %s */\n" name)
  142. (format lofdidx "/*****************************************************/\n")
  143. (format lofdidx "\n")
  144. (format lofdidx "#include \"cst_clunits.h\"\n")
  145. (format lofdidx "#include \"%s_lpc.h\"\n" name)
  146. (format mofdidx "/*****************************************************/\n")
  147. (format mofdidx "/** Autogenerated Clunits index for %s */\n" name)
  148. (format mofdidx "/*****************************************************/\n")
  149. (format mofdidx "\n")
  150. (format mofdidx "#include \"%s_mcep.h\"\n" name)
  151. (format mofdidx "#include \"cst_clunits.h\"\n")
  152. (set! pm_pos 0)
  153. (set! sample_pos 0)
  154. (set! times nil)
  155. ; (set! clunits_entries nil)
  156. (set! t_entries nil)
  157. (set! done_files nil)
  158. (set! num_unit_entries (length clindex))
  159. (set! residual_sizes nil)
  160. (set! lpc_info nil)
  161. (set! mcep_info nil)
  162. (set! n page_size)
  163. (set! f 0)
  164. (while clindex
  165. (if (equal? n page_size)
  166. (begin
  167. (if (> f 0)
  168. (begin
  169. (format lofdbitlpc " 0\n};\n\n")
  170. (fclose lofdbitlpc)
  171. (format lofdbitres " 0\n};\n\n")
  172. (fclose lofdbitres)
  173. (format mofdbitmcep " 0\n};\n\n")
  174. (fclose mofdbitmcep)
  175. ))
  176. (set! lofdbitlpc
  177. (if (equal? page_size 1000000)
  178. (fopen (format nil "%s/%s_lpcall.c" odir name) "w")
  179. (fopen (format nil "%s/%s_lpc_%03d.c" odir name f) "w")))
  180. (format lofdbitlpc
  181. "const unsigned short %s_sts_lpc_page_%d[] = { \n"
  182. name f )
  183. (format lofdh "extern const unsigned short %s_sts_lpc_page_%d[];\n"
  184. name f )
  185. (set! lofdbitres
  186. (if (equal? page_size 1000000)
  187. (fopen (format nil "%s/%s_resall.c" odir name) "w")
  188. (fopen (format nil "%s/%s_res_%03d.c" odir name f) "w")))
  189. (format lofdbitres
  190. "const unsigned char %s_sts_res_page_%d[] = { \n"
  191. name f )
  192. (format lofdh "extern const unsigned char %s_sts_res_page_%d[];\n"
  193. name f )
  194. (set! mofdbitmcep
  195. (if (equal? page_size 1000000)
  196. (fopen (format nil "%s/%s_mcepall.c" odir name) "w")
  197. (fopen (format nil "%s/%s_mcep_%03d.c" odir name f) "w")))
  198. (format mofdbitmcep
  199. "const unsigned short %s_sts_mcep_page_%d[] = {\n"
  200. name f )
  201. (format mofdh "extern const unsigned short %s_sts_mcep_page_%d[];\n"
  202. name f )
  203. (set! n 0)
  204. (set! lpc_page f)
  205. (set! mcep_page f)
  206. (set! lpc_page_pos 0)
  207. (set! res_page_pos 0)
  208. (set! mcep_page_pos 0)
  209. (set! f (+ 1 f))))
  210. (set! n (+ 1 n))
  211. (set! pms (find_pm_pos
  212. name
  213. (car clindex)
  214. stsdir
  215. mcepdir
  216. lofdbitlpc
  217. lofdbitres
  218. mofdbitmcep
  219. ))
  220. ;; Output unit_entry for this unit
  221. (set! entry
  222. (list
  223. (nth 0 (car clindex))
  224. (nth 2 pms) ; start_pm
  225. (nth 3 pms) ; phone_boundary_pm
  226. (nth 4 pms) ; end_pm
  227. (nth 5 (car clindex))
  228. (nth 6 (car clindex))
  229. ))
  230. (set! q_entries (assoc_string (string-before (car entry) "_") t_entries))
  231. (if q_entries
  232. (set-cdr! q_entries (cons entry (cdr q_entries)))
  233. (set! t_entries
  234. (cons
  235. (list
  236. (string-before (car entry) "_")
  237. entry)
  238. t_entries)))
  239. ; (set! clunits_entries
  240. ; (cons
  241. ; (list
  242. ; (nth 0 (car clindex))
  243. ; (nth 2 pms) ; start_pm
  244. ; (nth 3 pms) ; phone_boundary_pm
  245. ; (nth 4 pms) ; end_pm
  246. ; (nth 5 (car clindex))
  247. ; (nth 6 (car clindex))
  248. ; )
  249. ; clunits_entries))
  250. (set! clindex (cdr clindex)))
  251. (format t "Sorting cl entries\n")
  252. ; (format t "%l\n" (caar t_entries))
  253. ; (format t "%l\n" (car t_entries))
  254. (set! clunits_entries
  255. (sort_clentries t_entries clcatfnunitordered))
  256. (format t "End Sorting cl entries %d %d\n"
  257. (length clunits_entries)
  258. (length clcatfnunitordered)
  259. )
  260. (format lofdidx "\n\n")
  261. (format mofdidx "\n\n")
  262. (begin ;; Close of last pages
  263. (format lofdbitlpc " 0\n};\n\n")
  264. (fclose lofdbitlpc)
  265. (format lofdbitres " 0\n};\n\n")
  266. (fclose lofdbitres)
  267. (format mofdbitmcep " 0\n};\n\n")
  268. (fclose mofdbitmcep)
  269. )
  270. (if (equal? page_size 1000000)
  271. (begin ;; mmap
  272. (set! resofdidx (fopen (path-append odir (string-append name "_residx.c")) "w"))
  273. (set! i 0)
  274. (format resofdidx "const unsigned int %s_sts_residx[] = { \n" name)
  275. (mapcar
  276. (lambda (info)
  277. (format
  278. resofdidx " %d,\n" (nth 3 info))
  279. (set! i (+ 1 i)))
  280. (reverse lpc_info))
  281. (format resofdidx " 0\n};\n\n")
  282. (fclose resofdidx)
  283. (set! resofdidx (fopen (path-append odir (string-append name "_ressize.c")) "w"))
  284. (format resofdidx "const unsigned char %s_sts_ressize[] = { \n" name)
  285. (mapcar
  286. (lambda (info)
  287. (format
  288. resofdidx " %d,\n" (nth 2 info)))
  289. (reverse lpc_info))
  290. (format resofdidx " 0\n};\n\n")
  291. (fclose resofdidx)
  292. ; (format lofdh "extern const unsigned int %s_sts_residx[];\n" name)
  293. ; (format lofdh "extern const unsigned char %s_sts_ressize[];\n" name)
  294. )
  295. (begin ;; normal paging
  296. (format lofdidx "const cst_sts_paged %s_sts_paged_vals[] = { \n" name)
  297. (set! i 0)
  298. (mapcar
  299. (lambda (info)
  300. (format
  301. lofdidx " {%d,%d,%d,%s_sts_lpc_page_%d,%s_sts_res_page_%d},\n"
  302. (nth 1 info) (nth 2 info) (nth 3 info)
  303. name (nth 0 info)
  304. name (nth 0 info))
  305. (set! i (+ 1 i)))
  306. (reverse lpc_info))
  307. (format lofdidx " { 0, 0, 0, 0, 0 }};\n\n")
  308. ))
  309. (format lofdidx "cst_sts_list %s_sts = {\n" name)
  310. (format lofdidx " NULL, ")
  311. (if (equal? page_size 1000000)
  312. (begin
  313. (format lofdidx " NULL,\n")
  314. ; (format lofdidx " %s_sts_lpc_page_0,\n" name)
  315. ; (format lofdidx " %s_sts_res_page_0,\n" name)
  316. ; (format lofdidx " %s_sts_residx,\n" name)
  317. ; (format lofdidx " %s_sts_ressize,\n" name)
  318. (format lofdidx " NULL, NULL, NULL, NULL,\n")
  319. )
  320. (begin
  321. (format lofdidx " %s_sts_paged_vals,\n" name)
  322. (format lofdidx " NULL, NULL, NULL, NULL,\n")))
  323. (format lofdidx " %d, /* number of frames */ \n" i)
  324. (format lofdidx " %d, /* lpc order */ \n" lpc_order)
  325. (format lofdidx " %d, /* sample rate */ \n" sample_rate)
  326. (format lofdidx " %f, /* lpc min */ \n" lpc_min)
  327. (format lofdidx " %f, /* lpc range */ \n" lpc_range)
  328. (if (probe_file "flite/codec")
  329. (set! codec (car (load "flite/codec" t)))
  330. (set! codec "ulaw"))
  331. (format lofdidx " \"%s\" /* residual codec */ \n" codec)
  332. (format lofdidx "};\n\n")
  333. (if (not (equal? page_size 1000000))
  334. (begin ;; mmap
  335. (format mofdidx "const cst_sts_paged %s_mcep_paged_vals[] = { \n" name)
  336. (set! i 0)
  337. (mapcar
  338. (lambda (info)
  339. (format mofdidx " { %d, 0, 0, %s_sts_mcep_page_%d, 0 }, \n"
  340. (nth 1 info)
  341. name (nth 0 info))
  342. (set! i (+ 1 i)))
  343. (reverse mcep_info))
  344. (format mofdidx " { 0, 0, 0 }};\n\n")
  345. ))
  346. (format mofdidx "cst_sts_list %s_mcep = {\n" name)
  347. (format mofdidx " NULL, ")
  348. (if (equal? page_size 1000000)
  349. (begin
  350. (format mofdidx " NULL,\n")
  351. ; (format mofdidx " %s_sts_mcep_page_0,\n" name)
  352. ; (format mofdidx " NULL,NULL,NULL,\n")
  353. (format mofdidx " NULL, NULL, NULL, NULL,\n")
  354. )
  355. (begin
  356. (format mofdidx " %s_mcep_paged_vals,\n" name)
  357. (format mofdidx " NULL, NULL, NULL, NULL,\n")))
  358. (format mofdidx " %d, /* number of frames */ \n" i)
  359. (format mofdidx " %d, /* mcep order */ \n" mcep_order)
  360. (format mofdidx " %d, /* sample rate */ \n" sample_rate)
  361. (format mofdidx " %f, /* mcep min */ \n" mcep_min)
  362. (format mofdidx " %f, /* mcep range */ \n" (- mcep_max mcep_min))
  363. (format mofdidx " NULL /* residual codec */ \n")
  364. (format mofdidx "};\n\n")
  365. (format cofdidx "/*****************************************************/\n")
  366. (format cofdidx "/** Autogenerated Clunits index for %s */\n" name)
  367. (format cofdidx "/*****************************************************/\n")
  368. (format cofdidx "\n")
  369. (format cofdidx "#include \"cst_clunits.h\"\n")
  370. (format cofdidx "#include \"%s_clunits.h\"\n" name)
  371. (format cofdidx "#include \"%s_cltrees.h\"\n" name)
  372. (format cofdidx "\n\n")
  373. (set! unitbase_count 0)
  374. (set! unitbases nil)
  375. (mapcar
  376. (lambda (p)
  377. (if (and (not (string-matches (car p) ".*#.*"))
  378. (not (member_string (string-before (car p) "_") unitbases)))
  379. (begin
  380. (format cofdidx "#define unitbase_%s %d\n"
  381. (clunits_normal_phone_name
  382. (string-before (car p) "_"))
  383. unitbase_count)
  384. (set! unitbases (cons (string-before (car p) "_") unitbases))
  385. (set! unitbase_count (+ 1 unitbase_count)))))
  386. clunits_entries)
  387. (format cofdidx "\n\n")
  388. (format cofdidx "const cst_clunit %s_units[] = { \n" name)
  389. (set! num_entries 0)
  390. (set! this_ut "")
  391. (set! this_ut_count 0)
  392. (set! unit_name_to_idx nil)
  393. (set! all_unit_name_to_idx nil)
  394. (mapcar
  395. (lambda (e)
  396. (if (not (string-equal this_ut (unit_type (nth 0 e))))
  397. (begin
  398. (if (> this_ut_count 0)
  399. (format cofdh "#define unit_%s_num %d\n"
  400. (clunits_normal_phone_name this_ut)
  401. this_ut_count))
  402. (format cofdh "#define unit_%s_start %d\n"
  403. (clunits_normal_phone_name (unit_type (nth 0 e)))
  404. num_entries)
  405. (if unit_name_to_idx
  406. (set! all_unit_name_to_idx
  407. (cons
  408. (reverse unit_name_to_idx)
  409. all_unit_name_to_idx)))
  410. (set! unit_name_to_idx (list (unit_type (nth 0 e))))
  411. (set! current_unit_start num_entries)
  412. (set! this_ut (unit_type (nth 0 e)))
  413. (set! this_ut_count 0)
  414. ))
  415. (format cofdh "#define unit_%s %d\n"
  416. (clunits_normal_phone_name (nth 0 e)) num_entries)
  417. ;; need to preserver name to idx number as some units might pruned
  418. (set! unit_name_to_idx
  419. (cons
  420. (list (unit_occur (nth 0 e)) (- num_entries current_unit_start))
  421. unit_name_to_idx))
  422. (set! num_entries (+ 1 num_entries))
  423. (set! this_ut_count (+ 1 this_ut_count))
  424. (format cofdidx " { /* %s */ unit_type_%s, unitbase_%s, %d,%d, %s, %s },\n"
  425. (nth 0 e)
  426. (clunits_normal_phone_name (unit_type (nth 0 e)))
  427. (clunits_normal_phone_name (string-before (nth 0 e) "_"))
  428. (nth 1 e) ; start_pm
  429. (nth 3 e) ; end_pm
  430. (clunits_normal_phone_name (nth 4 e)) ; prev
  431. (clunits_normal_phone_name (nth 5 e)) ; next
  432. ))
  433. clunits_entries)
  434. (if unit_name_to_idx
  435. (set! all_unit_name_to_idx
  436. (cons
  437. (reverse unit_name_to_idx)
  438. all_unit_name_to_idx)))
  439. (format cofdidx " { 0,0,0,0 } };\n\n")
  440. (format cofdidx "#define %s_num_units %d\n" name num_entries)
  441. (if (> this_ut_count 0)
  442. (format cofdh "#define unit_%s_num %d\n"
  443. this_ut this_ut_count))
  444. (fclose lofdh)
  445. (fclose lofdidx)
  446. (fclose mofdh)
  447. (fclose mofdidx)
  448. (fclose cofdh)
  449. (fclose cofdidx)
  450. ))
  451. (defvar sts_coeffs_fname nil)
  452. (define (find_pm_pos name entry stsdir mcepdir lofdlpc lofdres mofdmcep)
  453. "(find_pm_pos entry lpddir)
  454. Diphone dics give times in seconds here we want them as indexes. This
  455. function converts the lpc to ascii and finds the pitch marks that
  456. go with this unit. These are written to ofdsts with ulaw residual
  457. as short term signal."
  458. (let ((start_time (nth 2 entry))
  459. (phoneboundary_time (nth 3 entry))
  460. (end_time (nth 4 entry))
  461. start_pm pb_pm end_pm
  462. (ltime 0))
  463. (format t "%l\n" entry)
  464. (if (not (string-equal (cadr entry) sts_coeffs_fname))
  465. (begin
  466. ;; Only load when when we have a new filename
  467. (set! sts_coeffs
  468. (load (format nil "%s/%s.sts" stsdir (cadr entry)) t))
  469. (set! mcep_coeffs
  470. (load_ascii_track
  471. (format nil "%s/%s.mcep" mcepdir (cadr entry))
  472. (nth 2 entry) ;; from this start time
  473. ))
  474. (set! sts_info (car sts_coeffs))
  475. (set! sts_coeffs (cdr sts_coeffs))
  476. (set! sts_coeffs_fname (cadr entry))
  477. ))
  478. (set! ltime 0)
  479. (set! size_to_now 0)
  480. ;; Flip through the sts's and mceps to find the right one --
  481. (while (and sts_coeffs (cdr sts_coeffs)
  482. (> (absdiff start_time (car (car sts_coeffs)))
  483. (absdiff start_time (car (cadr sts_coeffs)))))
  484. (set! ltime (car (car sts_coeffs)))
  485. (set! mcep_coeffs (cdr mcep_coeffs))
  486. (set! sts_coeffs (cdr sts_coeffs)))
  487. (set! sample_rate (nth 2 sts_info))
  488. (set! lpc_order (nth 1 sts_info))
  489. (set! lpc_min (nth 3 sts_info))
  490. (set! lpc_range (nth 4 sts_info))
  491. (set! start_pm pm_pos)
  492. (while (and sts_coeffs (cdr sts_coeffs)
  493. mcep_coeffs (cdr mcep_coeffs)
  494. (> (absdiff phoneboundary_time (car (car sts_coeffs)))
  495. (absdiff phoneboundary_time (car (cadr sts_coeffs)))))
  496. (output_mcep name (car mcep_coeffs) mofdmcep)
  497. (output_sts name (car sts_coeffs) (nth 1 entry) lofdlpc lofdres)
  498. (set! mcep_coeffs (cdr mcep_coeffs))
  499. (set! sts_coeffs (cdr sts_coeffs)))
  500. (set! pb_pm pm_pos)
  501. (while (and sts_coeffs (cdr sts_coeffs)
  502. mcep_coeffs (cdr mcep_coeffs)
  503. (> (absdiff end_time (car (car sts_coeffs)))
  504. (absdiff end_time (car (cadr sts_coeffs)))))
  505. (output_mcep name (car mcep_coeffs) mofdmcep)
  506. (output_sts name (car sts_coeffs) (nth 1 entry) lofdlpc lofdres)
  507. (set! mcep_coeffs (cdr mcep_coeffs))
  508. (set! sts_coeffs (cdr sts_coeffs)))
  509. (set! end_pm pm_pos)
  510. (list
  511. (car entry)
  512. (cadr entry)
  513. start_pm
  514. pb_pm
  515. end_pm)))
  516. (define (output_sts name frame fname ofdlpc ofdres)
  517. "(output_sts frame residual ofd)
  518. Ouput this LPC frame."
  519. (let ((time (nth 0 frame))
  520. (coeffs (nth 1 frame))
  521. (size (nth 2 frame))
  522. (r (nth 3 frame)))
  523. (set! times (cons time times))
  524. (set! l_n lpc_page_pos)
  525. (while coeffs
  526. (format ofdlpc " %d," (car coeffs))
  527. (set! coeffs (cdr coeffs)))
  528. (format ofdlpc "\n")
  529. (set! lpc_page_pos (+ 1 lpc_page_pos)) ;; we know the frame size
  530. (set! r_n res_page_pos)
  531. (while r
  532. (format ofdres " %d," (car r))
  533. (set! res_page_pos (+ 1 res_page_pos))
  534. (set! r (cdr r)))
  535. (format ofdres "\n")
  536. (set! lpc_info
  537. (cons
  538. (list lpc_page l_n size r_n)
  539. lpc_info))
  540. (set! pm_pos (+ 1 pm_pos))
  541. ))
  542. (define (lpccoeff_norm c)
  543. (* (/ (- c lpc_min) (- lpc_max lpc_min))
  544. 65535))
  545. (define (mcepcoeff_norm c)
  546. (* (/ (- c mcep_min) (- mcep_max mcep_min))
  547. 65535))
  548. (define (output_mcep name frame ofd)
  549. "(output_mcep frame duration residual ofd)
  550. Ouput this MCEP frame."
  551. (let ((i 0))
  552. (set! mcep_order (- (length frame) (+ 3 1)))
  553. (set! frame (cddr frame)) ;; skip time and the "1"
  554. (set! frame (cdr frame)) ;; skip the energy
  555. (set! m_n mcep_page_pos)
  556. (set! i 0)
  557. ; (format ofd " %d," (mcepcoeff_norm (/ (car frame) 2)))
  558. ; (set! frame (cdr frame))
  559. (while (< i mcep_order)
  560. (format ofd " %d," (mcepcoeff_norm (car frame)))
  561. (set! frame (cdr frame))
  562. (set! i (+ 1 i))
  563. )
  564. (format ofd "\n")
  565. (set! mcep_page_pos (+ 1 mcep_page_pos))
  566. (set! mcep_info
  567. (cons
  568. (list mcep_page m_n)
  569. mcep_info))
  570. ))
  571. (define (load_ascii_track trackfilename starttime)
  572. "(load_ascii_track trackfilename)
  573. Coverts trackfilename to simple ascii representation."
  574. (let ((tmpfile (make_tmp_filename))
  575. (nicestarttime (if (> starttime 0.100)
  576. (- starttime 0.100)
  577. starttime))
  578. b)
  579. (system (format nil "$ESTDIR/bin/ch_track -otype est -start %f %s |
  580. awk '{if ($1 == \"EST_Header_End\")
  581. header=1;
  582. else if (header == 1)
  583. printf(\"( %%s )\\n\",$0)}'>%s"
  584. nicestarttime trackfilename tmpfile))
  585. (set! b (load tmpfile t))
  586. (delete-file tmpfile)
  587. b))
  588. (define (absdiff a b)
  589. (let ((d (- a b )))
  590. (if (< d 0)
  591. (* -1 d)
  592. d)))
  593. (define (carttoC_extract_answer_list ofdh tree)
  594. "(carttoC_extract_answer_list tree)
  595. Get list of answers from leaf node."
  596. ; (carttoC_val_table ofdh
  597. ; (car (last (car tree)))
  598. ; 'none)
  599. ; (format t "%l\n" (car tree))
  600. ;; These should be sorted first (which may or may not make a difference)
  601. (cellstovals "cl" (mapcar car (caar tree)) ofdh)
  602. ; (cellstovals
  603. ; "cl"
  604. ; (mapcar
  605. ; (lambda (x)
  606. ; (set! xxx (cadr (assoc_string (car x) (cdr current_unit_name_to_idx))))
  607. ; )
  608. ; (clunits_sort_candidates (caar tree)))
  609. ; ofdh)
  610. (format nil "cl_%04d" cells_count))
  611. (define (sorted_cand_add cand s)
  612. (cond
  613. ((null s) (list cand))
  614. ((< (cadr cand) (cadr (car s)))
  615. (cons cand s))
  616. (t
  617. (cons (car s)
  618. (sorted_cand_add cand (cdr s))))))
  619. (define (clunits_sort_candidates cands)
  620. (let ((s nil))
  621. (mapcar
  622. (lambda (x)
  623. (set! s (sorted_cand_add x s))
  624. )
  625. cands)
  626. s
  627. )
  628. )
  629. (define (sort_cltrees trees clcatfn)
  630. (let ((neworder nil) (ut nil))
  631. (mapcar
  632. (lambda (unit)
  633. (set! ut (unit_type (car unit)))
  634. (if (not (assoc_string ut neworder))
  635. (set! neworder (cons (assoc_string ut trees) neworder))))
  636. (load clcatfn t))
  637. (reverse neworder)))
  638. (define (clunits_make_carts cartfn clcatfn name odir cofd)
  639. "(define clunits_make_carts cartfn name)
  640. Output clunit selection carts into odir/name_carts.c"
  641. (let (ofd ofdh)
  642. ;; Set up to dump full list of things at leafs
  643. (set! carttoC_extract_answer carttoC_extract_answer_list)
  644. (load cartfn)
  645. (set! ofd (fopen (format nil "%s/%s_cltrees.c" odir name) "w"))
  646. (set! ofdh (fopen (format nil "%s/%s_cltrees.h" odir name) "w"))
  647. (format ofd "\n")
  648. (format ofd "#include \"cst_string.h\"\n")
  649. (format ofd "#include \"cst_cart.h\"\n")
  650. (format ofd "#include \"cst_regex.h\"\n")
  651. (format ofd "#include \"%s_cltrees.h\"\n" name)
  652. (set! val_table nil)
  653. (format t "new order3 %d\n" (length clunits_selection_trees))
  654. (set! clunits_selection_trees (sort_cltrees clunits_selection_trees clcatfn))
  655. (format t "new order4 %d\n" (length clunits_selection_trees))
  656. (mapcar
  657. (lambda (cart)
  658. (set! current_node -1)
  659. (set! feat_nums nil)
  660. ;; The name to idx mapping (in case of pruning)
  661. (set! current_unit_name_to_idx
  662. (assoc_string (car cart) all_unit_name_to_idx))
  663. ; (format t "awb-debug current_unit_name_to_idx %l %l %d\n" (car cart) (car all_unit_name_to_idx) (length all_unit_name_to_idx))
  664. (do_carttoC ofd ofdh
  665. (format nil "%s_%s" name
  666. (clunits_normal_phone_name (car cart)))
  667. (cadr cart)))
  668. clunits_selection_trees)
  669. (format ofd "\n\n")
  670. (format ofd "const cst_cart * const %s_carts[] = {\n" name)
  671. (mapcar
  672. (lambda (cart)
  673. (format ofd " &%s_%s_cart,\n" name
  674. (clunits_normal_phone_name (car cart)))
  675. )
  676. clunits_selection_trees)
  677. (format ofd " 0 };\n")
  678. (format cofd "\n\n")
  679. (format cofd "#define %s_num_unit_types %d\n"
  680. name (length clunits_selection_trees))
  681. (format cofd "\n\n")
  682. (format cofd "const cst_clunit_type %s_unit_types[] = {\n" name)
  683. (set! n 0)
  684. (format t "new order2 %d\n" (length clunits_selection_trees))
  685. (mapcar
  686. (lambda (cart)
  687. (format ofdh "#define unit_type_%s %d\n"
  688. (clunits_normal_phone_name (car cart)) n)
  689. (format cofd " { \"%s\", unit_%s_start, unit_%s_num},\n"
  690. (car cart)
  691. (clunits_normal_phone_name (car cart) )
  692. (clunits_normal_phone_name (car cart)) )
  693. (set! n (+ 1 n))
  694. )
  695. clunits_selection_trees)
  696. (format cofd " { NULL, CLUNIT_NONE, CLUNIT_NONE } };\n")
  697. (fclose ofd)
  698. (fclose ofdh)
  699. )
  700. )
  701. (define (clunits_convert_durmodel durmodelfn name odir)
  702. (set! durmodel (load durmodelfn t))
  703. (set! phonedurs (cdr (cadr (car (cddr (car durmodel))))))
  704. (set! zdurtree (cadr (car (cddr (cadr durmodel)))))
  705. (set! carttoC_extract_answer basic_carttoC_extract_answer)
  706. (set! dfd (fopen (path-append odir (string-append name "_cl_durmodel.c")) "w"))
  707. (set! dfdh (fopen (path-append odir (string-append name "_cl_durmodel.h")) "w"))
  708. (format dfd "/*****************************************************/\n")
  709. (format dfd "/** Autogenerated durmodel_cl for %s */\n" name)
  710. (format dfd "/*****************************************************/\n")
  711. (format dfd "#include \"cst_synth.h\"\n")
  712. (format dfd "#include \"cst_string.h\"\n")
  713. (format dfd "#include \"cst_cart.h\"\n")
  714. (format dfd "#include \"%s_cl_durmodel.h\"\n\n" name)
  715. (mapcar
  716. (lambda (s)
  717. (format dfd "static const dur_stat dur_state_%s = { \"%s\", %f, %f };\n"
  718. (clunits_normal_phone_name (car s))
  719. (car s) (car (cdr s)) (car (cddr s)))
  720. )
  721. phonedurs)
  722. (format dfd "\n")
  723. (format dfd "const dur_stat * const %s_dur_stats[] = {\n" name)
  724. (mapcar
  725. (lambda (s)
  726. (format dfd " &dur_state_%s,\n" (clunits_normal_phone_name (car s))))
  727. phonedurs)
  728. (format dfd " NULL\n};\n")
  729. (set! val_table nil)
  730. (set! current_node -1)
  731. (set! feat_nums nil)
  732. (do_carttoC dfd dfdh
  733. (format nil "%s_%s" name "dur")
  734. zdurtree)
  735. (fclose dfd)
  736. (fclose dfdh)
  737. )
  738. (define (clunits_normal_phone_name x)
  739. ;; Some phonenames aren't valid C labels
  740. (cond
  741. ((string-matches x ".*@.*" x)
  742. (intern
  743. (string-append
  744. (string-before x "@")
  745. "atsign"
  746. (string-after x "@"))))
  747. ((string-matches x ".*#.*" x)
  748. (intern
  749. (string-append
  750. (string-before x "#")
  751. "hash"
  752. (string-after x "#"))))
  753. ((string-matches x ".*:.*")
  754. (intern
  755. (string-append
  756. (string-before x ":")
  757. "sc"
  758. (string-after x ":"))))
  759. (t x)))
  760. ;;;
  761. ;;; Usage count prune
  762. ;;;
  763. (define (clunits_prune_tree tree units_used)
  764. (let ((treey nil) (treen nil))
  765. (cond
  766. ((cdr tree) ;; question
  767. (format t "question %l\n" (car tree))
  768. (set! treey (clunits_prune_tree (car (cdr tree)) units_used))
  769. (set! treen (clunits_prune_tree (car (cddr tree)) units_used))
  770. (cond
  771. ((null treen)
  772. (format t "pruned whole cluster for n %l\n" (car tree))
  773. treey)
  774. ((null treey) ;; nothing left
  775. (format t "pruned whole cluster for y %l\n" (car tree))
  776. treen) ;; could be nil too, but higher level deals with it
  777. (t
  778. (list (car tree) treey treen))))
  779. (t ;; leaf
  780. (let ((nn nil))
  781. (mapcar
  782. (lambda (c)
  783. ; (format t "checking %s in %l\n" (car c) units_used)
  784. (if (member_string (car c) units_used)
  785. (set! nn (cons c nn))
  786. (format t "pruning %s\n" (car c))
  787. ))
  788. (caar tree))
  789. (if nn
  790. (list (list (reverse nn) (cadr (car tree))))
  791. nil))))
  792. ))
  793. (define (clunits_prune treesfile units_used_file otreefile)
  794. (load treesfile)
  795. (set! units_used (load units_used_file t))
  796. (set! new_trees
  797. (mapcar
  798. (lambda (tree)
  799. (format t "tree print %s\n" (car tree))
  800. (list
  801. (car tree)
  802. (clunits_prune_tree
  803. (cadr tree)
  804. (cdr (assoc_string (car tree) units_used)))))
  805. clunits_selection_trees))
  806. (set! ofd (fopen otreefile "w"))
  807. (format ofd "(set! clunits_selection_trees '(\n")
  808. (mapcar
  809. (lambda (x) ;; this is faster
  810. (pprintf x ofd))
  811. new_trees)
  812. (format ofd "))\n")
  813. (fclose ofd)
  814. )
  815. (provide 'make_clunits)