Art_Speaker.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* Art_Speaker.cpp
  2. *
  3. * Copyright (C) 1992-2009,2011,2012,2014-2018 Paul Boersma
  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. #include "Art_Speaker.h"
  19. #define DLIP 5e-3
  20. void Art_Speaker_toVocalTract (Art _art, Speaker speaker,
  21. double intX [], double intY [], double extX [], double extY [],
  22. double *bodyX, double *bodyY)
  23. {
  24. double *art = _art -> art;
  25. double f = speaker -> relativeSize * 1e-3;
  26. struct { double x, y, da; } jaw;
  27. struct { double dx, dy; } hyoid;
  28. struct { double x, y, r, radius; } body;
  29. struct { double x, y, r, a; } teeth;
  30. struct { double a; } blade;
  31. struct { double x, y, a; } tip;
  32. struct { double dx, dy; } lowerLip, upperLip;
  33. double HBody_x, HBody_y, HC, Sp, p, a, b;
  34. /* Determine the position of the hyoid bone (Mermelstein's H). */
  35. /* The rest position is a characteristic of the speaker. */
  36. /* The stylohyoid muscle pulls the hyoid bone up. */
  37. /* The sternohyoid muscle pulls the hyoid bone down. */
  38. /* The sphincter muscle pulls the hyoid bone backwards. */
  39. hyoid.dx = -5 * f * art [(int) kArt_muscle::SPHINCTER];
  40. hyoid.dy = 20 * f * (art [(int) kArt_muscle::STYLOHYOID]
  41. - art [(int) kArt_muscle::STERNOHYOID]);
  42. /* The larynx moves up and down with the hyoid bone. */
  43. /* Only the lowest point (Mermelstein's K) */
  44. /* does not follow completely the horizontal movements. */
  45. /* Anterior larynx. */
  46. intX [1] = -14.0 * f + 0.5 * hyoid.dx; intY [1] = -53.0 * f + hyoid.dy;
  47. /* Top of larynx. */
  48. intX [2] = -20.0 * f + hyoid.dx; intY [2] = -33.0 * f + hyoid.dy;
  49. /* Epiglottis. */
  50. intX [3] = -20.0 * f + hyoid.dx; intY [3] = -26.0 * f + hyoid.dy;
  51. /* Hyoid bone. */
  52. intX [4] = -16.0 * f + hyoid.dx; intY [4] = -26.0 * f + hyoid.dy;
  53. /* Posterior larynx. */
  54. extX [1] = -22.0 * f + hyoid.dx; extY [1] = -53.0 * f + hyoid.dy;
  55. /* Esophagus. */
  56. extX [2] = -26.0 * f + hyoid.dx; extY [2] = -40.0 * f + hyoid.dy;
  57. /* The lower pharynx moves up and down with the hyoid bone. */
  58. /* The lower constrictor muscle pulls the rear pharyngeal wall forwards. */
  59. extX [3] = -34.0 * f + art [(int) kArt_muscle::SPHINCTER] * (5.0 * f);
  60. extY [3] = extY [2];
  61. /* The upper pharynx is fixed at the height of the velum. */
  62. /* The upper constrictor muscle pulls the rear pharyngeal wall forwards. */
  63. extX [5] = -34.0 * f + art [(int) kArt_muscle::SPHINCTER] * (5.0 * f);
  64. extY [5] = speaker -> velum.y;
  65. /* The height of the middle pharynx is in between the lower and upper pharynx. */
  66. /* The middle constrictor muscle pulls the rear pharyngeal wall forwards. */
  67. extX [4] = -34.0 * f + art [(int) kArt_muscle::SPHINCTER] * (5.0 * f);
  68. extY [4] = 0.5 * (extY [3] + extY [5]);
  69. /* Tongue root. */
  70. jaw.x = -75.0 * f; // position of the condyle
  71. jaw.y = 53.0 * f;
  72. jaw.da = art [(int) kArt_muscle::MASSETER] * 0.15
  73. - art [(int) kArt_muscle::MYLOHYOID] * 0.20;
  74. body.x = jaw.x + 81.0 * f * cos (-0.60 + jaw.da)
  75. - art [(int) kArt_muscle::STYLOGLOSSUS] * (10.0 * f)
  76. + art [(int) kArt_muscle::GENIOGLOSSUS] * (10.0 * f);
  77. body.y = jaw.y + 81.0 * f * sin (-0.60 + jaw.da)
  78. - art [(int) kArt_muscle::HYOGLOSSUS] * (10.0 * f)
  79. + art [(int) kArt_muscle::STYLOGLOSSUS] * (5.0 * f);
  80. *bodyX = body.x;
  81. *bodyY = body.y;
  82. body.r = sqrt ((jaw.x - body.x) * (jaw.x - body.x) + (jaw.y - body.y) * (jaw.y - body.y));
  83. body.radius = 20.0 * f;
  84. HBody_x = body.x - intX [4];
  85. HBody_y = body.y - intY [4];
  86. HC = sqrt (HBody_x * HBody_x + HBody_y * HBody_y);
  87. if (HC <= body.radius) {
  88. HC = body.radius;
  89. Sp = 0.0; // prevent rounding errors in sqrt (can occur on processors with e.g. 80-bit registers)
  90. } else {
  91. Sp = sqrt (HC * HC - body.radius * body.radius);
  92. }
  93. a = atan2 (HBody_y, HBody_x);
  94. b = asin (body.radius / HC);
  95. p = 0.57 * (34.8 * f - Sp);
  96. intX [5] = intX [4] + 0.5 * Sp * cos (a + b) - p * sin (a + b);
  97. intY [5] = intY [4] + 0.5 * Sp * sin (a + b) + p * cos (a + b);
  98. HBody_x = body.x - intX [5];
  99. HBody_y = body.y - intY [5];
  100. HC = sqrt (HBody_x * HBody_x + HBody_y * HBody_y);
  101. if (HC <= body.radius) { HC = body.radius; Sp = 0.0; } else Sp = sqrt (HC * HC - body.radius * body.radius);
  102. a = atan2 (HBody_y, HBody_x);
  103. b = asin (body.radius / HC);
  104. intX [6] = intX [5] + Sp * cos (a + b);
  105. intY [6] = intY [5] + Sp * sin (a + b);
  106. /* Posterior blade. */
  107. teeth.a = speaker -> lowerTeeth.a + jaw.da;
  108. intX [7] = body.x + body.radius * cos (1.73 + teeth.a);
  109. intY [7] = body.y + body.radius * sin (1.73 + teeth.a);
  110. /* Tip. */
  111. tip.a = (art [(int) kArt_muscle::UPPER_TONGUE]
  112. - art [(int) kArt_muscle::LOWER_TONGUE]) * 1.0;
  113. blade.a = teeth.a
  114. + 0.004 * (body.r - speaker -> neutralBodyDistance) + tip.a;
  115. intX [8] = intX [7] + speaker -> tip.length * cos (blade.a);
  116. intY [8] = intY [7] + speaker -> tip.length * sin (blade.a);
  117. /* Jaw. */
  118. teeth.r = speaker -> lowerTeeth.r;
  119. teeth.x = jaw.x + teeth.r * cos (teeth.a);
  120. teeth.y = jaw.y + teeth.r * sin (teeth.a);
  121. intX [9] = teeth.x + speaker -> teethCavity.dx1;
  122. intY [9] = teeth.y + speaker -> teethCavity.dy;
  123. intX [10] = teeth.x + speaker -> teethCavity.dx2;
  124. intY [10] = intY [9];
  125. intX [11] = teeth.x;
  126. intY [11] = teeth.y;
  127. /* Lower lip. */
  128. lowerLip.dx = speaker -> lowerLip.dx + art [(int) kArt_muscle::ORBICULARIS_ORIS] * 0.02 - 5e-3;
  129. lowerLip.dy = speaker -> lowerLip.dy + art [(int) kArt_muscle::ORBICULARIS_ORIS] * 0.01;
  130. intX [12] = teeth.x;
  131. intY [12] = teeth.y + lowerLip.dy;
  132. intX [13] = teeth.x + lowerLip.dx;
  133. intY [13] = intY [12];
  134. /* Velum. */
  135. extX [6] = speaker -> velum.x;
  136. extY [6] = speaker -> velum.y;
  137. /* Palate. */
  138. extX [7] = speaker -> alveoli.x;
  139. extY [7] = speaker -> alveoli.y;
  140. extX [8] = speaker -> upperTeeth.x;
  141. extY [8] = speaker -> upperTeeth.y;
  142. /* Upper lip. */
  143. upperLip.dx = speaker -> upperLip.dx + art [(int) kArt_muscle::ORBICULARIS_ORIS] * 0.02 - 5e-3;
  144. upperLip.dy = speaker -> upperLip.dy - art [(int) kArt_muscle::ORBICULARIS_ORIS] * 0.01;
  145. extX [9] = extX [8];
  146. extY [9] = extY [8] + upperLip.dy;
  147. extX [10] = extX [9] + upperLip.dx;
  148. extY [10] = extY [9];
  149. extX [11] = extX [10] + 5e-3;
  150. extY [11] = extY [10] + DLIP;
  151. /* Chin. */
  152. intX [14] = intX [13] + 5e-3;
  153. intY [14] = intY [13] - DLIP;
  154. intX [15] = intX [11] + 0.5e-2;
  155. intY [15] = intY [11] - 3.0e-2;
  156. intX [16] = intX [1];
  157. intY [16] = intY [1];
  158. }
  159. void Art_Speaker_draw (Art art, Speaker speaker, Graphics g) {
  160. double f = speaker -> relativeSize * 1e-3;
  161. double intX [1 + 16], intY [1 + 16], extX [1 + 11], extY [1 + 11];
  162. double bodyX, bodyY;
  163. Graphics_Viewport previous;
  164. Art_Speaker_toVocalTract (art, speaker, intX, intY, extX, extY, & bodyX, & bodyY);
  165. previous = Graphics_insetViewport (g, 0.1, 0.9, 0.1, 0.9);
  166. Graphics_setWindow (g, -0.05, 0.05, -0.05, 0.05);
  167. /* Draw inner contour. */
  168. for (integer i = 1; i <= 5; i ++)
  169. Graphics_line (g, intX [i], intY [i], intX [i + 1], intY [i + 1]);
  170. Graphics_arc (g, bodyX, bodyY, 20.0 * f,
  171. atan2 (intY [7] - bodyY, intX [7] - bodyX) * (180.0 / NUMpi),
  172. atan2 (intY [6] - bodyY, intX [6] - bodyX) * (180.0 / NUMpi));
  173. for (integer i = 7; i <= 15; i ++)
  174. Graphics_line (g, intX [i], intY [i], intX [i + 1], intY [i + 1]);
  175. /* Draw outer contour. */
  176. for (integer i = 1; i <= 5; i ++)
  177. Graphics_line (g, extX [i], extY [i], extX [i + 1], extY [i + 1]);
  178. Graphics_arc (g, 0.0, 0.0, speaker -> palate.radius,
  179. speaker -> alveoli.a * (180.0 / NUMpi),
  180. speaker -> velum.a * (180.0 / NUMpi));
  181. for (integer i = 7; i <= 10; i ++)
  182. Graphics_line (g, extX [i], extY [i], extX [i + 1], extY [i + 1]);
  183. Graphics_resetViewport (g, previous);
  184. }
  185. void Art_Speaker_fillInnerContour (Art art, Speaker speaker, Graphics g) {
  186. double f = speaker -> relativeSize * 1e-3;
  187. double intX [1 + 16], intY [1 + 16], extX [1 + 11], extY [1 + 11];
  188. double x [1 + 16], y [1 + 16];
  189. double bodyX, bodyY;
  190. Graphics_Viewport previous;
  191. Art_Speaker_toVocalTract (art, speaker, intX, intY, extX, extY, & bodyX, & bodyY);
  192. previous = Graphics_insetViewport (g, 0.1, 0.9, 0.1, 0.9);
  193. Graphics_setWindow (g, -0.05, 0.05, -0.05, 0.05);
  194. for (integer i = 1; i <= 16; i ++) {
  195. x [i] = intX [i];
  196. y [i] = intY [i];
  197. }
  198. Graphics_setGrey (g, 0.8);
  199. Graphics_fillArea (g, 16, & x [1], & y [1]);
  200. Graphics_fillCircle (g, bodyX, bodyY, 20.0 * f);
  201. Graphics_setGrey (g, 0.0);
  202. Graphics_resetViewport (g, previous);
  203. }
  204. static double arcLength (double from, double to) {
  205. double result = to - from;
  206. while (result > 0.0) result -= 2.0 * NUMpi;
  207. while (result < 0.0) result += 2.0 * NUMpi;
  208. return result;
  209. }
  210. static int Art_Speaker_meshCount = 27;
  211. static double bodyX, bodyY, bodyRadius;
  212. static double toLine (double x, double y, const double intX [], const double intY [], integer i) {
  213. integer nearby;
  214. if (i == 6) {
  215. double a7 = atan2 (intY [7] - bodyY, intX [7] - bodyX);
  216. double a6 = atan2 (intY [6] - bodyY, intX [6] - bodyX);
  217. double a = atan2 (y - bodyY, x - bodyX);
  218. double da6 = arcLength (a7, a6);
  219. double da = arcLength (a7, a);
  220. if (da <= da6)
  221. return fabs (sqrt ((bodyX - x) * (bodyX - x) + (bodyY - y) * (bodyY - y)) - bodyRadius);
  222. else
  223. nearby = arcLength (a7 + 0.5 * da6, a) < NUMpi ? 6 : 7;
  224. } else if ((x - intX [i]) * (intX [i + 1] - intX [i]) +
  225. (y - intY [i]) * (intY [i + 1] - intY [i]) < 0) {
  226. nearby = i;
  227. } else if ((x - intX [i + 1]) * (intX [i] - intX [i + 1]) +
  228. (y - intY [i + 1]) * (intY [i] - intY [i + 1]) < 0) {
  229. nearby = i + 1;
  230. } else {
  231. double boundaryDistance =
  232. sqrt ((intX [i + 1] - intX [i]) * (intX [i + 1] - intX [i]) +
  233. (intY [i + 1] - intY [i]) * (intY [i + 1] - intY [i]));
  234. double outerProduct = (intX [i] - x) * (intY [i + 1] - intY [i]) - (intY [i] - y) * (intX [i + 1] - intX [i]);
  235. return fabs (outerProduct) / boundaryDistance;
  236. }
  237. return sqrt ((intX [nearby] - x) * (intX [nearby] - x) + (intY [nearby] - y) * (intY [nearby] - y));
  238. }
  239. static int inside (double x, double y,
  240. const double intX [], const double intY [])
  241. {
  242. integer up = 0;
  243. for (integer i = 1; i <= 16 - 1; i ++)
  244. if ((y > intY [i]) != (y > intY [i + 1])) {
  245. double slope = (intX [i + 1] - intX [i]) / (intY [i + 1] - intY [i]);
  246. if (x > intX [i] + (y - intY [i]) * slope)
  247. up += ( y > intY [i] ? 1 : -1 );
  248. }
  249. return up != 0 || bodyRadius * bodyRadius >
  250. (x - bodyX) * (x - bodyX) + (y - bodyY) * (y - bodyY);
  251. }
  252. void Art_Speaker_meshVocalTract (Art art, Speaker speaker,
  253. double xi [], double yi [], double xe [], double ye [],
  254. double xmm [], double ymm [], bool closed [])
  255. {
  256. double f = speaker -> relativeSize * 1e-3;
  257. double intX [1 + 16], intY [1 + 16], extX [1 + 11], extY [1 + 11], d_angle;
  258. double xm [40], ym [40];
  259. Art_Speaker_toVocalTract (art, speaker, intX, intY, extX, extY, & bodyX, & bodyY);
  260. bodyRadius = 20.0 * f;
  261. xe [1] = extX [1]; // eq. 5.45
  262. ye [1] = extY [1];
  263. xe [2] = 0.2 * extX [2] + 0.8 * extX [1];
  264. ye [2] = 0.2 * extY [2] + 0.8 * extY [1];
  265. xe [3] = 0.6 * extX [2] + 0.4 * extX [1];
  266. ye [3] = 0.6 * extY [2] + 0.4 * extY [1];
  267. xe [4] = 0.9 * extX [3] + 0.1 * extX [4]; // eq. 5.46
  268. ye [4] = 0.9 * extY [3] + 0.1 * extY [4];
  269. xe [5] = 0.7 * extX [3] + 0.3 * extX [4];
  270. ye [5] = 0.7 * extY [3] + 0.3 * extY [4];
  271. xe [6] = 0.5 * extX [3] + 0.5 * extX [4];
  272. ye [6] = 0.5 * extY [3] + 0.5 * extY [4];
  273. xe [7] = 0.3 * extX [3] + 0.7 * extX [4];
  274. ye [7] = 0.3 * extY [3] + 0.7 * extY [4];
  275. xe [8] = 0.1 * extX [3] + 0.9 * extX [4];
  276. ye [8] = 0.1 * extY [3] + 0.9 * extY [4];
  277. xe [9] = 0.9 * extX [4] + 0.1 * extX [5];
  278. ye [9] = 0.9 * extY [4] + 0.1 * extY [5];
  279. xe [10] = 0.7 * extX [4] + 0.3 * extX [5];
  280. ye [10] = 0.7 * extY [4] + 0.3 * extY [5];
  281. xe [11] = 0.5 * extX [4] + 0.5 * extX [5];
  282. ye [11] = 0.5 * extY [4] + 0.5 * extY [5];
  283. xe [12] = 0.3 * extX [4] + 0.7 * extX [5];
  284. ye [12] = 0.3 * extY [4] + 0.7 * extY [5];
  285. xe [13] = 0.1 * extX [4] + 0.9 * extX [5];
  286. ye [13] = 0.1 * extY [4] + 0.9 * extY [5];
  287. d_angle = (atan2 (ye [13], xe [13]) - 0.5 * NUMpi) / 6; // eq. 5.47
  288. for (integer i = 14; i <= 18; i ++) {
  289. double a = 0.5 * NUMpi + (19 - i) * d_angle;
  290. xe [i] = speaker -> palate.radius * cos (a);
  291. ye [i] = speaker -> palate.radius * sin (a);
  292. }
  293. xe [19] = 0;
  294. ye [19] = speaker -> palate.radius;
  295. xe [20] = 0.25 * extX [7];
  296. xe [21] = 0.50 * extX [7];
  297. xe [22] = 0.75 * extX [7];
  298. for (integer i = 20; i <= 22; i ++) {
  299. ye [i] = speaker -> palate.radius * sqrt (1.0 - xe [i] * xe [i] /
  300. (speaker -> palate.radius * speaker -> palate.radius));
  301. }
  302. xe [23] = extX [7];
  303. ye [23] = extY [7];
  304. xe [24] = 0.5 * (extX [7] + extX [8]);
  305. ye [24] = 0.5 * (extY [7] + extY [8]);
  306. xe [25] = extX [8];
  307. ye [25] = extY [8];
  308. xe [26] = 0.25 * extX [11] + 0.75 * extX [9];
  309. xe [27] = 0.75 * extX [11] + 0.25 * extX [9];
  310. ye [26] = extY [10];
  311. ye [27] = 0.5 * (extY [10] + extY [11]);
  312. for (integer i = 1; i <= 27; i ++) { // every mesh point
  313. double minimum = 100000.0;
  314. for (integer j = 1; j <= 15 - 1; j ++) { // every internal segment
  315. double d = toLine (xe [i], ye [i], intX, intY, j);
  316. if (d < minimum) minimum = d;
  317. }
  318. if (( closed [i] = inside (xe [i], ye [i], intX, intY) ))
  319. minimum = - minimum;
  320. if (xe [i] >= 0.0) { // vertical line pieces
  321. xi [i] = xe [i];
  322. yi [i] = ye [i] - minimum;
  323. } else if (ye [i] <= 0.0) { // horizontal line pieces
  324. xi [i] = xe [i] + minimum;
  325. yi [i] = ye [i];
  326. } else { // radial line pieces, centre = centre of palate arc
  327. double angle = atan2 (ye [i], xe [i]);
  328. xi [i] = xe [i] - minimum * cos (angle);
  329. yi [i] = ye [i] - minimum * sin (angle);
  330. }
  331. }
  332. for (integer i = 1; i <= Art_Speaker_meshCount; i ++) {
  333. xm [i] = 0.5 * (xe [i] + xi [i]);
  334. ym [i] = 0.5 * (ye [i] + yi [i]);
  335. }
  336. for (integer i = 2; i <= Art_Speaker_meshCount; i ++) {
  337. xmm [i] = 0.5 * (xm [i - 1] + xm [i]);
  338. ymm [i] = 0.5 * (ym [i - 1] + ym [i]);
  339. }
  340. xmm [1] = 2.0 * xm [1] - xmm [2];
  341. ymm [1] = 2.0 * ym [1] - ymm [2];
  342. xmm [Art_Speaker_meshCount + 1] = 2.0 * xm [Art_Speaker_meshCount]
  343. - xmm [Art_Speaker_meshCount];
  344. ymm [Art_Speaker_meshCount + 1] = 2.0 * ym [Art_Speaker_meshCount]
  345. - ymm [Art_Speaker_meshCount];
  346. }
  347. void Art_Speaker_drawMesh (Art art, Speaker speaker, Graphics graphics) {
  348. double xi [40], yi [40], xe [40], ye [40], xmm [40], ymm [40];
  349. bool closed [40];
  350. int oldLineType = Graphics_inqLineType (graphics);
  351. Art_Speaker_meshVocalTract (art, speaker, xi, yi, xe, ye, xmm, ymm, closed);
  352. Graphics_Viewport previous = Graphics_insetViewport (graphics, 0.1, 0.9, 0.1, 0.9); // must be square
  353. Graphics_setWindow (graphics, -0.05, 0.05, -0.05, 0.05);
  354. /* Mesh lines. */
  355. for (integer i = 1; i <= Art_Speaker_meshCount; i ++)
  356. Graphics_line (graphics, xi [i], yi [i], xe [i], ye [i]);
  357. /* Radii. */
  358. Graphics_setLineType (graphics, Graphics_DOTTED);
  359. for (integer i = 1; i <= Art_Speaker_meshCount; i ++)
  360. if (xe [i] <= 0.0 && ye [i] >= 0.0)
  361. Graphics_line (graphics, 0.0, 0.0, 0.9 * xi [i], 0.9 * yi [i]);
  362. Graphics_setLineType (graphics, oldLineType);
  363. /* Lengths. */
  364. for (integer i = 1; i <= Art_Speaker_meshCount; i ++)
  365. Graphics_line (graphics, xmm [i], ymm [i], xmm [i + 1], ymm [i + 1]);
  366. for (integer i = 1; i <= Art_Speaker_meshCount + 1; i ++)
  367. Graphics_speckle (graphics, xmm [i], ymm [i]);
  368. Graphics_setTextAlignment (graphics, Graphics_LEFT, Graphics_HALF);
  369. Graphics_text (graphics, 0.0, 0.0, U"O"); // origin
  370. Graphics_resetViewport (graphics, previous);
  371. }
  372. /* End of file Art_Speaker.cpp */