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

compiledata.c 71KB

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