eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
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.

intonation.c 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*
  2. * Copyright (C) 2005 to 2007 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015 Reece H. Dunn
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <wctype.h>
  24. #if HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #include "speak_lib.h"
  28. #include "speech.h"
  29. #include "phoneme.h"
  30. #include "synthesize.h"
  31. #include "voice.h"
  32. #include "translate.h"
  33. /* Note this module is mostly old code that needs to be rewritten to
  34. provide a more flexible intonation system.
  35. */
  36. // bits in SYLLABLE.flags
  37. #define SYL_RISE 1
  38. #define SYL_EMPHASIS 2
  39. #define SYL_END_CLAUSE 4
  40. typedef struct {
  41. char stress;
  42. char env;
  43. char flags; //bit 0=pitch rising, bit1=emnphasized, bit2=end of clause
  44. char nextph_type;
  45. unsigned char pitch1;
  46. unsigned char pitch2;
  47. } SYLLABLE;
  48. static SYLLABLE *syllable_tab;
  49. static int tone_pitch_env; /* used to return pitch envelope */
  50. /* Pitch data for tone types */
  51. /*****************************/
  52. #define PITCHfall 0
  53. #define PITCHrise 2
  54. #define PITCHfrise 4 // and 3 must be for the variant preceded by 'r'
  55. #define PITCHfrise2 6 // and 5 must be the 'r' variant
  56. #define PITCHrisefall 8
  57. /* 0 fall */
  58. unsigned char env_fall[128] = {
  59. 0xff, 0xfd, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0, 0xee, 0xec, 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0,
  60. 0xde, 0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0, 0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4, 0xc2, 0xc0,
  61. 0xbe, 0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0, 0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4, 0xa2, 0xa0,
  62. 0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90, 0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
  63. 0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70, 0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
  64. 0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50, 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
  65. 0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30, 0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
  66. 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00
  67. };
  68. /* 1 rise */
  69. unsigned char env_rise[128] = {
  70. 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
  71. 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
  72. 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
  73. 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
  74. 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
  75. 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
  76. 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
  77. 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfd, 0xff
  78. };
  79. unsigned char env_frise[128] = {
  80. 0xff, 0xf4, 0xea, 0xe0, 0xd6, 0xcc, 0xc3, 0xba, 0xb1, 0xa8, 0x9f, 0x97, 0x8f, 0x87, 0x7f, 0x78,
  81. 0x71, 0x6a, 0x63, 0x5c, 0x56, 0x50, 0x4a, 0x44, 0x3f, 0x39, 0x34, 0x2f, 0x2b, 0x26, 0x22, 0x1e,
  82. 0x1a, 0x17, 0x13, 0x10, 0x0d, 0x0b, 0x08, 0x06, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x13, 0x15, 0x17,
  84. 0x1a, 0x1d, 0x1f, 0x22, 0x25, 0x28, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x39, 0x3b, 0x3d, 0x40,
  85. 0x42, 0x45, 0x47, 0x4a, 0x4c, 0x4f, 0x51, 0x54, 0x57, 0x5a, 0x5d, 0x5f, 0x62, 0x65, 0x68, 0x6b,
  86. 0x6e, 0x71, 0x74, 0x78, 0x7b, 0x7e, 0x81, 0x85, 0x88, 0x8b, 0x8f, 0x92, 0x96, 0x99, 0x9d, 0xa0,
  87. 0xa4, 0xa8, 0xac, 0xaf, 0xb3, 0xb7, 0xbb, 0xbf, 0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7, 0xdb, 0xe0
  88. };
  89. static unsigned char env_r_frise[128] = {
  90. 0xcf, 0xcc, 0xc9, 0xc6, 0xc3, 0xc0, 0xbd, 0xb9, 0xb4, 0xb0, 0xab, 0xa7, 0xa2, 0x9c, 0x97, 0x92,
  91. 0x8c, 0x86, 0x81, 0x7b, 0x75, 0x6f, 0x69, 0x63, 0x5d, 0x57, 0x50, 0x4a, 0x44, 0x3e, 0x38, 0x33,
  92. 0x2d, 0x27, 0x22, 0x1c, 0x17, 0x12, 0x0d, 0x08, 0x04, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x0a, 0x0c, 0x0d, 0x0f, 0x12, 0x14, 0x16,
  94. 0x19, 0x1b, 0x1e, 0x21, 0x24, 0x27, 0x2a, 0x2d, 0x30, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3f, 0x41,
  95. 0x43, 0x46, 0x48, 0x4b, 0x4d, 0x50, 0x52, 0x55, 0x58, 0x5a, 0x5d, 0x60, 0x63, 0x66, 0x69, 0x6c,
  96. 0x6f, 0x72, 0x75, 0x78, 0x7b, 0x7e, 0x81, 0x85, 0x88, 0x8b, 0x8f, 0x92, 0x96, 0x99, 0x9d, 0xa0,
  97. 0xa4, 0xa8, 0xac, 0xaf, 0xb3, 0xb7, 0xbb, 0xbf, 0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7, 0xdb, 0xe0
  98. };
  99. static unsigned char env_frise2[128] = {
  100. 0xff, 0xf9, 0xf4, 0xee, 0xe9, 0xe4, 0xdf, 0xda, 0xd5, 0xd0, 0xcb, 0xc6, 0xc1, 0xbd, 0xb8, 0xb3,
  101. 0xaf, 0xaa, 0xa6, 0xa1, 0x9d, 0x99, 0x95, 0x90, 0x8c, 0x88, 0x84, 0x80, 0x7d, 0x79, 0x75, 0x71,
  102. 0x6e, 0x6a, 0x67, 0x63, 0x60, 0x5d, 0x59, 0x56, 0x53, 0x50, 0x4d, 0x4a, 0x47, 0x44, 0x41, 0x3e,
  103. 0x3c, 0x39, 0x37, 0x34, 0x32, 0x2f, 0x2d, 0x2b, 0x28, 0x26, 0x24, 0x22, 0x20, 0x1e, 0x1c, 0x1a,
  104. 0x19, 0x17, 0x15, 0x14, 0x12, 0x11, 0x0f, 0x0e, 0x0d, 0x0c, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05,
  105. 0x05, 0x04, 0x03, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x03, 0x04, 0x04, 0x05, 0x06, 0x07, 0x08,
  107. 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x0f, 0x10, 0x12, 0x13, 0x15, 0x17, 0x18, 0x1a, 0x1c, 0x1e, 0x20
  108. };
  109. static unsigned char env_r_frise2[128] = {
  110. 0xd0, 0xce, 0xcd, 0xcc, 0xca, 0xc8, 0xc7, 0xc5, 0xc3, 0xc1, 0xc0, 0xbd, 0xbb, 0xb8, 0xb5, 0xb3,
  111. 0xb0, 0xad, 0xaa, 0xa7, 0xa3, 0xa0, 0x9d, 0x99, 0x96, 0x92, 0x8f, 0x8b, 0x87, 0x84, 0x80, 0x7c,
  112. 0x78, 0x74, 0x70, 0x6d, 0x69, 0x65, 0x61, 0x5d, 0x59, 0x55, 0x51, 0x4d, 0x4a, 0x46, 0x42, 0x3e,
  113. 0x3b, 0x37, 0x34, 0x31, 0x2f, 0x2d, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20, 0x1e, 0x1c, 0x1a, 0x19,
  114. 0x17, 0x15, 0x14, 0x12, 0x11, 0x0f, 0x0e, 0x0d, 0x0c, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x05,
  115. 0x04, 0x03, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x03, 0x04, 0x04, 0x05, 0x06, 0x07, 0x08,
  117. 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x0f, 0x10, 0x12, 0x13, 0x15, 0x17, 0x18, 0x1a, 0x1c, 0x1e, 0x20
  118. };
  119. static unsigned char env_risefall[128] = {
  120. 0x98, 0x99, 0x99, 0x9a, 0x9c, 0x9d, 0x9f, 0xa1, 0xa4, 0xa7, 0xa9, 0xac, 0xb0, 0xb3, 0xb6, 0xba,
  121. 0xbe, 0xc1, 0xc5, 0xc9, 0xcd, 0xd1, 0xd4, 0xd8, 0xdc, 0xdf, 0xe3, 0xe6, 0xea, 0xed, 0xf0, 0xf2,
  122. 0xf5, 0xf7, 0xf9, 0xfb, 0xfc, 0xfd, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfd,
  123. 0xfb, 0xfa, 0xf8, 0xf6, 0xf3, 0xf1, 0xee, 0xec, 0xe9, 0xe6, 0xe4, 0xe0, 0xdd, 0xda, 0xd7, 0xd3,
  124. 0xd0, 0xcc, 0xc8, 0xc4, 0xc0, 0xbc, 0xb8, 0xb4, 0xb0, 0xac, 0xa7, 0xa3, 0x9f, 0x9a, 0x96, 0x91,
  125. 0x8d, 0x88, 0x84, 0x7f, 0x7b, 0x76, 0x72, 0x6d, 0x69, 0x65, 0x60, 0x5c, 0x58, 0x54, 0x50, 0x4c,
  126. 0x48, 0x44, 0x40, 0x3c, 0x39, 0x35, 0x32, 0x2f, 0x2b, 0x28, 0x26, 0x23, 0x20, 0x1d, 0x1a, 0x17,
  127. 0x15, 0x12, 0x0f, 0x0d, 0x0a, 0x08, 0x07, 0x05, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  128. };
  129. static unsigned char env_rise2[128] = {
  130. 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04, 0x05, 0x06, 0x06,
  131. 0x07, 0x08, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
  132. 0x16, 0x17, 0x18, 0x19, 0x1b, 0x1c, 0x1d, 0x1f, 0x20, 0x22, 0x23, 0x25, 0x26, 0x28, 0x29, 0x2b,
  133. 0x2d, 0x2f, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, 0x40, 0x42, 0x44, 0x47, 0x49, 0x4b,
  134. 0x4e, 0x50, 0x52, 0x55, 0x57, 0x5a, 0x5d, 0x5f, 0x62, 0x65, 0x67, 0x6a, 0x6d, 0x70, 0x73, 0x76,
  135. 0x79, 0x7c, 0x7f, 0x82, 0x86, 0x89, 0x8c, 0x90, 0x93, 0x96, 0x9a, 0x9d, 0xa0, 0xa3, 0xa6, 0xa9,
  136. 0xac, 0xaf, 0xb2, 0xb5, 0xb8, 0xbb, 0xbe, 0xc1, 0xc4, 0xc7, 0xca, 0xcd, 0xd0, 0xd3, 0xd6, 0xd9,
  137. 0xdc, 0xdf, 0xe2, 0xe4, 0xe7, 0xe9, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfb, 0xfd
  138. };
  139. static unsigned char env_fall2[128] = {
  140. 0xfe, 0xfe, 0xfd, 0xfd, 0xfc, 0xfb, 0xfb, 0xfa, 0xfa, 0xf9, 0xf8, 0xf8, 0xf7, 0xf7, 0xf6, 0xf6,
  141. 0xf5, 0xf4, 0xf4, 0xf3, 0xf3, 0xf2, 0xf2, 0xf1, 0xf0, 0xf0, 0xef, 0xee, 0xee, 0xed, 0xec, 0xeb,
  142. 0xea, 0xea, 0xe9, 0xe8, 0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0, 0xde, 0xdd, 0xdc, 0xdb,
  143. 0xd9, 0xd8, 0xd6, 0xd5, 0xd3, 0xd2, 0xd0, 0xce, 0xcc, 0xcb, 0xc9, 0xc7, 0xc5, 0xc3, 0xc0, 0xbe,
  144. 0xbc, 0xb9, 0xb7, 0xb5, 0xb2, 0xaf, 0xad, 0xaa, 0xa7, 0xa4, 0xa1, 0x9e, 0x9a, 0x97, 0x94, 0x90,
  145. 0x8d, 0x89, 0x85, 0x81, 0x7d, 0x79, 0x75, 0x71, 0x6d, 0x68, 0x64, 0x61, 0x5e, 0x5b, 0x57, 0x54,
  146. 0x51, 0x4d, 0x4a, 0x46, 0x43, 0x40, 0x3c, 0x39, 0x35, 0x32, 0x2e, 0x2a, 0x27, 0x23, 0x1f, 0x1c,
  147. 0x18, 0x14, 0x11, 0x0d, 0x0b, 0x09, 0x07, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00
  148. };
  149. static unsigned char env_fallrise3[128] = {
  150. 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfd, 0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf1, 0xee, 0xeb,
  151. 0xe8, 0xe5, 0xe1, 0xde, 0xda, 0xd6, 0xd2, 0xcd, 0xc9, 0xc4, 0xbf, 0xba, 0xb6, 0xb0, 0xab, 0xa6,
  152. 0xa1, 0x9c, 0x96, 0x91, 0x8b, 0x86, 0x80, 0x7b, 0x75, 0x6f, 0x6a, 0x64, 0x5f, 0x59, 0x54, 0x4f,
  153. 0x49, 0x44, 0x3f, 0x3a, 0x35, 0x30, 0x2b, 0x26, 0x22, 0x1d, 0x19, 0x15, 0x11, 0x0d, 0x0a, 0x07,
  154. 0x04, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x04, 0x05,
  155. 0x07, 0x09, 0x0b, 0x0d, 0x10, 0x12, 0x15, 0x18, 0x1b, 0x1e, 0x22, 0x25, 0x29, 0x2d, 0x31, 0x35,
  156. 0x3a, 0x3e, 0x43, 0x48, 0x4c, 0x51, 0x57, 0x5b, 0x5e, 0x62, 0x65, 0x68, 0x6b, 0x6e, 0x71, 0x74,
  157. 0x76, 0x78, 0x7b, 0x7c, 0x7e, 0x80, 0x81, 0x82, 0x83, 0x83, 0x84, 0x84, 0x83, 0x83, 0x82, 0x81
  158. };
  159. static unsigned char env_fallrise4[128] = {
  160. 0x72, 0x72, 0x71, 0x71, 0x70, 0x6f, 0x6d, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x61, 0x5f, 0x5c, 0x5a,
  161. 0x57, 0x54, 0x51, 0x4e, 0x4b, 0x48, 0x45, 0x42, 0x3f, 0x3b, 0x38, 0x35, 0x32, 0x2f, 0x2c, 0x29,
  162. 0x26, 0x23, 0x20, 0x1d, 0x1b, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
  163. 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x06,
  164. 0x07, 0x07, 0x08, 0x09, 0x0a, 0x0c, 0x0d, 0x0f, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1b, 0x1d, 0x20,
  165. 0x23, 0x26, 0x29, 0x2c, 0x2f, 0x33, 0x37, 0x3b, 0x3f, 0x43, 0x47, 0x4c, 0x51, 0x56, 0x5b, 0x60,
  166. 0x65, 0x6a, 0x6f, 0x74, 0x79, 0x7f, 0x84, 0x89, 0x8f, 0x95, 0x9b, 0xa1, 0xa7, 0xad, 0xb3, 0xba,
  167. 0xc0, 0xc7, 0xce, 0xd5, 0xdc, 0xe3, 0xea, 0xf1, 0xf5, 0xf7, 0xfa, 0xfc, 0xfd, 0xfe, 0xff, 0xff
  168. };
  169. static unsigned char env_risefallrise[128] = {
  170. 0x7f, 0x7f, 0x7f, 0x80, 0x81, 0x83, 0x84, 0x87, 0x89, 0x8c, 0x8f, 0x92, 0x96, 0x99, 0x9d, 0xa1,
  171. 0xa5, 0xaa, 0xae, 0xb2, 0xb7, 0xbb, 0xc0, 0xc5, 0xc9, 0xcd, 0xd2, 0xd6, 0xda, 0xde, 0xe2, 0xe6,
  172. 0xea, 0xed, 0xf0, 0xf3, 0xf5, 0xf8, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xfe, 0xfd, 0xfc, 0xfb, 0xf9,
  173. 0xf7, 0xf4, 0xf0, 0xec, 0xe7, 0xe2, 0xdc, 0xd5, 0xce, 0xc6, 0xbd, 0xb4, 0xa9, 0x9e, 0x92, 0x88,
  174. 0x82, 0x7d, 0x77, 0x72, 0x6c, 0x66, 0x60, 0x5a, 0x54, 0x4e, 0x49, 0x42, 0x3c, 0x37, 0x32, 0x2d,
  175. 0x28, 0x24, 0x1f, 0x1b, 0x18, 0x14, 0x11, 0x0e, 0x0c, 0x09, 0x07, 0x06, 0x05, 0x04, 0x04, 0x04,
  176. 0x04, 0x05, 0x06, 0x08, 0x0a, 0x0d, 0x10, 0x14, 0x18, 0x1d, 0x23, 0x29, 0x2f, 0x37, 0x3e, 0x47,
  177. 0x50, 0x5a, 0x64, 0x70, 0x7c, 0x83, 0x85, 0x88, 0x8a, 0x8c, 0x8e, 0x8f, 0x91, 0x92, 0x93, 0x93
  178. };
  179. unsigned char *envelope_data[N_ENVELOPE_DATA] = {
  180. env_fall, env_fall,
  181. env_rise, env_rise,
  182. env_frise, env_r_frise,
  183. env_frise2, env_r_frise2,
  184. env_risefall, env_risefall,
  185. env_fallrise3, env_fallrise3,
  186. env_fallrise4, env_fallrise4,
  187. env_fall2, env_fall2,
  188. env_rise2, env_rise2,
  189. env_risefallrise, env_risefallrise
  190. };
  191. /* indexed by stress */
  192. static int min_drop[] = {6,7,9,9,20,20,20,25};
  193. // pitch change during the main part of the clause
  194. static int drops_0[8] = {9,9,16,16,16,23,55,32};
  195. // overflow table values are 64ths of the body pitch range (between body_start and body_end)
  196. static signed char oflow[] = {0, 40, 24, 8, 0};
  197. static signed char oflow_emf[] = {10, 52, 32, 20, 10};
  198. static signed char oflow_less[] = {6, 38, 24, 14, 4};
  199. #define N_TONE_HEAD_TABLE 13
  200. #define N_TONE_NUCLEUS_TABLE 13
  201. typedef struct {
  202. unsigned char pre_start;
  203. unsigned char pre_end;
  204. unsigned char body_start;
  205. unsigned char body_end;
  206. int *body_drops;
  207. unsigned char body_max_steps;
  208. char body_lower_u;
  209. unsigned char n_overflow;
  210. signed char *overflow;
  211. } TONE_HEAD;
  212. typedef struct {
  213. unsigned char pitch_env0; /* pitch envelope, tonic syllable at end */
  214. unsigned char tonic_max0;
  215. unsigned char tonic_min0;
  216. unsigned char pitch_env1; /* followed by unstressed */
  217. unsigned char tonic_max1;
  218. unsigned char tonic_min1;
  219. short *backwards;
  220. unsigned char tail_start;
  221. unsigned char tail_end;
  222. unsigned char flags;
  223. } TONE_NUCLEUS;
  224. #define T_EMPH 1
  225. static TONE_HEAD tone_head_table[N_TONE_HEAD_TABLE] = {
  226. {46, 57, 78, 50, drops_0, 3, 7, 5, oflow}, // 0 statement
  227. {46, 57, 78, 46, drops_0, 3, 7, 5, oflow}, // 1 comma
  228. {46, 57, 78, 46, drops_0, 3, 7, 5, oflow}, // 2 question
  229. {46, 57, 90, 50, drops_0, 3, 9, 5, oflow_emf}, // 3 exclamation
  230. {46, 57, 78, 50, drops_0, 3, 7, 5, oflow}, // 4 statement, emphatic
  231. {46, 57, 74, 55, drops_0, 4, 7, 5, oflow_less},// 5 statement, less intonation
  232. {46, 57, 74, 55, drops_0, 4, 7, 5, oflow_less},// 6 comma, less intonation
  233. {46, 57, 74, 55, drops_0, 4, 7, 5, oflow_less},// 7 comma, less intonation, less rise
  234. {46, 57, 78, 50, drops_0, 3, 7, 5, oflow}, // 8 pitch raises at end of sentence
  235. {46, 57, 78, 46, drops_0, 3, 7, 5, oflow}, // 9 comma
  236. {46, 57, 78, 50, drops_0, 3, 7, 5, oflow}, // 10 question
  237. {34, 41, 41, 32, drops_0, 3, 7, 5, oflow_less}, // 11 test
  238. {46, 57, 55, 50, drops_0, 3, 7, 5, oflow_less}, // 12 test
  239. };
  240. static TONE_NUCLEUS tone_nucleus_table[N_TONE_NUCLEUS_TABLE] = {
  241. {PITCHfall, 64, 8, PITCHfall, 70,18, NULL, 24, 12, 0}, // 0 statement
  242. {PITCHfrise, 80,18, PITCHfrise2, 78,22, NULL, 34, 52, 0}, // 1 comma
  243. {PITCHfrise, 88,22, PITCHfrise2, 82,22, NULL, 34, 64, 0}, // 2 question
  244. {PITCHfall, 92, 8, PITCHfall, 92,80, NULL, 76, 8, T_EMPH}, // 3 exclamation
  245. {PITCHfall, 86, 4, PITCHfall, 94,66, NULL, 34, 10, 0}, // 4 statement, emphatic
  246. {PITCHfall, 62,10, PITCHfall, 62,20, NULL, 28, 16, 0}, // 5 statement, less intonation
  247. {PITCHfrise, 68,18, PITCHfrise2, 68,22, NULL, 30, 44, 0}, // 6 comma, less intonation
  248. {PITCHfrise2, 64,16, PITCHfall, 66,32, NULL, 32, 18, 0}, // 7 comma, less intonation, less rise
  249. {PITCHrise, 68,46, PITCHfall, 42,32, NULL, 46, 58, 0}, // 8 pitch raises at end of sentence
  250. {PITCHfrise, 78,24, PITCHfrise2, 72,22, NULL, 42, 52, 0}, // 9 comma
  251. {PITCHfrise, 88,34, PITCHfall, 64,32, NULL, 46, 82, 0}, // 10 question
  252. {PITCHfall, 56,12, PITCHfall, 56,20, NULL, 24, 12, 0}, // 11 test
  253. {PITCHfall, 70,18, PITCHfall, 70,24, NULL, 32, 20, 0}, // 12 test
  254. };
  255. /* index by 0=. 1=, 2=?, 3=! 4=none, 5=emphasized */
  256. unsigned char punctuation_to_tone[INTONATION_TYPES][PUNCT_INTONATIONS] = {
  257. {0,1,2,3,0,4},
  258. {0,1,2,3,0,4},
  259. {5,6,2,3,0,4},
  260. {5,7,1,3,0,4},
  261. {8,9,10,3,0,0},
  262. {8,8,10,3,0,0},
  263. {11,11,11,11,0,0}, // 6 test
  264. {12,12,12,12,0,0}
  265. };
  266. int n_tunes = 0;
  267. TUNE *tunes = NULL;
  268. #define SECONDARY 3
  269. #define PRIMARY 4
  270. #define PRIMARY_STRESSED 6
  271. #define PRIMARY_LAST 7
  272. static int number_pre;
  273. static int number_body;
  274. static int number_tail;
  275. static int last_primary;
  276. static int tone_posn;
  277. static int tone_posn2;
  278. static int no_tonic;
  279. static void count_pitch_vowels(int start, int end, int clause_end)
  280. {
  281. int ix;
  282. int stress;
  283. int max_stress = 0;
  284. int max_stress_posn = 0; // last syllable ot the highest stress
  285. int max_stress_posn2 = 0; // penuntimate syllable of the highest stress
  286. number_pre = -1; /* number of vowels before 1st primary stress */
  287. number_body = 0;
  288. number_tail = 0; /* number between tonic syllable and next primary */
  289. last_primary = -1;
  290. for(ix=start; ix<end; ix++)
  291. {
  292. stress = syllable_tab[ix].stress; /* marked stress level */
  293. if(stress >= max_stress)
  294. {
  295. if(stress > max_stress)
  296. {
  297. max_stress_posn2 = ix;
  298. }
  299. else
  300. {
  301. max_stress_posn2 = max_stress_posn;
  302. }
  303. max_stress_posn = ix;
  304. max_stress = stress;
  305. }
  306. if(stress >= PRIMARY)
  307. {
  308. if(number_pre < 0)
  309. number_pre = ix - start;
  310. last_primary = ix;
  311. }
  312. }
  313. if(number_pre < 0)
  314. number_pre = end;
  315. number_tail = end - max_stress_posn - 1;
  316. tone_posn = max_stress_posn;
  317. tone_posn2 = max_stress_posn2;
  318. if(no_tonic)
  319. {
  320. tone_posn = tone_posn2 = end; // next position after the end of the truncated clause
  321. }
  322. else
  323. if(last_primary >= 0)
  324. {
  325. if(end == clause_end)
  326. {
  327. syllable_tab[last_primary].stress = PRIMARY_LAST;
  328. }
  329. }
  330. else
  331. {
  332. // no primary stress. Use the highest stress
  333. syllable_tab[tone_posn].stress = PRIMARY_LAST;
  334. }
  335. }
  336. /* Count number of primary stresses up to tonic syllable or body_reset */
  337. static int count_increments(int ix, int end_ix, int min_stress)
  338. {
  339. int count = 0;
  340. int stress;
  341. while(ix < end_ix)
  342. {
  343. stress = syllable_tab[ix++].stress;
  344. if(stress >= PRIMARY_LAST)
  345. break;
  346. if(stress >= min_stress)
  347. count++;
  348. }
  349. return(count);
  350. }
  351. // Set the pitch of a vowel in syllable_tab
  352. static void set_pitch(SYLLABLE *syl, int base, int drop)
  353. {
  354. int pitch1, pitch2;
  355. int flags = 0;
  356. if(base < 0) base = 0;
  357. pitch2 = base;
  358. if(drop < 0)
  359. {
  360. flags = SYL_RISE;
  361. drop = -drop;
  362. }
  363. pitch1 = pitch2 + drop;
  364. if(pitch1 < 0)
  365. pitch1 = 0;
  366. if(pitch1 > 254) pitch1 = 254;
  367. if(pitch2 > 254) pitch2 = 254;
  368. syl->pitch1 = pitch1;
  369. syl->pitch2 = pitch2;
  370. syl->flags |= flags;
  371. }
  372. static int CountUnstressed(int start, int end, int limit)
  373. {
  374. int ix;
  375. for(ix=start; ix <= end; ix++)
  376. {
  377. if(syllable_tab[ix].stress >= limit)
  378. break;
  379. }
  380. return(ix - start);
  381. }
  382. static int SetHeadIntonation(TUNE *tune, int syl_ix, int end_ix, int control)
  383. {
  384. int stress;
  385. SYLLABLE *syl;
  386. int ix;
  387. int pitch=0;
  388. int increment=0;
  389. int n_steps=0;
  390. int stage; // onset, head, last
  391. int initial;
  392. int overflow_ix=0;
  393. int pitch_range;
  394. int pitch_range_abs;
  395. int *drops;
  396. int n_unstressed=0;
  397. int unstressed_ix=0;
  398. int unstressed_inc;
  399. int used_onset = 0;
  400. int head_final = end_ix;
  401. int secondary=2; // 2
  402. pitch_range = (tune->head_end - tune->head_start) << 8;
  403. pitch_range_abs = abs(pitch_range);
  404. drops = drops_0; // this should be controled by tune->head_drops
  405. initial = 1;
  406. stage = 0;
  407. if(tune->onset == 255)
  408. stage = 1; // no onset specified
  409. if(tune->head_last != 255)
  410. {
  411. // find the last primary stress in the body
  412. for(ix = end_ix-1; ix >= syl_ix; ix--)
  413. {
  414. if(syllable_tab[ix].stress >= 4)
  415. {
  416. head_final = ix;
  417. break;
  418. }
  419. }
  420. }
  421. while(syl_ix < end_ix)
  422. {
  423. syl = &syllable_tab[syl_ix];
  424. stress = syl->stress;
  425. if(initial || (stress >= 4))
  426. {
  427. // a primary stress
  428. if((initial) || (stress == 5))
  429. {
  430. initial = 0;
  431. overflow_ix = 0;
  432. if(tune->onset == 255)
  433. {
  434. n_steps = count_increments(syl_ix, head_final, 4);
  435. pitch = tune->head_start << 8;
  436. }
  437. else
  438. {
  439. // a pitch has been specified for the onset syllable, don't include it in the pitch incrementing
  440. n_steps = count_increments(syl_ix+1, head_final, 4);
  441. pitch = tune->onset << 8;
  442. used_onset = 1;
  443. }
  444. if(n_steps > tune->head_max_steps)
  445. n_steps = tune->head_max_steps;
  446. if(n_steps > 1)
  447. {
  448. increment = pitch_range / (n_steps -1);
  449. }
  450. else
  451. increment = 0;
  452. }
  453. else
  454. if(syl_ix == head_final)
  455. {
  456. // a pitch has been specified for the last primary stress before the nucleus
  457. pitch = tune->head_last << 8;
  458. stage = 2;
  459. }
  460. else
  461. {
  462. if(used_onset)
  463. {
  464. stage = 1;
  465. used_onset = 0;
  466. pitch = tune->head_start << 8;
  467. n_steps++;
  468. }
  469. else
  470. if(n_steps > 0)
  471. pitch += increment;
  472. else
  473. {
  474. pitch = (tune->head_end << 8) + (pitch_range_abs * tune->head_extend[overflow_ix++])/64;
  475. if(overflow_ix >= tune->n_head_extend)
  476. {
  477. overflow_ix = 0;
  478. }
  479. }
  480. }
  481. n_steps--;
  482. }
  483. if(stress >= PRIMARY)
  484. {
  485. n_unstressed = CountUnstressed(syl_ix+1, end_ix, secondary);
  486. unstressed_ix = 0;
  487. syl->stress = PRIMARY_STRESSED;
  488. syl->env = tune->stressed_env;
  489. set_pitch(syl,(pitch >> 8), tune->stressed_drop);
  490. }
  491. else
  492. if(stress >= secondary)
  493. {
  494. n_unstressed = CountUnstressed(syl_ix+1, end_ix, secondary);
  495. unstressed_ix = 0;
  496. set_pitch(syl,(pitch >> 8),drops[stress]);
  497. }
  498. else
  499. {
  500. if(n_unstressed > 1)
  501. unstressed_inc = (tune->unstr_end[stage] - tune->unstr_start[stage]) / (n_unstressed - 1);
  502. else
  503. unstressed_inc = 0;
  504. set_pitch(syl, (pitch >> 8) + tune->unstr_start[stage] + (unstressed_inc * unstressed_ix), drops[stress]);
  505. unstressed_ix++;
  506. }
  507. syl_ix++;
  508. }
  509. return(syl_ix);
  510. }
  511. /* Calculate pitches until next RESET or tonic syllable, or end.
  512. Increment pitch if stress is >= min_stress.
  513. Used for tonic segment */
  514. static int calc_pitch_segment(int ix, int end_ix, TONE_HEAD *th, TONE_NUCLEUS *tn, int min_stress, int continuing)
  515. {
  516. int stress;
  517. int pitch=0;
  518. int increment=0;
  519. int n_primary=0;
  520. int n_steps=0;
  521. int initial;
  522. int overflow=0;
  523. int n_overflow;
  524. int pitch_range;
  525. int pitch_range_abs;
  526. int *drops;
  527. signed char *overflow_tab;
  528. SYLLABLE *syl;
  529. static signed char continue_tab[5] = {-26, 32, 20, 8, 0};
  530. drops = th->body_drops;
  531. pitch_range = (th->body_end - th->body_start) << 8;
  532. pitch_range_abs = abs(pitch_range);
  533. if(continuing)
  534. {
  535. initial =0;
  536. overflow = 0;
  537. n_overflow = 5;
  538. overflow_tab = continue_tab;
  539. increment = pitch_range / (th->body_max_steps -1);
  540. }
  541. else
  542. {
  543. n_overflow = th->n_overflow;
  544. overflow_tab = th->overflow;
  545. initial = 1;
  546. }
  547. while(ix < end_ix)
  548. {
  549. syl = &syllable_tab[ix];
  550. stress = syl->stress;
  551. if(initial || (stress >= min_stress))
  552. {
  553. // a primary stress
  554. if((initial) || (stress == 5))
  555. {
  556. initial = 0;
  557. overflow = 0;
  558. n_steps = n_primary = count_increments(ix,end_ix,min_stress);
  559. if(n_steps > th->body_max_steps)
  560. n_steps = th->body_max_steps;
  561. if(n_steps > 1)
  562. {
  563. increment = pitch_range / (n_steps -1);
  564. }
  565. else
  566. increment = 0;
  567. pitch = th->body_start << 8;
  568. }
  569. else
  570. {
  571. if(n_steps > 0)
  572. pitch += increment;
  573. else
  574. {
  575. pitch = (th->body_end << 8) + (pitch_range_abs * overflow_tab[overflow++])/64;
  576. if(overflow >= n_overflow)
  577. {
  578. overflow = 0;
  579. overflow_tab = th->overflow;
  580. }
  581. }
  582. }
  583. n_steps--;
  584. n_primary--;
  585. if((tn->backwards) && (n_primary < 2))
  586. {
  587. pitch = tn->backwards[n_primary] << 8;
  588. }
  589. }
  590. if(stress >= PRIMARY)
  591. {
  592. syl->stress = PRIMARY_STRESSED;
  593. set_pitch(syl,(pitch >> 8),drops[stress]);
  594. }
  595. else
  596. if(stress >= SECONDARY)
  597. {
  598. set_pitch(syl,(pitch >> 8),drops[stress]);
  599. }
  600. else
  601. {
  602. /* unstressed, drop pitch if preceded by PRIMARY */
  603. if((syllable_tab[ix-1].stress & 0x3f) >= SECONDARY)
  604. set_pitch(syl,(pitch >> 8) - th->body_lower_u, drops[stress]);
  605. else
  606. set_pitch(syl,(pitch >> 8),drops[stress]);
  607. }
  608. ix++;
  609. }
  610. return(ix);
  611. }
  612. static void SetPitchGradient(int start_ix, int end_ix, int start_pitch, int end_pitch)
  613. {
  614. // Set a linear pitch change over a number of syllables.
  615. // Used for pre-head, unstressed syllables in the body, and the tail
  616. int ix;
  617. int stress;
  618. int pitch;
  619. int increment;
  620. int n_increments;
  621. int drop;
  622. SYLLABLE *syl;
  623. increment = (end_pitch - start_pitch) << 8;
  624. n_increments = end_ix - start_ix;
  625. if(n_increments <= 0)
  626. return;
  627. if(n_increments > 1)
  628. {
  629. increment = increment / n_increments;
  630. }
  631. pitch = start_pitch << 8;
  632. for(ix=start_ix; ix < end_ix; ix++)
  633. {
  634. syl = &syllable_tab[ix];
  635. stress = syl->stress;
  636. if(increment > 0)
  637. {
  638. set_pitch(syl,(pitch >> 8),-(increment >> 8));
  639. pitch += increment;
  640. }
  641. else
  642. {
  643. drop = -(increment >> 8);
  644. if(drop < min_drop[stress])
  645. drop = min_drop[stress];
  646. pitch += increment;
  647. if(drop > 18)
  648. drop = 18;
  649. set_pitch(syl, (pitch >> 8), drop);
  650. }
  651. }
  652. }
  653. // Calculate pitch values for the vowels in this tone group
  654. static int calc_pitches2(int start, int end, int tune_number)
  655. {
  656. int ix;
  657. TUNE *tune;
  658. int drop;
  659. tune = &tunes[tune_number];
  660. ix = start;
  661. /* vowels before the first primary stress */
  662. /******************************************/
  663. SetPitchGradient(ix, ix+number_pre, tune->prehead_start, tune->prehead_end);
  664. ix += number_pre;
  665. /* body of tonic segment */
  666. /*************************/
  667. if(option_tone_flags & OPTION_EMPHASIZE_PENULTIMATE)
  668. {
  669. tone_posn = tone_posn2; // put tone on the penultimate stressed word
  670. }
  671. ix = SetHeadIntonation(tune, ix, tone_posn, 0);
  672. if(no_tonic)
  673. return(0);
  674. /* tonic syllable */
  675. /******************/
  676. if(number_tail == 0)
  677. {
  678. tone_pitch_env = tune->nucleus0_env;
  679. drop = tune->nucleus0_max - tune->nucleus0_min;
  680. set_pitch(&syllable_tab[ix++],tune->nucleus0_min, drop);
  681. }
  682. else
  683. {
  684. tone_pitch_env = tune->nucleus1_env;
  685. drop = tune->nucleus1_max - tune->nucleus1_min;
  686. set_pitch(&syllable_tab[ix++],tune->nucleus1_min, drop);
  687. }
  688. syllable_tab[tone_posn].env = tone_pitch_env;
  689. if(syllable_tab[tone_posn].stress == PRIMARY)
  690. syllable_tab[tone_posn].stress = PRIMARY_STRESSED;
  691. /* tail, after the tonic syllable */
  692. /**********************************/
  693. SetPitchGradient(ix, end, tune->tail_start, tune->tail_end);
  694. return(tone_pitch_env);
  695. }
  696. // Calculate pitch values for the vowels in this tone group
  697. static int calc_pitches(int control, int start, int end, int tune_number)
  698. {
  699. int ix;
  700. TONE_HEAD *th;
  701. TONE_NUCLEUS *tn;
  702. int drop;
  703. int continuing = 0;
  704. if(control == 0)
  705. {
  706. return(calc_pitches2(start, end, tune_number));
  707. }
  708. if(start > 0)
  709. continuing = 1;
  710. th = &tone_head_table[tune_number];
  711. tn = &tone_nucleus_table[tune_number];
  712. ix = start;
  713. /* vowels before the first primary stress */
  714. /******************************************/
  715. SetPitchGradient(ix, ix+number_pre, th->pre_start, th->pre_end);
  716. ix += number_pre;
  717. /* body of tonic segment */
  718. /*************************/
  719. if(option_tone_flags & OPTION_EMPHASIZE_PENULTIMATE)
  720. {
  721. tone_posn = tone_posn2; // put tone on the penultimate stressed word
  722. }
  723. ix = calc_pitch_segment(ix,tone_posn, th, tn, PRIMARY, continuing);
  724. if(no_tonic)
  725. return(0);
  726. /* tonic syllable */
  727. /******************/
  728. if(tn->flags & T_EMPH)
  729. {
  730. syllable_tab[ix].flags |= SYL_EMPHASIS;
  731. }
  732. if(number_tail == 0)
  733. {
  734. tone_pitch_env = tn->pitch_env0;
  735. drop = tn->tonic_max0 - tn->tonic_min0;
  736. set_pitch(&syllable_tab[ix++],tn->tonic_min0, drop);
  737. }
  738. else
  739. {
  740. tone_pitch_env = tn->pitch_env1;
  741. drop = tn->tonic_max1 - tn->tonic_min1;
  742. set_pitch(&syllable_tab[ix++],tn->tonic_min1, drop);
  743. }
  744. syllable_tab[tone_posn].env = tone_pitch_env;
  745. if(syllable_tab[tone_posn].stress == PRIMARY)
  746. syllable_tab[tone_posn].stress = PRIMARY_STRESSED;
  747. /* tail, after the tonic syllable */
  748. /**********************************/
  749. SetPitchGradient(ix, end, tn->tail_start, tn->tail_end);
  750. return(tone_pitch_env);
  751. }
  752. static void CalcPitches_Tone(Translator *tr, int clause_tone)
  753. {
  754. // clause_tone: 0=. 1=, 2=?, 3=! 4=none
  755. PHONEME_LIST *p;
  756. int ix;
  757. int count_stressed=0;
  758. int final_stressed=0;
  759. int tone_ph;
  760. int pause;
  761. int tone_promoted;
  762. PHONEME_TAB *tph;
  763. PHONEME_TAB *prev_tph; // forget across word boundary
  764. PHONEME_TAB *prevw_tph; // remember across word boundary
  765. PHONEME_LIST *prev_p;
  766. int pitch_adjust = 0; // pitch gradient through the clause - inital value
  767. int pitch_decrement = 0; // decrease by this for each stressed syllable
  768. int pitch_low = 0; // until it drops to this
  769. int pitch_high = 0; // then reset to this
  770. // count number of stressed syllables
  771. p = &phoneme_list[0];
  772. for(ix=0; ix<n_phoneme_list; ix++, p++)
  773. {
  774. if((p->type == phVOWEL) && (p->stresslevel >= 4))
  775. {
  776. if(count_stressed == 0)
  777. final_stressed = ix;
  778. if(p->stresslevel >= 4)
  779. {
  780. final_stressed = ix;
  781. count_stressed++;
  782. }
  783. }
  784. }
  785. phoneme_list[final_stressed].stresslevel = 7;
  786. // language specific, changes to tones
  787. if(tr->translator_name == L('v','i'))
  788. {
  789. // LANG=vi
  790. p = &phoneme_list[final_stressed];
  791. if(p->tone_ph == 0)
  792. p->tone_ph = PhonemeCode('7'); // change default tone (tone 1) to falling tone at end of clause
  793. }
  794. pause = 1;
  795. tone_promoted = 0;
  796. prev_p = p = &phoneme_list[0];
  797. prev_tph = prevw_tph = phoneme_tab[phonPAUSE];
  798. // perform tone sandhi
  799. for(ix=0; ix<n_phoneme_list; ix++, p++)
  800. {
  801. if((p->type == phPAUSE) && (p->ph->std_length > 50))
  802. {
  803. pause = 1; // there is a pause since the previous vowel
  804. prevw_tph = phoneme_tab[phonPAUSE]; // forget previous tone
  805. }
  806. if(p->newword)
  807. {
  808. prev_tph = phoneme_tab[phonPAUSE]; // forget across word boundaries
  809. }
  810. if(p->synthflags & SFLAG_SYLLABLE)
  811. {
  812. tone_ph = p->tone_ph;
  813. tph = phoneme_tab[tone_ph];
  814. // Mandarin
  815. if(tr->translator_name == L('z','h'))
  816. {
  817. if(tone_ph == 0)
  818. {
  819. if(pause || tone_promoted)
  820. {
  821. tone_ph = PhonemeCode2('5','5'); // no previous vowel, use tone 1
  822. tone_promoted = 1;
  823. }
  824. else
  825. {
  826. tone_ph = PhonemeCode2('1','1'); // default tone 5
  827. }
  828. p->tone_ph = tone_ph;
  829. tph = phoneme_tab[tone_ph];
  830. }
  831. else
  832. {
  833. tone_promoted = 0;
  834. }
  835. if(ix == final_stressed)
  836. {
  837. if((tph->mnemonic == 0x3535 ) || (tph->mnemonic == 0x3135))
  838. {
  839. // change sentence final tone 1 or 4 to stress 6, not 7
  840. phoneme_list[final_stressed].stresslevel = 6;
  841. }
  842. }
  843. if(prevw_tph->mnemonic == 0x343132) // [214]
  844. {
  845. if(tph->mnemonic == 0x343132) // [214]
  846. prev_p->tone_ph = PhonemeCode2('3','5');
  847. else
  848. prev_p->tone_ph = PhonemeCode2('2','1');
  849. }
  850. if((prev_tph->mnemonic == 0x3135) && (tph->mnemonic == 0x3135)) // [51] + [51]
  851. {
  852. prev_p->tone_ph = PhonemeCode2('5','3');
  853. }
  854. if(tph->mnemonic == 0x3131) // [11] Tone 5
  855. {
  856. // tone 5, change its level depending on the previous tone (across word boundaries)
  857. if(prevw_tph->mnemonic == 0x3535)
  858. p->tone_ph = PhonemeCode2('2','2');
  859. if(prevw_tph->mnemonic == 0x3533)
  860. p->tone_ph = PhonemeCode2('3','3');
  861. if(prevw_tph->mnemonic == 0x343132)
  862. p->tone_ph = PhonemeCode2('4','4');
  863. // tone 5 is unstressed (shorter)
  864. p->stresslevel = 0; // diminished stress
  865. }
  866. }
  867. prev_p = p;
  868. prevw_tph = prev_tph = tph;
  869. pause = 0;
  870. }
  871. }
  872. // convert tone numbers to pitch
  873. p = &phoneme_list[0];
  874. for(ix=0; ix<n_phoneme_list; ix++, p++)
  875. {
  876. if(p->synthflags & SFLAG_SYLLABLE)
  877. {
  878. tone_ph = p->tone_ph;
  879. if(p->stresslevel != 0) // TEST, consider all syllables as stressed
  880. {
  881. if(ix == final_stressed)
  882. {
  883. // the last stressed syllable
  884. pitch_adjust = pitch_low;
  885. }
  886. else
  887. {
  888. pitch_adjust -= pitch_decrement;
  889. if(pitch_adjust <= pitch_low)
  890. pitch_adjust = pitch_high;
  891. }
  892. }
  893. if(tone_ph ==0)
  894. {
  895. tone_ph = phonDEFAULTTONE; // no tone specified, use default tone 1
  896. p->tone_ph = tone_ph;
  897. }
  898. p->pitch1 = pitch_adjust + phoneme_tab[tone_ph]->start_type;
  899. p->pitch2 = pitch_adjust + phoneme_tab[tone_ph]->end_type;
  900. }
  901. }
  902. }
  903. void CalcPitches(Translator *tr, int clause_type)
  904. {
  905. // clause_type: 0=. 1=, 2=?, 3=! 4=none
  906. PHONEME_LIST *p;
  907. SYLLABLE *syl;
  908. int ix;
  909. int x;
  910. int st_ix;
  911. int n_st;
  912. int option;
  913. int group_tone;
  914. int group_tone_comma;
  915. int ph_start=0;
  916. int st_start;
  917. int st_clause_end;
  918. int count;
  919. int n_primary;
  920. int count_primary;
  921. PHONEME_TAB *ph;
  922. int ph_end=n_phoneme_list;
  923. SYLLABLE syllable_tab2[N_PHONEME_LIST];
  924. syllable_tab = syllable_tab2; // don't use permanent storage. it's only needed during the call of CalcPitches()
  925. n_st = 0;
  926. n_primary = 0;
  927. for(ix=0; ix<(n_phoneme_list-1); ix++)
  928. {
  929. p = &phoneme_list[ix];
  930. if(p->synthflags & SFLAG_SYLLABLE)
  931. {
  932. syllable_tab[n_st].flags = 0;
  933. syllable_tab[n_st].env = PITCHfall;
  934. syllable_tab[n_st].nextph_type = phoneme_list[ix+1].type;
  935. syllable_tab[n_st++].stress = p->stresslevel;
  936. if(p->stresslevel >= 4)
  937. n_primary++;
  938. }
  939. else
  940. if((p->ph->code == phonPAUSE_CLAUSE) && (n_st > 0))
  941. {
  942. syllable_tab[n_st-1].flags |= SYL_END_CLAUSE;
  943. }
  944. }
  945. syllable_tab[n_st].stress = 0; // extra 0 entry at the end
  946. if(n_st == 0)
  947. return; // nothing to do
  948. if(tr->langopts.tone_language == 1)
  949. {
  950. CalcPitches_Tone(tr,clause_type);
  951. return;
  952. }
  953. option = tr->langopts.intonation_group;
  954. if(option >= INTONATION_TYPES)
  955. option = 1;
  956. if(option == 0)
  957. {
  958. group_tone = tr->langopts.tunes[clause_type];
  959. group_tone_comma = tr->langopts.tunes[1];
  960. }
  961. else
  962. {
  963. group_tone = tr->punct_to_tone[option][clause_type];
  964. group_tone_comma = tr->punct_to_tone[option][1]; // emphatic form of statement
  965. }
  966. if(clause_type == 4)
  967. no_tonic = 1; /* incomplete clause, used for abbreviations such as Mr. Dr. Mrs. */
  968. else
  969. no_tonic = 0;
  970. st_start = 0;
  971. count_primary=0;
  972. for(st_ix=0; st_ix<n_st; st_ix++)
  973. {
  974. syl = &syllable_tab[st_ix];
  975. if(syl->stress >= 4)
  976. count_primary++;
  977. if(syl->stress == 6)
  978. {
  979. // reduce the stress of the previous stressed syllable (review only the previous few syllables)
  980. for(ix=st_ix-1; ix>=st_start && ix>=(st_ix-3); ix--)
  981. {
  982. if(syllable_tab[ix].stress == 6)
  983. break;
  984. if(syllable_tab[ix].stress == 4)
  985. {
  986. syllable_tab[ix].stress = 3;
  987. break;
  988. }
  989. }
  990. // are the next primary syllables also emphasized ?
  991. for(ix=st_ix+1; ix<n_st; ix++)
  992. {
  993. if(syllable_tab[ix].stress == 4)
  994. break;
  995. if(syllable_tab[ix].stress == 6)
  996. {
  997. // emphasize this syllable, but don't end the current tone group
  998. syllable_tab[st_ix].flags = SYL_EMPHASIS;
  999. syl->stress = 5;
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. if(syl->stress == 6)
  1005. {
  1006. // an emphasized syllable, end the tone group after the next primary stress
  1007. syllable_tab[st_ix].flags = SYL_EMPHASIS;
  1008. count = 0;
  1009. if((n_primary - count_primary) > 1)
  1010. count =1;
  1011. for(ix=st_ix+1; ix<n_st; ix++)
  1012. {
  1013. if(syllable_tab[ix].stress > 4)
  1014. break;
  1015. if(syllable_tab[ix].stress == 4)
  1016. {
  1017. count++;
  1018. if(count > 1)
  1019. break;
  1020. }
  1021. }
  1022. count_pitch_vowels(st_start, ix, n_st);
  1023. if((ix < n_st) || (clause_type == 0))
  1024. {
  1025. calc_pitches(option, st_start, ix, group_tone); // split into > 1 tone groups
  1026. if((clause_type==1) || (clause_type==2))
  1027. group_tone = tr->langopts.tunes[1]; // , or ? remainder has comma-tone
  1028. else
  1029. group_tone = tr->langopts.tunes[0]; // . or ! remainder has statement tone
  1030. }
  1031. else
  1032. calc_pitches(option, st_start, ix, group_tone);
  1033. st_start = ix;
  1034. }
  1035. if((st_start < st_ix) && (syl->flags & SYL_END_CLAUSE))
  1036. {
  1037. // end of clause after this syllable, indicated by a phonPAUSE_CLAUSE phoneme
  1038. st_clause_end = st_ix+1;
  1039. count_pitch_vowels(st_start, st_clause_end, st_clause_end);
  1040. calc_pitches(option, st_start, st_clause_end, group_tone_comma);
  1041. st_start = st_clause_end;
  1042. }
  1043. }
  1044. if(st_start < st_ix)
  1045. {
  1046. count_pitch_vowels(st_start, st_ix, n_st);
  1047. calc_pitches(option, st_start, st_ix, group_tone);
  1048. }
  1049. // unpack pitch data
  1050. st_ix=0;
  1051. for(ix=ph_start; ix < ph_end; ix++)
  1052. {
  1053. p = &phoneme_list[ix];
  1054. p->stresslevel = syllable_tab[st_ix].stress;
  1055. if(p->synthflags & SFLAG_SYLLABLE)
  1056. {
  1057. syl = &syllable_tab[st_ix];
  1058. p->pitch1 = syl->pitch1;
  1059. p->pitch2 = syl->pitch2;
  1060. p->env = PITCHfall;
  1061. if(syl->flags & SYL_RISE)
  1062. {
  1063. p->env = PITCHrise;
  1064. }
  1065. else
  1066. if(p->stresslevel > 5)
  1067. p->env = syl->env;
  1068. if(p->pitch1 > p->pitch2)
  1069. {
  1070. // swap so that pitch2 is the higher
  1071. x = p->pitch1;
  1072. p->pitch1 = p->pitch2;
  1073. p->pitch2 = x;
  1074. }
  1075. if(p->tone_ph)
  1076. {
  1077. ph = phoneme_tab[p->tone_ph];
  1078. x = (p->pitch1 + p->pitch2)/2;
  1079. p->pitch2 = x + ph->end_type;
  1080. p->pitch1 = x + ph->start_type;
  1081. }
  1082. if(syl->flags & SYL_EMPHASIS)
  1083. {
  1084. p->stresslevel |= 8; // emphasized
  1085. }
  1086. st_ix++;
  1087. }
  1088. }
  1089. }