Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "tier0/dbg.h"
  11. struct PhonemeMap_t
  12. {
  13. const char *string;
  14. int code;
  15. float weight;
  16. bool isStandard;
  17. const char *desc;
  18. };
  19. static PhonemeMap_t g_Phonemes[] =
  20. {
  21. { "b", 'b', 0.8f, true, "Big : voiced alveolar stop" },
  22. { "m", 'm', 1.0f, true, "Mat : voiced bilabial nasal" },
  23. { "p", 'p', 0.8f, true, "Put; voiceless alveolar stop" },
  24. { "w", 'w', 1.0f, true, "With : voiced labial-velar approximant" },
  25. { "f", 'f', 0.8f, true, "Fork : voiceless labiodental fricative" },
  26. { "v", 'v', 0.8f, true, "Val : voiced labialdental fricative" },
  27. { "r", 0x0279, 1.0f, true, "Red : voiced alveolar approximant" },
  28. { "r2", 'r', 1.0f, true, "Red : voiced alveolar trill" },
  29. { "r3", 0x027b, 1.0f, true, "Red : voiced retroflex approximant" },
  30. { "er", 0x025a, 1.2f, true, "URn : rhotacized schwa" },
  31. { "er2", 0x025d, 1.2f, true, "URn : rhotacized lower-mid central vowel" },
  32. { "dh", 0x00f0, 1.0f, true, "THen : voiced dental fricative" },
  33. { "th", 0x03b8, 1.0f, true, "THin : voiceless dental fricative" },
  34. { "sh", 0x0283, 1.0f, true, "SHe : voiceless postalveolar fricative" },
  35. { "jh", 0x02a4, 1.0f, true, "Joy : voiced postalveolar afficate" },
  36. { "ch", 0x02a7, 1.0f, true, "CHin : voiceless postalveolar affricate" },
  37. { "s", 's', 0.8f, true, "Sit : voiceless alveolar fricative" },
  38. { "z", 'z', 0.8f, true, "Zap : voiced alveolar fricative" },
  39. { "d", 'd', 0.8f, true, "Dig : voiced bilabial stop" },
  40. { "d2", 0x027e, 0.8f, true, "Dig : voiced alveolar flap or tap" },
  41. { "l", 'l', 0.8f, true, "Lid : voiced alveolar lateral approximant" },
  42. { "l2", 0x026b, 0.8f, true, "Lid : velarized voiced alveolar lateral approximant" },
  43. { "n", 'n', 0.8f, true, "No : voiced alveolar nasal" },
  44. { "t", 't', 0.8f, true, "Talk : voiceless bilabial stop" },
  45. { "ow", 'o', 1.2f, true, "gO : upper-mid back rounded vowel" },
  46. { "uw", 'u', 1.2f, true, "tOO : high back rounded vowel" },
  47. { "ey", 'e', 1.0f, true, "Ate : upper-mid front unrounded vowel" },
  48. { "ae", 0x00e6, 1.0f, true, "cAt : semi-low front unrounded vowel" },
  49. { "aa", 0x0251, 1.0f, true, "fAther : low back unrounded vowel" },
  50. { "aa2", 'a', 1.0f, true, "fAther : low front unrounded vowel" },
  51. { "iy", 'i', 1.0f, true, "fEEl : high front unrounded vowel" },
  52. { "y", 'j', 0.7f, true, "Yacht : voiced palatal approximant" },
  53. { "ah", 0x028c, 1.0f, true, "cUt : lower-mid back unrounded vowel" },
  54. { "ao", 0x0254, 1.2f, true, "dOg : lower-mid back rounded vowel" },
  55. { "ax", 0x0259, 1.0f, true, "Ago : mid-central unrounded vowel" },
  56. { "ax2", 0x025c, 1.0f, true, "Ago : lower-mid central unrounded vowel" },
  57. { "eh", 0x025b, 1.0f, true, "pEt : lower-mid front unrounded vowel"},
  58. { "ih", 0x026a, 1.0f, true, "fIll : semi-high front unrounded vowel" },
  59. { "ih2", 0x0268, 1.0f, true, "fIll : high central unrounded vowel" },
  60. { "uh", 0x028a, 1.0f, true, "bOOk : semi-high back rounded vowel" },
  61. { "g", 'g', 0.8f, true, "taG : voiced velar stop" },
  62. { "g2", 0x0261, 1.0f, true, "taG : voiced velar stop" },
  63. { "hh", 'h', 0.8f, true, "Help : voiceless glottal fricative" },
  64. { "hh2", 0x0266, 0.8f, true, "Help : breathy-voiced glottal fricative" },
  65. { "c", 'k', 0.6f, true, "Cut : voiceless velar stop" },
  66. { "nx", 0x014b, 1.0f, true, "siNG : voiced velar nasal" },
  67. { "zh", 0x0292, 1.0f, true, "aZure : voiced postalveolar fricative" },
  68. // Added
  69. { "h", 'h', 0.8f, false, "Help : voiceless glottal fricative" },
  70. { "k", 'k', 0.6f, false, "Cut : voiceless velar stop" },
  71. { "ay", 0x0251, 1.0f, false, "fAther : low back unrounded vowel" }, // or possibly +0x026a (ih)
  72. { "ng", 0x014b, 1.0f, false, "siNG : voiced velar nasal" }, // nx
  73. { "aw", 0x0251, 1.2f, false, "fAther : low back unrounded vowel" }, // // vOWel, // aa + uh???
  74. { "oy", 'u', 1.2f, false, "tOO : high back rounded vowel" },
  75. // Silence
  76. { "<sil>", '_', 1.0f, true, "silence" },
  77. };
  78. //-----------------------------------------------------------------------------
  79. // Purpose:
  80. // Input : code -
  81. // Output : const char
  82. //-----------------------------------------------------------------------------
  83. const char *ConvertPhoneme( int code )
  84. {
  85. for ( int i = 0; i < ARRAYSIZE( g_Phonemes ); ++i )
  86. {
  87. PhonemeMap_t *test = &g_Phonemes[ i ];
  88. if ( test->code == code )
  89. return test->string;
  90. }
  91. Warning( "Unrecognized phoneme code %i\n", code );
  92. return "<sil>";
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose:
  96. // Input : *text -
  97. // Output : int
  98. //-----------------------------------------------------------------------------
  99. int TextToPhoneme( const char *text )
  100. {
  101. for ( int i = 0; i < ARRAYSIZE( g_Phonemes ); ++i )
  102. {
  103. PhonemeMap_t *test = &g_Phonemes[ i ];
  104. if ( !stricmp( test->string, text ) )
  105. return test->code;
  106. }
  107. Warning( "Unrecognized phoneme %s\n", text );
  108. return '_';
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Purpose:
  112. // Input : code -
  113. // Output : float
  114. //-----------------------------------------------------------------------------
  115. float WeightForPhonemeCode( int code )
  116. {
  117. for ( int i = 0; i < ARRAYSIZE( g_Phonemes ); ++i )
  118. {
  119. PhonemeMap_t *test = &g_Phonemes[ i ];
  120. if ( test->code == code )
  121. return test->weight;
  122. }
  123. Warning( "Unrecognized phoneme code %i\n", code );
  124. return 1.0f;
  125. }
  126. //-----------------------------------------------------------------------------
  127. // Purpose:
  128. // Input : *text -
  129. // Output : float
  130. //-----------------------------------------------------------------------------
  131. float WeightForPhoneme( char *text )
  132. {
  133. for ( int i = 0; i < ARRAYSIZE( g_Phonemes ); ++i )
  134. {
  135. PhonemeMap_t *test = &g_Phonemes[ i ];
  136. if ( !stricmp( test->string, text ) )
  137. return test->weight;
  138. }
  139. Warning( "WeightForPhoneme:: Unrecognized phoneme %s\n", text );
  140. return 1.0f;
  141. }
  142. int NumPhonemes()
  143. {
  144. return ARRAYSIZE( g_Phonemes );
  145. }
  146. const char *NameForPhonemeByIndex( int index )
  147. {
  148. Assert( index >= 0 && index < NumPhonemes() );
  149. return g_Phonemes[ index ].string;
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Purpose:
  153. // Input : *text -
  154. // Output : int
  155. //-----------------------------------------------------------------------------
  156. int TextToPhonemeIndex( const char *text )
  157. {
  158. for ( int i = 0; i < ARRAYSIZE( g_Phonemes ); ++i )
  159. {
  160. PhonemeMap_t *test = &g_Phonemes[ i ];
  161. if ( !stricmp( test->string, text ) )
  162. return i;
  163. }
  164. return -1;
  165. }
  166. int CodeForPhonemeByIndex( int index )
  167. {
  168. if ( index < 0 || index >= NumPhonemes() )
  169. return '_';
  170. return g_Phonemes[ index ].code;
  171. }
  172. bool IsStandardPhoneme( int index )
  173. {
  174. if ( index < 0 || index >= NumPhonemes() )
  175. return false;
  176. return g_Phonemes[ index ].isStandard;
  177. }
  178. const char *DescForPhonemeByIndex( int index )
  179. {
  180. if ( index < 0 || index >= NumPhonemes() )
  181. return NULL;
  182. return g_Phonemes[ index ].desc;
  183. }