Debian Codegraph @

tools/make_didb2.scm

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