Debian Codegraph @

src/wavesynth/cst_clunits.c

raw
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 /* Authors: Alan W Black ([email protected]) */
34 /* David Huggins-Daines ([email protected]) */
35 /* Date: April 2001 */
36 /* and more updates in 2009 */
37 /*************************************************************************/
38 /* */
39 /* clunits waveform synthesis */
40 /* */
41 /*************************************************************************/
42
43 #include <limits.h>
44
45 #include "cst_hrg.h"
46 #include "cst_utt_utils.h"
47 #include "cst_viterbi.h"
48 #include "cst_clunits.h"
49 #include "cst_units.h"
50 #include "cst_wave.h"
51 #include "cst_track.h"
52 #include "cst_sigpr.h"
53
54 /* #define CLUNITS_DEBUG 1 */
55
56 #ifndef CLUNITS_DEBUG
57 #define CLUNITS_DEBUG 0
58 #endif
59
60 #if CLUNITS_DEBUG > 0
61 # define DPRINTF(l,x) if (CLUNITS_DEBUG > (l)) cst_dbgmsg x
62 #else
63 # define DPRINTF(l,x)
64 #endif
65
66 #define BIG_BAD_NUMBER 50000 /* big enough to be bad, not big enough
67 to cause overflows */
68
69 CST_VAL_REGISTER_TYPE_NODEL(clunit_db,cst_clunit_db)
70 CST_VAL_REGISTER_TYPE_NODEL(vit_cand,cst_vit_cand)
71
72 static cst_utterance *clunits_select(cst_utterance *utt);
73 static cst_vit_cand *cl_cand(cst_item *i,
74 struct cst_viterbi_struct *vd);
75 static cst_vit_path *cl_path(cst_vit_path *p,
76 cst_vit_cand *c,
77 cst_viterbi *vd);
78 static const cst_cart *clunit_get_tree(cst_clunit_db *cludb, const char *name);
79 static void clunit_set_unit_name(cst_item *s,cst_clunit_db *clunit_db);
80
81 typedef int (*cst_distfunc)(const cst_clunit_db *, int, int, const int *, int, int);
82 static int optimal_couple_frame(cst_clunit_db *cludb, int u0, int u1,
83 cst_distfunc dfunc, int bestsofar);
84 static int optimal_couple(cst_clunit_db *cludb,
85 int u0, int u1,
86 int *u0_move, int *u1_move,
87 cst_distfunc dfunc);
88 static int frame_distance(const cst_clunit_db *cludb,
89 int a, int b,
90 const int *join_weights,
91 int order,
92 int best);
93 static int frame_distanceb(const cst_clunit_db *cludb,
94 int a, int b,
95 const int *join_weights,
96 int order,
97 int best);
98
99
100 cst_utterance *clunits_synth(cst_utterance *utt)
101 {
102 /* Basically the same as the diphone code */
103 clunits_select(utt);
104 join_units(utt);
105
106 return utt;
107 }
108
109 cst_utterance *clunits_dump_units(cst_utterance *utt)
110 {
111 cst_clunit_db *clunit_db;
112 cst_item *s, *u;
113 int unit_entry;
114
115 clunit_db = val_clunit_db(feat_val(utt->features,"clunit_db"));
116 for (s = relation_head(utt_relation(utt,"Segment")); s; s = item_next(s))
117 {
118 u = item_daughter(s);
119 unit_entry = item_feat_int(u,"unit_entry");
120 cst_dbgmsg("for %s end %f selected %d %s start move %d end move %d\n",
121 item_name(s),
122 item_feat_float(s,"end"),
123 unit_entry,
124 item_name(u),
125 item_feat_int(u,"unit_start") - clunit_db->units[unit_entry].start,
126 item_feat_int(u,"unit_end") - clunit_db->units[unit_entry].end);
127 }
128
129 return utt;
130 }
131
132 static cst_utterance *clunits_select(cst_utterance *utt)
133 {
134 cst_viterbi *vd;
135 cst_relation *units,*segs;
136 cst_item *s,*u;
137 cst_clunit_db *clunit_db;
138 int unit_entry;
139
140 segs = utt_relation(utt,"Segment");
141 vd = new_viterbi(cl_cand,cl_path);
142 vd->num_states = -1;
143 vd->big_is_good = FALSE;
144 feat_set(vd->f,"clunit_db",feat_val(utt->features,"clunit_db"));
145 clunit_db = val_clunit_db(feat_val(vd->f,"clunit_db"));
146 utt_set_feat(utt,"sts_list",sts_list_val(clunit_db->sts));
147
148 for (s=relation_head(segs); s; s=item_next(s))
149 clunit_set_unit_name(s,clunit_db);
150
151 viterbi_initialise(vd,segs);
152 viterbi_decode(vd);
153 if (!viterbi_result(vd,"selected_unit"))
154 {
155 cst_errmsg("clunits: can't find path\n");
156 cst_error();
157 }
158 viterbi_copy_feature(vd, "unit_prev_move");
159 viterbi_copy_feature(vd, "unit_this_move");
160 delete_viterbi(vd);
161
162 /* Construct unit stream with selected units */
163 units = utt_relation_create(utt,"Unit");
164 for (s=relation_head(segs); s; s=item_next(s))
165 {
166 u = relation_append(units,NULL);
167 item_set_string(u,"name",item_name(s));
168
169 unit_entry = item_feat_int(s,"selected_unit");
170
171 /* Get stuff from unit_db */
172 item_set(u,"unit_entry",item_feat(s,"selected_unit"));
173 item_set(u,"clunit_name",item_feat(s,"clunit_name"));
174 #if 0
175 printf("awb_debug %s\n",item_feat_string(u,"clunit_name"));
176 #endif
177
178 /* Use optimal join points if available */
179 if (item_feat_present(s, "unit_this_move"))
180 item_set_int(u,"unit_start", item_feat_int(s, "unit_this_move"));
181 else
182 item_set_int(u,"unit_start",clunit_db->units[unit_entry].start);
183
184 if (item_next(s) && item_feat_present(item_next(s), "unit_prev_move"))
185 item_set_int(u,"unit_end", item_feat_int(item_next(s), "unit_prev_move"));
186 else
187 item_set_int(u,"unit_end",clunit_db->units[unit_entry].end);
188
189 if (item_feat_int(u,"unit_start") > item_feat_int(u, "unit_end"))
190 {
191 /* cst_feat_print(stdout,s->contents->features); */
192 cst_errmsg("start %d end %d\n",
193 item_feat_int(u,"unit_start"), item_feat_int(u, "unit_end"));
194 /* cst_feat_print(stdout,u->contents->features); */
195 }
196
197 DPRINTF(0, ("selected %d=%s_%d %d %d\n",
198 unit_entry,
199 UNIT_TYPE(clunit_db,unit_entry),
200 UNIT_INDEX(clunit_db,unit_entry),
201 item_feat_int(u,"unit_start"), item_feat_int(u, "unit_end")));
202
203 item_set_int(u,"target_end",
204 (int)(item_feat_float(s,"end")*clunit_db->sts->sample_rate));
205 }
206
207 return utt;
208 }
209
210 /* This is used to add incremental target weighting to candidates */
211 /* It has been tuned but as the candidates are not actually ordered */
212 /* doing this is meaningless -- but *is* better */
213 #define clunits_target_weight 70
214
215 static cst_vit_cand *cl_cand(cst_item *i,cst_viterbi *vd)
216 {
217 const char *unit_type;
218 unsigned short nu;
219 int idx;
220 int e;
221 const cst_val *clist,*c;
222 cst_vit_cand *p,*all,*gt,*lc;
223 cst_clunit_db *clunit_db;
224 int ccc;
225
226 clunit_db = val_clunit_db(feat_val(vd->f,"clunit_db"));
227 unit_type = item_feat_string(i,"clunit_name");
228
229 /* get tree */
230 clist = cart_interpret(i,clunit_get_tree(clunit_db,unit_type));
231
232 all = 0; ccc = clunits_target_weight;
233 for (c=clist; c; c=val_cdr(c))
234 {
235 idx = clunit_get_unit_index(clunit_db, unit_type, val_int(val_car(c)));
236 p = new_vit_cand();
237 p->next = all;
238 p->item = i;
239 p->score = ccc;
240 ccc += clunits_target_weight;
241 vit_cand_set_int(p,idx);
242 all = p;
243 }
244
245 if ((clunit_db->extend_selections > 0) && (item_prev(i)))
246 {
247 lc = val_vit_cand(item_feat(item_prev(i),"clunit_cands"));
248 for (e=0; lc && (e < clunit_db->extend_selections); lc=lc->next)
249 {
250 nu = clunit_db->units[lc->ival].next;
251 if (nu == CLUNIT_NONE)
252 continue;
253 for (gt=all; gt; gt=gt->next)
254 if (nu == gt->ival)
255 break; /* we've got this one already */
256 if ((gt == 0)
257 && (clunit_db->units[nu].type
258 == clunit_db->units[all->ival].type))
259 {
260 p = new_vit_cand();
261 p->next = all;
262 p->item = i;
263 p->score = 0;
264 vit_cand_set_int(p, nu);
265 all = p;
266 e++;
267 }
268 }
269 }
270 item_set(i,"clunit_cands",vit_cand_val(all));
271
272 return all;
273 }
274
275 static cst_vit_path *cl_path(cst_vit_path *p,
276 cst_vit_cand *c,
277 cst_viterbi *vd)
278 {
279 int cost;
280 cst_vit_path *np;
281 cst_clunit_db *cludb;
282 cst_distfunc dfunc;
283 int u0,u1;
284 int u0_move = -1, u1_move = -1;
285
286 np = new_vit_path();
287 cludb = val_clunit_db(feat_val(vd->f,"clunit_db"));
288 if (cludb->mcep->sts)
289 dfunc = frame_distance;
290 else if (cludb->mcep->sts_paged)
291 dfunc = frame_distance;
292 else if (cludb->mcep->frames)
293 dfunc = frame_distance;
294 else
295 dfunc = frame_distanceb;
296
297 np->cand = c;
298 np->from = p;
299
300 if ((p==0) || (p->cand == 0))
301 cost = 0;
302 else
303 {
304 u0 = p->cand->ival;
305 u1 = c->ival;
306 if (cludb->optimal_coupling == 1) {
307 if (np->f == NULL)
308 np->f = new_features();
309 cost = optimal_couple(cludb, u0, u1, &u0_move, &u1_move, dfunc);
310 if (u0_move != -1)
311 feat_set(np->f, "unit_prev_move", int_val(u0_move));
312 if (u1_move != -1)
313 feat_set(np->f, "unit_this_move", int_val(u1_move));
314 } else if (cludb->optimal_coupling == 2)
315 cost = optimal_couple_frame(cludb, u0, u1, dfunc, INT_MAX);
316 else
317 cost = 0;
318 }
319
320 cost *= 1; /* magic number ("continuity weight") */
321 np->state = c->pos;
322 if (p==0)
323 np->score = cost + c->score;
324 else
325 np->score = cost + c->score + p->score;
326
327 return np;
328 }
329
330 static int optimal_couple_frame(cst_clunit_db *cludb, int u0, int u1,
331 cst_distfunc dfunc,
332 int bestsofar)
333 {
334 int a,b;
335
336 if (cludb->units[u1].prev == u0)
337 return 0; /* Consecutive units win */
338
339 if (cludb->units[u0].next != CLUNIT_NONE)
340 a = cludb->units[u0].end;
341 else /* don't want to do this but its all that is left to do */
342 a = cludb->units[u0].end-1; /* if num frames < 1 this is bad */
343 b = cludb->units[u1].start;
344
345 return (*dfunc)(cludb, a, b,
346 cludb->join_weights,
347 cludb->mcep->num_channels,bestsofar)
348 + abs(get_frame_size(cludb->sts, a)
349 - get_frame_size(cludb->sts,b)) * cludb->f0_weight;
350 }
351
352 static int optimal_couple(cst_clunit_db *cludb,
353 int u0, int u1,
354 int *u0_move, int *u1_move,
355 cst_distfunc dfunc)
356 {
357 int a,b;
358 int u1_p;
359 int i, fcount;
360 int u0_st, u1_p_st, u0_end, u1_p_end;
361 int best_u0, best_u1_p;
362 int dist, best_val;
363
364 u1_p = cludb->units[u1].prev;
365
366 if (u1_p == u0)
367 return 0;
368 if (u1_p == CLUNIT_NONE || cludb->units[u0].phone != cludb->units[u1_p].phone)
369 return 10 * optimal_couple_frame(cludb, u0, u1, dfunc,INT_MAX); /* laziness */
370
371 DPRINTF(1,("optimal_coupling %s_%d (%d,%d) %s_%d (%d,%d)\n",
372 UNIT_TYPE(cludb,u0),
373 UNIT_INDEX(cludb,u0),
374 cludb->units[u0].start, cludb->units[u0].end,
375 UNIT_TYPE(cludb,u1),
376 UNIT_INDEX(cludb,u1),
377 cludb->units[u1].start, cludb->units[u1].end));
378
379 u0_end = cludb->units[u0].end - cludb->units[u0].start;
380 u1_p_end = cludb->units[u1_p].end - cludb->units[u1_p].start;
381
382 u0_st = u0_end / 3;
383 u1_p_st = u1_p_end / 3;
384
385 if ((u0_end - u0_st) < (u1_p_end - u1_p_st))
386 {
387 fcount = u0_end - u0_st;
388 /* u1_p_st = u1_p_end - fcount; */
389 }
390 else
391 {
392 fcount = u1_p_end - u1_p_st;
393 /* u0_st = u0_end - fcount; */
394 }
395
396 DPRINTF(1,("%s == %s, sliding u0:(%d,%d) u1:(%d,%d)\n",
397 UNIT_TYPE(cludb,u0),
398 UNIT_TYPE(cludb,u1_p),
399 u0_st, u0_end, u1_p_st, u1_p_end));
400
401 best_u0 = u0_end;
402 best_u1_p = u1_p_end;
403 best_val = INT_MAX;
404
405 for (i = 0; i < fcount; ++i) {
406 a = cludb->units[u0].start + u0_st + i;
407 b = cludb->units[u1_p].start + u1_p_st + i;
408
409 dist =
410 (*dfunc)(cludb, a, b,
411 cludb->join_weights,
412 cludb->mcep->num_channels,
413 best_val)
414 + abs(get_frame_size(cludb->sts, a)
415 - get_frame_size(cludb->sts,b)) * cludb->f0_weight;
416
417 if (dist < best_val) {
418 best_val = dist;
419 best_u0 = u0_st + i;
420 best_u1_p = u1_p_st + i;
421 }
422 }
423
424 if (best_val == INT_MAX)
425 best_val = BIG_BAD_NUMBER; /* prevent overflows for zero-length units */
426
427 /* u0_move is the new end for u0
428 u1_move is the new start for u1
429
430 This works based on the assumption that the STS frames for
431 consecutive units in the recordings will also be consecutive.
432 The voice compiler MUST preserve this assumption! */
433
434 *u0_move = cludb->units[u0].start + best_u0;
435 *u1_move = cludb->units[u1_p].start + best_u1_p;
436 DPRINTF(1,("best_u0 %d = %d best_u1 %d = %d best_val %d\n",
437 best_u0, *u0_move, best_u1_p, *u1_move, best_val));
438
439 return 30000 + best_val;
440 }
441
442 static int frame_distance(const cst_clunit_db *cludb,
443 int a, int b,
444 const int *join_weights,
445 int order,
446 int bestsofar)
447 {
448 int r,diff;
449 int i;
450 const unsigned short *av, *bv;
451
452 bv = get_sts_frame(cludb->mcep, b);
453 av = get_sts_frame(cludb->mcep, a);
454
455 #if CLUNITS_DEBUG > 2
456 cst_dbgmsg("a(%d): ",a);
457 for (i = 0; i < order; ++i)
458 cst_dbgmsg("%.2f ",
459 (double) av[i] * cludb->mcep->coeff_range / 65536
460 + cludb->mcep->coeff_min);
461 cst_dbgmsg("\n");
462 cst_dbgmsg("b(%d): ",b);
463 for (i = 0; i < order; ++i)
464 cst_dbgmsg("%.2f ",
465 (double) bv[i] * cludb->mcep->coeff_range / 65536
466 + cludb->mcep->coeff_min);
467 cst_dbgmsg("\n");
468 #endif
469
470 /* Weighted Manhattan distance */
471 for (r = 0, i = 0; i < order; i++)
472 {
473 diff = av[i]-bv[i];
474 r += abs(diff) * join_weights[i] / 65536;
475 if (r > bestsofar)
476 return r; /* already worse than best */
477 }
478
479 return r;
480 }
481
482 static int frame_distanceb(const cst_clunit_db *cludb,
483 int a, int b,
484 const int *join_weights,
485 int order,
486 int bestsofar)
487 {
488 int r,diff;
489 int i;
490 const unsigned char *av, *bv;
491
492 bv = get_sts_residual_fixed(cludb->mcep, b);
493 av = get_sts_residual_fixed(cludb->mcep, a);
494
495 #if CLUNITS_DEBUG > 2
496 cst_dbgmsg("a(%d): ",a);
497 for (i = 0; i < order; ++i)
498 cst_dbgmsg("%.2f ",
499 (double) av[i] * cludb->mcep->coeff_range / 256
500 + cludb->mcep->coeff_min);
501 cst_dbgmsg("\n");
502 cst_dbgmsg("b(%d): ",b);
503 for (i = 0; i < order; ++i)
504 cst_dbgmsg("%.2f ",
505 (double) bv[i] * cludb->mcep->coeff_range / 256
506 + cludb->mcep->coeff_min);
507 cst_dbgmsg("\n");
508 #endif
509
510 /* Weighted Manhattan distance */
511 for (r = 0, i = 0; i < order; i++)
512 {
513 diff = (av[i]-bv[i]) * 256;
514 r += abs(diff) * join_weights[i] / 65536;
515 if (r > bestsofar)
516 return r; /* already worse than best */
517 }
518
519 return r;
520 }
521
522 int clunit_get_unit_type_index(cst_clunit_db *cludb, const char *name)
523 {
524 int start,end,mid,c;
525
526 start = 0;
527 end = cludb->num_types;
528
529 while (start < end)
530 {
531 mid = (start+end)/2;
532 c = strcmp(cludb->types[mid].name,name);
533
534 if (c == 0)
535 return mid;
536 else if (c > 0)
537 end = mid;
538 else
539 start = mid + 1;
540 }
541
542 return -1;
543 }
544
545 static const cst_cart *clunit_get_tree(cst_clunit_db *cludb, const char *name)
546 {
547 int i;
548
549 i = clunit_get_unit_type_index(cludb, name);
550 if (i == -1)
551 {
552 cst_errmsg("clunits: can't find tree for %s\n",name);
553 i = 0; /* "graceful" failure */
554 }
555 return cludb->trees[i];
556 }
557
558 static void clunit_set_unit_name(cst_item *s,cst_clunit_db *clunit_db)
559 {
560 if (clunit_db->unit_name_func)
561 {
562 char *cname;
563 cname = (clunit_db->unit_name_func)(s);
564 item_set_string(s,"clunit_name",cname);
565
566 /* printf("awb_debug clunit_name %s\n",cname); */
567
568 cst_free(cname);
569 }
570 else
571 {
572 /* is just the name by default */
573 item_set(s,"clunit_name",item_feat(s,"name"));
574 }
575
576 }
577
578 char *clunits_ldom_phone_word(cst_item *s)
579 {
580 const char *name;
581 const char *pname;
582 const char *wname;
583 const char *silence;
584 char *clname;
585 char *dname, *p, *q;
586
587 silence = val_string(feat_val(item_utt(s)->features,"silence"));
588 name = item_name(s);
589 if (cst_streq(name,silence))
590 {
591 pname = ffeature_string(s,"p.name");
592 clname = cst_alloc(char, cst_strlen(silence)+1+cst_strlen(pname)+1);
593 cst_sprintf(clname,"%s_%s",silence,pname);
594 }
595 else
596 {
597 /* remove single quotes from name */
598 wname = ffeature_string(s,"R:SylStructure.parent.parent.name");
599 dname = cst_downcase(wname);
600 for (q=p=dname; *p != '\0'; p++)
601 if (*p != '\'') *p = *q++;
602 *q = '\0';
603 clname = cst_alloc(char, cst_strlen(name)+1+cst_strlen(dname)+1);
604 cst_sprintf(clname,"%s_%s",name,dname);
605 cst_free(dname);
606 }
607 return clname;
608 }
609
610 int clunit_get_unit_index(cst_clunit_db *cludb,
611 const char *unit_type,
612 int instance)
613 {
614 int i;
615
616 i = clunit_get_unit_type_index(cludb, unit_type);
617 if (i == -1)
618 {
619 /* TODO: fall back to closest possible match */
620 cst_errmsg("clunit_get_unit_index: can't find unit type %s, using 0\n",
621 unit_type);
622 i = 0;
623 }
624 if (instance >= cludb->types[i].count)
625 {
626 cst_errmsg("clunit_get_unit_index: can't find instance %d of %s, using 0\n",
627 instance, unit_type);
628 instance = 0;
629 }
630
631 return cludb->types[i].start + instance;
632 }
633
634 int clunit_get_unit_index_name(cst_clunit_db *cludb,
635 const char *name)
636 {
637 const char *c;
638 char *type;
639 int idx, i;
640
641 c = cst_strrchr(name, '_');
642 if (c == NULL)
643 {
644 cst_errmsg("clunit_get_unit_index_name: invalid unit name %s\n", name);
645 return -1;
646 }
647 idx = atoi(c+1);
648 type = cst_substr(name, 0, c - name);
649 i = clunit_get_unit_index(cludb, type, idx);
650 cst_free(type);
651
652 return i;
653 }