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

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