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.

wavegen.c 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. /*
  2. * Copyright (C) 2005 to 2013 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. // this version keeps wavemult window as a constant fraction
  20. // of the cycle length - but that spreads out the HF peaks too much
  21. #include "config.h"
  22. #include <math.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <espeak-ng/espeak_ng.h>
  29. #include <espeak-ng/speak_lib.h>
  30. #include "synthesize.h"
  31. #include "speech.h"
  32. #include "phoneme.h"
  33. #include "voice.h"
  34. #ifdef INCLUDE_KLATT
  35. #include "klatt.h"
  36. #endif
  37. #if HAVE_SONIC_H
  38. #include "sonic.h"
  39. #endif
  40. #include "sintab.h"
  41. #define N_WAV_BUF 10
  42. voice_t *wvoice = NULL;
  43. FILE *f_log = NULL;
  44. static int option_harmonic1 = 10;
  45. static int flutter_amp = 64;
  46. static int general_amplitude = 60;
  47. static int consonant_amp = 26;
  48. int embedded_value[N_EMBEDDED_VALUES];
  49. static int PHASE_INC_FACTOR;
  50. int samplerate = 0; // this is set by Wavegeninit()
  51. int samplerate_native = 0;
  52. static wavegen_peaks_t peaks[N_PEAKS];
  53. static int peak_harmonic[N_PEAKS];
  54. static int peak_height[N_PEAKS];
  55. int echo_head;
  56. int echo_tail;
  57. int echo_amp = 0;
  58. short echo_buf[N_ECHO_BUF];
  59. static int echo_length = 0; // period (in sample\) to ensure completion of echo at the end of speech, set in WavegenSetEcho()
  60. static int voicing;
  61. static RESONATOR rbreath[N_PEAKS];
  62. static int harm_sqrt_n = 0;
  63. #define N_LOWHARM 30
  64. static int harm_inc[N_LOWHARM]; // only for these harmonics do we interpolate amplitude between steps
  65. static int *harmspect;
  66. static int hswitch = 0;
  67. static int hspect[2][MAX_HARMONIC]; // 2 copies, we interpolate between then
  68. static int max_hval = 0;
  69. static int nsamples = 0; // number to do
  70. static int modulation_type = 0;
  71. static int glottal_flag = 0;
  72. static int glottal_reduce = 0;
  73. WGEN_DATA wdata;
  74. static int amp_ix;
  75. static int amp_inc;
  76. static unsigned char *amplitude_env = NULL;
  77. static int samplecount = 0; // number done
  78. static int samplecount_start = 0; // count at start of this segment
  79. static int end_wave = 0; // continue to end of wave cycle
  80. static int wavephase;
  81. static int phaseinc;
  82. static int cycle_samples; // number of samples in a cycle at current pitch
  83. static int cbytes;
  84. static int hf_factor;
  85. static double minus_pi_t;
  86. static double two_pi_t;
  87. unsigned char *out_ptr;
  88. unsigned char *out_start;
  89. unsigned char *out_end;
  90. // the queue of operations passed to wavegen from sythesize
  91. intptr_t wcmdq[N_WCMDQ][4];
  92. int wcmdq_head = 0;
  93. int wcmdq_tail = 0;
  94. // pitch,speed,
  95. int embedded_default[N_EMBEDDED_VALUES] = { 0, 50, 175, 100, 50, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0 };
  96. static int embedded_max[N_EMBEDDED_VALUES] = { 0, 0x7fff, 750, 300, 99, 99, 99, 0, 750, 0, 0, 0, 0, 4, 0 };
  97. int current_source_index = 0;
  98. extern FILE *f_wave;
  99. #if HAVE_SONIC_H
  100. static sonicStream sonicSpeedupStream = NULL;
  101. double sonicSpeed = 1.0;
  102. #endif
  103. // 1st index=roughness
  104. // 2nd index=modulation_type
  105. // value: bits 0-3 amplitude (16ths), bits 4-7 every n cycles
  106. #define N_ROUGHNESS 8
  107. static unsigned char modulation_tab[N_ROUGHNESS][8] = {
  108. { 0, 0x00, 0x00, 0x00, 0, 0x46, 0xf2, 0x29 },
  109. { 0, 0x2f, 0x00, 0x2f, 0, 0x45, 0xf2, 0x29 },
  110. { 0, 0x2f, 0x00, 0x2e, 0, 0x45, 0xf2, 0x28 },
  111. { 0, 0x2e, 0x00, 0x2d, 0, 0x34, 0xf2, 0x28 },
  112. { 0, 0x2d, 0x2d, 0x2c, 0, 0x34, 0xf2, 0x28 },
  113. { 0, 0x2b, 0x2b, 0x2b, 0, 0x34, 0xf2, 0x28 },
  114. { 0, 0x2a, 0x2a, 0x2a, 0, 0x34, 0xf2, 0x28 },
  115. { 0, 0x29, 0x29, 0x29, 0, 0x34, 0xf2, 0x28 },
  116. };
  117. // Flutter table, to add natural variations to the pitch
  118. #define N_FLUTTER 0x170
  119. static int Flutter_inc;
  120. static const unsigned char Flutter_tab[N_FLUTTER] = {
  121. 0x80, 0x9b, 0xb5, 0xcb, 0xdc, 0xe8, 0xed, 0xec,
  122. 0xe6, 0xdc, 0xce, 0xbf, 0xb0, 0xa3, 0x98, 0x90,
  123. 0x8c, 0x8b, 0x8c, 0x8f, 0x92, 0x94, 0x95, 0x92,
  124. 0x8c, 0x83, 0x78, 0x69, 0x59, 0x49, 0x3c, 0x31,
  125. 0x2a, 0x29, 0x2d, 0x36, 0x44, 0x56, 0x69, 0x7d,
  126. 0x8f, 0x9f, 0xaa, 0xb1, 0xb2, 0xad, 0xa4, 0x96,
  127. 0x87, 0x78, 0x69, 0x5c, 0x53, 0x4f, 0x4f, 0x55,
  128. 0x5e, 0x6b, 0x7a, 0x88, 0x96, 0xa2, 0xab, 0xb0,
  129. 0xb1, 0xae, 0xa8, 0xa0, 0x98, 0x91, 0x8b, 0x88,
  130. 0x89, 0x8d, 0x94, 0x9d, 0xa8, 0xb2, 0xbb, 0xc0,
  131. 0xc1, 0xbd, 0xb4, 0xa5, 0x92, 0x7c, 0x63, 0x4a,
  132. 0x32, 0x1e, 0x0e, 0x05, 0x02, 0x05, 0x0f, 0x1e,
  133. 0x30, 0x44, 0x59, 0x6d, 0x7f, 0x8c, 0x96, 0x9c,
  134. 0x9f, 0x9f, 0x9d, 0x9b, 0x99, 0x99, 0x9c, 0xa1,
  135. 0xa9, 0xb3, 0xbf, 0xca, 0xd5, 0xdc, 0xe0, 0xde,
  136. 0xd8, 0xcc, 0xbb, 0xa6, 0x8f, 0x77, 0x60, 0x4b,
  137. 0x3a, 0x2e, 0x28, 0x29, 0x2f, 0x3a, 0x48, 0x59,
  138. 0x6a, 0x7a, 0x86, 0x90, 0x94, 0x95, 0x91, 0x89,
  139. 0x80, 0x75, 0x6b, 0x62, 0x5c, 0x5a, 0x5c, 0x61,
  140. 0x69, 0x74, 0x80, 0x8a, 0x94, 0x9a, 0x9e, 0x9d,
  141. 0x98, 0x90, 0x86, 0x7c, 0x71, 0x68, 0x62, 0x60,
  142. 0x63, 0x6b, 0x78, 0x88, 0x9b, 0xaf, 0xc2, 0xd2,
  143. 0xdf, 0xe6, 0xe7, 0xe2, 0xd7, 0xc6, 0xb2, 0x9c,
  144. 0x84, 0x6f, 0x5b, 0x4b, 0x40, 0x39, 0x37, 0x38,
  145. 0x3d, 0x43, 0x4a, 0x50, 0x54, 0x56, 0x55, 0x52,
  146. 0x4d, 0x48, 0x42, 0x3f, 0x3e, 0x41, 0x49, 0x56,
  147. 0x67, 0x7c, 0x93, 0xab, 0xc3, 0xd9, 0xea, 0xf6,
  148. 0xfc, 0xfb, 0xf4, 0xe7, 0xd5, 0xc0, 0xaa, 0x94,
  149. 0x80, 0x71, 0x64, 0x5d, 0x5a, 0x5c, 0x61, 0x68,
  150. 0x70, 0x77, 0x7d, 0x7f, 0x7f, 0x7b, 0x74, 0x6b,
  151. 0x61, 0x57, 0x4e, 0x48, 0x46, 0x48, 0x4e, 0x59,
  152. 0x66, 0x75, 0x84, 0x93, 0x9f, 0xa7, 0xab, 0xaa,
  153. 0xa4, 0x99, 0x8b, 0x7b, 0x6a, 0x5b, 0x4e, 0x46,
  154. 0x43, 0x45, 0x4d, 0x5a, 0x6b, 0x7f, 0x92, 0xa6,
  155. 0xb8, 0xc5, 0xcf, 0xd3, 0xd2, 0xcd, 0xc4, 0xb9,
  156. 0xad, 0xa1, 0x96, 0x8e, 0x89, 0x87, 0x87, 0x8a,
  157. 0x8d, 0x91, 0x92, 0x91, 0x8c, 0x84, 0x78, 0x68,
  158. 0x55, 0x41, 0x2e, 0x1c, 0x0e, 0x05, 0x01, 0x05,
  159. 0x0f, 0x1f, 0x34, 0x4d, 0x68, 0x81, 0x9a, 0xb0,
  160. 0xc1, 0xcd, 0xd3, 0xd3, 0xd0, 0xc8, 0xbf, 0xb5,
  161. 0xab, 0xa4, 0x9f, 0x9c, 0x9d, 0xa0, 0xa5, 0xaa,
  162. 0xae, 0xb1, 0xb0, 0xab, 0xa3, 0x96, 0x87, 0x76,
  163. 0x63, 0x51, 0x42, 0x36, 0x2f, 0x2d, 0x31, 0x3a,
  164. 0x48, 0x59, 0x6b, 0x7e, 0x8e, 0x9c, 0xa6, 0xaa,
  165. 0xa9, 0xa3, 0x98, 0x8a, 0x7b, 0x6c, 0x5d, 0x52,
  166. 0x4a, 0x48, 0x4a, 0x50, 0x5a, 0x67, 0x75, 0x82
  167. };
  168. // waveform shape table for HF peaks, formants 6,7,8
  169. #define N_WAVEMULT 128
  170. static int wavemult_offset = 0;
  171. static int wavemult_max = 0;
  172. // the presets are for 22050 Hz sample rate.
  173. // A different rate will need to recalculate the presets in WavegenInit()
  174. static unsigned char wavemult[N_WAVEMULT] = {
  175. 0, 0, 0, 2, 3, 5, 8, 11, 14, 18, 22, 27, 32, 37, 43, 49,
  176. 55, 62, 69, 76, 83, 90, 98, 105, 113, 121, 128, 136, 144, 152, 159, 166,
  177. 174, 181, 188, 194, 201, 207, 213, 218, 224, 228, 233, 237, 240, 244, 246, 249,
  178. 251, 252, 253, 253, 253, 253, 252, 251, 249, 246, 244, 240, 237, 233, 228, 224,
  179. 218, 213, 207, 201, 194, 188, 181, 174, 166, 159, 152, 144, 136, 128, 121, 113,
  180. 105, 98, 90, 83, 76, 69, 62, 55, 49, 43, 37, 32, 27, 22, 18, 14,
  181. 11, 8, 5, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  182. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  183. };
  184. // set from y = pow(2,x) * 128, x=-1 to 1
  185. unsigned char pitch_adjust_tab[MAX_PITCH_VALUE+1] = {
  186. 64, 65, 66, 67, 68, 69, 70, 71,
  187. 72, 73, 74, 75, 76, 77, 78, 79,
  188. 80, 81, 82, 83, 84, 86, 87, 88,
  189. 89, 91, 92, 93, 94, 96, 97, 98,
  190. 100, 101, 103, 104, 105, 107, 108, 110,
  191. 111, 113, 115, 116, 118, 119, 121, 123,
  192. 124, 126, 128, 130, 132, 133, 135, 137,
  193. 139, 141, 143, 145, 147, 149, 151, 153,
  194. 155, 158, 160, 162, 164, 167, 169, 171,
  195. 174, 176, 179, 181, 184, 186, 189, 191,
  196. 194, 197, 199, 202, 205, 208, 211, 214,
  197. 217, 220, 223, 226, 229, 232, 236, 239,
  198. 242, 246, 249, 252, 254, 255
  199. };
  200. void WcmdqStop()
  201. {
  202. wcmdq_head = 0;
  203. wcmdq_tail = 0;
  204. #if HAVE_SONIC_H
  205. if (sonicSpeedupStream != NULL) {
  206. sonicDestroyStream(sonicSpeedupStream);
  207. sonicSpeedupStream = NULL;
  208. }
  209. #endif
  210. if (mbrola_name[0] != 0)
  211. MbrolaReset();
  212. }
  213. int WcmdqFree()
  214. {
  215. int i;
  216. i = wcmdq_head - wcmdq_tail;
  217. if (i <= 0) i += N_WCMDQ;
  218. return i;
  219. }
  220. int WcmdqUsed()
  221. {
  222. return N_WCMDQ - WcmdqFree();
  223. }
  224. void WcmdqInc()
  225. {
  226. wcmdq_tail++;
  227. if (wcmdq_tail >= N_WCMDQ) wcmdq_tail = 0;
  228. }
  229. static void WcmdqIncHead()
  230. {
  231. wcmdq_head++;
  232. if (wcmdq_head >= N_WCMDQ) wcmdq_head = 0;
  233. }
  234. #define PEAKSHAPEW 256
  235. unsigned char pk_shape1[PEAKSHAPEW+1] = {
  236. 255, 254, 254, 254, 254, 254, 253, 253, 252, 251, 251, 250, 249, 248, 247, 246,
  237. 245, 244, 242, 241, 239, 238, 236, 234, 233, 231, 229, 227, 225, 223, 220, 218,
  238. 216, 213, 211, 209, 207, 205, 203, 201, 199, 197, 195, 193, 191, 189, 187, 185,
  239. 183, 180, 178, 176, 173, 171, 169, 166, 164, 161, 159, 156, 154, 151, 148, 146,
  240. 143, 140, 138, 135, 132, 129, 126, 123, 120, 118, 115, 112, 108, 105, 102, 99,
  241. 96, 95, 93, 91, 90, 88, 86, 85, 83, 82, 80, 79, 77, 76, 74, 73,
  242. 72, 70, 69, 68, 67, 66, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55,
  243. 55, 54, 53, 52, 52, 51, 50, 50, 49, 48, 48, 47, 47, 46, 46, 46,
  244. 45, 45, 45, 44, 44, 44, 44, 44, 44, 44, 43, 43, 43, 43, 44, 43,
  245. 42, 42, 41, 40, 40, 39, 38, 38, 37, 36, 36, 35, 35, 34, 33, 33,
  246. 32, 32, 31, 30, 30, 29, 29, 28, 28, 27, 26, 26, 25, 25, 24, 24,
  247. 23, 23, 22, 22, 21, 21, 20, 20, 19, 19, 18, 18, 18, 17, 17, 16,
  248. 16, 15, 15, 15, 14, 14, 13, 13, 13, 12, 12, 11, 11, 11, 10, 10,
  249. 10, 9, 9, 9, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 5, 5,
  250. 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2,
  251. 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  252. 0
  253. };
  254. static unsigned char pk_shape2[PEAKSHAPEW+1] = {
  255. 255, 254, 254, 254, 254, 254, 254, 254, 254, 254, 253, 253, 253, 253, 252, 252,
  256. 252, 251, 251, 251, 250, 250, 249, 249, 248, 248, 247, 247, 246, 245, 245, 244,
  257. 243, 243, 242, 241, 239, 237, 235, 233, 231, 229, 227, 225, 223, 221, 218, 216,
  258. 213, 211, 208, 205, 203, 200, 197, 194, 191, 187, 184, 181, 178, 174, 171, 167,
  259. 163, 160, 156, 152, 148, 144, 140, 136, 132, 127, 123, 119, 114, 110, 105, 100,
  260. 96, 94, 91, 88, 86, 83, 81, 78, 76, 74, 71, 69, 66, 64, 62, 60,
  261. 57, 55, 53, 51, 49, 47, 44, 42, 40, 38, 36, 34, 32, 30, 29, 27,
  262. 25, 23, 21, 19, 18, 16, 14, 12, 11, 9, 7, 6, 4, 3, 1, 0,
  263. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  264. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  265. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  266. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  267. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  268. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  269. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  270. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  271. 0
  272. };
  273. static unsigned char *pk_shape;
  274. void WavegenInit(int rate, int wavemult_fact)
  275. {
  276. int ix;
  277. double x;
  278. if (wavemult_fact == 0)
  279. wavemult_fact = 60; // default
  280. wvoice = NULL;
  281. samplerate = samplerate_native = rate;
  282. PHASE_INC_FACTOR = 0x8000000 / samplerate; // assumes pitch is Hz*32
  283. Flutter_inc = (64 * samplerate)/rate;
  284. samplecount = 0;
  285. nsamples = 0;
  286. wavephase = 0x7fffffff;
  287. max_hval = 0;
  288. wdata.amplitude = 32;
  289. wdata.amplitude_fmt = 100;
  290. for (ix = 0; ix < N_EMBEDDED_VALUES; ix++)
  291. embedded_value[ix] = embedded_default[ix];
  292. // set up window to generate a spread of harmonics from a
  293. // single peak for HF peaks
  294. wavemult_max = (samplerate * wavemult_fact)/(256 * 50);
  295. if (wavemult_max > N_WAVEMULT) wavemult_max = N_WAVEMULT;
  296. wavemult_offset = wavemult_max/2;
  297. if (samplerate != 22050) {
  298. // wavemult table has preset values for 22050 Hz, we only need to
  299. // recalculate them if we have a different sample rate
  300. for (ix = 0; ix < wavemult_max; ix++) {
  301. x = 127*(1.0 - cos((M_PI*2)*ix/wavemult_max));
  302. wavemult[ix] = (int)x;
  303. }
  304. }
  305. pk_shape = pk_shape2;
  306. #ifdef INCLUDE_KLATT
  307. KlattInit();
  308. #endif
  309. }
  310. int GetAmplitude(void)
  311. {
  312. int amp;
  313. // normal, none, reduced, moderate, strong
  314. static const unsigned char amp_emphasis[5] = { 16, 16, 10, 16, 22 };
  315. amp = (embedded_value[EMBED_A])*55/100;
  316. general_amplitude = amp * amp_emphasis[embedded_value[EMBED_F]] / 16;
  317. return general_amplitude;
  318. }
  319. static void WavegenSetEcho(void)
  320. {
  321. if (wvoice == NULL)
  322. return;
  323. int delay;
  324. int amp;
  325. voicing = wvoice->voicing;
  326. delay = wvoice->echo_delay;
  327. amp = wvoice->echo_amp;
  328. if (delay >= N_ECHO_BUF)
  329. delay = N_ECHO_BUF-1;
  330. if (amp > 100)
  331. amp = 100;
  332. memset(echo_buf, 0, sizeof(echo_buf));
  333. echo_tail = 0;
  334. if (embedded_value[EMBED_H] > 0) {
  335. // set echo from an embedded command in the text
  336. amp = embedded_value[EMBED_H];
  337. delay = 130;
  338. }
  339. if (delay == 0)
  340. amp = 0;
  341. echo_head = (delay * samplerate)/1000;
  342. echo_length = echo_head; // ensure completion of echo at the end of speech. Use 1 delay period?
  343. if (amp == 0)
  344. echo_length = 0;
  345. if (amp > 20)
  346. echo_length = echo_head * 2; // perhaps allow 2 echo periods if the echo is loud.
  347. // echo_amp units are 1/256ths of the amplitude of the original sound.
  348. echo_amp = amp;
  349. // compensate (partially) for increase in amplitude due to echo
  350. general_amplitude = GetAmplitude();
  351. general_amplitude = ((general_amplitude * (500-amp))/500);
  352. }
  353. int PeaksToHarmspect(wavegen_peaks_t *peaks, int pitch, int *htab, int control)
  354. {
  355. if (wvoice == NULL)
  356. return 1;
  357. // Calculate the amplitude of each harmonics from the formants
  358. // Only for formants 0 to 5
  359. // control 0=initial call, 1=every 64 cycles
  360. // pitch and freqs are Hz<<16
  361. int f;
  362. wavegen_peaks_t *p;
  363. int fp; // centre freq of peak
  364. int fhi; // high freq of peak
  365. int h; // harmonic number
  366. int pk;
  367. int hmax;
  368. int hmax_samplerate; // highest harmonic allowed for the samplerate
  369. int x;
  370. int ix;
  371. int h1;
  372. // initialise as much of *out as we will need
  373. hmax = (peaks[wvoice->n_harmonic_peaks].freq + peaks[wvoice->n_harmonic_peaks].right)/pitch;
  374. if (hmax >= MAX_HARMONIC)
  375. hmax = MAX_HARMONIC-1;
  376. // restrict highest harmonic to half the samplerate
  377. hmax_samplerate = (((samplerate * 19)/40) << 16)/pitch; // only 95% of Nyquist freq
  378. if (hmax > hmax_samplerate)
  379. hmax = hmax_samplerate;
  380. for (h = 0; h <= hmax; h++)
  381. htab[h] = 0;
  382. for (pk = 0; pk <= wvoice->n_harmonic_peaks; pk++) {
  383. p = &peaks[pk];
  384. if ((p->height == 0) || (fp = p->freq) == 0)
  385. continue;
  386. fhi = p->freq + p->right;
  387. h = ((p->freq - p->left) / pitch) + 1;
  388. if (h <= 0) h = 1;
  389. for (f = pitch*h; f < fp; f += pitch)
  390. htab[h++] += pk_shape[(fp-f)/(p->left>>8)] * p->height;
  391. for (; f < fhi; f += pitch)
  392. htab[h++] += pk_shape[(f-fp)/(p->right>>8)] * p->height;
  393. }
  394. int y;
  395. int h2;
  396. // increase bass
  397. y = peaks[1].height * 10; // addition as a multiple of 1/256s
  398. h2 = (1000<<16)/pitch; // decrease until 1000Hz
  399. if (h2 > 0) {
  400. x = y/h2;
  401. h = 1;
  402. while (y > 0) {
  403. htab[h++] += y;
  404. y -= x;
  405. }
  406. }
  407. // find the nearest harmonic for HF peaks where we don't use shape
  408. for (; pk < N_PEAKS; pk++) {
  409. x = peaks[pk].height >> 14;
  410. peak_height[pk] = (x * x * 5)/2;
  411. // find the nearest harmonic for HF peaks where we don't use shape
  412. if (control == 0) {
  413. // set this initially, but make changes only at the quiet point
  414. peak_harmonic[pk] = peaks[pk].freq / pitch;
  415. }
  416. // only use harmonics up to half the samplerate
  417. if (peak_harmonic[pk] >= hmax_samplerate)
  418. peak_height[pk] = 0;
  419. }
  420. // convert from the square-rooted values
  421. f = 0;
  422. for (h = 0; h <= hmax; h++, f += pitch) {
  423. x = htab[h] >> 15;
  424. htab[h] = (x * x) >> 8;
  425. if ((ix = (f >> 19)) < N_TONE_ADJUST)
  426. htab[h] = (htab[h] * wvoice->tone_adjust[ix]) >> 13; // index tone_adjust with Hz/8
  427. }
  428. // adjust the amplitude of the first harmonic, affects tonal quality
  429. h1 = htab[1] * option_harmonic1;
  430. htab[1] = h1/8;
  431. // calc intermediate increments of LF harmonics
  432. if (control & 1) {
  433. for (h = 1; h < N_LOWHARM; h++)
  434. harm_inc[h] = (htab[h] - harmspect[h]) >> 3;
  435. }
  436. return hmax; // highest harmonic number
  437. }
  438. static void AdvanceParameters()
  439. {
  440. // Called every 64 samples to increment the formant freq, height, and widths
  441. if (wvoice == NULL)
  442. return;
  443. int x;
  444. int ix;
  445. static int Flutter_ix = 0;
  446. // advance the pitch
  447. wdata.pitch_ix += wdata.pitch_inc;
  448. if ((ix = wdata.pitch_ix>>8) > 127) ix = 127;
  449. x = wdata.pitch_env[ix] * wdata.pitch_range;
  450. wdata.pitch = (x>>8) + wdata.pitch_base;
  451. amp_ix += amp_inc;
  452. /* add pitch flutter */
  453. if (Flutter_ix >= (N_FLUTTER*64))
  454. Flutter_ix = 0;
  455. x = ((int)(Flutter_tab[Flutter_ix >> 6])-0x80) * flutter_amp;
  456. Flutter_ix += Flutter_inc;
  457. wdata.pitch += x;
  458. if (wdata.pitch < 102400)
  459. wdata.pitch = 102400; // min pitch, 25 Hz (25 << 12)
  460. if (samplecount == samplecount_start)
  461. return;
  462. for (ix = 0; ix <= wvoice->n_harmonic_peaks; ix++) {
  463. peaks[ix].freq1 += peaks[ix].freq_inc;
  464. peaks[ix].freq = (int)peaks[ix].freq1;
  465. peaks[ix].height1 += peaks[ix].height_inc;
  466. if ((peaks[ix].height = (int)peaks[ix].height1) < 0)
  467. peaks[ix].height = 0;
  468. peaks[ix].left1 += peaks[ix].left_inc;
  469. peaks[ix].left = (int)peaks[ix].left1;
  470. if (ix < 3) {
  471. peaks[ix].right1 += peaks[ix].right_inc;
  472. peaks[ix].right = (int)peaks[ix].right1;
  473. } else
  474. peaks[ix].right = peaks[ix].left;
  475. }
  476. for (; ix < 8; ix++) {
  477. // formants 6,7,8 don't have a width parameter
  478. if (ix < 7) {
  479. peaks[ix].freq1 += peaks[ix].freq_inc;
  480. peaks[ix].freq = (int)peaks[ix].freq1;
  481. }
  482. peaks[ix].height1 += peaks[ix].height_inc;
  483. if ((peaks[ix].height = (int)peaks[ix].height1) < 0)
  484. peaks[ix].height = 0;
  485. }
  486. }
  487. static double resonator(RESONATOR *r, double input)
  488. {
  489. double x;
  490. x = r->a * input + r->b * r->x1 + r->c * r->x2;
  491. r->x2 = r->x1;
  492. r->x1 = x;
  493. return x;
  494. }
  495. static void setresonator(RESONATOR *rp, int freq, int bwidth, int init)
  496. {
  497. // freq Frequency of resonator in Hz
  498. // bwidth Bandwidth of resonator in Hz
  499. // init Initialize internal data
  500. double x;
  501. double arg;
  502. if (init) {
  503. rp->x1 = 0;
  504. rp->x2 = 0;
  505. }
  506. arg = minus_pi_t * bwidth;
  507. x = exp(arg);
  508. rp->c = -(x * x);
  509. arg = two_pi_t * freq;
  510. rp->b = x * cos(arg) * 2.0;
  511. rp->a = 1.0 - rp->b - rp->c;
  512. }
  513. void InitBreath(void)
  514. {
  515. int ix;
  516. minus_pi_t = -M_PI / samplerate;
  517. two_pi_t = -2.0 * minus_pi_t;
  518. for (ix = 0; ix < N_PEAKS; ix++)
  519. setresonator(&rbreath[ix], 2000, 200, 1);
  520. }
  521. static void SetBreath()
  522. {
  523. int pk;
  524. if (wvoice == NULL || wvoice->breath[0] == 0)
  525. return;
  526. for (pk = 1; pk < N_PEAKS; pk++) {
  527. if (wvoice->breath[pk] != 0) {
  528. // breath[0] indicates that some breath formants are needed
  529. // set the freq from the current synthesis formant and the width from the voice data
  530. setresonator(&rbreath[pk], peaks[pk].freq >> 16, wvoice->breathw[pk], 0);
  531. }
  532. }
  533. }
  534. static int ApplyBreath(void)
  535. {
  536. if (wvoice == NULL)
  537. return 0;
  538. int value = 0;
  539. int noise;
  540. int ix;
  541. int amp;
  542. // use two random numbers, for alternate formants
  543. noise = (rand() & 0x3fff) - 0x2000;
  544. for (ix = 1; ix < N_PEAKS; ix++) {
  545. if ((amp = wvoice->breath[ix]) != 0) {
  546. amp *= (peaks[ix].height >> 14);
  547. value += (int)resonator(&rbreath[ix], noise) * amp;
  548. }
  549. }
  550. return value;
  551. }
  552. static int Wavegen()
  553. {
  554. if (wvoice == NULL)
  555. return 0;
  556. unsigned short waveph;
  557. unsigned short theta;
  558. int total;
  559. int h;
  560. int ix;
  561. int z, z1, z2;
  562. int echo;
  563. int ov;
  564. static int maxh, maxh2;
  565. int pk;
  566. signed char c;
  567. int sample;
  568. int amp;
  569. int modn_amp = 1, modn_period;
  570. static int agc = 256;
  571. static int h_switch_sign = 0;
  572. static int cycle_count = 0;
  573. static int amplitude2 = 0; // adjusted for pitch
  574. // continue until the output buffer is full, or
  575. // the required number of samples have been produced
  576. for (;;) {
  577. if ((end_wave == 0) && (samplecount == nsamples))
  578. return 0;
  579. if ((samplecount & 0x3f) == 0) {
  580. // every 64 samples, adjust the parameters
  581. if (samplecount == 0) {
  582. hswitch = 0;
  583. harmspect = hspect[0];
  584. maxh2 = PeaksToHarmspect(peaks, wdata.pitch<<4, hspect[0], 0);
  585. // adjust amplitude to compensate for fewer harmonics at higher pitch
  586. amplitude2 = (wdata.amplitude * (wdata.pitch >> 8) * wdata.amplitude_fmt)/(10000 << 3);
  587. // switch sign of harmonics above about 900Hz, to reduce max peak amplitude
  588. h_switch_sign = 890 / (wdata.pitch >> 12);
  589. } else
  590. AdvanceParameters();
  591. // pitch is Hz<<12
  592. phaseinc = (wdata.pitch>>7) * PHASE_INC_FACTOR;
  593. cycle_samples = samplerate/(wdata.pitch >> 12); // sr/(pitch*2)
  594. hf_factor = wdata.pitch >> 11;
  595. maxh = maxh2;
  596. harmspect = hspect[hswitch];
  597. hswitch ^= 1;
  598. maxh2 = PeaksToHarmspect(peaks, wdata.pitch<<4, hspect[hswitch], 1);
  599. SetBreath();
  600. } else if ((samplecount & 0x07) == 0) {
  601. for (h = 1; h < N_LOWHARM && h <= maxh2 && h <= maxh; h++)
  602. harmspect[h] += harm_inc[h];
  603. // bring automatic gain control back towards unity
  604. if (agc < 256) agc++;
  605. }
  606. samplecount++;
  607. if (wavephase > 0) {
  608. wavephase += phaseinc;
  609. if (wavephase < 0) {
  610. // sign has changed, reached a quiet point in the waveform
  611. cbytes = wavemult_offset - (cycle_samples)/2;
  612. if (samplecount > nsamples)
  613. return 0;
  614. cycle_count++;
  615. for (pk = wvoice->n_harmonic_peaks+1; pk < N_PEAKS; pk++) {
  616. // find the nearest harmonic for HF peaks where we don't use shape
  617. peak_harmonic[pk] = ((peaks[pk].freq / (wdata.pitch*8)) + 1) / 2;
  618. }
  619. // adjust amplitude to compensate for fewer harmonics at higher pitch
  620. amplitude2 = (wdata.amplitude * (wdata.pitch >> 8) * wdata.amplitude_fmt)/(10000 << 3);
  621. if (glottal_flag > 0) {
  622. if (glottal_flag == 3) {
  623. if ((nsamples-samplecount) < (cycle_samples*2)) {
  624. // Vowel before glottal-stop.
  625. // This is the start of the penultimate cycle, reduce its amplitude
  626. glottal_flag = 2;
  627. amplitude2 = (amplitude2 * glottal_reduce)/256;
  628. }
  629. } else if (glottal_flag == 4) {
  630. // Vowel following a glottal-stop.
  631. // This is the start of the second cycle, reduce its amplitude
  632. glottal_flag = 2;
  633. amplitude2 = (amplitude2 * glottal_reduce)/256;
  634. } else
  635. glottal_flag--;
  636. }
  637. if (amplitude_env != NULL) {
  638. // amplitude envelope is only used for creaky voice effect on certain vowels/tones
  639. if ((ix = amp_ix>>8) > 127) ix = 127;
  640. amp = amplitude_env[ix];
  641. amplitude2 = (amplitude2 * amp)/128;
  642. }
  643. // introduce roughness into the sound by reducing the amplitude of
  644. modn_period = 0;
  645. if (voice->roughness < N_ROUGHNESS) {
  646. modn_period = modulation_tab[voice->roughness][modulation_type];
  647. modn_amp = modn_period & 0xf;
  648. modn_period = modn_period >> 4;
  649. }
  650. if (modn_period != 0) {
  651. if (modn_period == 0xf) {
  652. // just once */
  653. amplitude2 = (amplitude2 * modn_amp)/16;
  654. modulation_type = 0;
  655. } else {
  656. // reduce amplitude every [modn_period} cycles
  657. if ((cycle_count % modn_period) == 0)
  658. amplitude2 = (amplitude2 * modn_amp)/16;
  659. }
  660. }
  661. }
  662. } else
  663. wavephase += phaseinc;
  664. waveph = (unsigned short)(wavephase >> 16);
  665. total = 0;
  666. // apply HF peaks, formants 6,7,8
  667. // add a single harmonic and then spread this my multiplying by a
  668. // window. This is to reduce the processing power needed to add the
  669. // higher frequence harmonics.
  670. cbytes++;
  671. if (cbytes >= 0 && cbytes < wavemult_max) {
  672. for (pk = wvoice->n_harmonic_peaks+1; pk < N_PEAKS; pk++) {
  673. theta = peak_harmonic[pk] * waveph;
  674. total += (long)sin_tab[theta >> 5] * peak_height[pk];
  675. }
  676. // spread the peaks by multiplying by a window
  677. total = (long)(total / hf_factor) * wavemult[cbytes];
  678. }
  679. // apply main peaks, formants 0 to 5
  680. #ifdef USE_ASSEMBLER_1
  681. // use an optimised routine for this loop, if available
  682. total += AddSineWaves(waveph, h_switch_sign, maxh, harmspect); // call an assembler code routine
  683. #else
  684. theta = waveph;
  685. for (h = 1; h <= h_switch_sign; h++) {
  686. total += ((int)sin_tab[theta >> 5] * harmspect[h]);
  687. theta += waveph;
  688. }
  689. while (h <= maxh) {
  690. total -= ((int)sin_tab[theta >> 5] * harmspect[h]);
  691. theta += waveph;
  692. h++;
  693. }
  694. #endif
  695. if (voicing != 64)
  696. total = (total >> 6) * voicing;
  697. if (wvoice->breath[0])
  698. total += ApplyBreath();
  699. // mix with sampled wave if required
  700. z2 = 0;
  701. if (wdata.mix_wavefile_ix < wdata.n_mix_wavefile) {
  702. if (wdata.mix_wave_scale == 0) {
  703. // a 16 bit sample
  704. c = wdata.mix_wavefile[wdata.mix_wavefile_ix+wdata.mix_wavefile_offset+1];
  705. sample = wdata.mix_wavefile[wdata.mix_wavefile_ix+wdata.mix_wavefile_offset] + (c * 256);
  706. wdata.mix_wavefile_ix += 2;
  707. } else {
  708. // a 8 bit sample, scaled
  709. sample = (signed char)wdata.mix_wavefile[wdata.mix_wavefile_offset+wdata.mix_wavefile_ix++] * wdata.mix_wave_scale;
  710. }
  711. z2 = (sample * wdata.amplitude_v) >> 10;
  712. z2 = (z2 * wdata.mix_wave_amp)/32;
  713. if ((wdata.mix_wavefile_ix + wdata.mix_wavefile_offset) >= wdata.mix_wavefile_max) // reached the end of available WAV data
  714. wdata.mix_wavefile_offset -= (wdata.mix_wavefile_max*3)/4;
  715. }
  716. z1 = z2 + (((total>>8) * amplitude2) >> 13);
  717. echo = (echo_buf[echo_tail++] * echo_amp);
  718. z1 += echo >> 8;
  719. if (echo_tail >= N_ECHO_BUF)
  720. echo_tail = 0;
  721. z = (z1 * agc) >> 8;
  722. // check for overflow, 16bit signed samples
  723. if (z >= 32768) {
  724. ov = 8388608/z1 - 1; // 8388608 is 2^23, i.e. max value * 256
  725. if (ov < agc) agc = ov; // set agc to number of 1/256ths to multiply the sample by
  726. z = (z1 * agc) >> 8; // reduce sample by agc value to prevent overflow
  727. } else if (z <= -32768) {
  728. ov = -8388608/z1 - 1;
  729. if (ov < agc) agc = ov;
  730. z = (z1 * agc) >> 8;
  731. }
  732. *out_ptr++ = z;
  733. *out_ptr++ = z >> 8;
  734. echo_buf[echo_head++] = z;
  735. if (echo_head >= N_ECHO_BUF)
  736. echo_head = 0;
  737. if (out_ptr >= out_end)
  738. return 1;
  739. }
  740. }
  741. static int PlaySilence(int length, bool resume)
  742. {
  743. static int n_samples;
  744. int value = 0;
  745. nsamples = 0;
  746. samplecount = 0;
  747. wavephase = 0x7fffffff;
  748. if (length == 0)
  749. return 0;
  750. if (resume == false)
  751. n_samples = length;
  752. while (n_samples-- > 0) {
  753. value = (echo_buf[echo_tail++] * echo_amp) >> 8;
  754. if (echo_tail >= N_ECHO_BUF)
  755. echo_tail = 0;
  756. *out_ptr++ = value;
  757. *out_ptr++ = value >> 8;
  758. echo_buf[echo_head++] = value;
  759. if (echo_head >= N_ECHO_BUF)
  760. echo_head = 0;
  761. if (out_ptr >= out_end)
  762. return 1;
  763. }
  764. return 0;
  765. }
  766. static int PlayWave(int length, bool resume, unsigned char *data, int scale, int amp)
  767. {
  768. static int n_samples;
  769. static int ix = 0;
  770. int value;
  771. signed char c;
  772. if (resume == false) {
  773. n_samples = length;
  774. ix = 0;
  775. }
  776. nsamples = 0;
  777. samplecount = 0;
  778. while (n_samples-- > 0) {
  779. if (scale == 0) {
  780. // 16 bits data
  781. c = data[ix+1];
  782. value = data[ix] + (c * 256);
  783. ix += 2;
  784. } else {
  785. // 8 bit data, shift by the specified scale factor
  786. value = (signed char)data[ix++] * scale;
  787. }
  788. value *= (consonant_amp * general_amplitude); // reduce strength of consonant
  789. value = value >> 10;
  790. value = (value * amp)/32;
  791. value += ((echo_buf[echo_tail++] * echo_amp) >> 8);
  792. if (value > 32767)
  793. value = 32768;
  794. else if (value < -32768)
  795. value = -32768;
  796. if (echo_tail >= N_ECHO_BUF)
  797. echo_tail = 0;
  798. out_ptr[0] = value;
  799. out_ptr[1] = value >> 8;
  800. out_ptr += 2;
  801. echo_buf[echo_head++] = (value*3)/4;
  802. if (echo_head >= N_ECHO_BUF)
  803. echo_head = 0;
  804. if (out_ptr >= out_end)
  805. return 1;
  806. }
  807. return 0;
  808. }
  809. static int SetWithRange0(int value, int max)
  810. {
  811. if (value < 0)
  812. return 0;
  813. if (value > max)
  814. return max;
  815. return value;
  816. }
  817. static void SetPitchFormants()
  818. {
  819. if (wvoice == NULL)
  820. return;
  821. int ix;
  822. int factor = 256;
  823. int pitch_value;
  824. // adjust formants to give better results for a different voice pitch
  825. if ((pitch_value = embedded_value[EMBED_P]) > MAX_PITCH_VALUE)
  826. pitch_value = MAX_PITCH_VALUE;
  827. if (pitch_value > 50) {
  828. // only adjust if the pitch is higher than normal
  829. factor = 256 + (25 * (pitch_value - 50))/50;
  830. }
  831. for (ix = 0; ix <= 5; ix++)
  832. wvoice->freq[ix] = (wvoice->freq2[ix] * factor)/256;
  833. factor = embedded_value[EMBED_T]*3;
  834. wvoice->height[0] = (wvoice->height2[0] * (256 - factor*2))/256;
  835. wvoice->height[1] = (wvoice->height2[1] * (256 - factor))/256;
  836. }
  837. void SetEmbedded(int control, int value)
  838. {
  839. // there was an embedded command in the text at this point
  840. int sign = 0;
  841. int command;
  842. command = control & 0x1f;
  843. if ((control & 0x60) == 0x60)
  844. sign = -1;
  845. else if ((control & 0x60) == 0x40)
  846. sign = 1;
  847. if (command < N_EMBEDDED_VALUES) {
  848. if (sign == 0)
  849. embedded_value[command] = value;
  850. else
  851. embedded_value[command] += (value * sign);
  852. embedded_value[command] = SetWithRange0(embedded_value[command], embedded_max[command]);
  853. }
  854. switch (command)
  855. {
  856. case EMBED_T:
  857. WavegenSetEcho(); // and drop through to case P
  858. case EMBED_P:
  859. SetPitchFormants();
  860. break;
  861. case EMBED_A: // amplitude
  862. general_amplitude = GetAmplitude();
  863. break;
  864. case EMBED_F: // emphasis
  865. general_amplitude = GetAmplitude();
  866. break;
  867. case EMBED_H:
  868. WavegenSetEcho();
  869. break;
  870. }
  871. }
  872. void WavegenSetVoice(voice_t *v)
  873. {
  874. static voice_t v2;
  875. memcpy(&v2, v, sizeof(v2));
  876. wvoice = &v2;
  877. if (v->peak_shape == 0)
  878. pk_shape = pk_shape1;
  879. else
  880. pk_shape = pk_shape2;
  881. consonant_amp = (v->consonant_amp * 26) /100;
  882. if (samplerate <= 11000) {
  883. consonant_amp = consonant_amp*2; // emphasize consonants at low sample rates
  884. option_harmonic1 = 6;
  885. }
  886. WavegenSetEcho();
  887. SetPitchFormants();
  888. MarkerEvent(espeakEVENT_SAMPLERATE, 0, wvoice->samplerate, 0, out_ptr);
  889. }
  890. static void SetAmplitude(int length, unsigned char *amp_env, int value)
  891. {
  892. if (wvoice == NULL)
  893. return;
  894. amp_ix = 0;
  895. if (length == 0)
  896. amp_inc = 0;
  897. else
  898. amp_inc = (256 * ENV_LEN * STEPSIZE)/length;
  899. wdata.amplitude = (value * general_amplitude)/16;
  900. wdata.amplitude_v = (wdata.amplitude * wvoice->consonant_ampv * 15)/100; // for wave mixed with voiced sounds
  901. amplitude_env = amp_env;
  902. }
  903. void SetPitch2(voice_t *voice, int pitch1, int pitch2, int *pitch_base, int *pitch_range)
  904. {
  905. int x;
  906. int base;
  907. int range;
  908. int pitch_value;
  909. if (pitch1 > pitch2) {
  910. x = pitch1; // swap values
  911. pitch1 = pitch2;
  912. pitch2 = x;
  913. }
  914. if ((pitch_value = embedded_value[EMBED_P]) > MAX_PITCH_VALUE)
  915. pitch_value = MAX_PITCH_VALUE;
  916. pitch_value -= embedded_value[EMBED_T]; // adjust tone for announcing punctuation
  917. if (pitch_value < 0)
  918. pitch_value = 0;
  919. base = (voice->pitch_base * pitch_adjust_tab[pitch_value])/128;
  920. range = (voice->pitch_range * embedded_value[EMBED_R])/50;
  921. // compensate for change in pitch when the range is narrowed or widened
  922. base -= (range - voice->pitch_range)*18;
  923. *pitch_base = base + (pitch1 * range)/2;
  924. *pitch_range = base + (pitch2 * range)/2 - *pitch_base;
  925. }
  926. static void SetPitch(int length, unsigned char *env, int pitch1, int pitch2)
  927. {
  928. if (wvoice == NULL)
  929. return;
  930. // length in samples
  931. if ((wdata.pitch_env = env) == NULL)
  932. wdata.pitch_env = env_fall; // default
  933. wdata.pitch_ix = 0;
  934. if (length == 0)
  935. wdata.pitch_inc = 0;
  936. else
  937. wdata.pitch_inc = (256 * ENV_LEN * STEPSIZE)/length;
  938. SetPitch2(wvoice, pitch1, pitch2, &wdata.pitch_base, &wdata.pitch_range);
  939. // set initial pitch
  940. wdata.pitch = ((wdata.pitch_env[0] * wdata.pitch_range) >>8) + wdata.pitch_base; // Hz << 12
  941. flutter_amp = wvoice->flutter;
  942. }
  943. static void SetSynth(int length, int modn, frame_t *fr1, frame_t *fr2, voice_t *v)
  944. {
  945. if (wvoice == NULL || v == NULL)
  946. return;
  947. int ix;
  948. double next;
  949. int length2;
  950. int length4;
  951. int qix;
  952. int cmd;
  953. static int glottal_reduce_tab1[4] = { 0x30, 0x30, 0x40, 0x50 }; // vowel before [?], amp * 1/256
  954. static int glottal_reduce_tab2[4] = { 0x90, 0xa0, 0xb0, 0xc0 }; // vowel after [?], amp * 1/256
  955. harm_sqrt_n = 0;
  956. end_wave = 1;
  957. // any additional information in the param1 ?
  958. modulation_type = modn & 0xff;
  959. glottal_flag = 0;
  960. if (modn & 0x400) {
  961. glottal_flag = 3; // before a glottal stop
  962. glottal_reduce = glottal_reduce_tab1[(modn >> 8) & 3];
  963. }
  964. if (modn & 0x800) {
  965. glottal_flag = 4; // after a glottal stop
  966. glottal_reduce = glottal_reduce_tab2[(modn >> 8) & 3];
  967. }
  968. for (qix = wcmdq_head+1;; qix++) {
  969. if (qix >= N_WCMDQ) qix = 0;
  970. if (qix == wcmdq_tail) break;
  971. cmd = wcmdq[qix][0];
  972. if (cmd == WCMD_SPECT) {
  973. end_wave = 0; // next wave generation is from another spectrum
  974. break;
  975. }
  976. if ((cmd == WCMD_WAVE) || (cmd == WCMD_PAUSE))
  977. break; // next is not from spectrum, so continue until end of wave cycle
  978. }
  979. // round the length to a multiple of the stepsize
  980. length2 = (length + STEPSIZE/2) & ~0x3f;
  981. if (length2 == 0)
  982. length2 = STEPSIZE;
  983. // add this length to any left over from the previous synth
  984. samplecount_start = samplecount;
  985. nsamples += length2;
  986. length4 = length2/4;
  987. peaks[7].freq = (7800 * v->freq[7] + v->freqadd[7]*256) << 8;
  988. peaks[8].freq = (9000 * v->freq[8] + v->freqadd[8]*256) << 8;
  989. for (ix = 0; ix < 8; ix++) {
  990. if (ix < 7) {
  991. peaks[ix].freq1 = (fr1->ffreq[ix] * v->freq[ix] + v->freqadd[ix]*256) << 8;
  992. peaks[ix].freq = (int)peaks[ix].freq1;
  993. next = (fr2->ffreq[ix] * v->freq[ix] + v->freqadd[ix]*256) << 8;
  994. peaks[ix].freq_inc = ((next - peaks[ix].freq1) * (STEPSIZE/4)) / length4; // lower headroom for fixed point math
  995. }
  996. peaks[ix].height1 = (fr1->fheight[ix] * v->height[ix]) << 6;
  997. peaks[ix].height = (int)peaks[ix].height1;
  998. next = (fr2->fheight[ix] * v->height[ix]) << 6;
  999. peaks[ix].height_inc = ((next - peaks[ix].height1) * STEPSIZE) / length2;
  1000. if ((ix <= 5) && (ix <= wvoice->n_harmonic_peaks)) {
  1001. peaks[ix].left1 = (fr1->fwidth[ix] * v->width[ix]) << 10;
  1002. peaks[ix].left = (int)peaks[ix].left1;
  1003. next = (fr2->fwidth[ix] * v->width[ix]) << 10;
  1004. peaks[ix].left_inc = ((next - peaks[ix].left1) * STEPSIZE) / length2;
  1005. if (ix < 3) {
  1006. peaks[ix].right1 = (fr1->fright[ix] * v->width[ix]) << 10;
  1007. peaks[ix].right = (int)peaks[ix].right1;
  1008. next = (fr2->fright[ix] * v->width[ix]) << 10;
  1009. peaks[ix].right_inc = ((next - peaks[ix].right1) * STEPSIZE) / length2;
  1010. } else
  1011. peaks[ix].right = peaks[ix].left;
  1012. }
  1013. }
  1014. }
  1015. static int Wavegen2(int length, int modulation, bool resume, frame_t *fr1, frame_t *fr2)
  1016. {
  1017. if (resume == false)
  1018. SetSynth(length, modulation, fr1, fr2, wvoice);
  1019. return Wavegen();
  1020. }
  1021. void Write4Bytes(FILE *f, int value)
  1022. {
  1023. // Write 4 bytes to a file, least significant first
  1024. int ix;
  1025. for (ix = 0; ix < 4; ix++) {
  1026. fputc(value & 0xff, f);
  1027. value = value >> 8;
  1028. }
  1029. }
  1030. static int WavegenFill2()
  1031. {
  1032. // Pick up next wavegen commands from the queue
  1033. // return: 0 output buffer has been filled
  1034. // return: 1 input command queue is now empty
  1035. intptr_t *q;
  1036. int length;
  1037. int result;
  1038. int marker_type;
  1039. static bool resume = false;
  1040. static int echo_complete = 0;
  1041. while (out_ptr < out_end) {
  1042. if (WcmdqUsed() <= 0) {
  1043. if (echo_complete > 0) {
  1044. // continue to play silence until echo is completed
  1045. resume = PlaySilence(echo_complete, resume);
  1046. if (resume == true)
  1047. return 0; // not yet finished
  1048. }
  1049. return 1; // queue empty, close sound channel
  1050. }
  1051. result = 0;
  1052. q = wcmdq[wcmdq_head];
  1053. length = q[1];
  1054. switch (q[0] & 0xff)
  1055. {
  1056. case WCMD_PITCH:
  1057. SetPitch(length, (unsigned char *)q[2], q[3] >> 16, q[3] & 0xffff);
  1058. break;
  1059. case WCMD_PAUSE:
  1060. if (resume == false)
  1061. echo_complete -= length;
  1062. wdata.n_mix_wavefile = 0;
  1063. wdata.amplitude_fmt = 100;
  1064. #ifdef INCLUDE_KLATT
  1065. KlattReset(1);
  1066. #endif
  1067. result = PlaySilence(length, resume);
  1068. break;
  1069. case WCMD_WAVE:
  1070. echo_complete = echo_length;
  1071. wdata.n_mix_wavefile = 0;
  1072. #ifdef INCLUDE_KLATT
  1073. KlattReset(1);
  1074. #endif
  1075. result = PlayWave(length, resume, (unsigned char *)q[2], q[3] & 0xff, q[3] >> 8);
  1076. break;
  1077. case WCMD_WAVE2:
  1078. // wave file to be played at the same time as synthesis
  1079. wdata.mix_wave_amp = q[3] >> 8;
  1080. wdata.mix_wave_scale = q[3] & 0xff;
  1081. wdata.n_mix_wavefile = (length & 0xffff);
  1082. wdata.mix_wavefile_max = (length >> 16) & 0xffff;
  1083. if (wdata.mix_wave_scale == 0) {
  1084. wdata.n_mix_wavefile *= 2;
  1085. wdata.mix_wavefile_max *= 2;
  1086. }
  1087. wdata.mix_wavefile_ix = 0;
  1088. wdata.mix_wavefile_offset = 0;
  1089. wdata.mix_wavefile = (unsigned char *)q[2];
  1090. break;
  1091. case WCMD_SPECT2: // as WCMD_SPECT but stop any concurrent wave file
  1092. wdata.n_mix_wavefile = 0; // ... and drop through to WCMD_SPECT case
  1093. case WCMD_SPECT:
  1094. echo_complete = echo_length;
  1095. result = Wavegen2(length & 0xffff, q[1] >> 16, resume, (frame_t *)q[2], (frame_t *)q[3]);
  1096. break;
  1097. #ifdef INCLUDE_KLATT
  1098. case WCMD_KLATT2: // as WCMD_SPECT but stop any concurrent wave file
  1099. wdata.n_mix_wavefile = 0; // ... and drop through to WCMD_SPECT case
  1100. case WCMD_KLATT:
  1101. echo_complete = echo_length;
  1102. result = Wavegen_Klatt2(length & 0xffff, resume, (frame_t *)q[2], (frame_t *)q[3]);
  1103. break;
  1104. #endif
  1105. case WCMD_MARKER:
  1106. marker_type = q[0] >> 8;
  1107. MarkerEvent(marker_type, q[1], q[2], q[3], out_ptr);
  1108. if (marker_type == 1) // word marker
  1109. current_source_index = q[1] & 0xffffff;
  1110. break;
  1111. case WCMD_AMPLITUDE:
  1112. SetAmplitude(length, (unsigned char *)q[2], q[3]);
  1113. break;
  1114. case WCMD_VOICE:
  1115. WavegenSetVoice((voice_t *)q[2]);
  1116. free((voice_t *)q[2]);
  1117. break;
  1118. case WCMD_EMBEDDED:
  1119. SetEmbedded(q[1], q[2]);
  1120. break;
  1121. case WCMD_MBROLA_DATA:
  1122. if (wvoice != NULL)
  1123. result = MbrolaFill(length, resume, (general_amplitude * wvoice->voicing)/64);
  1124. break;
  1125. case WCMD_FMT_AMPLITUDE:
  1126. if ((wdata.amplitude_fmt = q[1]) == 0)
  1127. wdata.amplitude_fmt = 100; // percentage, but value=0 means 100%
  1128. break;
  1129. #if HAVE_SONIC_H
  1130. case WCMD_SONIC_SPEED:
  1131. sonicSpeed = (double)q[1] / 1024;
  1132. break;
  1133. #endif
  1134. }
  1135. if (result == 0) {
  1136. WcmdqIncHead();
  1137. resume = false;
  1138. } else
  1139. resume = true;
  1140. }
  1141. return 0;
  1142. }
  1143. #if HAVE_SONIC_H
  1144. // Speed up the audio samples with libsonic.
  1145. static int SpeedUp(short *outbuf, int length_in, int length_out, int end_of_text)
  1146. {
  1147. if (length_in > 0) {
  1148. if (sonicSpeedupStream == NULL)
  1149. sonicSpeedupStream = sonicCreateStream(22050, 1);
  1150. if (sonicGetSpeed(sonicSpeedupStream) != sonicSpeed)
  1151. sonicSetSpeed(sonicSpeedupStream, sonicSpeed);
  1152. sonicWriteShortToStream(sonicSpeedupStream, outbuf, length_in);
  1153. }
  1154. if (sonicSpeedupStream == NULL)
  1155. return 0;
  1156. if (end_of_text)
  1157. sonicFlushStream(sonicSpeedupStream);
  1158. return sonicReadShortFromStream(sonicSpeedupStream, outbuf, length_out);
  1159. }
  1160. #endif
  1161. // Call WavegenFill2, and then speed up the output samples.
  1162. int WavegenFill()
  1163. {
  1164. int finished;
  1165. unsigned char *p_start;
  1166. p_start = out_ptr;
  1167. finished = WavegenFill2();
  1168. #if HAVE_SONIC_H
  1169. if (sonicSpeed > 1.0) {
  1170. int length;
  1171. int max_length;
  1172. max_length = (out_end - p_start);
  1173. length = 2*SpeedUp((short *)p_start, (out_ptr-p_start)/2, max_length/2, finished);
  1174. out_ptr = p_start + length;
  1175. if (length >= max_length)
  1176. finished = 0; // there may be more data to flush
  1177. }
  1178. #endif
  1179. return finished;
  1180. }