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

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