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

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