make_didb2.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;; ;;;
  3. ;;; Language Technologies Institute ;;;
  4. ;;; Carnegie Mellon University ;;;
  5. ;;; Copyright (c) 2001-2005 ;;;
  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: January 2005 ;;;
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;; ;;;
  37. ;;; Take a diphone index and generate a single C track and residual ;;;
  38. ;;; for each of diphones in given index. A new index with pm ;;;
  39. ;;; positions is provided. ;;;
  40. ;;; ;;;
  41. ;;; Strategy 2: Dump into individual files ;;;
  42. ;;; ;;;
  43. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  44. (defvar lpc_min -7.992630)
  45. (defvar lpc_max 7.829990)
  46. (define (diphtoC diphindexfn name lpcdir odir)
  47. "(diphtoC diphindexfn name lpcdir odir)
  48. Convert a diphone index and its LPC representations into three files,
  49. an single track file with LPC coefficients, a wave file with
  50. the residual, and an index file relating diph names to positions in
  51. the main files."
  52. (let
  53. ((diphindex (load diphindexfn t))
  54. (ofdidx (fopen (path-append odir (string-append name "_diphone.c")) "w"))
  55. (ofdlpc (fopen (path-append odir (string-append name "_lpc.c")) "w"))
  56. (ofdres (fopen (path-append odir (string-append name "_res.c")) "w"))
  57. (ofdresi (fopen (path-append odir (string-append name "_residx.c")) "w"))
  58. (ofdressize (fopen (path-append odir (string-append name "_ressize.c")) "w")))
  59. (format ofdidx "/*****************************************************/\n")
  60. (format ofdidx "/** Autogenerated Diphone DB for %s */\n" name)
  61. (format ofdidx "/*****************************************************/\n")
  62. (format ofdidx "\n")
  63. (format ofdidx "#include \"cst_diphone.h\"\n")
  64. (format ofdidx "\n")
  65. (format ofdidx "extern const unsigned short %s_lpc[];\n" name)
  66. (format ofdidx "extern const unsigned char %s_res[];\n" name)
  67. (format ofdidx "extern const unsigned int %s_resi[];\n" name)
  68. (format ofdidx "extern const unsigned char %s_ressize[];\n" name)
  69. (format ofdlpc "/*****************************************************/\n")
  70. (format ofdlpc "/** Autogenerated Diphone LPC for %s */\n" name)
  71. (format ofdlpc "/*****************************************************/\n")
  72. (format ofdlpc "\n")
  73. (format ofdlpc "const unsigned short %s_lpc[] = {\n" name)
  74. (format ofdres "/*****************************************************/\n")
  75. (format ofdres "/** Autogenerated Diphone Residual for %s */\n" name)
  76. (format ofdres "/*****************************************************/\n")
  77. (format ofdres "\n")
  78. (format ofdres "const unsigned char %s_res[] = {\n" name)
  79. (format ofdresi "/****************************************************/\n")
  80. (format ofdresi "/** Autogenerated Diphone Residual index for %s */\n" name)
  81. (format ofdresi "/****************************************************/\n")
  82. (format ofdresi "\n")
  83. (format ofdresi "const unsigned int %s_resi[] = {\n" name)
  84. (format ofdressize "/****************************************************/\n")
  85. (format ofdressize "/** Autogenerated Diphone Residual sizes for %s */\n" name)
  86. (format ofdressize "/****************************************************/\n")
  87. (format ofdressize "\n")
  88. (format ofdressize "const unsigned char %s_ressize[] = {\n" name)
  89. (set! pm_pos 0)
  90. (set! res_byte_pos 0)
  91. (set! sample_pos 0)
  92. (set! times nil)
  93. (set! diphone_entries nil)
  94. (set! num_unit_entries (length diphindex))
  95. (while diphindex
  96. (set! pms (find_pm_pos
  97. (car diphindex)
  98. lpcdir
  99. ofdlpc
  100. ofdres
  101. ofdresi
  102. ofdressize
  103. ))
  104. ;; Output unit_entry for this diphone
  105. (set! diphone_entries
  106. (cons
  107. (list
  108. (nth 0 (car diphindex))
  109. (nth 2 pms) ; start_pm
  110. (nth 3 pms) ; phone_boundary_pm
  111. (nth 4 pms)) ; end_pm
  112. diphone_entries))
  113. (set! diphindex (cdr diphindex)))
  114. (format ofdidx "\n\n")
  115. (set! i (length times))
  116. (format ofdidx "cst_sts_list %s_sts = {\n" name)
  117. (format ofdidx " 0,0,\n") ;; no general index
  118. (format ofdidx "#ifdef CST_NO_STATIC_VOX\n") ;; don't link in data
  119. (format ofdidx " 0,0,0,0,\n")
  120. (format ofdidx "#else\n")
  121. (format ofdidx " %s_lpc,\n" name)
  122. (format ofdidx " %s_res,\n" name)
  123. (format ofdidx " %s_resi,\n" name)
  124. (format ofdidx " %s_ressize,\n" name)
  125. (format ofdidx "#endif\n")
  126. (format ofdidx " %d,\n" i)
  127. (format ofdidx " %d,\n" lpc_order)
  128. (format ofdidx " %d,\n" sample_rate)
  129. (format ofdidx " %f,\n" lpc_min)
  130. (format ofdidx " %f\n," lpc_range)
  131. (format ofdidx " \"g721\"\n") ;; encode of residual
  132. (format ofdidx "};\n\n")
  133. (format ofdidx "static const cst_diphone_entry %s_index[] = { \n" name)
  134. (mapcar
  135. (lambda (e)
  136. (format ofdidx " { \"%s\",%d,%d,%d },\n"
  137. (nth 0 e)
  138. (nth 1 e) ; start_pm
  139. (- (nth 2 e) (nth 1 e)) ; phone_boundary_pm (delta)
  140. (- (nth 3 e) (nth 2 e)) ; end_pm (delta)
  141. ))
  142. (reverse diphone_entries))
  143. (format ofdidx " { 0,0,0,0 } };\n\n")
  144. (format ofdidx "const cst_diphone_db %s_db = {\n" name)
  145. (format ofdidx " \"%s\",\n" name)
  146. (format ofdidx " %d,\n" num_unit_entries)
  147. (format ofdidx " %s_index,\n" name)
  148. (format ofdidx " &%s_sts\n" name)
  149. (format ofdidx "};\n\n")
  150. (format ofdlpc " 0 };\n\n")
  151. (format ofdres " 0 };\n\n")
  152. (format ofdresi " %d };\n\n" res_byte_pos)
  153. (format ofdressize " 0 };\n\n")
  154. (fclose ofdidx)
  155. (fclose ofdlpc)
  156. (fclose ofdres)
  157. (fclose ofdresi)
  158. (fclose ofdressize)
  159. ))
  160. (define (find_pm_pos entry stsdir ofdlpc ofdres ofdresi ofdressize)
  161. "(find_pm_pos entry lpddir)
  162. Diphone dics give times in seconds here we want them as indexes. This
  163. function converts the lpc to ascii and finds the pitch marks that
  164. go with this unit. These are written to ofdsts with ulaw residual
  165. as short term signal."
  166. (let ((sts_coeffs (load
  167. (format nil "%s/%s.sts" stsdir (cadr entry))
  168. t))
  169. (start_time (nth 2 entry))
  170. (phoneboundary_time (nth 3 entry))
  171. (end_time (nth 4 entry))
  172. start_pm pb_pm end_pm
  173. (ltime 0))
  174. (format t "%l\n" entry)
  175. (set! sts_info (car sts_coeffs))
  176. (set! sts_coeffs (cdr sts_coeffs))
  177. (set! ltime 0)
  178. (set! size_to_now 0)
  179. (while (and sts_coeffs
  180. (> (absdiff start_time (car (car sts_coeffs)))
  181. (absdiff start_time (car (cadr sts_coeffs)))))
  182. (set! ltime (car (car sts_coeffs)))
  183. (set! sts_coeffs (cdr sts_coeffs)))
  184. (set! sample_rate (nth 2 sts_info))
  185. (set! lpc_order (nth 1 sts_info))
  186. (set! lpc_min (nth 3 sts_info))
  187. (set! lpc_range (nth 4 sts_info))
  188. (set! start_pm pm_pos)
  189. (while (and sts_coeffs
  190. (> (absdiff phoneboundary_time (car (car sts_coeffs)))
  191. (absdiff phoneboundary_time (car (cadr sts_coeffs)))))
  192. (output_sts (car sts_coeffs) ofdlpc ofdres ofdresi ofdressize)
  193. (set! sts_coeffs (cdr sts_coeffs)))
  194. (set! pb_pm pm_pos)
  195. (while (and sts_coeffs (cdr sts_coeffs)
  196. (> (absdiff end_time (car (car sts_coeffs)))
  197. (absdiff end_time (car (cadr sts_coeffs)))))
  198. (output_sts (car sts_coeffs) ofdlpc ofdres ofdresi ofdressize)
  199. (set! sts_coeffs (cdr sts_coeffs)))
  200. (set! end_pm pm_pos)
  201. (list
  202. (car entry)
  203. (cadr entry)
  204. start_pm
  205. pb_pm
  206. end_pm)))
  207. (define (output_sts frame ofdlpc ofdres ofdresi ofdressize)
  208. "(output_sts frame residual ofd)
  209. Ouput this LPC frame."
  210. (let ((time (nth 0 frame))
  211. (coeffs (nth 1 frame))
  212. (size (nth 2 frame))
  213. (r (nth 3 frame)))
  214. (set! times (cons time times))
  215. (format ofdlpc "/* %d */ " pm_pos)
  216. (while coeffs
  217. (format ofdlpc " %d," (car coeffs))
  218. (set! coeffs (cdr coeffs)))
  219. (format ofdlpc "\n")
  220. (set! rbytes (length r))
  221. (set! s size)
  222. (format ofdres "/* %d */ " pm_pos)
  223. ;; Use difference between residuals indices as size
  224. ; ;; Residual size encoded as two bytes (could one at a push)
  225. ; (format ofdres " %d,%d, "
  226. ; (/ (- size (% size 256)) 256)
  227. ; (% size 256))
  228. (while r
  229. (format ofdres " %d," (car r))
  230. (set! r (cdr r)))
  231. (format ofdres " \n")
  232. (format ofdresi " %d, /* %d */\n" res_byte_pos pm_pos)
  233. (set! res_byte_pos (+ res_byte_pos rbytes))
  234. (if (> size 255)
  235. (set! size 255))
  236. (format ofdressize " %d, \n" size)
  237. (set! pm_pos (+ 1 pm_pos))
  238. ))
  239. (define (output_stsS frame ofdlpc ofdres ofdresi)
  240. "(output_sts frame residual ofd)
  241. Ouput this LPC frame."
  242. (format t "Spike excited diphones\n")
  243. (let ((time (nth 0 frame))
  244. (coeffs (nth 1 frame))
  245. (size (nth 2 frame))
  246. (r (nth 3 frame)))
  247. (set! times (cons time times))
  248. (format ofdlpc "/* %d */ " pm_pos)
  249. (while coeffs
  250. (format ofdlpc " %d," (car coeffs))
  251. (set! coeffs (cdr coeffs)))
  252. (format ofdlpc "\n")
  253. (set! s size)
  254. (format ofdres "/* %d */ " pm_pos)
  255. (set! sump 0)
  256. (while (cdr r)
  257. (set! sump (+ (* (car r) (car r)) sump))
  258. (set! r (cdr r)))
  259. (format ofdresi " %d, /* %d */\n" (sqrt sump) pm_pos)
  260. (set! pm_pos (+ 1 pm_pos))
  261. ))
  262. ;(defvar spike_excited t)
  263. (if (boundp 'spike_excited)
  264. (set! output_sts output_stsS))
  265. (define (oldfind_pm_pos entry lpcdir ofdsts)
  266. "(find_pm_pos entry lpddir)
  267. Diphone dics give times in seconds here we want them as indexes. This
  268. function converts the lpc to ascii and finds the pitch marks that
  269. go with this unit. These are written to ofdsts with ulaw residual
  270. as short term signal."
  271. (let ((lpc_coeffs (load_ascii_track
  272. (format nil "%s/%s.lpc" lpcdir (cadr entry))
  273. (nth 2 entry)))
  274. (ulaw_residual (load_ascii_ulaw_wave
  275. (format nil "%s/%s.res" lpcdir (cadr entry))
  276. (nth 2 entry)
  277. (nth 4 entry)))
  278. (start_time (nth 2 entry))
  279. (phoneboundary_time (nth 3 entry))
  280. (end_time (nth 4 entry))
  281. start_pm pb_pm end_pm
  282. (ltime 0))
  283. (format t "%l\n" entry)
  284. (while (and lpc_coeffs
  285. (> (absdiff start_time (car (car lpc_coeffs)))
  286. (absdiff start_time (car (cadr lpc_coeffs)))))
  287. ; (format t "%f %f\n" start_time (car (car lpc_coeffs)))
  288. (set! ltime (car (car lpc_coeffs)))
  289. (set! lpc_coeffs (cdr lpc_coeffs)))
  290. ; (format t "start time %f %f\n" start_time (car (car lpc_coeffs)))
  291. (set! start_pm pm_pos)
  292. (while (and lpc_coeffs
  293. (> (absdiff phoneboundary_time (car (car lpc_coeffs)))
  294. (absdiff phoneboundary_time (car (cadr lpc_coeffs)))))
  295. (output_sts (car lpc_coeffs)
  296. (- (car (car lpc_coeffs)) ltime)
  297. ulaw_residual ofdsts)
  298. (set! ulaw_residual
  299. (cons (car ulaw_residual)
  300. (nth_cdr (* (- (car (car lpc_coeffs)) ltime)
  301. (car ulaw_residual))
  302. (cdr ulaw_residual))))
  303. (set! ltime (car (car lpc_coeffs)))
  304. (set! lpc_coeffs (cdr lpc_coeffs)))
  305. (set! pb_pm pm_pos)
  306. (while (and lpc_coeffs (cdr lpc_coeffs)
  307. (> (absdiff end_time (car (car lpc_coeffs)))
  308. (absdiff end_time (car (cadr lpc_coeffs)))))
  309. (output_sts (car lpc_coeffs)
  310. (- (car (car lpc_coeffs)) ltime)
  311. ulaw_residual
  312. ofdsts)
  313. (set! ulaw_residual
  314. (cons (car ulaw_residual)
  315. (nth_cdr (* (- (car (car lpc_coeffs)) ltime)
  316. (car ulaw_residual))
  317. (cdr ulaw_residual))))
  318. (set! ltime (car (car lpc_coeffs)))
  319. (set! lpc_coeffs (cdr lpc_coeffs)))
  320. (set! end_pm pm_pos)
  321. (list
  322. (car entry)
  323. (cadr entry)
  324. start_pm
  325. pb_pm
  326. end_pm)))
  327. (define (oldoutput_sts frame duration residual ofd)
  328. "(output_sts frame residual ofd)
  329. Ouput this LPC frame."
  330. (let ((size (* duration (car residual)))
  331. (end_samp (* (car frame) (car residual))))
  332. (set! sample_rate (car residual))
  333. (set! lpc_order (- (length frame) 3))
  334. (set! times (cons (car frame) times))
  335. (format ofd "static const unsigned short diphs_frame_%d[] = { \n" pm_pos)
  336. (set! frame (cddr frame)) ;; skip the "1"
  337. (set! frame (cdr frame)) ;; skip the energy
  338. (while (cdr frame)
  339. (format ofd " %d," (lpccoeff_norm (car frame)))
  340. (set! frame (cdr frame))
  341. (if (not (cdr frame)) (format ofd " %d" (lpccoeff_norm (car frame)))))
  342. (format ofd " };\n")
  343. (format ofd "static const unsigned char diphs_res_%d[] = { \n" pm_pos)
  344. (set! s size)
  345. ; (set! r (nth_cdr (- end_samp size) residual))
  346. (set! r (cdr residual))
  347. ; (format t "length of residual is dur %f %d\n" duration (length r))
  348. (while (> s 1)
  349. (format ofd " %d," (car r))
  350. (set! r (cdr r))
  351. (set! s (- s 1))
  352. (if (equal? s 1) (format ofd " %d" (car r))))
  353. (format ofd " };\n")
  354. (format ofd "#define diphs_size_%d %d\n" pm_pos size)
  355. (format ofd "\n")
  356. (set! pm_pos (+ 1 pm_pos))
  357. ))
  358. (define (oldlpccoeff_norm c)
  359. (* (/ (- c lpc_min) (- lpc_max lpc_min))
  360. 65535))
  361. (define (oldload_ascii_ulaw_wave wavefilename starttime)
  362. "(load_ascii_ulaw_wave wavefilename starttime)
  363. Coverts wavefilename to simple ascii representation."
  364. (let ((tmpfile (make_tmp_filename))
  365. b)
  366. (system (format nil "ch_wave -info %s | grep Sample |
  367. awk '{printf(\"%%d \\n\",$3)}' >%s"
  368. wavefilename tmpfile))
  369. (system (format nil "ch_wave -otype raw -ostype ascii -start %f %s | asciiS2U >>%s"
  370. starttime wavefilename tmpfile))
  371. (set! b (load tmpfile t))
  372. (delete-file tmpfile)
  373. b))
  374. (define (oldload_ascii_track trackfilename starttime)
  375. "(load_ascii_track trackfilename)
  376. Coverts trackfilename to simple ascii representation."
  377. (let ((tmpfile (make_tmp_filename))
  378. (nicestarttime (if (> starttime 0.100)
  379. (- starttime 0.100)
  380. starttime))
  381. b)
  382. (system (format nil "ch_track -otype est -start %f %s |
  383. awk '{if ($1 == \"EST_Header_End\")
  384. header=1;
  385. else if (header == 1)
  386. printf(\"( %%s )\\n\",$0)}'>%s"
  387. nicestarttime trackfilename tmpfile))
  388. (set! b (load tmpfile t))
  389. (delete-file tmpfile)
  390. b))
  391. (define (absdiff a b)
  392. (let ((d (- a b )))
  393. (if (< d 0)
  394. (* -1 d)
  395. d)))
  396. (provide 'make_didb)