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

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