intonation.cpp 36 KB

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