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 69KB

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