Debian Codegraph @

LPC/VocalTractTier.cpp

raw
1 /* VocalTractTier.cpp
2 *
3 * Copyright (C) 2012-2020 David Weenink
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "NUM2.h"
20 #include "RealTier.h"
21 #include "VocalTractTier.h"
22
23 #include "oo_DESTROY.h"
24 #include "VocalTractTier_def.h"
25 #include "oo_COPY.h"
26 #include "VocalTractTier_def.h"
27 #include "oo_EQUAL.h"
28 #include "VocalTractTier_def.h"
29 #include "oo_CAN_WRITE_AS_ENCODING.h"
30 #include "VocalTractTier_def.h"
31 #include "oo_WRITE_TEXT.h"
32 #include "VocalTractTier_def.h"
33 #include "oo_READ_TEXT.h"
34 #include "VocalTractTier_def.h"
35 #include "oo_WRITE_BINARY.h"
36 #include "VocalTractTier_def.h"
37 #include "oo_READ_BINARY.h"
38 #include "VocalTractTier_def.h"
39 #include "oo_DESCRIPTION.h"
40 #include "VocalTractTier_def.h"
41
42 Thing_implement (VocalTractPoint, AnyPoint, 0);
43
44 autoVocalTractPoint VocalTractPoint_create (VocalTract me, double time) {
45 try {
46 autoVocalTractPoint thee = Thing_new (VocalTractPoint);
47 thy number = time;
48 thy d_vocalTract = Data_copy (me);
49 return thee;
50 } catch (MelderError) {
51 Melder_throw (me, U": VocalTractPoint not created.");
52 }
53 }
54
55 void VocalTract_drawSegments (VocalTract me, Graphics g, double maxLength, double maxArea, bool closedAtGlottis)
56 {
57 Graphics_setInner (g);
58 const double maxCrossection = sqrt (maxArea);
59 Graphics_setWindow (g, 0.0, maxLength, -maxCrossection, maxCrossection);
60 for (integer isection = 1; isection <= my nx; isection ++) {
61 const double x1 = (isection - 1.0) * my dx, x2 = x1 + my dx;
62 const double crosssection2 = sqrt (my z [1] [isection]);
63 Graphics_line (g, x1, crosssection2, x2, crosssection2);
64 Graphics_line (g, x1, -crosssection2, x2, -crosssection2);
65 if (isection > 1) {
66 double crosssection1 = sqrt (my z [1] [isection - 1]);
67 Graphics_line (g, x1, crosssection1, x1, crosssection2);
68 Graphics_line (g, x1, -crosssection1, x1, -crosssection2);
69 } else if (isection == 1 and closedAtGlottis) {
70 Graphics_line (g, x1, crosssection2, x1, -crosssection2);
71 }
72 }
73 Graphics_unsetInner (g);
74 }
75
76 Thing_implement (VocalTractTier, Function, 0);
77
78 autoVocalTractTier VocalTractTier_create (double fromTime, double toTime) {
79 try {
80 autoVocalTractTier me = Thing_new (VocalTractTier);
81 Function_init (me.get(), fromTime, toTime);
82 return me;
83 } catch (MelderError) {
84 Melder_throw (U": VocalTractTier not created.");
85 }
86 }
87
88 autoVocalTractTier VocalTract_to_VocalTractTier (VocalTract me, double startTime, double endTime, double time) {
89 try {
90 autoVocalTractTier thee = VocalTractTier_create (startTime, endTime);
91 VocalTractTier_addVocalTract (thee.get(), time, me);
92 return thee;
93 } catch (MelderError) {
94 Melder_throw (me, U": not converted to VocalTractTier");
95 }
96 }
97
98 void VocalTractTier_addVocalTract (VocalTractTier me, double time, VocalTract vocaltract) {
99 try {
100 autoVocalTractPoint thee = VocalTractPoint_create (vocaltract, time);
101 my d_vocalTracts. addItem_move (thee.move());
102 } catch (MelderError) {
103 Melder_throw (me, U": no VocalTract added.");
104 }
105 }
106
107 autoVocalTract VocalTractTier_to_VocalTract (VocalTractTier me, double time) {
108 try {
109 Melder_assert (my d_vocalTracts.size > 0);
110 const VocalTractPoint vtp = my d_vocalTracts.at [1];
111 const integer numberOfSections = vtp -> d_vocalTract -> nx;
112 autoVocalTract thee = VocalTract_create (numberOfSections, vtp -> d_vocalTract -> dx);
113 for (integer isection = 1; isection <= numberOfSections; isection ++) {
114 autoRealTier section = RealTier_create (my xmin, my xmax);
115 for (integer i = 1; i <= my d_vocalTracts.size; i ++) {
116 const VocalTractPoint vtpi = my d_vocalTracts.at [i];
117 const double areai = vtpi -> d_vocalTract -> z [1] [isection];
118 RealTier_addPoint (section.get(), vtpi -> number, areai);
119 }
120 thy z [1] [isection] = RealTier_getValueAtTime (section.get(), time);
121 }
122 return thee;
123 } catch (MelderError) {
124 Melder_throw (me, U": no VocalTract created.");
125 }
126 }
127
128 autoLPC VocalTractTier_to_LPC (VocalTractTier me, double timeStep) {
129 try {
130 Melder_require (my d_vocalTracts.size > 0.0,
131 U"The VocalTractTier should not be empty.");
132 const integer numberOfFrames = Melder_ifloor ((my xmax - my xmin) / timeStep);
133 const VocalTractPoint vtp = my d_vocalTracts.at [1];
134 integer numberOfSections = vtp -> d_vocalTract -> nx;
135 const double samplingPeriod = 1.0 / (1000.0 * numberOfSections);
136
137 autoMAT area = zero_MAT (numberOfFrames, numberOfSections);
138 autoVEC areavec = raw_VEC (numberOfSections);
139 autoLPC thee = LPC_create (my xmin, my xmax, numberOfFrames, timeStep, timeStep / 2.0, numberOfSections, samplingPeriod);
140 // interpolate each section
141 for (integer isection = 1; isection <= numberOfSections; isection ++) {
142 autoRealTier sectioni = RealTier_create (my xmin, my xmax);
143 for (integer i = 1; i <= my d_vocalTracts.size; i ++) {
144 const VocalTractPoint vtpi = my d_vocalTracts.at [i];
145 const double areai = vtpi -> d_vocalTract -> z [1] [isection];
146 RealTier_addPoint (sectioni.get(), vtpi -> number, areai);
147 }
148 for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) {
149 const double time = thy x1 + (iframe - 1) * thy dx;
150 area [iframe] [isection] = RealTier_getValueAtTime (sectioni.get(), time);
151 }
152 }
153 for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) {
154 const LPC_Frame frame = & thy d_frames [iframe];
155 LPC_Frame_init (frame, numberOfSections);
156 for (integer i = 1; i <= numberOfSections; i ++)
157 areavec [i] = area [iframe] [numberOfSections + 1 - i]; // reverse
158 VEClpc_from_area (frame -> a.get(), areavec.get());
159 frame -> gain = 1e-6; // something
160 }
161 return thee;
162 } catch (MelderError) {
163 Melder_throw (U": not converted to LPC.");
164 }
165 }
166
167
168 /* End of file VocalTractTier.cpp */