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.

compiledata.c 68KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2013-2017 Reece H. Dunn
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #include <stdarg.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/stat.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #include <espeak-ng/espeak_ng.h>
  32. #include <espeak-ng/speak_lib.h>
  33. #include "error.h"
  34. #include "speech.h"
  35. #include "phoneme.h"
  36. #include "synthesize.h"
  37. #include "voice.h"
  38. #include "spect.h"
  39. typedef struct {
  40. unsigned int value;
  41. char *name;
  42. } NAMETAB;
  43. NAMETAB *manifest = NULL;
  44. int n_manifest;
  45. char phsrc[sizeof(path_home)+40]; // Source: path to the 'phonemes' source file.
  46. extern ESPEAK_NG_API int utf8_in(int *c, const char *buf);
  47. extern int utf8_out(unsigned int c, char *buf);
  48. typedef struct {
  49. const char *mnem;
  50. int type;
  51. int data;
  52. } keywtab_t;
  53. #define k_AND 1
  54. #define k_OR 2
  55. #define k_THEN 3
  56. #define k_NOT 4
  57. #define kTHISSTRESS 0x800
  58. // keyword types
  59. enum {
  60. tPHONEME_TYPE = 1,
  61. tPHONEME_FLAG,
  62. tTRANSITION,
  63. tSTATEMENT,
  64. tINSTRN1,
  65. tWHICH_PHONEME,
  66. tTEST,
  67. };
  68. static keywtab_t k_conditions[] = {
  69. { "AND", 0, k_AND },
  70. { "OR", 0, k_OR },
  71. { "THEN", 0, k_THEN },
  72. { "NOT", 0, k_NOT },
  73. { "prevPh", tWHICH_PHONEME, 0 },
  74. { "thisPh", tWHICH_PHONEME, 1 },
  75. { "nextPh", tWHICH_PHONEME, 2 },
  76. { "next2Ph", tWHICH_PHONEME, 3 },
  77. { "nextPhW", tWHICH_PHONEME, 4 },
  78. { "prevPhW", tWHICH_PHONEME, 5 },
  79. { "next2PhW", tWHICH_PHONEME, 6 },
  80. { "nextVowel", tWHICH_PHONEME, 7 },
  81. { "prevVowel", tWHICH_PHONEME, 8 },
  82. { "next3PhW", tWHICH_PHONEME, 9 },
  83. { "prev2PhW", tWHICH_PHONEME, 10 },
  84. { "PreVoicing", tTEST, 0xf01 },
  85. { "KlattSynth", tTEST, 0xf02 },
  86. { "MbrolaSynth", tTEST, 0xf03 },
  87. { NULL, 0, 0 }
  88. };
  89. static keywtab_t k_properties[] = {
  90. { "isPause", 0, CONDITION_IS_PHONEME_TYPE | phPAUSE },
  91. { "isVowel", 0, CONDITION_IS_PHONEME_TYPE | phVOWEL },
  92. { "isNasal", 0, CONDITION_IS_PHONEME_TYPE | phNASAL },
  93. { "isLiquid", 0, CONDITION_IS_PHONEME_TYPE | phLIQUID },
  94. { "isUStop", 0, CONDITION_IS_PHONEME_TYPE | phSTOP },
  95. { "isVStop", 0, CONDITION_IS_PHONEME_TYPE | phVSTOP },
  96. { "isVFricative", 0, CONDITION_IS_PHONEME_TYPE | phVFRICATIVE },
  97. { "isPalatal", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_PALATAL },
  98. { "isLong", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_LONG },
  99. { "isRhotic", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_RHOTIC },
  100. { "isSibilant", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_SIBILANT },
  101. { "isFlag1", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_FLAG1 },
  102. { "isFlag2", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_FLAG2 },
  103. { "isVel", 0, CONDITION_IS_PLACE_OF_ARTICULATION | phPLACE_VELAR },
  104. { "isDiminished", 0, CONDITION_IS_OTHER | isDiminished },
  105. { "isUnstressed", 0, CONDITION_IS_OTHER | isUnstressed },
  106. { "isNotStressed", 0, CONDITION_IS_OTHER | isNotStressed },
  107. { "isStressed", 0, CONDITION_IS_OTHER | isStressed },
  108. { "isMaxStress", 0, CONDITION_IS_OTHER | isMaxStress },
  109. { "isPause2", 0, CONDITION_IS_OTHER | isBreak },
  110. { "isWordStart", 0, CONDITION_IS_OTHER | isWordStart },
  111. { "isWordEnd", 0, CONDITION_IS_OTHER | isWordEnd },
  112. { "isAfterStress", 0, CONDITION_IS_OTHER | isAfterStress },
  113. { "isNotVowel", 0, CONDITION_IS_OTHER | isNotVowel },
  114. { "isFinalVowel", 0, CONDITION_IS_OTHER | isFinalVowel },
  115. { "isVoiced", 0, CONDITION_IS_OTHER | isVoiced },
  116. { "isFirstVowel", 0, CONDITION_IS_OTHER | isFirstVowel },
  117. { "isSecondVowel", 0, CONDITION_IS_OTHER | isSecondVowel },
  118. { "isTranslationGiven", 0, CONDITION_IS_OTHER | isTranslationGiven },
  119. { NULL, 0, 0 }
  120. };
  121. enum {
  122. kPHONEMESTART = 1,
  123. kUTF8_BOM,
  124. kPROCEDURE,
  125. kENDPHONEME,
  126. kENDPROCEDURE,
  127. kPHONEMETABLE,
  128. kINCLUDE,
  129. kIMPORT_PH,
  130. kSTARTTYPE,
  131. kENDTYPE,
  132. kSTRESSTYPE,
  133. kVOICINGSWITCH,
  134. kIF,
  135. kELSE,
  136. kELIF,
  137. kENDIF,
  138. kCALLPH,
  139. kSWITCH_PREVVOWEL,
  140. kSWITCH_NEXTVOWEL,
  141. kENDSWITCH,
  142. kFMT,
  143. kWAV,
  144. kVOWELSTART,
  145. kVOWELENDING,
  146. kANDWAV,
  147. kVOWELIN,
  148. kVOWELOUT,
  149. kTONESPEC,
  150. kRETURN,
  151. kCONTINUE,
  152. };
  153. enum {
  154. kTUNE = 1,
  155. kENDTUNE,
  156. kTUNE_PREHEAD,
  157. kTUNE_ONSET,
  158. kTUNE_HEAD,
  159. kTUNE_HEADENV,
  160. kTUNE_HEADEXTEND,
  161. kTUNE_HEADLAST,
  162. kTUNE_NUCLEUS0,
  163. kTUNE_NUCLEUS1,
  164. kTUNE_SPLIT,
  165. };
  166. static unsigned const char utf8_bom[] = { 0xef, 0xbb, 0xbf, 0 };
  167. static keywtab_t k_intonation[] = {
  168. { "tune", 0, kTUNE },
  169. { "endtune", 0, kENDTUNE },
  170. { "prehead", 0, kTUNE_PREHEAD },
  171. { "onset", 0, kTUNE_ONSET },
  172. { "head", 0, kTUNE_HEAD },
  173. { "headenv", 0, kTUNE_HEADENV },
  174. { "headextend", 0, kTUNE_HEADEXTEND },
  175. { "headlast", 0, kTUNE_HEADLAST },
  176. { "nucleus0", 0, kTUNE_NUCLEUS0 },
  177. { "nucleus", 0, kTUNE_NUCLEUS1 },
  178. { "split", 0, kTUNE_SPLIT },
  179. { NULL, 0, -1 }
  180. };
  181. static keywtab_t keywords[] = {
  182. { "liquid", tPHONEME_TYPE, phLIQUID },
  183. { "pause", tPHONEME_TYPE, phPAUSE },
  184. { "stress", tPHONEME_TYPE, phSTRESS },
  185. { "virtual", tPHONEME_TYPE, phVIRTUAL },
  186. { "delete_phoneme", tPHONEME_TYPE, phDELETED },
  187. // keywords
  188. { "phonemetable", tSTATEMENT, kPHONEMETABLE },
  189. { "include", tSTATEMENT, kINCLUDE },
  190. { (const char *)utf8_bom, tSTATEMENT, kUTF8_BOM },
  191. { "phoneme", tSTATEMENT, kPHONEMESTART },
  192. { "procedure", tSTATEMENT, kPROCEDURE },
  193. { "endphoneme", tSTATEMENT, kENDPHONEME },
  194. { "endprocedure", tSTATEMENT, kENDPROCEDURE },
  195. { "import_phoneme", tSTATEMENT, kIMPORT_PH },
  196. { "stress_type", tSTATEMENT, kSTRESSTYPE },
  197. { "starttype", tSTATEMENT, kSTARTTYPE },
  198. { "endtype", tSTATEMENT, kENDTYPE },
  199. { "voicingswitch", tSTATEMENT, kVOICINGSWITCH },
  200. { "IF", tSTATEMENT, kIF },
  201. { "ELSE", tSTATEMENT, kELSE },
  202. { "ELIF", tSTATEMENT, kELIF },
  203. { "ELSEIF", tSTATEMENT, kELIF }, // same as ELIF
  204. { "ENDIF", tSTATEMENT, kENDIF },
  205. { "CALL", tSTATEMENT, kCALLPH },
  206. { "RETURN", tSTATEMENT, kRETURN },
  207. { "PrevVowelEndings", tSTATEMENT, kSWITCH_PREVVOWEL },
  208. { "NextVowelStarts", tSTATEMENT, kSWITCH_NEXTVOWEL },
  209. { "EndSwitch", tSTATEMENT, kENDSWITCH },
  210. { "Tone", tSTATEMENT, kTONESPEC },
  211. { "FMT", tSTATEMENT, kFMT },
  212. { "WAV", tSTATEMENT, kWAV },
  213. { "VowelStart", tSTATEMENT, kVOWELSTART },
  214. { "VowelEnding", tSTATEMENT, kVOWELENDING },
  215. { "addWav", tSTATEMENT, kANDWAV },
  216. { "Vowelin", tSTATEMENT, kVOWELIN },
  217. { "Vowelout", tSTATEMENT, kVOWELOUT },
  218. { "Continue", tSTATEMENT, kCONTINUE },
  219. { "ChangePhoneme", tINSTRN1, i_CHANGE_PHONEME },
  220. { "ChangeNextPhoneme", tINSTRN1, i_REPLACE_NEXT_PHONEME },
  221. { "InsertPhoneme", tINSTRN1, i_INSERT_PHONEME },
  222. { "AppendPhoneme", tINSTRN1, i_APPEND_PHONEME },
  223. { "IfNextVowelAppend", tINSTRN1, i_APPEND_IFNEXTVOWEL },
  224. { "ChangeIfDiminished", tINSTRN1, i_CHANGE_IF | isDiminished },
  225. { "ChangeIfUnstressed", tINSTRN1, i_CHANGE_IF | isUnstressed },
  226. { "ChangeIfNotStressed", tINSTRN1, i_CHANGE_IF | isNotStressed },
  227. { "ChangeIfStressed", tINSTRN1, i_CHANGE_IF | isStressed },
  228. { "PauseBefore", tINSTRN1, i_PAUSE_BEFORE },
  229. { "PauseAfter", tINSTRN1, i_PAUSE_AFTER },
  230. { "length", tINSTRN1, i_SET_LENGTH },
  231. { "LongLength", tINSTRN1, i_LONG_LENGTH },
  232. { "LengthAdd", tINSTRN1, i_ADD_LENGTH },
  233. { "lengthmod", tINSTRN1, i_LENGTH_MOD },
  234. { "ipa", tINSTRN1, i_IPA_NAME },
  235. // flags
  236. { "unstressed", tPHONEME_FLAG, phUNSTRESSED },
  237. { "nolink", tPHONEME_FLAG, phNOLINK },
  238. { "brkafter", tPHONEME_FLAG, phBRKAFTER },
  239. { "rhotic", tPHONEME_FLAG, phRHOTIC },
  240. { "lengthenstop", tPHONEME_FLAG, phLENGTHENSTOP },
  241. { "nopause", tPHONEME_FLAG, phNOPAUSE },
  242. { "prevoice", tPHONEME_FLAG, phPREVOICE },
  243. { "flag1", tPHONEME_FLAG, phFLAG1 },
  244. { "flag2", tPHONEME_FLAG, phFLAG2 },
  245. // vowel transition attributes
  246. { "len=", tTRANSITION, 1 },
  247. { "rms=", tTRANSITION, 2 },
  248. { "f1=", tTRANSITION, 3 },
  249. { "f2=", tTRANSITION, 4 },
  250. { "f3=", tTRANSITION, 5 },
  251. { "brk", tTRANSITION, 6 },
  252. { "rate", tTRANSITION, 7 },
  253. { "glstop", tTRANSITION, 8 },
  254. { "lenadd", tTRANSITION, 9 },
  255. { "f4", tTRANSITION, 10 },
  256. { "gpaus", tTRANSITION, 11 },
  257. { "colr=", tTRANSITION, 12 },
  258. { "amp=", tTRANSITION, 13 }, // set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
  259. { NULL, 0, -1 }
  260. };
  261. static keywtab_t *keyword_tabs[] = {
  262. keywords, k_conditions, k_properties, k_intonation
  263. };
  264. static PHONEME_TAB *phoneme_out;
  265. static int n_phcodes_list[N_PHONEME_TABS];
  266. static PHONEME_TAB_LIST phoneme_tab_list2[N_PHONEME_TABS];
  267. static PHONEME_TAB *phoneme_tab2;
  268. #define N_PROCS 50
  269. int n_procs;
  270. int proc_addr[N_PROCS];
  271. char proc_names[40][N_PROCS];
  272. #define MAX_PROG_BUF 2000
  273. USHORT *prog_out;
  274. USHORT *prog_out_max;
  275. USHORT prog_buf[MAX_PROG_BUF+20];
  276. static espeak_ng_STATUS ReadPhondataManifest(espeak_ng_ERROR_CONTEXT *context)
  277. {
  278. // Read the phondata-manifest file
  279. FILE *f;
  280. int n_lines = 0;
  281. int ix;
  282. char *p;
  283. unsigned int value;
  284. char buf[sizeof(path_home)+40];
  285. char name[120];
  286. sprintf(buf, "%s%c%s", path_home, PATHSEP, "phondata-manifest");
  287. if ((f = fopen(buf, "r")) == NULL)
  288. return create_file_error_context(context, errno, buf);
  289. while (fgets(buf, sizeof(buf), f) != NULL)
  290. n_lines++;
  291. rewind(f);
  292. if (manifest != NULL) {
  293. for (ix = 0; ix < n_manifest; ix++)
  294. free(manifest[ix].name);
  295. }
  296. if (n_lines == 0) {
  297. fclose(f);
  298. return ENS_EMPTY_PHONEME_MANIFEST;
  299. }
  300. NAMETAB *new_manifest = (NAMETAB *)realloc(manifest, n_lines * sizeof(NAMETAB));
  301. if (new_manifest == NULL) {
  302. fclose(f);
  303. free(manifest);
  304. return ENOMEM;
  305. } else
  306. manifest = new_manifest;
  307. n_manifest = 0;
  308. while (fgets(buf, sizeof(buf), f) != NULL) {
  309. if (!isalpha(buf[0]))
  310. continue;
  311. if (sscanf(&buf[2], "%x %s", &value, name) == 2) {
  312. if ((p = (char *)malloc(strlen(name)+1)) != NULL) {
  313. strcpy(p, name);
  314. manifest[n_manifest].value = value;
  315. manifest[n_manifest].name = p;
  316. n_manifest++;
  317. }
  318. }
  319. }
  320. fclose(f);
  321. return ENS_OK;
  322. }
  323. static int n_phoneme_tabs;
  324. static int n_phcodes;
  325. // outout files
  326. static FILE *f_phdata;
  327. static FILE *f_phindex;
  328. static FILE *f_phtab;
  329. static FILE *f_phcontents;
  330. static FILE *f_errors = NULL;
  331. static FILE *f_prog_log = NULL;
  332. static FILE *f_report;
  333. static FILE *f_in;
  334. static int f_in_linenum;
  335. static int f_in_displ;
  336. static int linenum;
  337. static int count_references = 0;
  338. static int duplicate_references = 0;
  339. static int count_frames = 0;
  340. static int error_count = 0;
  341. static int resample_count = 0;
  342. static int resample_fails = 0;
  343. static int then_count = 0;
  344. static int after_if = 0;
  345. static char current_fname[80];
  346. static int markers_used[8];
  347. typedef struct {
  348. void *link;
  349. int value;
  350. int ph_mnemonic;
  351. short ph_table;
  352. char string[1];
  353. } REF_HASH_TAB;
  354. static REF_HASH_TAB *ref_hash_tab[256];
  355. #define N_ENVELOPES 30
  356. int n_envelopes = 0;
  357. char envelope_paths[N_ENVELOPES][80];
  358. unsigned char envelope_dat[N_ENVELOPES][ENV_LEN];
  359. typedef struct {
  360. FILE *file;
  361. int linenum;
  362. char fname[80];
  363. } STACK;
  364. #define N_STACK 12
  365. int stack_ix;
  366. STACK stack[N_STACK];
  367. #define N_IF_STACK 12
  368. int if_level;
  369. typedef struct {
  370. USHORT *p_then;
  371. USHORT *p_else;
  372. int returned;
  373. } IF_STACK;
  374. IF_STACK if_stack[N_IF_STACK];
  375. enum {
  376. tENDFILE = 1,
  377. tSTRING,
  378. tNUMBER,
  379. tSIGNEDNUMBER,
  380. tPHONEMEMNEM,
  381. tOPENBRACKET,
  382. tKEYWORD,
  383. tCONDITION,
  384. tPROPERTIES,
  385. tINTONATION,
  386. };
  387. int item_type;
  388. int item_terminator;
  389. #define N_ITEM_STRING 256
  390. char item_string[N_ITEM_STRING];
  391. static int ref_sorter(char **a, char **b)
  392. {
  393. int ix;
  394. REF_HASH_TAB *p1 = (REF_HASH_TAB *)(*a);
  395. REF_HASH_TAB *p2 = (REF_HASH_TAB *)(*b);
  396. ix = strcoll(p1->string, p2->string);
  397. if (ix != 0)
  398. return ix;
  399. ix = p1->ph_table - p2->ph_table;
  400. if (ix != 0)
  401. return ix;
  402. return p1->ph_mnemonic - p2->ph_mnemonic;
  403. }
  404. static void CompileReport(void)
  405. {
  406. int ix;
  407. int hash;
  408. int n;
  409. REF_HASH_TAB *p;
  410. REF_HASH_TAB **list;
  411. const char *data_path;
  412. int prev_table;
  413. int procedure_num;
  414. int prev_mnemonic;
  415. if (f_report == NULL)
  416. return;
  417. // make a list of all the references and sort it
  418. list = (REF_HASH_TAB **)malloc((count_references)* sizeof(REF_HASH_TAB *));
  419. if (list == NULL)
  420. return;
  421. fprintf(f_report, "\n%d phoneme tables\n", n_phoneme_tabs);
  422. fprintf(f_report, " new total\n");
  423. for (ix = 0; ix < n_phoneme_tabs; ix++)
  424. fprintf(f_report, "%8s %3d %4d\n", phoneme_tab_list2[ix].name, phoneme_tab_list2[ix].n_phonemes, n_phcodes_list[ix]+1);
  425. fputc('\n', f_report);
  426. fprintf(f_report, "Data file Used by\n");
  427. ix = 0;
  428. for (hash = 0; (hash < 256) && (ix < count_references); hash++) {
  429. p = ref_hash_tab[hash];
  430. while (p != NULL) {
  431. list[ix++] = p;
  432. p = (REF_HASH_TAB *)(p->link);
  433. }
  434. }
  435. n = ix;
  436. qsort((void *)list, n, sizeof(REF_HASH_TAB *), (int (*)(const void *, const void *))ref_sorter);
  437. data_path = "";
  438. prev_mnemonic = 0;
  439. prev_table = 0;
  440. for (ix = 0; ix < n; ix++) {
  441. int j = 0;
  442. if (strcmp(list[ix]->string, data_path) != 0) {
  443. data_path = list[ix]->string;
  444. j = strlen(data_path);
  445. fprintf(f_report, "%s", data_path);
  446. } else if ((list[ix]->ph_table == prev_table) && (list[ix]->ph_mnemonic == prev_mnemonic))
  447. continue; // same phoneme, don't list twice
  448. while (j < 14) {
  449. fputc(' ', f_report); // pad filename with spaces
  450. j++;
  451. }
  452. prev_mnemonic = list[ix]->ph_mnemonic;
  453. if ((prev_mnemonic >> 24) == 'P') {
  454. // a procedure, not a phoneme
  455. procedure_num = atoi(WordToString(prev_mnemonic));
  456. fprintf(f_report, " %s %s", phoneme_tab_list2[prev_table = list[ix]->ph_table].name, proc_names[procedure_num]);
  457. } else
  458. fprintf(f_report, " [%s] %s", WordToString(prev_mnemonic), phoneme_tab_list2[prev_table = list[ix]->ph_table].name);
  459. fputc('\n', f_report);
  460. }
  461. for (ix = 0; ix < n; ix++) {
  462. free(list[ix]);
  463. list[ix] = NULL;
  464. }
  465. free(list);
  466. list = NULL;
  467. }
  468. static void error(const char *format, ...)
  469. {
  470. va_list args;
  471. va_start(args, format);
  472. fprintf(f_errors, "%s(%d): ", current_fname, linenum-1);
  473. vfprintf(f_errors, format, args);
  474. fprintf(f_errors, "\n");
  475. error_count++;
  476. va_end(args);
  477. }
  478. static void error_from_status(espeak_ng_STATUS status, const char *context)
  479. {
  480. char message[512];
  481. espeak_ng_GetStatusCodeMessage(status, message, sizeof(message));
  482. if (context)
  483. error("%s: '%s'.", message, context);
  484. else
  485. error("%s.", message);
  486. }
  487. static unsigned int StringToWord(const char *string)
  488. {
  489. // Pack 4 characters into a word
  490. int ix;
  491. unsigned char c;
  492. unsigned int word;
  493. if (string == NULL)
  494. return 0;
  495. word = 0;
  496. for (ix = 0; ix < 4; ix++) {
  497. if (string[ix] == 0) break;
  498. c = string[ix];
  499. word |= (c << (ix*8));
  500. }
  501. return word;
  502. }
  503. static MNEM_TAB reserved_phonemes[] = {
  504. { "_\001", phonCONTROL }, // NOT USED
  505. { "%", phonSTRESS_U },
  506. { "%%", phonSTRESS_D },
  507. { ",", phonSTRESS_2 },
  508. { ",,", phonSTRESS_3 },
  509. { "'", phonSTRESS_P },
  510. { "''", phonSTRESS_P2 },
  511. { "=", phonSTRESS_PREV }, // stress previous syllable
  512. { "_:", phonPAUSE }, // pause
  513. { "_", phonPAUSE_SHORT }, // short pause
  514. { "_!", phonPAUSE_NOLINK }, // short pause, no link
  515. { ":", phonLENGTHEN },
  516. { "@", phonSCHWA },
  517. { "@-", phonSCHWA_SHORT },
  518. { "||", phonEND_WORD },
  519. { "1", phonDEFAULTTONE }, // (numeral 1) default tone (for tone language)
  520. { "#X1", phonCAPITAL }, // capital letter indication
  521. { "?", phonGLOTTALSTOP }, // glottal stop
  522. { "-", phonSYLLABIC }, // syllabic consonant
  523. { "_^_", phonSWITCH }, // Change language
  524. { "_X1", phonX1 }, // a language specific action
  525. { "_|", phonPAUSE_VSHORT }, // very short pause
  526. { "_::", phonPAUSE_LONG }, // long pause
  527. { "t#", phonT_REDUCED }, // reduced version of [t]
  528. { "'!", phonSTRESS_TONIC }, // stress - emphasized
  529. { "_;_", phonPAUSE_CLAUSE }, // clause pause
  530. { "#@", phonVOWELTYPES }, // vowel type groups, these must be consecutive
  531. { "#a", phonVOWELTYPES+1 },
  532. { "#e", phonVOWELTYPES+2 },
  533. { "#i", phonVOWELTYPES+3 },
  534. { "#o", phonVOWELTYPES+4 },
  535. { "#u", phonVOWELTYPES+5 },
  536. { NULL, 0 }
  537. };
  538. static void ReservePhCodes()
  539. {
  540. // Reserve phoneme codes which have fixed numbers so that they can be
  541. // referred to from the program code.
  542. unsigned int word;
  543. MNEM_TAB *p;
  544. p = reserved_phonemes;
  545. while (p->mnem != NULL) {
  546. word = StringToWord(p->mnem);
  547. phoneme_tab2[p->value].mnemonic = word;
  548. phoneme_tab2[p->value].code = p->value;
  549. if (n_phcodes <= p->value)
  550. n_phcodes = p->value+1;
  551. p++;
  552. }
  553. }
  554. static int LookupPhoneme(const char *string, int control)
  555. {
  556. // control = 0 explicit declaration
  557. // control = 1 declare phoneme if not found
  558. // control = 2 start looking after control & stress phonemes
  559. int ix;
  560. int start;
  561. int use;
  562. unsigned int word;
  563. if (strcmp(string, "NULL") == 0)
  564. return 1;
  565. ix = strlen(string);
  566. if ((ix == 0) || (ix > 4))
  567. error("Bad phoneme name '%s'", string);
  568. word = StringToWord(string);
  569. // don't use phoneme number 0, reserved for string terminator
  570. start = 1;
  571. if (control == 2) {
  572. // don't look for control and stress phonemes (allows these characters to be
  573. // used for other purposes)
  574. start = 8;
  575. }
  576. use = 0;
  577. for (ix = start; ix < n_phcodes; ix++) {
  578. if (phoneme_tab2[ix].mnemonic == word)
  579. return ix;
  580. if ((use == 0) && (phoneme_tab2[ix].mnemonic == 0))
  581. use = ix;
  582. }
  583. if (use == 0) {
  584. if (control == 0)
  585. return -1;
  586. if (n_phcodes >= N_PHONEME_TAB-1)
  587. return -1; // phoneme table is full
  588. use = n_phcodes++;
  589. }
  590. // add this phoneme to the phoneme table
  591. phoneme_tab2[use].mnemonic = word;
  592. phoneme_tab2[use].type = phINVALID;
  593. phoneme_tab2[use].program = linenum; // for error report if the phoneme remains undeclared
  594. return use;
  595. }
  596. static unsigned int get_char()
  597. {
  598. unsigned int c;
  599. c = fgetc(f_in);
  600. if (c == '\n')
  601. linenum++;
  602. return c;
  603. }
  604. static void unget_char(unsigned int c)
  605. {
  606. ungetc(c, f_in);
  607. if (c == '\n')
  608. linenum--;
  609. }
  610. int CheckNextChar()
  611. {
  612. int c;
  613. while (((c = get_char()) == ' ') || (c == '\t'))
  614. ;
  615. unget_char(c);
  616. return c;
  617. }
  618. static int NextItem(int type)
  619. {
  620. int acc;
  621. unsigned char c = 0;
  622. unsigned char c2;
  623. int ix;
  624. int sign;
  625. char *p;
  626. keywtab_t *pk;
  627. item_type = -1;
  628. f_in_displ = ftell(f_in);
  629. f_in_linenum = linenum;
  630. while (!feof(f_in)) {
  631. c = get_char();
  632. if (c == '/') {
  633. if ((c2 = get_char()) == '/') {
  634. // comment, ignore to end of line
  635. while (!feof(f_in) && ((c = get_char()) != '\n'))
  636. ;
  637. } else
  638. unget_char(c2);
  639. }
  640. if (!isspace(c))
  641. break;
  642. }
  643. if (feof(f_in))
  644. return -2;
  645. if (c == '(') {
  646. if (type == tOPENBRACKET)
  647. return 1;
  648. return -1;
  649. }
  650. ix = 0;
  651. while (!feof(f_in) && !isspace(c) && (c != '(') && (c != ')') && (c != ',')) {
  652. if (c == '\\')
  653. c = get_char();
  654. item_string[ix++] = c;
  655. c = get_char();
  656. if (feof(f_in))
  657. break;
  658. if (item_string[ix-1] == '=')
  659. break;
  660. }
  661. item_string[ix] = 0;
  662. while (isspace(c))
  663. c = get_char();
  664. item_terminator = ' ';
  665. if ((c == ')') || (c == '(') || (c == ','))
  666. item_terminator = c;
  667. if ((c == ')') || (c == ','))
  668. c = ' ';
  669. if (!feof(f_in))
  670. unget_char(c);
  671. if (type == tSTRING)
  672. return 0;
  673. if ((type == tNUMBER) || (type == tSIGNEDNUMBER)) {
  674. acc = 0;
  675. sign = 1;
  676. p = item_string;
  677. if ((*p == '-') && (type == tSIGNEDNUMBER)) {
  678. sign = -1;
  679. p++;
  680. }
  681. if (!isdigit(*p)) {
  682. if ((type == tNUMBER) && (*p == '-'))
  683. error("Expected an unsigned number");
  684. else
  685. error("Expected a number");
  686. }
  687. while (isdigit(*p)) {
  688. acc *= 10;
  689. acc += (*p - '0');
  690. p++;
  691. }
  692. return acc * sign;
  693. }
  694. if ((type >= tKEYWORD) && (type <= tINTONATION)) {
  695. pk = keyword_tabs[type-tKEYWORD];
  696. while (pk->mnem != NULL) {
  697. if (strcmp(item_string, pk->mnem) == 0) {
  698. item_type = pk->type;
  699. return pk->data;
  700. }
  701. pk++;
  702. }
  703. item_type = -1;
  704. return -1; // keyword not found
  705. }
  706. if (type == tPHONEMEMNEM)
  707. return LookupPhoneme(item_string, 2);
  708. return -1;
  709. }
  710. static int NextItemMax(int max)
  711. {
  712. // Get a number, but restrict value to max
  713. int value;
  714. value = NextItem(tNUMBER);
  715. if (value > max) {
  716. error("Value %d is greater than maximum %d", value, max);
  717. value = max;
  718. }
  719. return value;
  720. }
  721. static int NextItemBrackets(int type, int control)
  722. {
  723. // Expect a parameter inside parantheses
  724. // control: bit 0 0= need (
  725. // bit 1 1= allow comma
  726. int value;
  727. if ((control & 1) == 0) {
  728. if (!NextItem(tOPENBRACKET))
  729. error("Expected '('");
  730. }
  731. value = NextItem(type);
  732. if ((control & 2) && (item_terminator == ','))
  733. return value;
  734. if (item_terminator != ')')
  735. error("Expected ')'");
  736. return value;
  737. }
  738. static void UngetItem()
  739. {
  740. fseek(f_in, f_in_displ, SEEK_SET);
  741. linenum = f_in_linenum;
  742. }
  743. static int Range(int value, int divide, int min, int max)
  744. {
  745. if (value < 0)
  746. value -= divide/2;
  747. else
  748. value += divide/2;
  749. value = value / divide;
  750. if (value > max)
  751. value = max;
  752. if (value < min)
  753. value = min;
  754. return value - min;
  755. }
  756. int CompileVowelTransition(int which)
  757. {
  758. // Compile a vowel transition
  759. int key;
  760. int len = 0;
  761. int rms = 0;
  762. int f1 = 0;
  763. int f2 = 0;
  764. int f2_min = 0;
  765. int f2_max = 0;
  766. int f3_adj = 0;
  767. int f3_amp = 0;
  768. int flags = 0;
  769. int vcolour = 0;
  770. int x;
  771. int instn = i_VOWELIN;
  772. int word1;
  773. int word2;
  774. if (which == 1) {
  775. len = 50 / 2; // defaults for transition into vowel
  776. rms = 25 / 2;
  777. if (phoneme_out->type == phSTOP) {
  778. len = 42 / 2; // defaults for transition into vowel
  779. rms = 30 / 2;
  780. }
  781. } else if (which == 2) {
  782. instn = i_VOWELOUT;
  783. len = 36 / 2; // defaults for transition out of vowel
  784. rms = 16 / 2;
  785. }
  786. for (;;) {
  787. key = NextItem(tKEYWORD);
  788. if (item_type != tTRANSITION) {
  789. UngetItem();
  790. break;
  791. }
  792. switch (key & 0xf)
  793. {
  794. case 1:
  795. len = Range(NextItem(tNUMBER), 2, 0, 63) & 0x3f;
  796. flags |= 1;
  797. break;
  798. case 2:
  799. rms = Range(NextItem(tNUMBER), 2, 0, 31) & 0x1f;
  800. flags |= 1;
  801. break;
  802. case 3:
  803. f1 = NextItem(tNUMBER);
  804. break;
  805. case 4:
  806. f2 = Range(NextItem(tNUMBER), 50, 0, 63) & 0x3f;
  807. f2_min = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  808. f2_max = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  809. if (f2_min > f2_max) {
  810. x = f2_min;
  811. f2_min = f2_max;
  812. f2_max = x;
  813. }
  814. break;
  815. case 5:
  816. f3_adj = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  817. f3_amp = Range(NextItem(tNUMBER), 8, 0, 15) & 0x1f;
  818. break;
  819. case 6:
  820. flags |= 2; // break
  821. break;
  822. case 7:
  823. flags |= 4; // rate
  824. break;
  825. case 8:
  826. flags |= 8; // glstop
  827. break;
  828. case 9:
  829. flags |= 16; // lenadd
  830. break;
  831. case 10:
  832. flags |= 32; // f4
  833. break;
  834. case 11:
  835. flags |= 64; // pause
  836. break;
  837. case 12:
  838. vcolour = NextItem(tNUMBER);
  839. break;
  840. case 13:
  841. // set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
  842. rms = (Range(NextItem(tNUMBER), 1, 0, 31) & 0x1f) | 0x20;
  843. flags |= 1;
  844. break;
  845. }
  846. }
  847. word1 = len + (rms << 6) + (flags << 12);
  848. word2 = f2 + (f2_min << 6) + (f2_max << 11) + (f3_adj << 16) + (f3_amp << 21) + (f1 << 26) + (vcolour << 29);
  849. prog_out[0] = instn + ((word1 >> 16) & 0xff);
  850. prog_out[1] = word1;
  851. prog_out[2] = word2 >> 16;
  852. prog_out[3] = word2;
  853. prog_out += 4;
  854. return 0;
  855. }
  856. espeak_ng_STATUS LoadSpect(const char *path, int control, int *addr)
  857. {
  858. SpectSeq *spectseq;
  859. int peak;
  860. int frame;
  861. int n_frames;
  862. int ix;
  863. int x, x2;
  864. int rms;
  865. float total;
  866. float pkheight;
  867. int marker1_set = 0;
  868. int frame_vowelbreak = 0;
  869. int klatt_flag = 0;
  870. SpectFrame *fr;
  871. frame_t *fr_out;
  872. char filename[sizeof(path_home)+20];
  873. SPECT_SEQ seq_out;
  874. SPECT_SEQK seqk_out;
  875. // create SpectSeq and import data
  876. spectseq = SpectSeqCreate();
  877. if (spectseq == NULL)
  878. return ENOMEM;
  879. snprintf(filename, sizeof(filename), "%s/%s", phsrc, path);
  880. espeak_ng_STATUS status = LoadSpectSeq(spectseq, filename);
  881. if (status != ENS_OK) {
  882. error("Bad vowel file: '%s'", path);
  883. SpectSeqDestroy(spectseq);
  884. return status;
  885. }
  886. // do we need additional klatt data ?
  887. for (frame = 0; frame < spectseq->numframes; frame++) {
  888. for (ix = 5; ix < N_KLATTP2; ix++) {
  889. if (spectseq->frames[frame]->klatt_param[ix] != 0)
  890. klatt_flag = FRFLAG_KLATT;
  891. }
  892. }
  893. *addr = ftell(f_phdata);
  894. seq_out.n_frames = 0;
  895. seq_out.sqflags = 0;
  896. seq_out.length_total = 0;
  897. total = 0;
  898. for (frame = 0; frame < spectseq->numframes; frame++) {
  899. if (spectseq->frames[frame]->keyframe) {
  900. if (seq_out.n_frames == 1)
  901. frame_vowelbreak = frame;
  902. if (spectseq->frames[frame]->markers & 0x2) {
  903. // marker 1 is set
  904. marker1_set = 1;
  905. }
  906. seq_out.n_frames++;
  907. if (frame > 0)
  908. total += spectseq->frames[frame-1]->length;
  909. }
  910. }
  911. seq_out.length_total = (int)total;
  912. if ((control & 1) && (marker1_set == 0)) {
  913. // This is a vowel, but no Vowel Break marker is set
  914. // set a marker flag for the second frame of a vowel
  915. spectseq->frames[frame_vowelbreak]->markers |= FRFLAG_VOWEL_CENTRE;
  916. }
  917. n_frames = 0;
  918. for (frame = 0; frame < spectseq->numframes; frame++) {
  919. fr = spectseq->frames[frame];
  920. if (fr->keyframe) {
  921. if (klatt_flag)
  922. fr_out = &seqk_out.frame[n_frames];
  923. else
  924. fr_out = (frame_t *)&seq_out.frame[n_frames];
  925. x = (int)(fr->length + 0.5); // round to nearest mS
  926. if (x > 255) x = 255;
  927. fr_out->length = x;
  928. fr_out->frflags = fr->markers | klatt_flag;
  929. rms = (int)GetFrameRms(fr, spectseq->amplitude);
  930. if (rms > 255) rms = 255;
  931. fr_out->rms = rms;
  932. if (n_frames == (seq_out.n_frames-1))
  933. fr_out->length = 0; // give last frame zero length
  934. // write: peak data
  935. count_frames++;
  936. for (peak = 0; peak < 8; peak++) {
  937. if (peak < 7)
  938. fr_out->ffreq[peak] = fr->peaks[peak].pkfreq;
  939. pkheight = spectseq->amplitude * fr->amp_adjust * fr->peaks[peak].pkheight;
  940. pkheight = pkheight/640000;
  941. if (pkheight > 255) pkheight = 255;
  942. fr_out->fheight[peak] = (int)pkheight;
  943. if (peak < 6) {
  944. x = fr->peaks[peak].pkwidth/4;
  945. if (x > 255) x = 255;
  946. fr_out->fwidth[peak] = x;
  947. if (peak < 3) {
  948. x2 = fr->peaks[peak].pkright/4;
  949. if (x2 > 255) x2 = 255;
  950. fr_out->fright[peak] = x2;
  951. }
  952. }
  953. if (peak < 4) {
  954. x = fr->peaks[peak].klt_bw / 2;
  955. if (x > 255) x = 255;
  956. fr_out->bw[peak] = x;
  957. }
  958. }
  959. for (ix = 0; ix < 5; ix++) {
  960. fr_out->klattp[ix] = fr->klatt_param[ix];
  961. fr_out->klattp[KLATT_FNZ] = fr->klatt_param[KLATT_FNZ] / 2;
  962. }
  963. if (klatt_flag) {
  964. // additional klatt parameters
  965. for (ix = 0; ix < 5; ix++)
  966. fr_out->klattp2[ix] = fr->klatt_param[ix+5];
  967. for (peak = 0; peak < 7; peak++) {
  968. fr_out->klatt_ap[peak] = fr->peaks[peak].klt_ap;
  969. x = fr->peaks[peak].klt_bp / 2;
  970. if (x > 255) x = 255;
  971. fr_out->klatt_bp[peak] = x;
  972. }
  973. fr_out->spare = 0;
  974. }
  975. if (fr_out->bw[1] == 0) {
  976. fr_out->bw[0] = 89 / 2;
  977. fr_out->bw[1] = 90 / 2;
  978. fr_out->bw[2] = 140 / 2;
  979. fr_out->bw[3] = 260 / 2;
  980. }
  981. n_frames++;
  982. }
  983. }
  984. if (klatt_flag) {
  985. seqk_out.n_frames = seq_out.n_frames;
  986. seqk_out.sqflags = seq_out.sqflags;
  987. seqk_out.length_total = seq_out.length_total;
  988. ix = (char *)(&seqk_out.frame[seqk_out.n_frames]) - (char *)(&seqk_out);
  989. fwrite(&seqk_out, ix, 1, f_phdata);
  990. while (ix & 3)
  991. {
  992. // round up to multiple of 4 bytes
  993. fputc(0, f_phdata);
  994. ix++;
  995. }
  996. } else {
  997. ix = (char *)(&seq_out.frame[seq_out.n_frames]) - (char *)(&seq_out);
  998. fwrite(&seq_out, ix, 1, f_phdata);
  999. while (ix & 3)
  1000. {
  1001. // round up to multiple of 4 bytes
  1002. fputc(0, f_phdata);
  1003. ix++;
  1004. }
  1005. }
  1006. SpectSeqDestroy(spectseq);
  1007. return ENS_OK;
  1008. }
  1009. static int LoadWavefile(FILE *f, const char *fname)
  1010. {
  1011. int displ;
  1012. unsigned char c1;
  1013. unsigned char c3;
  1014. int c2;
  1015. int sample;
  1016. int sample2;
  1017. float x;
  1018. int max = 0;
  1019. int length;
  1020. int sr1, sr2;
  1021. int failed;
  1022. int len;
  1023. int resample_wav = 0;
  1024. const char *fname2;
  1025. char fname_temp[100];
  1026. char msg[120];
  1027. int scale_factor = 0;
  1028. fseek(f, 24, SEEK_SET);
  1029. sr1 = Read4Bytes(f);
  1030. sr2 = Read4Bytes(f);
  1031. fseek(f, 40, SEEK_SET);
  1032. if ((sr1 != samplerate_native) || (sr2 != sr1*2)) {
  1033. int fd_temp;
  1034. char command[sizeof(path_home)+250];
  1035. failed = 0;
  1036. #ifdef HAVE_MKSTEMP
  1037. strcpy(fname_temp, "/tmp/espeakXXXXXX");
  1038. if ((fd_temp = mkstemp(fname_temp)) >= 0)
  1039. close(fd_temp);
  1040. #else
  1041. strcpy(fname_temp, tmpnam(NULL));
  1042. #endif
  1043. fname2 = fname;
  1044. len = strlen(fname);
  1045. if (strcmp(&fname[len-4], ".wav") == 0) {
  1046. strcpy(msg, fname);
  1047. msg[len-4] = 0;
  1048. fname2 = msg;
  1049. }
  1050. sprintf(command, "sox \"%s/%s.wav\" -r %d -c1 -t wav %s\n", phsrc, fname2, samplerate_native, fname_temp);
  1051. if (system(command) != 0)
  1052. failed = 1;
  1053. if (failed || (GetFileLength(fname_temp) <= 0)) {
  1054. if (resample_fails < 2)
  1055. error("Resample command failed: %s", command);
  1056. resample_fails++;
  1057. if (sr1 != samplerate_native)
  1058. error("Can't resample (%d to %d): %s", sr1, samplerate_native, fname);
  1059. else
  1060. error("WAV file is not mono: %s", fname);
  1061. remove(fname_temp);
  1062. return 0;
  1063. }
  1064. f = fopen(fname_temp, "rb");
  1065. if (f == NULL) {
  1066. error("Can't read temp file: %s", fname_temp);
  1067. return 0;
  1068. }
  1069. if (f_report != NULL)
  1070. fprintf(f_report, "resampled %s\n", fname);
  1071. resample_count++;
  1072. resample_wav = 1;
  1073. fseek(f, 40, SEEK_SET); // skip past the WAV header, up to before "data length"
  1074. }
  1075. displ = ftell(f_phdata);
  1076. // data contains: 4 bytes of length (n_samples * 2), followed by 2-byte samples (lsb byte first)
  1077. length = Read4Bytes(f);
  1078. while (true) {
  1079. int c;
  1080. if ((c = fgetc(f)) == EOF)
  1081. break;
  1082. c1 = (unsigned char)c;
  1083. if ((c = fgetc(f)) == EOF)
  1084. break;
  1085. c3 = (unsigned char)c;
  1086. c2 = c3 << 24;
  1087. c2 = c2 >> 16; // sign extend
  1088. sample = (c1 & 0xff) + c2;
  1089. if (sample > max)
  1090. max = sample;
  1091. else if (sample < -max)
  1092. max = -sample;
  1093. }
  1094. scale_factor = (max / 127) + 1;
  1095. #define MIN_FACTOR -1 // was 6, disable use of 16 bit samples
  1096. if (scale_factor > MIN_FACTOR) {
  1097. length = length/2 + (scale_factor << 16);
  1098. }
  1099. Write4Bytes(f_phdata, length);
  1100. fseek(f, 44, SEEK_SET);
  1101. while (!feof(f)) {
  1102. c1 = fgetc(f);
  1103. c3 = fgetc(f);
  1104. c2 = c3 << 24;
  1105. c2 = c2 >> 16; // sign extend
  1106. sample = (c1 & 0xff) + c2;
  1107. if (feof(f)) break;
  1108. if (scale_factor <= MIN_FACTOR) {
  1109. fputc(sample & 0xff, f_phdata);
  1110. fputc(sample >> 8, f_phdata);
  1111. } else {
  1112. x = ((float)sample / scale_factor) + 0.5;
  1113. sample2 = (int)x;
  1114. if (sample2 > 127)
  1115. sample2 = 127;
  1116. if (sample2 < -128)
  1117. sample2 = -128;
  1118. fputc(sample2, f_phdata);
  1119. }
  1120. }
  1121. length = ftell(f_phdata);
  1122. while ((length & 3) != 0) {
  1123. // pad to a multiple of 4 bytes
  1124. fputc(0, f_phdata);
  1125. length++;
  1126. }
  1127. if (resample_wav != 0) {
  1128. fclose(f);
  1129. remove(fname_temp);
  1130. }
  1131. return displ | 0x800000; // set bit 23 to indicate a wave file rather than a spectrum
  1132. }
  1133. static espeak_ng_STATUS LoadEnvelope(FILE *f, const char *fname, int *displ)
  1134. {
  1135. char buf[128];
  1136. if (displ)
  1137. *displ = ftell(f_phdata);
  1138. if (fseek(f, 12, SEEK_SET) == -1)
  1139. return errno;
  1140. if (fread(buf, 128, 1, f) != 128)
  1141. return errno;
  1142. fwrite(buf, 128, 1, f_phdata);
  1143. if (n_envelopes < N_ENVELOPES) {
  1144. strncpy0(envelope_paths[n_envelopes], fname, sizeof(envelope_paths[0]));
  1145. memcpy(envelope_dat[n_envelopes], buf, sizeof(envelope_dat[0]));
  1146. n_envelopes++;
  1147. }
  1148. return ENS_OK;
  1149. }
  1150. // Generate a hash code from the specified string
  1151. static int Hash8(const char *string)
  1152. {
  1153. int c;
  1154. int chars = 0;
  1155. int hash = 0;
  1156. while ((c = *string++) != 0) {
  1157. c = tolower(c) - 'a';
  1158. hash = hash * 8 + c;
  1159. hash = (hash & 0x1ff) ^ (hash >> 8); // exclusive or
  1160. chars++;
  1161. }
  1162. return (hash+chars) & 0xff;
  1163. }
  1164. static int LoadEnvelope2(FILE *f, const char *fname)
  1165. {
  1166. int ix, ix2;
  1167. int n;
  1168. int x, y;
  1169. int displ;
  1170. int n_points;
  1171. double yy;
  1172. char line_buf[128];
  1173. float env_x[20];
  1174. float env_y[20];
  1175. int env_lin[20];
  1176. unsigned char env[ENV_LEN];
  1177. n_points = 0;
  1178. fgets(line_buf, sizeof(line_buf), f); // skip first line
  1179. while (!feof(f)) {
  1180. if (fgets(line_buf, sizeof(line_buf), f) == NULL)
  1181. break;
  1182. env_lin[n_points] = 0;
  1183. n = sscanf(line_buf, "%f %f %d", &env_x[n_points], &env_y[n_points], &env_lin[n_points]);
  1184. if (n >= 2) {
  1185. env_x[n_points] *= (float)1.28; // convert range 0-100 to 0-128
  1186. n_points++;
  1187. }
  1188. }
  1189. env_x[n_points] = env_x[n_points-1];
  1190. env_y[n_points] = env_y[n_points-1];
  1191. ix = -1;
  1192. ix2 = 0;
  1193. for (x = 0; x < ENV_LEN; x++) {
  1194. if (x > env_x[ix+4])
  1195. ix++;
  1196. if (x >= env_x[ix2+1])
  1197. ix2++;
  1198. if (env_lin[ix2] > 0) {
  1199. yy = env_y[ix2] + (env_y[ix2+1] - env_y[ix2]) * ((float)x - env_x[ix2]) / (env_x[ix2+1] - env_x[ix2]);
  1200. y = (int)(yy * 2.55);
  1201. } else if (n_points > 3)
  1202. y = (int)(polint(&env_x[ix], &env_y[ix], 4, x) * 2.55); // convert to range 0-255
  1203. else
  1204. y = (int)(polint(&env_x[ix], &env_y[ix], 3, x) * 2.55);
  1205. if (y < 0) y = 0;
  1206. if (y > 255) y = 255;
  1207. env[x] = y;
  1208. }
  1209. if (n_envelopes < N_ENVELOPES) {
  1210. strncpy0(envelope_paths[n_envelopes], fname, sizeof(envelope_paths[0]));
  1211. memcpy(envelope_dat[n_envelopes], env, ENV_LEN);
  1212. n_envelopes++;
  1213. }
  1214. displ = ftell(f_phdata);
  1215. fwrite(env, 1, 128, f_phdata);
  1216. return displ;
  1217. }
  1218. static espeak_ng_STATUS LoadDataFile(const char *path, int control, int *addr)
  1219. {
  1220. // load spectrum sequence or sample data from a file.
  1221. // return index into spect or sample data area. bit 23=1 if a sample
  1222. FILE *f;
  1223. int id;
  1224. int hash;
  1225. int type_code = ' ';
  1226. REF_HASH_TAB *p, *p2;
  1227. char buf[sizeof(path_home)+150];
  1228. if (strcmp(path, "NULL") == 0)
  1229. return ENS_OK;
  1230. if (strcmp(path, "DFT") == 0) {
  1231. *addr = 1;
  1232. return ENS_OK;
  1233. }
  1234. count_references++;
  1235. hash = Hash8(path);
  1236. p = ref_hash_tab[hash];
  1237. while (p != NULL) {
  1238. if (strcmp(path, p->string) == 0) {
  1239. duplicate_references++;
  1240. *addr = p->value; // already loaded this data
  1241. break;
  1242. }
  1243. p = (REF_HASH_TAB *)p->link;
  1244. }
  1245. if (*addr == 0) {
  1246. sprintf(buf, "%s/%s", phsrc, path);
  1247. if ((f = fopen(buf, "rb")) == NULL) {
  1248. sprintf(buf, "%s/%s.wav", phsrc, path);
  1249. if ((f = fopen(buf, "rb")) == NULL) {
  1250. error("Can't read file: %s", path);
  1251. return errno;
  1252. }
  1253. }
  1254. id = Read4Bytes(f);
  1255. rewind(f);
  1256. espeak_ng_STATUS status = ENS_OK;
  1257. if (id == 0x43455053) {
  1258. status = LoadSpect(path, control, addr);
  1259. type_code = 'S';
  1260. } else if (id == 0x46464952) {
  1261. *addr = LoadWavefile(f, path);
  1262. type_code = 'W';
  1263. } else if (id == 0x43544950) {
  1264. status = LoadEnvelope(f, path, addr);
  1265. type_code = 'E';
  1266. } else if (id == 0x45564E45) {
  1267. *addr = LoadEnvelope2(f, path);
  1268. type_code = 'E';
  1269. } else {
  1270. error("File not SPEC or RIFF: %s", path);
  1271. *addr = -1;
  1272. status = ENS_UNSUPPORTED_PHON_FORMAT;
  1273. }
  1274. fclose(f);
  1275. if (status != ENS_OK)
  1276. return status;
  1277. if (*addr > 0)
  1278. fprintf(f_phcontents, "%c 0x%.5x %s\n", type_code, *addr & 0x7fffff, path);
  1279. }
  1280. // add this item to the hash table
  1281. if (*addr > 0) {
  1282. p = ref_hash_tab[hash];
  1283. p2 = (REF_HASH_TAB *)malloc(sizeof(REF_HASH_TAB)+strlen(path)+1);
  1284. if (p2 == NULL)
  1285. return ENOMEM;
  1286. p2->value = *addr;
  1287. p2->ph_mnemonic = phoneme_out->mnemonic; // phoneme which uses this file
  1288. p2->ph_table = n_phoneme_tabs-1;
  1289. strcpy(p2->string, path);
  1290. p2->link = (char *)p;
  1291. ref_hash_tab[hash] = p2;
  1292. }
  1293. return ENS_OK;
  1294. }
  1295. static void CompileToneSpec(void)
  1296. {
  1297. int pitch1 = 0;
  1298. int pitch2 = 0;
  1299. int pitch_env = 0;
  1300. int amp_env = 0;
  1301. pitch1 = NextItemBrackets(tNUMBER, 2);
  1302. pitch2 = NextItemBrackets(tNUMBER, 3);
  1303. if (item_terminator == ',') {
  1304. NextItemBrackets(tSTRING, 3);
  1305. LoadDataFile(item_string, 0, &pitch_env);
  1306. }
  1307. if (item_terminator == ',') {
  1308. NextItemBrackets(tSTRING, 1);
  1309. LoadDataFile(item_string, 0, &amp_env);
  1310. }
  1311. if (pitch1 < pitch2) {
  1312. phoneme_out->start_type = pitch1;
  1313. phoneme_out->end_type = pitch2;
  1314. } else {
  1315. phoneme_out->start_type = pitch2;
  1316. phoneme_out->end_type = pitch1;
  1317. }
  1318. if (pitch_env != 0) {
  1319. *prog_out++ = i_PITCHENV + ((pitch_env >> 16) & 0xf);
  1320. *prog_out++ = pitch_env;
  1321. }
  1322. if (amp_env != 0) {
  1323. *prog_out++ = i_AMPENV + ((amp_env >> 16) & 0xf);
  1324. *prog_out++ = amp_env;
  1325. }
  1326. }
  1327. static void CompileSound(int keyword, int isvowel)
  1328. {
  1329. int addr = 0;
  1330. int value = 0;
  1331. char path[N_ITEM_STRING];
  1332. static int sound_instns[] = { i_FMT, i_WAV, i_VWLSTART, i_VWLENDING, i_WAVADD };
  1333. NextItemBrackets(tSTRING, 2);
  1334. strcpy(path, item_string);
  1335. if (item_terminator == ',') {
  1336. if ((keyword == kVOWELSTART) || (keyword == kVOWELENDING)) {
  1337. value = NextItemBrackets(tSIGNEDNUMBER, 1);
  1338. if (value > 127) {
  1339. value = 127;
  1340. error("Parameter > 127");
  1341. }
  1342. if (value < -128) {
  1343. value = -128;
  1344. error("Parameter < -128");
  1345. }
  1346. } else {
  1347. value = NextItemBrackets(tNUMBER, 1);
  1348. if (value > 255) {
  1349. value = 255;
  1350. error("Parameter > 255");
  1351. }
  1352. }
  1353. }
  1354. LoadDataFile(path, isvowel, &addr);
  1355. addr = addr / 4; // addr is words not bytes
  1356. *prog_out++ = sound_instns[keyword-kFMT] + ((value & 0xff) << 4) + ((addr >> 16) & 0xf);
  1357. *prog_out++ = addr & 0xffff;
  1358. }
  1359. /*
  1360. Condition
  1361. bits 14,15 1
  1362. bit 13 1 = AND, 0 = OR
  1363. bit 12 spare
  1364. bit 8-11
  1365. =0-3 p,t,n,n2 data=phoneme code
  1366. =4-7 p,t,n,n2 data=(bits5-7: phtype, place, property, special) (bits0-4: data)
  1367. =8 data = stress bitmap
  1368. =9 special tests
  1369. */
  1370. int CompileIf(int elif)
  1371. {
  1372. int key;
  1373. int finish = 0;
  1374. int word = 0;
  1375. int word2;
  1376. int data;
  1377. int bitmap;
  1378. int brackets;
  1379. int not_flag;
  1380. USHORT *prog_last_if = NULL;
  1381. then_count = 2;
  1382. after_if = 1;
  1383. while (!finish) {
  1384. not_flag = 0;
  1385. word2 = 0;
  1386. if (prog_out >= prog_out_max) {
  1387. error("Phoneme program too large");
  1388. return 0;
  1389. }
  1390. if ((key = NextItem(tCONDITION)) < 0)
  1391. error("Expected a condition, not '%s'", item_string);
  1392. if ((item_type == 0) && (key == k_NOT)) {
  1393. not_flag = 1;
  1394. if ((key = NextItem(tCONDITION)) < 0)
  1395. error("Expected a condition, not '%s'", item_string);
  1396. }
  1397. if (item_type == tWHICH_PHONEME) {
  1398. // prevPh(), thisPh(), nextPh(), next2Ph() etc
  1399. if (key >= 6) {
  1400. // put the 'which' code in the next instruction
  1401. word2 = key;
  1402. key = 6;
  1403. }
  1404. key = key << 8;
  1405. data = NextItemBrackets(tPROPERTIES, 0);
  1406. if (data >= 0)
  1407. word = key + data + 0x700;
  1408. else {
  1409. data = LookupPhoneme(item_string, 2);
  1410. word = key + data;
  1411. }
  1412. } else if (item_type == tTEST) {
  1413. if (key == kTHISSTRESS) {
  1414. bitmap = 0;
  1415. brackets = 2;
  1416. do {
  1417. data = NextItemBrackets(tNUMBER, brackets);
  1418. if (data > 7)
  1419. error("Expected list of stress levels");
  1420. bitmap |= (1 << data);
  1421. brackets = 3;
  1422. } while (item_terminator == ',');
  1423. word = i_StressLevel | bitmap;
  1424. } else
  1425. word = key;
  1426. } else {
  1427. error("Unexpected keyword '%s'", item_string);
  1428. if ((strcmp(item_string, "phoneme") == 0) || (strcmp(item_string, "endphoneme") == 0))
  1429. return -1;
  1430. }
  1431. // output the word
  1432. prog_last_if = prog_out;
  1433. *prog_out++ = word | i_CONDITION;
  1434. if (word2 != 0)
  1435. *prog_out++ = word2;
  1436. if (not_flag)
  1437. *prog_out++ = i_NOT;
  1438. // expect AND, OR, THEN
  1439. switch (NextItem(tCONDITION))
  1440. {
  1441. case k_AND:
  1442. break;
  1443. case k_OR:
  1444. if (prog_last_if != NULL)
  1445. *prog_last_if |= i_OR;
  1446. break;
  1447. case k_THEN:
  1448. finish = 1;
  1449. break;
  1450. default:
  1451. error("Expected AND, OR, THEN");
  1452. break;
  1453. }
  1454. }
  1455. if (elif == 0) {
  1456. if_level++;
  1457. if_stack[if_level].p_else = NULL;
  1458. }
  1459. if_stack[if_level].returned = 0;
  1460. if_stack[if_level].p_then = prog_out;
  1461. *prog_out++ = i_JUMP_FALSE;
  1462. return 0;
  1463. }
  1464. void FillThen(int add)
  1465. {
  1466. USHORT *p;
  1467. int offset;
  1468. p = if_stack[if_level].p_then;
  1469. if (p != NULL) {
  1470. offset = prog_out - p + add;
  1471. if ((then_count == 1) && (if_level == 1)) {
  1472. // The THEN part only contains one statement, we can remove the THEN jump
  1473. // and the interpreter will implicitly skip the statement.
  1474. while (p < prog_out) {
  1475. p[0] = p[1];
  1476. p++;
  1477. }
  1478. prog_out--;
  1479. } else {
  1480. if (offset > MAX_JUMP)
  1481. error("IF block is too long");
  1482. *p = i_JUMP_FALSE + offset;
  1483. }
  1484. if_stack[if_level].p_then = NULL;
  1485. }
  1486. then_count = 0;
  1487. }
  1488. int CompileElse(void)
  1489. {
  1490. USHORT *ref;
  1491. USHORT *p;
  1492. if (if_level < 1) {
  1493. error("ELSE not expected");
  1494. return 0;
  1495. }
  1496. if (if_stack[if_level].returned == 0)
  1497. FillThen(1);
  1498. else
  1499. FillThen(0);
  1500. if (if_stack[if_level].returned == 0) {
  1501. ref = prog_out;
  1502. *prog_out++ = 0;
  1503. if ((p = if_stack[if_level].p_else) != NULL)
  1504. *ref = ref - p; // backwards offset to the previous else
  1505. if_stack[if_level].p_else = ref;
  1506. }
  1507. return 0;
  1508. }
  1509. int CompileElif(void)
  1510. {
  1511. if (if_level < 1) {
  1512. error("ELIF not expected");
  1513. return 0;
  1514. }
  1515. CompileElse();
  1516. CompileIf(1);
  1517. return 0;
  1518. }
  1519. int CompileEndif(void)
  1520. {
  1521. USHORT *p;
  1522. int chain;
  1523. int offset;
  1524. if (if_level < 1) {
  1525. error("ENDIF not expected");
  1526. return 0;
  1527. }
  1528. FillThen(0);
  1529. if ((p = if_stack[if_level].p_else) != NULL) {
  1530. do {
  1531. chain = *p; // a chain of previous else links
  1532. offset = prog_out - p;
  1533. if (offset > MAX_JUMP)
  1534. error("IF block is too long");
  1535. *p = i_JUMP + offset;
  1536. p -= chain;
  1537. } while (chain > 0);
  1538. }
  1539. if_level--;
  1540. return 0;
  1541. }
  1542. static int CompileSwitch(int type)
  1543. {
  1544. // Type 0: EndSwitch
  1545. // 1: SwitchPrevVowelType
  1546. // 2: SwitchNextVowelType
  1547. if (type == 0) {
  1548. // check the instructions in the Switch
  1549. return 0;
  1550. }
  1551. if (type == 1)
  1552. *prog_out++ = i_SWITCH_PREVVOWEL+6;
  1553. if (type == 2)
  1554. *prog_out++ = i_SWITCH_NEXTVOWEL+6;
  1555. return 0;
  1556. }
  1557. static PHONEME_TAB_LIST *FindPhonemeTable(const char *string)
  1558. {
  1559. int ix;
  1560. for (ix = 0; ix < n_phoneme_tabs; ix++) {
  1561. if (strcmp(phoneme_tab_list2[ix].name, string) == 0)
  1562. return &phoneme_tab_list2[ix];
  1563. }
  1564. error("Unknown phoneme table: '%s'", string);
  1565. return NULL;
  1566. }
  1567. static PHONEME_TAB *FindPhoneme(const char *string)
  1568. {
  1569. PHONEME_TAB_LIST *phtab = NULL;
  1570. int ix;
  1571. unsigned int mnem;
  1572. char *phname;
  1573. char buf[200];
  1574. // is this the name of a phoneme which is in scope
  1575. if ((strlen(string) <= 4) && ((ix = LookupPhoneme(string, 0)) != -1))
  1576. return &phoneme_tab2[ix];
  1577. // no, treat the name as phonemetable/phoneme
  1578. strcpy(buf, string);
  1579. if ((phname = strchr(buf, '/')) != 0)
  1580. *phname++ = 0;
  1581. phtab = FindPhonemeTable(buf);
  1582. if (phtab == NULL)
  1583. return NULL; // phoneme table not found
  1584. mnem = StringToWord(phname);
  1585. for (ix = 1; ix < 256; ix++) {
  1586. if (mnem == phtab->phoneme_tab_ptr[ix].mnemonic)
  1587. return &phtab->phoneme_tab_ptr[ix];
  1588. }
  1589. error("Phoneme reference not found: '%s'", string);
  1590. return NULL;
  1591. }
  1592. static void ImportPhoneme(void)
  1593. {
  1594. unsigned int ph_mnem;
  1595. unsigned int ph_code;
  1596. PHONEME_TAB *ph;
  1597. NextItem(tSTRING);
  1598. if ((ph = FindPhoneme(item_string)) == NULL) {
  1599. error("Cannot find phoneme '%s' to import.", item_string);
  1600. return;
  1601. }
  1602. if (phoneme_out->phflags != 0 ||
  1603. phoneme_out->type != phINVALID ||
  1604. phoneme_out->start_type != 0 ||
  1605. phoneme_out->end_type != 0 ||
  1606. phoneme_out->std_length != 0 ||
  1607. phoneme_out->length_mod != 0) {
  1608. error("Phoneme import will override set properties.");
  1609. }
  1610. ph_mnem = phoneme_out->mnemonic;
  1611. ph_code = phoneme_out->code;
  1612. memcpy(phoneme_out, ph, sizeof(PHONEME_TAB));
  1613. phoneme_out->mnemonic = ph_mnem;
  1614. phoneme_out->code = ph_code;
  1615. if (phoneme_out->type != phVOWEL)
  1616. phoneme_out->end_type = 0; // voicingswitch, this must be set later to refer to a local phoneme
  1617. }
  1618. static void CallPhoneme(void)
  1619. {
  1620. PHONEME_TAB *ph;
  1621. int ix;
  1622. int addr = 0;
  1623. NextItem(tSTRING);
  1624. // first look for a procedure name
  1625. for (ix = 0; ix < n_procs; ix++) {
  1626. if (strcmp(proc_names[ix], item_string) == 0) {
  1627. addr = proc_addr[ix];
  1628. break;
  1629. }
  1630. }
  1631. if (ix == n_procs) {
  1632. // procedure not found, try a phoneme name
  1633. if ((ph = FindPhoneme(item_string)) == NULL)
  1634. return;
  1635. addr = ph->program;
  1636. if (phoneme_out->type == phINVALID) {
  1637. // Phoneme type has not been set. Copy it from the called phoneme
  1638. phoneme_out->type = ph->type;
  1639. phoneme_out->start_type = ph->start_type;
  1640. phoneme_out->end_type = ph->end_type;
  1641. phoneme_out->std_length = ph->std_length;
  1642. phoneme_out->length_mod = ph->length_mod;
  1643. }
  1644. }
  1645. *prog_out++ = i_CALLPH + (addr >> 16);
  1646. *prog_out++ = addr;
  1647. }
  1648. static void DecThenCount()
  1649. {
  1650. if (then_count > 0)
  1651. then_count--;
  1652. }
  1653. int CompilePhoneme(int compile_phoneme)
  1654. {
  1655. int endphoneme = 0;
  1656. int keyword;
  1657. int value;
  1658. int phcode = 0;
  1659. int flags;
  1660. int ix;
  1661. int start;
  1662. int count;
  1663. int c;
  1664. char *p;
  1665. int vowel_length_factor = 100; // for testing
  1666. char number_buf[12];
  1667. char ipa_buf[N_ITEM_STRING+1];
  1668. PHONEME_TAB phoneme_out2;
  1669. PHONEME_PROG_LOG phoneme_prog_log;
  1670. prog_out = prog_buf;
  1671. prog_out_max = &prog_buf[MAX_PROG_BUF-1];
  1672. if_level = 0;
  1673. if_stack[0].returned = 0;
  1674. after_if = 0;
  1675. int phoneme_flags = 0;
  1676. NextItem(tSTRING);
  1677. if (compile_phoneme) {
  1678. phcode = LookupPhoneme(item_string, 1); // declare phoneme if not already there
  1679. if (phcode == -1) return 0;
  1680. phoneme_out = &phoneme_tab2[phcode];
  1681. } else {
  1682. // declare a procedure
  1683. if (n_procs >= N_PROCS) {
  1684. error("Too many procedures");
  1685. return 0;
  1686. }
  1687. strcpy(proc_names[n_procs], item_string);
  1688. phoneme_out = &phoneme_out2;
  1689. sprintf(number_buf, "%.3dP", n_procs);
  1690. phoneme_out->mnemonic = StringToWord(number_buf);
  1691. }
  1692. phoneme_out->code = phcode;
  1693. phoneme_out->program = 0;
  1694. phoneme_out->type = phINVALID;
  1695. phoneme_out->std_length = 0;
  1696. phoneme_out->start_type = 0;
  1697. phoneme_out->end_type = 0;
  1698. phoneme_out->length_mod = 0;
  1699. phoneme_out->phflags = 0;
  1700. while (!endphoneme && !feof(f_in)) {
  1701. if ((keyword = NextItem(tKEYWORD)) < 0) {
  1702. if (keyword == -2) {
  1703. error("Missing 'endphoneme' before end-of-file"); // end of file
  1704. break;
  1705. }
  1706. phoneme_feature_t feature = phoneme_feature_from_string(item_string);
  1707. espeak_ng_STATUS status = phoneme_add_feature(phoneme_out, feature);
  1708. if (status == ENS_OK)
  1709. continue;
  1710. error_from_status(status, item_string);
  1711. continue;
  1712. }
  1713. switch (item_type)
  1714. {
  1715. case tPHONEME_TYPE:
  1716. if (phoneme_out->type != phINVALID) {
  1717. if (phoneme_out->type == phFRICATIVE && keyword == phLIQUID)
  1718. ; // apr liquid => ok
  1719. else
  1720. error("More than one phoneme type: %s", item_string);
  1721. }
  1722. phoneme_out->type = keyword;
  1723. break;
  1724. case tPHONEME_FLAG:
  1725. phoneme_flags |= keyword;
  1726. break;
  1727. case tINSTRN1:
  1728. // instruction group 0, with 8 bit operands which set data in PHONEME_DATA
  1729. switch (keyword)
  1730. {
  1731. case i_CHANGE_PHONEME:
  1732. case i_APPEND_PHONEME:
  1733. case i_APPEND_IFNEXTVOWEL:
  1734. case i_INSERT_PHONEME:
  1735. case i_REPLACE_NEXT_PHONEME:
  1736. case i_VOICING_SWITCH:
  1737. case i_CHANGE_IF | isDiminished:
  1738. case i_CHANGE_IF | isUnstressed:
  1739. case i_CHANGE_IF | isNotStressed:
  1740. case i_CHANGE_IF | isStressed:
  1741. value = NextItemBrackets(tPHONEMEMNEM, 0);
  1742. *prog_out++ = (keyword << 8) + value;
  1743. DecThenCount();
  1744. break;
  1745. case i_PAUSE_BEFORE:
  1746. value = NextItemMax(255);
  1747. *prog_out++ = (i_PAUSE_BEFORE << 8) + value;
  1748. DecThenCount();
  1749. break;
  1750. case i_PAUSE_AFTER:
  1751. value = NextItemMax(255);
  1752. *prog_out++ = (i_PAUSE_AFTER << 8) + value;
  1753. DecThenCount();
  1754. break;
  1755. case i_SET_LENGTH:
  1756. value = NextItemMax(511);
  1757. if (phoneme_out->type == phVOWEL)
  1758. value = (value * vowel_length_factor)/100;
  1759. if (after_if == 0)
  1760. phoneme_out->std_length = value/2;
  1761. else {
  1762. *prog_out++ = (i_SET_LENGTH << 8) + value/2;
  1763. DecThenCount();
  1764. }
  1765. break;
  1766. case i_ADD_LENGTH:
  1767. value = NextItem(tSIGNEDNUMBER) / 2;
  1768. *prog_out++ = (i_ADD_LENGTH << 8) + (value & 0xff);
  1769. DecThenCount();
  1770. break;
  1771. case i_LENGTH_MOD:
  1772. value = NextItem(tNUMBER);
  1773. phoneme_out->length_mod = value;
  1774. break;
  1775. case i_IPA_NAME:
  1776. NextItem(tSTRING);
  1777. if (strcmp(item_string, "NULL") == 0)
  1778. strcpy(item_string, " ");
  1779. // copy the string, recognize characters in the form U+9999
  1780. flags = 0;
  1781. count = 0;
  1782. ix = 1;
  1783. for (p = item_string; *p != 0;) {
  1784. p += utf8_in(&c, p);
  1785. if ((c == '|') && (count > 0)) {
  1786. // '|' means don't allow a tie or joiner before this letter
  1787. flags |= (1 << (count -1));
  1788. } else if ((c == 'U') && (p[0] == '+')) {
  1789. int j;
  1790. // U+9999
  1791. p++;
  1792. memcpy(number_buf, p, 4); // U+ should be followed by 4 hex digits
  1793. number_buf[4] = 0;
  1794. c = '#';
  1795. sscanf(number_buf, "%x", (unsigned int *)&c);
  1796. // move past the 4 hexdecimal digits
  1797. for (j = 0; j < 4; j++) {
  1798. if (!isalnum(*p))
  1799. break;
  1800. p++;
  1801. }
  1802. ix += utf8_out(c, &ipa_buf[ix]);
  1803. count++;
  1804. } else {
  1805. ix += utf8_out(c, &ipa_buf[ix]);
  1806. count++;
  1807. }
  1808. }
  1809. ipa_buf[0] = flags;
  1810. ipa_buf[ix] = 0;
  1811. start = 1;
  1812. if (flags != 0)
  1813. start = 0; // only include the flags byte if bits are set
  1814. value = strlen(&ipa_buf[start]); // number of UTF-8 bytes
  1815. *prog_out++ = (i_IPA_NAME << 8) + value;
  1816. for (ix = 0; ix < value; ix += 2)
  1817. *prog_out++ = (ipa_buf[ix+start] << 8) + (ipa_buf[ix+start+1] & 0xff);
  1818. DecThenCount();
  1819. break;
  1820. }
  1821. break;
  1822. case tSTATEMENT:
  1823. switch (keyword)
  1824. {
  1825. case kIMPORT_PH:
  1826. ImportPhoneme();
  1827. phoneme_flags = phoneme_out->phflags;
  1828. break;
  1829. case kSTARTTYPE:
  1830. phcode = NextItem(tPHONEMEMNEM);
  1831. if (phcode == -1)
  1832. phcode = LookupPhoneme(item_string, 1);
  1833. phoneme_out->start_type = phcode;
  1834. if (phoneme_out->type == phINVALID)
  1835. error("a phoneme type or manner of articulation must be specified before starttype");
  1836. break;
  1837. case kENDTYPE:
  1838. phcode = NextItem(tPHONEMEMNEM);
  1839. if (phcode == -1)
  1840. phcode = LookupPhoneme(item_string, 1);
  1841. if (phoneme_out->type == phINVALID)
  1842. error("a phoneme type or manner of articulation must be specified before endtype");
  1843. else if (phoneme_out->type == phVOWEL)
  1844. phoneme_out->end_type = phcode;
  1845. else if (phcode != phoneme_out->start_type)
  1846. error("endtype must equal starttype for consonants");
  1847. break;
  1848. case kVOICINGSWITCH:
  1849. phcode = NextItem(tPHONEMEMNEM);
  1850. if (phcode == -1)
  1851. phcode = LookupPhoneme(item_string, 1);
  1852. if (phoneme_out->type == phVOWEL)
  1853. error("voicingswitch cannot be used on vowels");
  1854. else
  1855. phoneme_out->end_type = phcode; // use end_type field for consonants as voicing_switch
  1856. break;
  1857. case kSTRESSTYPE:
  1858. value = NextItem(tNUMBER);
  1859. phoneme_out->std_length = value;
  1860. if (prog_out > prog_buf) {
  1861. error("stress phonemes can't contain program instructions");
  1862. prog_out = prog_buf;
  1863. }
  1864. break;
  1865. case kIF:
  1866. endphoneme = CompileIf(0);
  1867. break;
  1868. case kELSE:
  1869. endphoneme = CompileElse();
  1870. break;
  1871. case kELIF:
  1872. endphoneme = CompileElif();
  1873. break;
  1874. case kENDIF:
  1875. endphoneme = CompileEndif();
  1876. break;
  1877. case kENDSWITCH:
  1878. break;
  1879. case kSWITCH_PREVVOWEL:
  1880. endphoneme = CompileSwitch(1);
  1881. break;
  1882. case kSWITCH_NEXTVOWEL:
  1883. endphoneme = CompileSwitch(2);
  1884. break;
  1885. case kCALLPH:
  1886. CallPhoneme();
  1887. DecThenCount();
  1888. break;
  1889. case kFMT:
  1890. if_stack[if_level].returned = 1;
  1891. DecThenCount();
  1892. if (phoneme_out->type == phVOWEL)
  1893. CompileSound(keyword, 1);
  1894. else
  1895. CompileSound(keyword, 0);
  1896. break;
  1897. case kWAV:
  1898. if_stack[if_level].returned = 1;
  1899. // fallthrough:
  1900. case kVOWELSTART:
  1901. case kVOWELENDING:
  1902. case kANDWAV:
  1903. DecThenCount();
  1904. CompileSound(keyword, 0);
  1905. break;
  1906. case kVOWELIN:
  1907. DecThenCount();
  1908. endphoneme = CompileVowelTransition(1);
  1909. break;
  1910. case kVOWELOUT:
  1911. DecThenCount();
  1912. endphoneme = CompileVowelTransition(2);
  1913. break;
  1914. case kTONESPEC:
  1915. DecThenCount();
  1916. CompileToneSpec();
  1917. break;
  1918. case kCONTINUE:
  1919. *prog_out++ = OPCODE_CONTINUE;
  1920. DecThenCount();
  1921. break;
  1922. case kRETURN:
  1923. *prog_out++ = OPCODE_RETURN;
  1924. DecThenCount();
  1925. break;
  1926. case kINCLUDE:
  1927. case kPHONEMETABLE:
  1928. error("Missing 'endphoneme' before '%s'", item_string); // drop through to endphoneme
  1929. // fallthrough:
  1930. case kENDPHONEME:
  1931. case kENDPROCEDURE:
  1932. endphoneme = 1;
  1933. if (if_level > 0)
  1934. error("Missing ENDIF");
  1935. if ((prog_out > prog_buf) && (if_stack[0].returned == 0))
  1936. *prog_out++ = OPCODE_RETURN;
  1937. break;
  1938. }
  1939. break;
  1940. }
  1941. }
  1942. if (endphoneme != 1)
  1943. error("'endphoneme' not expected here");
  1944. if (compile_phoneme) {
  1945. if (phoneme_out->type == phINVALID) {
  1946. error("Phoneme type is missing");
  1947. phoneme_out->type = 0;
  1948. }
  1949. phoneme_out->phflags |= phoneme_flags;
  1950. if (phoneme_out->phflags & phVOICED) {
  1951. if (phoneme_out->type == phSTOP)
  1952. phoneme_out->type = phVSTOP;
  1953. else if (phoneme_out->type == phFRICATIVE)
  1954. phoneme_out->type = phVFRICATIVE;
  1955. }
  1956. if (phoneme_out->std_length == 0) {
  1957. if (phoneme_out->type == phVOWEL)
  1958. phoneme_out->std_length = 180/2; // default length for vowel
  1959. }
  1960. phoneme_out->phflags |= phLOCAL; // declared in this phoneme table
  1961. if (phoneme_out->type == phDELETED)
  1962. phoneme_out->mnemonic = 0x01; // will not be recognised
  1963. }
  1964. if (prog_out > prog_buf) {
  1965. // write out the program for this phoneme
  1966. fflush(f_phindex);
  1967. phoneme_out->program = ftell(f_phindex) / sizeof(USHORT);
  1968. if (f_prog_log != NULL) {
  1969. phoneme_prog_log.addr = phoneme_out->program;
  1970. phoneme_prog_log.length = prog_out - prog_buf;
  1971. fwrite(&phoneme_prog_log, 1, sizeof(phoneme_prog_log), f_prog_log);
  1972. }
  1973. if (compile_phoneme == 0)
  1974. proc_addr[n_procs++] = ftell(f_phindex) / sizeof(USHORT);
  1975. fwrite(prog_buf, sizeof(USHORT), prog_out - prog_buf, f_phindex);
  1976. }
  1977. return 0;
  1978. }
  1979. static void WritePhonemeTables()
  1980. {
  1981. int ix;
  1982. int j;
  1983. int n;
  1984. int value;
  1985. int count;
  1986. PHONEME_TAB *p;
  1987. value = n_phoneme_tabs;
  1988. fputc(value, f_phtab);
  1989. fputc(0, f_phtab);
  1990. fputc(0, f_phtab);
  1991. fputc(0, f_phtab);
  1992. for (ix = 0; ix < n_phoneme_tabs; ix++) {
  1993. p = phoneme_tab_list2[ix].phoneme_tab_ptr;
  1994. n = n_phcodes_list[ix];
  1995. memset(&p[n], 0, sizeof(p[n]));
  1996. p[n].mnemonic = 0; // terminate the phoneme table
  1997. // count number of locally declared phonemes
  1998. count = 0;
  1999. for (j = 0; j < n; j++) {
  2000. if (ix == 0)
  2001. p[j].phflags |= phLOCAL; // write all phonemes in the base phoneme table
  2002. if (p[j].phflags & phLOCAL)
  2003. count++;
  2004. }
  2005. phoneme_tab_list2[ix].n_phonemes = count+1;
  2006. fputc(count+1, f_phtab);
  2007. fputc(phoneme_tab_list2[ix].includes, f_phtab);
  2008. fputc(0, f_phtab);
  2009. fputc(0, f_phtab);
  2010. fwrite(phoneme_tab_list2[ix].name, 1, N_PHONEME_TAB_NAME, f_phtab);
  2011. for (j = 0; j < n; j++) {
  2012. if (p[j].phflags & phLOCAL) {
  2013. // this bit is set temporarily to incidate a local phoneme, declared in
  2014. // in the current phoneme file
  2015. p[j].phflags &= ~phLOCAL;
  2016. fwrite(&p[j], sizeof(PHONEME_TAB), 1, f_phtab);
  2017. }
  2018. }
  2019. fwrite(&p[n], sizeof(PHONEME_TAB), 1, f_phtab); // include the extra list-terminator phoneme entry
  2020. free(p);
  2021. }
  2022. }
  2023. static void EndPhonemeTable()
  2024. {
  2025. int ix;
  2026. int *pw;
  2027. int length;
  2028. if (n_phoneme_tabs == 0)
  2029. return;
  2030. // check that all referenced phonemes have been declared
  2031. for (ix = 0; ix < n_phcodes; ix++) {
  2032. if (phoneme_tab2[ix].type == phINVALID) {
  2033. error("Phoneme [%s] not declared, referenced at line %d",
  2034. WordToString(phoneme_tab2[ix].mnemonic), (int)(phoneme_tab2[ix].program));
  2035. error_count++;
  2036. phoneme_tab2[ix].type = 0; // prevent the error message repeating
  2037. }
  2038. }
  2039. n_phcodes_list[n_phoneme_tabs-1] = n_phcodes;
  2040. }
  2041. static void StartPhonemeTable(const char *name)
  2042. {
  2043. int ix;
  2044. int j;
  2045. PHONEME_TAB *p;
  2046. if (n_phoneme_tabs >= N_PHONEME_TABS-1) {
  2047. error("Too many phonemetables");
  2048. return;
  2049. }
  2050. p = (PHONEME_TAB *)calloc(sizeof(PHONEME_TAB), N_PHONEME_TAB);
  2051. if (p == NULL) {
  2052. error("Out of memory");
  2053. return;
  2054. }
  2055. memset(&phoneme_tab_list2[n_phoneme_tabs], 0, sizeof(PHONEME_TAB_LIST));
  2056. phoneme_tab_list2[n_phoneme_tabs].phoneme_tab_ptr = phoneme_tab2 = p;
  2057. memset(phoneme_tab_list2[n_phoneme_tabs].name, 0, sizeof(phoneme_tab_list2[n_phoneme_tabs].name));
  2058. strncpy0(phoneme_tab_list2[n_phoneme_tabs].name, name, N_PHONEME_TAB_NAME);
  2059. n_phcodes = 1;
  2060. phoneme_tab_list2[n_phoneme_tabs].includes = 0;
  2061. if (n_phoneme_tabs > 0) {
  2062. NextItem(tSTRING); // name of base phoneme table
  2063. for (ix = 0; ix < n_phoneme_tabs; ix++) {
  2064. if (strcmp(item_string, phoneme_tab_list2[ix].name) == 0) {
  2065. phoneme_tab_list2[n_phoneme_tabs].includes = ix+1;
  2066. // initialise the new phoneme table with the contents of this one
  2067. memcpy(phoneme_tab2, phoneme_tab_list2[ix].phoneme_tab_ptr, sizeof(PHONEME_TAB)*N_PHONEME_TAB);
  2068. n_phcodes = n_phcodes_list[ix];
  2069. // clear "local phoneme" bit"
  2070. for (j = 0; j < n_phcodes; j++)
  2071. phoneme_tab2[j].phflags &= ~phLOCAL;
  2072. break;
  2073. }
  2074. }
  2075. if (ix == n_phoneme_tabs && strcmp(item_string, "_") != 0)
  2076. error("Can't find base phonemetable '%s'", item_string);
  2077. } else
  2078. ReservePhCodes();
  2079. n_phoneme_tabs++;
  2080. }
  2081. static void CompilePhonemeFiles()
  2082. {
  2083. int item;
  2084. FILE *f;
  2085. char buf[sizeof(path_home)+120];
  2086. linenum = 1;
  2087. count_references = 0;
  2088. duplicate_references = 0;
  2089. count_frames = 0;
  2090. n_procs = 0;
  2091. for (;;) {
  2092. if (feof(f_in)) {
  2093. // end of file, go back to previous from, from which this was included
  2094. if (stack_ix == 0)
  2095. break; // end of top level, finished
  2096. fclose(f_in);
  2097. f_in = stack[--stack_ix].file;
  2098. strcpy(current_fname, stack[stack_ix].fname);
  2099. linenum = stack[stack_ix].linenum;
  2100. }
  2101. item = NextItem(tKEYWORD);
  2102. switch (item)
  2103. {
  2104. case kUTF8_BOM:
  2105. break; // ignore bytes 0xef 0xbb 0xbf
  2106. case kINCLUDE:
  2107. NextItem(tSTRING);
  2108. sprintf(buf, "%s/%s", phsrc, item_string);
  2109. if ((stack_ix < N_STACK) && (f = fopen(buf, "rb")) != NULL) {
  2110. stack[stack_ix].linenum = linenum;
  2111. strcpy(stack[stack_ix].fname, current_fname);
  2112. stack[stack_ix++].file = f_in;
  2113. f_in = f;
  2114. strncpy0(current_fname, item_string, sizeof(current_fname));
  2115. linenum = 1;
  2116. } else
  2117. error("Missing file: %s", item_string);
  2118. break;
  2119. case kPHONEMETABLE:
  2120. EndPhonemeTable();
  2121. NextItem(tSTRING); // name of the new phoneme table
  2122. StartPhonemeTable(item_string);
  2123. break;
  2124. case kPHONEMESTART:
  2125. if (n_phoneme_tabs == 0) {
  2126. error("phonemetable is missing");
  2127. return;
  2128. }
  2129. CompilePhoneme(1);
  2130. break;
  2131. case kPROCEDURE:
  2132. CompilePhoneme(0);
  2133. break;
  2134. default:
  2135. if (!feof(f_in))
  2136. error("Keyword 'phoneme' expected");
  2137. break;
  2138. }
  2139. }
  2140. memset(&phoneme_tab2[n_phcodes+1], 0, sizeof(phoneme_tab2[n_phcodes+1]));
  2141. phoneme_tab2[n_phcodes+1].mnemonic = 0; // terminator
  2142. }
  2143. #pragma GCC visibility push(default)
  2144. espeak_ng_STATUS
  2145. espeak_ng_CompilePhonemeData(long rate,
  2146. FILE *log,
  2147. espeak_ng_ERROR_CONTEXT *context)
  2148. {
  2149. return espeak_ng_CompilePhonemeDataPath(rate, NULL, NULL, log, context);
  2150. }
  2151. espeak_ng_STATUS
  2152. espeak_ng_CompilePhonemeDataPath(long rate,
  2153. const char *source_path,
  2154. const char *destination_path,
  2155. FILE *log,
  2156. espeak_ng_ERROR_CONTEXT *context)
  2157. {
  2158. if (!log) log = stderr;
  2159. char fname[sizeof(path_home)+40];
  2160. char phdst[sizeof(path_home)+40]; // Destination: path to the phondata/phontab/phonindex output files.
  2161. if (source_path) {
  2162. sprintf(phsrc, "%s", source_path);
  2163. } else {
  2164. sprintf(phsrc, "%s/../phsource", path_home);
  2165. }
  2166. if (destination_path) {
  2167. sprintf(phdst, "%s", destination_path);
  2168. } else {
  2169. sprintf(phdst, "%s", path_home);
  2170. }
  2171. samplerate_native = samplerate = rate;
  2172. LoadPhData(NULL, NULL);
  2173. if (LoadVoice("", 0) == NULL)
  2174. return ENS_VOICE_NOT_FOUND;
  2175. WavegenInit(rate, 0);
  2176. WavegenSetVoice(voice);
  2177. n_envelopes = 0;
  2178. error_count = 0;
  2179. resample_count = 0;
  2180. memset(markers_used, 0, sizeof(markers_used));
  2181. f_errors = log;
  2182. strncpy0(current_fname, "phonemes", sizeof(current_fname));
  2183. sprintf(fname, "%s/phonemes", phsrc);
  2184. fprintf(log, "Compiling phoneme data: %s\n", fname);
  2185. f_in = fopen(fname, "rb");
  2186. if (f_in == NULL)
  2187. return create_file_error_context(context, errno, fname);
  2188. sprintf(fname, "%s/%s", phsrc, "compile_report");
  2189. f_report = fopen(fname, "w");
  2190. if (f_report == NULL) {
  2191. int error = errno;
  2192. fclose(f_in);
  2193. return create_file_error_context(context, error, fname);
  2194. }
  2195. sprintf(fname, "%s/%s", phdst, "phondata-manifest");
  2196. if ((f_phcontents = fopen(fname, "w")) == NULL)
  2197. f_phcontents = stderr;
  2198. fprintf(f_phcontents,
  2199. "# This file lists the type of data that has been compiled into the\n"
  2200. "# phondata file\n"
  2201. "#\n"
  2202. "# The first character of a line indicates the type of data:\n"
  2203. "# S - A SPECT_SEQ structure\n"
  2204. "# W - A wavefile segment\n"
  2205. "# E - An envelope\n"
  2206. "#\n"
  2207. "# Address is the displacement within phondata of this item\n"
  2208. "#\n"
  2209. "# Address Data file\n"
  2210. "# ------- ---------\n");
  2211. sprintf(fname, "%s/%s", phdst, "phondata");
  2212. f_phdata = fopen(fname, "wb");
  2213. if (f_phdata == NULL) {
  2214. int error = errno;
  2215. fclose(f_in);
  2216. fclose(f_report);
  2217. fclose(f_phcontents);
  2218. return create_file_error_context(context, error, fname);
  2219. }
  2220. sprintf(fname, "%s/%s", phdst, "phonindex");
  2221. f_phindex = fopen(fname, "wb");
  2222. if (f_phindex == NULL) {
  2223. int error = errno;
  2224. fclose(f_in);
  2225. fclose(f_report);
  2226. fclose(f_phcontents);
  2227. fclose(f_phdata);
  2228. return create_file_error_context(context, error, fname);
  2229. }
  2230. sprintf(fname, "%s/%s", phdst, "phontab");
  2231. f_phtab = fopen(fname, "wb");
  2232. if (f_phtab == NULL) {
  2233. int error = errno;
  2234. fclose(f_in);
  2235. fclose(f_report);
  2236. fclose(f_phcontents);
  2237. fclose(f_phdata);
  2238. fclose(f_phindex);
  2239. return create_file_error_context(context, error, fname);
  2240. }
  2241. sprintf(fname, "%s/compile_prog_log", phsrc);
  2242. f_prog_log = fopen(fname, "wb");
  2243. // write a word so that further data doesn't start at displ=0
  2244. Write4Bytes(f_phdata, version_phdata);
  2245. Write4Bytes(f_phdata, samplerate_native);
  2246. Write4Bytes(f_phindex, version_phdata);
  2247. memset(ref_hash_tab, 0, sizeof(ref_hash_tab));
  2248. n_phoneme_tabs = 0;
  2249. stack_ix = 0;
  2250. StartPhonemeTable("base");
  2251. CompilePhonemeFiles();
  2252. EndPhonemeTable();
  2253. WritePhonemeTables();
  2254. fprintf(f_errors, "\nRefs %d, Reused %d\n", count_references, duplicate_references);
  2255. fclose(f_in);
  2256. fclose(f_phcontents);
  2257. fclose(f_phdata);
  2258. fclose(f_phindex);
  2259. fclose(f_phtab);
  2260. if (f_prog_log != NULL)
  2261. fclose(f_prog_log);
  2262. LoadPhData(NULL, NULL);
  2263. CompileReport();
  2264. fclose(f_report);
  2265. if (resample_count > 0) {
  2266. fprintf(f_errors, "\n%d WAV files resampled to %d Hz\n", resample_count, samplerate_native);
  2267. fprintf(log, "Compiled phonemes: %d errors, %d files resampled to %d Hz.\n", error_count, resample_count, samplerate_native);
  2268. } else
  2269. fprintf(log, "Compiled phonemes: %d errors.\n", error_count);
  2270. if (f_errors != stderr && f_errors != stdout)
  2271. fclose(f_errors);
  2272. espeak_ng_STATUS status = ReadPhondataManifest(context);
  2273. if (status != ENS_OK)
  2274. return status;
  2275. return error_count > 0 ? ENS_COMPILE_ERROR : ENS_OK;
  2276. }
  2277. #pragma GCC visibility pop
  2278. static const char *preset_tune_names[] = {
  2279. "s1", "c1", "q1", "e1", NULL
  2280. };
  2281. static const TUNE default_tune = {
  2282. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2283. { 0, 0, 0, 0 },
  2284. { 0, 40, 24, 8, 0, 0, 0, 0 },
  2285. 46, 57, PITCHfall, 16, 0, 0,
  2286. 255, 78, 50, 255,
  2287. 3, 5,
  2288. { -7, -7, -7 }, { -7, -7, -7 },
  2289. PITCHfall, 64, 8,
  2290. PITCHfall, 70, 18, 24, 12,
  2291. PITCHfall, 70, 18, 24, 12, 0,
  2292. { 0, 0, 0, 0, 0, 0, 0, 0 }, 0
  2293. };
  2294. #define N_TUNE_NAMES 100
  2295. MNEM_TAB envelope_names[] = {
  2296. { "fall", 0 },
  2297. { "rise", 2 },
  2298. { "fall-rise", 4 },
  2299. { "fall-rise2", 6 },
  2300. { "rise-fall", 8 },
  2301. { "fall-rise3", 10 },
  2302. { "fall-rise4", 12 },
  2303. { "fall2", 14 },
  2304. { "rise2", 16 },
  2305. { "rise-fall-rise", 18 },
  2306. { NULL, -1 }
  2307. };
  2308. int LookupEnvelopeName(const char *name)
  2309. {
  2310. return LookupMnem(envelope_names, name);
  2311. }
  2312. #pragma GCC visibility push(default)
  2313. espeak_ng_STATUS espeak_ng_CompileIntonation(FILE *log, espeak_ng_ERROR_CONTEXT *context)
  2314. {
  2315. if (!log) log = stderr;
  2316. int ix;
  2317. char *p;
  2318. char c;
  2319. int keyword;
  2320. int n_tune_names = 0;
  2321. int done_split = 0;
  2322. int done_onset = 0;
  2323. int done_last = 0;
  2324. int n_preset_tunes = 0;
  2325. int found;
  2326. int tune_number = 0;
  2327. FILE *f_out;
  2328. TUNE *tune_data;
  2329. TUNE new_tune;
  2330. char name[12];
  2331. char tune_names[N_TUNE_NAMES][12];
  2332. char buf[sizeof(path_home)+150];
  2333. error_count = 0;
  2334. f_errors = log;
  2335. sprintf(buf, "%s/../phsource/intonation.txt", path_home);
  2336. if ((f_in = fopen(buf, "r")) == NULL) {
  2337. sprintf(buf, "%s/../phsource/intonation", path_home);
  2338. if ((f_in = fopen(buf, "r")) == NULL) {
  2339. int error = errno;
  2340. fclose(f_errors);
  2341. return create_file_error_context(context, error, buf);
  2342. }
  2343. }
  2344. for (ix = 0; preset_tune_names[ix] != NULL; ix++)
  2345. strcpy(tune_names[ix], preset_tune_names[ix]);
  2346. n_tune_names = ix;
  2347. n_preset_tunes = ix;
  2348. // make a list of the tune names
  2349. while (!feof(f_in)) {
  2350. if (fgets(buf, sizeof(buf), f_in) == NULL)
  2351. break;
  2352. if ((memcmp(buf, "tune", 4) == 0) && isspace(buf[4])) {
  2353. p = &buf[5];
  2354. while (isspace(*p)) p++;
  2355. ix = 0;
  2356. while ((ix < (int)(sizeof(name) - 1)) && !isspace(*p))
  2357. name[ix++] = *p++;
  2358. name[ix] = 0;
  2359. found = 0;
  2360. for (ix = 0; ix < n_tune_names; ix++) {
  2361. if (strcmp(name, tune_names[ix]) == 0) {
  2362. found = 1;
  2363. break;
  2364. }
  2365. }
  2366. if (found == 0) {
  2367. strncpy0(tune_names[n_tune_names++], name, sizeof(name));
  2368. if (n_tune_names >= N_TUNE_NAMES)
  2369. break;
  2370. }
  2371. }
  2372. }
  2373. rewind(f_in);
  2374. linenum = 1;
  2375. tune_data = (n_tune_names == 0) ? NULL : (TUNE *)calloc(n_tune_names, sizeof(TUNE));
  2376. if (tune_data == NULL) {
  2377. fclose(f_in);
  2378. fclose(f_errors);
  2379. return ENOMEM;
  2380. }
  2381. sprintf(buf, "%s/intonations", path_home);
  2382. f_out = fopen(buf, "wb");
  2383. if (f_out == NULL) {
  2384. int error = errno;
  2385. fclose(f_in);
  2386. fclose(f_errors);
  2387. free(tune_data);
  2388. return create_file_error_context(context, error, buf);
  2389. }
  2390. while (!feof(f_in)) {
  2391. keyword = NextItem(tINTONATION);
  2392. switch (keyword)
  2393. {
  2394. case kTUNE:
  2395. done_split = 0;
  2396. memcpy(&new_tune, &default_tune, sizeof(TUNE));
  2397. NextItem(tSTRING);
  2398. strncpy0(new_tune.name, item_string, sizeof(new_tune.name));
  2399. found = 0;
  2400. tune_number = 0;
  2401. for (ix = 0; ix < n_tune_names; ix++) {
  2402. if (strcmp(new_tune.name, tune_names[ix]) == 0) {
  2403. found = 1;
  2404. tune_number = ix;
  2405. if (tune_data[ix].name[0] != 0)
  2406. found = 2;
  2407. break;
  2408. }
  2409. }
  2410. if (found == 2)
  2411. error("Duplicate tune name: '%s'", new_tune.name);
  2412. if (found == 0)
  2413. error("Bad tune name: '%s;", new_tune.name);
  2414. break;
  2415. case kENDTUNE:
  2416. if (done_onset == 0) {
  2417. new_tune.unstr_start[0] = new_tune.unstr_start[1];
  2418. new_tune.unstr_end[0] = new_tune.unstr_end[1];
  2419. }
  2420. if (done_last == 0) {
  2421. new_tune.unstr_start[2] = new_tune.unstr_start[1];
  2422. new_tune.unstr_end[2] = new_tune.unstr_end[1];
  2423. }
  2424. memcpy(&tune_data[tune_number], &new_tune, sizeof(TUNE));
  2425. break;
  2426. case kTUNE_PREHEAD:
  2427. new_tune.prehead_start = NextItem(tNUMBER);
  2428. new_tune.prehead_end = NextItem(tNUMBER);
  2429. break;
  2430. case kTUNE_ONSET:
  2431. new_tune.onset = NextItem(tNUMBER);
  2432. new_tune.unstr_start[0] = NextItem(tSIGNEDNUMBER);
  2433. new_tune.unstr_end[0] = NextItem(tSIGNEDNUMBER);
  2434. done_onset = 1;
  2435. break;
  2436. case kTUNE_HEADLAST:
  2437. new_tune.head_last = NextItem(tNUMBER);
  2438. new_tune.unstr_start[2] = NextItem(tSIGNEDNUMBER);
  2439. new_tune.unstr_end[2] = NextItem(tSIGNEDNUMBER);
  2440. done_last = 1;
  2441. break;
  2442. case kTUNE_HEADENV:
  2443. NextItem(tSTRING);
  2444. if ((ix = LookupEnvelopeName(item_string)) < 0)
  2445. error("Bad envelope name: '%s'", item_string);
  2446. else
  2447. new_tune.stressed_env = ix;
  2448. new_tune.stressed_drop = NextItem(tNUMBER);
  2449. break;
  2450. case kTUNE_HEAD:
  2451. new_tune.head_max_steps = NextItem(tNUMBER);
  2452. new_tune.head_start = NextItem(tNUMBER);
  2453. new_tune.head_end = NextItem(tNUMBER);
  2454. new_tune.unstr_start[1] = NextItem(tSIGNEDNUMBER);
  2455. new_tune.unstr_end[1] = NextItem(tSIGNEDNUMBER);
  2456. break;
  2457. case kTUNE_HEADEXTEND:
  2458. // up to 8 numbers
  2459. for (ix = 0; ix < (int)(sizeof(new_tune.head_extend)); ix++) {
  2460. if (!isdigit(c = CheckNextChar()) && (c != '-'))
  2461. break;
  2462. new_tune.head_extend[ix] = (NextItem(tSIGNEDNUMBER) * 64) / 100; // convert from percentage to 64ths
  2463. }
  2464. new_tune.n_head_extend = ix; // number of values
  2465. break;
  2466. case kTUNE_NUCLEUS0:
  2467. NextItem(tSTRING);
  2468. if ((ix = LookupEnvelopeName(item_string)) < 0) {
  2469. error("Bad envelope name: '%s'", item_string);
  2470. break;
  2471. }
  2472. new_tune.nucleus0_env = ix;
  2473. new_tune.nucleus0_max = NextItem(tNUMBER);
  2474. new_tune.nucleus0_min = NextItem(tNUMBER);
  2475. break;
  2476. case kTUNE_NUCLEUS1:
  2477. NextItem(tSTRING);
  2478. if ((ix = LookupEnvelopeName(item_string)) < 0) {
  2479. error("Bad envelope name: '%s'", item_string);
  2480. break;
  2481. }
  2482. new_tune.nucleus1_env = ix;
  2483. new_tune.nucleus1_max = NextItem(tNUMBER);
  2484. new_tune.nucleus1_min = NextItem(tNUMBER);
  2485. new_tune.tail_start = NextItem(tNUMBER);
  2486. new_tune.tail_end = NextItem(tNUMBER);
  2487. if (!done_split) {
  2488. // also this as the default setting for 'split'
  2489. new_tune.split_nucleus_env = ix;
  2490. new_tune.split_nucleus_max = new_tune.nucleus1_max;
  2491. new_tune.split_nucleus_min = new_tune.nucleus1_min;
  2492. new_tune.split_tail_start = new_tune.tail_start;
  2493. new_tune.split_tail_end = new_tune.tail_end;
  2494. }
  2495. break;
  2496. case kTUNE_SPLIT:
  2497. NextItem(tSTRING);
  2498. if ((ix = LookupEnvelopeName(item_string)) < 0) {
  2499. error("Bad envelope name: '%s'", item_string);
  2500. break;
  2501. }
  2502. done_split = 1;
  2503. new_tune.split_nucleus_env = ix;
  2504. new_tune.split_nucleus_max = NextItem(tNUMBER);
  2505. new_tune.split_nucleus_min = NextItem(tNUMBER);
  2506. new_tune.split_tail_start = NextItem(tNUMBER);
  2507. new_tune.split_tail_end = NextItem(tNUMBER);
  2508. NextItem(tSTRING);
  2509. item_string[12] = 0;
  2510. for (ix = 0; ix < n_tune_names; ix++) {
  2511. if (strcmp(item_string, tune_names[ix]) == 0)
  2512. break;
  2513. }
  2514. if (ix == n_tune_names)
  2515. error("Tune '%s' not found", item_string);
  2516. else
  2517. new_tune.split_tune = ix;
  2518. break;
  2519. default:
  2520. error("Unexpected: '%s'", item_string);
  2521. break;
  2522. }
  2523. }
  2524. for (ix = 0; ix < n_preset_tunes; ix++) {
  2525. if (tune_data[ix].name[0] == 0)
  2526. error("Tune '%s' not defined", preset_tune_names[ix]);
  2527. }
  2528. fwrite(tune_data, n_tune_names, sizeof(TUNE), f_out);
  2529. free(tune_data);
  2530. fclose(f_in);
  2531. fclose(f_out);
  2532. fprintf(log, "Compiled %d intonation tunes: %d errors.\n", n_tune_names, error_count);
  2533. LoadPhData(NULL, NULL);
  2534. return error_count > 0 ? ENS_COMPILE_ERROR : ENS_OK;
  2535. }
  2536. #pragma GCC visibility pop