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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2013 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * Copyright (C) 2015 Reece H. Dunn *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, see: *
  18. * <http://www.gnu.org/licenses/>. *
  19. ***************************************************************************/
  20. // this version keeps wavemult window as a constant fraction
  21. // of the cycle length - but that spreads out the HF peaks too much
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <math.h>
  26. #include "speak_lib.h"
  27. #include "speech.h"
  28. #include "phoneme.h"
  29. #include "synthesize.h"
  30. #include "voice.h"
  31. #ifdef INCLUDE_SONIC
  32. #include "sonic.h"
  33. #endif
  34. #ifdef USE_PORTAUDIO
  35. #include "portaudio.h"
  36. #undef USE_PORTAUDIO
  37. // determine portaudio version by looking for a #define which is not in V18
  38. #ifdef paNeverDropInput
  39. #define USE_PORTAUDIO 19
  40. #else
  41. #define USE_PORTAUDIO 18
  42. #endif
  43. #endif
  44. #define N_SINTAB 2048
  45. #include "sintab.h"
  46. #define PI 3.1415927
  47. #define PI2 6.283185307
  48. #define N_WAV_BUF 10
  49. voice_t *wvoice;
  50. FILE *f_log = NULL;
  51. int option_waveout = 0;
  52. static int option_harmonic1 = 10; // 10
  53. int option_log_frames = 0;
  54. static int flutter_amp = 64;
  55. static int general_amplitude = 60;
  56. static int consonant_amp = 26; // 24
  57. int embedded_value[N_EMBEDDED_VALUES];
  58. static int PHASE_INC_FACTOR;
  59. int samplerate = 0; // this is set by Wavegeninit()
  60. int samplerate_native=0;
  61. extern int option_device_number;
  62. extern int option_quiet;
  63. static wavegen_peaks_t peaks[N_PEAKS];
  64. static int peak_harmonic[N_PEAKS];
  65. static int peak_height[N_PEAKS];
  66. int echo_head;
  67. int echo_tail;
  68. int echo_amp = 0;
  69. short echo_buf[N_ECHO_BUF];
  70. static int echo_length = 0; // period (in sample\) to ensure completion of echo at the end of speech, set in WavegenSetEcho()
  71. static int voicing;
  72. static RESONATOR rbreath[N_PEAKS];
  73. static int harm_sqrt_n = 0;
  74. #define N_LOWHARM 30
  75. static int harm_inc[N_LOWHARM]; // only for these harmonics do we interpolate amplitude between steps
  76. static int *harmspect;
  77. static int hswitch=0;
  78. static int hspect[2][MAX_HARMONIC]; // 2 copies, we interpolate between then
  79. static int max_hval=0;
  80. static int nsamples=0; // number to do
  81. static int modulation_type = 0;
  82. static int glottal_flag = 0;
  83. static int glottal_reduce = 0;
  84. WGEN_DATA wdata;
  85. static int amp_ix;
  86. static int amp_inc;
  87. static unsigned char *amplitude_env = NULL;
  88. static int samplecount=0; // number done
  89. static int samplecount_start=0; // count at start of this segment
  90. static int end_wave=0; // continue to end of wave cycle
  91. static int wavephase;
  92. static int phaseinc;
  93. static int cycle_samples; // number of samples in a cycle at current pitch
  94. static int cbytes;
  95. static int hf_factor;
  96. static double minus_pi_t;
  97. static double two_pi_t;
  98. unsigned char *out_ptr;
  99. unsigned char *out_start;
  100. unsigned char *out_end;
  101. int outbuf_size = 0;
  102. // the queue of operations passed to wavegen from sythesize
  103. long64 wcmdq[N_WCMDQ][4];
  104. int wcmdq_head=0;
  105. int wcmdq_tail=0;
  106. // pitch,speed,
  107. int embedded_default[N_EMBEDDED_VALUES] = {0, 50,175,100,50, 0, 0, 0,175,0,0,0,0,0,0};
  108. static int embedded_max[N_EMBEDDED_VALUES] = {0,0x7fff,750,300,99,99,99, 0,750,0,0,0,0,4,0};
  109. #define N_CALLBACK_IX N_WAV_BUF-2 // adjust this delay to match display with the currently spoken word
  110. int current_source_index=0;
  111. extern FILE *f_wave;
  112. #if (USE_PORTAUDIO == 18)
  113. static PortAudioStream *pa_stream=NULL;
  114. #endif
  115. #if (USE_PORTAUDIO == 19)
  116. static PaStream *pa_stream=NULL;
  117. #endif
  118. #ifdef INCLUDE_SONIC
  119. static sonicStream sonicSpeedupStream = NULL;
  120. double sonicSpeed = 1.0;
  121. #endif
  122. // 1st index=roughness
  123. // 2nd index=modulation_type
  124. // value: bits 0-3 amplitude (16ths), bits 4-7 every n cycles
  125. #define N_ROUGHNESS 8
  126. static unsigned char modulation_tab[N_ROUGHNESS][8] = {
  127. {0, 0x00, 0x00, 0x00, 0, 0x46, 0xf2, 0x29},
  128. {0, 0x2f, 0x00, 0x2f, 0, 0x45, 0xf2, 0x29},
  129. {0, 0x2f, 0x00, 0x2e, 0, 0x45, 0xf2, 0x28},
  130. {0, 0x2e, 0x00, 0x2d, 0, 0x34, 0xf2, 0x28},
  131. {0, 0x2d, 0x2d, 0x2c, 0, 0x34, 0xf2, 0x28},
  132. {0, 0x2b, 0x2b, 0x2b, 0, 0x34, 0xf2, 0x28},
  133. {0, 0x2a, 0x2a, 0x2a, 0, 0x34, 0xf2, 0x28},
  134. {0, 0x29, 0x29, 0x29, 0, 0x34, 0xf2, 0x28},
  135. };
  136. // Flutter table, to add natural variations to the pitch
  137. #define N_FLUTTER 0x170
  138. static int Flutter_inc;
  139. static const unsigned char Flutter_tab[N_FLUTTER] = {
  140. 0x80, 0x9b, 0xb5, 0xcb, 0xdc, 0xe8, 0xed, 0xec,
  141. 0xe6, 0xdc, 0xce, 0xbf, 0xb0, 0xa3, 0x98, 0x90,
  142. 0x8c, 0x8b, 0x8c, 0x8f, 0x92, 0x94, 0x95, 0x92,
  143. 0x8c, 0x83, 0x78, 0x69, 0x59, 0x49, 0x3c, 0x31,
  144. 0x2a, 0x29, 0x2d, 0x36, 0x44, 0x56, 0x69, 0x7d,
  145. 0x8f, 0x9f, 0xaa, 0xb1, 0xb2, 0xad, 0xa4, 0x96,
  146. 0x87, 0x78, 0x69, 0x5c, 0x53, 0x4f, 0x4f, 0x55,
  147. 0x5e, 0x6b, 0x7a, 0x88, 0x96, 0xa2, 0xab, 0xb0,
  148. 0xb1, 0xae, 0xa8, 0xa0, 0x98, 0x91, 0x8b, 0x88,
  149. 0x89, 0x8d, 0x94, 0x9d, 0xa8, 0xb2, 0xbb, 0xc0,
  150. 0xc1, 0xbd, 0xb4, 0xa5, 0x92, 0x7c, 0x63, 0x4a,
  151. 0x32, 0x1e, 0x0e, 0x05, 0x02, 0x05, 0x0f, 0x1e,
  152. 0x30, 0x44, 0x59, 0x6d, 0x7f, 0x8c, 0x96, 0x9c,
  153. 0x9f, 0x9f, 0x9d, 0x9b, 0x99, 0x99, 0x9c, 0xa1,
  154. 0xa9, 0xb3, 0xbf, 0xca, 0xd5, 0xdc, 0xe0, 0xde,
  155. 0xd8, 0xcc, 0xbb, 0xa6, 0x8f, 0x77, 0x60, 0x4b,
  156. 0x3a, 0x2e, 0x28, 0x29, 0x2f, 0x3a, 0x48, 0x59,
  157. 0x6a, 0x7a, 0x86, 0x90, 0x94, 0x95, 0x91, 0x89,
  158. 0x80, 0x75, 0x6b, 0x62, 0x5c, 0x5a, 0x5c, 0x61,
  159. 0x69, 0x74, 0x80, 0x8a, 0x94, 0x9a, 0x9e, 0x9d,
  160. 0x98, 0x90, 0x86, 0x7c, 0x71, 0x68, 0x62, 0x60,
  161. 0x63, 0x6b, 0x78, 0x88, 0x9b, 0xaf, 0xc2, 0xd2,
  162. 0xdf, 0xe6, 0xe7, 0xe2, 0xd7, 0xc6, 0xb2, 0x9c,
  163. 0x84, 0x6f, 0x5b, 0x4b, 0x40, 0x39, 0x37, 0x38,
  164. 0x3d, 0x43, 0x4a, 0x50, 0x54, 0x56, 0x55, 0x52,
  165. 0x4d, 0x48, 0x42, 0x3f, 0x3e, 0x41, 0x49, 0x56,
  166. 0x67, 0x7c, 0x93, 0xab, 0xc3, 0xd9, 0xea, 0xf6,
  167. 0xfc, 0xfb, 0xf4, 0xe7, 0xd5, 0xc0, 0xaa, 0x94,
  168. 0x80, 0x71, 0x64, 0x5d, 0x5a, 0x5c, 0x61, 0x68,
  169. 0x70, 0x77, 0x7d, 0x7f, 0x7f, 0x7b, 0x74, 0x6b,
  170. 0x61, 0x57, 0x4e, 0x48, 0x46, 0x48, 0x4e, 0x59,
  171. 0x66, 0x75, 0x84, 0x93, 0x9f, 0xa7, 0xab, 0xaa,
  172. 0xa4, 0x99, 0x8b, 0x7b, 0x6a, 0x5b, 0x4e, 0x46,
  173. 0x43, 0x45, 0x4d, 0x5a, 0x6b, 0x7f, 0x92, 0xa6,
  174. 0xb8, 0xc5, 0xcf, 0xd3, 0xd2, 0xcd, 0xc4, 0xb9,
  175. 0xad, 0xa1, 0x96, 0x8e, 0x89, 0x87, 0x87, 0x8a,
  176. 0x8d, 0x91, 0x92, 0x91, 0x8c, 0x84, 0x78, 0x68,
  177. 0x55, 0x41, 0x2e, 0x1c, 0x0e, 0x05, 0x01, 0x05,
  178. 0x0f, 0x1f, 0x34, 0x4d, 0x68, 0x81, 0x9a, 0xb0,
  179. 0xc1, 0xcd, 0xd3, 0xd3, 0xd0, 0xc8, 0xbf, 0xb5,
  180. 0xab, 0xa4, 0x9f, 0x9c, 0x9d, 0xa0, 0xa5, 0xaa,
  181. 0xae, 0xb1, 0xb0, 0xab, 0xa3, 0x96, 0x87, 0x76,
  182. 0x63, 0x51, 0x42, 0x36, 0x2f, 0x2d, 0x31, 0x3a,
  183. 0x48, 0x59, 0x6b, 0x7e, 0x8e, 0x9c, 0xa6, 0xaa,
  184. 0xa9, 0xa3, 0x98, 0x8a, 0x7b, 0x6c, 0x5d, 0x52,
  185. 0x4a, 0x48, 0x4a, 0x50, 0x5a, 0x67, 0x75, 0x82
  186. };
  187. // waveform shape table for HF peaks, formants 6,7,8
  188. #define N_WAVEMULT 128
  189. static int wavemult_offset=0;
  190. static int wavemult_max=0;
  191. // the presets are for 22050 Hz sample rate.
  192. // A different rate will need to recalculate the presets in WavegenInit()
  193. static unsigned char wavemult[N_WAVEMULT] = {
  194. 0, 0, 0, 2, 3, 5, 8, 11, 14, 18, 22, 27, 32, 37, 43, 49,
  195. 55, 62, 69, 76, 83, 90, 98,105,113,121,128,136,144,152,159,166,
  196. 174,181,188,194,201,207,213,218,224,228,233,237,240,244,246,249,
  197. 251,252,253,253,253,253,252,251,249,246,244,240,237,233,228,224,
  198. 218,213,207,201,194,188,181,174,166,159,152,144,136,128,121,113,
  199. 105, 98, 90, 83, 76, 69, 62, 55, 49, 43, 37, 32, 27, 22, 18, 14,
  200. 11, 8, 5, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  201. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  202. // set from y = pow(2,x) * 128, x=-1 to 1
  203. unsigned char pitch_adjust_tab[MAX_PITCH_VALUE+1] = {
  204. 64, 65, 66, 67, 68, 69, 70, 71,
  205. 72, 73, 74, 75, 76, 77, 78, 79,
  206. 80, 81, 82, 83, 84, 86, 87, 88,
  207. 89, 91, 92, 93, 94, 96, 97, 98,
  208. 100,101,103,104,105,107,108,110,
  209. 111,113,115,116,118,119,121,123,
  210. 124,126,128,130,132,133,135,137,
  211. 139,141,143,145,147,149,151,153,
  212. 155,158,160,162,164,167,169,171,
  213. 174,176,179,181,184,186,189,191,
  214. 194,197,199,202,205,208,211,214,
  215. 217,220,223,226,229,232,236,239,
  216. 242,246,249,252, 254,255 };
  217. #ifdef LOG_FRAMES
  218. static void LogMarker(int type, int value, int value2)
  219. {//===================================================
  220. char buf[20];
  221. int *p;
  222. if(option_log_frames == 0)
  223. return;
  224. if((type == espeakEVENT_PHONEME) || (type == espeakEVENT_SENTENCE))
  225. {
  226. f_log=fopen("log-espeakedit","a");
  227. if(f_log)
  228. {
  229. if(type == espeakEVENT_PHONEME)
  230. {
  231. p = (int *)buf;
  232. p[0] = value;
  233. p[1] = value2;
  234. buf[8] = 0;
  235. fprintf(f_log,"Phoneme [%s]\n", buf);
  236. }
  237. else
  238. fprintf(f_log,"\n");
  239. fclose(f_log);
  240. f_log = NULL;
  241. }
  242. }
  243. }
  244. #endif
  245. void WcmdqStop()
  246. {//=============
  247. wcmdq_head = 0;
  248. wcmdq_tail = 0;
  249. #ifdef INCLUDE_SONIC
  250. if(sonicSpeedupStream != NULL)
  251. {
  252. sonicDestroyStream(sonicSpeedupStream);
  253. sonicSpeedupStream = NULL;
  254. }
  255. #endif
  256. #ifdef USE_PORTAUDIO
  257. Pa_AbortStream(pa_stream);
  258. #endif
  259. if(mbrola_name[0] != 0)
  260. MbrolaReset();
  261. }
  262. int WcmdqFree()
  263. {//============
  264. int i;
  265. i = wcmdq_head - wcmdq_tail;
  266. if(i <= 0) i += N_WCMDQ;
  267. return(i);
  268. }
  269. int WcmdqUsed()
  270. {//============
  271. return(N_WCMDQ - WcmdqFree());
  272. }
  273. void WcmdqInc()
  274. {//============
  275. wcmdq_tail++;
  276. if(wcmdq_tail >= N_WCMDQ) wcmdq_tail=0;
  277. }
  278. static void WcmdqIncHead()
  279. {//=======================
  280. wcmdq_head++;
  281. if(wcmdq_head >= N_WCMDQ) wcmdq_head=0;
  282. }
  283. // data points from which to make the presets for pk_shape1 and pk_shape2
  284. #define PEAKSHAPEW 256
  285. static const float pk_shape_x[2][8] = {
  286. {0,-0.6f, 0.0f, 0.6f, 1.4f, 2.5f, 4.5f, 5.5f},
  287. {0,-0.6f, 0.0f, 0.6f, 1.4f, 2.0f, 4.5f, 5.5f }};
  288. static const float pk_shape_y[2][8] = {
  289. {0, 67, 81, 67, 31, 14, 0, -6} ,
  290. {0, 77, 81, 77, 31, 7, 0, -6 }};
  291. unsigned char pk_shape1[PEAKSHAPEW+1] = {
  292. 255,254,254,254,254,254,253,253,252,251,251,250,249,248,247,246,
  293. 245,244,242,241,239,238,236,234,233,231,229,227,225,223,220,218,
  294. 216,213,211,209,207,205,203,201,199,197,195,193,191,189,187,185,
  295. 183,180,178,176,173,171,169,166,164,161,159,156,154,151,148,146,
  296. 143,140,138,135,132,129,126,123,120,118,115,112,108,105,102, 99,
  297. 96, 95, 93, 91, 90, 88, 86, 85, 83, 82, 80, 79, 77, 76, 74, 73,
  298. 72, 70, 69, 68, 67, 66, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55,
  299. 55, 54, 53, 52, 52, 51, 50, 50, 49, 48, 48, 47, 47, 46, 46, 46,
  300. 45, 45, 45, 44, 44, 44, 44, 44, 44, 44, 43, 43, 43, 43, 44, 43,
  301. 42, 42, 41, 40, 40, 39, 38, 38, 37, 36, 36, 35, 35, 34, 33, 33,
  302. 32, 32, 31, 30, 30, 29, 29, 28, 28, 27, 26, 26, 25, 25, 24, 24,
  303. 23, 23, 22, 22, 21, 21, 20, 20, 19, 19, 18, 18, 18, 17, 17, 16,
  304. 16, 15, 15, 15, 14, 14, 13, 13, 13, 12, 12, 11, 11, 11, 10, 10,
  305. 10, 9, 9, 9, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 5, 5,
  306. 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2,
  307. 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  308. 0 };
  309. static unsigned char pk_shape2[PEAKSHAPEW+1] = {
  310. 255,254,254,254,254,254,254,254,254,254,253,253,253,253,252,252,
  311. 252,251,251,251,250,250,249,249,248,248,247,247,246,245,245,244,
  312. 243,243,242,241,239,237,235,233,231,229,227,225,223,221,218,216,
  313. 213,211,208,205,203,200,197,194,191,187,184,181,178,174,171,167,
  314. 163,160,156,152,148,144,140,136,132,127,123,119,114,110,105,100,
  315. 96, 94, 91, 88, 86, 83, 81, 78, 76, 74, 71, 69, 66, 64, 62, 60,
  316. 57, 55, 53, 51, 49, 47, 44, 42, 40, 38, 36, 34, 32, 30, 29, 27,
  317. 25, 23, 21, 19, 18, 16, 14, 12, 11, 9, 7, 6, 4, 3, 1, 0,
  318. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  319. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  320. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  321. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  322. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  323. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  324. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  325. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  326. 0 };
  327. static unsigned char *pk_shape;
  328. #ifdef USE_PORTAUDIO
  329. // PortAudio interface
  330. static int userdata[4];
  331. static PaError pa_init_err=0;
  332. static int out_channels=1;
  333. unsigned char *outbuffer = NULL;
  334. int outbuffer_size = 0;
  335. #if USE_PORTAUDIO == 18
  336. static int WaveCallback(void *inputBuffer, void *outputBuffer,
  337. unsigned long framesPerBuffer, PaTimestamp outTime, void *userData )
  338. #else
  339. static int WaveCallback(const void *inputBuffer, void *outputBuffer,
  340. long unsigned int framesPerBuffer, const PaStreamCallbackTimeInfo *outTime,
  341. PaStreamCallbackFlags flags, void *userData )
  342. #endif
  343. {
  344. int ix;
  345. int result;
  346. unsigned char *p;
  347. unsigned char *out_buf;
  348. unsigned char *out_end2;
  349. int pa_size;
  350. pa_size = framesPerBuffer*2;
  351. // make a buffer 3x size of the portaudio output
  352. ix = pa_size*3;
  353. if(ix > outbuffer_size)
  354. {
  355. outbuffer = (unsigned char *)realloc(outbuffer, ix);
  356. if(outbuffer == NULL)
  357. {
  358. fprintf(stderr, "espeak: out of memory\n");
  359. }
  360. outbuffer_size = ix;
  361. out_ptr = NULL;
  362. }
  363. if(out_ptr == NULL)
  364. {
  365. out_ptr = out_start = outbuffer;
  366. out_end = out_start + outbuffer_size;
  367. }
  368. out_end2 = &outbuffer[pa_size]; // top of data needed for the portaudio buffer
  369. #ifdef LIBRARY
  370. event_list_ix = 0;
  371. #endif
  372. result = WavegenFill(1);
  373. // copy from the outbut buffer into the portaudio buffer
  374. if(result && (out_ptr > out_end2))
  375. {
  376. result = 0; // don't end yet, there is more data in the buffer than can fit in portaudio
  377. }
  378. while(out_ptr < out_end2)
  379. *out_ptr++ = 0; // fill with zeros up to the size of the portaudio buffer
  380. memcpy(outputBuffer, outbuffer, pa_size);
  381. // move the remaining contents of the start of the output buffer
  382. for(p = out_end2; p < out_end; p++)
  383. {
  384. p[-pa_size] = p[0];
  385. }
  386. out_ptr -= pa_size;
  387. #ifdef LIBRARY
  388. count_samples += framesPerBuffer;
  389. if(synth_callback)
  390. {
  391. // synchronous-playback mode, allow the calling process to abort the speech
  392. event_list[event_list_ix].type = espeakEVENT_LIST_TERMINATED; // indicates end of event list
  393. event_list[event_list_ix].user_data = 0;
  394. if(synth_callback(NULL,0,event_list) == 1)
  395. {
  396. SpeakNextClause(NULL,NULL,2); // stop speaking
  397. result = 1;
  398. }
  399. }
  400. #endif
  401. #ifdef ARCH_BIG
  402. {
  403. // swap the order of bytes in each sound sample in the portaudio buffer
  404. int c;
  405. unsigned char *buf_end;
  406. out_buf = (unsigned char *)outputBuffer;
  407. buf_end = out_buf + framesPerBuffer*2;
  408. while(out_buf < buf_end)
  409. {
  410. c = out_buf[0];
  411. out_buf[0] = out_buf[1];
  412. out_buf[1] = c;
  413. out_buf += 2;
  414. }
  415. }
  416. #endif
  417. if(out_channels == 2)
  418. {
  419. // sound output can only do stereo, not mono. Duplicate each sound sample to
  420. // produce 2 channels.
  421. out_buf = (unsigned char *)outputBuffer;
  422. for(ix=framesPerBuffer-1; ix>=0; ix--)
  423. {
  424. p = &out_buf[ix*4];
  425. p[3] = p[1] = out_buf[ix*2 + 1];
  426. p[2] = p[0] = out_buf[ix*2];
  427. }
  428. }
  429. #if USE_PORTAUDIO == 18
  430. #ifdef PLATFORM_WINDOWS
  431. return(result);
  432. #endif
  433. if(result != 0)
  434. {
  435. static int end_timer = 0;
  436. if(end_timer == 0)
  437. end_timer = 4;
  438. if(end_timer > 0)
  439. {
  440. end_timer--;
  441. if(end_timer == 0)
  442. return(1);
  443. }
  444. }
  445. return(0);
  446. #else
  447. return(result);
  448. #endif
  449. } // end of WaveCallBack
  450. #if USE_PORTAUDIO == 19
  451. /* This is a fixed version of Pa_OpenDefaultStream() for use if the version in portaudio V19
  452. is broken */
  453. static PaError Pa_OpenDefaultStream2( PaStream** stream,
  454. int inputChannelCount,
  455. int outputChannelCount,
  456. PaSampleFormat sampleFormat,
  457. double sampleRate,
  458. unsigned long framesPerBuffer,
  459. PaStreamCallback *streamCallback,
  460. void *userData )
  461. {
  462. PaError result;
  463. PaStreamParameters hostApiOutputParameters;
  464. if(option_device_number >= 0)
  465. hostApiOutputParameters.device = option_device_number;
  466. else
  467. hostApiOutputParameters.device = Pa_GetDefaultOutputDevice();
  468. if( hostApiOutputParameters.device == paNoDevice )
  469. return paDeviceUnavailable;
  470. hostApiOutputParameters.channelCount = outputChannelCount;
  471. hostApiOutputParameters.sampleFormat = sampleFormat;
  472. /* defaultHighOutputLatency is used below instead of
  473. defaultLowOutputLatency because it is more important for the default
  474. stream to work reliably than it is for it to work with the lowest
  475. latency.
  476. */
  477. hostApiOutputParameters.suggestedLatency =
  478. Pa_GetDeviceInfo( hostApiOutputParameters.device )->defaultHighOutputLatency;
  479. hostApiOutputParameters.hostApiSpecificStreamInfo = NULL;
  480. result = Pa_OpenStream(
  481. stream, NULL, &hostApiOutputParameters, sampleRate, framesPerBuffer, paNoFlag, streamCallback, userData );
  482. return(result);
  483. }
  484. #endif
  485. int WavegenOpenSound()
  486. {//===================
  487. PaError err, err2;
  488. PaError active;
  489. if(option_waveout || option_quiet)
  490. {
  491. // writing to WAV file, not to portaudio
  492. return(0);
  493. }
  494. #if USE_PORTAUDIO == 18
  495. active = Pa_StreamActive(pa_stream);
  496. #else
  497. active = Pa_IsStreamActive(pa_stream);
  498. #endif
  499. if(active == 1)
  500. return(0);
  501. if(active < 0)
  502. {
  503. out_channels = 1;
  504. #if USE_PORTAUDIO == 18
  505. err2 = Pa_OpenDefaultStream(&pa_stream,0,1,paInt16,samplerate,512,N_WAV_BUF,WaveCallback,(void *)userdata);
  506. if(err2 == paInvalidChannelCount)
  507. {
  508. // failed to open with mono, try stereo
  509. out_channels=2;
  510. err2 = Pa_OpenDefaultStream(&pa_stream,0,2,paInt16,samplerate,512,N_WAV_BUF,WaveCallback,(void *)userdata);
  511. }
  512. #else
  513. err2 = Pa_OpenDefaultStream2(&pa_stream,0,1,paInt16,(double)samplerate,512,WaveCallback,(void *)userdata);
  514. if(err2 == paInvalidChannelCount)
  515. {
  516. // failed to open with mono, try stereo
  517. out_channels=2;
  518. err2 = Pa_OpenDefaultStream(&pa_stream,0,2,paInt16,(double)samplerate,512,WaveCallback,(void *)userdata);
  519. }
  520. #endif
  521. }
  522. err = Pa_StartStream(pa_stream);
  523. #if USE_PORTAUDIO == 19
  524. if(err == paStreamIsNotStopped)
  525. {
  526. // not sure why we need this, but PA v19 seems to need it
  527. err = Pa_StopStream(pa_stream);
  528. err = Pa_StartStream(pa_stream);
  529. }
  530. #endif
  531. if(err != paNoError)
  532. {
  533. // exit speak if we can't open the sound device - this is OK if speak is being run for each utterance
  534. exit(2);
  535. }
  536. return(0);
  537. }
  538. int WavegenCloseSound()
  539. {//====================
  540. PaError active;
  541. // check whether speaking has finished, and close the stream
  542. if(pa_stream != NULL)
  543. {
  544. #if USE_PORTAUDIO == 18
  545. active = Pa_StreamActive(pa_stream);
  546. #else
  547. active = Pa_IsStreamActive(pa_stream);
  548. #endif
  549. if(WcmdqUsed() == 0) // also check that the queue is empty
  550. {
  551. if(active == 0)
  552. {
  553. Pa_CloseStream(pa_stream);
  554. pa_stream = NULL;
  555. return(1);
  556. }
  557. }
  558. else
  559. {
  560. WavegenOpenSound(); // still items in the queue, shouldn't be closed
  561. }
  562. }
  563. return(0);
  564. }
  565. int WavegenInitSound()
  566. {//===================
  567. PaError err;
  568. if(option_quiet)
  569. return(0);
  570. // PortAudio sound output library
  571. err = Pa_Initialize();
  572. pa_init_err = err;
  573. if(err != paNoError)
  574. {
  575. fprintf(stderr,"Failed to initialise the PortAudio sound\n");
  576. return(1);
  577. }
  578. return(0);
  579. }
  580. #else
  581. int WavegenOpenSound()
  582. {//===================
  583. return(0);
  584. }
  585. int WavegenCloseSound()
  586. {//====================
  587. return(0);
  588. }
  589. int WavegenInitSound()
  590. {//===================
  591. return(0);
  592. }
  593. #endif
  594. void WavegenInit(int rate, int wavemult_fact)
  595. {//==========================================
  596. int ix;
  597. double x;
  598. if(wavemult_fact == 0)
  599. wavemult_fact=60; // default
  600. wvoice = NULL;
  601. samplerate = samplerate_native = rate;
  602. PHASE_INC_FACTOR = 0x8000000 / samplerate; // assumes pitch is Hz*32
  603. Flutter_inc = (64 * samplerate)/rate;
  604. samplecount = 0;
  605. nsamples = 0;
  606. wavephase = 0x7fffffff;
  607. max_hval = 0;
  608. wdata.amplitude = 32;
  609. wdata.amplitude_fmt = 100;
  610. for(ix=0; ix<N_EMBEDDED_VALUES; ix++)
  611. embedded_value[ix] = embedded_default[ix];
  612. // set up window to generate a spread of harmonics from a
  613. // single peak for HF peaks
  614. wavemult_max = (samplerate * wavemult_fact)/(256 * 50);
  615. if(wavemult_max > N_WAVEMULT) wavemult_max = N_WAVEMULT;
  616. wavemult_offset = wavemult_max/2;
  617. if(samplerate != 22050)
  618. {
  619. // wavemult table has preset values for 22050 Hz, we only need to
  620. // recalculate them if we have a different sample rate
  621. for(ix=0; ix<wavemult_max; ix++)
  622. {
  623. x = 127*(1.0 - cos(PI2*ix/wavemult_max));
  624. wavemult[ix] = (int)x;
  625. }
  626. }
  627. pk_shape = pk_shape2; // pk_shape2
  628. #ifdef INCLUDE_KLATT
  629. KlattInit();
  630. #endif
  631. #ifdef LOG_FRAMES
  632. remove("log-espeakedit");
  633. remove("log-klatt");
  634. #endif
  635. } // end of WavegenInit
  636. int GetAmplitude(void)
  637. {//===================
  638. int amp;
  639. // normal, none, reduced, moderate, strong
  640. static const unsigned char amp_emphasis[5] = {16, 16, 10, 16, 22};
  641. amp = (embedded_value[EMBED_A])*55/100;
  642. general_amplitude = amp * amp_emphasis[embedded_value[EMBED_F]] / 16;
  643. return(general_amplitude);
  644. }
  645. static void WavegenSetEcho(void)
  646. {//=============================
  647. int delay;
  648. int amp;
  649. voicing = wvoice->voicing;
  650. delay = wvoice->echo_delay;
  651. amp = wvoice->echo_amp;
  652. if(delay >= N_ECHO_BUF)
  653. delay = N_ECHO_BUF-1;
  654. if(amp > 100)
  655. amp = 100;
  656. memset(echo_buf,0,sizeof(echo_buf));
  657. echo_tail = 0;
  658. if(embedded_value[EMBED_H] > 0)
  659. {
  660. // set echo from an embedded command in the text
  661. amp = embedded_value[EMBED_H];
  662. delay = 130;
  663. }
  664. if(delay == 0)
  665. amp = 0;
  666. echo_head = (delay * samplerate)/1000;
  667. echo_length = echo_head; // ensure completion of echo at the end of speech. Use 1 delay period?
  668. if(amp == 0)
  669. echo_length = 0;
  670. if(amp > 20)
  671. echo_length = echo_head * 2; // perhaps allow 2 echo periods if the echo is loud.
  672. // echo_amp units are 1/256ths of the amplitude of the original sound.
  673. echo_amp = amp;
  674. // compensate (partially) for increase in amplitude due to echo
  675. general_amplitude = GetAmplitude();
  676. general_amplitude = ((general_amplitude * (500-amp))/500);
  677. } // end of WavegenSetEcho
  678. int PeaksToHarmspect(wavegen_peaks_t *peaks, int pitch, int *htab, int control)
  679. {//============================================================================
  680. // Calculate the amplitude of each harmonics from the formants
  681. // Only for formants 0 to 5
  682. // control 0=initial call, 1=every 64 cycles
  683. // pitch and freqs are Hz<<16
  684. int f;
  685. wavegen_peaks_t *p;
  686. int fp; // centre freq of peak
  687. int fhi; // high freq of peak
  688. int h; // harmonic number
  689. int pk;
  690. int hmax;
  691. int hmax_samplerate; // highest harmonic allowed for the samplerate
  692. int x;
  693. int ix;
  694. int h1;
  695. #ifdef SPECT_EDITOR
  696. if(harm_sqrt_n > 0)
  697. return(HarmToHarmspect(pitch,htab));
  698. #endif
  699. // initialise as much of *out as we will need
  700. if(wvoice == NULL)
  701. return(1);
  702. hmax = (peaks[wvoice->n_harmonic_peaks].freq + peaks[wvoice->n_harmonic_peaks].right)/pitch;
  703. if(hmax >= MAX_HARMONIC)
  704. hmax = MAX_HARMONIC-1;
  705. // restrict highest harmonic to half the samplerate
  706. hmax_samplerate = (((samplerate * 19)/40) << 16)/pitch; // only 95% of Nyquist freq
  707. // hmax_samplerate = (samplerate << 16)/(pitch*2);
  708. if(hmax > hmax_samplerate)
  709. hmax = hmax_samplerate;
  710. for(h=0;h<=hmax;h++)
  711. htab[h]=0;
  712. h=0;
  713. for(pk=0; pk<=wvoice->n_harmonic_peaks; pk++)
  714. {
  715. p = &peaks[pk];
  716. if((p->height == 0) || (fp = p->freq)==0)
  717. continue;
  718. fhi = p->freq + p->right;
  719. h = ((p->freq - p->left) / pitch) + 1;
  720. if(h <= 0) h = 1;
  721. for(f=pitch*h; f < fp; f+=pitch)
  722. {
  723. htab[h++] += pk_shape[(fp-f)/(p->left>>8)] * p->height;
  724. }
  725. for(; f < fhi; f+=pitch)
  726. {
  727. htab[h++] += pk_shape[(f-fp)/(p->right>>8)] * p->height;
  728. }
  729. }
  730. {
  731. int y;
  732. int h2;
  733. // increase bass
  734. y = peaks[1].height * 10; // addition as a multiple of 1/256s
  735. h2 = (1000<<16)/pitch; // decrease until 1000Hz
  736. if(h2 > 0)
  737. {
  738. x = y/h2;
  739. h = 1;
  740. while(y > 0)
  741. {
  742. htab[h++] += y;
  743. y -= x;
  744. }
  745. }
  746. }
  747. // find the nearest harmonic for HF peaks where we don't use shape
  748. for(; pk<N_PEAKS; pk++)
  749. {
  750. x = peaks[pk].height >> 14;
  751. peak_height[pk] = (x * x * 5)/2;
  752. // find the nearest harmonic for HF peaks where we don't use shape
  753. if(control == 0)
  754. {
  755. // set this initially, but make changes only at the quiet point
  756. peak_harmonic[pk] = peaks[pk].freq / pitch;
  757. }
  758. // only use harmonics up to half the samplerate
  759. if(peak_harmonic[pk] >= hmax_samplerate)
  760. peak_height[pk] = 0;
  761. }
  762. // convert from the square-rooted values
  763. f = 0;
  764. for(h=0; h<=hmax; h++, f+=pitch)
  765. {
  766. x = htab[h] >> 15;
  767. htab[h] = (x * x) >> 8;
  768. if((ix = (f >> 19)) < N_TONE_ADJUST)
  769. {
  770. htab[h] = (htab[h] * wvoice->tone_adjust[ix]) >> 13; // index tone_adjust with Hz/8
  771. }
  772. }
  773. // adjust the amplitude of the first harmonic, affects tonal quality
  774. h1 = htab[1] * option_harmonic1;
  775. htab[1] = h1/8;
  776. // calc intermediate increments of LF harmonics
  777. if(control & 1)
  778. {
  779. for(h=1; h<N_LOWHARM; h++)
  780. {
  781. harm_inc[h] = (htab[h] - harmspect[h]) >> 3;
  782. }
  783. }
  784. return(hmax); // highest harmonic number
  785. } // end of PeaksToHarmspect
  786. static void AdvanceParameters()
  787. {//============================
  788. // Called every 64 samples to increment the formant freq, height, and widths
  789. int x;
  790. int ix;
  791. static int Flutter_ix = 0;
  792. // advance the pitch
  793. wdata.pitch_ix += wdata.pitch_inc;
  794. if((ix = wdata.pitch_ix>>8) > 127) ix = 127;
  795. x = wdata.pitch_env[ix] * wdata.pitch_range;
  796. wdata.pitch = (x>>8) + wdata.pitch_base;
  797. amp_ix += amp_inc;
  798. /* add pitch flutter */
  799. if(Flutter_ix >= (N_FLUTTER*64))
  800. Flutter_ix = 0;
  801. x = ((int)(Flutter_tab[Flutter_ix >> 6])-0x80) * flutter_amp;
  802. Flutter_ix += Flutter_inc;
  803. wdata.pitch += x;
  804. if(wdata.pitch < 102400)
  805. wdata.pitch = 102400; // min pitch, 25 Hz (25 << 12)
  806. if(samplecount == samplecount_start)
  807. return;
  808. for(ix=0; ix <= wvoice->n_harmonic_peaks; ix++)
  809. {
  810. peaks[ix].freq1 += peaks[ix].freq_inc;
  811. peaks[ix].freq = (int)peaks[ix].freq1;
  812. peaks[ix].height1 += peaks[ix].height_inc;
  813. if((peaks[ix].height = (int)peaks[ix].height1) < 0)
  814. peaks[ix].height = 0;
  815. peaks[ix].left1 += peaks[ix].left_inc;
  816. peaks[ix].left = (int)peaks[ix].left1;
  817. if(ix < 3)
  818. {
  819. peaks[ix].right1 += peaks[ix].right_inc;
  820. peaks[ix].right = (int)peaks[ix].right1;
  821. }
  822. else
  823. {
  824. peaks[ix].right = peaks[ix].left;
  825. }
  826. }
  827. for(;ix < 8; ix++)
  828. {
  829. // formants 6,7,8 don't have a width parameter
  830. if(ix < 7)
  831. {
  832. peaks[ix].freq1 += peaks[ix].freq_inc;
  833. peaks[ix].freq = (int)peaks[ix].freq1;
  834. }
  835. peaks[ix].height1 += peaks[ix].height_inc;
  836. if((peaks[ix].height = (int)peaks[ix].height1) < 0)
  837. peaks[ix].height = 0;
  838. }
  839. #ifdef SPECT_EDITOR
  840. if(harm_sqrt_n != 0)
  841. {
  842. // We are generating from a harmonic spectrum at a given pitch, not from formant peaks
  843. for(ix=0; ix<harm_sqrt_n; ix++)
  844. harm_sqrt[ix] += harm_sqrt_inc[ix];
  845. }
  846. #endif
  847. } // end of AdvanceParameters
  848. #ifndef PLATFORM_RISCOS
  849. static double resonator(RESONATOR *r, double input)
  850. {//================================================
  851. double x;
  852. x = r->a * input + r->b * r->x1 + r->c * r->x2;
  853. r->x2 = r->x1;
  854. r->x1 = x;
  855. return x;
  856. }
  857. static void setresonator(RESONATOR *rp, int freq, int bwidth, int init)
  858. {//====================================================================
  859. // freq Frequency of resonator in Hz
  860. // bwidth Bandwidth of resonator in Hz
  861. // init Initialize internal data
  862. double x;
  863. double arg;
  864. if(init)
  865. {
  866. rp->x1 = 0;
  867. rp->x2 = 0;
  868. }
  869. // x = exp(-pi * bwidth * t)
  870. arg = minus_pi_t * bwidth;
  871. x = exp(arg);
  872. // c = -(x*x)
  873. rp->c = -(x * x);
  874. // b = x * 2*cos(2 pi * freq * t)
  875. arg = two_pi_t * freq;
  876. rp->b = x * cos(arg) * 2.0;
  877. // a = 1.0 - b - c
  878. rp->a = 1.0 - rp->b - rp->c;
  879. } // end if setresonator
  880. #endif
  881. void InitBreath(void)
  882. {//==================
  883. #ifndef PLATFORM_RISCOS
  884. int ix;
  885. minus_pi_t = -PI / samplerate;
  886. two_pi_t = -2.0 * minus_pi_t;
  887. for(ix=0; ix<N_PEAKS; ix++)
  888. {
  889. setresonator(&rbreath[ix],2000,200,1);
  890. }
  891. #endif
  892. } // end of InitBreath
  893. static void SetBreath()
  894. {//====================
  895. #ifndef PLATFORM_RISCOS
  896. int pk;
  897. if(wvoice->breath[0] == 0)
  898. return;
  899. for(pk=1; pk<N_PEAKS; pk++)
  900. {
  901. if(wvoice->breath[pk] != 0)
  902. {
  903. // breath[0] indicates that some breath formants are needed
  904. // set the freq from the current ynthesis formant and the width from the voice data
  905. setresonator(&rbreath[pk], peaks[pk].freq >> 16, wvoice->breathw[pk],0);
  906. }
  907. }
  908. #endif
  909. } // end of SetBreath
  910. static int ApplyBreath(void)
  911. {//=========================
  912. int value = 0;
  913. #ifndef PLATFORM_RISCOS
  914. int noise;
  915. int ix;
  916. int amp;
  917. // use two random numbers, for alternate formants
  918. noise = (rand() & 0x3fff) - 0x2000;
  919. for(ix=1; ix < N_PEAKS; ix++)
  920. {
  921. if((amp = wvoice->breath[ix]) != 0)
  922. {
  923. amp *= (peaks[ix].height >> 14);
  924. value += (int)resonator(&rbreath[ix],noise) * amp;
  925. }
  926. }
  927. #endif
  928. return (value);
  929. }
  930. int Wavegen()
  931. {//==========
  932. unsigned short waveph;
  933. unsigned short theta;
  934. int total;
  935. int h;
  936. int ix;
  937. int z, z1, z2;
  938. int echo;
  939. int ov;
  940. static int maxh, maxh2;
  941. int pk;
  942. signed char c;
  943. int sample;
  944. int amp;
  945. int modn_amp, modn_period;
  946. static int agc = 256;
  947. static int h_switch_sign = 0;
  948. static int cycle_count = 0;
  949. static int amplitude2 = 0; // adjusted for pitch
  950. // continue until the output buffer is full, or
  951. // the required number of samples have been produced
  952. for(;;)
  953. {
  954. if((end_wave==0) && (samplecount==nsamples))
  955. return(0);
  956. if((samplecount & 0x3f) == 0)
  957. {
  958. // every 64 samples, adjust the parameters
  959. if(samplecount == 0)
  960. {
  961. hswitch = 0;
  962. harmspect = hspect[0];
  963. maxh2 = PeaksToHarmspect(peaks, wdata.pitch<<4, hspect[0], 0);
  964. // adjust amplitude to compensate for fewer harmonics at higher pitch
  965. // amplitude2 = (wdata.amplitude * wdata.pitch)/(100 << 11);
  966. amplitude2 = (wdata.amplitude * (wdata.pitch >> 8) * wdata.amplitude_fmt)/(10000 << 3);
  967. // switch sign of harmonics above about 900Hz, to reduce max peak amplitude
  968. h_switch_sign = 890 / (wdata.pitch >> 12);
  969. }
  970. else
  971. AdvanceParameters();
  972. // pitch is Hz<<12
  973. phaseinc = (wdata.pitch>>7) * PHASE_INC_FACTOR;
  974. cycle_samples = samplerate/(wdata.pitch >> 12); // sr/(pitch*2)
  975. hf_factor = wdata.pitch >> 11;
  976. maxh = maxh2;
  977. harmspect = hspect[hswitch];
  978. hswitch ^= 1;
  979. maxh2 = PeaksToHarmspect(peaks, wdata.pitch<<4, hspect[hswitch], 1);
  980. SetBreath();
  981. }
  982. else
  983. if((samplecount & 0x07) == 0)
  984. {
  985. for(h=1; h<N_LOWHARM && h<=maxh2 && h<=maxh; h++)
  986. {
  987. harmspect[h] += harm_inc[h];
  988. }
  989. // bring automctic gain control back towards unity
  990. if(agc < 256) agc++;
  991. }
  992. samplecount++;
  993. if(wavephase > 0)
  994. {
  995. wavephase += phaseinc;
  996. if(wavephase < 0)
  997. {
  998. // sign has changed, reached a quiet point in the waveform
  999. cbytes = wavemult_offset - (cycle_samples)/2;
  1000. if(samplecount > nsamples)
  1001. return(0);
  1002. cycle_count++;
  1003. for(pk=wvoice->n_harmonic_peaks+1; pk<N_PEAKS; pk++)
  1004. {
  1005. // find the nearest harmonic for HF peaks where we don't use shape
  1006. peak_harmonic[pk] = ((peaks[pk].freq / (wdata.pitch*8)) + 1) / 2;
  1007. }
  1008. // adjust amplitude to compensate for fewer harmonics at higher pitch
  1009. // amplitude2 = (wdata.amplitude * wdata.pitch)/(100 << 11);
  1010. amplitude2 = (wdata.amplitude * (wdata.pitch >> 8) * wdata.amplitude_fmt)/(10000 << 3);
  1011. if(glottal_flag > 0)
  1012. {
  1013. if(glottal_flag == 3)
  1014. {
  1015. if((nsamples-samplecount) < (cycle_samples*2))
  1016. {
  1017. // Vowel before glottal-stop.
  1018. // This is the start of the penultimate cycle, reduce its amplitude
  1019. glottal_flag = 2;
  1020. amplitude2 = (amplitude2 * glottal_reduce)/256;
  1021. }
  1022. }
  1023. else
  1024. if(glottal_flag == 4)
  1025. {
  1026. // Vowel following a glottal-stop.
  1027. // This is the start of the second cycle, reduce its amplitude
  1028. glottal_flag = 2;
  1029. amplitude2 = (amplitude2 * glottal_reduce)/256;
  1030. }
  1031. else
  1032. {
  1033. glottal_flag--;
  1034. }
  1035. }
  1036. if(amplitude_env != NULL)
  1037. {
  1038. // amplitude envelope is only used for creaky voice effect on certain vowels/tones
  1039. if((ix = amp_ix>>8) > 127) ix = 127;
  1040. amp = amplitude_env[ix];
  1041. amplitude2 = (amplitude2 * amp)/128;
  1042. // if(amp < 255)
  1043. // modulation_type = 7;
  1044. }
  1045. // introduce roughness into the sound by reducing the amplitude of
  1046. modn_period = 0;
  1047. if(voice->roughness < N_ROUGHNESS)
  1048. {
  1049. modn_period = modulation_tab[voice->roughness][modulation_type];
  1050. modn_amp = modn_period & 0xf;
  1051. modn_period = modn_period >> 4;
  1052. }
  1053. if(modn_period != 0)
  1054. {
  1055. if(modn_period==0xf)
  1056. {
  1057. // just once */
  1058. amplitude2 = (amplitude2 * modn_amp)/16;
  1059. modulation_type = 0;
  1060. }
  1061. else
  1062. {
  1063. // reduce amplitude every [modn_period} cycles
  1064. if((cycle_count % modn_period)==0)
  1065. amplitude2 = (amplitude2 * modn_amp)/16;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. else
  1071. {
  1072. wavephase += phaseinc;
  1073. }
  1074. waveph = (unsigned short)(wavephase >> 16);
  1075. total = 0;
  1076. // apply HF peaks, formants 6,7,8
  1077. // add a single harmonic and then spread this my multiplying by a
  1078. // window. This is to reduce the processing power needed to add the
  1079. // higher frequence harmonics.
  1080. cbytes++;
  1081. if(cbytes >=0 && cbytes<wavemult_max)
  1082. {
  1083. for(pk=wvoice->n_harmonic_peaks+1; pk<N_PEAKS; pk++)
  1084. {
  1085. theta = peak_harmonic[pk] * waveph;
  1086. total += (long)sin_tab[theta >> 5] * peak_height[pk];
  1087. }
  1088. // spread the peaks by multiplying by a window
  1089. total = (long)(total / hf_factor) * wavemult[cbytes];
  1090. }
  1091. // apply main peaks, formants 0 to 5
  1092. #ifdef USE_ASSEMBLER_1
  1093. // use an optimised routine for this loop, if available
  1094. total += AddSineWaves(waveph, h_switch_sign, maxh, harmspect); // call an assembler code routine
  1095. #else
  1096. theta = waveph;
  1097. for(h=1; h<=h_switch_sign; h++)
  1098. {
  1099. total += ((int)sin_tab[theta >> 5] * harmspect[h]);
  1100. theta += waveph;
  1101. }
  1102. while(h<=maxh)
  1103. {
  1104. total -= ((int)sin_tab[theta >> 5] * harmspect[h]);
  1105. theta += waveph;
  1106. h++;
  1107. }
  1108. #endif
  1109. if(voicing != 64)
  1110. {
  1111. total = (total >> 6) * voicing;
  1112. }
  1113. #ifndef PLATFORM_RISCOS
  1114. if(wvoice->breath[0])
  1115. {
  1116. total += ApplyBreath();
  1117. }
  1118. #endif
  1119. // mix with sampled wave if required
  1120. z2 = 0;
  1121. if(wdata.mix_wavefile_ix < wdata.n_mix_wavefile)
  1122. {
  1123. if(wdata.mix_wave_scale == 0)
  1124. {
  1125. // a 16 bit sample
  1126. c = wdata.mix_wavefile[wdata.mix_wavefile_ix+wdata.mix_wavefile_offset+1];
  1127. sample = wdata.mix_wavefile[wdata.mix_wavefile_ix+wdata.mix_wavefile_offset] + (c * 256);
  1128. wdata.mix_wavefile_ix += 2;
  1129. }
  1130. else
  1131. {
  1132. // a 8 bit sample, scaled
  1133. sample = (signed char)wdata.mix_wavefile[wdata.mix_wavefile_offset+wdata.mix_wavefile_ix++] * wdata.mix_wave_scale;
  1134. }
  1135. z2 = (sample * wdata.amplitude_v) >> 10;
  1136. z2 = (z2 * wdata.mix_wave_amp)/32;
  1137. if((wdata.mix_wavefile_ix + wdata.mix_wavefile_offset) >= wdata.mix_wavefile_max) // reached the end of available WAV data
  1138. wdata.mix_wavefile_offset -= (wdata.mix_wavefile_max*3)/4;
  1139. }
  1140. z1 = z2 + (((total>>8) * amplitude2) >> 13);
  1141. echo = (echo_buf[echo_tail++] * echo_amp);
  1142. z1 += echo >> 8;
  1143. if(echo_tail >= N_ECHO_BUF)
  1144. echo_tail=0;
  1145. z = (z1 * agc) >> 8;
  1146. // check for overflow, 16bit signed samples
  1147. if(z >= 32768)
  1148. {
  1149. ov = 8388608/z1 - 1; // 8388608 is 2^23, i.e. max value * 256
  1150. if(ov < agc) agc = ov; // set agc to number of 1/256ths to multiply the sample by
  1151. z = (z1 * agc) >> 8; // reduce sample by agc value to prevent overflow
  1152. }
  1153. else
  1154. if(z <= -32768)
  1155. {
  1156. ov = -8388608/z1 - 1;
  1157. if(ov < agc) agc = ov;
  1158. z = (z1 * agc) >> 8;
  1159. }
  1160. *out_ptr++ = z;
  1161. *out_ptr++ = z >> 8;
  1162. echo_buf[echo_head++] = z;
  1163. if(echo_head >= N_ECHO_BUF)
  1164. echo_head = 0;
  1165. if(out_ptr >= out_end)
  1166. return(1);
  1167. }
  1168. return(0);
  1169. } // end of Wavegen
  1170. static int PlaySilence(int length, int resume)
  1171. {//===========================================
  1172. static int n_samples;
  1173. int value=0;
  1174. nsamples = 0;
  1175. samplecount = 0;
  1176. wavephase = 0x7fffffff;
  1177. if(length == 0)
  1178. return(0);
  1179. if(resume==0)
  1180. n_samples = length;
  1181. while(n_samples-- > 0)
  1182. {
  1183. value = (echo_buf[echo_tail++] * echo_amp) >> 8;
  1184. if(echo_tail >= N_ECHO_BUF)
  1185. echo_tail = 0;
  1186. *out_ptr++ = value;
  1187. *out_ptr++ = value >> 8;
  1188. echo_buf[echo_head++] = value;
  1189. if(echo_head >= N_ECHO_BUF)
  1190. echo_head = 0;
  1191. if(out_ptr >= out_end)
  1192. return(1);
  1193. }
  1194. return(0);
  1195. } // end of PlaySilence
  1196. static int PlayWave(int length, int resume, unsigned char *data, int scale, int amp)
  1197. {//=================================================================================
  1198. static int n_samples;
  1199. static int ix=0;
  1200. int value;
  1201. signed char c;
  1202. if(resume==0)
  1203. {
  1204. n_samples = length;
  1205. ix = 0;
  1206. }
  1207. nsamples = 0;
  1208. samplecount = 0;
  1209. while(n_samples-- > 0)
  1210. {
  1211. if(scale == 0)
  1212. {
  1213. // 16 bits data
  1214. c = data[ix+1];
  1215. value = data[ix] + (c * 256);
  1216. ix+=2;
  1217. }
  1218. else
  1219. {
  1220. // 8 bit data, shift by the specified scale factor
  1221. value = (signed char)data[ix++] * scale;
  1222. }
  1223. value *= (consonant_amp * general_amplitude); // reduce strength of consonant
  1224. value = value >> 10;
  1225. value = (value * amp)/32;
  1226. value += ((echo_buf[echo_tail++] * echo_amp) >> 8);
  1227. if(value > 32767)
  1228. value = 32768;
  1229. else
  1230. if(value < -32768)
  1231. value = -32768;
  1232. if(echo_tail >= N_ECHO_BUF)
  1233. echo_tail = 0;
  1234. out_ptr[0] = value;
  1235. out_ptr[1] = value >> 8;
  1236. out_ptr+=2;
  1237. echo_buf[echo_head++] = (value*3)/4;
  1238. if(echo_head >= N_ECHO_BUF)
  1239. echo_head = 0;
  1240. if(out_ptr >= out_end)
  1241. return(1);
  1242. }
  1243. return(0);
  1244. }
  1245. static int SetWithRange0(int value, int max)
  1246. {//=========================================
  1247. if(value < 0)
  1248. return(0);
  1249. if(value > max)
  1250. return(max);
  1251. return(value);
  1252. }
  1253. static void SetPitchFormants()
  1254. {//===========================
  1255. int ix;
  1256. int factor = 256;
  1257. int pitch_value;
  1258. // adjust formants to give better results for a different voice pitch
  1259. if((pitch_value = embedded_value[EMBED_P]) > MAX_PITCH_VALUE)
  1260. pitch_value = MAX_PITCH_VALUE;
  1261. if(pitch_value > 50)
  1262. {
  1263. // only adjust if the pitch is higher than normal
  1264. factor = 256 + (25 * (pitch_value - 50))/50;
  1265. }
  1266. for(ix=0; ix<=5; ix++)
  1267. {
  1268. wvoice->freq[ix] = (wvoice->freq2[ix] * factor)/256;
  1269. }
  1270. factor = embedded_value[EMBED_T]*3;
  1271. wvoice->height[0] = (wvoice->height2[0] * (256 - factor*2))/256;
  1272. wvoice->height[1] = (wvoice->height2[1] * (256 - factor))/256;
  1273. }
  1274. void SetEmbedded(int control, int value)
  1275. {//=====================================
  1276. // there was an embedded command in the text at this point
  1277. int sign=0;
  1278. int command;
  1279. command = control & 0x1f;
  1280. if((control & 0x60) == 0x60)
  1281. sign = -1;
  1282. else
  1283. if((control & 0x60) == 0x40)
  1284. sign = 1;
  1285. if(command < N_EMBEDDED_VALUES)
  1286. {
  1287. if(sign == 0)
  1288. embedded_value[command] = value;
  1289. else
  1290. embedded_value[command] += (value * sign);
  1291. embedded_value[command] = SetWithRange0(embedded_value[command],embedded_max[command]);
  1292. }
  1293. switch(command)
  1294. {
  1295. case EMBED_T:
  1296. WavegenSetEcho(); // and drop through to case P
  1297. case EMBED_P:
  1298. SetPitchFormants();
  1299. break;
  1300. case EMBED_A: // amplitude
  1301. general_amplitude = GetAmplitude();
  1302. break;
  1303. case EMBED_F: // emphasis
  1304. general_amplitude = GetAmplitude();
  1305. break;
  1306. case EMBED_H:
  1307. WavegenSetEcho();
  1308. break;
  1309. }
  1310. }
  1311. void WavegenSetVoice(voice_t *v)
  1312. {//=============================
  1313. static voice_t v2;
  1314. memcpy(&v2,v,sizeof(v2));
  1315. wvoice = &v2;
  1316. if(v->peak_shape==0)
  1317. pk_shape = pk_shape1;
  1318. else
  1319. pk_shape = pk_shape2;
  1320. consonant_amp = (v->consonant_amp * 26) /100;
  1321. if(samplerate <= 11000)
  1322. {
  1323. consonant_amp = consonant_amp*2; // emphasize consonants at low sample rates
  1324. option_harmonic1 = 6;
  1325. }
  1326. WavegenSetEcho();
  1327. SetPitchFormants();
  1328. MarkerEvent(espeakEVENT_SAMPLERATE, 0, wvoice->samplerate, 0, out_ptr);
  1329. // WVoiceChanged(wvoice);
  1330. }
  1331. static void SetAmplitude(int length, unsigned char *amp_env, int value)
  1332. {//====================================================================
  1333. amp_ix = 0;
  1334. if(length==0)
  1335. amp_inc = 0;
  1336. else
  1337. amp_inc = (256 * ENV_LEN * STEPSIZE)/length;
  1338. wdata.amplitude = (value * general_amplitude)/16;
  1339. wdata.amplitude_v = (wdata.amplitude * wvoice->consonant_ampv * 15)/100; // for wave mixed with voiced sounds
  1340. amplitude_env = amp_env;
  1341. }
  1342. void SetPitch2(voice_t *voice, int pitch1, int pitch2, int *pitch_base, int *pitch_range)
  1343. {//======================================================================================
  1344. int x;
  1345. int base;
  1346. int range;
  1347. int pitch_value;
  1348. if(pitch1 > pitch2)
  1349. {
  1350. x = pitch1; // swap values
  1351. pitch1 = pitch2;
  1352. pitch2 = x;
  1353. }
  1354. if((pitch_value = embedded_value[EMBED_P]) > MAX_PITCH_VALUE)
  1355. pitch_value = MAX_PITCH_VALUE;
  1356. pitch_value -= embedded_value[EMBED_T]; // adjust tone for announcing punctuation
  1357. if(pitch_value < 0)
  1358. pitch_value = 0;
  1359. base = (voice->pitch_base * pitch_adjust_tab[pitch_value])/128;
  1360. range = (voice->pitch_range * embedded_value[EMBED_R])/50;
  1361. // compensate for change in pitch when the range is narrowed or widened
  1362. base -= (range - voice->pitch_range)*18;
  1363. *pitch_base = base + (pitch1 * range)/2;
  1364. *pitch_range = base + (pitch2 * range)/2 - *pitch_base;
  1365. }
  1366. void SetPitch(int length, unsigned char *env, int pitch1, int pitch2)
  1367. {//==================================================================
  1368. // length in samples
  1369. #ifdef LOG_FRAMES
  1370. if(option_log_frames)
  1371. {
  1372. f_log=fopen("log-espeakedit","a");
  1373. if(f_log != NULL)
  1374. {
  1375. fprintf(f_log," pitch %3d %3d %3dmS\n",pitch1,pitch2,(length*1000)/samplerate);
  1376. fclose(f_log);
  1377. f_log=NULL;
  1378. }
  1379. }
  1380. #endif
  1381. if((wdata.pitch_env = env)==NULL)
  1382. wdata.pitch_env = env_fall; // default
  1383. wdata.pitch_ix = 0;
  1384. if(length==0)
  1385. wdata.pitch_inc = 0;
  1386. else
  1387. wdata.pitch_inc = (256 * ENV_LEN * STEPSIZE)/length;
  1388. SetPitch2(wvoice, pitch1, pitch2, &wdata.pitch_base, &wdata.pitch_range);
  1389. // set initial pitch
  1390. wdata.pitch = ((wdata.pitch_env[0] * wdata.pitch_range) >>8) + wdata.pitch_base; // Hz << 12
  1391. flutter_amp = wvoice->flutter;
  1392. } // end of SetPitch
  1393. void SetSynth(int length, int modn, frame_t *fr1, frame_t *fr2, voice_t *v)
  1394. {//========================================================================
  1395. int ix;
  1396. DOUBLEX next;
  1397. int length2;
  1398. int length4;
  1399. int qix;
  1400. int cmd;
  1401. static int glottal_reduce_tab1[4] = {0x30, 0x30, 0x40, 0x50}; // vowel before [?], amp * 1/256
  1402. // static int glottal_reduce_tab1[4] = {0x30, 0x40, 0x50, 0x60}; // vowel before [?], amp * 1/256
  1403. static int glottal_reduce_tab2[4] = {0x90, 0xa0, 0xb0, 0xc0}; // vowel after [?], amp * 1/256
  1404. #ifdef LOG_FRAMES
  1405. if(option_log_frames)
  1406. {
  1407. f_log=fopen("log-espeakedit","a");
  1408. if(f_log != NULL)
  1409. {
  1410. fprintf(f_log,"%3dmS %3d %3d %4d %4d (%3d %3d %3d %3d) to %3d %3d %4d %4d (%3d %3d %3d %3d)\n",length*1000/samplerate,
  1411. fr1->ffreq[0],fr1->ffreq[1],fr1->ffreq[2],fr1->ffreq[3], fr1->fheight[0],fr1->fheight[1],fr1->fheight[2],fr1->fheight[3],
  1412. fr2->ffreq[0],fr2->ffreq[1],fr2->ffreq[2],fr2->ffreq[3], fr2->fheight[0],fr2->fheight[1],fr2->fheight[2],fr2->fheight[3] );
  1413. fclose(f_log);
  1414. f_log=NULL;
  1415. }
  1416. }
  1417. #endif
  1418. harm_sqrt_n = 0;
  1419. end_wave = 1;
  1420. // any additional information in the param1 ?
  1421. modulation_type = modn & 0xff;
  1422. glottal_flag = 0;
  1423. if(modn & 0x400)
  1424. {
  1425. glottal_flag = 3; // before a glottal stop
  1426. glottal_reduce = glottal_reduce_tab1[(modn >> 8) & 3];
  1427. }
  1428. if(modn & 0x800)
  1429. {
  1430. glottal_flag = 4; // after a glottal stop
  1431. glottal_reduce = glottal_reduce_tab2[(modn >> 8) & 3];
  1432. }
  1433. for(qix=wcmdq_head+1;;qix++)
  1434. {
  1435. if(qix >= N_WCMDQ) qix = 0;
  1436. if(qix == wcmdq_tail) break;
  1437. cmd = wcmdq[qix][0];
  1438. if(cmd==WCMD_SPECT)
  1439. {
  1440. end_wave = 0; // next wave generation is from another spectrum
  1441. break;
  1442. }
  1443. if((cmd==WCMD_WAVE) || (cmd==WCMD_PAUSE))
  1444. break; // next is not from spectrum, so continue until end of wave cycle
  1445. }
  1446. // round the length to a multiple of the stepsize
  1447. length2 = (length + STEPSIZE/2) & ~0x3f;
  1448. if(length2 == 0)
  1449. length2 = STEPSIZE;
  1450. // add this length to any left over from the previous synth
  1451. samplecount_start = samplecount;
  1452. nsamples += length2;
  1453. length4 = length2/4;
  1454. peaks[7].freq = (7800 * v->freq[7] + v->freqadd[7]*256) << 8;
  1455. peaks[8].freq = (9000 * v->freq[8] + v->freqadd[8]*256) << 8;
  1456. for(ix=0; ix < 8; ix++)
  1457. {
  1458. if(ix < 7)
  1459. {
  1460. peaks[ix].freq1 = (fr1->ffreq[ix] * v->freq[ix] + v->freqadd[ix]*256) << 8;
  1461. peaks[ix].freq = (int)peaks[ix].freq1;
  1462. next = (fr2->ffreq[ix] * v->freq[ix] + v->freqadd[ix]*256) << 8;
  1463. peaks[ix].freq_inc = ((next - peaks[ix].freq1) * (STEPSIZE/4)) / length4; // lower headroom for fixed point math
  1464. }
  1465. peaks[ix].height1 = (fr1->fheight[ix] * v->height[ix]) << 6;
  1466. peaks[ix].height = (int)peaks[ix].height1;
  1467. next = (fr2->fheight[ix] * v->height[ix]) << 6;
  1468. peaks[ix].height_inc = ((next - peaks[ix].height1) * STEPSIZE) / length2;
  1469. if((ix <= 5) && (ix <= wvoice->n_harmonic_peaks))
  1470. {
  1471. peaks[ix].left1 = (fr1->fwidth[ix] * v->width[ix]) << 10;
  1472. peaks[ix].left = (int)peaks[ix].left1;
  1473. next = (fr2->fwidth[ix] * v->width[ix]) << 10;
  1474. peaks[ix].left_inc = ((next - peaks[ix].left1) * STEPSIZE) / length2;
  1475. if(ix < 3)
  1476. {
  1477. peaks[ix].right1 = (fr1->fright[ix] * v->width[ix]) << 10;
  1478. peaks[ix].right = (int)peaks[ix].right1;
  1479. next = (fr2->fright[ix] * v->width[ix]) << 10;
  1480. peaks[ix].right_inc = ((next - peaks[ix].right1) * STEPSIZE) / length2;
  1481. }
  1482. else
  1483. {
  1484. peaks[ix].right = peaks[ix].left;
  1485. }
  1486. }
  1487. }
  1488. } // end of SetSynth
  1489. static int Wavegen2(int length, int modulation, int resume, frame_t *fr1, frame_t *fr2)
  1490. {//====================================================================================
  1491. if(resume==0)
  1492. SetSynth(length, modulation, fr1, fr2, wvoice);
  1493. return(Wavegen());
  1494. }
  1495. void Write4Bytes(FILE *f, int value)
  1496. {//=================================
  1497. // Write 4 bytes to a file, least significant first
  1498. int ix;
  1499. for(ix=0; ix<4; ix++)
  1500. {
  1501. fputc(value & 0xff,f);
  1502. value = value >> 8;
  1503. }
  1504. }
  1505. int WavegenFill2(int fill_zeros)
  1506. {//============================
  1507. // Pick up next wavegen commands from the queue
  1508. // return: 0 output buffer has been filled
  1509. // return: 1 input command queue is now empty
  1510. long64 *q;
  1511. int length;
  1512. int result;
  1513. int marker_type;
  1514. static int resume=0;
  1515. static int echo_complete=0;
  1516. while(out_ptr < out_end)
  1517. {
  1518. if(WcmdqUsed() <= 0)
  1519. {
  1520. if(echo_complete > 0)
  1521. {
  1522. // continue to play silence until echo is completed
  1523. resume = PlaySilence(echo_complete,resume);
  1524. if(resume == 1)
  1525. return(0); // not yet finished
  1526. }
  1527. if(fill_zeros)
  1528. {
  1529. while(out_ptr < out_end)
  1530. *out_ptr++ = 0;
  1531. }
  1532. return(1); // queue empty, close sound channel
  1533. }
  1534. result = 0;
  1535. q = wcmdq[wcmdq_head];
  1536. length = q[1];
  1537. switch(q[0] & 0xff)
  1538. {
  1539. case WCMD_PITCH:
  1540. SetPitch(length,(unsigned char *)q[2],q[3] >> 16,q[3] & 0xffff);
  1541. break;
  1542. case WCMD_PAUSE:
  1543. if(resume==0)
  1544. {
  1545. echo_complete -= length;
  1546. }
  1547. wdata.n_mix_wavefile = 0;
  1548. wdata.amplitude_fmt = 100;
  1549. #ifdef INCLUDE_KLATT
  1550. KlattReset(1);
  1551. #endif
  1552. result = PlaySilence(length,resume);
  1553. break;
  1554. case WCMD_WAVE:
  1555. echo_complete = echo_length;
  1556. wdata.n_mix_wavefile = 0;
  1557. #ifdef INCLUDE_KLATT
  1558. KlattReset(1);
  1559. #endif
  1560. result = PlayWave(length,resume,(unsigned char*)q[2], q[3] & 0xff, q[3] >> 8);
  1561. break;
  1562. case WCMD_WAVE2:
  1563. // wave file to be played at the same time as synthesis
  1564. wdata.mix_wave_amp = q[3] >> 8;
  1565. wdata.mix_wave_scale = q[3] & 0xff;
  1566. wdata.n_mix_wavefile = (length & 0xffff);
  1567. wdata.mix_wavefile_max = (length >> 16) & 0xffff;
  1568. if(wdata.mix_wave_scale == 0)
  1569. {
  1570. wdata.n_mix_wavefile *= 2;
  1571. wdata.mix_wavefile_max *= 2;
  1572. }
  1573. wdata.mix_wavefile_ix = 0;
  1574. wdata.mix_wavefile_offset = 0;
  1575. wdata.mix_wavefile = (unsigned char *)q[2];
  1576. break;
  1577. case WCMD_SPECT2: // as WCMD_SPECT but stop any concurrent wave file
  1578. wdata.n_mix_wavefile = 0; // ... and drop through to WCMD_SPECT case
  1579. case WCMD_SPECT:
  1580. echo_complete = echo_length;
  1581. result = Wavegen2(length & 0xffff,q[1] >> 16,resume,(frame_t *)q[2],(frame_t *)q[3]);
  1582. break;
  1583. #ifdef INCLUDE_KLATT
  1584. case WCMD_KLATT2: // as WCMD_SPECT but stop any concurrent wave file
  1585. wdata.n_mix_wavefile = 0; // ... and drop through to WCMD_SPECT case
  1586. case WCMD_KLATT:
  1587. echo_complete = echo_length;
  1588. result = Wavegen_Klatt2(length & 0xffff,q[1] >> 16,resume,(frame_t *)q[2],(frame_t *)q[3]);
  1589. break;
  1590. #endif
  1591. case WCMD_MARKER:
  1592. marker_type = q[0] >> 8;
  1593. MarkerEvent(marker_type, q[1],q[2],q[3],out_ptr);
  1594. #ifdef LOG_FRAMES
  1595. LogMarker(marker_type, q[2], q[3]);
  1596. #endif
  1597. if(marker_type == 1) // word marker
  1598. {
  1599. current_source_index = q[1] & 0xffffff;
  1600. }
  1601. break;
  1602. case WCMD_AMPLITUDE:
  1603. SetAmplitude(length,(unsigned char *)q[2],q[3]);
  1604. break;
  1605. case WCMD_VOICE:
  1606. WavegenSetVoice((voice_t *)q[2]);
  1607. free((voice_t *)q[2]);
  1608. break;
  1609. case WCMD_EMBEDDED:
  1610. SetEmbedded(q[1],q[2]);
  1611. break;
  1612. case WCMD_MBROLA_DATA:
  1613. result = MbrolaFill(length, resume, (general_amplitude * wvoice->voicing)/64);
  1614. break;
  1615. case WCMD_FMT_AMPLITUDE:
  1616. if((wdata.amplitude_fmt = q[1]) == 0)
  1617. wdata.amplitude_fmt = 100; // percentage, but value=0 means 100%
  1618. break;
  1619. #ifdef INCLUDE_SONIC
  1620. case WCMD_SONIC_SPEED:
  1621. sonicSpeed = (double)q[1] / 1024;
  1622. break;
  1623. #endif
  1624. }
  1625. if(result==0)
  1626. {
  1627. WcmdqIncHead();
  1628. resume=0;
  1629. }
  1630. else
  1631. {
  1632. resume=1;
  1633. }
  1634. }
  1635. return(0);
  1636. } // end of WavegenFill2
  1637. #ifdef INCLUDE_SONIC
  1638. /* Speed up the audio samples with libsonic. */
  1639. static int SpeedUp(short *outbuf, int length_in, int length_out, int end_of_text)
  1640. {//==============================================================================
  1641. if(length_in >0)
  1642. {
  1643. if(sonicSpeedupStream == NULL)
  1644. {
  1645. sonicSpeedupStream = sonicCreateStream(22050, 1);
  1646. }
  1647. if(sonicGetSpeed(sonicSpeedupStream) != sonicSpeed)
  1648. {
  1649. sonicSetSpeed(sonicSpeedupStream, sonicSpeed);
  1650. }
  1651. sonicWriteShortToStream(sonicSpeedupStream, outbuf, length_in);
  1652. }
  1653. if(sonicSpeedupStream == NULL)
  1654. return(0);
  1655. if(end_of_text)
  1656. {
  1657. sonicFlushStream(sonicSpeedupStream);
  1658. }
  1659. return sonicReadShortFromStream(sonicSpeedupStream, outbuf, length_out);
  1660. } // end of SpeedUp
  1661. #endif
  1662. /* Call WavegenFill2, and then speed up the output samples. */
  1663. int WavegenFill(int fill_zeros)
  1664. {//============================
  1665. int finished;
  1666. unsigned char *p_start;
  1667. p_start = out_ptr;
  1668. // fill_zeros is ignored. It is now done in the portaudio callback
  1669. finished = WavegenFill2(0);
  1670. #ifdef INCLUDE_SONIC
  1671. if(sonicSpeed > 1.0)
  1672. {
  1673. int length;
  1674. int max_length;
  1675. max_length = (out_end - p_start);
  1676. length = 2*SpeedUp((short *)p_start, (out_ptr-p_start)/2, max_length/2, finished);
  1677. out_ptr = p_start + length;
  1678. if(length >= max_length)
  1679. finished = 0; // there may be more data to flush
  1680. }
  1681. #endif
  1682. return finished;
  1683. } // end of WavegenFill