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

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