make_didb.scm 14 KB

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