viseme.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program 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 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** 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 program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <wwdebug.h>
  21. #include "Viseme.h"
  22. #define IS_VOWEL(x) ( x && (x=='a' || x=='e' || x=='i' || x=='o' || x=='u') )
  23. #define IS_CONSONANT(x) ( x && !IS_VOWEL(x) )
  24. struct VisemeTableItem
  25. {
  26. char *LetterCombination;
  27. char Visemes[2];
  28. };
  29. static VisemeTableItem gsVisemeTable[] =
  30. {
  31. {"air", VISEME_IF, -1},
  32. {"ar", VISEME_OX, -1},
  33. {"ch", VISEME_CHURCH, -1},
  34. {"ea", VISEME_EAT, -1},
  35. {"ee", VISEME_EAT, -1},
  36. {"er", VISEME_EARTH, -1},
  37. {"oa", VISEME_OX, -1},
  38. {"oo", VISEME_WET, -1},
  39. {"ou", VISEME_OX, VISEME_WET},
  40. {"ow", VISEME_WET},
  41. {"qu", VISEME_WET, -1},
  42. {"sch", VISEME_SIZE, VISEME_CAGE},
  43. {"sh", VISEME_CHURCH, -1},
  44. {"tch", VISEME_CHURCH, -1},
  45. {"th", VISEME_THOUGH, -1},
  46. {"gn", VISEME_NEW, -1},
  47. {"kn", VISEME_NEW, -1},
  48. {"eye", VISEME_IF, -1},
  49. {"uy", VISEME_IF, -1},
  50. {"ar", VISEME_OX, -1},
  51. {"kw", VISEME_WET, -1},
  52. {"and", VISEME_NEW, VISEME_TOLD},
  53. {"ze", VISEME_ROAR, VISEME_EAT}, // numbers: zero to nine
  54. {"ro", VISEME_WET, VISEME_OX},
  55. {"one", VISEME_WET, VISEME_CAT},
  56. {"two", VISEME_EAT, VISEME_WET},
  57. {"thr", VISEME_THOUGH, VISEME_ROAR},
  58. {"fou", VISEME_FAVE, VISEME_OX},
  59. {"ive", VISEME_IF, VISEME_FAVE},
  60. {"six", VISEME_UP, VISEME_IF},
  61. {"se", VISEME_UP, VISEME_EAT},
  62. {"ven", VISEME_FAVE, VISEME_EAT},
  63. {"eight", VISEME_CAT, VISEME_EAT},
  64. {"ni", VISEME_THOUGH, VISEME_IF},
  65. {"ne", VISEME_THOUGH, -1}, // end numbers
  66. };
  67. struct VisemeTableReferenceItem
  68. {
  69. int StartIndex;
  70. int Count;
  71. };
  72. #define NUM_VISEME_REFERENCES 26 // 'a' - 'z'
  73. static VisemeTableReferenceItem VisemeReferenceTable[NUM_VISEME_REFERENCES];
  74. //======================================================================================
  75. static int CompareVisemeTableItems( const void *arg1, const void *arg2 )
  76. {
  77. VisemeTableItem *p1 = (VisemeTableItem *)arg1;
  78. VisemeTableItem *p2 = (VisemeTableItem *)arg2;
  79. return strcmp(p1->LetterCombination, p2->LetterCombination);
  80. }
  81. //======================================================================================
  82. VisemeManager::VisemeManager(void)
  83. {
  84. // sort viseme lookup table
  85. int numVisemeTableItems = sizeof(gsVisemeTable) / sizeof(VisemeTableItem);
  86. qsort(gsVisemeTable, numVisemeTableItems, sizeof(VisemeTableItem), CompareVisemeTableItems);
  87. // build viseme lookup reference table
  88. memset(VisemeReferenceTable, 0, sizeof(VisemeReferenceTable));
  89. VisemeTableItem *pItem = gsVisemeTable;
  90. for (int i=0; i<numVisemeTableItems; i++,pItem++) {
  91. int index = (int)tolower(pItem->LetterCombination[0]) - 'a';
  92. if ( index >= 0 && index < NUM_VISEME_REFERENCES ) {
  93. VisemeTableReferenceItem *pR = &VisemeReferenceTable[index];
  94. if ( pR->Count == 0 ) {
  95. pR->StartIndex = i;
  96. }
  97. pR->Count++;
  98. }
  99. }
  100. }
  101. //======================================================================================
  102. // GetVisemes(const char *word, int *visemelist, int maxvisemes)
  103. // Input: word - word to parse
  104. // maxvisemes - max. number of visemes on the output list visemelist
  105. // Output: visemelist - contains viseme ID's
  106. // Remark: this routine examines word and identifies visemes and place them
  107. // on the given output list(visemelist)
  108. // Return: number of visemes on the output list
  109. //
  110. int VisemeManager::Get_Visemes(const char *word, int *visemelist, int maxvisemes) const
  111. {
  112. char prevchar = 0;
  113. int last_viseme = -1;
  114. int viseme[2];
  115. int i;
  116. int offset;
  117. int num_visemes = 0;
  118. char local_buf[128];
  119. // make a local copy of the word in lower case
  120. strncpy(local_buf, word, sizeof(local_buf)-1);
  121. local_buf[sizeof(local_buf)-1] = 0;
  122. _strlwr(local_buf);
  123. const char *pchar = local_buf;
  124. while ( *pchar ) {
  125. // check for tabled viseme combinations
  126. offset = Lookup(pchar, word, viseme);
  127. if ( offset == 0 ) {
  128. // analyse the current letter
  129. // init default viseme ID
  130. viseme[0] = VISEME_TOLD;
  131. viseme[1] = -1;
  132. offset = 1;
  133. switch (*pchar) {
  134. case 'a':
  135. offset = Do_Letter_a(pchar, prevchar, viseme);
  136. break;
  137. case 'b':
  138. viseme[0] = VISEME_BUMP;
  139. break;
  140. case 'c':
  141. viseme[0] = VISEME_CAGE;
  142. break;
  143. case 'd':
  144. viseme[0] = VISEME_TOLD;
  145. break;
  146. case 'e':
  147. offset = Do_Letter_e(pchar, prevchar, viseme);
  148. break;
  149. case 'f':
  150. viseme[0] = VISEME_FAVE;
  151. break;
  152. case 'g':
  153. viseme[0] = VISEME_CAGE;
  154. break;
  155. case 'h':
  156. // no sound
  157. break;
  158. case 'i':
  159. offset = Do_Letter_i(pchar, prevchar, viseme);
  160. break;
  161. case 'j':
  162. viseme[0] = VISEME_CHURCH;
  163. break;
  164. case 'k':
  165. viseme[0] = VISEME_CAGE;
  166. break;
  167. case 'l':
  168. viseme[0] = VISEME_THOUGH;
  169. break;
  170. case 'm':
  171. viseme[0] = VISEME_BUMP;
  172. break;
  173. case 'n':
  174. viseme[0] = VISEME_NEW;
  175. break;
  176. case 'o':
  177. offset = Do_Letter_o(pchar, prevchar, viseme);
  178. break;
  179. case 'p':
  180. viseme[0] = VISEME_BUMP;
  181. break;
  182. case 'q':
  183. viseme[0] = VISEME_CAGE;
  184. break;
  185. case 'r':
  186. viseme[0] = VISEME_ROAR;
  187. break;
  188. case 's':
  189. offset = Do_Letter_s(pchar, prevchar, viseme);
  190. break;
  191. case 't':
  192. offset = Do_Letter_t(pchar, prevchar, viseme);
  193. break;
  194. case 'u':
  195. offset = Do_Letter_u(pchar, prevchar, viseme);
  196. break;
  197. case 'v':
  198. viseme[0] = VISEME_FAVE;
  199. break;
  200. case 'w':
  201. viseme[0] = VISEME_WET;
  202. break;
  203. case 'x':
  204. viseme[0] = VISEME_CAGE;
  205. viseme[1] = VISEME_SIZE;
  206. break;
  207. case 'y':
  208. // geo-aug22/00 no viseme reference
  209. //viseme[0] = VISEME_WET;
  210. break;
  211. case 'z':
  212. viseme[0] = VISEME_SIZE;
  213. break;
  214. }
  215. }
  216. // add the newly found visemes to the list
  217. for (i=0; i<2; i++) {
  218. if ( viseme[i] >= 0 && viseme[i] != last_viseme ) {
  219. *visemelist++ = viseme[i];
  220. last_viseme = viseme[i];
  221. // check for list limitation
  222. if ( ++num_visemes >= maxvisemes ) {
  223. // limit reached
  224. return(num_visemes);
  225. }
  226. }
  227. }
  228. // next letter
  229. pchar += offset;
  230. prevchar = *(pchar - 1);
  231. }
  232. return(num_visemes);
  233. }
  234. //======================================================================================
  235. // return: number of bytes to jump over when done
  236. //
  237. int VisemeManager::Lookup(const char *pchar, const char * /*word*/, int viseme[]) const
  238. {
  239. int length = 0;
  240. char ch = (char)tolower(*pchar);
  241. int index = ch - 'a';
  242. if ( index < 0 || index >= NUM_VISEME_REFERENCES ) {
  243. // out of range
  244. return 0;
  245. }
  246. VisemeTableReferenceItem *pR = &VisemeReferenceTable[index];
  247. // we search backwards so that we can find the max. match first
  248. VisemeTableItem *pI = &gsVisemeTable[pR->StartIndex + pR->Count - 1];
  249. for (int i=0; i<pR->Count; i++,pI--) {
  250. length = strlen(pI->LetterCombination);
  251. if ( strnicmp(pchar, pI->LetterCombination, length) == 0 )
  252. {
  253. // found!
  254. viseme[0] = pI->Visemes[0];
  255. viseme[1] = pI->Visemes[1];
  256. return length;
  257. }
  258. }
  259. return 0;
  260. }
  261. //======================================================================================
  262. // return: number of bytes to jump over when done
  263. //
  264. int VisemeManager::Do_Letter_a(const char *pchar, char prevchar, int *viseme) const
  265. {
  266. int offset = 1; // default
  267. // default as in "a" or "about"
  268. viseme[0] = VISEME_TOLD;
  269. viseme[1] = -1;
  270. if ( IS_VOWEL(pchar[1]) ) {
  271. offset++;
  272. switch (pchar[1]) {
  273. case 'i': // ai
  274. viseme[1] = VISEME_EAT; // default: e.g. plaid, aim, maize, train
  275. if ( pchar[2] == 's' ) {
  276. // as in "aisle"
  277. viseme[0] = VISEME_CAT;
  278. }
  279. break;
  280. case 'o': // ao
  281. // as in "gaol"
  282. viseme[1] = VISEME_EAT;
  283. break;
  284. case 'u': // au
  285. switch (prevchar) {
  286. case 'g': // as in "gauge"
  287. viseme[1] = VISEME_EAT;
  288. break;
  289. case 'l': // as in "laugh"
  290. viseme[0] = VISEME_CAT;
  291. break;
  292. case 's': // as in "sauerkraut"
  293. viseme[0] = VISEME_CAT;
  294. viseme[1] = VISEME_WET;
  295. break;
  296. default: // as in "daughter"
  297. viseme[0] = VISEME_CAT;
  298. }
  299. break;
  300. }
  301. }
  302. else {
  303. if ( IS_VOWEL(pchar[2]) ) {
  304. // e.g. "ate", 12+2
  305. viseme[1] = VISEME_EAT;
  306. }
  307. else {
  308. // e.g. "add", 1
  309. viseme[0] = VISEME_CAT;
  310. }
  311. }
  312. return(offset);
  313. }
  314. //======================================================================================
  315. // return: number of bytes to jump over when done
  316. //
  317. int VisemeManager::Do_Letter_e(const char *pchar, char prevchar, int *viseme) const
  318. {
  319. int offset = 1;
  320. // default as in "e" or "be"
  321. viseme[0] = VISEME_EAT; // default, as in "beach"
  322. viseme[1] = -1;
  323. if ( IS_VOWEL(pchar[1]) ) {
  324. offset++;
  325. //viseme[0] = VISEME_EAT; // default, as in "beach"
  326. switch ( pchar[1] ) {
  327. case 'a': // ea
  328. if ( pchar[2] == 'u' ) {
  329. // as in "beauty"
  330. viseme[0] = VISEME_EAT;
  331. viseme[1] = VISEME_WET;
  332. offset++;
  333. }
  334. break;
  335. case 'i': // ei
  336. if ( prevchar == 'h' ) {
  337. // as in "height"
  338. viseme[0] = VISEME_CAT;
  339. viseme[1] = VISEME_EAT;
  340. }
  341. else if ( pchar[2] == 'g' ) {
  342. // as in "eight", "reign"
  343. viseme[0] = VISEME_TOLD;
  344. viseme[1] = VISEME_EAT;
  345. }
  346. else if ( pchar[2] == 'z' ) {
  347. // as in "seize"
  348. viseme[0] = VISEME_EAT;
  349. }
  350. else {// plain old "ei"
  351. // default:
  352. viseme[0] = VISEME_IF;
  353. }
  354. break;
  355. case 'o': // eo
  356. if ( prevchar == 'p' ) {
  357. // as in "people"
  358. viseme[0] = VISEME_EAT;
  359. }
  360. else {
  361. // default:
  362. viseme[0] = VISEME_TOLD;
  363. }
  364. break;
  365. case 'u': // eu
  366. // as in "maneuver", "eulogy", "queue"
  367. viseme[0] = VISEME_WET;
  368. break;
  369. }
  370. }
  371. else {
  372. if ( prevchar == 'b' || pchar[1] == 0 ) {
  373. // as in "be"
  374. viseme[0] = VISEME_EAT;
  375. }
  376. else {
  377. // default as in "pet",
  378. viseme[0] = VISEME_WET;
  379. }
  380. }
  381. return(offset);
  382. }
  383. //======================================================================================
  384. // return: number of bytes to jump over when done
  385. //
  386. int VisemeManager::Do_Letter_i(const char *pchar, char prevchar, int *viseme) const
  387. {
  388. int offset = 1;
  389. // default as in "I"
  390. viseme[0] = VISEME_IF;
  391. viseme[1] = -1;
  392. if ( IS_VOWEL(pchar[1]) ) {
  393. offset++;
  394. viseme[0] = VISEME_TOLD; // default,
  395. viseme[1] = -1;
  396. switch ( pchar[1] ) {
  397. case 'e': // ie
  398. if ( pchar[2] == 0 && prevchar == 'l' ) {
  399. // as in "lie"
  400. viseme[0] = VISEME_CAT;
  401. viseme[1] = VISEME_EAT;
  402. }
  403. else if ( pchar[2] == 'u' || pchar[2] == 'w' ) {
  404. viseme[0] = VISEME_WET; // as in lieutenant or view
  405. offset++;
  406. }
  407. else {
  408. viseme[0] = VISEME_EAT; // as in grief
  409. }
  410. break;
  411. case 'o': // io
  412. viseme[0] = VISEME_EAT; // as in onion
  413. break;
  414. }
  415. }
  416. else {
  417. viseme[0] = VISEME_IF; // as in "pig"
  418. viseme[1] = -1;
  419. }
  420. return(offset);
  421. }
  422. //======================================================================================
  423. // return: number of bytes to jump over when done
  424. //
  425. int VisemeManager::Do_Letter_o(const char *pchar, char /*prevchar*/, int *viseme) const
  426. {
  427. int offset = 1;
  428. // default as in "no"
  429. viseme[0] = VISEME_OX;
  430. viseme[1] = -1;
  431. // geo-modified aug21/00
  432. //viseme[1] = VISEME_WET;
  433. if ( IS_VOWEL(pchar[1]) ) {
  434. offset++;
  435. viseme[0] = VISEME_UP; // default, as in "took"
  436. viseme[1] = -1;
  437. switch ( pchar[1] ) {
  438. case 'a': // oa
  439. if ( pchar[-2] == 'b' && pchar[-1] == 'r' ) {
  440. viseme[0] = VISEME_UP; // as in "broad"
  441. }
  442. else {
  443. viseme[0] = VISEME_OX; // as in "loan"
  444. viseme[1] = VISEME_WET;
  445. }
  446. break;
  447. case 'e': // oe
  448. viseme[0] = VISEME_WET; // default as in "canoe", "foe"
  449. if ( pchar[-1] == 'd' && pchar[2] == 's' ) {
  450. viseme[0] = VISEME_UP; // as in "does"
  451. }
  452. else if ( pchar[-2] == 'p' && pchar[-1] == 'h' ) {
  453. viseme[0] = VISEME_EAT; // as in "phoenix"
  454. }
  455. break;
  456. case 'i': // oi
  457. viseme[0] = VISEME_UP; // as in "noise"
  458. viseme[1] = VISEME_EAT;
  459. break;
  460. case 'o': // oo
  461. // GEO-modified aug21/00
  462. viseme[0] = VISEME_WET; // default as in "took", "look", "book"
  463. break;
  464. case 'u': // ou
  465. viseme[0] = VISEME_WET; // default as in "through", "though","croup"
  466. if ( pchar[-2] == 't' && pchar[-1] == 'r' ) {
  467. // as in "trouble", "trough"
  468. viseme[0] = VISEME_UP;
  469. }
  470. else if ( pchar[-2] == 't' && pchar[-1] == 'h' ) {
  471. if ( strncmp(&pchar[2], "ght", 3) == 0 ) {
  472. viseme[0] = VISEME_UP; // as in "thought"
  473. offset += 3;
  474. }
  475. }
  476. else if ( pchar[2] == 'r' ) {
  477. viseme[0] = VISEME_TOLD; // as in "journey"
  478. offset++;
  479. }
  480. break;
  481. }
  482. }
  483. else if ( pchar[1] ) {
  484. if ( IS_VOWEL(pchar[2]) ) {
  485. viseme[0] = VISEME_WET; // as in "move"
  486. viseme[1] = -1;
  487. }
  488. else {
  489. viseme[0] = VISEME_OX; // as in "comment"
  490. viseme[1] = -1;
  491. }
  492. }
  493. return(offset);
  494. }
  495. //======================================================================================
  496. // return: number of bytes to jump over when done
  497. //
  498. int VisemeManager::Do_Letter_s(const char *pchar, char /*prevchar*/, int *viseme) const
  499. {
  500. int offset = 1;
  501. // default as in "Say", "yourS"
  502. //viseme[0] = VISEME_TOLD;
  503. viseme[0] = VISEME_SIZE;
  504. viseme[1] = -1;
  505. // special cases
  506. if ( pchar[1] == 'h' || pchar[1] == 'u' ) {
  507. viseme[0] = VISEME_CHURCH; // e.g. "ship", "sure", "shoot"
  508. }
  509. else if ( pchar[1] == 'c' ) {
  510. if ( pchar[2] == 'h' ) {
  511. viseme[0] = VISEME_CHURCH; // e.g. "school"
  512. viseme[1] = VISEME_TOLD;
  513. offset = 3;
  514. }
  515. else {
  516. viseme[0] = VISEME_TOLD; // e.g. "scent"
  517. offset = 2;
  518. }
  519. }
  520. else if ( strncmp(&pchar[1], "eou", 3) == 0 ) {
  521. viseme[0] = VISEME_CHURCH; // e.g. "nauseous"
  522. offset = 4;
  523. }
  524. return(offset);
  525. }
  526. //======================================================================================
  527. // return: number of bytes to jump over when done
  528. //
  529. int VisemeManager::Do_Letter_t(const char *pchar, char /*prevchar*/, int *viseme) const
  530. {
  531. int offset = 1;
  532. // default as in "t"
  533. viseme[0] = VISEME_TOLD;
  534. // special cases
  535. if ( pchar[1] == 'c' ) {
  536. if ( pchar[2] == 'h' ) {
  537. viseme[0] = VISEME_CHURCH; // e.g. "catch"
  538. offset = 3;
  539. }
  540. }
  541. else if ( pchar[1] == 'h' ) {
  542. viseme[0] = VISEME_THOUGH; // e.g. "this","thin", "through", "then"
  543. offset = 2; // except for "thomas"
  544. }
  545. return(offset);
  546. }
  547. //======================================================================================
  548. // return: number of bytes to jump over when done
  549. //
  550. int VisemeManager::Do_Letter_u(const char *pchar, char /*prevchar*/, int *viseme) const
  551. {
  552. int offset = 1;
  553. // default as in "u" or "use"
  554. //viseme[0] = VISEME_EAT;
  555. //viseme[1] = VISEME_WET;
  556. // geo-modified aug21/00
  557. viseme[0] = VISEME_UP;
  558. if ( IS_VOWEL(pchar[1]) ) {
  559. offset++;
  560. switch ( pchar[1] ) {
  561. case 'e': // ue
  562. case 'i': // ui
  563. viseme[0] = VISEME_WET; // as in "blue" or "fruit"
  564. viseme[1] = -1;
  565. break;
  566. }
  567. }
  568. else {
  569. if ( IS_CONSONANT(pchar[2]) ) {
  570. viseme[0] = VISEME_UP; // as in "utter"
  571. viseme[1] = -1;
  572. }
  573. }
  574. return(offset);
  575. }
  576. //======================================================================================
  577. //======================================================================================
  578. //======================================================================================