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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 ix;
  280. int stress;
  281. int max_stress = 0;
  282. int max_stress_posn = 0; // last syllable ot the highest stress
  283. int max_stress_posn2 = 0; // penuntimate syllable of the highest stress
  284. number_pre = -1; // number of vowels before 1st primary stress
  285. number_body = 0;
  286. number_tail = 0; // number between tonic syllable and next primary
  287. last_primary = -1;
  288. for (ix = start; ix < end; ix++) {
  289. stress = syllable_tab[ix].stress; // marked stress level
  290. if (stress >= max_stress) {
  291. if (stress > max_stress)
  292. max_stress_posn2 = ix;
  293. else
  294. max_stress_posn2 = max_stress_posn;
  295. max_stress_posn = ix;
  296. max_stress = stress;
  297. }
  298. if (stress >= PRIMARY) {
  299. if (number_pre < 0)
  300. number_pre = ix - start;
  301. last_primary = ix;
  302. }
  303. }
  304. if (number_pre < 0)
  305. number_pre = end;
  306. number_tail = end - max_stress_posn - 1;
  307. tone_posn = max_stress_posn;
  308. tone_posn2 = max_stress_posn2;
  309. if (no_tonic)
  310. tone_posn = tone_posn2 = end; // next position after the end of the truncated clause
  311. else if (last_primary >= 0) {
  312. if (end == clause_end)
  313. syllable_tab[last_primary].stress = PRIMARY_LAST;
  314. } else {
  315. // no primary stress. Use the highest stress
  316. syllable_tab[tone_posn].stress = PRIMARY_LAST;
  317. }
  318. }
  319. // Count number of primary stresses up to tonic syllable or body_reset
  320. static int count_increments(int ix, int end_ix, int min_stress)
  321. {
  322. int count = 0;
  323. int stress;
  324. while (ix < end_ix) {
  325. stress = syllable_tab[ix++].stress;
  326. if (stress >= PRIMARY_LAST)
  327. break;
  328. if (stress >= min_stress)
  329. count++;
  330. }
  331. return count;
  332. }
  333. // Set the pitch of a vowel in syllable_tab
  334. static void set_pitch(SYLLABLE *syl, int base, int drop)
  335. {
  336. int pitch1, pitch2;
  337. int flags = 0;
  338. if (base < 0) base = 0;
  339. pitch2 = base;
  340. if (drop < 0) {
  341. flags = SYL_RISE;
  342. drop = -drop;
  343. }
  344. pitch1 = pitch2 + drop;
  345. if (pitch1 < 0)
  346. pitch1 = 0;
  347. if (pitch1 > 254) pitch1 = 254;
  348. if (pitch2 > 254) pitch2 = 254;
  349. syl->pitch1 = pitch1;
  350. syl->pitch2 = pitch2;
  351. syl->flags |= flags;
  352. }
  353. static int CountUnstressed(int start, int end, int limit)
  354. {
  355. int ix;
  356. for (ix = start; ix <= end; ix++) {
  357. if (syllable_tab[ix].stress >= limit)
  358. break;
  359. }
  360. return ix - start;
  361. }
  362. static int SetHeadIntonation(TUNE *tune, int syl_ix, int end_ix, int control)
  363. {
  364. int stress;
  365. SYLLABLE *syl;
  366. int ix;
  367. int pitch = 0;
  368. int increment = 0;
  369. int n_steps = 0;
  370. int stage; // onset, head, last
  371. int initial;
  372. int overflow_ix = 0;
  373. int pitch_range;
  374. int pitch_range_abs;
  375. int *drops;
  376. int n_unstressed = 0;
  377. int unstressed_ix = 0;
  378. int unstressed_inc;
  379. int used_onset = 0;
  380. int head_final = end_ix;
  381. int secondary = 2;
  382. pitch_range = (tune->head_end - tune->head_start) << 8;
  383. pitch_range_abs = abs(pitch_range);
  384. drops = drops_0; // this should be controled by tune->head_drops
  385. initial = 1;
  386. stage = 0;
  387. if (tune->onset == 255)
  388. stage = 1; // no onset specified
  389. if (tune->head_last != 255) {
  390. // find the last primary stress in the body
  391. for (ix = end_ix-1; ix >= syl_ix; ix--) {
  392. if (syllable_tab[ix].stress >= 4) {
  393. head_final = ix;
  394. break;
  395. }
  396. }
  397. }
  398. while (syl_ix < end_ix) {
  399. syl = &syllable_tab[syl_ix];
  400. stress = syl->stress;
  401. if (initial || (stress >= 4)) {
  402. // a primary stress
  403. if ((initial) || (stress == 5)) {
  404. initial = 0;
  405. overflow_ix = 0;
  406. if (tune->onset == 255) {
  407. n_steps = count_increments(syl_ix, head_final, 4);
  408. pitch = tune->head_start << 8;
  409. } else {
  410. // a pitch has been specified for the onset syllable, don't include it in the pitch incrementing
  411. n_steps = count_increments(syl_ix+1, head_final, 4);
  412. pitch = tune->onset << 8;
  413. used_onset = 1;
  414. }
  415. if (n_steps > tune->head_max_steps)
  416. n_steps = tune->head_max_steps;
  417. if (n_steps > 1)
  418. increment = pitch_range / (n_steps -1);
  419. else
  420. increment = 0;
  421. } else if (syl_ix == head_final) {
  422. // a pitch has been specified for the last primary stress before the nucleus
  423. pitch = tune->head_last << 8;
  424. stage = 2;
  425. } else {
  426. if (used_onset) {
  427. stage = 1;
  428. used_onset = 0;
  429. pitch = tune->head_start << 8;
  430. n_steps++;
  431. } else if (n_steps > 0)
  432. pitch += increment;
  433. else {
  434. pitch = (tune->head_end << 8) + (pitch_range_abs * tune->head_extend[overflow_ix++])/64;
  435. if (overflow_ix >= tune->n_head_extend)
  436. overflow_ix = 0;
  437. }
  438. }
  439. n_steps--;
  440. }
  441. if (stress >= PRIMARY) {
  442. n_unstressed = CountUnstressed(syl_ix+1, end_ix, secondary);
  443. unstressed_ix = 0;
  444. syl->stress = PRIMARY_STRESSED;
  445. syl->env = tune->stressed_env;
  446. set_pitch(syl, (pitch >> 8), tune->stressed_drop);
  447. } else if (stress >= secondary) {
  448. n_unstressed = CountUnstressed(syl_ix+1, end_ix, secondary);
  449. unstressed_ix = 0;
  450. set_pitch(syl, (pitch >> 8), drops[stress]);
  451. } else {
  452. if (n_unstressed > 1)
  453. unstressed_inc = (tune->unstr_end[stage] - tune->unstr_start[stage]) / (n_unstressed - 1);
  454. else
  455. unstressed_inc = 0;
  456. set_pitch(syl, (pitch >> 8) + tune->unstr_start[stage] + (unstressed_inc * unstressed_ix), drops[stress]);
  457. unstressed_ix++;
  458. }
  459. syl_ix++;
  460. }
  461. return syl_ix;
  462. }
  463. /* Calculate pitches until next RESET or tonic syllable, or end.
  464. Increment pitch if stress is >= min_stress.
  465. Used for tonic segment */
  466. static int calc_pitch_segment(int ix, int end_ix, TONE_HEAD *th, TONE_NUCLEUS *tn, int min_stress, int continuing)
  467. {
  468. int stress;
  469. int pitch = 0;
  470. int increment = 0;
  471. int n_primary = 0;
  472. int n_steps = 0;
  473. int initial;
  474. int overflow = 0;
  475. int n_overflow;
  476. int pitch_range;
  477. int pitch_range_abs;
  478. int *drops;
  479. signed char *overflow_tab;
  480. SYLLABLE *syl;
  481. static signed char continue_tab[5] = { -26, 32, 20, 8, 0 };
  482. drops = th->body_drops;
  483. pitch_range = (th->body_end - th->body_start) << 8;
  484. pitch_range_abs = abs(pitch_range);
  485. if (continuing) {
  486. initial = 0;
  487. overflow = 0;
  488. n_overflow = 5;
  489. overflow_tab = continue_tab;
  490. increment = pitch_range / (th->body_max_steps -1);
  491. } else {
  492. n_overflow = th->n_overflow;
  493. overflow_tab = th->overflow;
  494. initial = 1;
  495. }
  496. while (ix < end_ix) {
  497. syl = &syllable_tab[ix];
  498. stress = syl->stress;
  499. if (initial || (stress >= min_stress)) {
  500. // a primary stress
  501. if ((initial) || (stress == 5)) {
  502. initial = 0;
  503. overflow = 0;
  504. n_steps = n_primary = count_increments(ix, end_ix, min_stress);
  505. if (n_steps > th->body_max_steps)
  506. n_steps = th->body_max_steps;
  507. if (n_steps > 1)
  508. increment = pitch_range / (n_steps -1);
  509. else
  510. increment = 0;
  511. pitch = th->body_start << 8;
  512. } else {
  513. if (n_steps > 0)
  514. pitch += increment;
  515. else {
  516. pitch = (th->body_end << 8) + (pitch_range_abs * overflow_tab[overflow++])/64;
  517. if (overflow >= n_overflow) {
  518. overflow = 0;
  519. overflow_tab = th->overflow;
  520. }
  521. }
  522. }
  523. n_steps--;
  524. n_primary--;
  525. if ((tn->backwards) && (n_primary < 2))
  526. pitch = tn->backwards[n_primary] << 8;
  527. }
  528. if (stress >= PRIMARY) {
  529. syl->stress = PRIMARY_STRESSED;
  530. set_pitch(syl, (pitch >> 8), drops[stress]);
  531. } else if (stress >= SECONDARY)
  532. set_pitch(syl, (pitch >> 8), drops[stress]);
  533. else {
  534. // unstressed, drop pitch if preceded by PRIMARY
  535. if ((syllable_tab[ix-1].stress & 0x3f) >= SECONDARY)
  536. set_pitch(syl, (pitch >> 8) - th->body_lower_u, drops[stress]);
  537. else
  538. set_pitch(syl, (pitch >> 8), drops[stress]);
  539. }
  540. ix++;
  541. }
  542. return ix;
  543. }
  544. static void SetPitchGradient(int start_ix, int end_ix, int start_pitch, int end_pitch)
  545. {
  546. // Set a linear pitch change over a number of syllables.
  547. // Used for pre-head, unstressed syllables in the body, and the tail
  548. int ix;
  549. int stress;
  550. int pitch;
  551. int increment;
  552. int n_increments;
  553. int drop;
  554. SYLLABLE *syl;
  555. increment = (end_pitch - start_pitch) << 8;
  556. n_increments = end_ix - start_ix;
  557. if (n_increments <= 0)
  558. return;
  559. if (n_increments > 1)
  560. increment = increment / n_increments;
  561. pitch = start_pitch << 8;
  562. for (ix = start_ix; ix < end_ix; ix++) {
  563. syl = &syllable_tab[ix];
  564. stress = syl->stress;
  565. if (increment > 0) {
  566. set_pitch(syl, (pitch >> 8), -(increment >> 8));
  567. pitch += increment;
  568. } else {
  569. drop = -(increment >> 8);
  570. if (drop < min_drop[stress])
  571. drop = min_drop[stress];
  572. pitch += increment;
  573. if (drop > 18)
  574. drop = 18;
  575. set_pitch(syl, (pitch >> 8), drop);
  576. }
  577. }
  578. }
  579. // Calculate pitch values for the vowels in this tone group
  580. static int calc_pitches2(int start, int end, int tune_number)
  581. {
  582. int ix;
  583. TUNE *tune;
  584. int drop;
  585. tune = &tunes[tune_number];
  586. ix = start;
  587. // vowels before the first primary stress
  588. SetPitchGradient(ix, ix+number_pre, tune->prehead_start, tune->prehead_end);
  589. ix += number_pre;
  590. // body of tonic segment
  591. if (option_tone_flags & OPTION_EMPHASIZE_PENULTIMATE)
  592. tone_posn = tone_posn2; // put tone on the penultimate stressed word
  593. ix = SetHeadIntonation(tune, ix, tone_posn, 0);
  594. if (no_tonic)
  595. return 0;
  596. // tonic syllable
  597. if (number_tail == 0) {
  598. tone_pitch_env = tune->nucleus0_env;
  599. drop = tune->nucleus0_max - tune->nucleus0_min;
  600. set_pitch(&syllable_tab[ix++], tune->nucleus0_min, drop);
  601. } else {
  602. tone_pitch_env = tune->nucleus1_env;
  603. drop = tune->nucleus1_max - tune->nucleus1_min;
  604. set_pitch(&syllable_tab[ix++], tune->nucleus1_min, drop);
  605. }
  606. syllable_tab[tone_posn].env = tone_pitch_env;
  607. if (syllable_tab[tone_posn].stress == PRIMARY)
  608. syllable_tab[tone_posn].stress = PRIMARY_STRESSED;
  609. // tail, after the tonic syllable
  610. SetPitchGradient(ix, end, tune->tail_start, tune->tail_end);
  611. return tone_pitch_env;
  612. }
  613. // Calculate pitch values for the vowels in this tone group
  614. static int calc_pitches(int control, int start, int end, int tune_number)
  615. {
  616. int ix;
  617. TONE_HEAD *th;
  618. TONE_NUCLEUS *tn;
  619. int drop;
  620. int continuing = 0;
  621. if (control == 0)
  622. return calc_pitches2(start, end, tune_number);
  623. if (start > 0)
  624. continuing = 1;
  625. th = &tone_head_table[tune_number];
  626. tn = &tone_nucleus_table[tune_number];
  627. ix = start;
  628. // vowels before the first primary stress
  629. SetPitchGradient(ix, ix+number_pre, th->pre_start, th->pre_end);
  630. ix += number_pre;
  631. // body of tonic segment
  632. if (option_tone_flags & OPTION_EMPHASIZE_PENULTIMATE)
  633. tone_posn = tone_posn2; // put tone on the penultimate stressed word
  634. ix = calc_pitch_segment(ix, tone_posn, th, tn, PRIMARY, continuing);
  635. if (no_tonic)
  636. return 0;
  637. // tonic syllable
  638. if (tn->flags & T_EMPH)
  639. syllable_tab[ix].flags |= SYL_EMPHASIS;
  640. if (number_tail == 0) {
  641. tone_pitch_env = tn->pitch_env0;
  642. drop = tn->tonic_max0 - tn->tonic_min0;
  643. set_pitch(&syllable_tab[ix++], tn->tonic_min0, drop);
  644. } else {
  645. tone_pitch_env = tn->pitch_env1;
  646. drop = tn->tonic_max1 - tn->tonic_min1;
  647. set_pitch(&syllable_tab[ix++], tn->tonic_min1, drop);
  648. }
  649. syllable_tab[tone_posn].env = tone_pitch_env;
  650. if (syllable_tab[tone_posn].stress == PRIMARY)
  651. syllable_tab[tone_posn].stress = PRIMARY_STRESSED;
  652. // tail, after the tonic syllable
  653. SetPitchGradient(ix, end, tn->tail_start, tn->tail_end);
  654. return tone_pitch_env;
  655. }
  656. static void CalcPitches_Tone(Translator *tr, int clause_tone)
  657. {
  658. // clause_tone: 0=. 1=, 2=?, 3=! 4=none
  659. PHONEME_LIST *p;
  660. int ix;
  661. int count_stressed = 0;
  662. int final_stressed = 0;
  663. int tone_ph;
  664. int pause;
  665. int tone_promoted;
  666. PHONEME_TAB *tph;
  667. PHONEME_TAB *prev_tph; // forget across word boundary
  668. PHONEME_TAB *prevw_tph; // remember across word boundary
  669. PHONEME_LIST *prev_p;
  670. int pitch_adjust = 0; // pitch gradient through the clause - inital value
  671. int pitch_decrement = 0; // decrease by this for each stressed syllable
  672. int pitch_low = 0; // until it drops to this
  673. int pitch_high = 0; // then reset to this
  674. // count number of stressed syllables
  675. p = &phoneme_list[0];
  676. for (ix = 0; ix < n_phoneme_list; ix++, p++) {
  677. if ((p->type == phVOWEL) && (p->stresslevel >= 4)) {
  678. if (count_stressed == 0)
  679. final_stressed = ix;
  680. if (p->stresslevel >= 4) {
  681. final_stressed = ix;
  682. count_stressed++;
  683. }
  684. }
  685. }
  686. phoneme_list[final_stressed].stresslevel = 7;
  687. // language specific, changes to tones
  688. if (tr->translator_name == L('v', 'i')) {
  689. // LANG=vi
  690. p = &phoneme_list[final_stressed];
  691. if (p->tone_ph == 0)
  692. p->tone_ph = PhonemeCode('7'); // change default tone (tone 1) to falling tone at end of clause
  693. }
  694. pause = 1;
  695. tone_promoted = 0;
  696. prev_p = p = &phoneme_list[0];
  697. prev_tph = prevw_tph = phoneme_tab[phonPAUSE];
  698. // perform tone sandhi
  699. for (ix = 0; ix < n_phoneme_list; ix++, p++) {
  700. if ((p->type == phPAUSE) && (p->ph->std_length > 50)) {
  701. pause = 1; // there is a pause since the previous vowel
  702. prevw_tph = phoneme_tab[phonPAUSE]; // forget previous tone
  703. }
  704. if (p->newword)
  705. prev_tph = phoneme_tab[phonPAUSE]; // forget across word boundaries
  706. if (p->synthflags & SFLAG_SYLLABLE) {
  707. tone_ph = p->tone_ph;
  708. tph = phoneme_tab[tone_ph];
  709. // Mandarin
  710. if (tr->translator_name == L('z', 'h')) {
  711. if (tone_ph == 0) {
  712. if (pause || tone_promoted) {
  713. tone_ph = PhonemeCode2('5', '5'); // no previous vowel, use tone 1
  714. tone_promoted = 1;
  715. } else
  716. tone_ph = PhonemeCode2('1', '1'); // default tone 5
  717. p->tone_ph = tone_ph;
  718. tph = phoneme_tab[tone_ph];
  719. } else
  720. tone_promoted = 0;
  721. if (ix == final_stressed) {
  722. if ((tph->mnemonic == 0x3535 ) || (tph->mnemonic == 0x3135)) {
  723. // change sentence final tone 1 or 4 to stress 6, not 7
  724. phoneme_list[final_stressed].stresslevel = 6;
  725. }
  726. }
  727. if (prevw_tph->mnemonic == 0x343132) { // [214]
  728. if (tph->mnemonic == 0x343132) // [214]
  729. prev_p->tone_ph = PhonemeCode2('3', '5');
  730. else
  731. prev_p->tone_ph = PhonemeCode2('2', '1');
  732. }
  733. if ((prev_tph->mnemonic == 0x3135) && (tph->mnemonic == 0x3135)) // [51] + [51]
  734. prev_p->tone_ph = PhonemeCode2('5', '3');
  735. if (tph->mnemonic == 0x3131) { // [11] Tone 5
  736. // tone 5, change its level depending on the previous tone (across word boundaries)
  737. if (prevw_tph->mnemonic == 0x3535)
  738. p->tone_ph = PhonemeCode2('2', '2');
  739. if (prevw_tph->mnemonic == 0x3533)
  740. p->tone_ph = PhonemeCode2('3', '3');
  741. if (prevw_tph->mnemonic == 0x343132)
  742. p->tone_ph = PhonemeCode2('4', '4');
  743. // tone 5 is unstressed (shorter)
  744. p->stresslevel = 0; // diminished stress
  745. }
  746. }
  747. prev_p = p;
  748. prevw_tph = prev_tph = tph;
  749. pause = 0;
  750. }
  751. }
  752. // convert tone numbers to pitch
  753. p = &phoneme_list[0];
  754. for (ix = 0; ix < n_phoneme_list; ix++, p++) {
  755. if (p->synthflags & SFLAG_SYLLABLE) {
  756. tone_ph = p->tone_ph;
  757. if (p->stresslevel != 0) { // TEST, consider all syllables as stressed
  758. if (ix == final_stressed) {
  759. // the last stressed syllable
  760. pitch_adjust = pitch_low;
  761. } else {
  762. pitch_adjust -= pitch_decrement;
  763. if (pitch_adjust <= pitch_low)
  764. pitch_adjust = pitch_high;
  765. }
  766. }
  767. if (tone_ph == 0) {
  768. tone_ph = phonDEFAULTTONE; // no tone specified, use default tone 1
  769. p->tone_ph = tone_ph;
  770. }
  771. p->pitch1 = pitch_adjust + phoneme_tab[tone_ph]->start_type;
  772. p->pitch2 = pitch_adjust + phoneme_tab[tone_ph]->end_type;
  773. }
  774. }
  775. }
  776. void CalcPitches(Translator *tr, int clause_type)
  777. {
  778. // clause_type: 0=. 1=, 2=?, 3=! 4=none
  779. PHONEME_LIST *p;
  780. SYLLABLE *syl;
  781. int ix;
  782. int x;
  783. int st_ix;
  784. int n_st;
  785. int option;
  786. int group_tone;
  787. int group_tone_comma;
  788. int ph_start = 0;
  789. int st_start;
  790. int st_clause_end;
  791. int count;
  792. int n_primary;
  793. int count_primary;
  794. PHONEME_TAB *ph;
  795. int ph_end = n_phoneme_list;
  796. SYLLABLE syllable_tab2[N_PHONEME_LIST];
  797. syllable_tab = syllable_tab2; // don't use permanent storage. it's only needed during the call of CalcPitches()
  798. n_st = 0;
  799. n_primary = 0;
  800. for (ix = 0; ix < (n_phoneme_list-1); ix++) {
  801. p = &phoneme_list[ix];
  802. if (p->synthflags & SFLAG_SYLLABLE) {
  803. syllable_tab[n_st].flags = 0;
  804. syllable_tab[n_st].env = PITCHfall;
  805. syllable_tab[n_st].nextph_type = phoneme_list[ix+1].type;
  806. syllable_tab[n_st++].stress = p->stresslevel;
  807. if (p->stresslevel >= 4)
  808. n_primary++;
  809. } else if ((p->ph->code == phonPAUSE_CLAUSE) && (n_st > 0))
  810. syllable_tab[n_st-1].flags |= SYL_END_CLAUSE;
  811. }
  812. syllable_tab[n_st].stress = 0; // extra 0 entry at the end
  813. if (n_st == 0)
  814. return; // nothing to do
  815. if (tr->langopts.tone_language == 1) {
  816. CalcPitches_Tone(tr, clause_type);
  817. return;
  818. }
  819. option = tr->langopts.intonation_group;
  820. if (option >= INTONATION_TYPES)
  821. option = 1;
  822. if (option == 0) {
  823. group_tone = tr->langopts.tunes[clause_type];
  824. group_tone_comma = tr->langopts.tunes[1];
  825. } else {
  826. group_tone = tr->punct_to_tone[option][clause_type];
  827. group_tone_comma = tr->punct_to_tone[option][1]; // emphatic form of statement
  828. }
  829. if (clause_type == 4)
  830. no_tonic = 1; // incomplete clause, used for abbreviations such as Mr. Dr. Mrs.
  831. else
  832. no_tonic = 0;
  833. st_start = 0;
  834. count_primary = 0;
  835. for (st_ix = 0; st_ix < n_st; st_ix++) {
  836. syl = &syllable_tab[st_ix];
  837. if (syl->stress >= 4)
  838. count_primary++;
  839. if (syl->stress == 6) {
  840. // reduce the stress of the previous stressed syllable (review only the previous few syllables)
  841. for (ix = st_ix-1; ix >= st_start && ix >= (st_ix-3); ix--) {
  842. if (syllable_tab[ix].stress == 6)
  843. break;
  844. if (syllable_tab[ix].stress == 4) {
  845. syllable_tab[ix].stress = 3;
  846. break;
  847. }
  848. }
  849. // are the next primary syllables also emphasized ?
  850. for (ix = st_ix+1; ix < n_st; ix++) {
  851. if (syllable_tab[ix].stress == 4)
  852. break;
  853. if (syllable_tab[ix].stress == 6) {
  854. // emphasize this syllable, but don't end the current tone group
  855. syllable_tab[st_ix].flags = SYL_EMPHASIS;
  856. syl->stress = 5;
  857. break;
  858. }
  859. }
  860. }
  861. if (syl->stress == 6) {
  862. // an emphasized syllable, end the tone group after the next primary stress
  863. syllable_tab[st_ix].flags = SYL_EMPHASIS;
  864. count = 0;
  865. if ((n_primary - count_primary) > 1)
  866. count = 1;
  867. for (ix = st_ix+1; ix < n_st; ix++) {
  868. if (syllable_tab[ix].stress > 4)
  869. break;
  870. if (syllable_tab[ix].stress == 4) {
  871. count++;
  872. if (count > 1)
  873. break;
  874. }
  875. }
  876. count_pitch_vowels(st_start, ix, n_st);
  877. if ((ix < n_st) || (clause_type == 0)) {
  878. calc_pitches(option, st_start, ix, group_tone); // split into > 1 tone groups
  879. if ((clause_type == 1) || (clause_type == 2))
  880. group_tone = tr->langopts.tunes[1]; // , or ? remainder has comma-tone
  881. else
  882. group_tone = tr->langopts.tunes[0]; // . or ! remainder has statement tone
  883. } else
  884. calc_pitches(option, st_start, ix, group_tone);
  885. st_start = ix;
  886. }
  887. if ((st_start < st_ix) && (syl->flags & SYL_END_CLAUSE)) {
  888. // end of clause after this syllable, indicated by a phonPAUSE_CLAUSE phoneme
  889. st_clause_end = st_ix+1;
  890. count_pitch_vowels(st_start, st_clause_end, st_clause_end);
  891. calc_pitches(option, st_start, st_clause_end, group_tone_comma);
  892. st_start = st_clause_end;
  893. }
  894. }
  895. if (st_start < st_ix) {
  896. count_pitch_vowels(st_start, st_ix, n_st);
  897. calc_pitches(option, st_start, st_ix, group_tone);
  898. }
  899. // unpack pitch data
  900. st_ix = 0;
  901. for (ix = ph_start; ix < ph_end; ix++) {
  902. p = &phoneme_list[ix];
  903. p->stresslevel = syllable_tab[st_ix].stress;
  904. if (p->synthflags & SFLAG_SYLLABLE) {
  905. syl = &syllable_tab[st_ix];
  906. p->pitch1 = syl->pitch1;
  907. p->pitch2 = syl->pitch2;
  908. p->env = PITCHfall;
  909. if (syl->flags & SYL_RISE)
  910. p->env = PITCHrise;
  911. else if (p->stresslevel > 5)
  912. p->env = syl->env;
  913. if (p->pitch1 > p->pitch2) {
  914. // swap so that pitch2 is the higher
  915. x = p->pitch1;
  916. p->pitch1 = p->pitch2;
  917. p->pitch2 = x;
  918. }
  919. if (p->tone_ph) {
  920. ph = phoneme_tab[p->tone_ph];
  921. x = (p->pitch1 + p->pitch2)/2;
  922. p->pitch2 = x + ph->end_type;
  923. p->pitch1 = x + ph->start_type;
  924. }
  925. if (syl->flags & SYL_EMPHASIS)
  926. p->stresslevel |= 8; // emphasized
  927. st_ix++;
  928. }
  929. }
  930. }