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

compiledata.c 70KB

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