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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2013 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. #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. static void WavegenInitPkData(int which)
  329. {//=====================================
  330. // this is only needed to set up the presets for pk_shape1 and pk_shape2
  331. // These have already been pre-calculated and preset
  332. #ifdef deleted
  333. int ix;
  334. int p;
  335. float x;
  336. float y[PEAKSHAPEW];
  337. float maxy=0;
  338. if(which==0)
  339. pk_shape = pk_shape1;
  340. else
  341. pk_shape = pk_shape2;
  342. p = 0;
  343. for(ix=0;ix<PEAKSHAPEW;ix++)
  344. {
  345. x = (4.5*ix)/PEAKSHAPEW;
  346. if(x >= pk_shape_x[which][p+3]) p++;
  347. y[ix] = polint(&pk_shape_x[which][p],&pk_shape_y[which][p],3,x);
  348. if(y[ix] > maxy) maxy = y[ix];
  349. }
  350. for(ix=0;ix<PEAKSHAPEW;ix++)
  351. {
  352. p = (int)(y[ix]*255/maxy);
  353. pk_shape[ix] = (p >= 0) ? p : 0;
  354. }
  355. pk_shape[PEAKSHAPEW]=0;
  356. #endif
  357. } // end of WavegenInitPkData
  358. #ifdef USE_PORTAUDIO
  359. // PortAudio interface
  360. static int userdata[4];
  361. static PaError pa_init_err=0;
  362. static int out_channels=1;
  363. unsigned char *outbuffer = NULL;
  364. int outbuffer_size = 0;
  365. #if USE_PORTAUDIO == 18
  366. static int WaveCallback(void *inputBuffer, void *outputBuffer,
  367. unsigned long framesPerBuffer, PaTimestamp outTime, void *userData )
  368. #else
  369. static int WaveCallback(const void *inputBuffer, void *outputBuffer,
  370. long unsigned int framesPerBuffer, const PaStreamCallbackTimeInfo *outTime,
  371. PaStreamCallbackFlags flags, void *userData )
  372. #endif
  373. {
  374. int ix;
  375. int result;
  376. unsigned char *p;
  377. unsigned char *out_buf;
  378. unsigned char *out_end2;
  379. int pa_size;
  380. pa_size = framesPerBuffer*2;
  381. // make a buffer 3x size of the portaudio output
  382. ix = pa_size*3;
  383. if(ix > outbuffer_size)
  384. {
  385. outbuffer = (unsigned char *)realloc(outbuffer, ix);
  386. if(outbuffer == NULL)
  387. {
  388. fprintf(stderr, "espeak: out of memory\n");
  389. }
  390. outbuffer_size = ix;
  391. out_ptr = NULL;
  392. }
  393. if(out_ptr == NULL)
  394. {
  395. out_ptr = out_start = outbuffer;
  396. out_end = out_start + outbuffer_size;
  397. }
  398. out_end2 = &outbuffer[pa_size]; // top of data needed for the portaudio buffer
  399. #ifdef LIBRARY
  400. event_list_ix = 0;
  401. #endif
  402. result = WavegenFill(1);
  403. // copy from the outbut buffer into the portaudio buffer
  404. if(result && (out_ptr > out_end2))
  405. {
  406. result = 0; // don't end yet, there is more data in the buffer than can fit in portaudio
  407. }
  408. while(out_ptr < out_end2)
  409. *out_ptr++ = 0; // fill with zeros up to the size of the portaudio buffer
  410. memcpy(outputBuffer, outbuffer, pa_size);
  411. // move the remaining contents of the start of the output buffer
  412. for(p = out_end2; p < out_end; p++)
  413. {
  414. p[-pa_size] = p[0];
  415. }
  416. out_ptr -= pa_size;
  417. #ifdef LIBRARY
  418. count_samples += framesPerBuffer;
  419. if(synth_callback)
  420. {
  421. // synchronous-playback mode, allow the calling process to abort the speech
  422. event_list[event_list_ix].type = espeakEVENT_LIST_TERMINATED; // indicates end of event list
  423. event_list[event_list_ix].user_data = 0;
  424. if(synth_callback(NULL,0,event_list) == 1)
  425. {
  426. SpeakNextClause(NULL,NULL,2); // stop speaking
  427. result = 1;
  428. }
  429. }
  430. #endif
  431. #ifdef ARCH_BIG
  432. {
  433. // swap the order of bytes in each sound sample in the portaudio buffer
  434. int c;
  435. unsigned char *buf_end;
  436. out_buf = (unsigned char *)outputBuffer;
  437. buf_end = out_buf + framesPerBuffer*2;
  438. while(out_buf < buf_end)
  439. {
  440. c = out_buf[0];
  441. out_buf[0] = out_buf[1];
  442. out_buf[1] = c;
  443. out_buf += 2;
  444. }
  445. }
  446. #endif
  447. if(out_channels == 2)
  448. {
  449. // sound output can only do stereo, not mono. Duplicate each sound sample to
  450. // produce 2 channels.
  451. out_buf = (unsigned char *)outputBuffer;
  452. for(ix=framesPerBuffer-1; ix>=0; ix--)
  453. {
  454. p = &out_buf[ix*4];
  455. p[3] = p[1] = out_buf[ix*2 + 1];
  456. p[2] = p[0] = out_buf[ix*2];
  457. }
  458. }
  459. #if USE_PORTAUDIO == 18
  460. #ifdef PLATFORM_WINDOWS
  461. return(result);
  462. #endif
  463. if(result != 0)
  464. {
  465. static int end_timer = 0;
  466. if(end_timer == 0)
  467. end_timer = 4;
  468. if(end_timer > 0)
  469. {
  470. end_timer--;
  471. if(end_timer == 0)
  472. return(1);
  473. }
  474. }
  475. return(0);
  476. #else
  477. return(result);
  478. #endif
  479. } // end of WaveCallBack
  480. #if USE_PORTAUDIO == 19
  481. /* This is a fixed version of Pa_OpenDefaultStream() for use if the version in portaudio V19
  482. is broken */
  483. static PaError Pa_OpenDefaultStream2( PaStream** stream,
  484. int inputChannelCount,
  485. int outputChannelCount,
  486. PaSampleFormat sampleFormat,
  487. double sampleRate,
  488. unsigned long framesPerBuffer,
  489. PaStreamCallback *streamCallback,
  490. void *userData )
  491. {
  492. PaError result;
  493. PaStreamParameters hostApiOutputParameters;
  494. if(option_device_number >= 0)
  495. hostApiOutputParameters.device = option_device_number;
  496. else
  497. hostApiOutputParameters.device = Pa_GetDefaultOutputDevice();
  498. if( hostApiOutputParameters.device == paNoDevice )
  499. return paDeviceUnavailable;
  500. hostApiOutputParameters.channelCount = outputChannelCount;
  501. hostApiOutputParameters.sampleFormat = sampleFormat;
  502. /* defaultHighOutputLatency is used below instead of
  503. defaultLowOutputLatency because it is more important for the default
  504. stream to work reliably than it is for it to work with the lowest
  505. latency.
  506. */
  507. hostApiOutputParameters.suggestedLatency =
  508. Pa_GetDeviceInfo( hostApiOutputParameters.device )->defaultHighOutputLatency;
  509. hostApiOutputParameters.hostApiSpecificStreamInfo = NULL;
  510. result = Pa_OpenStream(
  511. stream, NULL, &hostApiOutputParameters, sampleRate, framesPerBuffer, paNoFlag, streamCallback, userData );
  512. return(result);
  513. }
  514. #endif
  515. int WavegenOpenSound()
  516. {//===================
  517. PaError err, err2;
  518. PaError active;
  519. if(option_waveout || option_quiet)
  520. {
  521. // writing to WAV file, not to portaudio
  522. return(0);
  523. }
  524. #if USE_PORTAUDIO == 18
  525. active = Pa_StreamActive(pa_stream);
  526. #else
  527. active = Pa_IsStreamActive(pa_stream);
  528. #endif
  529. if(active == 1)
  530. return(0);
  531. if(active < 0)
  532. {
  533. out_channels = 1;
  534. #if USE_PORTAUDIO == 18
  535. err2 = Pa_OpenDefaultStream(&pa_stream,0,1,paInt16,samplerate,512,N_WAV_BUF,WaveCallback,(void *)userdata);
  536. if(err2 == paInvalidChannelCount)
  537. {
  538. // failed to open with mono, try stereo
  539. out_channels=2;
  540. err2 = Pa_OpenDefaultStream(&pa_stream,0,2,paInt16,samplerate,512,N_WAV_BUF,WaveCallback,(void *)userdata);
  541. }
  542. #else
  543. err2 = Pa_OpenDefaultStream2(&pa_stream,0,1,paInt16,(double)samplerate,512,WaveCallback,(void *)userdata);
  544. if(err2 == paInvalidChannelCount)
  545. {
  546. // failed to open with mono, try stereo
  547. out_channels=2;
  548. err2 = Pa_OpenDefaultStream(&pa_stream,0,2,paInt16,(double)samplerate,512,WaveCallback,(void *)userdata);
  549. }
  550. #endif
  551. }
  552. err = Pa_StartStream(pa_stream);
  553. #if USE_PORTAUDIO == 19
  554. if(err == paStreamIsNotStopped)
  555. {
  556. // not sure why we need this, but PA v19 seems to need it
  557. err = Pa_StopStream(pa_stream);
  558. err = Pa_StartStream(pa_stream);
  559. }
  560. #endif
  561. if(err != paNoError)
  562. {
  563. // exit speak if we can't open the sound device - this is OK if speak is being run for each utterance
  564. exit(2);
  565. }
  566. return(0);
  567. }
  568. int WavegenCloseSound()
  569. {//====================
  570. PaError active;
  571. // check whether speaking has finished, and close the stream
  572. if(pa_stream != NULL)
  573. {
  574. #if USE_PORTAUDIO == 18
  575. active = Pa_StreamActive(pa_stream);
  576. #else
  577. active = Pa_IsStreamActive(pa_stream);
  578. #endif
  579. if(WcmdqUsed() == 0) // also check that the queue is empty
  580. {
  581. if(active == 0)
  582. {
  583. Pa_CloseStream(pa_stream);
  584. pa_stream = NULL;
  585. return(1);
  586. }
  587. }
  588. else
  589. {
  590. WavegenOpenSound(); // still items in the queue, shouldn't be closed
  591. }
  592. }
  593. return(0);
  594. }
  595. int WavegenInitSound()
  596. {//===================
  597. PaError err;
  598. if(option_quiet)
  599. return(0);
  600. // PortAudio sound output library
  601. err = Pa_Initialize();
  602. pa_init_err = err;
  603. if(err != paNoError)
  604. {
  605. fprintf(stderr,"Failed to initialise the PortAudio sound\n");
  606. return(1);
  607. }
  608. return(0);
  609. }
  610. #else
  611. int WavegenOpenSound()
  612. {//===================
  613. return(0);
  614. }
  615. int WavegenCloseSound()
  616. {//====================
  617. return(0);
  618. }
  619. int WavegenInitSound()
  620. {//===================
  621. return(0);
  622. }
  623. #endif
  624. void WavegenInit(int rate, int wavemult_fact)
  625. {//==========================================
  626. int ix;
  627. double x;
  628. if(wavemult_fact == 0)
  629. wavemult_fact=60; // default
  630. wvoice = NULL;
  631. samplerate = samplerate_native = rate;
  632. PHASE_INC_FACTOR = 0x8000000 / samplerate; // assumes pitch is Hz*32
  633. Flutter_inc = (64 * samplerate)/rate;
  634. samplecount = 0;
  635. nsamples = 0;
  636. wavephase = 0x7fffffff;
  637. max_hval = 0;
  638. wdata.amplitude = 32;
  639. wdata.amplitude_fmt = 100;
  640. for(ix=0; ix<N_EMBEDDED_VALUES; ix++)
  641. embedded_value[ix] = embedded_default[ix];
  642. // set up window to generate a spread of harmonics from a
  643. // single peak for HF peaks
  644. wavemult_max = (samplerate * wavemult_fact)/(256 * 50);
  645. if(wavemult_max > N_WAVEMULT) wavemult_max = N_WAVEMULT;
  646. wavemult_offset = wavemult_max/2;
  647. if(samplerate != 22050)
  648. {
  649. // wavemult table has preset values for 22050 Hz, we only need to
  650. // recalculate them if we have a different sample rate
  651. for(ix=0; ix<wavemult_max; ix++)
  652. {
  653. x = 127*(1.0 - cos(PI2*ix/wavemult_max));
  654. wavemult[ix] = (int)x;
  655. }
  656. }
  657. WavegenInitPkData(1);
  658. WavegenInitPkData(0);
  659. pk_shape = pk_shape2; // pk_shape2
  660. #ifdef INCLUDE_KLATT
  661. KlattInit();
  662. #endif
  663. #ifdef LOG_FRAMES
  664. remove("log-espeakedit");
  665. remove("log-klatt");
  666. #endif
  667. } // end of WavegenInit
  668. int GetAmplitude(void)
  669. {//===================
  670. int amp;
  671. // normal, none, reduced, moderate, strong
  672. static const unsigned char amp_emphasis[5] = {16, 16, 10, 16, 22};
  673. amp = (embedded_value[EMBED_A])*55/100;
  674. general_amplitude = amp * amp_emphasis[embedded_value[EMBED_F]] / 16;
  675. return(general_amplitude);
  676. }
  677. static void WavegenSetEcho(void)
  678. {//=============================
  679. int delay;
  680. int amp;
  681. voicing = wvoice->voicing;
  682. delay = wvoice->echo_delay;
  683. amp = wvoice->echo_amp;
  684. if(delay >= N_ECHO_BUF)
  685. delay = N_ECHO_BUF-1;
  686. if(amp > 100)
  687. amp = 100;
  688. memset(echo_buf,0,sizeof(echo_buf));
  689. echo_tail = 0;
  690. if(embedded_value[EMBED_H] > 0)
  691. {
  692. // set echo from an embedded command in the text
  693. amp = embedded_value[EMBED_H];
  694. delay = 130;
  695. }
  696. if(delay == 0)
  697. amp = 0;
  698. echo_head = (delay * samplerate)/1000;
  699. echo_length = echo_head; // ensure completion of echo at the end of speech. Use 1 delay period?
  700. if(amp == 0)
  701. echo_length = 0;
  702. if(amp > 20)
  703. echo_length = echo_head * 2; // perhaps allow 2 echo periods if the echo is loud.
  704. // echo_amp units are 1/256ths of the amplitude of the original sound.
  705. echo_amp = amp;
  706. // compensate (partially) for increase in amplitude due to echo
  707. general_amplitude = GetAmplitude();
  708. general_amplitude = ((general_amplitude * (500-amp))/500);
  709. } // end of WavegenSetEcho
  710. int PeaksToHarmspect(wavegen_peaks_t *peaks, int pitch, int *htab, int control)
  711. {//============================================================================
  712. // Calculate the amplitude of each harmonics from the formants
  713. // Only for formants 0 to 5
  714. // control 0=initial call, 1=every 64 cycles
  715. // pitch and freqs are Hz<<16
  716. int f;
  717. wavegen_peaks_t *p;
  718. int fp; // centre freq of peak
  719. int fhi; // high freq of peak
  720. int h; // harmonic number
  721. int pk;
  722. int hmax;
  723. int hmax_samplerate; // highest harmonic allowed for the samplerate
  724. int x;
  725. int ix;
  726. int h1;
  727. #ifdef SPECT_EDITOR
  728. if(harm_sqrt_n > 0)
  729. return(HarmToHarmspect(pitch,htab));
  730. #endif
  731. // initialise as much of *out as we will need
  732. if(wvoice == NULL)
  733. return(1);
  734. hmax = (peaks[wvoice->n_harmonic_peaks].freq + peaks[wvoice->n_harmonic_peaks].right)/pitch;
  735. if(hmax >= MAX_HARMONIC)
  736. hmax = MAX_HARMONIC-1;
  737. // restrict highest harmonic to half the samplerate
  738. hmax_samplerate = (((samplerate * 19)/40) << 16)/pitch; // only 95% of Nyquist freq
  739. // hmax_samplerate = (samplerate << 16)/(pitch*2);
  740. if(hmax > hmax_samplerate)
  741. hmax = hmax_samplerate;
  742. for(h=0;h<=hmax;h++)
  743. htab[h]=0;
  744. h=0;
  745. for(pk=0; pk<=wvoice->n_harmonic_peaks; pk++)
  746. {
  747. p = &peaks[pk];
  748. if((p->height == 0) || (fp = p->freq)==0)
  749. continue;
  750. fhi = p->freq + p->right;
  751. h = ((p->freq - p->left) / pitch) + 1;
  752. if(h <= 0) h = 1;
  753. for(f=pitch*h; f < fp; f+=pitch)
  754. {
  755. htab[h++] += pk_shape[(fp-f)/(p->left>>8)] * p->height;
  756. }
  757. for(; f < fhi; f+=pitch)
  758. {
  759. htab[h++] += pk_shape[(f-fp)/(p->right>>8)] * p->height;
  760. }
  761. }
  762. {
  763. int y;
  764. int h2;
  765. // increase bass
  766. y = peaks[1].height * 10; // addition as a multiple of 1/256s
  767. h2 = (1000<<16)/pitch; // decrease until 1000Hz
  768. if(h2 > 0)
  769. {
  770. x = y/h2;
  771. h = 1;
  772. while(y > 0)
  773. {
  774. htab[h++] += y;
  775. y -= x;
  776. }
  777. }
  778. }
  779. // find the nearest harmonic for HF peaks where we don't use shape
  780. for(; pk<N_PEAKS; pk++)
  781. {
  782. x = peaks[pk].height >> 14;
  783. peak_height[pk] = (x * x * 5)/2;
  784. // find the nearest harmonic for HF peaks where we don't use shape
  785. if(control == 0)
  786. {
  787. // set this initially, but make changes only at the quiet point
  788. peak_harmonic[pk] = peaks[pk].freq / pitch;
  789. }
  790. // only use harmonics up to half the samplerate
  791. if(peak_harmonic[pk] >= hmax_samplerate)
  792. peak_height[pk] = 0;
  793. }
  794. // convert from the square-rooted values
  795. f = 0;
  796. for(h=0; h<=hmax; h++, f+=pitch)
  797. {
  798. x = htab[h] >> 15;
  799. htab[h] = (x * x) >> 8;
  800. if((ix = (f >> 19)) < N_TONE_ADJUST)
  801. {
  802. htab[h] = (htab[h] * wvoice->tone_adjust[ix]) >> 13; // index tone_adjust with Hz/8
  803. }
  804. }
  805. // adjust the amplitude of the first harmonic, affects tonal quality
  806. h1 = htab[1] * option_harmonic1;
  807. htab[1] = h1/8;
  808. // calc intermediate increments of LF harmonics
  809. if(control & 1)
  810. {
  811. for(h=1; h<N_LOWHARM; h++)
  812. {
  813. harm_inc[h] = (htab[h] - harmspect[h]) >> 3;
  814. }
  815. }
  816. return(hmax); // highest harmonic number
  817. } // end of PeaksToHarmspect
  818. static void AdvanceParameters()
  819. {//============================
  820. // Called every 64 samples to increment the formant freq, height, and widths
  821. int x;
  822. int ix;
  823. static int Flutter_ix = 0;
  824. // advance the pitch
  825. wdata.pitch_ix += wdata.pitch_inc;
  826. if((ix = wdata.pitch_ix>>8) > 127) ix = 127;
  827. x = wdata.pitch_env[ix] * wdata.pitch_range;
  828. wdata.pitch = (x>>8) + wdata.pitch_base;
  829. amp_ix += amp_inc;
  830. /* add pitch flutter */
  831. if(Flutter_ix >= (N_FLUTTER*64))
  832. Flutter_ix = 0;
  833. x = ((int)(Flutter_tab[Flutter_ix >> 6])-0x80) * flutter_amp;
  834. Flutter_ix += Flutter_inc;
  835. wdata.pitch += x;
  836. if(wdata.pitch < 102400)
  837. wdata.pitch = 102400; // min pitch, 25 Hz (25 << 12)
  838. if(samplecount == samplecount_start)
  839. return;
  840. for(ix=0; ix <= wvoice->n_harmonic_peaks; ix++)
  841. {
  842. peaks[ix].freq1 += peaks[ix].freq_inc;
  843. peaks[ix].freq = int(peaks[ix].freq1);
  844. peaks[ix].height1 += peaks[ix].height_inc;
  845. if((peaks[ix].height = int(peaks[ix].height1)) < 0)
  846. peaks[ix].height = 0;
  847. peaks[ix].left1 += peaks[ix].left_inc;
  848. peaks[ix].left = int(peaks[ix].left1);
  849. if(ix < 3)
  850. {
  851. peaks[ix].right1 += peaks[ix].right_inc;
  852. peaks[ix].right = int(peaks[ix].right1);
  853. }
  854. else
  855. {
  856. peaks[ix].right = peaks[ix].left;
  857. }
  858. }
  859. for(;ix < 8; ix++)
  860. {
  861. // formants 6,7,8 don't have a width parameter
  862. if(ix < 7)
  863. {
  864. peaks[ix].freq1 += peaks[ix].freq_inc;
  865. peaks[ix].freq = int(peaks[ix].freq1);
  866. }
  867. peaks[ix].height1 += peaks[ix].height_inc;
  868. if((peaks[ix].height = int(peaks[ix].height1)) < 0)
  869. peaks[ix].height = 0;
  870. }
  871. #ifdef SPECT_EDITOR
  872. if(harm_sqrt_n != 0)
  873. {
  874. // We are generating from a harmonic spectrum at a given pitch, not from formant peaks
  875. for(ix=0; ix<harm_sqrt_n; ix++)
  876. harm_sqrt[ix] += harm_sqrt_inc[ix];
  877. }
  878. #endif
  879. } // end of AdvanceParameters
  880. #ifndef PLATFORM_RISCOS
  881. static double resonator(RESONATOR *r, double input)
  882. {//================================================
  883. double x;
  884. x = r->a * input + r->b * r->x1 + r->c * r->x2;
  885. r->x2 = r->x1;
  886. r->x1 = x;
  887. return x;
  888. }
  889. static void setresonator(RESONATOR *rp, int freq, int bwidth, int init)
  890. {//====================================================================
  891. // freq Frequency of resonator in Hz
  892. // bwidth Bandwidth of resonator in Hz
  893. // init Initialize internal data
  894. double x;
  895. double arg;
  896. if(init)
  897. {
  898. rp->x1 = 0;
  899. rp->x2 = 0;
  900. }
  901. // x = exp(-pi * bwidth * t)
  902. arg = minus_pi_t * bwidth;
  903. x = exp(arg);
  904. // c = -(x*x)
  905. rp->c = -(x * x);
  906. // b = x * 2*cos(2 pi * freq * t)
  907. arg = two_pi_t * freq;
  908. rp->b = x * cos(arg) * 2.0;
  909. // a = 1.0 - b - c
  910. rp->a = 1.0 - rp->b - rp->c;
  911. } // end if setresonator
  912. #endif
  913. void InitBreath(void)
  914. {//==================
  915. #ifndef PLATFORM_RISCOS
  916. int ix;
  917. minus_pi_t = -PI / samplerate;
  918. two_pi_t = -2.0 * minus_pi_t;
  919. for(ix=0; ix<N_PEAKS; ix++)
  920. {
  921. setresonator(&rbreath[ix],2000,200,1);
  922. }
  923. #endif
  924. } // end of InitBreath
  925. static void SetBreath()
  926. {//====================
  927. #ifndef PLATFORM_RISCOS
  928. int pk;
  929. if(wvoice->breath[0] == 0)
  930. return;
  931. for(pk=1; pk<N_PEAKS; pk++)
  932. {
  933. if(wvoice->breath[pk] != 0)
  934. {
  935. // breath[0] indicates that some breath formants are needed
  936. // set the freq from the current ynthesis formant and the width from the voice data
  937. setresonator(&rbreath[pk], peaks[pk].freq >> 16, wvoice->breathw[pk],0);
  938. }
  939. }
  940. #endif
  941. } // end of SetBreath
  942. static int ApplyBreath(void)
  943. {//=========================
  944. int value = 0;
  945. #ifndef PLATFORM_RISCOS
  946. int noise;
  947. int ix;
  948. int amp;
  949. // use two random numbers, for alternate formants
  950. noise = (rand() & 0x3fff) - 0x2000;
  951. for(ix=1; ix < N_PEAKS; ix++)
  952. {
  953. if((amp = wvoice->breath[ix]) != 0)
  954. {
  955. amp *= (peaks[ix].height >> 14);
  956. value += int(resonator(&rbreath[ix],noise) * amp);
  957. }
  958. }
  959. #endif
  960. return (value);
  961. }
  962. int Wavegen()
  963. {//==========
  964. unsigned short waveph;
  965. unsigned short theta;
  966. int total;
  967. int h;
  968. int ix;
  969. int z, z1, z2;
  970. int echo;
  971. int ov;
  972. static int maxh, maxh2;
  973. int pk;
  974. signed char c;
  975. int sample;
  976. int amp;
  977. int modn_amp, modn_period;
  978. static int agc = 256;
  979. static int h_switch_sign = 0;
  980. static int cycle_count = 0;
  981. static int amplitude2 = 0; // adjusted for pitch
  982. // continue until the output buffer is full, or
  983. // the required number of samples have been produced
  984. for(;;)
  985. {
  986. if((end_wave==0) && (samplecount==nsamples))
  987. return(0);
  988. if((samplecount & 0x3f) == 0)
  989. {
  990. // every 64 samples, adjust the parameters
  991. if(samplecount == 0)
  992. {
  993. hswitch = 0;
  994. harmspect = hspect[0];
  995. maxh2 = PeaksToHarmspect(peaks, wdata.pitch<<4, hspect[0], 0);
  996. // adjust amplitude to compensate for fewer harmonics at higher pitch
  997. // amplitude2 = (wdata.amplitude * wdata.pitch)/(100 << 11);
  998. amplitude2 = (wdata.amplitude * (wdata.pitch >> 8) * wdata.amplitude_fmt)/(10000 << 3);
  999. // switch sign of harmonics above about 900Hz, to reduce max peak amplitude
  1000. h_switch_sign = 890 / (wdata.pitch >> 12);
  1001. }
  1002. else
  1003. AdvanceParameters();
  1004. // pitch is Hz<<12
  1005. phaseinc = (wdata.pitch>>7) * PHASE_INC_FACTOR;
  1006. cycle_samples = samplerate/(wdata.pitch >> 12); // sr/(pitch*2)
  1007. hf_factor = wdata.pitch >> 11;
  1008. maxh = maxh2;
  1009. harmspect = hspect[hswitch];
  1010. hswitch ^= 1;
  1011. maxh2 = PeaksToHarmspect(peaks, wdata.pitch<<4, hspect[hswitch], 1);
  1012. SetBreath();
  1013. }
  1014. else
  1015. if((samplecount & 0x07) == 0)
  1016. {
  1017. for(h=1; h<N_LOWHARM && h<=maxh2 && h<=maxh; h++)
  1018. {
  1019. harmspect[h] += harm_inc[h];
  1020. }
  1021. // bring automctic gain control back towards unity
  1022. if(agc < 256) agc++;
  1023. }
  1024. samplecount++;
  1025. if(wavephase > 0)
  1026. {
  1027. wavephase += phaseinc;
  1028. if(wavephase < 0)
  1029. {
  1030. // sign has changed, reached a quiet point in the waveform
  1031. cbytes = wavemult_offset - (cycle_samples)/2;
  1032. if(samplecount > nsamples)
  1033. return(0);
  1034. cycle_count++;
  1035. for(pk=wvoice->n_harmonic_peaks+1; pk<N_PEAKS; pk++)
  1036. {
  1037. // find the nearest harmonic for HF peaks where we don't use shape
  1038. peak_harmonic[pk] = peaks[pk].freq / (wdata.pitch*16);
  1039. }
  1040. // adjust amplitude to compensate for fewer harmonics at higher pitch
  1041. // amplitude2 = (wdata.amplitude * wdata.pitch)/(100 << 11);
  1042. amplitude2 = (wdata.amplitude * (wdata.pitch >> 8) * wdata.amplitude_fmt)/(10000 << 3);
  1043. if(glottal_flag > 0)
  1044. {
  1045. if(glottal_flag == 3)
  1046. {
  1047. if((nsamples-samplecount) < (cycle_samples*2))
  1048. {
  1049. // Vowel before glottal-stop.
  1050. // This is the start of the penultimate cycle, reduce its amplitude
  1051. glottal_flag = 2;
  1052. amplitude2 = (amplitude2 * glottal_reduce)/256;
  1053. }
  1054. }
  1055. else
  1056. if(glottal_flag == 4)
  1057. {
  1058. // Vowel following a glottal-stop.
  1059. // This is the start of the second cycle, reduce its amplitude
  1060. glottal_flag = 2;
  1061. amplitude2 = (amplitude2 * glottal_reduce)/256;
  1062. }
  1063. else
  1064. {
  1065. glottal_flag--;
  1066. }
  1067. }
  1068. if(amplitude_env != NULL)
  1069. {
  1070. // amplitude envelope is only used for creaky voice effect on certain vowels/tones
  1071. if((ix = amp_ix>>8) > 127) ix = 127;
  1072. amp = amplitude_env[ix];
  1073. amplitude2 = (amplitude2 * amp)/128;
  1074. // if(amp < 255)
  1075. // modulation_type = 7;
  1076. }
  1077. // introduce roughness into the sound by reducing the amplitude of
  1078. modn_period = 0;
  1079. if(voice->roughness < N_ROUGHNESS)
  1080. {
  1081. modn_period = modulation_tab[voice->roughness][modulation_type];
  1082. modn_amp = modn_period & 0xf;
  1083. modn_period = modn_period >> 4;
  1084. }
  1085. if(modn_period != 0)
  1086. {
  1087. if(modn_period==0xf)
  1088. {
  1089. // just once */
  1090. amplitude2 = (amplitude2 * modn_amp)/16;
  1091. modulation_type = 0;
  1092. }
  1093. else
  1094. {
  1095. // reduce amplitude every [modn_period} cycles
  1096. if((cycle_count % modn_period)==0)
  1097. amplitude2 = (amplitude2 * modn_amp)/16;
  1098. }
  1099. }
  1100. }
  1101. }
  1102. else
  1103. {
  1104. wavephase += phaseinc;
  1105. }
  1106. waveph = (unsigned short)(wavephase >> 16);
  1107. total = 0;
  1108. // apply HF peaks, formants 6,7,8
  1109. // add a single harmonic and then spread this my multiplying by a
  1110. // window. This is to reduce the processing power needed to add the
  1111. // higher frequence harmonics.
  1112. cbytes++;
  1113. if(cbytes >=0 && cbytes<wavemult_max)
  1114. {
  1115. for(pk=wvoice->n_harmonic_peaks+1; pk<N_PEAKS; pk++)
  1116. {
  1117. theta = peak_harmonic[pk] * waveph;
  1118. total += (long)sin_tab[theta >> 5] * peak_height[pk];
  1119. }
  1120. // spread the peaks by multiplying by a window
  1121. total = (long)(total / hf_factor) * wavemult[cbytes];
  1122. }
  1123. // apply main peaks, formants 0 to 5
  1124. #ifdef USE_ASSEMBLER_1
  1125. // use an optimised routine for this loop, if available
  1126. total += AddSineWaves(waveph, h_switch_sign, maxh, harmspect); // call an assembler code routine
  1127. #else
  1128. theta = waveph;
  1129. for(h=1; h<=h_switch_sign; h++)
  1130. {
  1131. total += (int(sin_tab[theta >> 5]) * harmspect[h]);
  1132. theta += waveph;
  1133. }
  1134. while(h<=maxh)
  1135. {
  1136. total -= (int(sin_tab[theta >> 5]) * harmspect[h]);
  1137. theta += waveph;
  1138. h++;
  1139. }
  1140. #endif
  1141. if(voicing != 64)
  1142. {
  1143. total = (total >> 6) * voicing;
  1144. }
  1145. #ifndef PLATFORM_RISCOS
  1146. if(wvoice->breath[0])
  1147. {
  1148. total += ApplyBreath();
  1149. }
  1150. #endif
  1151. // mix with sampled wave if required
  1152. z2 = 0;
  1153. if(wdata.mix_wavefile_ix < wdata.n_mix_wavefile)
  1154. {
  1155. if(wdata.mix_wave_scale == 0)
  1156. {
  1157. // a 16 bit sample
  1158. c = wdata.mix_wavefile[wdata.mix_wavefile_ix+wdata.mix_wavefile_offset+1];
  1159. sample = wdata.mix_wavefile[wdata.mix_wavefile_ix+wdata.mix_wavefile_offset] + (c * 256);
  1160. wdata.mix_wavefile_ix += 2;
  1161. }
  1162. else
  1163. {
  1164. // a 8 bit sample, scaled
  1165. sample = (signed char)wdata.mix_wavefile[wdata.mix_wavefile_offset+wdata.mix_wavefile_ix++] * wdata.mix_wave_scale;
  1166. }
  1167. z2 = (sample * wdata.amplitude_v) >> 10;
  1168. z2 = (z2 * wdata.mix_wave_amp)/32;
  1169. if((wdata.mix_wavefile_ix + wdata.mix_wavefile_offset) >= wdata.mix_wavefile_max) // reached the end of available WAV data
  1170. wdata.mix_wavefile_offset -= (wdata.mix_wavefile_max*3)/4;
  1171. }
  1172. z1 = z2 + (((total>>8) * amplitude2) >> 13);
  1173. echo = (echo_buf[echo_tail++] * echo_amp);
  1174. z1 += echo >> 8;
  1175. if(echo_tail >= N_ECHO_BUF)
  1176. echo_tail=0;
  1177. z = (z1 * agc) >> 8;
  1178. // check for overflow, 16bit signed samples
  1179. if(z >= 32768)
  1180. {
  1181. ov = 8388608/z1 - 1; // 8388608 is 2^23, i.e. max value * 256
  1182. if(ov < agc) agc = ov; // set agc to number of 1/256ths to multiply the sample by
  1183. z = (z1 * agc) >> 8; // reduce sample by agc value to prevent overflow
  1184. }
  1185. else
  1186. if(z <= -32768)
  1187. {
  1188. ov = -8388608/z1 - 1;
  1189. if(ov < agc) agc = ov;
  1190. z = (z1 * agc) >> 8;
  1191. }
  1192. *out_ptr++ = z;
  1193. *out_ptr++ = z >> 8;
  1194. echo_buf[echo_head++] = z;
  1195. if(echo_head >= N_ECHO_BUF)
  1196. echo_head = 0;
  1197. if(out_ptr >= out_end)
  1198. return(1);
  1199. }
  1200. return(0);
  1201. } // end of Wavegen
  1202. static int PlaySilence(int length, int resume)
  1203. {//===========================================
  1204. static int n_samples;
  1205. int value=0;
  1206. nsamples = 0;
  1207. samplecount = 0;
  1208. wavephase = 0x7fffffff;
  1209. if(length == 0)
  1210. return(0);
  1211. if(resume==0)
  1212. n_samples = length;
  1213. while(n_samples-- > 0)
  1214. {
  1215. value = (echo_buf[echo_tail++] * echo_amp) >> 8;
  1216. if(echo_tail >= N_ECHO_BUF)
  1217. echo_tail = 0;
  1218. *out_ptr++ = value;
  1219. *out_ptr++ = value >> 8;
  1220. echo_buf[echo_head++] = value;
  1221. if(echo_head >= N_ECHO_BUF)
  1222. echo_head = 0;
  1223. if(out_ptr >= out_end)
  1224. return(1);
  1225. }
  1226. return(0);
  1227. } // end of PlaySilence
  1228. static int PlayWave(int length, int resume, unsigned char *data, int scale, int amp)
  1229. {//=================================================================================
  1230. static int n_samples;
  1231. static int ix=0;
  1232. int value;
  1233. signed char c;
  1234. if(resume==0)
  1235. {
  1236. n_samples = length;
  1237. ix = 0;
  1238. }
  1239. nsamples = 0;
  1240. samplecount = 0;
  1241. while(n_samples-- > 0)
  1242. {
  1243. if(scale == 0)
  1244. {
  1245. // 16 bits data
  1246. c = data[ix+1];
  1247. value = data[ix] + (c * 256);
  1248. ix+=2;
  1249. }
  1250. else
  1251. {
  1252. // 8 bit data, shift by the specified scale factor
  1253. value = (signed char)data[ix++] * scale;
  1254. }
  1255. value *= (consonant_amp * general_amplitude); // reduce strength of consonant
  1256. value = value >> 10;
  1257. value = (value * amp)/32;
  1258. value += ((echo_buf[echo_tail++] * echo_amp) >> 8);
  1259. if(value > 32767)
  1260. value = 32768;
  1261. else
  1262. if(value < -32768)
  1263. value = -32768;
  1264. if(echo_tail >= N_ECHO_BUF)
  1265. echo_tail = 0;
  1266. out_ptr[0] = value;
  1267. out_ptr[1] = value >> 8;
  1268. out_ptr+=2;
  1269. echo_buf[echo_head++] = (value*3)/4;
  1270. if(echo_head >= N_ECHO_BUF)
  1271. echo_head = 0;
  1272. if(out_ptr >= out_end)
  1273. return(1);
  1274. }
  1275. return(0);
  1276. }
  1277. static int SetWithRange0(int value, int max)
  1278. {//=========================================
  1279. if(value < 0)
  1280. return(0);
  1281. if(value > max)
  1282. return(max);
  1283. return(value);
  1284. }
  1285. static void SetPitchFormants()
  1286. {//===========================
  1287. int ix;
  1288. int factor = 256;
  1289. int pitch_value;
  1290. // adjust formants to give better results for a different voice pitch
  1291. if((pitch_value = embedded_value[EMBED_P]) > MAX_PITCH_VALUE)
  1292. pitch_value = MAX_PITCH_VALUE;
  1293. if(pitch_value > 50)
  1294. {
  1295. // only adjust if the pitch is higher than normal
  1296. factor = 256 + (25 * (pitch_value - 50))/50;
  1297. }
  1298. for(ix=0; ix<=5; ix++)
  1299. {
  1300. wvoice->freq[ix] = (wvoice->freq2[ix] * factor)/256;
  1301. }
  1302. factor = embedded_value[EMBED_T]*3;
  1303. wvoice->height[0] = (wvoice->height2[0] * (256 - factor*2))/256;
  1304. wvoice->height[1] = (wvoice->height2[1] * (256 - factor))/256;
  1305. }
  1306. void SetEmbedded(int control, int value)
  1307. {//=====================================
  1308. // there was an embedded command in the text at this point
  1309. int sign=0;
  1310. int command;
  1311. command = control & 0x1f;
  1312. if((control & 0x60) == 0x60)
  1313. sign = -1;
  1314. else
  1315. if((control & 0x60) == 0x40)
  1316. sign = 1;
  1317. if(command < N_EMBEDDED_VALUES)
  1318. {
  1319. if(sign == 0)
  1320. embedded_value[command] = value;
  1321. else
  1322. embedded_value[command] += (value * sign);
  1323. embedded_value[command] = SetWithRange0(embedded_value[command],embedded_max[command]);
  1324. }
  1325. switch(command)
  1326. {
  1327. case EMBED_T:
  1328. WavegenSetEcho(); // and drop through to case P
  1329. case EMBED_P:
  1330. SetPitchFormants();
  1331. break;
  1332. case EMBED_A: // amplitude
  1333. general_amplitude = GetAmplitude();
  1334. break;
  1335. case EMBED_F: // emphasis
  1336. general_amplitude = GetAmplitude();
  1337. break;
  1338. case EMBED_H:
  1339. WavegenSetEcho();
  1340. break;
  1341. }
  1342. }
  1343. void WavegenSetVoice(voice_t *v)
  1344. {//=============================
  1345. static voice_t v2;
  1346. memcpy(&v2,v,sizeof(v2));
  1347. wvoice = &v2;
  1348. if(v->peak_shape==0)
  1349. pk_shape = pk_shape1;
  1350. else
  1351. pk_shape = pk_shape2;
  1352. consonant_amp = (v->consonant_amp * 26) /100;
  1353. if(samplerate <= 11000)
  1354. {
  1355. consonant_amp = consonant_amp*2; // emphasize consonants at low sample rates
  1356. option_harmonic1 = 6;
  1357. }
  1358. WavegenSetEcho();
  1359. SetPitchFormants();
  1360. MarkerEvent(espeakEVENT_SAMPLERATE, 0, wvoice->samplerate, 0, out_ptr);
  1361. // WVoiceChanged(wvoice);
  1362. }
  1363. static void SetAmplitude(int length, unsigned char *amp_env, int value)
  1364. {//====================================================================
  1365. amp_ix = 0;
  1366. if(length==0)
  1367. amp_inc = 0;
  1368. else
  1369. amp_inc = (256 * ENV_LEN * STEPSIZE)/length;
  1370. wdata.amplitude = (value * general_amplitude)/16;
  1371. wdata.amplitude_v = (wdata.amplitude * wvoice->consonant_ampv * 15)/100; // for wave mixed with voiced sounds
  1372. amplitude_env = amp_env;
  1373. }
  1374. void SetPitch2(voice_t *voice, int pitch1, int pitch2, int *pitch_base, int *pitch_range)
  1375. {//======================================================================================
  1376. int x;
  1377. int base;
  1378. int range;
  1379. int pitch_value;
  1380. if(pitch1 > pitch2)
  1381. {
  1382. x = pitch1; // swap values
  1383. pitch1 = pitch2;
  1384. pitch2 = x;
  1385. }
  1386. if((pitch_value = embedded_value[EMBED_P]) > MAX_PITCH_VALUE)
  1387. pitch_value = MAX_PITCH_VALUE;
  1388. pitch_value -= embedded_value[EMBED_T]; // adjust tone for announcing punctuation
  1389. if(pitch_value < 0)
  1390. pitch_value = 0;
  1391. base = (voice->pitch_base * pitch_adjust_tab[pitch_value])/128;
  1392. range = (voice->pitch_range * embedded_value[EMBED_R])/50;
  1393. // compensate for change in pitch when the range is narrowed or widened
  1394. base -= (range - voice->pitch_range)*18;
  1395. *pitch_base = base + (pitch1 * range)/2;
  1396. *pitch_range = base + (pitch2 * range)/2 - *pitch_base;
  1397. }
  1398. void SetPitch(int length, unsigned char *env, int pitch1, int pitch2)
  1399. {//==================================================================
  1400. // length in samples
  1401. #ifdef LOG_FRAMES
  1402. if(option_log_frames)
  1403. {
  1404. f_log=fopen("log-espeakedit","a");
  1405. if(f_log != NULL)
  1406. {
  1407. fprintf(f_log," pitch %3d %3d %3dmS\n",pitch1,pitch2,(length*1000)/samplerate);
  1408. fclose(f_log);
  1409. f_log=NULL;
  1410. }
  1411. }
  1412. #endif
  1413. if((wdata.pitch_env = env)==NULL)
  1414. wdata.pitch_env = env_fall; // default
  1415. wdata.pitch_ix = 0;
  1416. if(length==0)
  1417. wdata.pitch_inc = 0;
  1418. else
  1419. wdata.pitch_inc = (256 * ENV_LEN * STEPSIZE)/length;
  1420. SetPitch2(wvoice, pitch1, pitch2, &wdata.pitch_base, &wdata.pitch_range);
  1421. // set initial pitch
  1422. wdata.pitch = ((wdata.pitch_env[0] * wdata.pitch_range) >>8) + wdata.pitch_base; // Hz << 12
  1423. flutter_amp = wvoice->flutter;
  1424. } // end of SetPitch
  1425. void SetSynth(int length, int modn, frame_t *fr1, frame_t *fr2, voice_t *v)
  1426. {//========================================================================
  1427. int ix;
  1428. DOUBLEX next;
  1429. int length2;
  1430. int length4;
  1431. int qix;
  1432. int cmd;
  1433. static int glottal_reduce_tab1[4] = {0x30, 0x30, 0x40, 0x50}; // vowel before [?], amp * 1/256
  1434. // static int glottal_reduce_tab1[4] = {0x30, 0x40, 0x50, 0x60}; // vowel before [?], amp * 1/256
  1435. static int glottal_reduce_tab2[4] = {0x90, 0xa0, 0xb0, 0xc0}; // vowel after [?], amp * 1/256
  1436. #ifdef LOG_FRAMES
  1437. if(option_log_frames)
  1438. {
  1439. f_log=fopen("log-espeakedit","a");
  1440. if(f_log != NULL)
  1441. {
  1442. 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,
  1443. fr1->ffreq[0],fr1->ffreq[1],fr1->ffreq[2],fr1->ffreq[3], fr1->fheight[0],fr1->fheight[1],fr1->fheight[2],fr1->fheight[3],
  1444. fr2->ffreq[0],fr2->ffreq[1],fr2->ffreq[2],fr2->ffreq[3], fr2->fheight[0],fr2->fheight[1],fr2->fheight[2],fr2->fheight[3] );
  1445. fclose(f_log);
  1446. f_log=NULL;
  1447. }
  1448. }
  1449. #endif
  1450. harm_sqrt_n = 0;
  1451. end_wave = 1;
  1452. // any additional information in the param1 ?
  1453. modulation_type = modn & 0xff;
  1454. glottal_flag = 0;
  1455. if(modn & 0x400)
  1456. {
  1457. glottal_flag = 3; // before a glottal stop
  1458. glottal_reduce = glottal_reduce_tab1[(modn >> 8) & 3];
  1459. }
  1460. if(modn & 0x800)
  1461. {
  1462. glottal_flag = 4; // after a glottal stop
  1463. glottal_reduce = glottal_reduce_tab2[(modn >> 8) & 3];
  1464. }
  1465. for(qix=wcmdq_head+1;;qix++)
  1466. {
  1467. if(qix >= N_WCMDQ) qix = 0;
  1468. if(qix == wcmdq_tail) break;
  1469. cmd = wcmdq[qix][0];
  1470. if(cmd==WCMD_SPECT)
  1471. {
  1472. end_wave = 0; // next wave generation is from another spectrum
  1473. break;
  1474. }
  1475. if((cmd==WCMD_WAVE) || (cmd==WCMD_PAUSE))
  1476. break; // next is not from spectrum, so continue until end of wave cycle
  1477. }
  1478. // round the length to a multiple of the stepsize
  1479. length2 = (length + STEPSIZE/2) & ~0x3f;
  1480. if(length2 == 0)
  1481. length2 = STEPSIZE;
  1482. // add this length to any left over from the previous synth
  1483. samplecount_start = samplecount;
  1484. nsamples += length2;
  1485. length4 = length2/4;
  1486. peaks[7].freq = (7800 * v->freq[7] + v->freqadd[7]*256) << 8;
  1487. peaks[8].freq = (9000 * v->freq[8] + v->freqadd[8]*256) << 8;
  1488. for(ix=0; ix < 8; ix++)
  1489. {
  1490. if(ix < 7)
  1491. {
  1492. peaks[ix].freq1 = (fr1->ffreq[ix] * v->freq[ix] + v->freqadd[ix]*256) << 8;
  1493. peaks[ix].freq = int(peaks[ix].freq1);
  1494. next = (fr2->ffreq[ix] * v->freq[ix] + v->freqadd[ix]*256) << 8;
  1495. peaks[ix].freq_inc = ((next - peaks[ix].freq1) * (STEPSIZE/4)) / length4; // lower headroom for fixed point math
  1496. }
  1497. peaks[ix].height1 = (fr1->fheight[ix] * v->height[ix]) << 6;
  1498. peaks[ix].height = int(peaks[ix].height1);
  1499. next = (fr2->fheight[ix] * v->height[ix]) << 6;
  1500. peaks[ix].height_inc = ((next - peaks[ix].height1) * STEPSIZE) / length2;
  1501. if((ix <= 5) && (ix <= wvoice->n_harmonic_peaks))
  1502. {
  1503. peaks[ix].left1 = (fr1->fwidth[ix] * v->width[ix]) << 10;
  1504. peaks[ix].left = int(peaks[ix].left1);
  1505. next = (fr2->fwidth[ix] * v->width[ix]) << 10;
  1506. peaks[ix].left_inc = ((next - peaks[ix].left1) * STEPSIZE) / length2;
  1507. if(ix < 3)
  1508. {
  1509. peaks[ix].right1 = (fr1->fright[ix] * v->width[ix]) << 10;
  1510. peaks[ix].right = int(peaks[ix].right1);
  1511. next = (fr2->fright[ix] * v->width[ix]) << 10;
  1512. peaks[ix].right_inc = ((next - peaks[ix].right1) * STEPSIZE) / length2;
  1513. }
  1514. else
  1515. {
  1516. peaks[ix].right = peaks[ix].left;
  1517. }
  1518. }
  1519. }
  1520. } // end of SetSynth
  1521. static int Wavegen2(int length, int modulation, int resume, frame_t *fr1, frame_t *fr2)
  1522. {//====================================================================================
  1523. if(resume==0)
  1524. SetSynth(length, modulation, fr1, fr2, wvoice);
  1525. return(Wavegen());
  1526. }
  1527. void Write4Bytes(FILE *f, int value)
  1528. {//=================================
  1529. // Write 4 bytes to a file, least significant first
  1530. int ix;
  1531. for(ix=0; ix<4; ix++)
  1532. {
  1533. fputc(value & 0xff,f);
  1534. value = value >> 8;
  1535. }
  1536. }
  1537. int WavegenFill2(int fill_zeros)
  1538. {//============================
  1539. // Pick up next wavegen commands from the queue
  1540. // return: 0 output buffer has been filled
  1541. // return: 1 input command queue is now empty
  1542. long64 *q;
  1543. int length;
  1544. int result;
  1545. int marker_type;
  1546. static int resume=0;
  1547. static int echo_complete=0;
  1548. while(out_ptr < out_end)
  1549. {
  1550. if(WcmdqUsed() <= 0)
  1551. {
  1552. if(echo_complete > 0)
  1553. {
  1554. // continue to play silence until echo is completed
  1555. resume = PlaySilence(echo_complete,resume);
  1556. if(resume == 1)
  1557. return(0); // not yet finished
  1558. }
  1559. if(fill_zeros)
  1560. {
  1561. while(out_ptr < out_end)
  1562. *out_ptr++ = 0;
  1563. }
  1564. return(1); // queue empty, close sound channel
  1565. }
  1566. result = 0;
  1567. q = wcmdq[wcmdq_head];
  1568. length = q[1];
  1569. switch(q[0] & 0xff)
  1570. {
  1571. case WCMD_PITCH:
  1572. SetPitch(length,(unsigned char *)q[2],q[3] >> 16,q[3] & 0xffff);
  1573. break;
  1574. case WCMD_PAUSE:
  1575. if(resume==0)
  1576. {
  1577. echo_complete -= length;
  1578. }
  1579. wdata.n_mix_wavefile = 0;
  1580. wdata.amplitude_fmt = 100;
  1581. #ifdef INCLUDE_KLATT
  1582. KlattReset(1);
  1583. #endif
  1584. result = PlaySilence(length,resume);
  1585. break;
  1586. case WCMD_WAVE:
  1587. echo_complete = echo_length;
  1588. wdata.n_mix_wavefile = 0;
  1589. #ifdef INCLUDE_KLATT
  1590. KlattReset(1);
  1591. #endif
  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. #ifdef INCLUDE_SONIC
  1652. case WCMD_SONIC_SPEED:
  1653. sonicSpeed = (double)q[1] / 1024;
  1654. break;
  1655. #endif
  1656. }
  1657. if(result==0)
  1658. {
  1659. WcmdqIncHead();
  1660. resume=0;
  1661. }
  1662. else
  1663. {
  1664. resume=1;
  1665. }
  1666. }
  1667. return(0);
  1668. } // end of WavegenFill2
  1669. #ifdef INCLUDE_SONIC
  1670. /* Speed up the audio samples with libsonic. */
  1671. static int SpeedUp(short *outbuf, int length_in, int length_out, int end_of_text)
  1672. {//==============================================================================
  1673. if(length_in >0)
  1674. {
  1675. if(sonicSpeedupStream == NULL)
  1676. {
  1677. sonicSpeedupStream = sonicCreateStream(22050, 1);
  1678. }
  1679. if(sonicGetSpeed(sonicSpeedupStream) != sonicSpeed)
  1680. {
  1681. sonicSetSpeed(sonicSpeedupStream, sonicSpeed);
  1682. }
  1683. sonicWriteShortToStream(sonicSpeedupStream, outbuf, length_in);
  1684. }
  1685. if(sonicSpeedupStream == NULL)
  1686. return(0);
  1687. if(end_of_text)
  1688. {
  1689. sonicFlushStream(sonicSpeedupStream);
  1690. }
  1691. return sonicReadShortFromStream(sonicSpeedupStream, outbuf, length_out);
  1692. } // end of SpeedUp
  1693. #endif
  1694. /* Call WavegenFill2, and then speed up the output samples. */
  1695. int WavegenFill(int fill_zeros)
  1696. {//============================
  1697. int finished;
  1698. unsigned char *p_start;
  1699. p_start = out_ptr;
  1700. // fill_zeros is ignored. It is now done in the portaudio callback
  1701. finished = WavegenFill2(0);
  1702. #ifdef INCLUDE_SONIC
  1703. if(sonicSpeed > 1.0)
  1704. {
  1705. int length;
  1706. int max_length;
  1707. max_length = (out_end - p_start);
  1708. length = 2*SpeedUp((short *)p_start, (out_ptr-p_start)/2, max_length/2, finished);
  1709. out_ptr = p_start + length;
  1710. if(length >= max_length)
  1711. finished = 0; // there may be more data to flush
  1712. }
  1713. #endif
  1714. return finished;
  1715. } // end of WavegenFill