Debian Codegraph @

tools/make_didb.scm

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