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 36KB

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