Debian Codegraph @

lang/usenglish/us_f0_model.c

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 /* An F0 model */
38 /* This is derived fromthe f2b model freely distributed in Festival */
39 /* */
40 /*************************************************************************/
41
42 #include "cst_hrg.h"
43 #include "cst_phoneset.h"
44 #include "us_f0.h"
45
46 static void apply_lr_model(cst_item *s,
47 const us_f0_lr_term *f0_lr_terms,
48 float *start,
49 float *mid,
50 float *end)
51 {
52 int i;
53 const cst_val *v=0;
54 float fv;
55
56 /* Interceptors */
57 *start = f0_lr_terms[0].start;
58 *mid = f0_lr_terms[0].mid;
59 *end = f0_lr_terms[0].end;
60 for (i=1; f0_lr_terms[i].feature; i++)
61 {
62 if (!cst_streq(f0_lr_terms[i].feature,f0_lr_terms[i-1].feature))
63 v = ffeature(s,f0_lr_terms[i].feature);
64 if (f0_lr_terms[i].type)
65 {
66 if (cst_streq(val_string(v),f0_lr_terms[i].type))
67 fv = 1.0;
68 else
69 fv = 0.0;
70 }
71 else
72 fv = val_float(v);
73 (*start) += fv*f0_lr_terms[i].start;
74 (*mid) += fv*f0_lr_terms[i].mid;
75 (*end) += fv*f0_lr_terms[i].end;
76 /* printf("f %s start %f mid %f end %f\n",
77 f0_lr_terms[i].feature,
78 *start,*mid,*end); */
79 }
80 }
81
82 static void add_target_point(cst_relation *targ,float pos, float f0)
83 {
84 cst_item *t;
85
86 /* printf("target %f at %f\n",f0,pos); */
87 t = relation_append(targ,NULL);
88 item_set_float(t,"pos",pos);
89 /* them there can sometimes do silly things, so guard for that */
90 if (f0 > 500.0)
91 item_set_float(t,"f0",500.0);
92 else if (f0 < 50.0)
93 item_set_float(t,"f0",50.0);
94 else
95 item_set_float(t,"f0",f0);
96 }
97
98 /* model mean and stddev take from f2b/kal_diphone */
99 #define model_mean 170.0
100 #define model_stddev 34
101 #define map_f0(v,m,s) ((((v-model_mean)/model_stddev)*s)+m)
102
103 static int post_break(cst_item *syl)
104 {
105 if ((item_prev(syl) == 0) ||
106 (cst_streq("pau",
107 ffeature_string(syl,
108 "R:SylStructure.daughter.R:Segment.p.name"))))
109 return TRUE;
110 else
111 return FALSE;
112 }
113
114 static int pre_break(cst_item *syl)
115 {
116 if ((item_next(syl) == 0) ||
117 (cst_streq("pau",
118 ffeature_string(syl,
119 "R:SylStructure.daughtern.R:Segment.n.name"))))
120 return TRUE;
121 else
122 return FALSE;
123 }
124
125 static float vowel_mid(cst_item *syl)
126 {
127 /* return time point mid way in vowel in this syl */
128 cst_item *s;
129 cst_item *ts;
130 const cst_phoneset *ps = item_phoneset(syl);
131
132 ts = item_daughter(item_as(syl,"SylStructure"));
133 for (s=ts; s; s = item_next(s))
134 {
135 if (cst_streq("+", phone_feature_string(ps,item_feat_string(s,"name"),
136 "vc")))
137 {
138 return (item_feat_float(s,"end")+
139 ffeature_float(s,"R:Segment.p.end"))/2.0;
140 }
141 }
142
143 /* no segments, shouldn't happen */
144 if (ts == 0)
145 return 0;
146
147 /* no vowel in syllable, shouldn't happen */
148 return (item_feat_float(ts,"end")+
149 ffeature_float(ts,"R:Segment.p.end"))/2.0;
150 }
151
152 cst_utterance *us_f0_model(cst_utterance *u)
153 {
154 /* F0 target model: Black and Hunt ICSLP96, three points per syl */
155 cst_item *syl, *t, *nt;
156 cst_relation *targ_rel;
157 float mean, stddev, local_mean, local_stddev;
158 float start, mid, end, lend;
159 float seg_end;
160
161 if (feat_present(u->features,"no_f0_target_model"))
162 return u;
163
164 targ_rel = utt_relation_create(u,"Target");
165 mean = get_param_float(u->features,"int_f0_target_mean", 100.0);
166 mean *= get_param_float(u->features,"f0_shift", 1.0);
167 stddev = get_param_float(u->features,"int_f0_target_stddev", 12.0);
168
169 lend = 0;
170 for (syl=relation_head(utt_relation(u,"Syllable"));
171 syl;
172 syl = item_next(syl))
173
174 {
175 /* printf("word %s, accent %s endtone %s\n",
176 ffeature_string(syl,"R:SylStructure.parent.name"),
177 ffeature_string(syl,"accent"),
178 ffeature_string(syl,"endtone")); */
179 if (!item_daughter(item_as(syl,"SylStructure")))
180 continue; /* no segs in syl */
181
182 local_mean = ffeature_float(syl,"R:SylStructure.parent.R:Token.parent.local_f0_shift");
183 if (local_mean)
184 local_mean *= mean;
185 else
186 local_mean = mean;
187 local_stddev = ffeature_float(syl,"R:SylStructure.parent.R:Token.parent.local_f0_range");
188 if (local_stddev == 0.0)
189 local_stddev = stddev;
190
191 apply_lr_model(syl,f0_lr_terms,&start,&mid,&end);
192 if (post_break(syl))
193 lend = map_f0(start,local_mean,local_stddev);
194 add_target_point(targ_rel,
195 ffeature_float(syl,
196 "R:SylStructure.daughter.R:Segment.p.end"),
197 map_f0((start+lend)/2.0,local_mean,local_stddev));
198 add_target_point(targ_rel,
199 vowel_mid(syl),
200 map_f0(mid,local_mean,local_stddev));
201 lend = map_f0(end,local_mean,local_stddev);
202 if (pre_break(syl))
203 add_target_point(targ_rel,
204 ffeature_float(syl,"R:SylStructure.daughtern.end"),
205 map_f0(end,local_mean,local_stddev));
206 }
207
208 /* Guarantee targets go from start to end of utterance */
209 t = relation_head(targ_rel);
210 if (t == 0)
211 add_target_point(targ_rel,0,mean);
212 else if (item_feat_float(t,"pos") > 0)
213 {
214 nt = item_prepend(t,NULL);
215 item_set_float(nt,"pos",0.0);
216 item_set_float(nt,"f0",item_feat_float(t,"f0"));
217 }
218
219 t = relation_tail(targ_rel);
220 seg_end = item_feat_float(relation_tail(utt_relation(u,"Segment")),"end");
221 if (item_feat_float(t,"pos") < seg_end)
222 add_target_point(targ_rel,seg_end,item_feat_float(t,"f0"));
223
224 return u;
225 }