eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiledata.c 68KB

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