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