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

compiledata.c 70KB

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