eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.

compiledata.c 68KB

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