load_abc.cpp 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189
  1. /*
  2. MikMod Sound System
  3. By Jake Stine of Divine Entertainment (1996-2000)
  4. Support:
  5. If you find problems with this code, send mail to:
  6. [email protected]
  7. Distribution / Code rights:
  8. Use this source code in any fashion you see fit. Giving me credit where
  9. credit is due is optional, depending on your own levels of integrity and
  10. honesty.
  11. -----------------------------------------
  12. Module: LOAD_ABC
  13. ABC module loader.
  14. by Peter Grootswagers (2006)
  15. <email:[email protected]>
  16. Portability:
  17. All systems - all compilers (hopefully)
  18. */
  19. #include <stdlib.h>
  20. #include <time.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <ctype.h>
  24. #ifndef _WIN32
  25. #include <unistd.h> // for sleep
  26. #endif // _WIN32
  27. #ifdef NEWMIKMOD
  28. #include "mikmod.h"
  29. #include "uniform.h"
  30. typedef UBYTE BYTE;
  31. typedef UWORD WORD;
  32. #else
  33. #include "stdafx.h"
  34. #include "sndfile.h"
  35. #endif
  36. #include "load_pat.h"
  37. #define MAXABCINCLUDES 8
  38. #define MAXCHORDNAMES 80
  39. #define ABC_ENV_DUMPTRACKS "MMABC_DUMPTRACKS"
  40. #define ABC_ENV_NORANDOMPICK "MMABC_NO_RANDOM_PICK"
  41. // gchords use tracks with vpos 1 thru 7
  42. // drums use track with vpos 8
  43. // voice chords use vpos 0 and vpos from 11 up
  44. #define GCHORDBPOS 1
  45. #define GCHORDFPOS 2
  46. #define GCHORDCPOS 3
  47. #define DRUMPOS 8
  48. #define DRONEPOS1 9
  49. #define DRONEPOS2 10
  50. // in the patterns a whole note at unmodified tempo is 16 rows
  51. #define ROWSPERNOTE 16
  52. // a 1/64-th note played in triool equals a 1/96-th note, to be able
  53. // to play them and also to play the 1/64-th we need a resolution of 192
  54. // because 2/192 = 1/96 and 3/192 = 1/64
  55. #define RESOLUTION 192
  56. #pragma pack(1)
  57. /**************************************************************************
  58. **************************************************************************/
  59. #ifdef NEWMIKMOD
  60. static char ABC_Version[] = "ABC+2.0 (draft IV)";
  61. #endif
  62. typedef enum {
  63. note,
  64. octave,
  65. smpno,
  66. volume,
  67. effect,
  68. effoper
  69. } ABCEVENT_X_NOTE;
  70. typedef enum {
  71. none,
  72. trill,
  73. bow,
  74. accent
  75. } ABCEVENT_X_EFFECT;
  76. typedef enum {
  77. cmdflag,
  78. command,
  79. chordnum,
  80. chordnote,
  81. chordbase,
  82. jumptype
  83. } ABCEVENT_X_CMD;
  84. typedef enum {
  85. cmdsegno = '$',
  86. cmdcapo = 'B',
  87. cmdchord = 'C',
  88. cmdfine = 'F',
  89. cmdhide = 'H',
  90. cmdjump = 'J',
  91. cmdloop = 'L',
  92. cmdcoda = 'O',
  93. cmdpartbrk = 'P',
  94. cmdsync = 'S',
  95. cmdtempo = 'T',
  96. cmdvariant = 'V',
  97. cmdtocoda = 'X'
  98. } ABCEVENT_CMD;
  99. typedef enum {
  100. jumpnormal,
  101. jumpfade,
  102. jumpdacapo,
  103. jumpdcfade,
  104. jumpdasegno,
  105. jumpdsfade,
  106. jumpfine,
  107. jumptocoda,
  108. jumpvariant,
  109. jumpnot
  110. } ABCEVENT_JUMPTYPE;
  111. typedef struct _ABCEVENT
  112. {
  113. struct _ABCEVENT *next;
  114. uint32_t tracktick;
  115. union {
  116. uint8_t par[6];
  117. struct {
  118. uint8_t flg;
  119. uint8_t cmd;
  120. uint32_t lpar; // for variant selections, bit pattern
  121. };
  122. };
  123. uint8_t part;
  124. uint8_t tiednote;
  125. } ABCEVENT;
  126. typedef struct _ABCTRACK
  127. {
  128. struct _ABCTRACK *next;
  129. ABCEVENT *head;
  130. ABCEVENT *tail;
  131. ABCEVENT *capostart;
  132. ABCEVENT *tienote;
  133. int transpose;
  134. int octave_shift;
  135. uint32_t slidevoltime; // for crescendo and diminuendo
  136. int slidevol; // -2:fade away, -1:diminuendo, 0:none, +1:crescendo
  137. uint8_t vno; // 0 is track is free for use, from previous song in multi-songbook
  138. uint8_t vpos; // 0 is main voice, other is subtrack for gchords, gchords or drumnotes
  139. uint8_t tiedvpos;
  140. uint8_t mute;
  141. uint8_t chan; // 10 is percussion channel, any other is melodic channel
  142. uint8_t volume;
  143. uint8_t instr; // current instrument for this track
  144. uint8_t legato;
  145. char v[22]; // first twenty characters are significant
  146. } ABCTRACK;
  147. typedef struct _ABCMACRO
  148. {
  149. struct _ABCMACRO *next;
  150. char *name;
  151. char *subst;
  152. char *n;
  153. } ABCMACRO;
  154. /**************************************************************************
  155. **************************************************************************/
  156. typedef struct _ABCHANDLE
  157. {
  158. #ifdef NEWMIKMOD
  159. MM_ALLOC *allochandle;
  160. MM_ALLOC *macrohandle;
  161. MM_ALLOC *trackhandle;
  162. MM_ALLOC *ho;
  163. #endif
  164. ABCMACRO *macro;
  165. ABCMACRO *umacro;
  166. ABCTRACK *track;
  167. long int pickrandom;
  168. unsigned int len;
  169. int speed;
  170. char *line;
  171. char *beatstring;
  172. uint8_t beat[4]; // a:first note, b:strong notes, c:weak notes, n:strong note every n
  173. char gchord[80]; // last setting for gchord
  174. char drum[80]; // last setting for drum
  175. char drumins[80]; // last setting for drum
  176. char drumvol[80]; // last setting for drum
  177. uint32_t barticks;
  178. // parse variables, declared here to avoid parameter pollution
  179. int abcchordvol, abcchordprog, abcbassvol, abcbassprog;
  180. int ktrans;
  181. int drumon, gchordon, droneon;
  182. int dronegm, dronepitch[2], dronevol[2];
  183. ABCTRACK *tp, *tpc, *tpr;
  184. uint32_t tracktime;
  185. } ABCHANDLE;
  186. static int global_voiceno, global_octave_shift, global_tempo_factor, global_tempo_divider;
  187. static char global_part;
  188. static uint32_t global_songstart;
  189. /* Named guitar chords */
  190. static char chordname[MAXCHORDNAMES][8];
  191. static int chordnotes[MAXCHORDNAMES][6];
  192. static int chordlen[MAXCHORDNAMES];
  193. static int chordsnamed = 0;
  194. static const char *sig[] = {
  195. " C D EF G A Bc d ef g a b", // 7 sharps C#
  196. " C D EF G AB c d ef g ab ", // 6 sharps F#
  197. " C DE F G AB c de f g ab ", // 5 sharps B
  198. " C DE F GA B c de f ga b ", // 4 sharps E
  199. " CD E F GA B cd e f ga b ", // 3 sharps A
  200. " CD E FG A B cd e fg a b ", // 2 sharps D
  201. " C D E FG A Bc d e fg a b", // 1 sharps G
  202. " C D EF G A Bc d ef g a b", // 0 sharps C
  203. " C D EF G AB c d ef g ab ", // 1 flats F
  204. " C DE F G AB c de f g ab ", // 2 flats Bb
  205. " C DE F GA B c de f ga b ", // 3 flats Eb
  206. " CD E F GA B cd e f ga b ", // 4 flats Ab
  207. " CD E FG A B cd e fg a b ", // 5 flats Db
  208. "C D E FG A Bc d e fg a b ", // 6 flats Gb
  209. "C D EF G A Bc d ef g a b ", // 7 flats Cb
  210. // 0123456789012345678901234
  211. };
  212. static const char *keySigs[] = {
  213. /* 0....:....1....:....2....:....3....:....4....:....5. */
  214. "7 sharps: C# A#m G#Mix D#Dor E#Phr F#Lyd B#Loc ",
  215. "6 sharps: F# D#m C#Mix G#Dor A#Phr BLyd E#Loc ",
  216. "5 sharps: B G#m F#Mix C#Dor D#Phr ELyd A#Loc ",
  217. "4 sharps: E C#m BMix F#Dor G#Phr ALyd D#Loc ",
  218. "3 sharps: A F#m EMix BDor C#Phr DLyd G#Loc ",
  219. "2 sharps: D Bm AMix EDor F#Phr GLyd C#Loc ",
  220. "1 sharp : G Em DMix ADor BPhr CLyd F#Loc ",
  221. "0 sharps: C Am GMix DDor EPhr FLyd BLoc ",
  222. "1 flat : F Dm CMix GDor APhr BbLyd ELoc ",
  223. "2 flats : Bb Gm FMix CDor DPhr EbLyd ALoc ",
  224. "3 flats : Eb Cm BbMix FDor GPhr AbLyd DLoc ",
  225. "4 flats : Ab Fm EbMix BbDor CPhr DbLyd GLoc ",
  226. "5 flats : Db Bbm AbMix EbDor FPhr GbLyd CLoc ",
  227. "6 flats : Gb Ebm DbMix AbDor BbPhr CbLyd FLoc ",
  228. "7 flats : Cb Abm GbMix DbDor EbPhr FbLyd BbLoc ",
  229. 0
  230. };
  231. // local prototypes
  232. static int abc_getnumber(const char *p, int *number);
  233. static ABCTRACK *abc_locate_track(ABCHANDLE *h, const char *voice, int pos);
  234. static void abc_add_event(ABCHANDLE *h, ABCTRACK *tp, ABCEVENT *e);
  235. static void abc_add_setloop(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime);
  236. static void abc_add_setjumploop(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime, ABCEVENT_JUMPTYPE j);
  237. static uint32_t abc_pattracktime(ABCHANDLE *h, uint32_t tracktime);
  238. static int abc_patno(ABCHANDLE *h, uint32_t tracktime);
  239. #ifndef HAVE_SETENV
  240. static void setenv(const char *name, const char *value, int overwrite)
  241. {
  242. int len = strlen(name)+1+strlen(value)+1;
  243. char *str = (char *)malloc(len);
  244. sprintf(str, "%s=%s", name, value);
  245. putenv(str);
  246. free(str);
  247. }
  248. #endif
  249. static int abc_isvalidchar(char c) {
  250. return(isalpha(c) || isdigit(c) || isspace(c) || c == '%' || c == ':');
  251. }
  252. static const char *abc_skipspace(const char *p)
  253. {
  254. while (*p && isspace(*p))
  255. p++;
  256. return p;
  257. }
  258. static void abc_extractkeyvalue(char *key, size_t key_max,
  259. char *value, size_t value_max, const char *src)
  260. {
  261. while (*src && isspace(*src))
  262. src++;
  263. size_t key_size;
  264. for (key_size = 0; key_size < key_max - 1 && *src;) {
  265. if (*src == '=') {
  266. src++;
  267. break;
  268. }
  269. key[key_size++] = *src++;
  270. }
  271. while (key_size > 0 && isspace(key[key_size - 1]))
  272. key_size--;
  273. key[key_size] = '\0';
  274. while (*src && isspace(*src))
  275. src++;
  276. size_t value_size;
  277. for (value_size = 0; value_size < value_max - 1 && *src;)
  278. value[value_size++] = *src++;
  279. while (value_size > 0 && isspace(value[value_size - 1]))
  280. value_size--;
  281. value[value_size] = '\0';
  282. }
  283. static void abc_message(const char *s1, const char *s2)
  284. {
  285. char txt[256];
  286. if( strlen(s1) + strlen(s2) > 255 ) return;
  287. sprintf(txt, s1, s2);
  288. #ifdef NEWMIKMOD
  289. _mmlog(txt);
  290. #else
  291. fprintf(stderr, "load_abc > %s\n", txt);
  292. #endif
  293. }
  294. static uint32_t modticks(uint32_t abcticks)
  295. {
  296. return abcticks / RESOLUTION;
  297. }
  298. static uint32_t abcticks(uint32_t modticks)
  299. {
  300. return modticks * RESOLUTION;
  301. }
  302. static uint32_t notelen_notediv_to_ticks(int speed, int len, int div)
  303. {
  304. uint32_t u;
  305. if (div == 0) return 0;
  306. u = (ROWSPERNOTE * RESOLUTION * speed * len * global_tempo_factor) / (div * global_tempo_divider);
  307. return u;
  308. }
  309. static void abc_dumptracks(ABCHANDLE *h, const char *p)
  310. {
  311. ABCTRACK *t;
  312. ABCEVENT *e;
  313. int n,pat,row,tck;
  314. char nn[3];
  315. if( !h ) return;
  316. for( t=h->track; t; t=t->next ) {
  317. printf("track %d.%d chan=%d %s\n", (int)(t->vno), (int)(t->vpos),
  318. (int)(t->chan), (char *)(t->v));
  319. if( strcmp(p,"nonotes") )
  320. n = 1;
  321. else
  322. n = 0;
  323. for( e=t->head; e; e=e->next ) {
  324. tck = modticks(e->tracktick);
  325. row = tck / h->speed;
  326. pat = row / 64;
  327. tck = tck % h->speed;
  328. row = row % 64;
  329. nn[0] = ( e->tracktick % abcticks(h->speed * 64) ) ? ' ': '-';
  330. if( e->flg == 1 ) {
  331. printf(" %6d.%02d.%d%c%c %d.%d %s ",
  332. pat, row, tck, nn[0], (int)(e->part), (int)(t->vno),
  333. (int)(t->vpos), (char *)(t->v));
  334. if( e->cmd == cmdchord ) {
  335. nn[0] = "CCCDDEFFGGAABccddeffggaabb"[e->par[chordnote]];
  336. nn[1] = "b # # # # # # # # # # #"[e->par[chordnote]];
  337. nn[2] = '\0';
  338. if( isspace(nn[1]) ) nn[1] = '\0';
  339. printf("CMD %c: gchord %s%s",
  340. (char)(e->cmd), nn, chordname[e->par[chordnum]]);
  341. if( e->par[chordbase] != e->par[chordnote] ) {
  342. nn[0] = "CCCDDEFFGGAABccddeffggaabb"[e->par[chordbase]];
  343. nn[1] = "b # # # # # # # # # # #"[e->par[chordbase]];
  344. nn[2] = '\0';
  345. printf("/%s", nn);
  346. }
  347. printf("\n");
  348. }
  349. else
  350. printf("CMD %c @%p 0x%08lX\n",
  351. (char)(e->cmd), e,
  352. (unsigned long)(e->lpar));
  353. if( strcmp(p,"nonotes") )
  354. n = 1;
  355. else
  356. n = 0;
  357. }
  358. else if( n ) {
  359. printf(" %6d.%02d.%d%c%c %d.%d %s ", pat, row, tck, nn[0], e->part, t->vno, t->vpos, t->v);
  360. if( e->par[note] ) {
  361. nn[0] = "CCCDDEFFGGAABccddeffggaabb"[e->par[note]-23];
  362. nn[1] = "b # # # # # # # # # # #"[e->par[note]-23];
  363. nn[2] = '\0';
  364. }
  365. else strcpy(nn,"--");
  366. printf("NOTE %s octave %d inst %s vol %03d\n",
  367. nn, e->par[octave], pat_gm_name(pat_smptogm(e->par[smpno])),e->par[volume]);
  368. if( strcmp(p,"all") )
  369. n = 0;
  370. }
  371. }
  372. }
  373. }
  374. #ifdef NEWMIKMOD
  375. #define MMFILE MMSTREAM
  376. #define mmfgetc(x) _mm_read_SBYTE(x)
  377. #define mmfeof(x) _mm_feof(x)
  378. #define mmfgets(buf,sz,f) _mm_fgets(f,buf,sz)
  379. #define mmftell(x) _mm_ftell(x)
  380. #define mmfseek(f,p,w) _mm_fseek(f,p,w)
  381. #define mmfopen(s,m) _mm_fopen(s,m)
  382. #define mmfclose(f) _mm_fclose(f)
  383. #else
  384. #if defined(WIN32) && defined(_mm_free)
  385. #undef _mm_free
  386. #endif
  387. #define MMSTREAM FILE
  388. #define _mm_fopen(name,mode) fopen(name,mode)
  389. #define _mm_fgets(f,buf,sz) fgets(buf,sz,f)
  390. #define _mm_fseek(f,pos,whence) fseek(f,pos,whence)
  391. #define _mm_ftell(f) ftell(f)
  392. #define _mm_read_UBYTES(buf,sz,f) fread(buf,sz,1,f)
  393. #define _mm_read_SBYTES(buf,sz,f) fread(buf,sz,1,f)
  394. #define _mm_feof(f) feof(f)
  395. #define _mm_fclose(f) fclose(f)
  396. #define DupStr(h,buf,sz) strdup(buf)
  397. #define _mm_calloc(h,n,sz) calloc(n,sz)
  398. #define _mm_recalloc(h,buf,sz,elsz) realloc(buf,sz)
  399. #define _mm_free(h,p) free(p)
  400. typedef struct {
  401. char *mm;
  402. int sz;
  403. int pos;
  404. } MMFILE;
  405. static MMFILE *mmfopen(const char *name, const char *mode)
  406. {
  407. FILE *fp;
  408. MMFILE *mmfile;
  409. long len;
  410. if( *mode != 'r' ) return NULL;
  411. fp = fopen(name, mode);
  412. if( !fp ) return NULL;
  413. fseek(fp, 0, SEEK_END);
  414. len = ftell(fp);
  415. mmfile = (MMFILE *)malloc(len+sizeof(MMFILE));
  416. if( !mmfile ) return NULL;
  417. fseek(fp, 0, SEEK_SET);
  418. fread(&mmfile[1],1,len,fp);
  419. fclose(fp);
  420. mmfile->mm = (char *)&mmfile[1];
  421. mmfile->sz = len;
  422. mmfile->pos = 0;
  423. return mmfile;
  424. }
  425. static void mmfclose(MMFILE *mmfile)
  426. {
  427. free(mmfile);
  428. }
  429. static bool mmfeof(MMFILE *mmfile)
  430. {
  431. if( mmfile->pos < 0 ) return TRUE;
  432. if( mmfile->pos < mmfile->sz ) return FALSE;
  433. return TRUE;
  434. }
  435. static int mmfgetc(MMFILE *mmfile)
  436. {
  437. int b;
  438. if( mmfeof(mmfile) ) return EOF;
  439. b = mmfile->mm[mmfile->pos];
  440. mmfile->pos++;
  441. if( b=='\r' && !mmfeof(mmfile) && mmfile->mm[mmfile->pos] == '\n' ) {
  442. b = '\n';
  443. mmfile->pos++;
  444. }
  445. return b;
  446. }
  447. static void mmfgets(char buf[], unsigned int bufsz, MMFILE *mmfile)
  448. {
  449. int i,b;
  450. for( i=0; i<(int)bufsz-1; i++ ) {
  451. b = mmfgetc(mmfile);
  452. if( b==EOF ) break;
  453. buf[i] = b;
  454. if( b == '\n' ) break;
  455. }
  456. buf[i] = '\0';
  457. }
  458. static long mmftell(MMFILE *mmfile)
  459. {
  460. return mmfile->pos;
  461. }
  462. static void mmfseek(MMFILE *mmfile, long p, int whence)
  463. {
  464. switch(whence) {
  465. case SEEK_SET:
  466. mmfile->pos = p;
  467. break;
  468. case SEEK_CUR:
  469. mmfile->pos += p;
  470. break;
  471. case SEEK_END:
  472. mmfile->pos = mmfile->sz + p;
  473. break;
  474. }
  475. }
  476. #endif
  477. // =====================================================================================
  478. static ABCEVENT *abc_new_event(ABCHANDLE *h, uint32_t abctick, const char data[])
  479. // =====================================================================================
  480. {
  481. ABCEVENT *retval;
  482. int i;
  483. retval = (ABCEVENT *)_mm_calloc(h->trackhandle, 1,sizeof(ABCEVENT));
  484. retval->next = NULL;
  485. retval->tracktick = abctick;
  486. for( i=0; i<6; i++ )
  487. retval->par[i] = data[i];
  488. retval->part = global_part;
  489. retval->tiednote = 0;
  490. return retval;
  491. }
  492. // =============================================================================
  493. static ABCEVENT *abc_copy_event(ABCHANDLE *h, ABCEVENT *se)
  494. // =============================================================================
  495. {
  496. ABCEVENT *e;
  497. e = (ABCEVENT *)_mm_calloc(h->trackhandle, 1,sizeof(ABCEVENT));
  498. e->next = NULL;
  499. e->tracktick = se->tracktick;
  500. e->flg = se->flg;
  501. e->cmd = se->cmd;
  502. e->lpar = se->lpar;
  503. e->part = se->part;
  504. return e;
  505. }
  506. // =============================================================================
  507. static void abc_new_macro(ABCHANDLE *h, const char *m)
  508. // =============================================================================
  509. {
  510. ABCMACRO *retval;
  511. char key[256], value[256];
  512. abc_extractkeyvalue(key, sizeof(key), value, sizeof(value), m);
  513. retval = (ABCMACRO *)_mm_calloc(h->macrohandle, 1,sizeof(ABCTRACK));
  514. retval->name = DupStr(h->macrohandle, key, strlen(key));
  515. retval->n = strrchr(retval->name, 'n'); // for transposing macro's
  516. retval->subst = DupStr(h->macrohandle, value, strlen(value));
  517. retval->next = h->macro;
  518. h->macro = retval;
  519. }
  520. // =============================================================================
  521. static void abc_new_umacro(ABCHANDLE *h, const char *m)
  522. // =============================================================================
  523. {
  524. ABCMACRO *retval, *mp;
  525. char key[256], value[256];
  526. abc_extractkeyvalue(key, sizeof(key), value, sizeof(value), m);
  527. if( strlen(key) > 1 || strchr("~HIJKLMNOPQRSTUVWXY",toupper(key[0])) == 0 ) return;
  528. while( char *q = strchr(key, '!') )
  529. *q = '+'; // translate oldstyle to newstyle
  530. if( !strcmp(key,"+nil+") ) { // delete a macro
  531. mp = NULL;
  532. for( retval=h->umacro; retval; retval = retval->next ) {
  533. if( retval->name[0] == key[0] ) { // delete this one
  534. if( mp ) mp->next = retval->next;
  535. else h->umacro = retval->next;
  536. _mm_free(h->macrohandle, retval);
  537. return;
  538. }
  539. mp = retval;
  540. }
  541. return;
  542. }
  543. retval = (ABCMACRO *)_mm_calloc(h->macrohandle, 1,sizeof(ABCTRACK));
  544. retval->name = DupStr(h->macrohandle, key, 1);
  545. retval->subst = DupStr(h->macrohandle, value, strlen(value));
  546. retval->n = 0;
  547. retval->next = h->umacro; // by placing it up front we mask out the old macro until we +nil+ it
  548. h->umacro = retval;
  549. }
  550. // =============================================================================
  551. static ABCTRACK *abc_new_track(ABCHANDLE *h, const char *voice, int pos)
  552. // =============================================================================
  553. {
  554. ABCTRACK *retval;
  555. if( !pos ) global_voiceno++;
  556. retval = (ABCTRACK *)_mm_calloc(h->trackhandle, 1,sizeof(ABCTRACK));
  557. retval->next = NULL;
  558. retval->vno = global_voiceno;
  559. retval->vpos = pos;
  560. retval->tiedvpos = pos;
  561. retval->instr = 1;
  562. strncpy(retval->v, voice, 20);
  563. retval->v[20] = '\0';
  564. retval->head = NULL;
  565. retval->tail = NULL;
  566. retval->capostart = NULL;
  567. retval->tienote = NULL;
  568. retval->mute = 0;
  569. retval->chan = 0;
  570. retval->transpose = 0;
  571. retval->volume = h->track? h->track->volume: 120;
  572. retval->slidevoltime = 0;
  573. retval->slidevol = 0;
  574. retval->legato = 0;
  575. return retval;
  576. }
  577. static int abc_numtracks(ABCHANDLE *h)
  578. {
  579. int n;
  580. ABCTRACK *t;
  581. n=0;
  582. for( t = h->track; t; t=t->next )
  583. n++;
  584. return n;
  585. }
  586. static int abc_interval(const char *s, const char *d)
  587. {
  588. const char *p;
  589. int i,j,k;
  590. int n,oct,m[2];
  591. for( j=0; j<2; j++ ) {
  592. if( j ) p = d;
  593. else p = s;
  594. switch(p[0]) {
  595. case '^':
  596. n = p[1];
  597. i = 2;
  598. break;
  599. case '_':
  600. n = p[1];
  601. i = 2;
  602. break;
  603. case '=':
  604. n = p[1];
  605. i = 2;
  606. break;
  607. default:
  608. n = p[0];
  609. i = 1;
  610. break;
  611. }
  612. for( k=0; k<25; k++ )
  613. if( n == sig[7][k] )
  614. break;
  615. oct = 4; // ABC note pitch C is C4 and pitch c is C5
  616. if( k > 12 ) {
  617. oct++;
  618. k -= 12;
  619. }
  620. while( p[i] == ',' || p[i] == '\'' ) {
  621. if( p[i] == ',' )
  622. oct--;
  623. else
  624. oct++;
  625. i++;
  626. }
  627. m[j] = k + 12 * oct;
  628. }
  629. return m[0] - m[1];
  630. }
  631. static int abc_transpose(const char *v)
  632. {
  633. int i,j,t;
  634. const char *m = "B", *mv = "";
  635. t = 0;
  636. global_octave_shift = 99;
  637. for( ; *v && *v != ']'; v++ ) {
  638. if( !strncasecmp(v,"t=",2) ) {
  639. v+=2;
  640. if( *v=='-' ) {
  641. j = -1;
  642. v++;
  643. }
  644. else j = 1;
  645. v+=abc_getnumber(v,&i);
  646. t += i * j;
  647. global_octave_shift = 0;
  648. }
  649. if( !strncasecmp(v,"octave=",7) ) {
  650. v+=7;
  651. if( *v=='-' ) {
  652. j = -1;
  653. v++;
  654. }
  655. else j = 1;
  656. v+=abc_getnumber(v,&i);
  657. t += i * j * 12;
  658. global_octave_shift = 0;
  659. }
  660. if( !strncasecmp(v,"transpose=",10) ) {
  661. v+=10;
  662. if( *v=='-' ) {
  663. j = -1;
  664. v++;
  665. }
  666. else j = 1;
  667. v+=abc_getnumber(v,&i);
  668. t += i * j;
  669. global_octave_shift = 0;
  670. }
  671. if( !strncasecmp(v,"octave=",7) ) { // used in kv304*.abc
  672. v+=7;
  673. if( *v=='-' ) {
  674. j = -1;
  675. v++;
  676. }
  677. else j = 1;
  678. v+=abc_getnumber(v,&i);
  679. t += i * j * 12;
  680. global_octave_shift = 0;
  681. }
  682. if( !strncasecmp(v,"m=",2) ) {
  683. v += 2;
  684. mv = v; // get the pitch for the middle staff line
  685. while( *v && *v != ' ' && *v != ']' ) v++;
  686. global_octave_shift = 0;
  687. }
  688. if( !strncasecmp(v,"middle=",7) ) {
  689. v += 7;
  690. mv = v; // get the pitch for the middle staff line
  691. while( *v && *v != ' ' && *v != ']' ) v++;
  692. global_octave_shift = 0;
  693. }
  694. if( !strncasecmp(v,"clef=",5) )
  695. v += 5;
  696. j = 1;
  697. if( !strncasecmp(v,"treble",6) ) {
  698. j = 0;
  699. v += 6;
  700. switch( *v ) {
  701. case '1': v++; m = "d"; break;
  702. case '2': v++;
  703. default: m = "B"; break;
  704. case '3': v++; m = "G"; break;
  705. case '4': v++; m = "E"; break;
  706. case '5': v++; m = "C"; break;
  707. }
  708. global_octave_shift = 0;
  709. }
  710. if( j && !strncasecmp(v,"bass",4) ) {
  711. m = "D,";
  712. j = 0;
  713. v += 4;
  714. switch( *v ) {
  715. case '1': v++; m = "C"; break;
  716. case '2': v++; m = "A,"; break;
  717. case '3': v++; m = "F,"; break;
  718. case '4': v++;
  719. default: m = "D,"; break;
  720. case '5': v++; m = "B,,"; break;
  721. }
  722. if( global_octave_shift == 99 )
  723. global_octave_shift = -2;
  724. }
  725. if( j && !strncasecmp(v,"tenor",5) ) {
  726. j = 0;
  727. v += 5;
  728. switch( *v ) {
  729. case '1': v++; m = "G"; break;
  730. case '2': v++; m = "E"; break;
  731. case '3': v++; m = "C"; break;
  732. case '4': v++;
  733. default: m = "A,"; break;
  734. case '5': v++; m = "F,"; break;
  735. }
  736. if( global_octave_shift == 99 )
  737. global_octave_shift = 1;
  738. }
  739. if( j && !strncasecmp(v,"alto",4) ) {
  740. j = 0;
  741. v += 4;
  742. switch( *v ) {
  743. case '1': v++; m = "G"; break;
  744. case '2': v++; m = "E"; break;
  745. case '3': v++;
  746. default: m = "C"; break;
  747. case '4': v++; m = "A,"; break;
  748. case '5': v++; m = "F,"; break;
  749. }
  750. if( global_octave_shift == 99 )
  751. global_octave_shift = 1;
  752. }
  753. if( j && strchr("+-",*v) && *v && v[1]=='8' ) {
  754. switch(*v) {
  755. case '+':
  756. t += 12;
  757. break;
  758. case '-':
  759. t -= 12;
  760. break;
  761. }
  762. v += 2;
  763. if( !strncasecmp(v,"va",2) ) v += 2;
  764. global_octave_shift = 0;
  765. j = 0;
  766. }
  767. if( j ) {
  768. while( *v && *v != ' ' && *v != ']' ) v++;
  769. }
  770. }
  771. if( strlen(mv) > 0 ) // someone set the middle note
  772. t += abc_interval(mv, m);
  773. if( global_octave_shift == 99 )
  774. global_octave_shift = 0;
  775. return t;
  776. }
  777. // =============================================================================
  778. static ABCTRACK *abc_locate_track(ABCHANDLE *h, const char *voice, int pos)
  779. // =============================================================================
  780. {
  781. ABCTRACK *tr, *prev, *trunused;
  782. char vc[21];
  783. int i, trans=0, voiceno=0, instrno = 1, channo = 0;
  784. for( ; *voice == ' '; voice++ ) ; // skip leading spaces
  785. for( i=0; i+1 < sizeof(vc) && *voice && *voice != ']' && *voice != '%' && !isspace(*voice); voice++ ) // can work with inline voice instructions
  786. vc[i++] = *voice;
  787. vc[i] = '\0';
  788. prev = NULL;
  789. trunused = NULL;
  790. if( !pos ) trans = abc_transpose(voice);
  791. for( tr=h->track; tr; tr=tr->next ) {
  792. if( tr->vno == 0 ) {
  793. if( !trunused ) trunused = tr; // must reuse mastertrack (h->track) as first
  794. }
  795. else {
  796. if( !strncasecmp(tr->v, vc, 20) ) {
  797. if( tr->vpos == pos )
  798. return tr;
  799. trans = tr->transpose;
  800. global_octave_shift = tr->octave_shift;
  801. voiceno = tr->vno;
  802. instrno = tr->instr;
  803. channo = tr->chan;
  804. }
  805. }
  806. prev = tr;
  807. }
  808. if( trunused ) {
  809. tr = trunused;
  810. if( pos ) {
  811. tr->vno = voiceno;
  812. tr->instr = instrno;
  813. tr->chan = channo;
  814. }
  815. else {
  816. global_voiceno++;
  817. tr->vno = global_voiceno;
  818. tr->instr = 1;
  819. tr->chan = 0;
  820. }
  821. tr->vpos = pos;
  822. tr->tiedvpos = pos;
  823. strncpy(tr->v, vc, 20);
  824. tr->v[20] = '\0';
  825. tr->mute = 0;
  826. tr->transpose = trans;
  827. tr->octave_shift = global_octave_shift;
  828. tr->volume = h->track->volume;
  829. tr->tienote = NULL;
  830. tr->legato = 0;
  831. return tr;
  832. }
  833. tr = abc_new_track(h, vc, pos);
  834. if( pos ) {
  835. tr->vno = voiceno;
  836. tr->instr = instrno;
  837. tr->chan = channo;
  838. }
  839. tr->transpose = trans;
  840. tr->octave_shift = global_octave_shift;
  841. if( prev ) prev->next = tr;
  842. else h->track = tr;
  843. return tr;
  844. }
  845. // =============================================================================
  846. static ABCTRACK *abc_check_track(ABCHANDLE *h, ABCTRACK *tp)
  847. // =============================================================================
  848. {
  849. if( !tp ) {
  850. tp = abc_locate_track(h, "", 0); // must work for voiceless abc too...
  851. tp->transpose = h->ktrans;
  852. }
  853. return tp;
  854. }
  855. static void abc_add_capo(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  856. {
  857. ABCEVENT *e;
  858. char d[6];
  859. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  860. d[cmdflag] = 1;
  861. d[command] = cmdcapo;
  862. e = abc_new_event(h, tracktime, d);
  863. tp->capostart = e;
  864. abc_add_event(h, tp, e); // do this last (recursion danger)
  865. }
  866. static void abc_add_segno(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  867. {
  868. ABCEVENT *e;
  869. char d[6];
  870. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  871. d[cmdflag] = 1;
  872. d[command] = cmdsegno;
  873. e = abc_new_event(h, tracktime, d);
  874. abc_add_event(h, tp, e);
  875. }
  876. static void abc_add_coda(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  877. {
  878. ABCEVENT *e;
  879. char d[6];
  880. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  881. d[cmdflag] = 1;
  882. d[command] = cmdcoda;
  883. e = abc_new_event(h, tracktime, d);
  884. abc_add_event(h, tp, e);
  885. }
  886. static void abc_add_fine(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  887. {
  888. ABCEVENT *e;
  889. char d[6];
  890. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  891. d[cmdflag] = 1;
  892. d[command] = cmdfine;
  893. e = abc_new_event(h, tracktime, d);
  894. abc_add_event(h, tp, e);
  895. }
  896. static void abc_add_tocoda(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  897. {
  898. ABCEVENT *e;
  899. char d[6];
  900. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  901. d[cmdflag] = 1;
  902. d[command] = cmdtocoda;
  903. e = abc_new_event(h, tracktime, d);
  904. abc_add_event(h, tp, e);
  905. }
  906. // first track is dirigent, remove all control events from other tracks
  907. // to keep the information where the events should be relative to note events
  908. // in the same tick the ticks are octated and four added for note events
  909. // the control events that come before the note events get a decremented tick,
  910. // those that come after get an incremented tick, for example:
  911. // ctrl ctrl note ctrl ctrl note
  912. // original: t t t t t+1 t+1
  913. // recoded: 8t+1 8t+2 8t+4 8t+5 8t+11 8t+12
  914. static void abc_remove_unnecessary_events(ABCHANDLE *h)
  915. {
  916. ABCTRACK *tp,*ptp;
  917. ABCEVENT *ep, *el;
  918. uint32_t ct, et;
  919. int d;
  920. ptp = NULL;
  921. for( tp=h->track; tp; tp=tp->next ) {
  922. el = NULL;
  923. ep = tp->head;
  924. ct = 0;
  925. d = -3;
  926. while( ep ) {
  927. et = ep->tracktick;
  928. ep->tracktick <<= 3;
  929. ep->tracktick += 4;
  930. if( ep->flg == 1 ) {
  931. ep->tracktick += d;
  932. d++;
  933. if( d == 0 ) d = -1;
  934. if( d == 4 ) d = 3;
  935. if( tp!=h->track ) ep->cmd = cmdhide;
  936. switch( ep->cmd ) {
  937. case cmdhide:
  938. case cmdsync:
  939. if( el ) {
  940. el->next = ep->next;
  941. if( !el->next )
  942. tp->tail = el;
  943. _mm_free(h->trackhandle,ep);
  944. ep = el->next;
  945. }
  946. else {
  947. tp->head = ep->next;
  948. if( !tp->head )
  949. tp->tail = NULL;
  950. _mm_free(h->trackhandle,ep);
  951. ep = tp->head;
  952. }
  953. break;
  954. default:
  955. el = ep;
  956. ep = ep->next;
  957. break;
  958. }
  959. }
  960. else {
  961. el = ep;
  962. ep = ep->next;
  963. d = 1;
  964. }
  965. if( et > ct )
  966. d = -3;
  967. ct = et;
  968. }
  969. if( !tp->head ) { // no need to keep empty tracks...
  970. if( ptp ) {
  971. ptp->next = tp->next;
  972. _mm_free(h->trackhandle,tp);
  973. tp = ptp;
  974. }
  975. else if (tp->next) {
  976. h->track = tp->next;
  977. _mm_free(h->trackhandle,tp);
  978. tp = h->track;
  979. } else {
  980. break;
  981. }
  982. }
  983. ptp = tp; // remember previous track
  984. }
  985. }
  986. // set ticks back, and handle partbreaks
  987. static void abc_retick_events(ABCHANDLE *h)
  988. {
  989. ABCTRACK *tp;
  990. ABCEVENT *ep;
  991. uint32_t et, tt=0, at = abcticks(64 * h->speed);
  992. for( tp=h->track; tp; tp=tp->next ) {
  993. // make ticks relative
  994. tt = 0;
  995. for( ep=tp->head; ep; ep=ep->next ) {
  996. et = ep->tracktick >> 3;
  997. ep->tracktick = et - tt;
  998. tt = et;
  999. }
  1000. // make ticks absolute again, skipping no-op partbreaks
  1001. tt = 0;
  1002. for( ep=tp->head; ep; ep=ep->next ) {
  1003. ep->tracktick += tt;
  1004. tt = ep->tracktick;
  1005. if( ep->flg == 1 && ep->cmd == cmdpartbrk ) {
  1006. if( tt % at ) {
  1007. tt += at;
  1008. tt /= at;
  1009. tt *= at;
  1010. ep->tracktick -= abcticks(h->speed); // break plays current row
  1011. }
  1012. else ep->cmd = cmdhide;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. // make sure every track has the control events it needs, this way it is not
  1018. // necessary to have redundant +segno+ +D.C.+ etc in the voices, the first voice
  1019. // is the master, it is pointed to by the member 'track' in the ABCHANDLE
  1020. static void abc_synchronise_tracks(ABCHANDLE *h)
  1021. {
  1022. ABCTRACK *tp;
  1023. uint32_t tm; // tracktime in master
  1024. ABCEVENT *em, *es, *et, *ec; // events in master, slave, slave temporary and copied event
  1025. if( !h || !h->track ) return;
  1026. abc_remove_unnecessary_events(h);
  1027. for( tp = h->track->next; tp; tp = tp->next ) {
  1028. for( em=h->track->head; em; em=em->next ) {
  1029. if( em->flg == 1 ) { // some kind of control event
  1030. switch( em->cmd ) {
  1031. case cmdchord:
  1032. case cmdhide:
  1033. case cmdtempo:
  1034. case cmdsync:
  1035. break;
  1036. default: // check to see if copy is necessary
  1037. ec = abc_copy_event(h, em);
  1038. tm = em->tracktick;
  1039. es = tp->head; // allways search from the begin...
  1040. for( et=es; et && et->tracktick <= tm; et=et->next )
  1041. es = et;
  1042. if( es == NULL || es->tracktick > tm ) { // special case: head of track
  1043. ec->next = es;
  1044. tp->head = ec;
  1045. }
  1046. else {
  1047. ec->next = es->next;
  1048. es->next = ec;
  1049. }
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. }
  1055. abc_retick_events(h);
  1056. }
  1057. static void abc_add_event(ABCHANDLE *h, ABCTRACK *tp, ABCEVENT *e)
  1058. {
  1059. if( !tp->capostart ) abc_add_capo(h, tp, global_songstart);
  1060. if( tp->tail ) {
  1061. tp->tail->next = e;
  1062. tp->tail = e;
  1063. }
  1064. else {
  1065. tp->head = e;
  1066. tp->tail = e;
  1067. }
  1068. }
  1069. static void abc_add_partbreak(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  1070. {
  1071. ABCEVENT *e;
  1072. char d[6];
  1073. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  1074. d[cmdflag] = 1;
  1075. d[command] = cmdpartbrk;
  1076. e = abc_new_event(h, tracktime, d);
  1077. abc_add_event(h, tp, e);
  1078. }
  1079. static void abc_add_tempo_event(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime, int tempo)
  1080. {
  1081. ABCEVENT *e;
  1082. char d[6];
  1083. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  1084. d[cmdflag] = 1;
  1085. d[command] = cmdtempo;
  1086. e = abc_new_event(h, tracktime, d);
  1087. e->lpar = tempo;
  1088. abc_add_event(h, tp, e);
  1089. }
  1090. static void abc_add_noteoff(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  1091. {
  1092. ABCEVENT *e;
  1093. char d[6];
  1094. d[note] = 0;
  1095. d[octave] = 0;
  1096. d[smpno] = pat_gmtosmp(tp->instr);
  1097. d[volume] = 0;
  1098. d[effect] = 0;
  1099. d[effoper] = 0;
  1100. e = abc_new_event(h, tracktime, d);
  1101. abc_add_event(h, tp, e);
  1102. }
  1103. static int abc_dynamic_volume(ABCTRACK *tp, uint32_t tracktime, int vol)
  1104. {
  1105. uint32_t slidetime;
  1106. int voldelta;
  1107. if( tp->mute ) return 0;
  1108. if( tp->slidevol == 0 ) return vol;
  1109. if( tracktime < tp->slidevoltime ) return vol;
  1110. slidetime = modticks(tracktime - tp->slidevoltime);
  1111. voldelta = (slidetime * 15) / 64 / 6; // slide from say mf up to f in one pattern's time
  1112. if( tp->slidevol > -2 && voldelta > 15 ) voldelta = 15; // never to much dynamics
  1113. if( tp->slidevol > 0 ) vol += voldelta;
  1114. else vol -= voldelta;
  1115. if( vol < 2 ) vol = 2; // xmms divides this by 2....
  1116. if( vol > 127 ) vol = 127;
  1117. return vol;
  1118. }
  1119. static void abc_track_untie_short_chordnotes(ABCHANDLE *h)
  1120. {
  1121. ABCTRACK *tp;
  1122. int vn;
  1123. tp = h->tp;
  1124. vn = tp->vno;
  1125. for( tp = h->track; tp; tp = tp->next )
  1126. if( tp != h->tp && tp->vno == vn && tp->tienote ) {
  1127. abc_message("short notes in chord can not be tied:\n%s", h->line);
  1128. tp->tienote = 0;
  1129. }
  1130. }
  1131. static void abc_track_clear_tiednote(ABCHANDLE *h)
  1132. {
  1133. ABCTRACK *tp;
  1134. int vn;
  1135. tp = h->tp;
  1136. vn = tp->vno;
  1137. for( tp = h->track; tp; tp = tp->next )
  1138. if( tp->vno == vn ) tp->tienote = 0;
  1139. }
  1140. static void abc_track_clear_tiedvpos(ABCHANDLE *h)
  1141. {
  1142. ABCTRACK *tp;
  1143. int vn;
  1144. tp = h->tp;
  1145. vn = tp->vno;
  1146. for( tp = h->track; tp; tp = tp->next )
  1147. if( tp->vno == vn ) tp->tiedvpos = tp->vpos;
  1148. }
  1149. static ABCTRACK *abc_track_with_note_tied(ABCHANDLE *h, uint32_t tracktime, int n, int oct)
  1150. {
  1151. int vn, vp;
  1152. ABCTRACK *tp;
  1153. ABCEVENT *e;
  1154. tp = h->tp;
  1155. vn = tp->vno;
  1156. vp = tp->vpos;
  1157. for( tp = h->track; tp; tp = tp->next ) {
  1158. if( tp->vno == vn ) {
  1159. e = tp->tienote;
  1160. if( e && e->tracktick < tracktime
  1161. && e->par[octave] == oct && abs(e->par[note] - n) < 3 ) {
  1162. if( tp->vpos != vp ) tp->tiedvpos = vp;
  1163. h->tp = tp;
  1164. return tp;
  1165. }
  1166. }
  1167. }
  1168. tp = h->tp;
  1169. vp = tp->tiedvpos;
  1170. if( tp->vpos != vp ) {
  1171. // chord note track allready returned in previous call
  1172. for( tp = h->track; tp; tp = tp->next ) {
  1173. if( tp->vno == vn && tp->vpos == vp ) {
  1174. tp->tiedvpos = h->tp->vpos;
  1175. h->tp = tp;
  1176. return tp;
  1177. }
  1178. }
  1179. }
  1180. return h->tp;
  1181. }
  1182. static int abc_add_noteon(ABCHANDLE *h, int ch, const char *p, uint32_t tracktime, char *barkey, int vol, ABCEVENT_X_EFFECT fx, int fxop)
  1183. {
  1184. ABCEVENT *e;
  1185. ABCTRACK *tp;
  1186. int i,j,k;
  1187. int n,oct;
  1188. char d[6];
  1189. tp = h->tp;
  1190. switch(ch) {
  1191. case '^':
  1192. if( p[0] == '^' ) {
  1193. n = p[1];
  1194. i = 2;
  1195. ch = 'x';
  1196. }
  1197. else {
  1198. n = p[0];
  1199. i = 1;
  1200. }
  1201. break;
  1202. case '_':
  1203. if( p[0] == '_' ) {
  1204. n = p[1];
  1205. i = 2;
  1206. ch = 'b';
  1207. }
  1208. else {
  1209. n = p[0];
  1210. i = 1;
  1211. }
  1212. break;
  1213. case '=':
  1214. n = p[0];
  1215. i = 1;
  1216. break;
  1217. default:
  1218. n = ch;
  1219. i = 0;
  1220. break;
  1221. }
  1222. for( k=0; k<51; k++ ) {
  1223. if( n == barkey[k] )
  1224. break;
  1225. }
  1226. j = k;
  1227. if( k > 24 )
  1228. k -= 25; // had something like A# over Bb key F signature....
  1229. if( i ) {
  1230. // propagate accidentals if necessary
  1231. // DON'T do redundant accidentals they're always relative to C-scale
  1232. for( k=0; k<25; k++ ) {
  1233. if( n == sig[7][k] )
  1234. break;
  1235. }
  1236. if( k < 25 ) { // only do real notes...
  1237. switch(ch) {
  1238. case 'x':
  1239. k++;
  1240. case '^':
  1241. k++;
  1242. break;
  1243. case 'b':
  1244. k--;
  1245. case '_':
  1246. k--;
  1247. break;
  1248. case '=':
  1249. break;
  1250. }
  1251. if( j < 25 ) // was it not A# over Bb?
  1252. barkey[j] = ' ';
  1253. barkey[k] = n;
  1254. }
  1255. }
  1256. oct = 3; // ABC note pitch C is C4 and pitch c is C5
  1257. if( k < 25 ) {
  1258. k += tp->transpose;
  1259. while( k > 12 ) {
  1260. oct++;
  1261. k -= 12;
  1262. }
  1263. while( k < 0 ) {
  1264. oct--;
  1265. k += 12;
  1266. }
  1267. d[note] = 23 + k; // C0 is midi notenumber 24
  1268. }
  1269. else
  1270. d[note] = 0; // someone has doen ^X3 or something like it...
  1271. while( p[i] && strchr(",'",p[i]) ) {
  1272. if( p[i]==',' ) oct--;
  1273. else oct++;
  1274. i++;
  1275. tp->octave_shift = 0; // forget we ever had to look at it
  1276. }
  1277. if( tp->octave_shift )
  1278. tp->transpose += 12 * tp->octave_shift;
  1279. oct += tp->octave_shift;
  1280. tp->octave_shift = 0; // after the first note we never have to look at it again
  1281. if( oct < 0 ) oct = 0;
  1282. if( oct > 9 ) oct = 9;
  1283. d[octave] = oct;
  1284. d[smpno] = pat_gmtosmp(tp->instr);
  1285. d[volume] = abc_dynamic_volume(tp, tracktime, vol);
  1286. d[effect] = fx; // effect
  1287. d[effoper] = fxop;
  1288. tp = abc_track_with_note_tied(h, tracktime, d[note], oct);
  1289. if( tp->tienote ) {
  1290. if( tp->tienote->par[note] != d[note] ) {
  1291. if( abs(tp->tienote->par[note] - d[note]) < 3 ) {
  1292. // may be tied over bar symbol, recover local accidental to barkey
  1293. k = tp->tienote->par[note] - 23 - tp->transpose;
  1294. while( k < 0 ) k += 12;
  1295. while( k > 12 ) k -= 12;
  1296. if( (isupper(n) && barkey[k+12] == ' ') || (islower(n) && barkey[k] == ' ') ) {
  1297. barkey[j] = ' ';
  1298. if( isupper(n) )
  1299. barkey[k] = n;
  1300. else
  1301. barkey[k+12] = n;
  1302. d[note] = tp->tienote->par[note];
  1303. d[octave] = tp->tienote->par[octave];
  1304. }
  1305. }
  1306. }
  1307. }
  1308. if( tp->tienote
  1309. && tp->tienote->par[note] == d[note]
  1310. && tp->tienote->par[octave] == d[octave] ) {
  1311. for( e = tp->tienote; e; e = e->next ) {
  1312. if( e->par[note] == 0 && e->par[octave] == 0 ) { // undo noteoff
  1313. e->flg = 1;
  1314. e->cmd = cmdhide;
  1315. e->lpar = 0;
  1316. break;
  1317. }
  1318. }
  1319. tp->tienote->tiednote = 1; // mark him for the pattern writers
  1320. for( j=i; isdigit(p[j]) || p[j]=='/'; j++ ) ; // look ahead to see if this one is tied too
  1321. if( p[j] != '-' ) // is this note tied too?
  1322. tp->tienote = NULL; // if not the tie ends here...
  1323. return i;
  1324. }
  1325. tp->tienote = NULL;
  1326. if( tp->tail
  1327. && tp->tail->tracktick == tracktime
  1328. && tp->tail->par[note] == 0
  1329. && tp->tail->par[octave] == 0 ) {
  1330. for( j=0; j<6; j++ )
  1331. tp->tail->par[j] = d[j];
  1332. }
  1333. else {
  1334. e = abc_new_event(h, tracktime, d);
  1335. abc_add_event(h, tp, e);
  1336. }
  1337. if( i > 0 && p[i-1] == '"' ) {
  1338. i--; // someone coded a weird note like ^"E"
  1339. abc_message("strange note encountered scanning %s", h->line);
  1340. }
  1341. return i;
  1342. }
  1343. static void abc_add_dronenote(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime, int nnum, int vol)
  1344. {
  1345. ABCEVENT *e;
  1346. int j,k;
  1347. int oct;
  1348. char d[6];
  1349. oct = -1; // ABC note pitch C is C4 and pitch c is C5
  1350. k = nnum + 1;
  1351. while( k > 12 ) {
  1352. oct++;
  1353. k -= 12;
  1354. }
  1355. while( k < 0 ) {
  1356. oct--;
  1357. k += 12;
  1358. }
  1359. if( oct < 0 ) oct = 0;
  1360. d[note] = 23 + k; // C0 is midi notenumber 24
  1361. d[octave] = oct;
  1362. d[smpno] = pat_gmtosmp(tp->instr);
  1363. d[volume] = abc_dynamic_volume(tp, tracktime, vol);
  1364. d[effect] = 0; // effect
  1365. d[effoper] = 0;
  1366. if( tp->tail
  1367. && tp->tail->tracktick == tracktime
  1368. && tp->tail->par[note] == 0
  1369. && tp->tail->par[octave] == 0 ) {
  1370. for( j=0; j<6; j++ )
  1371. tp->tail->par[j] = d[j];
  1372. }
  1373. else {
  1374. e = abc_new_event(h, tracktime, d);
  1375. abc_add_event(h, tp, e);
  1376. }
  1377. }
  1378. static void abc_add_chordnote(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime, int nnum, int vol)
  1379. {
  1380. abc_add_dronenote(h, tp, tracktime, nnum + 23, tp->mute? 0: vol);
  1381. }
  1382. static void abc_add_drumnote(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime, int nnum, int vol)
  1383. {
  1384. abc_add_dronenote(h, tp, tracktime, nnum, tp->mute? 0: vol);
  1385. }
  1386. static void abc_add_variant_start(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime, int n)
  1387. {
  1388. ABCEVENT *e;
  1389. char d[6];
  1390. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  1391. d[cmdflag] = 1;
  1392. d[command] = cmdvariant;
  1393. e = abc_new_event(h, tracktime, d);
  1394. e->lpar = 1<<n;
  1395. abc_add_event(h, tp, e);
  1396. }
  1397. static void abc_add_variant_choise(ABCTRACK *tp, int n)
  1398. {
  1399. tp->tail->lpar |= 1<<n;
  1400. }
  1401. static void abc_add_chord(const char *p, ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  1402. {
  1403. ABCEVENT *e;
  1404. char d[6];
  1405. char s[8];
  1406. int i;
  1407. const char *n = " C D EF G A Bc d ef g a b";
  1408. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  1409. d[cmdflag] = 1;
  1410. d[command] = cmdchord;
  1411. if( p[0] == '(' ) p++; // chord between parens like: (C)
  1412. for( i=0; n[i]; i++ )
  1413. if( *p == n[i] ) {
  1414. d[chordnote] = i;
  1415. break;
  1416. }
  1417. p++;
  1418. switch(*p) {
  1419. case 'b':
  1420. d[chordnote]--;
  1421. p++;
  1422. break;
  1423. case '#':
  1424. d[chordnote]++;
  1425. p++;
  1426. break;
  1427. }
  1428. d[chordbase] = d[chordnote];
  1429. for( i=0; i < sizeof(s) - 1 && p[i] && p[i] != '"' && p[i] != '/' && p[i] != '(' && p[i] != ')' && p[i] != ' '; i++ ) s[i] = p[i];
  1430. s[i] = '\0';
  1431. p = &p[i];
  1432. if( *p=='/' ) {
  1433. p++;
  1434. for( i=0; n[i]; i++ )
  1435. if( *p == n[i] ) {
  1436. d[chordbase] = i;
  1437. break;
  1438. }
  1439. p++;
  1440. switch(*p) {
  1441. case 'b':
  1442. d[chordbase]--;
  1443. p++;
  1444. break;
  1445. case '#':
  1446. d[chordbase]++;
  1447. p++;
  1448. break;
  1449. }
  1450. }
  1451. for( i=0; i<chordsnamed; i++ )
  1452. if( !strcmp(s, chordname[i]) ) {
  1453. d[chordnum] = i;
  1454. break;
  1455. }
  1456. if( i==chordsnamed ) {
  1457. abc_message("Failure: unrecognized chordname %s",s);
  1458. return;
  1459. }
  1460. e = abc_new_event(h, tracktime, d);
  1461. abc_add_event(h, tp, e);
  1462. }
  1463. static void abc_add_setloop(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  1464. {
  1465. ABCEVENT *e;
  1466. char d[6];
  1467. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  1468. d[cmdflag] = 1;
  1469. d[command] = cmdloop;
  1470. e = abc_new_event(h, tracktime, d);
  1471. abc_add_event(h, tp, e);
  1472. }
  1473. static void abc_fade_track(ABCTRACK *tp, ABCEVENT *e)
  1474. {
  1475. while(e) {
  1476. if( e->flg != 1 && e->par[note] != 0 )
  1477. e->par[volume] = abc_dynamic_volume(tp, e->tracktick, e->par[volume]);
  1478. e = e->next;
  1479. }
  1480. }
  1481. static void abc_add_setjumploop(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime, ABCEVENT_JUMPTYPE j)
  1482. {
  1483. ABCEVENT *e;
  1484. char d[8];
  1485. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  1486. d[cmdflag] = 1;
  1487. d[command] = cmdjump;
  1488. d[jumptype] = j;
  1489. e = abc_new_event(h, tracktime, d);
  1490. abc_add_event(h, tp, e);
  1491. }
  1492. static void abc_add_sync(ABCHANDLE *h, ABCTRACK *tp, uint32_t tracktime)
  1493. {
  1494. ABCEVENT *e;
  1495. char d[6];
  1496. e = tp->tail;
  1497. if( e && e->tracktick == tracktime ) return;
  1498. if( e && e->flg == 1 && e->cmd == cmdsync ) {
  1499. e->tracktick = tracktime;
  1500. return;
  1501. }
  1502. d[0] = d[1] = d[2] = d[3] = d[4] = d[5] = 0;
  1503. d[cmdflag] = 1;
  1504. d[command] = cmdsync;
  1505. e = abc_new_event(h, tracktime, d);
  1506. abc_add_event(h, tp, e);
  1507. }
  1508. static void abc_add_gchord_syncs(ABCHANDLE *h, ABCTRACK *tpc, uint32_t tracktime)
  1509. {
  1510. ABCTRACK *tp;
  1511. int i;
  1512. for( i = GCHORDBPOS; i < DRUMPOS; i++ ) {
  1513. tp = abc_locate_track(h, tpc->v, i);
  1514. abc_add_sync(h,tp,tracktime);
  1515. }
  1516. }
  1517. static void abc_add_drum_sync(ABCHANDLE *h, ABCTRACK *tpr, uint32_t tracktime)
  1518. {
  1519. ABCTRACK *tp;
  1520. tp = abc_locate_track(h, tpr->v, DRUMPOS);
  1521. abc_add_sync(h,tp,tracktime);
  1522. }
  1523. static int abc_getnumber(const char *p, int *number)
  1524. {
  1525. int i,h;
  1526. i = 0;
  1527. h = 0;
  1528. while( isdigit(p[i]) ) {
  1529. h = 10 * h + p[i] - '0';
  1530. i++;
  1531. }
  1532. if( i==0 )
  1533. *number = 1;
  1534. else
  1535. *number = h;
  1536. return i;
  1537. }
  1538. static int abc_getexpr(const char *p, int *number)
  1539. {
  1540. int i, term, total;
  1541. i = 0;
  1542. while( isspace(p[i]) )
  1543. i++;
  1544. if( p[i] == '(' ) {
  1545. i += abc_getexpr(p+i+1, number);
  1546. while( p[i] && (p[i] != ')') )
  1547. i++;
  1548. return i;
  1549. }
  1550. i += abc_getnumber(p+i, &total);
  1551. while( isspace(p[i]) )
  1552. i++;
  1553. while( p[i] == '+' ) {
  1554. i += 1 + abc_getexpr(p+i+1, &term);
  1555. total += term;
  1556. while( isspace(p[i]) )
  1557. i++;
  1558. }
  1559. *number = total;
  1560. return i;
  1561. }
  1562. static int abc_notelen(const char *p, int *len, int *div)
  1563. {
  1564. int i,h,k;
  1565. i = abc_getnumber(p,len);
  1566. h = 1;
  1567. while( p[i] == '/' ) {
  1568. h *= 2;
  1569. i++;
  1570. }
  1571. if( isdigit(p[i]) ) {
  1572. h /= 2;
  1573. i += abc_getnumber(p+i,&k);
  1574. }
  1575. else k = 1;
  1576. *div = h * k;
  1577. return i;
  1578. }
  1579. static int abc_brokenrithm(const char *p, int *nl, int *nd, int *b, int hornpipe)
  1580. {
  1581. switch( *b ) {
  1582. case '<':
  1583. *nl *= 3;
  1584. *nd *= 2;
  1585. hornpipe = 0;
  1586. break;
  1587. case '>':
  1588. *nd *= 2;
  1589. hornpipe = 0;
  1590. break;
  1591. }
  1592. *b = *p;
  1593. switch( *b ) {
  1594. case '>':
  1595. *nl *= 3;
  1596. *nd *= 2;
  1597. return 1;
  1598. case '<':
  1599. *nd *= 2;
  1600. return 1;
  1601. default:
  1602. *b = 0;
  1603. break;
  1604. }
  1605. if( hornpipe ) { // still true then make 1/8 notes broken rithme
  1606. if( *nl == 1 && *nd == 1 ) {
  1607. *b = '>';
  1608. *nl = 3;
  1609. *nd = 2;
  1610. }
  1611. }
  1612. return 0;
  1613. }
  1614. // put p notes in the time q for the next r notes
  1615. static int abc_tuplet(int *nl, int *nd, int p, int q, int r)
  1616. {
  1617. if( !r ) return 0;
  1618. *nl *= q;
  1619. *nd *= p;
  1620. return r - 1;
  1621. }
  1622. // evaluate [Q:"string" n1/m1 n2/m2 n3/m3 n4/m4=bpm "string"]
  1623. // minimal form [Q:"string"]
  1624. // most used form [Q: 1/4=120]
  1625. static int abc_extract_tempo(const char *p, int invoice)
  1626. {
  1627. int nl, nd, ns, in, tempo;
  1628. int nl1=0, nd1, notes, state;
  1629. const char *q;
  1630. in = 0;
  1631. nl = 0;
  1632. nd = 1;
  1633. ns = 120;
  1634. notes = 0;
  1635. state = 0;
  1636. for( q=p; *q; q++ ) {
  1637. if( in ) {
  1638. if( *q=='"' )
  1639. in = 0;
  1640. }
  1641. else {
  1642. if( *q == ']' ) break;
  1643. switch( *q ) {
  1644. case '"':
  1645. in = 1;
  1646. break;
  1647. case '/':
  1648. notes++;
  1649. state = 1;
  1650. nl1 = ns;
  1651. break;
  1652. case '=':
  1653. break;
  1654. default:
  1655. if( isdigit(*q) ) {
  1656. if( state ) {
  1657. q+=abc_getnumber(q,&nd1)-1;
  1658. state = 0;
  1659. nl = nl * nd1 + nl1 * nd;
  1660. nd = nd * nd1;
  1661. }
  1662. else
  1663. q+=abc_getnumber(q,&ns)-1;
  1664. }
  1665. break;
  1666. }
  1667. }
  1668. }
  1669. if( !notes ) {
  1670. nl = 1;
  1671. nd = 4;
  1672. }
  1673. if( !nd ) tempo = 120;
  1674. else tempo = ns * nl * 4 / nd; // mod tempo is really BPM where one B is equal to a quartnote
  1675. if( tempo <= 0 )
  1676. tempo = 120;
  1677. if( invoice ) {
  1678. nl = global_tempo_factor;
  1679. nd = global_tempo_divider;
  1680. }
  1681. global_tempo_factor = 1;
  1682. global_tempo_divider = 1;
  1683. while( tempo/global_tempo_divider > 255 )
  1684. global_tempo_divider++;
  1685. tempo /= global_tempo_divider;
  1686. while( tempo * global_tempo_factor < 256 )
  1687. global_tempo_factor++;
  1688. global_tempo_factor--;
  1689. tempo *= global_tempo_factor;
  1690. if( tempo * 3 < 512 ) {
  1691. global_tempo_factor *= 3;
  1692. global_tempo_divider *= 2;
  1693. tempo = (tempo * 3) / 2;
  1694. }
  1695. if( invoice ) {
  1696. if( nl != global_tempo_factor || nd != global_tempo_divider ) {
  1697. ns = (tempo * nl * global_tempo_divider) / (nd * global_tempo_factor);
  1698. if( ns > 31 && ns < 256 ) {
  1699. tempo = ns;
  1700. global_tempo_factor = nl;
  1701. global_tempo_divider = nd;
  1702. }
  1703. else
  1704. abc_message("Failure: inconvenient tempo change in middle of voice (%s)", p);
  1705. }
  1706. }
  1707. return tempo;
  1708. }
  1709. static void abc_set_parts(char **d, char *p)
  1710. {
  1711. int i,j,k,m,n;
  1712. char *q;
  1713. #ifdef NEWMIKMOD
  1714. static MM_ALLOC *h;
  1715. if( *d ) _mmalloc_close(h);
  1716. #else
  1717. if( *d ) free(*d);
  1718. #endif
  1719. *d = 0;
  1720. if( !p ) return;
  1721. for( i=0; p[i] && p[i] != '%'; i++ ) {
  1722. if( !strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ().0123456789 ",p[i]) ) {
  1723. abc_message("invalid characters in part string scanning P:%s", p);
  1724. return;
  1725. }
  1726. }
  1727. #ifdef NEWMIKMOD
  1728. h = _mmalloc_create("Load_ABC_parts", NULL);
  1729. #endif
  1730. // decode constructs like "((AB)2.(CD)2)3.(AB)E2" to "ABABCDCDABABCDCDABABCDCDABEE"
  1731. // first compute needed storage...
  1732. j=0;
  1733. k=0;
  1734. for( i=0; p[i] && p[i] != '%'; i++ ) {
  1735. if( isupper(p[i]) ) {
  1736. j++;
  1737. }
  1738. if( isdigit(p[i]) ) {
  1739. n=abc_getnumber(p+i,&k);
  1740. if( k == 0 )
  1741. k = 1;
  1742. if( p[i-1] == ')' )
  1743. j *= k; // never mind multiple parens, just take the worst case
  1744. else
  1745. j += k-1;
  1746. i += n-1;
  1747. }
  1748. }
  1749. q = (char *)_mm_calloc(h, j+1, sizeof(char)); // enough storage for the worst case
  1750. // now copy bytes from p to *d, taking parens and digits in account
  1751. j = 0;
  1752. for( i=0; p[i] && p[i] != '%'; i++ ) {
  1753. if( isdigit(p[i]) || isupper(p[i]) || p[i] == '(' || p[i] == ')' ) {
  1754. if( p[i] == ')' ) {
  1755. for( n=j; n > 0 && q[n-1] != '('; n-- ) ; // find open paren in q
  1756. // q[n+1] to q[j] contains the substring that must be repeated
  1757. if( n > 0 ) {
  1758. for( k = n; k<j; k++ ) q[k-1] = q[k]; // shift to the left...
  1759. j--;
  1760. }
  1761. else {
  1762. abc_message("Warning: Unbalanced right parens in P: definition %s",p);
  1763. break;
  1764. }
  1765. n = j - n + 1; // number of repeatable characters
  1766. i += abc_getnumber(p+i+1,&k);
  1767. while( k-- > 1 ) {
  1768. for( m=0; m<n; m++ ) {
  1769. q[j] = q[j-n];
  1770. j++;
  1771. }
  1772. }
  1773. continue;
  1774. }
  1775. if( isdigit(p[i]) ) {
  1776. n = abc_getnumber(p+i,&k);
  1777. i += n - 1;
  1778. while( k-- > 1 ) {
  1779. q[j] = q[j-1];
  1780. j++;
  1781. }
  1782. continue;
  1783. }
  1784. q[j] = p[i];
  1785. j++;
  1786. }
  1787. }
  1788. q[j] = '\0';
  1789. // remove any left over parens
  1790. for( i=0; i<j; i++ ) {
  1791. if( q[i] == '(' ) {
  1792. abc_message("Warning: Unbalanced left parens in P: definition %s",p);
  1793. for( k=i; k<j; k++ ) q[k] = q[k+1];
  1794. j--;
  1795. }
  1796. }
  1797. *d = q;
  1798. }
  1799. static void abc_appendpart(ABCHANDLE *h, ABCTRACK *tp, uint32_t pt1, uint32_t pt2)
  1800. {
  1801. ABCEVENT *e, *ec;
  1802. uint32_t dt;
  1803. dt = tp->tail->tracktick - pt1;
  1804. for( e=tp->head; e && e->tracktick <= pt2; e=e->next ) {
  1805. if( e->tracktick >= pt1 ) {
  1806. if( e->flg != 1 || e->cmd == cmdsync || e->cmd == cmdchord ) {
  1807. if( e != tp->tail ) {
  1808. // copy this event at tail
  1809. ec = abc_copy_event(h,e);
  1810. ec->tracktick += dt;
  1811. ec->part = '*';
  1812. tp->tail->next = ec;
  1813. tp->tail = ec;
  1814. }
  1815. }
  1816. }
  1817. }
  1818. abc_add_sync(h, tp, pt2 + dt); // make sure there is progression...
  1819. }
  1820. static uint32_t abc_pattracktime(ABCHANDLE *h, uint32_t tracktime)
  1821. {
  1822. ABCEVENT *e;
  1823. uint32_t dt,et,pt=abcticks(64 * h->speed);
  1824. if(!h || !h->track || !h->track->head ) return 0;
  1825. dt = 0;
  1826. for( e=h->track->head; e && e->tracktick <= tracktime; e=e->next ) {
  1827. if( e->flg == 1 && e->cmd == cmdpartbrk ) {
  1828. et = e->tracktick + dt;
  1829. if( et % pt ) {
  1830. et += pt;
  1831. et /= pt;
  1832. et *= pt;
  1833. dt = et - e->tracktick;
  1834. }
  1835. }
  1836. }
  1837. return (tracktime + dt);
  1838. }
  1839. static int abc_patno(ABCHANDLE *h, uint32_t tracktime)
  1840. {
  1841. return modticks(abc_pattracktime(h, tracktime)) / 64 / h->speed;
  1842. }
  1843. static void abc_stripoff(ABCHANDLE *h, ABCTRACK *tp, uint32_t tt)
  1844. {
  1845. ABCEVENT *e1, *e2;
  1846. e2 = NULL;
  1847. for( e1 = tp->head; e1 && e1->tracktick <= tt; e1=e1->next )
  1848. e2 = e1;
  1849. if( e2 ) {
  1850. e1 = e2->next;
  1851. tp->tail = e2;
  1852. e2->next = NULL;
  1853. }
  1854. else {
  1855. e1 = tp->tail;
  1856. tp->head = NULL;
  1857. tp->tail = NULL;
  1858. }
  1859. while( e1 ) {
  1860. e2 = e1->next;
  1861. _mm_free(h->trackhandle,e1);
  1862. e1 = e2;
  1863. }
  1864. }
  1865. static void abc_keeptiednotes(ABCHANDLE *h, uint32_t fromtime, uint32_t totime) {
  1866. ABCTRACK *tp;
  1867. ABCEVENT *e,*n,*f;
  1868. if( totime <= fromtime ) return;
  1869. for( tp=h->track; tp; tp=tp->next ) {
  1870. if( tp->vno ) { // if track is in use...
  1871. n = NULL;
  1872. for( e=tp->head; e && e->tracktick < fromtime; e = e->next )
  1873. if( e->flg != 1 ) n = e; // remember it when it is a note event
  1874. if( n && n->tiednote ) { // we've a candidate to tie over the break
  1875. while( e && e->tracktick < totime ) e=e->next; // skip to other part
  1876. if( e && e->tracktick == totime ) { // if this is on begin row of this part
  1877. f = NULL;
  1878. while( !f && e && e->tracktick == totime ) {
  1879. if( e->flg != 1 ) f = e;
  1880. e = e->next;
  1881. }
  1882. if( f && f->par[note] ) { // pfoeie, we've found a candidate
  1883. if( abs(n->par[note] - f->par[note]) < 3 ) { // undo the note on
  1884. f->flg = 1;
  1885. f->cmd = cmdhide;
  1886. f->lpar = 0;
  1887. }
  1888. }
  1889. }
  1890. }
  1891. }
  1892. }
  1893. }
  1894. static uint32_t abc_fade_tracks(ABCHANDLE *h, char *abcparts, uint32_t ptt[27])
  1895. {
  1896. ABCTRACK *tp;
  1897. ABCEVENT *e0;
  1898. char *p;
  1899. int vol;
  1900. uint32_t pt1, pt2;
  1901. uint32_t tt;
  1902. tt = h->track->tail->tracktick;
  1903. for( tp=h->track->next; tp; tp=tp->next ) {
  1904. if( !tp->tail ) abc_add_sync(h, tp, tt); // no empty tracks please...
  1905. if( tp->tail->tracktick > tt ) abc_stripoff(h, tp, tt); // should not happen....
  1906. if( tp->tail->tracktick < tt ) abc_add_sync(h, tp, tt);
  1907. }
  1908. for( tp=h->track; tp; tp=tp->next ) {
  1909. vol = 127;
  1910. e0 = tp->tail;
  1911. if( tp->slidevol != -2 ) {
  1912. tp->slidevol = -2;
  1913. tp->slidevoltime = e0->tracktick;
  1914. }
  1915. tp->mute = 0; // unmute track for safety, notes in a muted track already have zero volume...
  1916. while( vol > 5 ) {
  1917. for( p=abcparts; *p && vol > 5; p++ ) {
  1918. pt1 = ptt[*p-'A'];
  1919. pt2 = ptt[*p-'A'+1];
  1920. abc_appendpart(h, tp, pt1, pt2);
  1921. vol = abc_dynamic_volume(tp, tp->tail->tracktick, 127);
  1922. }
  1923. }
  1924. abc_fade_track(tp,e0);
  1925. }
  1926. return h->track->tail->tracktick;
  1927. }
  1928. static void abc_song_to_parts(ABCHANDLE *h, char **abcparts, BYTE partp[27][2])
  1929. {
  1930. uint32_t starttick;
  1931. ABCEVENT *e;
  1932. int i, fading, loop, normal, partno, partsegno, partloop, partcoda, parttocoda, partfine, skip, x, y;
  1933. int vmask[27],nextp[27];
  1934. uint32_t ptt[27];
  1935. char buf[256]; // must be enough, mod's cannot handle more than 240 patterns
  1936. char *pfade;
  1937. if( !h || !h->track || !h->track->capostart ) return;
  1938. strcpy(buf,"A"); // initialize our temporary array
  1939. i = 1;
  1940. loop = 1;
  1941. partno = 0;
  1942. partsegno = 0;
  1943. partloop = 0;
  1944. partcoda = -1;
  1945. parttocoda = -1;
  1946. partfine = -1;
  1947. starttick = h->track->capostart->tracktick;
  1948. ptt[0] = starttick;
  1949. vmask[0] = -1;
  1950. nextp[0] = 1;
  1951. for( e=h->track->capostart; e; e=e->next ) {
  1952. if( e->flg == 1 ) {
  1953. switch( e->cmd ) {
  1954. case cmdpartbrk:
  1955. if( e->tracktick > starttick) {
  1956. starttick = e->tracktick; // do not make empty parts
  1957. if( partno < 26 ) {
  1958. partno++;
  1959. ptt[partno] = starttick;
  1960. }
  1961. if( i < 255 ) buf[i++] = partno+'A';
  1962. vmask[partno] = -1;
  1963. nextp[partno] = partno+1;
  1964. }
  1965. break;
  1966. case cmdloop:
  1967. partloop = partno;
  1968. loop = 1; // start counting anew...
  1969. break;
  1970. case cmdvariant:
  1971. vmask[partno] = e->lpar;
  1972. break;
  1973. case cmdjump:
  1974. x = 0;
  1975. fading = 0;
  1976. normal = 0;
  1977. skip = 0;
  1978. pfade = &buf[i];
  1979. switch( e->par[jumptype] ) {
  1980. case jumpfade:
  1981. fading = 1;
  1982. case jumpnormal:
  1983. normal = 1;
  1984. x = partloop;
  1985. loop++;
  1986. break;
  1987. case jumpdsfade:
  1988. fading = 1;
  1989. case jumpdasegno:
  1990. x = partsegno;
  1991. break;
  1992. case jumpdcfade:
  1993. fading = 1;
  1994. case jumpdacapo:
  1995. x = 0;
  1996. break;
  1997. default:
  1998. x = 0;
  1999. break;
  2000. }
  2001. if( vmask[partno] != -1 ) nextp[partno] = x;
  2002. if( partno < 26 ) ptt[partno+1] = e->tracktick; // for handling ties over breaks
  2003. while( x <= partno ) {
  2004. if( skip == 1 && x == partcoda ) skip = 0;
  2005. y = !skip;
  2006. if( y ) {
  2007. if( !normal ) {
  2008. if( x == partfine ) skip = 2;
  2009. if( x == parttocoda ) skip = 1;
  2010. y = !skip;
  2011. }
  2012. if( !(vmask[x] & (1<<loop)) ) y = 0;
  2013. }
  2014. if( y ) {
  2015. if( i < 255 ) buf[i++] = x+'A';
  2016. if( nextp[x] != x + 1 ) loop++;
  2017. x = nextp[x];
  2018. }
  2019. else
  2020. x++;
  2021. }
  2022. if( fading && partno < 25 && i < 254 ) { // add single part with fading tracks
  2023. partno++;
  2024. ptt[partno] = e->tracktick;
  2025. buf[i] = '\0'; // close up pfade with zero byte
  2026. starttick = abc_fade_tracks(h, pfade, ptt);
  2027. buf[i++] = partno+'A';
  2028. partno++;
  2029. ptt[partno] = starttick;
  2030. buf[i++] = partno+'A'; // one extra to throw away...
  2031. e = h->track->tail; // this is the edge of the world captain...
  2032. }
  2033. break;
  2034. case cmdtocoda:
  2035. parttocoda = partno;
  2036. break;
  2037. case cmdcoda:
  2038. partcoda = partno;
  2039. break;
  2040. case cmdfine:
  2041. partfine = partno;
  2042. break;
  2043. case cmdsegno:
  2044. partsegno = partno;
  2045. break;
  2046. }
  2047. }
  2048. e->part = partno+'a'; // small caps for generated parts...
  2049. }
  2050. i--; // strip off last partno
  2051. if( partno > 0 ) partno--;
  2052. buf[i] = '\0';
  2053. if( i > 1 ) {
  2054. for( i=1; buf[i]; i++ ) {
  2055. if( buf[i] != buf[i-1] + 1 ) {
  2056. x = buf[i-1] - 'A';
  2057. y = buf[i] - 'A';
  2058. abc_keeptiednotes(h, ptt[x+1], ptt[y]);
  2059. }
  2060. }
  2061. }
  2062. starttick = h->track->tail->tracktick;
  2063. ptt[partno+1] = starttick;
  2064. for( i=0; i<=partno; i++ ) {
  2065. partp[i][0] = abc_patno(h, ptt[i]);
  2066. partp[i][1] = abc_patno(h, ptt[i+1]);
  2067. }
  2068. // calculate end point of last part
  2069. starttick = abc_pattracktime(h, starttick);
  2070. if( starttick % abcticks(64 * h->speed) )
  2071. partp[partno][1]++;
  2072. abc_set_parts(abcparts, buf);
  2073. }
  2074. // =====================================================================================
  2075. static char *abc_fgets(MMFILE *mmfile, char buf[], unsigned int bufsz)
  2076. // =====================================================================================
  2077. {
  2078. if( mmfeof(mmfile) ) return NULL;
  2079. mmfgets(buf,bufsz,mmfile);
  2080. return buf;
  2081. }
  2082. // =====================================================================================
  2083. static char *abc_fgetbytes(MMFILE *mmfile, char buf[], unsigned int bufsz)
  2084. // =====================================================================================
  2085. {
  2086. unsigned int i;
  2087. long pos;
  2088. if( mmfeof(mmfile) ) return NULL;
  2089. for( i=0; i<bufsz-2; i++ ) {
  2090. buf[i] = (char)mmfgetc(mmfile);
  2091. if( buf[i] == '\n' ) break;
  2092. if( buf[i] == '\r' ) {
  2093. pos = mmftell(mmfile);
  2094. if( mmfgetc(mmfile) != '\n' ) mmfseek(mmfile, pos, SEEK_SET);
  2095. buf[i] = '\n';
  2096. break;
  2097. }
  2098. }
  2099. if( i != bufsz-2 && buf[i] == '\n' ) i++;
  2100. buf[i] = '\0';
  2101. return buf;
  2102. }
  2103. static void abc_substitute(ABCHANDLE *h, char *target, char *s)
  2104. {
  2105. char *p, *q;
  2106. int i;
  2107. int l = strlen(target);
  2108. int n = strlen(s);
  2109. if (l <= 0 ||n <= 0 || strstr(s, target))
  2110. return;
  2111. while( (p=strstr(h->line, target)) ) {
  2112. if( (i=strlen(h->line)) + n - l >= (int)h->len ) {
  2113. h->line = (char *)_mm_recalloc(h->allochandle, h->line, h->len<<1, sizeof(char));
  2114. h->len <<= 1;
  2115. p=strstr(h->line, target);
  2116. }
  2117. if( n > l ) {
  2118. for( q=&h->line[i]; q>p; q-- ) q[n-l] = q[0];
  2119. for( q=s; *q; q++ ) *p++ = *q;
  2120. }
  2121. else {
  2122. strcpy(p,s);
  2123. strcat(p,p+l);
  2124. }
  2125. }
  2126. }
  2127. static void abc_preprocess(ABCHANDLE *h, ABCMACRO *m)
  2128. {
  2129. int i, j, k, l, a, b;
  2130. if( m->n ) {
  2131. k = m->n - m->name;
  2132. for( i=0; i<14; i++ ) {
  2133. char t[strlen(m->name) + 1];
  2134. strcpy(t, m->name);
  2135. t[k] = "CDEFGABcdefgab"[i];
  2136. l = strlen(m->subst);
  2137. char s[2 * l + 1];
  2138. char *p = s;
  2139. for( j=0; j<l; j++ ) {
  2140. a = m->subst[j];
  2141. if( a > 'g' && islower(a) ) {
  2142. b = a - 'n';
  2143. a = "CDEFGABCDEFGABcdefgabcdefgab"[i+b+7];
  2144. *p++ = a;
  2145. if( i+b < 0 )
  2146. *p++ = ',';
  2147. else if( i+b > 13 )
  2148. *p++ = '\'';
  2149. }
  2150. else *p++ = a;
  2151. }
  2152. *p = '\0';
  2153. abc_substitute(h, t, s);
  2154. }
  2155. }
  2156. else
  2157. abc_substitute(h, m->name, m->subst);
  2158. }
  2159. static char *abc_gets(ABCHANDLE *h, MMFILE *mmfile)
  2160. {
  2161. int i;
  2162. ABCMACRO *mp;
  2163. if( !h->len ) {
  2164. h->len = 64; // initial line size, adequate for most abc's
  2165. h->line = (char *)_mm_calloc(h->allochandle, h->len, sizeof(char));
  2166. }
  2167. if( abc_fgetbytes(mmfile, h->line, h->len) ) {
  2168. while( (i=strlen(h->line)) > (int)(h->len - 3) ) {
  2169. // line too short, double it
  2170. h->line = (char *)_mm_recalloc(h->allochandle, h->line, h->len<<1, sizeof(char));
  2171. if( h->line[i-1] != '\n' )
  2172. abc_fgetbytes(mmfile, &h->line[i], h->len);
  2173. h->len <<= 1;
  2174. }
  2175. h->line[i-1] = '\0'; // strip off newline
  2176. for( mp=h->macro; mp; mp=mp->next )
  2177. abc_preprocess(h,mp);
  2178. return h->line;
  2179. }
  2180. return NULL;
  2181. }
  2182. static int abc_parse_decorations(ABCHANDLE *h, ABCTRACK *tp, const char *p)
  2183. {
  2184. int vol=0;
  2185. if( !strncmp(p,"mp",2) ) vol = 75;
  2186. if( !strncmp(p,"mf",2) ) vol = 90;
  2187. if( !strncmp(p,"sfz",3) ) vol = 100;
  2188. if( *p == 'p' ) {
  2189. vol = 60;
  2190. while( *p++ == 'p' ) vol -= 15;
  2191. if( vol < 1 ) vol = 1;
  2192. }
  2193. if( *p == 'f' ) {
  2194. vol = 105;
  2195. while( *p++ == 'f' ) vol += 15;
  2196. if( vol > 135 ) vol = 127; // ffff
  2197. if( vol > 127 ) vol = 125; // fff
  2198. }
  2199. if( vol ) {
  2200. tp->volume = vol;
  2201. if( tp == h->track ) { // copy volume over to all voice tracks
  2202. for( ; tp; tp=tp->next ) {
  2203. if( tp->vpos == 0 || tp->vpos > DRONEPOS2 ) tp->volume = vol;
  2204. }
  2205. tp = h->track;
  2206. }
  2207. }
  2208. return tp->volume;
  2209. }
  2210. // =====================================================================================
  2211. #ifdef NEWMIKMOD
  2212. BOOL ABC_Test(MMSTREAM *mmfile)
  2213. #else
  2214. BOOL CSoundFile::TestABC(const BYTE *lpStream, DWORD dwMemLength)
  2215. #endif
  2216. // =====================================================================================
  2217. {
  2218. char id[128];
  2219. // scan file for first K: line (last in header)
  2220. #ifdef NEWMIKMOD
  2221. _mm_fseek(mmfile,0,SEEK_SET);
  2222. while(abc_fgets(mmfile,id,128)) {
  2223. #else
  2224. MMFILE mmfile;
  2225. mmfile.mm = (char *)lpStream;
  2226. mmfile.sz = dwMemLength;
  2227. mmfseek(&mmfile,0,SEEK_SET);
  2228. int ppos = mmfile.pos;
  2229. while(abc_fgets(&mmfile,id,128)) {
  2230. #endif
  2231. if (id[0] == 0 && mmfile.pos < ppos + 120) return(0); //probably binary
  2232. if (id[0] == 0) continue; // blank line.
  2233. if (!abc_isvalidchar(id[0]) || !abc_isvalidchar(id[1])) {
  2234. return(0); // probably not an ABC.
  2235. }
  2236. if(id[0]=='K'
  2237. && id[1]==':'
  2238. && (isalpha(id[2]) || isspace(id[2])) ) return 1;
  2239. }
  2240. return 0;
  2241. }
  2242. // =====================================================================================
  2243. static ABCHANDLE *ABC_Init(void)
  2244. {
  2245. ABCHANDLE *retval;
  2246. char *p;
  2247. char buf[10];
  2248. #ifdef NEWMIKMOD
  2249. MM_ALLOC *allochandle;
  2250. allochandle = _mmalloc_create("Load_ABC", NULL);
  2251. retval = (ABCHANDLE *)_mm_calloc(allochandle, 1,sizeof(ABCHANDLE));
  2252. if( !retval ) return NULL;
  2253. retval->allochandle = allochandle;
  2254. allochandle = _mmalloc_create("Load_ABC_macros", NULL);
  2255. retval->macrohandle = allochandle;
  2256. allochandle = _mmalloc_create("Load_ABC_tracks", NULL);
  2257. retval->trackhandle = allochandle;
  2258. #else
  2259. retval = (ABCHANDLE *)calloc(1,sizeof(ABCHANDLE));
  2260. if( !retval ) return NULL;
  2261. #endif
  2262. retval->track = NULL;
  2263. retval->macro = NULL;
  2264. retval->umacro = NULL;
  2265. retval->beatstring = NULL;
  2266. retval->pickrandom = 0;
  2267. retval->len = 0;
  2268. retval->line = NULL;
  2269. strcpy(retval->gchord, "");
  2270. retval->barticks = 0;
  2271. p = getenv(ABC_ENV_NORANDOMPICK);
  2272. if( p ) {
  2273. if( isdigit(*p) )
  2274. retval->pickrandom = atoi(p);
  2275. if( *p == '-' ) {
  2276. #ifdef NEWMIKMOD
  2277. retval->pickrandom = atoi(p+1);
  2278. sprintf(buf,"-%ld",retval->pickrandom+1);
  2279. #else
  2280. retval->pickrandom = atoi(p+1)-1; // xmms preloads the file
  2281. sprintf(buf,"-%ld",retval->pickrandom+2);
  2282. #endif
  2283. setenv(ABC_ENV_NORANDOMPICK, buf, 1);
  2284. }
  2285. }
  2286. else {
  2287. srandom((uint32_t)time(0)); // initialize random generator with seed
  2288. retval->pickrandom = 1+(int)(10000.0*random()/(RAND_MAX+1.0));
  2289. // can handle pickin' from songbooks with 10.000 songs
  2290. #ifdef NEWMIKMOD
  2291. sprintf(buf,"-%ld",retval->pickrandom+1); // next in sequence
  2292. #else
  2293. sprintf(buf,"-%ld",retval->pickrandom); // xmms preloads the file
  2294. #endif
  2295. setenv(ABC_ENV_NORANDOMPICK, buf, 1);
  2296. }
  2297. return retval;
  2298. }
  2299. #ifndef NEWMIKMOD
  2300. static void ABC_CleanupTrack(ABCTRACK *tp)
  2301. {
  2302. ABCEVENT *ep, *en;
  2303. if( tp ) {
  2304. for( ep=tp->head; ep; ep = en ) {
  2305. en=ep->next;
  2306. free(ep);
  2307. }
  2308. tp->head = NULL;
  2309. }
  2310. }
  2311. static void ABC_CleanupMacro(ABCMACRO *m)
  2312. {
  2313. if( m->name )
  2314. free(m->name);
  2315. if( m->subst )
  2316. free(m->subst);
  2317. free(m);
  2318. }
  2319. #endif
  2320. // =====================================================================================
  2321. static void ABC_CleanupTracks(ABCHANDLE *handle)
  2322. // =====================================================================================
  2323. {
  2324. #ifdef NEWMIKMOD
  2325. if(handle && handle->trackhandle) {
  2326. _mmalloc_close(handle->trackhandle);
  2327. handle->trackhandle = 0;
  2328. }
  2329. #else
  2330. ABCTRACK *tp, *tn;
  2331. if(handle) {
  2332. for( tp=handle->track; tp; tp = tn ) {
  2333. tn=tp->next;
  2334. ABC_CleanupTrack(tp);
  2335. }
  2336. handle->track = NULL;
  2337. }
  2338. #endif
  2339. }
  2340. // =====================================================================================
  2341. static void ABC_CleanupMacros(ABCHANDLE *handle)
  2342. // =====================================================================================
  2343. {
  2344. #ifdef NEWMIKMOD
  2345. if(handle && handle->macrohandle) {
  2346. _mmalloc_close(handle->macrohandle);
  2347. handle->macrohandle = 0;
  2348. }
  2349. #else
  2350. ABCMACRO *mp, *mn;
  2351. if(handle) {
  2352. for( mp=handle->macro; mp; mp = mn ) {
  2353. mn=mp->next;
  2354. ABC_CleanupMacro(mp);
  2355. }
  2356. for( mp=handle->umacro; mp; mp = mn ) {
  2357. mn=mp->next;
  2358. ABC_CleanupMacro(mp);
  2359. }
  2360. handle->macro = NULL;
  2361. handle->umacro = NULL;
  2362. }
  2363. #endif
  2364. }
  2365. // =====================================================================================
  2366. static void ABC_Cleanup(ABCHANDLE *handle)
  2367. // =====================================================================================
  2368. {
  2369. #ifdef NEWMIKMOD
  2370. if(handle && handle->allochandle) {
  2371. #else
  2372. if(handle) {
  2373. #endif
  2374. ABC_CleanupMacros(handle);
  2375. ABC_CleanupTracks(handle);
  2376. #ifdef NEWMIKMOD
  2377. _mmalloc_close(handle->allochandle);
  2378. handle->allochandle = 0;
  2379. handle->len = 0;
  2380. #else
  2381. if( handle->line )
  2382. free(handle->line);
  2383. if( handle->beatstring )
  2384. free(handle->beatstring);
  2385. free(handle);
  2386. #endif
  2387. }
  2388. }
  2389. static int abc_is_global_event(ABCEVENT *e)
  2390. {
  2391. return e->flg == 1 && (e->cmd == cmdtempo || e->cmd == cmdpartbrk);
  2392. }
  2393. static ABCEVENT *abc_next_global(ABCEVENT *e)
  2394. {
  2395. for( ; e && !abc_is_global_event(e); e=e->next ) ;
  2396. return e;
  2397. }
  2398. static ABCEVENT *abc_next_note(ABCEVENT *e)
  2399. {
  2400. for( ; e && e->flg == 1; e=e->next ) ;
  2401. return e;
  2402. }
  2403. // =============================================================================
  2404. #ifdef NEWMIKMOD
  2405. static void ABC_ReadPatterns(UNIMOD *of, ABCHANDLE *h, int numpat)
  2406. // =============================================================================
  2407. {
  2408. int pat,row,i,ch,trillbits;
  2409. BYTE n,ins,vol;
  2410. ABCTRACK *t;
  2411. ABCEVENT *e, *en, *ef, *el;
  2412. uint32_t tt1, tt2;
  2413. UNITRK_EFFECT eff;
  2414. // initialize start points of event list in tracks
  2415. for( t = h->track; t; t = t->next ) t->capostart = t->head;
  2416. trillbits = 0; // trill effect admininstration: one bit per channel, max 32 channnels
  2417. for( pat = 0; pat < numpat; pat++ ) {
  2418. utrk_reset(of->ut);
  2419. for( row = 0; row < 64; row++ ) {
  2420. tt1 = abcticks((pat * 64 + row ) * h->speed);
  2421. tt2 = tt1 + abcticks(h->speed);
  2422. ch = 0;
  2423. for( e=abc_next_global(h->track->capostart); e && e->tracktick < tt2; e=abc_next_global(e->next) ) {
  2424. if( e && e->tracktick >= tt1 ) { // we have a tempo event in this row
  2425. switch( e->cmd ) {
  2426. case cmdtempo:
  2427. eff.effect = UNI_GLOB_TEMPO;
  2428. eff.param.u = e->lpar;
  2429. eff.framedly = UFD_RUNONCE;
  2430. utrk_write_global(of->ut, &eff, PTMEM_TEMPO);
  2431. break;
  2432. case cmdpartbrk:
  2433. eff.effect = UNI_GLOB_PATBREAK;
  2434. eff.param.u = 0;
  2435. eff.framedly = UFD_RUNONCE;
  2436. utrk_write_global(of->ut, &eff, UNIMEM_NONE);
  2437. break;
  2438. }
  2439. }
  2440. }
  2441. for( t = h->track; t; t = t->next ) {
  2442. for( e=abc_next_note(t->capostart); e && e->tracktick < tt1; e=abc_next_note(e->next) )
  2443. t->capostart = e;
  2444. i = 0;
  2445. ef = NULL;
  2446. en = e;
  2447. el = e;
  2448. for( ; e && e->tracktick < tt2; e=abc_next_note(e->next) ) { // we have a note event in this row
  2449. t->capostart = e;
  2450. i++;
  2451. if( e->par[volume] ) {
  2452. if( !ef ) ef = e;
  2453. el = e;
  2454. }
  2455. }
  2456. if( i ) {
  2457. trillbits &= ~(1<<ch);
  2458. utrk_settrack(of->ut, ch);
  2459. if( i == 1 || ef == el || !ef ) { // only one event in this row
  2460. if( ef ) e = ef;
  2461. else e = en;
  2462. el = t->capostart;
  2463. i = e->par[note] + ((e->par[octave])*12);
  2464. if( t->chan == 10 ) {
  2465. n = pat_gm_drumnote(i) + 23;
  2466. ins = pat_gmtosmp(pat_gm_drumnr(i));
  2467. }
  2468. else {
  2469. n = pat_modnote(i);
  2470. ins = e->par[smpno];
  2471. }
  2472. eff.framedly = modticks(e->tracktick - tt1);
  2473. vol = e->par[volume];
  2474. if( e->par[effect] == accent ) {
  2475. vol += vol / 10;
  2476. if( vol > 127 ) vol = 127;
  2477. }
  2478. if (vol <= 0) {}
  2479. else if( el->par[volume] == 0 ) {
  2480. eff.framedly = modticks(el->tracktick - tt1);
  2481. eff.param.u = 0;
  2482. eff.param.byte_a = n;
  2483. eff.param.byte_b = ins;
  2484. eff.effect = UNI_NOTEKILL;
  2485. utrk_write_local(of->ut, &eff, UNIMEM_NONE);
  2486. }
  2487. else {
  2488. switch( e->par[effect] ) {
  2489. case trill:
  2490. eff.effect = UNI_VIBRATO_DEPTH;
  2491. eff.param.u = 12; // depth 1.5
  2492. utrk_write_local(of->ut, &eff, PTMEM_VIBRATO_DEPTH);
  2493. eff.effect = UNI_VIBRATO_SPEED;
  2494. eff.param.u = 48; // speed 12
  2495. utrk_write_local(of->ut, &eff, PTMEM_VIBRATO_SPEED);
  2496. trillbits |= (1<<ch);
  2497. break;
  2498. case bow:
  2499. eff.effect = UNI_PITCHSLIDE;
  2500. eff.framedly = (h->speed/2)|UFD_RUNONCE;
  2501. eff.param.s = 2;
  2502. utrk_write_local(of->ut, &eff, (e->par[effoper])? PTMEM_PITCHSLIDEUP: PTMEM_PITCHSLIDEDN);
  2503. break;
  2504. default:
  2505. break;
  2506. }
  2507. if( eff.framedly ) {
  2508. eff.param.u = 0;
  2509. eff.param.byte_a = n;
  2510. eff.param.byte_b = ins;
  2511. eff.effect = UNI_NOTEDELAY;
  2512. utrk_write_local(of->ut, &eff, UNIMEM_NONE);
  2513. }
  2514. }
  2515. utrk_write_inst(of->ut, ins);
  2516. utrk_write_note(of->ut, n); // <- normal note
  2517. pt_write_effect(of->ut, 0xc, vol);
  2518. }
  2519. else {
  2520. // two notes in one row, use FINEPITCHSLIDE runonce effect
  2521. // start first note on first tick and framedly runonce on seconds note tick
  2522. // use volume and instrument of last note
  2523. if( t->chan == 10 ) {
  2524. i = el->par[note] + ((el->par[octave])*12);
  2525. n = pat_gm_drumnote(i) + 23;
  2526. ins = pat_gmtosmp(pat_gm_drumnr(i));
  2527. i = n; // cannot change instrument here..
  2528. }
  2529. else {
  2530. i = ef->par[note] + ((ef->par[octave])*12);
  2531. n = pat_modnote(i);
  2532. ins = el->par[smpno];
  2533. i = pat_modnote(el->par[note] + ((el->par[octave])*12));
  2534. }
  2535. vol = el->par[volume];
  2536. eff.effect = UNI_PITCHSLIDE;
  2537. eff.framedly = modticks(el->tracktick - tt1)|UFD_RUNONCE;
  2538. eff.param.s = ((i > n)?i-n:n-i);
  2539. utrk_write_inst(of->ut, ins);
  2540. utrk_write_note(of->ut, n); // <- normal note
  2541. pt_write_effect(of->ut, 0xc, vol);
  2542. utrk_write_local(of->ut, &eff, (i > n)? PTMEM_PITCHSLIDEUP: PTMEM_PITCHSLIDEDN);
  2543. }
  2544. }
  2545. else { // no new notes, keep on trilling...
  2546. if( trillbits & (1<<ch) ) {
  2547. utrk_settrack(of->ut, ch);
  2548. eff.effect = UNI_VIBRATO_DEPTH;
  2549. eff.param.u = 12; // depth 1.5
  2550. utrk_write_local(of->ut, &eff, PTMEM_VIBRATO_DEPTH);
  2551. eff.effect = UNI_VIBRATO_SPEED;
  2552. eff.param.u = 60; // speed 15
  2553. utrk_write_local(of->ut, &eff, PTMEM_VIBRATO_SPEED);
  2554. }
  2555. }
  2556. ch++;
  2557. }
  2558. utrk_newline(of->ut);
  2559. }
  2560. if(!utrk_dup_pattern(of->ut,of)) return;
  2561. }
  2562. }
  2563. #else
  2564. static int ABC_ReadPatterns(MODCOMMAND *pattern[], WORD psize[], ABCHANDLE *h, int numpat, int channels)
  2565. // =====================================================================================
  2566. {
  2567. int pat,row,i,ch,trillbits;
  2568. BYTE n,ins,vol;
  2569. ABCTRACK *t;
  2570. ABCEVENT *e, *en, *ef, *el;
  2571. uint32_t tt1, tt2;
  2572. MODCOMMAND *m;
  2573. int patbrk, tempo;
  2574. if( numpat > MAX_PATTERNS ) numpat = MAX_PATTERNS;
  2575. // initialize start points of event list in tracks
  2576. for( t = h->track; t; t = t->next ) t->capostart = t->head;
  2577. trillbits = 0; // trill effect admininstration: one bit per channel, max 32 channnels
  2578. for( pat = 0; pat < numpat; pat++ ) {
  2579. pattern[pat] = CSoundFile::AllocatePattern(64, channels);
  2580. if( !pattern[pat] ) return 0;
  2581. psize[pat] = 64;
  2582. for( row = 0; row < 64; row++ ) {
  2583. tt1 = abcticks((pat * 64 + row ) * h->speed);
  2584. tt2 = tt1 + abcticks(h->speed);
  2585. ch = 0;
  2586. tempo = 0;
  2587. patbrk = 0;
  2588. for( e=abc_next_global(h->track->capostart); e && e->tracktick < tt2; e=abc_next_global(e->next) ) {
  2589. if( e && e->tracktick >= tt1 ) { // we have a tempo event in this row
  2590. switch( e->cmd ) {
  2591. case cmdtempo:
  2592. tempo = e->lpar;
  2593. break;
  2594. case cmdpartbrk:
  2595. patbrk = 1;
  2596. break;
  2597. }
  2598. }
  2599. }
  2600. for( t = h->track; t; t = t->next ) {
  2601. for( e=abc_next_note(t->capostart); e && e->tracktick < tt1; e=abc_next_note(e->next) ) ;
  2602. i = 0;
  2603. ef = NULL;
  2604. en = e;
  2605. el = e;
  2606. for( ; e && e->tracktick < tt2; e=abc_next_note(e->next) ) { // we have a note event in this row
  2607. t->capostart = e;
  2608. i++;
  2609. if( e->par[volume] ) {
  2610. if( !ef ) ef = e;
  2611. el = e;
  2612. }
  2613. }
  2614. m = &pattern[pat][row * channels + ch];
  2615. m->param = 0;
  2616. m->command = CMD_NONE;
  2617. if( i ) {
  2618. trillbits &= ~(1<<ch);
  2619. if( i == 1 || ef == el || !ef ) { // only one event in this row
  2620. if( ef ) e = ef;
  2621. else e = en;
  2622. el = t->capostart;
  2623. i = e->par[note] + ((e->par[octave])*12);
  2624. if( t->chan == 10 ) {
  2625. n = pat_gm_drumnote(i) + 23;
  2626. ins = pat_gmtosmp(pat_gm_drumnr(i));
  2627. }
  2628. else {
  2629. n = pat_modnote(i);
  2630. ins = e->par[smpno];
  2631. }
  2632. vol = e->par[volume]/2;
  2633. if( e->par[volume] > 0 ) {
  2634. if( e->par[effect] == accent ) vol += vol / 20;
  2635. if( vol > 64 ) vol = 64;
  2636. if( el->par[volume] == 0 ) { // note cut
  2637. m->param = el->tracktick - tt1;
  2638. m->command = CMD_S3MCMDEX;
  2639. m->param |= 0xC0;
  2640. }
  2641. else {
  2642. switch( e->par[effect] ) {
  2643. case trill:
  2644. m->command = CMD_VIBRATO;
  2645. m->param = 0xC2; // speed 12 depth 2
  2646. trillbits |= (1<<ch);
  2647. break;
  2648. case bow:
  2649. m->command = CMD_XFINEPORTAUPDOWN;
  2650. m->param |= (e->par[effoper])? 0x12: 0x22;
  2651. break;
  2652. default:
  2653. m->param = modticks(e->tracktick - tt1);
  2654. if( m->param ) { // note delay
  2655. m->command = CMD_S3MCMDEX;
  2656. m->param |= 0xD0;
  2657. }
  2658. break;
  2659. }
  2660. }
  2661. }
  2662. m->instr = ins;
  2663. m->note = n; // <- normal note
  2664. m->volcmd = VOLCMD_VOLUME;
  2665. m->vol = vol;
  2666. }
  2667. else {
  2668. // two notes in one row, use FINEPITCHSLIDE runonce effect
  2669. // start first note on first tick and framedly runonce on seconds note tick
  2670. // use volume and instrument of last note
  2671. if( t->chan == 10 ) {
  2672. i = el->par[note] + ((el->par[octave])*12);
  2673. n = pat_gm_drumnote(i) + 23;
  2674. ins = pat_gmtosmp(pat_gm_drumnr(i));
  2675. i = n; // cannot change instrument here..
  2676. }
  2677. else {
  2678. i = ef->par[note] + ((ef->par[octave])*12);
  2679. n = pat_modnote(i);
  2680. ins = el->par[smpno];
  2681. i = pat_modnote(el->par[note] + ((el->par[octave])*12));
  2682. }
  2683. vol = el->par[volume]/2;
  2684. if( vol > 64 ) vol = 64;
  2685. m->instr = ins;
  2686. m->note = n; // <- normal note
  2687. m->volcmd = VOLCMD_VOLUME;
  2688. m->vol = vol;
  2689. m->param = ((i > n)?i-n:n-i);
  2690. if( m->param < 16 ) {
  2691. if( m->param ) {
  2692. m->command = CMD_XFINEPORTAUPDOWN;
  2693. m->param |= (i > n)? 0x10: 0x20;
  2694. }
  2695. else { // retrigger same note...
  2696. m->command = CMD_RETRIG;
  2697. m->param = modticks(el->tracktick - tt1);
  2698. }
  2699. }
  2700. else
  2701. m->command = (i > n)? CMD_PORTAMENTOUP: CMD_PORTAMENTODOWN;
  2702. }
  2703. }
  2704. else { // no new notes, keep on trilling...
  2705. if( trillbits & (1<<ch) ) {
  2706. m = &pattern[pat][row * channels + ch];
  2707. m->command = CMD_VIBRATO;
  2708. m->param = 0; // inherited from first effect
  2709. m->instr = 0;
  2710. m->note = 0;
  2711. m->volcmd = 0;
  2712. m->vol = 0;
  2713. }
  2714. }
  2715. if( m->param == 0 && m->command == CMD_NONE ) {
  2716. if( tempo ) {
  2717. m->command = CMD_TEMPO;
  2718. m->param = tempo;
  2719. tempo = 0;
  2720. }
  2721. else {
  2722. if( patbrk ) {
  2723. m->command = CMD_PATTERNBREAK;
  2724. patbrk = 0;
  2725. }
  2726. }
  2727. }
  2728. ch++;
  2729. }
  2730. if( tempo || patbrk ) return 1;
  2731. }
  2732. }
  2733. return 0;
  2734. }
  2735. #endif
  2736. static int ABC_Key(const char *p)
  2737. {
  2738. int i,j;
  2739. char c[8] = {0};
  2740. const char *q;
  2741. while( isspace(*p) ) p++;
  2742. i = 0;
  2743. q = p;
  2744. for( i=0; i<8 && *p && *p != ']'; p++ ) {
  2745. if( isspace(*p) ) {
  2746. while( isspace(*p) ) p++;
  2747. if( strncasecmp(p, "min", 3) && strncasecmp(p, "maj", 3) )
  2748. break;
  2749. }
  2750. c[i] = *p;
  2751. i++;
  2752. }
  2753. c[i] = '\0';
  2754. if( !strcmp(c,"Hp") || !strcmp(c,"HP") ) // highland pipes
  2755. strcpy(c,"Bm"); // two sharps at c and f
  2756. if( !strcasecmp(c+1, "minor") ) i=2;
  2757. if( !strcasecmp(c+2, "minor") ) i=3;
  2758. if( !strcasecmp(c+1, "major") ) i=1;
  2759. if( !strcasecmp(c+2, "major") ) i=2;
  2760. if( !strcasecmp(c+1, "min") ) i=2;
  2761. if( !strcasecmp(c+2, "min") ) i=3;
  2762. if( !strcasecmp(c+1, "maj") ) i=1;
  2763. if( !strcasecmp(c+2, "maj") ) i=2;
  2764. for( ; i<6; i++ )
  2765. c[i] = ' ';
  2766. c[i] = '\0';
  2767. for( i=0; keySigs[i]; i++ ) {
  2768. for( j=10; j<46; j+=6 )
  2769. if( !strncasecmp(keySigs[i]+j, c, 6) )
  2770. return i;
  2771. }
  2772. abc_message("Failure: Unrecognised K: field %s", q);
  2773. return 7;
  2774. }
  2775. static char *abc_skip_word(char *p)
  2776. {
  2777. while( isspace(*p) ) p++;
  2778. while( *p && !isspace(*p) && *p != ']') p++;
  2779. while( isspace(*p) ) p++;
  2780. return p;
  2781. }
  2782. static uint32_t abc_tracktime(ABCTRACK *tp)
  2783. {
  2784. uint32_t tracktime;
  2785. if( tp->tail ) tracktime = tp->tail->tracktick;
  2786. else tracktime = 0;
  2787. if( tracktime < global_songstart )
  2788. tracktime = global_songstart;
  2789. return tracktime;
  2790. }
  2791. static void abc_addchordname(const char *s, int len, const int *notes)
  2792. // adds chord name and note set to list of known chords
  2793. {
  2794. int i, j;
  2795. if(strlen(s) > 7) {
  2796. abc_message("Failure: Chord name cannot exceed 7 characters, %s", s);
  2797. return;
  2798. }
  2799. if(len > 6) {
  2800. abc_message("Failure: Named chord cannot have more than 6 notes, %s", s);
  2801. return;
  2802. }
  2803. for( i=0; i < chordsnamed; i++ ) {
  2804. if(strcmp(s, chordname[i]) == 0) {
  2805. /* change chord */
  2806. chordlen[i] = len;
  2807. for(j = 0; j < len; j++) chordnotes[i][j] = notes[j];
  2808. return;
  2809. }
  2810. }
  2811. if(chordsnamed > MAXCHORDNAMES - 1)
  2812. abc_message("Failure: Too many Guitar Chord Names used, %s", s);
  2813. else {
  2814. strcpy(chordname[chordsnamed], s);
  2815. chordlen[chordsnamed] = len;
  2816. for(j = 0; j < len; j++) chordnotes[chordsnamed][j] = notes[j];
  2817. chordsnamed++;
  2818. }
  2819. }
  2820. static void abc_setup_chordnames()
  2821. // set up named guitar chords
  2822. {
  2823. static const int list_Maj[3] = { 0, 4, 7 };
  2824. static const int list_m[3] = { 0, 3, 7 };
  2825. static const int list_7[4] = { 0, 4, 7, 10 };
  2826. static const int list_m7[4] = { 0, 3, 7, 10 };
  2827. static const int list_maj7[4] = { 0, 4, 7, 11 };
  2828. static const int list_M7[4] = { 0, 4, 7, 11 };
  2829. static const int list_6[4] = { 0, 4, 7, 9 };
  2830. static const int list_m6[4] = { 0, 3, 7, 9 };
  2831. static const int list_aug[3] = { 0, 4, 8 };
  2832. static const int list_plus[3] = { 0, 4, 8 };
  2833. static const int list_aug7[4] = { 0, 4, 8, 10 };
  2834. static const int list_dim[3] = { 0, 3, 6 };
  2835. static const int list_dim7[4] = { 0, 3, 6, 9 };
  2836. static const int list_9[5] = { 0, 4, 7, 10, 2 };
  2837. static const int list_m9[5] = { 0, 3, 7, 10, 2 };
  2838. static const int list_maj9[5] = { 0, 4, 7, 11, 2 };
  2839. static const int list_M9[5] = { 0, 4, 7, 11, 2 };
  2840. static const int list_11[6] = { 0, 4, 7, 10, 2, 5 };
  2841. static const int list_dim9[5] = { 0, 4, 7, 10, 13 };
  2842. static const int list_sus[3] = { 0, 5, 7 };
  2843. static const int list_sus9[3] = { 0, 2, 7 };
  2844. static const int list_7sus[4] = { 0, 5, 7, 10 };
  2845. static const int list_7sus4[4] = { 0, 5, 7, 10 };
  2846. static const int list_7sus9[4] = { 0, 2, 7, 10 };
  2847. static const int list_9sus4[5] = { 0, 5, 10, 14, 19 };
  2848. static const int list_5[2] = { 0, 7 };
  2849. static const int list_13[6] = { 0, 4, 7, 10, 16, 21 };
  2850. chordsnamed = 0;
  2851. abc_addchordname("", 3, list_Maj);
  2852. abc_addchordname("m", 3, list_m);
  2853. abc_addchordname("7", 4, list_7);
  2854. abc_addchordname("m7", 4, list_m7);
  2855. abc_addchordname("maj7", 4, list_maj7);
  2856. abc_addchordname("M7", 4, list_M7);
  2857. abc_addchordname("6", 4, list_6);
  2858. abc_addchordname("m6", 4, list_m6);
  2859. abc_addchordname("aug", 3, list_aug);
  2860. abc_addchordname("+", 3, list_plus);
  2861. abc_addchordname("aug7", 4, list_aug7);
  2862. abc_addchordname("7+", 4, list_aug7);
  2863. abc_addchordname("dim", 3, list_dim);
  2864. abc_addchordname("dim7", 4, list_dim7);
  2865. abc_addchordname("9", 5, list_9);
  2866. abc_addchordname("m9", 5, list_m9);
  2867. abc_addchordname("maj9", 5, list_maj9);
  2868. abc_addchordname("M9", 5, list_M9);
  2869. abc_addchordname("11", 6, list_11);
  2870. abc_addchordname("dim9", 5, list_dim9);
  2871. abc_addchordname("sus", 3, list_sus);
  2872. abc_addchordname("sus9", 3, list_sus9);
  2873. abc_addchordname("7sus", 4, list_7sus);
  2874. abc_addchordname("7sus4", 4, list_7sus4);
  2875. abc_addchordname("7sus9", 4, list_7sus9);
  2876. abc_addchordname("9sus4", 5, list_9sus4);
  2877. abc_addchordname("5", 2, list_5);
  2878. abc_addchordname("13", 6, list_13);
  2879. }
  2880. static int abc_MIDI_getnumber(const char *p)
  2881. {
  2882. int n;
  2883. while( isspace(*p) ) p++;
  2884. abc_getnumber(p, &n);
  2885. if( n < 0 ) n = 0;
  2886. if( n > 127 ) n = 127;
  2887. return n;
  2888. }
  2889. static int abc_MIDI_getprog(const char *p)
  2890. {
  2891. int n;
  2892. while( isspace(*p) ) p++;
  2893. abc_getnumber(p, &n);
  2894. if( n < 1 ) n = 1;
  2895. if( n > 128 ) n = 128;
  2896. return n;
  2897. }
  2898. // MIDI drone <instr0> <pitch1> <pitch2> <vel1> <vel2>
  2899. static void abc_MIDI_drone(const char *p, int *gm, int *ptch, int *vol)
  2900. {
  2901. int i;
  2902. while( isspace(*p) ) p++;
  2903. p += abc_getnumber(p, &i);
  2904. i++; // adjust for 1..128
  2905. if( i>0 && i < 129 )
  2906. *gm = i;
  2907. else
  2908. *gm = 71; // bassoon
  2909. while( isspace(*p) ) p++;
  2910. p += abc_getnumber(p, &i);
  2911. if( i>0 && i < 127 )
  2912. ptch[0] = i;
  2913. else
  2914. ptch[0] = 45;
  2915. while( isspace(*p) ) p++;
  2916. p += abc_getnumber(p, &i);
  2917. if( i>0 && i < 127 )
  2918. ptch[1] = i;
  2919. else
  2920. ptch[1] = 33;
  2921. while( isspace(*p) ) p++;
  2922. p += abc_getnumber(p, &i);
  2923. if( i>0 && i < 127 )
  2924. vol[0] = i;
  2925. else
  2926. vol[0] = 80;
  2927. while( isspace(*p) ) p++;
  2928. p += abc_getnumber(p, &i);
  2929. if( i>0 && i < 127 )
  2930. vol[1] = i;
  2931. else
  2932. vol[1] = 80;
  2933. }
  2934. static void abc_chan_to_tracks(ABCHANDLE *h, int tno, int ch)
  2935. {
  2936. ABCTRACK *tp;
  2937. if( tno>0 && tno<33 ) {
  2938. for( tp=h->track; tp; tp=tp->next ) {
  2939. if( tp->vno == tno && (tp->vpos < GCHORDBPOS || tp->vpos > DRONEPOS2) )
  2940. tp->chan = ch;
  2941. }
  2942. }
  2943. }
  2944. // %%MIDI channel int1
  2945. // channel numbers are 1-16
  2946. static void abc_MIDI_channel(const char *p, ABCTRACK *tp, ABCHANDLE *h)
  2947. {
  2948. int i1, i2;
  2949. i1 = tp? tp->vno: 1;
  2950. for( ; *p && isspace(*p); p++ ) ;
  2951. if( isdigit(*p) ) {
  2952. p += abc_getnumber(p, &i2);
  2953. if( i2 >= 1 && i2 <= 16 )
  2954. abc_chan_to_tracks(h, i1, i2); // we start at 1
  2955. }
  2956. }
  2957. static void abc_instr_to_tracks(ABCHANDLE *h, int tno, int gm)
  2958. {
  2959. ABCTRACK *tp;
  2960. if( tno>0 && tno<33 && gm>0 && gm<129 ) {
  2961. for( tp=h->track; tp; tp=tp->next ) {
  2962. if( tp->vno == tno && (tp->vpos < GCHORDBPOS || tp->vpos > DRONEPOS2) )
  2963. tp->instr = gm;
  2964. }
  2965. }
  2966. }
  2967. // %%MIDI program [int1] <int2>
  2968. // instrument numbers are 0-127
  2969. static void abc_MIDI_program(const char *p, ABCTRACK *tp, ABCHANDLE *h)
  2970. {
  2971. int i1, i2;
  2972. i1 = tp? tp->vno: 1;
  2973. for( ; *p && isspace(*p); p++ ) ;
  2974. if( isdigit(*p) ) {
  2975. p += abc_getnumber(p, &i2);
  2976. for( ; *p && isspace(*p); p++ ) ;
  2977. if( isdigit(*p) ) {
  2978. i1 = i2;
  2979. p += abc_getnumber(p, &i2);
  2980. }
  2981. abc_instr_to_tracks(h, i1, i2 + 1); // we start at 1
  2982. }
  2983. }
  2984. static void abc_mute_voice(ABCHANDLE *h, ABCTRACK *tp, int m)
  2985. {
  2986. ABCTRACK *t;
  2987. for( t=h->track; t; t=t->next ) {
  2988. if( t->vno == tp->vno ) t->mute = m;
  2989. }
  2990. }
  2991. // %%MIDI voice [<ID>] [instrument=<integer> [bank=<integer>]] [mute]
  2992. // instrument numbers are 1-128
  2993. static void abc_MIDI_voice(const char *p, ABCTRACK *tp, ABCHANDLE *h)
  2994. {
  2995. int i1, i2;
  2996. for( ; *p && isspace(*p); p++ ) ;
  2997. if( strncmp(p,"instrument=",11) && strncmp(p,"mute",4) ) {
  2998. tp = abc_locate_track(h, p, 0);
  2999. for( ; *p && !isspace(*p); p++ ) ;
  3000. for( ; *p && isspace(*p); p++ ) ;
  3001. }
  3002. i1 = tp? tp->vno: 1;
  3003. i2 = 0;
  3004. if( !strncmp(p,"instrument=",11) && isdigit(p[11]) ) {
  3005. p += 11;
  3006. p += abc_getnumber(p, &i2);
  3007. for( ; *p && isspace(*p); p++ ) ;
  3008. if( !strncmp(p,"bank=",5) && isdigit(p[5]) ) {
  3009. for( ; *p && !isspace(*p); p++ ) ;
  3010. for( ; *p && isspace(*p); p++ ) ;
  3011. }
  3012. }
  3013. if( tp ) abc_mute_voice(h,tp,0);
  3014. if( !strncmp(p,"mute",4) && (p[4]=='\0' || p[4]=='%' || isspace(p[4])) ) {
  3015. if( tp ) abc_mute_voice(h,tp,1);
  3016. }
  3017. abc_instr_to_tracks(h, i1, i2); // starts already at 1 (draft 4.0)
  3018. }
  3019. // %%MIDI chordname <string> <int1> <int2> ... <int6>
  3020. static void abc_MIDI_chordname(const char *p)
  3021. {
  3022. char name[20];
  3023. int i, notes[6];
  3024. for( ; *p && isspace(*p); p++ ) ;
  3025. i = 0;
  3026. while ((i < 19) && (*p != ' ') && (*p != '\0')) {
  3027. name[i] = *p;
  3028. p = p + 1;
  3029. i = i + 1;
  3030. }
  3031. name[i] = '\0';
  3032. if(*p != ' ') {
  3033. abc_message("Failure: Bad format for chordname command, %s", p);
  3034. }
  3035. else {
  3036. i = 0;
  3037. while ((i <= 6) && isspace(*p)) {
  3038. for( ; *p && isspace(*p); p++ ) ;
  3039. p += abc_getnumber(p, &notes[i]);
  3040. i = i + 1;
  3041. }
  3042. abc_addchordname(name, i, notes);
  3043. }
  3044. }
  3045. // %%MIDI drum <string> <inst 1> ... <inst n> <vol 1> ... <vol n>
  3046. // instrument numbers are 0-127
  3047. static int abc_MIDI_drum(const char *p, ABCHANDLE *h)
  3048. {
  3049. char *q;
  3050. int i,n,m;
  3051. while( isspace(*p) ) p++;
  3052. if( !strncmp(p,"on",2) && (isspace(p[2]) || p[2] == '\0') ) return 2;
  3053. if( !strncmp(p,"off",3) && (isspace(p[3]) || p[3] == '\0') ) return 1;
  3054. n = 0;
  3055. for( q = h->drum; *p && !isspace(*p); p++ ) {
  3056. if( !strchr("dz0123456789",*p) ) break;
  3057. *q++ = *p;
  3058. if( !isdigit(*p) ) {
  3059. if( !isdigit(p[1]) ) *q++ = '1';
  3060. n++; // count the silences too....
  3061. }
  3062. }
  3063. *q = '\0';
  3064. q = h->drumins;
  3065. for( i = 0; i<n; i++ ) {
  3066. if( h->drum[i*2] == 'd' ) {
  3067. while( isspace(*p) ) p++;
  3068. if( !isdigit(*p) ) {
  3069. m = 0;
  3070. while( !isspace(*p) ) p++;
  3071. }
  3072. else
  3073. p += abc_getnumber(p,&m);
  3074. q[i] = m + 1; // we start at 1
  3075. }
  3076. else q[i] = 0;
  3077. }
  3078. q = h->drumvol;
  3079. for( i = 0; i<n; i++ ) {
  3080. if( h->drum[i*2] == 'd' ) {
  3081. while( isspace(*p) ) p++;
  3082. if( !isdigit(*p) ) {
  3083. m = 0;
  3084. while( !isspace(*p) ) p++;
  3085. }
  3086. else
  3087. p += abc_getnumber(p,&m);
  3088. q[i] = m;
  3089. }
  3090. else q[i] = 0;
  3091. }
  3092. return 0;
  3093. }
  3094. // %%MIDI gchord <string>
  3095. static int abc_MIDI_gchord(const char *p, ABCHANDLE *h)
  3096. {
  3097. char *q;
  3098. while( isspace(*p) ) p++;
  3099. if( !strncmp(p,"on",2) && (isspace(p[2]) || p[2] == '\0') ) return 2;
  3100. if( !strncmp(p,"off",3) && (isspace(p[3]) || p[3] == '\0') ) return 1;
  3101. for( q = h->gchord; *p && !isspace(*p); p++ ) {
  3102. if( !strchr("fbcz0123456789ghijGHIJ",*p) ) break;
  3103. *q++ = *p;
  3104. if( !isdigit(*p) && !isdigit(p[1]) ) *q++ = '1';
  3105. }
  3106. *q = '\0';
  3107. return 0;
  3108. }
  3109. static void abc_metric_gchord(ABCHANDLE *h, int mlen, int mdiv)
  3110. {
  3111. switch( 16 * mlen + mdiv ) {
  3112. case 0x24:
  3113. case 0x44:
  3114. case 0x22:
  3115. abc_MIDI_gchord("fzczfzcz", h);
  3116. break;
  3117. case 0x64:
  3118. case 0x32:
  3119. abc_MIDI_gchord("fzczczfzczcz", h);
  3120. break;
  3121. case 0x34:
  3122. case 0x38:
  3123. abc_MIDI_gchord("fzczcz", h);
  3124. break;
  3125. case 0x68:
  3126. abc_MIDI_gchord("fzcfzc", h);
  3127. break;
  3128. case 0x98:
  3129. abc_MIDI_gchord("fzcfzcfzc", h);
  3130. break;
  3131. case 0xc8:
  3132. abc_MIDI_gchord("fzcfzcfzcfzc", h);
  3133. break;
  3134. default:
  3135. if( mlen % 3 == 0 )
  3136. abc_MIDI_gchord("fzcfzcfzcfzcfzcfzcfzcfzcfzc", h);
  3137. else
  3138. abc_MIDI_gchord("fzczfzczfzczfzczfzczfzczfzcz", h);
  3139. if( mdiv == 8 ) h->gchord[mlen*2] = '\0';
  3140. else h->gchord[mlen*4] = '\0';
  3141. break;
  3142. }
  3143. }
  3144. static void abc_MIDI_legato(const char *p, ABCTRACK *tp)
  3145. {
  3146. for( ; *p && isspace(*p); p++ ) ;
  3147. if( !strncmp(p,"off",3) ) tp->legato = 0;
  3148. else tp->legato = 1;
  3149. }
  3150. static void abc_M_field(const char *p, int *mlen, int *mdiv)
  3151. {
  3152. if( !strncmp(p,"none",4) ) {
  3153. *mlen = 1;
  3154. *mdiv = 1;
  3155. return;
  3156. }
  3157. if( !strncmp(p,"C|",2) ) {
  3158. *mlen = 2;
  3159. *mdiv = 2;
  3160. return;
  3161. }
  3162. if( *p == 'C' ) {
  3163. *mlen = 4;
  3164. *mdiv = 4;
  3165. return;
  3166. }
  3167. p += abc_getexpr(p,mlen);
  3168. sscanf(p," / %d", mdiv);
  3169. }
  3170. static int abc_drum_steps(const char *dch)
  3171. {
  3172. const char *p;
  3173. int i=0;
  3174. for( p=dch; *p; p++ ) {
  3175. if( isdigit(*p) ) i += *p - '0';;
  3176. }
  3177. return i;
  3178. }
  3179. static void abc_add_drum(ABCHANDLE *h, uint32_t tracktime, uint32_t bartime)
  3180. {
  3181. ABCEVENT *e;
  3182. ABCTRACK *tp;
  3183. uint32_t etime, ctime , rtime, stime;
  3184. int i, g, steps, gnote, gsteps, nnum;
  3185. steps = abc_drum_steps(h->drum);
  3186. ctime = h->barticks;
  3187. // look up the last event in tpr drumtrack
  3188. tp = abc_locate_track(h, h->tpr->v, DRUMPOS);
  3189. e = tp->tail;
  3190. etime = e? e->tracktick: bartime;
  3191. if( etime > tracktime ) return;
  3192. if( etime < bartime ) rtime = h->barticks - ((bartime - etime) % h->barticks);
  3193. else rtime = (etime - bartime) % h->barticks;
  3194. stime = ctime*steps;
  3195. rtime *= steps;
  3196. rtime += stime;
  3197. gsteps = strlen(h->drum)/2;
  3198. g = 0;
  3199. while( rtime > stime ) {
  3200. rtime -= ctime*(h->drum[g*2+1] - '0');
  3201. if( ++g == gsteps ) g = 0;
  3202. }
  3203. stime = (tracktime - etime) * steps;
  3204. rtime = 0;
  3205. while( rtime < stime ) {
  3206. gnote = h->drum[g*2];
  3207. i = h->drum[g*2+1] - '0';
  3208. if(gnote=='d') {
  3209. tp->instr = pat_gm_drumnr(h->drumins[g]-1);
  3210. nnum = pat_gm_drumnote(h->drumins[g]);
  3211. abc_add_drumnote(h, tp, etime + rtime/steps, nnum, h->drumvol[g]);
  3212. abc_add_noteoff(h,tp,etime + ( rtime + ctime * i )/steps);
  3213. }
  3214. if( ++g == gsteps ) g = 0;
  3215. rtime += ctime * i;
  3216. }
  3217. }
  3218. static int abc_gchord_steps(const char *gch)
  3219. {
  3220. const char *p;
  3221. int i=0;
  3222. for( p=gch; *p; p++ )
  3223. if( isdigit(*p) ) i += *p - '0';
  3224. return i;
  3225. }
  3226. static void abc_add_gchord(ABCHANDLE *h, uint32_t tracktime, uint32_t bartime)
  3227. {
  3228. ABCEVENT *e, *c;
  3229. ABCTRACK *tp;
  3230. uint32_t etime, ctime , rtime, stime;
  3231. int i, g, steps, gnote, gcnum, gsteps, nnum, glen;
  3232. // look up the last chord event in tpc
  3233. c = 0;
  3234. for( e = h->tpc->head; e; e = e->next )
  3235. if( e->flg == 1 && e->cmd == cmdchord )
  3236. c = e;
  3237. if( !c ) return;
  3238. gcnum = c->par[chordnum];
  3239. steps = abc_gchord_steps(h->gchord);
  3240. ctime = h->barticks;
  3241. etime = 0;
  3242. for( i = GCHORDBPOS; i < DRUMPOS; i++ ) {
  3243. tp = abc_locate_track(h, h->tpc->v, i);
  3244. e = tp->tail;
  3245. if( !e ) e = c;
  3246. stime = e->tracktick;
  3247. if( stime > etime ) etime = stime;
  3248. }
  3249. if( etime > tracktime ) return;
  3250. if( etime < bartime ) rtime = h->barticks - ((bartime - etime) % h->barticks);
  3251. else rtime = (etime - bartime) % h->barticks;
  3252. stime = ctime * steps;
  3253. rtime *= steps;
  3254. rtime += stime;
  3255. gsteps = strlen(h->gchord);
  3256. g = 0;
  3257. while( rtime > stime ) {
  3258. glen = h->gchord[2*g+1] - '0';
  3259. rtime -= ctime * glen;
  3260. if( ++g == gsteps ) g = 0;
  3261. }
  3262. stime = (tracktime - etime) * steps;
  3263. rtime = 0;
  3264. while( rtime < stime ) {
  3265. gnote = h->gchord[2*g];
  3266. glen = h->gchord[2*g+1] - '0';
  3267. if( ++g == gsteps ) g = 0;
  3268. nnum = 0;
  3269. switch(gnote) {
  3270. case 'b':
  3271. tp = abc_locate_track(h, h->tpc->v, GCHORDFPOS);
  3272. tp->instr = h->abcbassprog;
  3273. nnum = c->par[chordnote]+chordnotes[gcnum][0]+24;
  3274. abc_add_chordnote(h, tp, etime + rtime/steps, nnum, h->abcbassvol);
  3275. abc_add_noteoff(h,tp,etime + ( rtime + ctime * glen )/steps);
  3276. case 'c':
  3277. for( i = 1; i < chordlen[gcnum]; i++ ) {
  3278. tp = abc_locate_track(h, h->tpc->v, i+GCHORDFPOS);
  3279. tp->instr = h->abcchordprog;
  3280. nnum = c->par[chordnote]+chordnotes[gcnum][i]+24;
  3281. abc_add_chordnote(h, tp, etime + rtime/steps, nnum, h->abcchordvol);
  3282. abc_add_noteoff(h,tp,etime + ( rtime + ctime * glen )/steps);
  3283. }
  3284. rtime += ctime * glen;
  3285. break;
  3286. case 'f':
  3287. tp = abc_locate_track(h, h->tpc->v, GCHORDFPOS);
  3288. tp->instr = h->abcbassprog;
  3289. nnum = c->par[chordbase]+12;
  3290. abc_add_chordnote(h, tp, etime + rtime/steps, nnum, h->abcbassvol);
  3291. rtime += ctime * glen;
  3292. abc_add_noteoff(h,tp,etime + rtime/steps);
  3293. break;
  3294. case 'g':
  3295. case 'h':
  3296. case 'i':
  3297. case 'j':
  3298. case 'G':
  3299. case 'H':
  3300. case 'I':
  3301. case 'J':
  3302. i = toupper(gnote) - 'G';
  3303. nnum = 0;
  3304. if( i < chordlen[gcnum] ) {
  3305. tp = abc_locate_track(h, h->tpc->v, GCHORDFPOS+i+1);
  3306. tp->instr = h->abcchordprog;
  3307. nnum = c->par[chordnote]+chordnotes[gcnum][i]+24;
  3308. if( isupper(gnote) ) nnum -= 12;
  3309. abc_add_chordnote(h, tp, etime + rtime/steps, nnum, h->abcchordvol);
  3310. }
  3311. rtime += ctime * glen;
  3312. if( nnum ) abc_add_noteoff(h,tp,etime + rtime/steps);
  3313. break;
  3314. case 'z':
  3315. rtime += ctime * glen;
  3316. break;
  3317. }
  3318. }
  3319. }
  3320. // %%MIDI beat a b c n
  3321. //
  3322. // controls the way note velocities are selected. The first note in a bar has
  3323. // velocity a. Other "strong" notes have velocity b and all the rest have velocity
  3324. // c. a, b and c must be in the range 0-128. The parameter n determines which
  3325. // notes are "strong". If the time signature is x/y, then each note is given
  3326. // a position number k = 0, 1, 2 .. x-1 within each bar. Note that the units for
  3327. // n are not the unit note length. If k is a multiple of n, then the note is
  3328. // "strong". The volume specifiers !ppp! to !fff! are equivalent to the
  3329. // following :
  3330. //
  3331. // !ppp! = %%MIDI beat 30 20 10 1
  3332. // !pp! = %%MIDI beat 45 35 20 1
  3333. // !p! = %%MIDI beat 60 50 35 1
  3334. // !mp! = %%MIDI beat 75 65 50 1
  3335. // !mf! = %%MIDI beat 90 80 65 1
  3336. // !f! = %%MIDI beat 105 95 80 1
  3337. // !ff! = %%MIDI beat 120 110 95 1
  3338. // !fff! = %%MIDI beat 127 125 110 1
  3339. static void abc_MIDI_beat(ABCHANDLE *h, const char *p)
  3340. {
  3341. int i,j;
  3342. h->beat[0] = 127;
  3343. h->beat[1] = 125;
  3344. h->beat[2] = 110;
  3345. h->beat[3] = 1;
  3346. for( j=0; j<4; j++ ) {
  3347. while( isspace(*p) ) p++;
  3348. if( *p ) {
  3349. p += abc_getnumber(p, &i);
  3350. if( i < 0 ) i = 0;
  3351. if( i > 127 ) i = 127;
  3352. h->beat[j] = i;
  3353. }
  3354. }
  3355. if( h->beat[3] == 0 ) h->beat[3] = 1; // BB Ruud says: do not let you make mad
  3356. }
  3357. //
  3358. // %%MIDI beatstring <string of f, m and p>
  3359. //
  3360. // This provides an alternative way of specifying where the strong and weak
  3361. // stresses fall within a bar. 'f' means velocity a (normally strong), 'm'
  3362. // means velocity b (medium velocity) and 'p' means velocity c (soft velocity).
  3363. // For example, if the time signature is 7/8 with stresses on the first, fourth
  3364. // and sixth notes in the bar, we could use the following
  3365. //
  3366. // %%MIDI beatstring fppmpmp
  3367. static void abc_MIDI_beatstring(ABCHANDLE *h, const char *p)
  3368. {
  3369. while( isspace(*p) ) p++;
  3370. if( h->beatstring ) _mm_free(h->allochandle, h->beatstring);
  3371. if( strlen(p) )
  3372. h->beatstring = DupStr(h->allochandle,p,strlen(p)+1);
  3373. else
  3374. h->beatstring = NULL;
  3375. }
  3376. static int abc_beat_vol(ABCHANDLE *h, int abcvol, int barpos)
  3377. {
  3378. int vol;
  3379. if( h->beatstring ) {
  3380. vol = (h->beat[2] * 9) / 10;
  3381. if( barpos < (int)strlen(h->beatstring) ) {
  3382. switch(h->beatstring[barpos]) {
  3383. case 'f':
  3384. vol = h->beat[0];
  3385. break;
  3386. case 'm':
  3387. vol = h->beat[1];
  3388. break;
  3389. case 'p':
  3390. vol = h->beat[2];
  3391. break;
  3392. default:
  3393. break;
  3394. }
  3395. }
  3396. }
  3397. else {
  3398. if( (barpos % h->beat[3]) == 0 ) {
  3399. if( barpos )
  3400. vol = h->beat[1];
  3401. else
  3402. vol = h->beat[0];
  3403. }
  3404. else
  3405. vol = h->beat[2];
  3406. }
  3407. vol *= abcvol;
  3408. vol /= 128;
  3409. return vol;
  3410. }
  3411. static void abc_init_partpat(BYTE partp[27][2])
  3412. {
  3413. int i;
  3414. for( i=0; i<27; i++ ) {
  3415. partp[i][0] = 0xff;
  3416. partp[i][1] = 0;
  3417. }
  3418. }
  3419. static int abc_partpat_to_orderlist(BYTE partp[27][2], const char *abcparts, ABCHANDLE *h, BYTE **list, int orderlen)
  3420. {
  3421. int t, partsused;
  3422. const char *p;
  3423. BYTE *orderlist = *list;
  3424. static int ordersize = 0;
  3425. if( *list == NULL ) {
  3426. ordersize = 128;
  3427. orderlist = (BYTE *)_mm_calloc(h->ho, ordersize, sizeof(BYTE));
  3428. *list = orderlist;
  3429. }
  3430. if( abcparts ) {
  3431. partsused = 0;
  3432. for( p = abcparts; *p; p++ ) {
  3433. for( t = partp[*p - 'A'][0]; t < partp[*p - 'A'][1]; t++ ) {
  3434. if( orderlen == ordersize ) {
  3435. ordersize <<= 1;
  3436. orderlist = (BYTE *)_mm_recalloc(h->ho, orderlist, ordersize, sizeof(BYTE));
  3437. *list = orderlist;
  3438. }
  3439. orderlist[orderlen] = t;
  3440. orderlen++;
  3441. partsused++;
  3442. }
  3443. }
  3444. if( partsused ) return orderlen;
  3445. }
  3446. // some fool wrote a P: string in the header but didn't use P: in the body
  3447. for( t = partp[26][0]; t < partp[26][1]; t++ ) {
  3448. if( orderlen == ordersize ) {
  3449. ordersize <<= 1;
  3450. orderlist = (BYTE *)_mm_recalloc(h->ho, orderlist, ordersize, sizeof(BYTE));
  3451. *list = orderlist;
  3452. }
  3453. orderlist[orderlen] = t;
  3454. orderlen++;
  3455. }
  3456. return orderlen;
  3457. }
  3458. static void abc_globalslide(ABCHANDLE *h, uint32_t tracktime, int slide)
  3459. {
  3460. ABCTRACK *tp;
  3461. ABCEVENT *e;
  3462. int hslide;
  3463. hslide = h->track? h->track->slidevol: slide;
  3464. for( tp=h->track; tp; tp = tp->next ) {
  3465. if( slide ) {
  3466. tp->slidevoltime = tracktime;
  3467. if( slide == 2 )
  3468. tp->slidevol = 0;
  3469. }
  3470. if( tp->slidevol > -2 && slide < 2 )
  3471. tp->slidevol = slide;
  3472. }
  3473. if( h->track && h->track->tail
  3474. && hslide != slide && slide == -2
  3475. && h->track->tail->tracktick >= tracktime ) {
  3476. // need to update jumptypes in mastertrack from tracktime on...
  3477. for( e=h->track->head; e; e=e->next ) {
  3478. if( e->flg == 1 && e->cmd == cmdjump && e->tracktick >= tracktime ) {
  3479. switch( e->par[jumptype] ) {
  3480. case jumpnormal:
  3481. case jumpfade:
  3482. e->par[jumptype] = jumpfade;
  3483. break;
  3484. case jumpdacapo:
  3485. case jumpdcfade:
  3486. e->par[jumptype] = jumpdcfade;
  3487. break;
  3488. case jumpdasegno:
  3489. case jumpdsfade:
  3490. e->par[jumptype] = jumpdsfade;
  3491. break;
  3492. }
  3493. }
  3494. }
  3495. }
  3496. }
  3497. static void abc_recalculate_tracktime(ABCHANDLE *h) {
  3498. ABCTRACK *ttp;
  3499. h->tracktime = 0;
  3500. for( ttp=h->track; ttp; ttp=ttp->next )
  3501. if( ttp->tail && ttp->tail->tracktick > h->tracktime )
  3502. h->tracktime = ttp->tail->tracktick;
  3503. }
  3504. static void abc_MIDI_command(ABCHANDLE *h, char *p, char delim) {
  3505. int t;
  3506. // interpret some of the possibilitys
  3507. if( !strncmp(p,"bassprog",8) && isspace(p[8]) ) h->abcbassprog = abc_MIDI_getprog(p+8)+1;
  3508. if( !strncmp(p,"bassvol",7) && isspace(p[7]) ) h->abcbassvol = abc_MIDI_getnumber(p+7);
  3509. if( !strncmp(p,"beat",4) && isspace(p[4]) ) abc_MIDI_beat(h, p+4);
  3510. if( !strncmp(p,"beatstring",10) && isspace(p[10]) ) abc_MIDI_beatstring(h, p+4);
  3511. if( !strncmp(p,"chordname",9) && isspace(p[9]) ) abc_MIDI_chordname(p+9);
  3512. if( !strncmp(p,"chordprog",9) && isspace(p[9]) ) h->abcchordprog = abc_MIDI_getprog(p+9)+1;
  3513. if( !strncmp(p,"chordvol",8) && isspace(p[8]) ) h->abcchordvol = abc_MIDI_getnumber(p+8);
  3514. if( !strncmp(p,"drone",5) && isspace(p[5]) ) abc_MIDI_drone(p+5, &h->dronegm, h->dronepitch, h->dronevol);
  3515. if( !strncmp(p,"droneoff",8) && (p[8]=='\0' || p[8]==delim || isspace(p[8])) ) h->droneon = 0;
  3516. if( !strncmp(p,"droneon",7) && (p[7]=='\0' || p[7]==delim || isspace(p[7])) ) h->droneon = 1;
  3517. t = h->drumon;
  3518. if( !strncmp(p,"drum",4) && isspace(p[4]) ) {
  3519. h->drumon = abc_MIDI_drum(p+4, h);
  3520. if( h->drumon ) --h->drumon;
  3521. else h->drumon = t;
  3522. }
  3523. if( !strncmp(p,"drumoff",7) && (p[7]=='\0' || p[7]==delim || isspace(p[7])) ) h->drumon = 0;
  3524. if( !strncmp(p,"drumon",6) && (p[6]=='\0' || p[6]==delim || isspace(p[6])) ) h->drumon = 1;
  3525. if( t != h->drumon ) {
  3526. if( h->drumon && !h->tpr ) h->tpr = h->track;
  3527. if( h->tpr ) abc_add_drum_sync(h, h->tpr, h->tracktime); // don't start drumming from the beginning of time!
  3528. if( h->tpr && !h->drumon ) h->tpr = NULL;
  3529. }
  3530. t = h->gchordon;
  3531. if( !strncmp(p,"gchord",6) && (p[6]=='\0' || p[6]==delim || isspace(p[6])) ) {
  3532. h->gchordon = abc_MIDI_gchord(p+6, h);
  3533. if( h->gchordon ) --h->gchordon;
  3534. else h->gchordon = t;
  3535. }
  3536. if( !strncmp(p,"gchordoff",9) && (p[9]=='\0' || p[9]==delim || isspace(p[9])) ) h->gchordon = 0;
  3537. if( !strncmp(p,"gchordon",8) && (p[8]=='\0' || p[8]==delim || isspace(p[8])) ) h->gchordon = 1;
  3538. if( t != h->gchordon ) {
  3539. if( h->tpc ) abc_add_gchord_syncs(h, h->tpc, h->tracktime);
  3540. }
  3541. if( !strncmp(p,"channel",7) && isspace(p[7]) )
  3542. abc_MIDI_channel(p+8, h->tp = abc_check_track(h, h->tp), h);
  3543. if( !strncmp(p,"program",7) && isspace(p[7]) )
  3544. abc_MIDI_program(p+8, h->tp = abc_check_track(h, h->tp), h);
  3545. if( !strncmp(p,"voice",5) && isspace(p[5]) )
  3546. abc_MIDI_voice(p+6, h->tp = abc_check_track(h, h->tp), h);
  3547. if( !strncmp(p,"legato",6) && (p[6]=='\0' || p[6]==delim || isspace(p[6])) )
  3548. abc_MIDI_legato(p+6, h->tp = abc_check_track(h, h->tp));
  3549. }
  3550. // continuate line that ends with a backslash, can't do this in abc_gets because voice lines
  3551. // can have comment lines in between that must be parsed properly, for example:
  3552. // [V:1] cdef gabc' |\ << continuation backslash
  3553. // %%MIDI program 25
  3554. // c'bag fedc |
  3555. // informational lines can have this too, so it is rather convoluted code...
  3556. static char *abc_continuated(ABCHANDLE *h, MMFILE *mmf, char *p) {
  3557. char *pm, *p1, *p2 = 0;
  3558. int continued;
  3559. pm = p;
  3560. while( pm[strlen(pm)-1]=='\\' ) {
  3561. p1 = strdup(pm);
  3562. if( p2 ) free(p2);
  3563. continued = 1;
  3564. while( continued ) {
  3565. continued = 0;
  3566. pm = abc_gets(h, mmf);
  3567. if( !pm ) {
  3568. abc_message("line not properly continued\n%s", p1);
  3569. return p1;
  3570. }
  3571. while( *pm && isspace(*pm) ) ++pm;
  3572. if( !strncmp(pm,"%%",2) ) {
  3573. for( p2 = pm+2; *p2 && isspace(*p2); p2++ ) ;
  3574. if( !strncmp(p2,"MIDI",4) && (p2[4]=='=' || isspace(p2[4])) ) {
  3575. for( p2+=5; *p2 && isspace(*p2); p2++ ) ;
  3576. if( *p2 == '=' )
  3577. for( p2+=1; *p2 && isspace(*p2); p2++ ) ;
  3578. abc_MIDI_command(h,p2,'%');
  3579. }
  3580. continued = 1;
  3581. }
  3582. }
  3583. p2 = (char *)malloc(strlen(p1)+strlen(pm));
  3584. if( !p2 ) {
  3585. abc_message("macro line too long\n%s", p1);
  3586. return p1;
  3587. }
  3588. p1[strlen(p1)-1] = '\0'; // strip off the backslash
  3589. strcpy(p2,p1);
  3590. strcat(p2,pm);
  3591. pm = p2;
  3592. free(p1);
  3593. }
  3594. return pm;
  3595. }
  3596. // =====================================================================================
  3597. #ifdef NEWMIKMOD
  3598. BOOL ABC_Load(ABCHANDLE *h, UNIMOD *of, MMSTREAM *mmfile)
  3599. #else
  3600. BOOL CSoundFile::ReadABC(const uint8_t *lpStream, DWORD dwMemLength)
  3601. #endif
  3602. {
  3603. static int avoid_reentry = 0;
  3604. #ifdef NEWMIKMOD
  3605. #define m_nDefaultTempo of->inittempo
  3606. #else
  3607. ABCHANDLE *h;
  3608. uint32_t numpat;
  3609. MMFILE mm, *mmfile;
  3610. #endif
  3611. uint32_t t;
  3612. char *line, *p, *pp, ch, ch0=0;
  3613. char barsig[52]; // for propagated accidental key signature within bar
  3614. char *abcparts;
  3615. uint8_t partpat[27][2], *orderlist;
  3616. int orderlen;
  3617. enum { NOWHERE, INBETWEEN, INHEAD, INBODY, INSKIPFORX, INSKIPFORQUOTE } abcstate;
  3618. ABCEVENT_JUMPTYPE j;
  3619. ABCEVENT_X_EFFECT abceffect;
  3620. int abceffoper;
  3621. int abcxcount=0, abcxwanted=0, abcxnumber=1;
  3622. int abckey, abcrate, abcchord, abcvol, abcbeatvol, abcnoslurs, abcnolegato, abcfermata, abcarpeggio, abcto;
  3623. int abctempo;
  3624. int cnotelen=0, cnotediv=0, snotelen, snotediv, mnotelen, mnotediv, notelen, notediv;
  3625. // c for chords, s for standard L: setting, m for M: barlength
  3626. int abchornpipe, brokenrithm, tupletp, tupletq, tupletr;
  3627. int ktempo;
  3628. uint32_t abcgrace=0, bartime, thistime=0;
  3629. ABCTRACK *tpd, *ttp;
  3630. ABCMACRO *mp;
  3631. int mmsp;
  3632. #ifdef NEWMIKMOD
  3633. MMSTREAM *mmstack[MAXABCINCLUDES];
  3634. h->ho = _mmalloc_create("Load_ABC_ORDERLIST", NULL);
  3635. #else
  3636. MMFILE *mmstack[MAXABCINCLUDES];
  3637. if( !TestABC(lpStream, dwMemLength) ) return FALSE;
  3638. h = ABC_Init();
  3639. if( !h ) return FALSE;
  3640. mmfile = &mm;
  3641. mm.mm = (char *)lpStream;
  3642. mm.sz = dwMemLength;
  3643. mm.pos = 0;
  3644. #endif
  3645. while( avoid_reentry ) sleep(1);
  3646. avoid_reentry = 1;
  3647. pat_resetsmp();
  3648. pat_init_patnames();
  3649. m_nDefaultTempo = 0;
  3650. global_voiceno = 0;
  3651. abckey = 0;
  3652. h->tracktime = 0;
  3653. global_songstart = 0;
  3654. h->speed = 6;
  3655. abcrate = 240;
  3656. global_tempo_factor = 2;
  3657. global_tempo_divider = 1;
  3658. abctempo = 0;
  3659. ktempo = 0;
  3660. abceffect = none;
  3661. abceffoper = 0;
  3662. abcvol = 120;
  3663. h->abcchordvol = abcvol;
  3664. h->abcbassvol = abcvol;
  3665. h->abcchordprog = 25; // acoustic guitar
  3666. h->abcbassprog = 33; // acoustic bass
  3667. abcparts = 0;
  3668. abcnoslurs = 1;
  3669. abcnolegato = 1;
  3670. abcfermata = 0;
  3671. abcarpeggio = 0;
  3672. abcto = 0;
  3673. snotelen = 0;
  3674. snotediv = 0;
  3675. mnotelen = 1;
  3676. mnotediv = 1;
  3677. abchornpipe = 0;
  3678. brokenrithm = 0;
  3679. tupletp = 0;
  3680. tupletq = 0;
  3681. tupletr = 0;
  3682. h->ktrans = 0;
  3683. h->drumon = 0;
  3684. h->gchordon = 1;
  3685. h->droneon = 0;
  3686. h->tracktime = 0;
  3687. bartime = 0;
  3688. h->tp = NULL;
  3689. h->tpc = NULL;
  3690. h->tpr = NULL;
  3691. tpd = NULL;
  3692. h->dronegm = 71;
  3693. h->dronepitch[0] = 45;
  3694. h->dronepitch[1] = 33;
  3695. h->dronevol[0] = 80;
  3696. h->dronevol[1] = 80;
  3697. abc_new_umacro(h, "v = +downbow+");
  3698. abc_new_umacro(h, "u = +upbow+");
  3699. abc_new_umacro(h, "O = +coda+");
  3700. abc_new_umacro(h, "S = +segno+");
  3701. abc_new_umacro(h, "P = +uppermordent+");
  3702. abc_new_umacro(h, "M = +lowermordent+");
  3703. abc_new_umacro(h, "L = +emphasis+");
  3704. abc_new_umacro(h, "H = +fermata+");
  3705. abc_new_umacro(h, "T = +trill+");
  3706. abc_new_umacro(h, "~ = +roll+");
  3707. abc_setup_chordnames();
  3708. abc_init_partpat(partpat);
  3709. abc_MIDI_beat(h, ""); // reset beat array
  3710. abc_MIDI_beatstring(h, ""); // reset beatstring
  3711. orderlist = NULL;
  3712. orderlen = 0;
  3713. mmsp = 1;
  3714. mmstack[0] = mmfile;
  3715. mmfseek(mmfile,0,SEEK_SET);
  3716. abcstate = NOWHERE;
  3717. if( h->pickrandom ) {
  3718. abcstate = INSKIPFORX;
  3719. abcxcount = 0;
  3720. mmfseek(mmfile,0,SEEK_SET);
  3721. while( (line=abc_gets(h, mmfile)) ) {
  3722. for( p=line; isspace(*p); p++ ) ;
  3723. if( !strncmp(p,"X:",2) ) abcxcount++;
  3724. }
  3725. if( abcxcount == 0 )
  3726. abcstate = NOWHERE;
  3727. else
  3728. abcxwanted = (h->pickrandom - 1) % abcxcount;
  3729. abcxcount = 0;
  3730. mmfseek(mmfile,0,SEEK_SET);
  3731. }
  3732. while( mmsp > 0 ) {
  3733. mmsp--;
  3734. while((line=abc_gets(h, mmstack[mmsp]))) {
  3735. for( p=line; isspace(*p); p++ ) ;
  3736. switch(abcstate) {
  3737. case INSKIPFORX:
  3738. if( !strncmp(p,"X:",2) ) {
  3739. if( abcxcount++ != abcxwanted )
  3740. break;
  3741. }
  3742. // fall through
  3743. case INBETWEEN:
  3744. if( !strncmp(p,"X:",2) ) {
  3745. abcstate = INHEAD;
  3746. #ifdef NEWMIKMOD
  3747. of->songname = NULL;
  3748. #else
  3749. memset(m_szNames[0], 0, 32);
  3750. #endif
  3751. for( p+=2; isspace(*p); p++ ) ;
  3752. abcxnumber = atoi(p);
  3753. abchornpipe = 0;
  3754. h->droneon = 0;
  3755. h->dronegm = 71;
  3756. h->dronepitch[0] = 45;
  3757. h->dronepitch[1] = 33;
  3758. h->dronevol[0] = 80;
  3759. h->dronevol[1] = 80;
  3760. for( ttp = h->track; ttp; ttp=ttp->next ) {
  3761. ttp->vno = 0; // mark track unused
  3762. ttp->capostart = NULL;
  3763. }
  3764. h->tp = NULL; // forget old voices
  3765. h->tpc = NULL;
  3766. h->tpr = NULL;
  3767. global_voiceno = 0;
  3768. abc_set_parts(&abcparts, 0);
  3769. abcgrace = 0;
  3770. h->ktrans = 0;
  3771. ktempo = 0;
  3772. h->gchordon = 1;
  3773. h->drumon = 0;
  3774. global_songstart = h->tracktime;
  3775. abc_MIDI_beat(h, ""); // reset beat array
  3776. abc_MIDI_beatstring(h, ""); // reset beatstring
  3777. strcpy(h->gchord, ""); // reset gchord string
  3778. abcnolegato = 1; // reset legato switch
  3779. }
  3780. break;
  3781. case NOWHERE:
  3782. if( p[0] != '\0' && p[1] == ':' ) {
  3783. abcstate = INHEAD;
  3784. abc_set_parts(&abcparts, 0);
  3785. strcpy(h->gchord, "");
  3786. if( h->drumon && h->tpr ) abc_add_drum_sync(h, h->tpr, h->tracktime);
  3787. if( h->tpc && !h->gchordon ) abc_add_gchord_syncs(h, h->tpc, h->tracktime);
  3788. h->gchordon = 1;
  3789. h->drumon = 0;
  3790. }
  3791. else
  3792. break;
  3793. case INHEAD:
  3794. if( !strncmp(p,"L:",2) ) {
  3795. sscanf(p+2," %d / %d", &snotelen, &snotediv);
  3796. break;
  3797. }
  3798. if( !strncmp(p,"M:",2) ) {
  3799. abc_M_field(p+2, &mnotelen, &mnotediv);
  3800. break;
  3801. }
  3802. if( !strncmp(p,"P:",2) ) {
  3803. abc_set_parts(&abcparts, p+2);
  3804. break;
  3805. }
  3806. if( !strncmp(p,"Q:",2) ) {
  3807. abctempo = abc_extract_tempo(p+2,0);
  3808. ktempo = 1;
  3809. if( h->track ) {
  3810. // make h->tracktime start of a new age...
  3811. abc_add_partbreak(h, h->track, h->tracktime);
  3812. abc_add_tempo_event(h, h->track, h->tracktime, abctempo);
  3813. }
  3814. if( m_nDefaultTempo == 0 ) m_nDefaultTempo = abctempo;
  3815. break;
  3816. }
  3817. if( !strncmp(p,"T:",2) ) {
  3818. char buf[200];
  3819. if( strchr(p,'%') ) *strchr(p,'%') = '\0';
  3820. for( t=strlen(p)-1; isspace(p[t]); t-- )
  3821. p[t]='\0';
  3822. for( t=2; isspace(p[t]); t++ ) ;
  3823. #ifdef NEWMIKMOD
  3824. if( of->songname )
  3825. strcpy(buf,of->songname);
  3826. else
  3827. strcpy(buf,"");
  3828. #else
  3829. strcpy(buf,m_szNames[0]);
  3830. #endif
  3831. if( strlen(buf) + strlen(p+t) > 199 ) p[t+199-strlen(buf)] = '\0'; // chop it of
  3832. if( strlen(buf) ) strcat(buf," "); // add a space
  3833. strcat(buf, p+t);
  3834. #ifdef NEWMIKMOD
  3835. of->songname = DupStr(of->allochandle, buf, strlen(buf));
  3836. #else
  3837. if( strlen(buf) > 31 ) buf[31] = '\0'; // chop it of
  3838. strcpy(m_szNames[0], buf);
  3839. #endif
  3840. break;
  3841. }
  3842. if( !strncmp(p,"R:",2) ) {
  3843. for( p+=2; isspace(*p); p++ ) ;
  3844. if( !strncmp(p,"hornpipe",8) && (isspace(p[8]) || p[8]=='\0') ) abchornpipe = 1;
  3845. else abchornpipe = 0;
  3846. break;
  3847. }
  3848. if( !strncmp(p,"V:",2) ) {
  3849. for( t=2; p[t]==' '; t++ ) ;
  3850. h->tp = abc_locate_track(h, p+t, 0);
  3851. abcvol = h->tp->volume;
  3852. abcnolegato = !h->tp->legato;
  3853. if( !abcnolegato ) abcnoslurs = 0;
  3854. break;
  3855. }
  3856. if( !strncmp(p,"K:",2) ) {
  3857. abcstate = INBODY;
  3858. abckey = ABC_Key(p+2);
  3859. sprintf(barsig, "%s%s", sig[abckey], sig[abckey]); // reset the key signature
  3860. p = abc_skip_word(p+2);
  3861. h->ktrans = abc_transpose(p);
  3862. *p = '%'; // force skip rest of line
  3863. if( snotelen == 0 ) { // calculate default notelen from meter M:
  3864. if( mnotediv == 0 ) mnotediv = mnotelen = 1; // do'nt get nuked
  3865. snotelen = 100 * mnotelen / mnotediv;
  3866. if( snotelen > 74 )
  3867. snotediv = 8;
  3868. else
  3869. snotediv = 16;
  3870. snotelen = 1;
  3871. }
  3872. abceffect = none;
  3873. abceffoper = 0;
  3874. if( !(snotelen == 1 && snotediv == 8) ) abchornpipe = 0; // no matter what they said at R:
  3875. brokenrithm = 0;
  3876. global_part = ' ';
  3877. abcgrace = 0;
  3878. abcnoslurs = abcnolegato;
  3879. abcto = 0;
  3880. h->tpc = NULL; // reset chord track
  3881. tpd = NULL; // reset drone track
  3882. h->tpr = NULL; // reset drum track
  3883. if( !strlen(h->gchord) ) abc_metric_gchord(h, mnotelen, mnotediv);
  3884. h->barticks = notelen_notediv_to_ticks(h->speed, mnotelen, mnotediv);
  3885. if( abctempo && !ktempo ) { // did not set tempo in this songpiece so reset to abcrate
  3886. abctempo = 0;
  3887. global_tempo_factor = 2;
  3888. global_tempo_divider = 1;
  3889. if( h->track ) {
  3890. // make h->tracktime start of a new age...
  3891. abc_add_partbreak(h, h->track, h->tracktime);
  3892. abc_add_tempo_event(h, h->track, h->tracktime, abcrate);
  3893. }
  3894. if( m_nDefaultTempo == 0 ) m_nDefaultTempo = abcrate;
  3895. }
  3896. abc_init_partpat(partpat);
  3897. partpat[26][0] = abc_patno(h, h->tracktime);
  3898. partpat[26][1] = 0;
  3899. abc_globalslide(h, h->tracktime, 2); // reset all volumeslides
  3900. break;
  3901. }
  3902. if( !strlen(p) )
  3903. abcstate = INBETWEEN;
  3904. break;
  3905. case INSKIPFORQUOTE:
  3906. while( *p && *p != '"' )
  3907. p++;
  3908. if( *p == '\0' )
  3909. break;
  3910. abcstate = INBODY;
  3911. // fall through
  3912. case INBODY:
  3913. if( !strlen(p) && h->track ) { // end of this song
  3914. abcstate = h->pickrandom? INSKIPFORX: INBETWEEN;
  3915. // last but not least shut off all pending events
  3916. abc_recalculate_tracktime(h);
  3917. for( ttp=h->track; ttp; ttp=ttp->next )
  3918. abc_add_noteoff(h,ttp,h->tracktime);
  3919. abc_add_partbreak(h, h->track, h->tracktime);
  3920. t = abc_patno(h, h->tracktime);
  3921. if( abc_pattracktime(h, h->tracktime) % abcticks(64 * h->speed) ) t++;
  3922. if( global_part == ' ' ) {
  3923. partpat[26][1] = t;
  3924. if( abcparts ) {
  3925. for( t=0; t<26; t++ )
  3926. if( partpat[t][0] < partpat[t][1] ) break;
  3927. if( t == 26 ) {
  3928. abc_message("parts (%s) set but not used", abcparts);
  3929. abc_set_parts(&abcparts, 0); // forget the parts array
  3930. }
  3931. }
  3932. }
  3933. else
  3934. partpat[global_part - 'A'][1] = t;
  3935. if( !abcparts ) abc_song_to_parts(h, &abcparts, partpat);
  3936. orderlen = abc_partpat_to_orderlist(partpat, abcparts, h, &orderlist, orderlen);
  3937. }
  3938. if( !strncmp(p,"V:",2) ) {
  3939. for( t=2; p[t]==' '; t++ ) ;
  3940. h->tp = abc_locate_track(h, p+t, 0);
  3941. sprintf(barsig, "%s%s", sig[abckey], sig[abckey]); // reset the key signature
  3942. abcgrace = 0;
  3943. brokenrithm = 0;
  3944. h->tracktime = abc_tracktime(h->tp);
  3945. bartime = h->tracktime; // it is not friendly to break voices in the middle of a track...
  3946. abcnolegato = !h->tp->legato;
  3947. if( !abcnolegato ) abcnoslurs = 0;
  3948. *p = '%'; // make me skip the rest of the line....
  3949. }
  3950. if( !strncmp(p,"K:",2) ) {
  3951. abckey = ABC_Key(p+2);
  3952. sprintf(barsig, "%s%s", sig[abckey], sig[abckey]); // reset the key signature
  3953. p = abc_skip_word(p+2);
  3954. h->ktrans = abc_transpose(p);
  3955. *p = '%'; // make me skip the rest of the line....
  3956. }
  3957. if( !strncmp(p,"L:",2) ) {
  3958. sscanf(p+2," %d / %d", &snotelen, &snotediv);
  3959. *p = '%'; // make me skip the rest of the line....
  3960. }
  3961. if( !strncmp(p,"M:",2) ) {
  3962. abc_M_field(p+2, &mnotelen, &mnotediv);
  3963. h->barticks = notelen_notediv_to_ticks(h->speed, mnotelen, mnotediv);
  3964. *p = '%'; // make me skip the rest of the line....
  3965. }
  3966. if( !strncmp(p,"Q:",2) ) {
  3967. abctempo = abc_extract_tempo(p+2,ch0=='\\');
  3968. if( !h->track ) {
  3969. h->tp = abc_check_track(h, h->track);
  3970. h->tp->vno = 0; // mark reuseable (temporarely, until first notes come up)
  3971. }
  3972. abc_add_tempo_event(h, h->track, h->tracktime, abctempo);
  3973. *p = '%'; // make me skip the rest of the line....
  3974. }
  3975. if( !strncmp(p,"T:",2) ) {
  3976. char buf[200];
  3977. if( strchr(p,'%') ) *strchr(p,'%') = '\0';
  3978. for( t=strlen(p)-1; isspace(p[t]); t-- )
  3979. p[t]='\0';
  3980. for( t=2; isspace(p[t]); t++ ) ;
  3981. #ifdef NEWMIKMOD
  3982. if( of->songname )
  3983. strcpy(buf,of->songname);
  3984. else
  3985. strcpy(buf,"");
  3986. #else
  3987. strcpy(buf,m_szNames[0]);
  3988. #endif
  3989. if( strlen(buf) + strlen(p+t) > 198 ) p[t+198-strlen(buf)] = '\0'; // chop it of
  3990. if( strlen(buf) ) strcat(buf," "); // add a space
  3991. strcat(buf, p+t);
  3992. #ifdef NEWMIKMOD
  3993. of->songname = DupStr(of->allochandle, buf, strlen(buf));
  3994. #else
  3995. if( strlen(buf) > 31 ) buf[31] = '\0'; // chop it of
  3996. strcpy(m_szNames[0], buf);
  3997. #endif
  3998. *p = '%'; // make me skip the rest of the line....
  3999. }
  4000. break;
  4001. }
  4002. if( !strncmp(p,"m:",2) ) {
  4003. if( abcstate != INSKIPFORX ) {
  4004. char *pm;
  4005. pm = abc_continuated(h, mmstack[mmsp], p);
  4006. abc_new_macro(h, pm+2);
  4007. if( pm != p ) {
  4008. free(pm);
  4009. if( h->tp ) abcnolegato = !h->tp->legato;
  4010. if( !abcnolegato ) abcnoslurs = 0;
  4011. }
  4012. }
  4013. *p = '%'; // skip rest of line
  4014. }
  4015. if( !strncmp(p,"U:",2) ) {
  4016. abc_new_umacro(h, p+2);
  4017. *p = '%'; // skip rest of line
  4018. }
  4019. if( !strncmp(p,"w:",2) ) { // inline lyrics
  4020. *p = '%'; // skip rest of line
  4021. }
  4022. if( !strncmp(p,"W:",2) ) { // lyrics at end of song body
  4023. *p = '%'; // skip rest of line
  4024. }
  4025. if( !strncmp(p,"d:",2) ) { // oldstyle decorations
  4026. abc_message("warning: old style decorations not handled\n%s", p);
  4027. *p = '%'; // skip rest of line
  4028. }
  4029. if( !strncmp(p,"s:",2) ) { // newstyle decorations (symbols)
  4030. abc_message("warning: new style decorations not handled\n%s", p);
  4031. *p = '%'; // skip rest of line
  4032. }
  4033. if( !strncmp(p,"I:",2) && abcstate != INSKIPFORX ) { // handle like oldstyle '%%command' lines
  4034. p[0]= '%';
  4035. p[1]= '%';
  4036. }
  4037. if( !strncmp(p,"%%",2) ) {
  4038. for( p+=2; *p && isspace(*p); p++ ) ;
  4039. if( !strncmp(p,"abc-include",11) && isspace(p[11]) ) {
  4040. for( t=12; isspace(p[t]); t++ ) ;
  4041. if( p[t] ) {
  4042. mmsp++;
  4043. if( mmsp == MAXABCINCLUDES ) {
  4044. mmsp--;
  4045. abc_message("failure: too many abc-include's, %s", &p[t]);
  4046. } else {
  4047. mmstack[mmsp] = mmfopen(&p[t], "r");
  4048. if( !mmstack[mmsp] ) {
  4049. mmsp--;
  4050. abc_message("failure: abc-include file %s not found", &p[t]);
  4051. }
  4052. }
  4053. }
  4054. else abc_message("failure: abc-include missing file name, %s", p);
  4055. }
  4056. if( !strncmp(p,"MIDI",4) && (p[4]=='=' || isspace(p[4])) && abcstate != INSKIPFORX ) {
  4057. for( p+=5; *p && isspace(*p); p++ ) ;
  4058. if( *p == '=' )
  4059. for( p+=1; *p && isspace(*p); p++ ) ;
  4060. abc_MIDI_command(h,p,'%');
  4061. if( h->tp ) abcnolegato = !h->tp->legato;
  4062. if( !abcnolegato ) abcnoslurs = 0;
  4063. }
  4064. if(*p) *p = '%'; // skip rest of line
  4065. }
  4066. if( abcstate == INBODY ) {
  4067. if( *p == 'P' && p[1] == ':' ) { // a line with a part indication
  4068. if( abcparts != NULL ) {
  4069. // make h->tracktime start of a new age...
  4070. if( !h->track ) {
  4071. h->tp = abc_check_track(h, h->track);
  4072. h->tp->vno = 0; // mark reuseable (temporarely, until first notes come up)
  4073. }
  4074. h->tracktime = h->track? abc_tracktime(h->track): 0; // global parts are voice independent
  4075. abc_add_partbreak(h, h->track, h->tracktime);
  4076. t = abc_patno(h, h->tracktime);
  4077. if( global_part == ' ' ) {
  4078. partpat[26][1] = t;
  4079. if( abcparts ) {
  4080. for( t=0; t<26; t++ )
  4081. if( partpat[t][0] < partpat[t][1] ) break;
  4082. if( t == 26 ) {
  4083. abc_message("parts (%s) set but not used", abcparts);
  4084. abc_set_parts(&abcparts, 0); // forget the parts array
  4085. }
  4086. }
  4087. }
  4088. else
  4089. partpat[global_part - 'A'][1] = t;
  4090. // give every new coming abcevent the desired part indication
  4091. while( p[2]==' ' || p[2]=='.' ) p++; // skip blancs and dots
  4092. if( isupper(p[2]) )
  4093. global_part = p[2];
  4094. else
  4095. global_part = ' ';
  4096. if( global_part == ' ' )
  4097. partpat[26][0] = t;
  4098. else
  4099. partpat[global_part - 'A'][0] = t;
  4100. }
  4101. *p = '%'; // make me skip the rest of the line....
  4102. }
  4103. if( h->droneon && !tpd ) {
  4104. tpd = h->track;
  4105. if( tpd ) {
  4106. tpd = abc_locate_track(h, tpd->v, DRONEPOS1);
  4107. tpd->instr = h->dronegm;
  4108. abc_add_dronenote(h, tpd, h->tracktime, h->dronepitch[0], h->dronevol[0]);
  4109. tpd = abc_locate_track(h, tpd->v, DRONEPOS2);
  4110. tpd->instr = h->dronegm;
  4111. abc_add_dronenote(h, tpd, h->tracktime, h->dronepitch[1], h->dronevol[1]);
  4112. }
  4113. }
  4114. if( tpd && !h->droneon ) {
  4115. tpd = abc_locate_track(h, tpd->v, DRONEPOS1);
  4116. abc_add_noteoff(h, tpd, h->tracktime);
  4117. tpd = abc_locate_track(h, tpd->v, DRONEPOS2);
  4118. abc_add_noteoff(h, tpd, h->tracktime);
  4119. tpd = NULL;
  4120. }
  4121. if( h->drumon && !h->tpr ) {
  4122. h->tpr = h->track;
  4123. if( h->tpr ) abc_add_drum_sync(h, h->tpr, h->tracktime); // don't start drumming from the beginning of time!
  4124. }
  4125. if( h->tpr && !h->drumon ) h->tpr = NULL;
  4126. if( *p && *p != '%' ) { // skip uninteresting lines
  4127. // plough thru the songline gathering mos....
  4128. ch0 = ' ';
  4129. pp = 0;
  4130. while( (ch = *p++) ) {
  4131. if( !pp && isalpha(ch) && *p != ':' ) { // maybe a macro
  4132. for( mp=h->umacro; mp; mp=mp->next ) {
  4133. if( ch == mp->name[0] ) {
  4134. pp = p;
  4135. p = mp->subst;
  4136. ch = *p;
  4137. if( ch ) p++;
  4138. break;
  4139. }
  4140. }
  4141. }
  4142. switch(ch) {
  4143. case '%':
  4144. abcto = 0;
  4145. while( *p ) p++;
  4146. break;
  4147. case '[': // chord follows or some inline field
  4148. abcto = 0;
  4149. if( *p=='|' ) break; // [| a thick-thin bar line, loop around and let case '|' handle it
  4150. if( !strncmp(p,"V:",2) ) { // inline voice change
  4151. for( t=2; isspace(p[t]); t++ ) ;
  4152. h->tp = abc_locate_track(h, p+t, 0);
  4153. for( ; *p && *p != ']'; p++ ) ;
  4154. abcgrace = 0;
  4155. brokenrithm = 0;
  4156. sprintf(barsig, "%s%s", sig[abckey], sig[abckey]); // reset the key signature
  4157. h->tracktime = abc_tracktime(h->tp);
  4158. bartime = h->tracktime; // it is not wise to break voices in the middle of a track...
  4159. abcvol = h->tp->volume;
  4160. abcnolegato = !h->tp->legato;
  4161. if( !abcnolegato ) abcnoslurs = 0;
  4162. break;
  4163. }
  4164. if( !strncmp(p,"K:",2) ) {
  4165. abckey = ABC_Key(p+2);
  4166. sprintf(barsig, "%s%s", sig[abckey], sig[abckey]); // reset the key signature
  4167. p = abc_skip_word(p+2);
  4168. h->ktrans = abc_transpose(p);
  4169. for( ; *p && *p != ']'; p++ ) ;
  4170. break;
  4171. }
  4172. if( !strncmp(p,"M:",2) ) {
  4173. abc_M_field(p+2, &mnotelen, &mnotediv);
  4174. for( ; *p && *p != ']'; p++ ) ;
  4175. h->barticks = notelen_notediv_to_ticks(h->speed, mnotelen, mnotediv);
  4176. break;
  4177. }
  4178. if( !strncmp(p,"P:",2) ) { // a [P:X] field inline
  4179. if( abcparts != NULL ) {
  4180. // make h->tracktime start of a new age...
  4181. if( h->track )
  4182. abc_add_partbreak(h, h->track, h->tracktime);
  4183. t = abc_patno(h, h->tracktime);
  4184. if( global_part == ' ' )
  4185. partpat[26][1] = t;
  4186. else
  4187. partpat[global_part - 'A'][1] = t;
  4188. // give every new coming abcevent the desired part indication
  4189. while( isspace(p[2]) || p[2]=='.' ) p++; // skip blancs and dots
  4190. if( isupper(p[2]) )
  4191. global_part = p[2];
  4192. else
  4193. global_part = ' ';
  4194. if( global_part == ' ' )
  4195. partpat[26][0] = t;
  4196. else
  4197. partpat[global_part - 'A'][0] = t;
  4198. }
  4199. for( ; *p && *p != ']'; p++ ) ;
  4200. break;
  4201. }
  4202. if( !strncmp(p,"Q:",2) ) {
  4203. abctempo = abc_extract_tempo(p+2,1);
  4204. for( ; *p && *p != ']'; p++ ) ;
  4205. if( h->track )
  4206. abc_add_tempo_event(h, h->track, h->tracktime, abctempo);
  4207. break;
  4208. }
  4209. if( !strncmp(p,"I:",2) ) { // interpret some of the possibilitys
  4210. for( p += 2; isspace(*p); p++ ) ;
  4211. if( !strncmp(p,"MIDI",4) && (p[4]=='=' || isspace(p[4])) ) { // interpret some of the possibilitys
  4212. for( p += 4; isspace(*p); p++ ) ;
  4213. if( *p == '=' )
  4214. for( p += 1; isspace(*p); p++ ) ;
  4215. abc_MIDI_command(h, p, ']');
  4216. if( h->tp ) abcnolegato = !h->tp->legato;
  4217. if( !abcnolegato ) abcnoslurs = 0;
  4218. }
  4219. for( ; *p && *p != ']'; p++ ) ; // skip rest of inline field
  4220. }
  4221. if( *p && p[1] == ':' ) { // some other kind of inline field
  4222. for( ; *p && *p != ']'; p++ ) ;
  4223. break;
  4224. }
  4225. if( *p && strchr("abcdefgABCDEFG^_=",*p) ) {
  4226. int cnl[8],cnd[8],vnl,nl0=0,nd0=0; // for chords with notes of varying length
  4227. abcchord = 0;
  4228. vnl = 0;
  4229. h->tp = abc_check_track(h, h->tp);
  4230. abc_track_clear_tiedvpos(h);
  4231. abcbeatvol = abc_beat_vol(h, abcvol, (h->tracktime - bartime)/notelen_notediv_to_ticks(h->speed,1,mnotediv));
  4232. while( (ch=*p++) && (ch != ']') ) {
  4233. h->tp = abc_locate_track(h, h->tp->v, abcchord? abcchord+DRONEPOS2: 0);
  4234. p += abc_add_noteon(h, ch, p, h->tracktime, barsig, abcbeatvol, abceffect, abceffoper);
  4235. p += abc_notelen(p, &notelen, &notediv);
  4236. if( *p == '-' ) {
  4237. p++;
  4238. if( h->tp->tail && h->tp->tail->flg != 1 )
  4239. h->tp->tienote = h->tp->tail;
  4240. }
  4241. if( abcchord<8 ) {
  4242. cnl[abcchord] = notelen;
  4243. cnd[abcchord] = notediv;
  4244. }
  4245. if( abcchord==0 ) {
  4246. cnotelen = notelen;
  4247. cnotediv = notediv;
  4248. nl0 = notelen;
  4249. nd0 = notediv;
  4250. }
  4251. else {
  4252. if( cnotelen != notelen || cnotediv != notediv ) {
  4253. vnl = 1;
  4254. // update to longest duration
  4255. if( cnotelen * notediv < notelen * cnotediv ) {
  4256. cnotelen = notelen;
  4257. cnotediv = notediv;
  4258. abc_track_untie_short_chordnotes(h);
  4259. }
  4260. if( cnotelen * notediv > notelen * cnotediv ) {
  4261. if( h->tp->tienote ) {
  4262. abc_message("short notes in chord can not be tied:\n%s", h->line);
  4263. h->tp->tienote = 0; // short chord notes cannot be tied...
  4264. }
  4265. }
  4266. // update to shortest duration
  4267. if( nl0 * notediv > notelen * nd0 ) {
  4268. nl0 = notelen;
  4269. nd0 = notediv;
  4270. }
  4271. }
  4272. }
  4273. abcchord++;
  4274. }
  4275. p += abc_notelen(p, &notelen, &notediv);
  4276. if( (ch = *p) == '-' ) p++; // tied chord...
  4277. if( abcarpeggio ) { // update starttime in the noteon events...
  4278. thistime = notelen_notediv_to_ticks(h->speed, nl0*notelen*snotelen, nd0*notediv*snotediv)/abcchord;
  4279. if( thistime > abcticks(h->speed) ) thistime = abcticks(h->speed);
  4280. for( nl0=1; nl0<abcchord; nl0++ ) {
  4281. h->tp = abc_locate_track(h, h->tp->v, nl0+DRONEPOS2);
  4282. if( h->tp->tail )
  4283. h->tp->tail->tracktick = h->tracktime + thistime * nl0;
  4284. }
  4285. }
  4286. notelen *= cnotelen;
  4287. notediv *= cnotediv;
  4288. tupletr = abc_tuplet(&notelen, &notediv, tupletp, tupletq, tupletr);
  4289. while( isspace(*p) ) p++; // allow spacing in broken rithm notation
  4290. p += abc_brokenrithm(p, &notelen, &notediv, &brokenrithm, abchornpipe);
  4291. thistime = notelen_notediv_to_ticks(h->speed, notelen*snotelen, notediv*snotediv);
  4292. if( abcfermata ) {
  4293. thistime <<= 1;
  4294. abcfermata = 0;
  4295. }
  4296. if( thistime > abcgrace ) {
  4297. thistime -= abcgrace;
  4298. abcgrace = 0;
  4299. }
  4300. else {
  4301. abcgrace -= thistime;
  4302. thistime = abcticks(h->speed);
  4303. abcgrace += abcticks(h->speed);
  4304. }
  4305. h->tracktime += thistime;
  4306. while( abcchord>0 ) {
  4307. abcchord--;
  4308. h->tp = abc_locate_track(h, h->tp->v, abcchord? abcchord+DRONEPOS2: 0);
  4309. if( vnl && (abcchord < 8) && (cnl[abcchord] != cnotelen || cnd[abcchord] != cnotediv) ) {
  4310. abc_add_noteoff(h, h->tp,
  4311. h->tracktime - thistime
  4312. + (thistime * cnl[abcchord] * cnotediv)/(cnd[abcchord] * cnotelen) );
  4313. }
  4314. else {
  4315. if( ch=='-' && h->tp->tail && h->tp->tail->flg != 1 )
  4316. h->tp->tienote = h->tp->tail; // copy noteon event to tienote in track
  4317. if( thistime > abcticks(h->speed) )
  4318. abc_add_noteoff(h, h->tp, h->tracktime - abcnoslurs);
  4319. else
  4320. abc_add_noteoff(h, h->tp, h->tracktime);
  4321. }
  4322. }
  4323. if( h->gchordon && (h->tp == h->tpc) )
  4324. abc_add_gchord(h, h->tracktime, bartime);
  4325. if( h->drumon && (h->tp == h->tpr) )
  4326. abc_add_drum(h, h->tracktime, bartime);
  4327. abcarpeggio = 0;
  4328. if( abceffoper != 255 ) abceffect = none;
  4329. break;
  4330. }
  4331. if( isdigit(*p) ) { // different endings in repeats [i,j,n-r,s,...
  4332. h->tp = abc_check_track(h, h->tp);
  4333. abc_add_partbreak(h, h->tp, h->tracktime);
  4334. p += abc_getnumber(p, &notelen);
  4335. abc_add_variant_start(h, h->tp, h->tracktime, notelen);
  4336. while( *p==',' || *p=='-' ) {
  4337. if( *p==',' ) {
  4338. p++;
  4339. p += abc_getnumber(p, &notelen);
  4340. abc_add_variant_choise(h->tp, notelen);
  4341. }
  4342. else {
  4343. p++;
  4344. p += abc_getnumber(p, &notediv);
  4345. while( notelen < notediv ) {
  4346. notelen++;
  4347. abc_add_variant_choise(h->tp, notelen);
  4348. }
  4349. }
  4350. }
  4351. break;
  4352. }
  4353. // collect the notes in the chord
  4354. break;
  4355. case '(': // slurs follow or some tuplet (duplet, triplet etc.)
  4356. abcto = 0;
  4357. if( isdigit(*p) ) {
  4358. p += abc_getnumber(p,&tupletp);
  4359. tupletr = tupletp; // ABC draft 2.0 (4.13): if r is not given it defaults to p
  4360. switch( tupletp ) { // ABC draft 2.0 (4.13): q defaults depending on p and time signature
  4361. case 2: case 4: case 8:
  4362. tupletq = 3;
  4363. break;
  4364. case 3: case 6:
  4365. tupletq = 2;
  4366. break;
  4367. default:
  4368. if( snotediv == 8 )
  4369. tupletq = 3;
  4370. else
  4371. tupletq = 2;
  4372. break;
  4373. }
  4374. if( *p==':' ) {
  4375. p++;
  4376. if( isdigit(*p) ) p += abc_getnumber(p,&tupletq);
  4377. if( *p==':' ) {
  4378. p++;
  4379. if( isdigit(*p) ) p += abc_getnumber(p,&tupletr);
  4380. }
  4381. }
  4382. }
  4383. else
  4384. abcnoslurs=0;
  4385. break;
  4386. case ')': // end of slurs
  4387. abcto = 0;
  4388. abcnoslurs = abcnolegato;
  4389. break;
  4390. case '{': // grace notes follow
  4391. abcto = 0;
  4392. h->tp = abc_check_track(h, h->tp);
  4393. abc_track_clear_tiedvpos(h);
  4394. abcgrace = 0;
  4395. abcbeatvol = abc_beat_vol(h, abcvol, (h->tracktime - bartime)/notelen_notediv_to_ticks(h->speed,1,mnotediv));
  4396. while( (ch=*p++) && (ch != '}') ) {
  4397. p += abc_add_noteon(h, ch, p, h->tracktime+abcgrace, barsig, abcbeatvol, none, 0);
  4398. p += abc_notelen(p, &notelen, &notediv);
  4399. if( *p=='-' ) {
  4400. p++;
  4401. if( h->tp->tail->flg != 1 )
  4402. h->tp->tienote = h->tp->tail;
  4403. }
  4404. notediv *= 4; // grace notes factor 4 shorter (1/8 => 1/32)
  4405. abcgrace += notelen_notediv_to_ticks(h->speed, notelen*snotelen, notediv*snotediv);
  4406. abc_add_noteoff(h, h->tp, h->tracktime + abcgrace);
  4407. }
  4408. h->tracktime += abcgrace;
  4409. abc_add_sync(h, h->tp, h->tracktime);
  4410. if( h->gchordon && (h->tp == h->tpc) )
  4411. abc_add_gchord(h, h->tracktime, bartime);
  4412. if( h->drumon && (h->tp == h->tpr) )
  4413. abc_add_drum(h, h->tracktime, bartime);
  4414. break;
  4415. case '|': // bar symbols
  4416. abcto = 0;
  4417. if( h->gchordon && h->tp && (h->tp == h->tpc) )
  4418. abc_add_gchord(h, h->tracktime, bartime);
  4419. if( h->drumon && (h->tp == h->tpr) )
  4420. abc_add_drum(h, h->tracktime, bartime);
  4421. sprintf(barsig, "%s%s", sig[abckey], sig[abckey]); // reset the key signature
  4422. bartime = h->tracktime;
  4423. if( h->tp && h->tp->vpos ) h->tp = abc_locate_track(h, h->tp->v, 0); // reset from voice overlay
  4424. if( isdigit(*p) ) { // different endings in repeats |i,j,n-r,s,...
  4425. h->tp = abc_check_track(h, h->tp);
  4426. abc_add_partbreak(h, h->tp, h->tracktime);
  4427. p += abc_getnumber(p, &notelen);
  4428. abc_add_variant_start(h, h->tp, h->tracktime, notelen);
  4429. while( *p==',' || *p=='-' ) {
  4430. if( *p==',' ) {
  4431. p++;
  4432. p += abc_getnumber(p, &notelen);
  4433. abc_add_variant_choise(h->tp, notelen);
  4434. }
  4435. else {
  4436. p++;
  4437. p += abc_getnumber(p, &notediv);
  4438. while( notelen < notediv ) {
  4439. notelen++;
  4440. abc_add_variant_choise(h->tp, notelen);
  4441. }
  4442. }
  4443. }
  4444. break;
  4445. }
  4446. if( *p==':' ) { // repeat start
  4447. p++;
  4448. h->tp = abc_check_track(h, h->tp);
  4449. abc_add_partbreak(h, h->tp, h->tracktime);
  4450. abc_add_setloop(h, h->tp, h->tracktime);
  4451. }
  4452. break;
  4453. case '&': // voice overlay
  4454. abcto = 0;
  4455. h->tracktime = bartime;
  4456. h->tp = abc_check_track(h, h->tp);
  4457. t = h->tp->vpos;
  4458. h->tp = abc_locate_track(h, h->tp->v, t? t+1: DRONEPOS2+1);
  4459. break;
  4460. case ']': // staff break, end of song
  4461. abcto = 0;
  4462. break;
  4463. case ':': // repeat jump
  4464. abcto = 0;
  4465. h->tp = abc_check_track(h, h->tp);
  4466. j = (h->tp->slidevol == -2)? jumpfade: jumpnormal;
  4467. abc_add_setjumploop(h, h->tp, h->tracktime, j);
  4468. abc_add_partbreak(h, h->tp, h->tracktime);
  4469. if( *p==':' ) { // repeat start without intermediate bar symbol
  4470. p++;
  4471. abc_add_setloop(h, h->tp, h->tracktime);
  4472. }
  4473. break;
  4474. case '"': // chord notation
  4475. if( !strchr("_^<>@", *p) && !isdigit(*p) ) { // if it's not a annotation string
  4476. h->tp = abc_check_track(h, h->tp);
  4477. if( !h->tpc ) h->tpc = abc_locate_track(h, h->tp->v, 0);
  4478. if( h->tp == h->tpc ) abc_add_chord(p, h, h->tpc, h->tracktime); // only do chords for one voice
  4479. }
  4480. abcto = 0;
  4481. while( (ch=*p++) && (ch != '"') ) {
  4482. if( !strncasecmp(p,"fade",4) && h->track && h->track->slidevol > -2 )
  4483. abc_globalslide(h, h->tracktime, -2); // set volumeslide to fade away...
  4484. if( !strncasecmp(p,"to coda",7) ) {
  4485. h->tp = abc_check_track(h, h->tp);
  4486. abc_add_partbreak(h, h->tp, h->tracktime);
  4487. abc_add_tocoda(h, h->tp, h->tracktime);
  4488. p+=7;
  4489. abcto = -1;
  4490. }
  4491. else
  4492. if( !isspace(*p) ) abcto = 0;
  4493. if( !strncasecmp(p,"to",2) && (isspace(p[2]) || p[2] == '"') ) abcto = 1;
  4494. }
  4495. if( !ch ) abcstate = INSKIPFORQUOTE;
  4496. break;
  4497. case '\\': // skip the rest of this line, should be the end of the line anyway
  4498. while( *p ) p++;
  4499. ch = '\\'; // remember for invoice tempo changes....
  4500. break;
  4501. case '!': // line break, or deprecated old style decoration
  4502. case '+': // decorations new style
  4503. if( !strncmp(p,"coda",4) && p[4] == ch ) {
  4504. h->tp = abc_check_track(h, h->tp);
  4505. if( abcto ) {
  4506. if( abcto > 0 ) {
  4507. abc_add_partbreak(h, h->tp, h->tracktime);
  4508. abc_add_tocoda(h, h->tp, h->tracktime);
  4509. }
  4510. }
  4511. else {
  4512. abc_add_partbreak(h, h->tp, h->tracktime);
  4513. abc_add_coda(h, h->tp, h->tracktime);
  4514. }
  4515. p += 5;
  4516. abcto = 0;
  4517. break;
  4518. }
  4519. abcto = 0;
  4520. if( !strncmp(p,"arpeggio",8) && p[8] == ch ) {
  4521. abcarpeggio = 1;
  4522. p += 9;
  4523. break;
  4524. }
  4525. if( !strncmp(p,"crescendo(",10) && p[10] == ch ) {
  4526. h->tp = abc_check_track(h, h->tp);
  4527. abc_globalslide(h, h->tracktime, 1);
  4528. p += 11;
  4529. break;
  4530. }
  4531. if( !strncmp(p,"crescendo)",10) && p[10] == ch ) {
  4532. h->tp = abc_check_track(h, h->tp);
  4533. abc_globalslide(h, h->tracktime, 0);
  4534. p += 11;
  4535. break;
  4536. }
  4537. if( !strncmp(p,"<(",2) && p[2] == ch ) {
  4538. h->tp = abc_check_track(h, h->tp);
  4539. abc_globalslide(h, h->tracktime, 1);
  4540. p += 3;
  4541. break;
  4542. }
  4543. if( !strncmp(p,"<)",2) && p[2] == ch ) {
  4544. h->tp = abc_check_track(h, h->tp);
  4545. abc_globalslide(h, h->tracktime, 0);
  4546. p += 3;
  4547. break;
  4548. }
  4549. if( !strncmp(p,"dimimuendo(",11) && p[11] == ch ) {
  4550. h->tp = abc_check_track(h, h->tp);
  4551. abc_globalslide(h, h->tracktime, -1);
  4552. p += 12;
  4553. break;
  4554. }
  4555. if( !strncmp(p,"diminuendo)",11) && p[11] == ch ) {
  4556. h->tp = abc_check_track(h, h->tp);
  4557. abc_globalslide(h, h->tracktime, 0);
  4558. p += 12;
  4559. break;
  4560. }
  4561. if( !strncmp(p,">(",2) && p[2] == ch ) {
  4562. h->tp = abc_check_track(h, h->tp);
  4563. abc_globalslide(h, h->tracktime, -1);
  4564. p += 3;
  4565. break;
  4566. }
  4567. if( !strncmp(p,">)",2) && p[2] == ch ) {
  4568. h->tp = abc_check_track(h, h->tp);
  4569. abc_globalslide(h, h->tracktime, 0);
  4570. p += 3;
  4571. break;
  4572. }
  4573. if( !strncmp(p,"upbow",5) && p[5] == ch ) {
  4574. abceffect = bow;
  4575. abceffoper = 1;
  4576. p += 6;
  4577. break;
  4578. }
  4579. if( !strncmp(p,"downbow",7) && p[7] == ch ) {
  4580. abceffect = bow;
  4581. abceffoper = 0;
  4582. p += 8;
  4583. break;
  4584. }
  4585. if( !strncmp(p,"trill",5) && p[5] == ch ) {
  4586. abceffect = trill;
  4587. abceffoper = 0;
  4588. p += 6;
  4589. break;
  4590. }
  4591. if( !strncmp(p,"trill(",6) && p[6] == ch ) {
  4592. abceffect = trill;
  4593. abceffoper = 255;
  4594. p += 7;
  4595. break;
  4596. }
  4597. if( !strncmp(p,"trill)",6) && p[6] == ch ) {
  4598. abceffect = none;
  4599. abceffoper = 0;
  4600. p += 7;
  4601. break;
  4602. }
  4603. if( !strncmp(p,"accent",6) && p[6] == ch ) {
  4604. abceffect = accent;
  4605. abceffoper = 0;
  4606. p += 7;
  4607. break;
  4608. }
  4609. if( !strncmp(p,"emphasis",8) && p[8] == ch ) {
  4610. abceffect = accent;
  4611. abceffoper = 0;
  4612. p += 9;
  4613. break;
  4614. }
  4615. if( !strncmp(p,">",1) && p[1] == ch ) {
  4616. abceffect = accent;
  4617. abceffoper = 0;
  4618. p += 2;
  4619. break;
  4620. }
  4621. if( !strncmp(p,"fermata",7) && p[7] == ch ) {
  4622. abcfermata = 1;
  4623. p += 8;
  4624. break;
  4625. }
  4626. if( !strncmp(p,"fine",4) && p[4] == ch ) {
  4627. h->tp = abc_check_track(h, h->tp);
  4628. abc_add_partbreak(h, h->tp, h->tracktime);
  4629. abc_add_fine(h, h->tp, h->tracktime);
  4630. p += 5;
  4631. break;
  4632. }
  4633. if( !strncmp(p,"segno",5) && p[5] == ch ) {
  4634. h->tp = abc_check_track(h, h->tp);
  4635. abc_add_partbreak(h, h->tp, h->tracktime);
  4636. abc_add_segno(h, h->tp, h->tracktime);
  4637. p += 6;
  4638. break;
  4639. }
  4640. if( !strncmp(p,"tocoda",6) && p[6] == ch ) {
  4641. h->tp = abc_check_track(h, h->tp);
  4642. abc_add_partbreak(h, h->tp, h->tracktime);
  4643. abc_add_tocoda(h, h->tp, h->tracktime);
  4644. p += 7;
  4645. break;
  4646. }
  4647. if( !strncmp(p,"D.C.",4) && p[4] == ch ) {
  4648. h->tp = abc_check_track(h, h->tp);
  4649. j = (h->tp->slidevol == -2)? jumpdcfade: jumpdacapo;
  4650. abc_add_setjumploop(h, h->tp, h->tracktime, j);
  4651. abc_add_partbreak(h, h->tp, h->tracktime);
  4652. p += 5;
  4653. break;
  4654. }
  4655. if( !strncmp(p,"D.S.",4) && p[4] == ch ) {
  4656. h->tp = abc_check_track(h, h->tp);
  4657. j = (h->tp->slidevol == -2)? jumpdsfade: jumpdasegno;
  4658. abc_add_setjumploop(h, h->tp, h->tracktime, j);
  4659. abc_add_partbreak(h, h->tp, h->tracktime);
  4660. p += 5;
  4661. break;
  4662. }
  4663. if( !strncmp(p,"dacapo",6) && p[6] == ch ) {
  4664. h->tp = abc_check_track(h, h->tp);
  4665. j = (h->tp->slidevol == -2)? jumpdcfade: jumpdacapo;
  4666. abc_add_setjumploop(h, h->tp, h->tracktime, j);
  4667. abc_add_partbreak(h, h->tp, h->tracktime);
  4668. p += 7;
  4669. break;
  4670. }
  4671. if( !strncmp(p,"dacoda",6) && p[6] == ch ) {
  4672. h->tp = abc_check_track(h, h->tp);
  4673. j = (h->tp->slidevol == -2)? jumpdcfade: jumpdacapo;
  4674. abc_add_setjumploop(h, h->tp, h->tracktime, j);
  4675. abc_add_partbreak(h, h->tp, h->tracktime);
  4676. p += 7;
  4677. break;
  4678. }
  4679. if( ch == '!' ) {
  4680. for( t=0; p[t] && strchr("|[:]!",p[t])==0 && !isspace(p[t]); t++ ) ;
  4681. if( p[t] == '!' ) { // volume and other decorations, deprecated
  4682. h->tp = abc_check_track(h, h->tp);
  4683. abcvol = abc_parse_decorations(h, h->tp, p);
  4684. p = &p[t+1];
  4685. }
  4686. }
  4687. else {
  4688. h->tp = abc_check_track(h, h->tp);
  4689. abcvol = abc_parse_decorations(h, h->tp, p);
  4690. while( (ch=*p++) && (ch != '+') )
  4691. ;
  4692. }
  4693. break;
  4694. case '`': // back quotes are for readability
  4695. break;
  4696. case '.': // staccato marks
  4697. break;
  4698. default: // some kinda note must follow
  4699. if( strchr("abcdefgABCDEFG^_=X",ch) ) {
  4700. h->tp = abc_check_track(h, h->tp);
  4701. abc_track_clear_tiedvpos(h);
  4702. abcbeatvol = abc_beat_vol(h, abcvol, (h->tracktime - bartime)/notelen_notediv_to_ticks(h->speed,1,mnotediv));
  4703. p += abc_add_noteon(h, ch, p, h->tracktime, barsig, abcbeatvol, abceffect, abceffoper);
  4704. if( abceffoper != 255 ) abceffect = none;
  4705. p += abc_notelen(p, &notelen, &notediv);
  4706. if( *p=='-' ) {
  4707. p++;
  4708. if( h->tp->tail && h->tp->tail->flg != 1 )
  4709. h->tp->tienote = h->tp->tail;
  4710. }
  4711. tupletr = abc_tuplet(&notelen, &notediv, tupletp, tupletq, tupletr);
  4712. while( isspace(*p) ) p++; // allow spacing in broken rithm notation
  4713. p += abc_brokenrithm(p, &notelen, &notediv, &brokenrithm, abchornpipe);
  4714. thistime = notelen_notediv_to_ticks(h->speed, notelen*snotelen, notediv*snotediv);
  4715. if( abcfermata ) {
  4716. thistime <<= 1;
  4717. abcfermata = 0;
  4718. }
  4719. if( thistime > abcgrace ) {
  4720. thistime -= abcgrace;
  4721. abcgrace = 0;
  4722. }
  4723. else {
  4724. abcgrace -= thistime;
  4725. thistime = abcticks(h->speed);
  4726. abcgrace += abcticks(h->speed);
  4727. }
  4728. h->tracktime += thistime;
  4729. if( thistime > abcticks(h->speed) )
  4730. abc_add_noteoff(h, h->tp, h->tracktime - abcnoslurs - (( ch0 == '.')? thistime / 2: 0));
  4731. else
  4732. abc_add_noteoff(h, h->tp, h->tracktime);
  4733. abc_add_sync(h, h->tp, h->tracktime);
  4734. if( h->gchordon && (h->tp == h->tpc) )
  4735. abc_add_gchord(h, h->tracktime, bartime);
  4736. if( h->drumon && (h->tp == h->tpr) )
  4737. abc_add_drum(h, h->tracktime, bartime);
  4738. abcarpeggio = 0;
  4739. break;
  4740. }
  4741. if( strchr("zx",ch) ) {
  4742. h->tp = abc_check_track(h, h->tp);
  4743. abc_track_clear_tiednote(h);
  4744. p += abc_notelen(p, &notelen, &notediv);
  4745. tupletr = abc_tuplet(&notelen, &notediv, tupletp, tupletq, tupletr);
  4746. while( isspace(*p) ) p++; // allow spacing in broken rithm notation
  4747. p += abc_brokenrithm(p, &notelen, &notediv, &brokenrithm, abchornpipe);
  4748. thistime = notelen_notediv_to_ticks(h->speed, notelen*snotelen, notediv*snotediv);
  4749. if( abcfermata ) {
  4750. thistime <<= 1;
  4751. abcfermata = 0;
  4752. }
  4753. if( thistime > abcgrace ) {
  4754. thistime -= abcgrace;
  4755. abcgrace = 0;
  4756. }
  4757. else {
  4758. abcgrace -= thistime;
  4759. thistime = abcticks(h->speed);
  4760. abcgrace += abcticks(h->speed);
  4761. }
  4762. h->tracktime += thistime;
  4763. abc_add_sync(h, h->tp, h->tracktime);
  4764. if( h->gchordon && (h->tp == h->tpc) )
  4765. abc_add_gchord(h, h->tracktime, bartime);
  4766. if( h->drumon && (h->tp == h->tpr) )
  4767. abc_add_drum(h, h->tracktime, bartime);
  4768. abcarpeggio = 0;
  4769. break;
  4770. }
  4771. if( strchr("Z",ch) ) {
  4772. h->tp = abc_check_track(h, h->tp);
  4773. abc_track_clear_tiednote(h);
  4774. p += abc_notelen(p, &notelen, &notediv);
  4775. thistime = notelen_notediv_to_ticks(h->speed, notelen*mnotelen, notediv*mnotediv);
  4776. if( abcfermata ) {
  4777. thistime <<= 1;
  4778. abcfermata = 0;
  4779. }
  4780. if( thistime > abcgrace ) {
  4781. thistime -= abcgrace;
  4782. abcgrace = 0;
  4783. }
  4784. else {
  4785. abcgrace -= thistime;
  4786. thistime = abcticks(h->speed);
  4787. abcgrace += abcticks(h->speed);
  4788. }
  4789. h->tracktime += thistime;
  4790. sprintf(barsig, "%s%s", sig[abckey], sig[abckey]); // reset the key signature
  4791. abc_add_sync(h, h->tp, h->tracktime);
  4792. if( h->gchordon && (h->tp == h->tpc) )
  4793. abc_add_gchord(h, h->tracktime, bartime);
  4794. if( h->drumon && (h->tp == h->tpr) )
  4795. abc_add_drum(h, h->tracktime, bartime);
  4796. abcarpeggio = 0;
  4797. break;
  4798. }
  4799. if( isalpha(ch) && *p==':' ) {
  4800. // some unprocessed field line?
  4801. while( *p ) p++; // skip it
  4802. break;
  4803. }
  4804. break;
  4805. }
  4806. ch0 = ch; // remember previous char, can be staccato dot...
  4807. if( pp ) { // did we have a U: macro substitution?
  4808. if( !*p ) {
  4809. p = pp;
  4810. pp = 0;
  4811. }
  4812. }
  4813. }
  4814. }
  4815. }
  4816. }
  4817. if( mmsp ) mmfclose(mmstack[mmsp]);
  4818. }
  4819. ABC_CleanupMacros(h); // we dont need them anymore
  4820. if( !h->track ) {
  4821. char buf[10];
  4822. sprintf(buf,"%u",abcxnumber);
  4823. abc_message("abc X:%s has no body", buf);
  4824. h->track = abc_check_track(h, h->track); // for sanity...
  4825. }
  4826. if( abcstate == INBODY ) {
  4827. // last but not least shut off all pending events
  4828. abc_recalculate_tracktime(h);
  4829. for( ttp=h->track; ttp; ttp=ttp->next )
  4830. abc_add_noteoff(h,ttp,h->tracktime);
  4831. abc_add_partbreak(h, h->track, h->tracktime);
  4832. t = abc_patno(h, h->tracktime);
  4833. if( abc_pattracktime(h, h->tracktime) % abcticks(64 * h->speed) ) t++;
  4834. if( global_part == ' ' ) {
  4835. partpat[26][1] = t;
  4836. if( abcparts ) {
  4837. for( t=0; t<26; t++ )
  4838. if( partpat[t][0] < partpat[t][1] ) break;
  4839. if( t == 26 ) {
  4840. abc_message("parts (%s) set but not used", abcparts);
  4841. abc_set_parts(&abcparts, 0); // forget the parts array
  4842. }
  4843. }
  4844. }
  4845. else
  4846. partpat[global_part - 'A'][1] = t;
  4847. if( !abcparts ) abc_song_to_parts(h, &abcparts, partpat);
  4848. orderlen = abc_partpat_to_orderlist(partpat, abcparts, h, &orderlist, orderlen);
  4849. }
  4850. abc_synchronise_tracks(h); // distribute all control events
  4851. abc_recalculate_tracktime(h);
  4852. /*
  4853. abctrack:
  4854. tracktick long
  4855. note byte
  4856. octave byte
  4857. instrument byte
  4858. effects byte
  4859. tick = tracktick modulo speed
  4860. row = (tracktick div speed) modulo 64
  4861. pat = (tracktick div speed) div 64
  4862. ord = calculated
  4863. */
  4864. if( (p=getenv(ABC_ENV_DUMPTRACKS)) ) {
  4865. printf("P:%s\n",abcparts);
  4866. for( t=0; t<26; t++ )
  4867. if( partpat[t][1] >= partpat[t][0] )
  4868. printf(" %c ",t+'A');
  4869. if( partpat[26][1] >= partpat[26][0] )
  4870. printf("All");
  4871. printf("\n");
  4872. for( t=0; t<27; t++ )
  4873. if( partpat[t][1] >= partpat[t][0] )
  4874. printf("%3d ",partpat[t][0]);
  4875. printf("\n");
  4876. for( t=0; t<27; t++ )
  4877. if( partpat[t][1] >= partpat[t][0] )
  4878. printf("%3d ",partpat[t][1]);
  4879. printf("\n");
  4880. for( t=0; (int)t<orderlen; t++ )
  4881. printf("%3d ",t);
  4882. printf("\n");
  4883. for( t=0; (int)t<orderlen; t++ )
  4884. printf("%3d ",orderlist[t]);
  4885. printf("\n");
  4886. abc_dumptracks(h,p);
  4887. }
  4888. // set module variables
  4889. if( abctempo == 0 ) abctempo = abcrate;
  4890. if( m_nDefaultTempo == 0 ) m_nDefaultTempo = abctempo;
  4891. #ifdef NEWMIKMOD
  4892. of->memsize = PTMEM_LAST; // Number of memory slots to reserve!
  4893. of->modtype = _mm_strdup(of->allochandle, ABC_Version);
  4894. of->numpat = 1+(modticks(h->tracktime) / h->speed / 64);
  4895. of->numpos = orderlen;
  4896. of->reppos = 0;
  4897. of->initspeed = h->speed;
  4898. of->numchn = abc_numtracks(h);
  4899. of->numtrk = of->numpat * of->numchn;
  4900. of->initvolume = 64;
  4901. of->pansep = 128;
  4902. // orderlist
  4903. if(!AllocPositions(of, orderlen)) {
  4904. avoid_reentry = 0;
  4905. return FALSE;
  4906. }
  4907. for(t=0; t<orderlen; t++)
  4908. of->positions[t] = orderlist[t];
  4909. _mmalloc_close(h->ho); // get rid of orderlist memory
  4910. #else
  4911. m_nType = MOD_TYPE_ABC;
  4912. numpat = 1+(modticks(h->tracktime) / h->speed / 64);
  4913. if( numpat > MAX_PATTERNS )
  4914. numpat = MAX_PATTERNS;
  4915. m_nDefaultSpeed = h->speed;
  4916. m_nChannels = abc_numtracks(h);
  4917. m_dwSongFlags = SONG_LINEARSLIDES;
  4918. m_nMinPeriod = 28 << 2;
  4919. m_nMaxPeriod = 1712 << 3;
  4920. // orderlist
  4921. for(t=0; t < (uint32_t)orderlen; t++){
  4922. if( t >= MAX_ORDERS )
  4923. break;
  4924. Order[t] = orderlist[t];
  4925. }
  4926. free(orderlist); // get rid of orderlist memory
  4927. #endif
  4928. #ifdef NEWMIKMOD
  4929. // ==============================
  4930. // Load the pattern info now!
  4931. if(!AllocTracks(of)) return 0;
  4932. if(!AllocPatterns(of)) return 0;
  4933. of->ut = utrk_init(of->numchn, h->allochandle);
  4934. utrk_memory_reset(of->ut);
  4935. utrk_local_memflag(of->ut, PTMEM_PORTAMENTO, TRUE, FALSE);
  4936. ABC_ReadPatterns(of, h, of->numpat);
  4937. // load instruments after building the patterns (chan == 10 track handling)
  4938. if( !PAT_Load_Instruments(of) ) {
  4939. avoid_reentry = 0;
  4940. return FALSE;
  4941. }
  4942. // ============================================================
  4943. // set panning positions
  4944. for(t=0; t<of->numchn; t++) {
  4945. of->panning[t] = PAN_LEFT+((t+2)%5)*((PAN_RIGHT - PAN_LEFT)/5); // 0x30 = std s3m val
  4946. }
  4947. #else
  4948. // ==============================
  4949. // Load the pattern info now!
  4950. if( ABC_ReadPatterns(Patterns, PatternSize, h, numpat, m_nChannels) ) {
  4951. // :^( need one more channel to handle the global events ;^b
  4952. m_nChannels++;
  4953. h->tp = abc_locate_track(h, "", 99);
  4954. abc_add_sync(h, h->tp, h->tracktime);
  4955. for( t=0; t<numpat; t++ ) {
  4956. FreePattern(Patterns[t]);
  4957. Patterns[t] = NULL;
  4958. }
  4959. ABC_ReadPatterns(Patterns, PatternSize, h, numpat, m_nChannels);
  4960. }
  4961. // load instruments after building the patterns (chan == 10 track handling)
  4962. if( !PAT_Load_Instruments(this) ) {
  4963. avoid_reentry = 0;
  4964. return FALSE;
  4965. }
  4966. // ============================================================
  4967. // set panning positions
  4968. if( m_nChannels > MAX_BASECHANNELS )
  4969. m_nChannels = MAX_BASECHANNELS;
  4970. for(t=0; t<m_nChannels; t++) {
  4971. ChnSettings[t].nPan = 0x30+((t+2)%5)*((0xD0 - 0x30)/5); // 0x30 = std s3m val
  4972. ChnSettings[t].nVolume = 64;
  4973. }
  4974. #endif
  4975. avoid_reentry = 0; // it is safe now, I'm finished
  4976. abc_set_parts(&abcparts, 0); // free the parts array
  4977. #ifndef NEWMIKMOD
  4978. ABC_Cleanup(h); // we dont need it anymore
  4979. #endif
  4980. return 1;
  4981. }
  4982. #ifdef NEWMIKMOD
  4983. // =====================================================================================
  4984. CHAR *ABC_LoadTitle(MMSTREAM *mmfile)
  4985. // =====================================================================================
  4986. {
  4987. char s[128];
  4988. int i;
  4989. // get the first line with T:songtitle
  4990. _mm_fseek(mmfile,0,SEEK_SET);
  4991. while(abc_fgets(mmfile,s,128)) {
  4992. if( s[0]=='T' && s[1]==':' ) {
  4993. for( i=2; s[i]==' '; i++ ) ;
  4994. return(DupStr(NULL, s+i,strlen(s+i)));
  4995. }
  4996. }
  4997. return NULL;
  4998. }
  4999. MLOADER load_abc =
  5000. {
  5001. "ABC",
  5002. "ABC draft 2.0",
  5003. 0x30,
  5004. NULL,
  5005. ABC_Test,
  5006. (void *(*)(void))ABC_Init,
  5007. (void (*)(ML_HANDLE *))ABC_Cleanup,
  5008. /* Every single loader seems to need one of these! */
  5009. (BOOL (*)(ML_HANDLE *, UNIMOD *, MMSTREAM *))ABC_Load,
  5010. ABC_LoadTitle
  5011. };
  5012. #endif