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

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