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

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