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

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