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