1 /*************************************************************************/
2 /*                                                                       */
3 /*                Centre for Speech Technology Research                  */
4 /*                     University of Edinburgh, UK                       */
5 /*                         Copyright (c) 1998                            */
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 /*  THE UNIVERSITY OF EDINBURGH 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 THE UNIVERSITY OF EDINBURGH 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 and Paul Taylor                    */
34 /*             Date   :  February 1998                                   */
35 /*-----------------------------------------------------------------------*/
36 /*                                                                       */
37 /*  An implementation of Metrical Tree Phonology                         */
38 /*                                                                       */
39 /*=======================================================================*/
40 
41 #include <cmath>
42 #include "festival.h"
43 #include "us_duration.h"
44 
phone_z_score(const EST_String & p,float dur)45 float phone_z_score(const EST_String &p, float dur)
46 {
47     float mean, sd;
48     mean = met_duration.val(p).val("mean");
49     sd = met_duration.val(p).val("sd");
50     return ((dur - mean) / sd);
51 }
52 
clear_feature(EST_Relation & r,const EST_String & name)53 void clear_feature(EST_Relation &r, const EST_String &name)
54 {
55     for (EST_Item *p = r.head(); p ; p = p->next())
56 	p->f_remove(name);
57 }
58 
59 // void dur_to_end(EST_Relation &r)
60 // moved to UniSyn us_diphone.cc
61 
end_to_dur(EST_Relation & r)62 void end_to_dur(EST_Relation &r)
63 {
64     float prev_end = 0;
65 
66     for (EST_Item *p = r.head(); p ; p = p->next())
67     {
68 	p->set("dur", p->F("end") - prev_end);
69 	prev_end = p->F("end");
70     }
71 }
72 
assign_phone_z_scores(EST_Utterance & u,const EST_String & seg_name)73 void assign_phone_z_scores(EST_Utterance &u, const EST_String &seg_name)
74 {
75     EST_Item *s;
76 
77     end_to_dur(*u.relation(seg_name));
78 
79     for (s = u.relation(seg_name)->head(); s; s = s->next())
80 	s->set("z_score", phone_z_score(s->f("name"), s->F("dur")));
81 }
82 
promote_mean_z_score(EST_Utterance & u,const EST_String & st_name,const EST_String & syl_name)83 void promote_mean_z_score(EST_Utterance &u, const EST_String &st_name,
84 		      const EST_String &syl_name)
85 {
86     EST_Item *p, *s, *l;
87     float z, n;
88 
89 
90     for (s = u.relation(syl_name)->head(); s; s = s->next())
91     {
92 	p = s->as_relation(st_name);
93 	z = 0.0;
94 	for (n = 1, l = first_leaf_in_tree(p); l!= last_leaf_in_tree(p);
95 	     l = next_leaf(l), n += 1.0)
96 	    z += l->F("z_score");
97 
98 	z += l->F("z_score");
99 	z = z / n;
100 	s->set("m_z_score", z);
101 
102 
103 //	n = named_daughter(s->as_relation(st_name), "sylval", "Rhyme");
104 //       n = daughter1(named_daughter(n, "sylval", "Nucleus"));
105 //	s->set("z_score", n->F("z_score"));
106 
107     }
108 }
109 
promote_vowel_z_score(EST_Utterance & u,const EST_String & st_name,const EST_String & syl_name)110 void promote_vowel_z_score(EST_Utterance &u, const EST_String &st_name,
111 		      const EST_String &syl_name)
112 {
113     EST_Item *n, *s;
114 
115     for (s = u.relation(syl_name)->head(); s; s = s->next())
116     {
117 	n = named_daughter(s->as_relation(st_name), "sylval", "Rhyme");
118 	n = daughter1(named_daughter(n, "sylval", "Nucleus"));
119 	s->set("z_score", n->F("z_score"));
120     }
121 }
122 
123 
124 // set everything to its phone's mean duration
FT_met_dur_predict_1(LISP lutt,LISP lrel)125 LISP FT_met_dur_predict_1(LISP lutt, LISP lrel)
126 {
127     EST_Utterance *utt = get_c_utt(lutt);
128     EST_String rel = get_c_string(lrel);
129     EST_Item *p;
130 
131     clear_feature(*utt->relation(rel), "dur");
132     clear_feature(*utt->relation(rel), "end");
133 
134     for (p = utt->relation(rel)->head(); p ; p = p->next())
135 	p->set("dur", met_duration.val(p->f("name")).F("mean"));
136 
137     cout << "dur end\n";
138 
139     dur_to_end(*utt->relation(rel));
140 
141     return lutt;
142 }
143 
FT_met_dur_predict_2(LISP lutt,LISP lrel)144 LISP FT_met_dur_predict_2(LISP lutt, LISP lrel)
145 {
146     EST_Utterance *utt = get_c_utt(lutt);
147     EST_String rel = get_c_string(lrel);
148 
149     clear_feature(*utt->relation(rel), "dur");
150     clear_feature(*utt->relation(rel), "end");
151 
152     for (EST_Item *p = utt->relation(rel)->head(); p ; p = p->next())
153 	p->set("dur", 0.2);
154 
155     dur_to_end(*utt->relation(rel));
156 
157     return lutt;
158 }
159 
160 typedef
161 float (*local_cost_function)(const EST_Item *item1,
162 			     const EST_Item *item2);
163 
164 float local_cost(const EST_Item *s1, const EST_Item *s2);
165 
166 bool dp_match(const EST_Relation &lexical,
167 	      const EST_Relation &surface,
168 	      EST_Relation &match,
169 	      local_cost_function lcf,
170 	      EST_Item *null_syl);
171 
172 void add_times(EST_Relation &lexical, EST_Relation &surface,
173 	       EST_Relation &match);
174 
FT_nat_dur_predict(LISP lutt,LISP lrel_name,LISP llab_file)175 LISP FT_nat_dur_predict(LISP lutt, LISP lrel_name, LISP llab_file)
176 {
177     EST_Utterance *utt = get_c_utt(lutt);
178     EST_String rel_name = get_c_string(lrel_name);
179     EST_String lab_file = get_c_string(llab_file);
180     EST_Relation lab, *segment, match, *ulab, *umatch;
181 
182     utt->create_relation("Match");
183     utt->create_relation("Lab");
184 
185     if (utt->relation("Lab")->load(lab_file) != format_ok)
186 	festival_error();
187 //    if (lab.load(lab_file) != format_ok)
188 //	festival_error();
189 
190     EST_Item xx;
191 
192     segment = utt->relation(rel_name);
193     ulab = utt->relation("Lab");
194     umatch = utt->relation("Match");
195 
196     clear_feature(*segment, "dur");
197     clear_feature(*segment, "end");
198 
199     dp_match(*segment, *ulab, *umatch, local_cost, &xx);
200     add_times(*segment, *ulab, match);
201 
202     utt->remove_relation("Match");
203     utt->remove_relation("Lab");
204 
205 //    dp_match(*segment, lab, match, local_cost, &xx);
206 //    add_times(*segment, lab, match);
207 
208     return lutt;
209 }
210 
211