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.cpp 51KB

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