libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmXScore.c 115KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmComplexTypes.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmXml.h"
  12. #include "cmText.h"
  13. #include "cmFileSys.h"
  14. #include "cmXScore.h"
  15. #include "cmTime.h"
  16. #include "cmMidi.h"
  17. #include "cmMidiFile.h"
  18. #include "cmLex.h"
  19. #include "cmCsv.h"
  20. #include "cmSymTbl.h"
  21. #include "cmScore.h"
  22. #include "cmFile.h"
  23. #include "cmSymTbl.h"
  24. #include "cmAudioFile.h"
  25. #include "cmAudioFile.h"
  26. #include "cmProcObj.h"
  27. #include "cmProcTemplate.h"
  28. #include "cmProc.h"
  29. #include "cmProc2.h"
  30. #include "cmProc5.h"
  31. #include "cmSvgWriter.h"
  32. cmXsH_t cmXsNullHandle = cmSTATIC_NULL_HANDLE;
  33. enum
  34. {
  35. kSectionXsFl = 0x00000001, // rvalue holds section number
  36. kBarXsFl = 0x00000002,
  37. kRestXsFl = 0x00000004,
  38. kGraceXsFl = 0x00000008,
  39. kDotXsFl = 0x00000010,
  40. kChordXsFl = 0x00000020,
  41. kDynXsFl = 0x00000040,
  42. kEvenXsFl = 0x00000080,
  43. kTempoXsFl = 0x00000100,
  44. kHeelXsFl = 0x00000200,
  45. kTieBegXsFl = 0x00000400,
  46. kTieEndXsFl = 0x00000800,
  47. kTieProcXsFl = 0x00001000,
  48. kDampDnXsFl = 0x00002000,
  49. kDampUpXsFl = 0x00004000,
  50. kDampUpDnXsFl = 0x00008000,
  51. kSostDnXsFl = 0x00010000,
  52. kSostUpXsFl = 0x00020000,
  53. kMetronomeXsFl = 0x00040000, // duration holds BPM
  54. kOnsetXsFl = 0x00080000, // this is a sounding note
  55. kBegGroupXsFl = 0x00100000,
  56. kEndGroupXsFl = 0x00200000,
  57. kBegGraceXsFl = 0x00400000, // (b) beg grace note group
  58. kEndGraceXsFl = 0x00800000, // end grace note group
  59. kAddGraceXsFl = 0x01000000, // (a) end grace note group operator flag - add time
  60. kSubGraceXsFl = 0x02000000, // (s) " " " " " " - subtract time
  61. kAFirstGraceXsFl = 0x04000000, // (A) add time after first note
  62. kNFirstGraceXsFl = 0x08000000, // (n) grace notes start as soon as possible after first note and add time
  63. kDeleteXsFl = 0x10000000,
  64. kDynBegForkXsFl = 0x20000000,
  65. kDynEndForkXsFl = 0x40000000,
  66. kDynEndXsFl = 0x100000000,
  67. kEvenEndXsFl = 0x200000000,
  68. kTempoEndXsFl = 0x400000000
  69. };
  70. struct cmXsMeas_str;
  71. struct cmXsVoice_str;
  72. // Values measured for each sounding note in the preceding time window....
  73. typedef struct cmXsComplexity_str
  74. {
  75. unsigned sum_d_vel; // sum of first order difference of cmXsNote_t.dynamics
  76. unsigned sum_d_rym; // sum of first order difference of cmXsNote_t.rvalue
  77. unsigned sum_d_lpch; // sum of first order difference of cmXsNote_t.pitch of note assigned to the bass cleff
  78. unsigned sum_n_lpch; // count of notes assigned to the bass cleff
  79. unsigned sum_d_rpch; // sum of first order difference of cmXsNote_t.pitch of note assigned to the treble cleff
  80. unsigned sum_n_rpch; // count of notes assigned to the treble cleff
  81. } cmXsComplexity_t;
  82. typedef struct cmXsNote_str
  83. {
  84. unsigned uid; // unique id of this note record
  85. unsigned long long flags; // See k???XsFl
  86. unsigned pitch; // midi pitch
  87. unsigned dynamics; // dynamic level 1=pppp 9=fff
  88. unsigned vel; // score specified MIDI velocity
  89. cmChar_t step; // A-G
  90. unsigned octave; // sci pitch octave
  91. int alter; // +n=sharps,-n=flats
  92. unsigned staff; // 1=treble 2=bass
  93. unsigned tick; //
  94. unsigned duration; // duration in ticks
  95. unsigned tied_dur; // duration in ticks (including all tied notes)
  96. double secs; // absolute time in seconds
  97. double dsecs; // delta time in seconds since previous event
  98. unsigned locIdx; // location index (chords share the same location index)
  99. double rvalue; // 1/rvalue = rythmic value (1/0.5 double whole 1/1 whole 1/2 half 1/4=quarter note, 1/8=eighth note, ...)
  100. const cmChar_t* tvalue; // text value
  101. unsigned evenGroupId; // eveness group id
  102. unsigned dynGroupId; // dynamics group id
  103. unsigned tempoGroupId; // tempo group id
  104. unsigned graceGroupId; // grace note group id
  105. struct cmXsVoice_str* voice; // voice this note belongs to
  106. struct cmXsMeas_str* meas; // measure this note belongs to
  107. const cmXmlNode_t* xmlNode; // note xml ptr
  108. struct cmXsNote_str* tied; // subsequent note tied to this note
  109. struct cmXsNote_str* grace; // grace note groups link backward in time from the anchor note
  110. struct cmXsNote_str* mlink; // measure note list
  111. struct cmXsNote_str* slink; // time sorted event list
  112. cmXsComplexity_t cplx;
  113. } cmXsNote_t;
  114. typedef struct cmXsVoice_str
  115. {
  116. unsigned id; // Voice id
  117. cmXsNote_t* noteL; // List of notes in this voice
  118. struct cmXsVoice_str* link; // Link to other voices in this measure
  119. } cmXsVoice_t;
  120. typedef struct cmXsSpan_str
  121. {
  122. unsigned staff;
  123. unsigned number;
  124. struct cmXsMeas_str* meas;
  125. unsigned tick0;
  126. unsigned tick1;
  127. int pitch_offset;
  128. struct cmXsSpan_str* link;
  129. } cmXsSpan_t;
  130. typedef struct cmXsMeas_str
  131. {
  132. unsigned number; // Measure number
  133. unsigned divisions; // ticks-per-quarter-note
  134. unsigned beats; // beats per measure
  135. unsigned beat_type; // whole/half/quarter/eighth ...
  136. cmXsVoice_t* voiceL; // List of voices in this measure
  137. cmXsNote_t* noteL; // List of time sorted notes in this measure
  138. struct cmXsMeas_str* link; // Link to other measures in this part.
  139. } cmXsMeas_t;
  140. typedef struct cmXsPart_str
  141. {
  142. const cmChar_t* idStr; // Id of this part
  143. cmXsMeas_t* measL; // List of measures in this part.
  144. struct cmXsPart_str* link; // Link to other parts in this score
  145. } cmXsPart_t;
  146. typedef struct
  147. {
  148. cmErr_t err;
  149. cmXmlH_t xmlH;
  150. cmLHeapH_t lhH;
  151. cmXsPart_t* partL;
  152. cmCsvH_t csvH;
  153. cmXsSpan_t* spanL;
  154. unsigned nextUid;
  155. cmXsComplexity_t cplx_max;
  156. } cmXScore_t;
  157. void _cmXScoreReport( cmXScore_t* p, cmRpt_t* rpt, bool sortFl );
  158. cmXScore_t* _cmXScoreHandleToPtr( cmXsH_t h )
  159. {
  160. cmXScore_t* p = (cmXScore_t*)h.h;
  161. assert( p != NULL );
  162. return p;
  163. }
  164. cmXsRC_t _cmXScoreFinalize( cmXScore_t* p )
  165. {
  166. cmXsRC_t rc = kOkXsRC;
  167. // release the XML file
  168. cmXmlFree( &p->xmlH );
  169. // release the local linked heap memory
  170. cmLHeapDestroy(&p->lhH);
  171. // release the CSV output object
  172. cmCsvFinalize(&p->csvH);
  173. cmMemFree(p);
  174. return rc;
  175. }
  176. cmXsRC_t _cmXScoreMissingNode( cmXScore_t* p, const cmXmlNode_t* parent, const cmChar_t* label )
  177. {
  178. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s'. Parent line:%i",label,parent->line);
  179. }
  180. cmXsRC_t _cmXScoreMissingAttribute( cmXScore_t* p, const cmXmlNode_t* np, const cmChar_t* attrLabel )
  181. {
  182. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML attribute '%s' from node '%s'.",attrLabel,np->label);
  183. }
  184. cmXsVoice_t* _cmXScoreIdToVoice( cmXsMeas_t* meas, unsigned voiceId )
  185. {
  186. cmXsVoice_t* v = meas->voiceL;
  187. for(; v!=NULL; v=v->link)
  188. if( v->id == voiceId )
  189. return v;
  190. return NULL;
  191. }
  192. cmXsRC_t _cmXScorePushNote( cmXScore_t* p, cmXsMeas_t* meas, unsigned voiceId, cmXsNote_t* note )
  193. {
  194. cmXsVoice_t* v;
  195. // get the voice recd
  196. if((v = _cmXScoreIdToVoice(meas,voiceId)) == NULL)
  197. {
  198. // the voice recd doesn't exist for this voiceId - allocate one
  199. v = cmLhAllocZ(p->lhH,cmXsVoice_t,1);
  200. v->id = voiceId;
  201. // add the voice record to the meas->voiceL
  202. if( meas->voiceL == NULL )
  203. meas->voiceL = v;
  204. else
  205. {
  206. cmXsVoice_t* vp = meas->voiceL;
  207. while( vp->link!=NULL )
  208. vp = vp->link;
  209. vp->link = v;
  210. }
  211. }
  212. // add the note record to the end of meas->voiceL
  213. if( v->noteL == NULL )
  214. v->noteL = note;
  215. else
  216. {
  217. cmXsNote_t* n = v->noteL;
  218. while( n->mlink != NULL )
  219. n = n->mlink;
  220. n->mlink = note;
  221. }
  222. note->voice = v;
  223. note->uid = p->nextUid++;
  224. note->tied_dur = note->duration;
  225. return kOkXsRC;
  226. }
  227. void _cmXScoreRemoveNote( cmXsNote_t* note )
  228. {
  229. cmXsNote_t* n0 = NULL;
  230. cmXsNote_t* n1 = note->voice->noteL;
  231. for(; n1!=NULL; n1=n1->mlink)
  232. if( n1->uid == note->uid )
  233. {
  234. if( n0 == NULL )
  235. note->voice->noteL = NULL;
  236. else
  237. n0->mlink = n1->mlink;
  238. break;
  239. }
  240. n0 = NULL;
  241. n1 = note->meas->noteL;
  242. for(; n1!=NULL; n1=n1->slink)
  243. if( n1->uid == note->uid )
  244. {
  245. if( n0 == NULL )
  246. note->voice->noteL = NULL;
  247. else
  248. n0->slink = n1->slink;
  249. break;
  250. }
  251. }
  252. void _cmXScoreInsertNoteBefore( cmXsNote_t* note, cmXsNote_t* nn )
  253. {
  254. // insert the new note into the voice list before 'note'
  255. cmXsNote_t* n0 = NULL;
  256. cmXsNote_t* n1 = note->voice->noteL;
  257. for(; n1 != NULL; n1=n1->mlink )
  258. {
  259. if( n1->uid == note->uid )
  260. {
  261. if( n0 == NULL )
  262. note->voice->noteL = nn;
  263. else
  264. n0->mlink = nn;
  265. nn->mlink = note;
  266. break;
  267. }
  268. n0 = n1;
  269. }
  270. assert( n1 != NULL );
  271. // insert the new note into the time sorted note list before 'note'
  272. n0 = NULL;
  273. n1 = note->meas->noteL;
  274. for(; n1!=NULL; n1=n1->slink)
  275. {
  276. if( n1->tick >= nn->tick )
  277. {
  278. if( n0 == NULL )
  279. note->meas->noteL = nn;
  280. else
  281. n0->slink = nn;
  282. nn->slink = n1;
  283. break;
  284. }
  285. n0 = n1;
  286. }
  287. assert( n1 != NULL );
  288. }
  289. void _cmXScoreInsertNoteAfter( cmXsNote_t* note, cmXsNote_t* nn )
  290. {
  291. // insert the new note into the voice list after 'note'
  292. cmXsNote_t* n0 = note->voice->noteL;
  293. for(; n0 != NULL; n0=n0->mlink )
  294. if( n0->uid == note->uid )
  295. {
  296. nn->mlink = note->mlink;
  297. note->mlink = nn;
  298. break;
  299. }
  300. assert(n0 != NULL );
  301. // insert the new note into the time sorted note list after 'note'
  302. n0 = note->meas->noteL;
  303. for(; n0!=NULL; n0=n0->slink)
  304. {
  305. if( n0->tick >= nn->tick )
  306. {
  307. nn->slink = n0->slink;
  308. n0->slink = nn;
  309. break;
  310. }
  311. // if n0 is the last ele in the list
  312. if( n0->slink == NULL )
  313. {
  314. n0->slink = nn;
  315. nn->slink = NULL;
  316. break;
  317. }
  318. }
  319. assert(n0 != NULL);
  320. }
  321. cmXsRC_t _cmXScoreParsePartList( cmXScore_t* p )
  322. {
  323. cmXsRC_t rc = kOkXsRC;
  324. cmXsPart_t* lastPartPtr = NULL;
  325. const cmXmlNode_t* xnp;
  326. // find the 'part-list'
  327. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part-list", NULL, 0)) == NULL )
  328. return _cmXScoreMissingNode(p,cmXmlRoot(p->xmlH),"part-list");
  329. const cmXmlNode_t* cnp = xnp->children;
  330. // for each child of the 'part-list'
  331. for(; cnp!=NULL; cnp=cnp->sibling)
  332. if( cmTextCmp( cnp->label, "score-part" ) == 0 )
  333. {
  334. const cmXmlAttr_t* a;
  335. // find the 'score-part' id
  336. if((a = cmXmlFindAttrib(cnp,"id")) == NULL )
  337. return _cmXScoreMissingAttribute(p,cnp,"id");
  338. // allocate a new part record
  339. cmXsPart_t* pp = cmLhAllocZ(p->lhH,cmXsPart_t,1);
  340. pp->idStr = a->value; // set the part id
  341. // link the new part record to the end of the part list
  342. if(lastPartPtr == NULL)
  343. p->partL = pp;
  344. else
  345. lastPartPtr->link = pp;
  346. lastPartPtr = pp;
  347. }
  348. return rc;
  349. }
  350. cmXsRC_t _cmXScoreParsePitch( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* np )
  351. {
  352. cmXsRC_t rc = kOkXsRC;
  353. unsigned octave = 0;
  354. double alter = 0;
  355. const cmChar_t* step = NULL;
  356. if((step = cmXmlNodeValue(nnp,"pitch","step",NULL)) == NULL )
  357. return _cmXScoreMissingNode(p,nnp,"step");
  358. if((rc = cmXmlNodeUInt( nnp,&octave,"pitch","octave",NULL)) != kOkXmlRC )
  359. return _cmXScoreMissingNode(p,nnp,"octave");
  360. cmXmlNodeDouble( nnp,&alter,"pitch","alter",NULL);
  361. int acc = alter;
  362. unsigned midi = cmSciPitchToMidiPitch(*step,acc,octave);
  363. np->pitch = midi;
  364. np->step = *step;
  365. np->octave = octave;
  366. np->alter = alter;
  367. np->flags |= kOnsetXsFl;
  368. return rc;
  369. }
  370. cmXsRC_t _cmXScoreParseNoteRValue( cmXScore_t* p, const cmXmlNode_t* nnp, const cmChar_t* label, double* rvalueRef )
  371. {
  372. typedef struct map_str
  373. {
  374. double rvalue;
  375. const cmChar_t* label;
  376. } map_t;
  377. map_t mapV[] =
  378. {
  379. {-1.0, "measure" }, // whole measure rest
  380. { 0.5, "breve" }, // double whole
  381. { 1.0, "whole" },
  382. { 2.0, "half" },
  383. { 4.0, "quarter" },
  384. { 8.0, "eighth" },
  385. {16.0, "16th" },
  386. {32.0, "32nd" },
  387. { 64.0, "64th" },
  388. {128.0, "128th" },
  389. { 0.0, "" }
  390. };
  391. const cmChar_t* str;
  392. // get the XML rvalue label
  393. if((str = cmXmlNodeValue(nnp,label,NULL)) == NULL)
  394. {
  395. if((nnp = cmXmlSearch(nnp,"rest",NULL,0)) != NULL )
  396. {
  397. const cmXmlAttr_t* a;
  398. if((a = cmXmlFindAttrib(nnp,"measure")) != NULL && cmTextCmp(a->value,"yes")==0)
  399. {
  400. *rvalueRef = -1;
  401. return kOkXsRC;
  402. }
  403. }
  404. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'beat-unit' metronome value is missing on line %i.",nnp->line);
  405. }
  406. unsigned i;
  407. // lookup the rvalue numeric value from the mapV[] table
  408. for(i=0; mapV[i].rvalue!=0; ++i)
  409. if( cmTextCmp(mapV[i].label,str) == 0 )
  410. {
  411. *rvalueRef = mapV[i].rvalue;
  412. return kOkXsRC;
  413. }
  414. // the rvalue label was not found
  415. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unknown rvalue type='%s'.",str);
  416. }
  417. cmXsRC_t _cmXScoreParseColor( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* note )
  418. {
  419. cmXsRC_t rc = kOkXsRC;
  420. const cmXmlAttr_t* a;
  421. typedef struct map_str
  422. {
  423. unsigned long long value;
  424. const cmChar_t* label;
  425. } map_t;
  426. map_t mapV[] =
  427. {
  428. { kEvenXsFl, "#0000FF" }, // blue (even)
  429. { kEvenXsFl | kEvenEndXsFl, "#0000FE" }, // blue (even end)
  430. { kEvenXsFl | kEvenEndXsFl, "#0000FD" }, // blue (even end)
  431. { kTempoXsFl, "#00FF00" }, // green (tempo)
  432. { kTempoXsFl | kTempoEndXsFl, "#00FE00" }, // green (tempo end)
  433. { kDynXsFl, "#FF0000" }, // red (dynamics)
  434. { kDynXsFl | kDynEndXsFl, "#FE0000" }, // red (dynamics end)
  435. { kDynXsFl | kDynEndXsFl, "#FD0000" }, // red (dynamics end)
  436. { kTempoXsFl | kEvenXsFl, "#00FFFF" }, // green + blue (turquoise)
  437. { kTempoXsFl | kEvenXsFl | kEvenEndXsFl, "#00FFFE" }, // green + blue (turquoise) (end)
  438. { kDynXsFl | kEvenXsFl, "#FF00FF" }, // red + blue
  439. { kDynXsFl | kEvenXsFl | kEvenEndXsFl, "#FF00FE" }, // red + blue (end)
  440. { kDynXsFl | kEvenXsFl | kEvenEndXsFl, "#FF00FD" }, // red + blue (end)
  441. { kDynXsFl | kEvenXsFl, "#FF0CF7" }, // magenta (even+dyn)
  442. { kDynXsFl | kTempoXsFl, "#FF7F00" }, // red + green (brown)
  443. { kDynXsFl | kTempoXsFl, "#FE7F00" }, // red + green (brown) (end)
  444. { kDynXsFl | kTempoXsFl, "#FD7F00" }, // red + green (brown) (end)
  445. { kTempoXsFl | kEvenXsFl | kDynXsFl, "#996633" }, // (purple)
  446. { kTempoXsFl | kEvenXsFl | kDynXsFl | kDynEndXsFl, "#996632" }, // (purple)
  447. { kDynXsFl, "#FF6A03" }, // 176 orange (dynamics)
  448. { kEvenXsFl, "#2F00E8" }, // 1001 blue (even)
  449. { kTempoXsFl, "#01CD1F" }, // 1196 green (tempo)
  450. { kEvenXsFl, "#3600E8" }, // 1627 blue (even)
  451. { kDynXsFl | kTempoXsFl, "#9E8F15" }, // 8827 brown (dyn + tempo)
  452. { kEvenXsFl, "#2E00E6" }, // 5393 blue (even)
  453. { kEvenXsFl, "#2C00DD" }, // 5895 blue (even)
  454. { kDynXsFl, "#FF5B03" }, // 6498 orange (dyn)
  455. { kDynXsFl, "#FF6104" }, // 6896 orange
  456. { kEvenXsFl, "#2A00E6" }, // 7781 blue
  457. { kEvenXsFl, "#2300DD" }, // 8300 blue (even)
  458. { kTempoXsFl, "#03CD22" }, // 10820 green (tempo)
  459. { kEvenXsFl, "#3400DB" }, // 11627 blue (dyn)
  460. { -1, "" }
  461. };
  462. /*
  463. orange #FF6A03
  464. magenta #FF0CF7
  465. blue #2F00E8
  466. green #01CD1F
  467. gold #9E8F15
  468. green #03CD22
  469. */
  470. if((a = cmXmlFindAttrib(nnp, "color" )) != NULL )
  471. {
  472. unsigned i;
  473. for(i=0; mapV[i].value != -1; ++i)
  474. if( cmTextCmp(a->value,mapV[i].label) == 0 )
  475. {
  476. note->flags += mapV[i].value;
  477. break;
  478. }
  479. if( mapV[i].value == -1 )
  480. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The note color '%s' was not found on line %i.",a->value,nnp->line);
  481. }
  482. return rc;
  483. }
  484. // On input tick0Ref is set to the tick of the previous event.
  485. // On input tickRef is set to the tick of this event.
  486. // On output tick0Ref is set to the tick of this event.
  487. // On output tickRef is set to the tick of the next event.
  488. cmXsRC_t _cmXScoreParseNote(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* nnp, unsigned* tick0Ref, unsigned* tickRef )
  489. {
  490. cmXsRC_t rc = kOkXsRC;
  491. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  492. unsigned voiceId;
  493. note->meas = meas;
  494. note->xmlNode = nnp;
  495. // get the voice id for this node
  496. if( cmXmlNodeUInt(nnp,&voiceId,"voice",NULL) != kOkXmlRC )
  497. return _cmXScoreMissingNode(p,nnp,"voice");
  498. // if this note has a pitch
  499. if( cmXmlNodeHasChild(nnp,"pitch",NULL) )
  500. if((rc = _cmXScoreParsePitch(p,nnp,note)) != kOkXsRC )
  501. return rc;
  502. cmXmlNodeUInt(nnp,&note->duration,"duration",NULL); // get the note duration
  503. cmXmlNodeUInt(nnp,&note->staff,"staff",NULL); // get th staff number
  504. // is 'rest'
  505. if( cmXmlNodeHasChild(nnp,"rest",NULL) )
  506. note->flags |= kRestXsFl;
  507. // is 'grace'
  508. if( cmXmlNodeHasChild(nnp,"grace",NULL) )
  509. note->flags |= kGraceXsFl;
  510. // is 'dot'
  511. if( cmXmlNodeHasChild(nnp,"dot",NULL) )
  512. note->flags |= kDotXsFl;
  513. // is 'chord'
  514. if( cmXmlNodeHasChild(nnp,"chord",NULL) )
  515. note->flags |= kChordXsFl;
  516. // is this is first note in a tied pair
  517. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","start",NULL) )
  518. note->flags |= kTieBegXsFl;
  519. // is this is second note in a tied pair
  520. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","stop",NULL) )
  521. note->flags |= kTieEndXsFl;
  522. // has 'heel' mark
  523. if( cmXmlNodeHasChild(nnp,"notations","technical","heel",NULL) )
  524. note->flags |= kHeelXsFl;
  525. // set color coded flags
  526. if((rc = _cmXScoreParseColor(p, nnp, note )) != kOkXsRC )
  527. return rc;
  528. // get the note's rythmic value
  529. if((rc = _cmXScoreParseNoteRValue(p,nnp,"type",&note->rvalue)) != kOkXsRC )
  530. return rc;
  531. // if this is a chord note
  532. if( cmIsFlag(note->flags,kChordXsFl) )
  533. {
  534. note->tick = *tick0Ref; // then use the onset time from the previous note and do not advance time
  535. }
  536. else
  537. {
  538. *tick0Ref = *tickRef;
  539. note->tick = *tickRef;
  540. *tickRef += note->duration;
  541. }
  542. return _cmXScorePushNote(p, meas, voiceId, note );
  543. }
  544. cmXsRC_t _cmXScorePushNonNote( cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* noteXmlNode, unsigned tick, unsigned duration, double rvalue, const cmChar_t* tvalue, unsigned flags )
  545. {
  546. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  547. unsigned voiceId = 0; // non-note's are always assigned to voiceId=0;
  548. note->tick = tick;
  549. note->flags = flags;
  550. note->rvalue = rvalue;
  551. note->tvalue = tvalue;
  552. note->duration = duration;
  553. note->tied_dur = duration;
  554. note->meas = meas;
  555. note->xmlNode = noteXmlNode;
  556. return _cmXScorePushNote(p, meas, voiceId, note );
  557. }
  558. cmXsSpan_t* _cmXScoreFindOpenOctaveShift( cmXScore_t* p, unsigned staff, unsigned number )
  559. {
  560. cmXsSpan_t* s = p->spanL;
  561. for(; s!=NULL; s=s->link)
  562. if( s->tick1 == -1 && s->staff == staff && s->number == number )
  563. return s;
  564. return NULL;
  565. }
  566. cmXsRC_t _cmXScorePushOctaveShift(cmXScore_t* p, cmXsMeas_t* meas, unsigned staff, unsigned span_number, const cmChar_t* type_str, unsigned tick)
  567. {
  568. assert( meas != NULL);
  569. cmXsSpan_t* s;
  570. if( cmTextCmp(type_str,"stop") == 0 )
  571. {
  572. if((s = _cmXScoreFindOpenOctaveShift(p,staff,span_number)) == NULL )
  573. return cmErrWarnMsg(&p->err,kUnterminatedOctaveShiftXsrRC,"An illegal octave shift was encounted in meas %i.\n",meas->number);
  574. s->tick1 = tick;
  575. }
  576. else
  577. {
  578. s = cmLhAllocZ(p->lhH,cmXsSpan_t,1);
  579. s->staff = staff;
  580. s->meas = meas;
  581. s->number = span_number;
  582. s->tick0 = tick;
  583. s->tick1 = -1;
  584. s->pitch_offset = cmTextCmp(type_str,"up")==0 ? -12 : 12;
  585. s->link = p->spanL;
  586. p->spanL = s;
  587. }
  588. return kOkXsRC;
  589. }
  590. cmXsRC_t _cmXScoreParseDirection(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* dnp, unsigned tick)
  591. {
  592. cmXsRC_t rc = kOkXsRC;
  593. const cmXmlNode_t* np = NULL;
  594. const cmXmlAttr_t* a = NULL;
  595. unsigned flags = 0;
  596. int offset = 0;
  597. double rvalue = 0;
  598. const cmChar_t* tvalue = NULL;
  599. unsigned duration = 0;
  600. bool pushFl = true;
  601. unsigned staff = 0;
  602. cmXmlNodeInt( dnp, &offset, "offset", NULL );
  603. cmXmlNodeUInt(dnp, &staff, "staff", NULL );
  604. // if this is a metronome direction
  605. if((np = cmXmlSearch( dnp, "metronome", NULL, 0)) != NULL )
  606. {
  607. if( cmXmlNodeUInt(np,&duration,"per-minute",NULL) != kOkXmlRC )
  608. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'per-minute' metronome value is missing on line %i.",np->line);
  609. if((rc = _cmXScoreParseNoteRValue(p,np,"beat-unit",&rvalue)) != kOkXsRC )
  610. return rc;
  611. flags = kMetronomeXsFl;
  612. }
  613. else
  614. // if this is a pedal direction
  615. if((np = cmXmlSearch( dnp, "pedal",NULL,0)) != NULL )
  616. {
  617. if((a = cmXmlFindAttrib(np,"type")) == NULL )
  618. return _cmXScoreMissingAttribute(p, np, "type" );
  619. if( cmTextCmp(a->value,"start") == 0 )
  620. flags = kDampDnXsFl;
  621. else
  622. if( cmTextCmp(a->value,"change") == 0 )
  623. flags = kDampUpDnXsFl;
  624. else
  625. if( cmTextCmp(a->value,"stop") == 0 )
  626. flags = kDampUpXsFl;
  627. else
  628. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unrecognized pedal type:'%s'.",cmStringNullGuard(a->value));
  629. }
  630. else
  631. // if this is a 'words' direction
  632. if((np = cmXmlSearch( dnp, "words", NULL, 0)) != NULL )
  633. {
  634. if((a = cmXmlFindAttrib(np,"enclosure")) != NULL && cmTextCmp(a->value,"rectangle")==0 )
  635. {
  636. if( cmTextIsEmpty( tvalue = np->dataStr ) )
  637. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Section number is blank or missing on line %i.",np->line);
  638. flags = kSectionXsFl;
  639. }
  640. }
  641. else
  642. // if this is an 'octave-shift' direction
  643. if((np = cmXmlSearch( dnp, "octave-shift", NULL, 0)) != NULL )
  644. {
  645. unsigned span_number = -1;
  646. if( cmXmlAttrUInt(np,"number",&span_number) != kOkXmlRC )
  647. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'number' attribute.");
  648. if((a = cmXmlFindAttrib(np,"type")) == NULL)
  649. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'type' attribute.");
  650. rc = _cmXScorePushOctaveShift(p,meas,staff,span_number,a->value,tick+offset);
  651. pushFl = false;
  652. }
  653. else
  654. {
  655. pushFl = false;
  656. }
  657. if( pushFl )
  658. rc = _cmXScorePushNonNote(p,meas,dnp,tick+offset,duration,rvalue,tvalue,flags);
  659. return rc;
  660. }
  661. // On input tickRef is set to the absolute tick of the bar line and on output it is set
  662. // to the absolute tick of the next bar line.
  663. cmXsRC_t _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp, unsigned* tickRef)
  664. {
  665. cmXsRC_t rc = kOkXsRC;
  666. const cmXmlNode_t* np = NULL;
  667. unsigned tick = *tickRef;
  668. unsigned tick0= 0;
  669. cmXsMeas_t* m = NULL;
  670. // allocate the 'measure' record
  671. cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1);
  672. // get measure number
  673. if( cmXmlAttrUInt(mnp,"number", &meas->number) != kOkXmlRC )
  674. return _cmXScoreMissingAttribute(p,mnp,"number");
  675. if( pp->measL == NULL )
  676. pp->measL = meas;
  677. else
  678. {
  679. m = pp->measL;
  680. while( m->link != NULL )
  681. m = m->link;
  682. m->link = meas;
  683. meas->divisions = m->divisions;
  684. meas->beats = m->beats;
  685. meas->beat_type = m->beat_type;
  686. }
  687. // get measure attributes node
  688. if((np = cmXmlSearch(mnp,"attributes",NULL,0)) != NULL)
  689. {
  690. cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL);
  691. cmXmlNodeUInt(np,&meas->beats, "time","beats",NULL);
  692. cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL);
  693. }
  694. // store the bar line
  695. if((rc = _cmXScorePushNonNote(p,meas,mnp,tick,0,0,NULL,kBarXsFl)) != kOkXsRC )
  696. return rc;
  697. np = mnp->children;
  698. // for each child of the 'meas' XML node
  699. for(; rc==kOkXsRC && np!=NULL; np=np->sibling)
  700. {
  701. // if this is a 'note' node
  702. if( cmTextCmp(np->label,"note") == 0 )
  703. {
  704. rc = _cmXScoreParseNote(p,meas,np,&tick0,&tick);
  705. }
  706. else
  707. // if this is a 'backup' node
  708. if( cmTextCmp(np->label,"backup") == 0 )
  709. {
  710. unsigned backup;
  711. cmXmlNodeUInt(np,&backup,"duration",NULL);
  712. if( backup > tick )
  713. tick = 0;
  714. else
  715. tick -= backup;
  716. tick0 = tick;
  717. }
  718. else
  719. // if this is a 'direction' node
  720. if( cmTextCmp(np->label,"direction") == 0 )
  721. {
  722. rc = _cmXScoreParseDirection(p,meas,np,tick);
  723. }
  724. }
  725. *tickRef = tick;
  726. return rc;
  727. }
  728. cmXsRC_t _cmXScoreParsePart( cmXScore_t* p, cmXsPart_t* pp )
  729. {
  730. cmXsRC_t rc = kOkXsRC;
  731. const cmXmlNode_t* xnp;
  732. cmXmlAttr_t partAttr = { "id", pp->idStr };
  733. unsigned barTick = 0;
  734. // find the 'part'
  735. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part", &partAttr, 1)) == NULL )
  736. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The part '%s' was not found.",pp->idStr);
  737. // for each child of this part - find each measure
  738. const cmXmlNode_t* cnp = xnp->children;
  739. for(; cnp!=NULL; cnp=cnp->sibling)
  740. if( cmTextCmp(cnp->label,"measure") == 0 )
  741. if((rc = _cmXScoreParseMeasure(p,pp,cnp,&barTick)) != kOkXsRC )
  742. return rc;
  743. return rc;
  744. }
  745. // Insert note 'np' into the sorted note list based at 's0'.
  746. // Return a pointer to the base of the list after the insertion.
  747. cmXsNote_t* _cmXScoreInsertSortedNote( cmXsNote_t* s0, cmXsNote_t* np )
  748. {
  749. assert( np != NULL );
  750. // np->slink is not NULL if the list is being resorted
  751. np->slink = NULL;
  752. // this list is empty so np is the first element on the list
  753. if( s0 == NULL )
  754. return np;
  755. // np is before the first element on the list
  756. if( np->tick < s0->tick )
  757. {
  758. np->slink = s0;
  759. return np;
  760. }
  761. cmXsNote_t* s1 = s0;
  762. cmXsNote_t* s2 = s0->slink;
  763. while( s2 != NULL )
  764. {
  765. if( s2->tick > np->tick )
  766. {
  767. s1->slink = np;
  768. np->slink = s2;
  769. return s0;
  770. }
  771. s1 = s2;
  772. s2 = s2->slink;
  773. }
  774. s1->slink = np;
  775. return s0;
  776. }
  777. // Set the cmXsNode_t.secs and dsecs.
  778. void _cmXScoreSetAbsoluteTime( cmXScore_t* p )
  779. {
  780. double tpqn = 0; // ticks per quarter note
  781. double tps = 0; // ticks per second
  782. unsigned metro_tick = 0;
  783. double metro_sec = 0;
  784. double sec0 = 0;
  785. cmXsPart_t* pp = p->partL;
  786. for(; pp!=NULL; pp=pp->link)
  787. {
  788. cmXsMeas_t* mp = pp->measL;
  789. for(; mp!=NULL; mp=mp->link)
  790. {
  791. if( mp->divisions != 0 )
  792. tpqn = mp->divisions;
  793. cmXsNote_t* np = mp->noteL;
  794. for(; np!=NULL; np=np->slink)
  795. {
  796. // Seconds are calculated as:
  797. // dticks = np->tick - metro_tick; // where metro_tick is the absolute tick of the last metro event
  798. // secs = (dticks/tps) + metro_secs; // where metro_secs is the absoute time of the last metro event
  799. unsigned dticks = np->tick - metro_tick;
  800. double secs = tps==0 ? 0 : (dticks/tps) + metro_sec;
  801. double dsecs = secs - sec0;
  802. //
  803. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  804. {
  805. double bpm = np->duration; // beats per minute
  806. double bps = bpm / 60.0; // beats per second
  807. tps = bps * tpqn; // ticks per second
  808. metro_tick = np->tick; // store tick of the last metronome marker
  809. metro_sec = secs; // store time of the last metronome marker
  810. }
  811. if( cmIsFlag(np->flags,kBarXsFl|kDampDnXsFl|kDampUpXsFl|kDampUpDnXsFl|kSostDnXsFl|kSostUpXsFl|kOnsetXsFl|kSectionXsFl) )
  812. {
  813. np->secs = secs;
  814. np->dsecs = dsecs;
  815. sec0 = secs;
  816. }
  817. }
  818. }
  819. }
  820. }
  821. void _cmXScoreSort( cmXScore_t* p )
  822. {
  823. // for each part
  824. cmXsPart_t* pp = p->partL;
  825. for(; pp!=NULL; pp=pp->link)
  826. {
  827. // for each measure in this part
  828. cmXsMeas_t* mp = pp->measL;
  829. for(; mp!=NULL; mp=mp->link)
  830. {
  831. // explicitely set noteL to NULL to in case we are re-sorting
  832. mp->noteL = NULL;
  833. // for each voice in this measure
  834. cmXsVoice_t* vp = mp->voiceL;
  835. for(; vp!=NULL; vp=vp->link)
  836. {
  837. // for each note in this measure
  838. cmXsNote_t* np = vp->noteL;
  839. for(; np!=NULL; np=np->mlink)
  840. mp->noteL = _cmXScoreInsertSortedNote(mp->noteL,np);
  841. }
  842. }
  843. }
  844. // The order of events may have changed update the absolute time.
  845. _cmXScoreSetAbsoluteTime( p );
  846. }
  847. // All notes in a[aN] are on the same tick
  848. unsigned _cmXsSpreadGraceNotes( cmXsNote_t** a, unsigned aN )
  849. {
  850. unsigned i;
  851. bool barFl = false;
  852. // set barFl to true if a bar marker is included in the notes
  853. for(i=0; i<aN; ++i)
  854. if( cmIsFlag(a[i]->flags,kBarXsFl) )
  855. barFl = true;
  856. // spread any grace notes by one tick
  857. unsigned nextGraceTick = UINT_MAX;
  858. for(i=0; i<aN; ++i)
  859. if( cmIsFlag(a[i]->flags,kGraceXsFl) )
  860. {
  861. if( nextGraceTick == UINT_MAX )
  862. nextGraceTick = a[i]->tick + 1;
  863. else
  864. {
  865. a[i]->tick = nextGraceTick;
  866. nextGraceTick += 1;
  867. }
  868. }
  869. // if this tick group includes the bar ...
  870. if( barFl && nextGraceTick != UINT_MAX )
  871. {
  872. // ... then move all non-grace note events (except the bar marker) after
  873. // the grace notes
  874. for(i=0; i<aN; ++i)
  875. if( cmIsNotFlag(a[i]->flags,kGraceXsFl) && cmIsNotFlag(a[i]->flags,kBarXsFl) )
  876. a[i]->tick = nextGraceTick;
  877. }
  878. return nextGraceTick==UINT_MAX ? 0 : nextGraceTick;
  879. }
  880. void _cmXScoreSpreadGraceNotes( cmXScore_t* p )
  881. {
  882. cmXsPart_t* pp = p->partL;
  883. for(; pp!=NULL; pp=pp->link)
  884. {
  885. // tick1 is the location of the minimum current tick
  886. // (or 0 if it should be ignored)
  887. unsigned tick1 = 0;
  888. cmXsMeas_t* mp = pp->measL;
  889. for(; mp!=NULL; mp=mp->link)
  890. {
  891. cmXsNote_t* np = mp->noteL;
  892. unsigned aN = 128;
  893. cmXsNote_t* a[ aN ];
  894. unsigned ai = 0;
  895. // The first event in a measure may have been forced ahead
  896. // by spreading at the end of the previous measure
  897. if( tick1 > np->tick )
  898. np->tick = tick1;
  899. else
  900. tick1 = 0;
  901. // tick0 is the tick of the current tick group we are examining
  902. // A tick group is a group of events that share the same tick.
  903. unsigned tick0 = np->tick;
  904. // for each note
  905. for(; np!=NULL; np=np->slink)
  906. {
  907. // if this event is the first of a new tick group (then a[] holds a group completed on the previous note)
  908. if( np->tick != tick0 )
  909. {
  910. // if there is more than one event in the completed tick group ...
  911. if( ai > 1 )
  912. tick1 = _cmXsSpreadGraceNotes(a,ai); // ... then process the group
  913. ai = 0; // empty the tick group array
  914. tick0 = np->tick; // update the current group's common tick
  915. }
  916. // if the min. current tick is ahead of this event then move the event ahead
  917. if( tick1 > np->tick )
  918. np->tick = tick1;
  919. else
  920. tick1 = 0; // otherwise disable tick1
  921. // add this event to the tick group
  922. assert(ai<aN);
  923. a[ai++] = np;
  924. }
  925. // if there are events in the group array then process them
  926. if( ai > 1 )
  927. tick1 = _cmXsSpreadGraceNotes(a,ai);
  928. }
  929. }
  930. }
  931. bool _cmXScoreFindTiedNote( cmXScore_t* p, cmXsMeas_t* mp, cmXsNote_t* n0p, bool rptFl )
  932. {
  933. cmXsNote_t* nbp = n0p;
  934. cmXsNote_t* nnp = n0p->slink; // begin w/ note following np
  935. unsigned measNumb = mp->number;
  936. cmChar_t acc = n0p->alter==-1?'b' : (n0p->alter==1?'#':' ');
  937. if( rptFl )
  938. printf("%i %i %s ",n0p->meas->number,n0p->tick,cmMidiToSciPitch(n0p->pitch,NULL,0));
  939. while(1)
  940. {
  941. // if we are at the end of a measure advance to the next measure
  942. if( nnp == NULL )
  943. {
  944. mp = mp->link;
  945. nnp = mp->noteL;
  946. // if a measure was completed and no end note was found ... then the tie is unterminated
  947. // (a tie must be continued in every measure which it passes through)
  948. if( mp->number > measNumb + 1 )
  949. break;
  950. }
  951. // for each note starting at nnp
  952. for(; nnp!=NULL; nnp=nnp->slink)
  953. {
  954. // if this note is tied to the originating note (np)
  955. if( nnp->voice->id == n0p->voice->id && nnp->step == n0p->step && nnp->octave == n0p->octave )
  956. {
  957. nnp->flags |= kTieProcXsFl;
  958. nnp->flags = cmClrFlag(nnp->flags,kOnsetXsFl);
  959. n0p->tied = nnp;
  960. nbp->tied_dur += nnp->duration;
  961. nnp->tied_dur = 0;
  962. if( rptFl )
  963. printf("---> %i %i %s ",nnp->meas->number,nnp->tick,cmMidiToSciPitch(nnp->pitch,NULL,0));
  964. // if this note is not tied to a subsequent note
  965. if( cmIsNotFlag(nnp->flags,kTieBegXsFl) )
  966. return true;
  967. n0p = nnp;
  968. // record the measure number of the last note with a tie-start
  969. measNumb = mp->number;
  970. }
  971. }
  972. }
  973. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i (tick:%i) was not terminated.",n0p->step,acc,n0p->octave,measNumb,n0p->tick);
  974. return false;
  975. }
  976. void _cmXScoreResolveTiesAndLoc( cmXScore_t* p )
  977. {
  978. unsigned n = 0; // count of notes which begin a tie
  979. unsigned m = 0; // count of tied notes that are correctly terminated.
  980. bool rptFl = false;
  981. cmXsPart_t* pp = p->partL;
  982. // for each part
  983. for(; pp!=NULL; pp=pp->link)
  984. {
  985. unsigned locIdx = 1;
  986. cmXsMeas_t* mp = pp->measL;
  987. // for each measure
  988. for(; mp!=NULL; mp=mp->link)
  989. {
  990. cmXsNote_t* n0 = NULL;
  991. cmXsNote_t* np = mp->noteL;
  992. // for each note in this measure
  993. for(; np!=NULL; np=np->slink)
  994. {
  995. // if this note begins a tie and has not yet been processed
  996. // (A note that continues a tie and therefore has a kTieBegXsFl set
  997. // may have already been processed by an earlier tied note.)
  998. if( cmIsFlag(np->flags,kTieBegXsFl) && cmIsNotFlag(np->flags,kTieProcXsFl))
  999. {
  1000. if( _cmXScoreFindTiedNote(p,mp,np,rptFl) )
  1001. m += 1;
  1002. if( rptFl )
  1003. printf("\n");
  1004. n += 1;
  1005. }
  1006. // Validate the tie state of the current note.
  1007. if( cmIsFlag(np->flags,kTieEndXsFl) && cmIsFlag(np->flags,kOnsetXsFl) )
  1008. {
  1009. cmChar_t acc = np->alter==-1?'b' : (np->alter==1?'#':' ');
  1010. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i marked as a tied note but is also marked to sound.",np->step,acc,np->octave,mp->number);
  1011. }
  1012. //
  1013. // Set the score location of notes marked for onset and bar lines.
  1014. //
  1015. if( cmIsFlag(np->flags,kOnsetXsFl|kBarXsFl) )
  1016. {
  1017. // if this note does not share the same location as the previous 'located' note then increment the 'loc' index
  1018. if( cmIsFlag(np->flags,kBarXsFl) || (n0!=NULL && n0->tick!=np->tick))
  1019. locIdx += 1;
  1020. np->locIdx = locIdx;
  1021. n0 = np;
  1022. }
  1023. }
  1024. }
  1025. }
  1026. printf("Tied notes found:%i Not found:%i\n",m,n-m);
  1027. }
  1028. cmXsRC_t _cmXScoreResolveOctaveShift( cmXScore_t* p )
  1029. {
  1030. const cmXsSpan_t* s;
  1031. for(s=p->spanL; s!=NULL; s=s->link)
  1032. {
  1033. if( s->tick1 == -1)
  1034. {
  1035. cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"An unterminated octave shift span was encountered in measure %i staff=%i.",s->meas->number,s->staff);
  1036. }
  1037. else
  1038. {
  1039. cmXsMeas_t* m = p->partL->measL;
  1040. for(; m!=NULL; m=m->link)
  1041. if( m->number == s->meas->number )
  1042. break;
  1043. assert( m != NULL );
  1044. cmXsNote_t* note = m->noteL;
  1045. for(; note!=NULL; note=note->slink)
  1046. if( note->staff==s->staff && s->tick0 <= note->tick && note->tick < s->tick1 )
  1047. note->pitch += s->pitch_offset;
  1048. }
  1049. }
  1050. return kOkXsRC;
  1051. }
  1052. cmXsNote_t* _cmXScoreFindOverlappingNote( cmXScore_t* p, const cmXsNote_t* knp )
  1053. {
  1054. cmXsPart_t* pp = p->partL;
  1055. // for each part
  1056. for(; pp!=NULL; pp=pp->link)
  1057. {
  1058. cmXsMeas_t* mp = pp->measL;
  1059. // for each measure
  1060. for(; mp!=NULL; mp=mp->link)
  1061. {
  1062. cmXsNote_t* np = mp->noteL;
  1063. // for each note in this measure
  1064. for(; np!=NULL; np=np->slink)
  1065. if( np->uid != knp->uid
  1066. && cmIsFlag(np->flags,kOnsetXsFl)
  1067. && knp->pitch == np->pitch
  1068. && knp->tick >= np->tick
  1069. && knp->tick < (np->tick + np->tied_dur) )
  1070. {
  1071. return np;
  1072. }
  1073. }
  1074. }
  1075. return NULL;
  1076. }
  1077. void _cmXScoreProcessOverlappingNotes( cmXScore_t* p )
  1078. {
  1079. cmXsPart_t* pp = p->partL;
  1080. // for each part
  1081. for(; pp!=NULL; pp=pp->link)
  1082. {
  1083. cmXsMeas_t* mp = pp->measL;
  1084. // for each measure
  1085. for(; mp!=NULL; mp=mp->link)
  1086. {
  1087. cmXsNote_t* np = mp->noteL;
  1088. cmXsNote_t* fnp;
  1089. // for each note in this measure
  1090. for(; np!=NULL; np=np->slink)
  1091. if( cmIsFlag(np->flags,kOnsetXsFl) && (fnp = _cmXScoreFindOverlappingNote(p,np)) != NULL)
  1092. {
  1093. // is np entirely contained inside fnp
  1094. bool embeddedFl = fnp->tick + fnp->tied_dur > np->tick + np->tied_dur;
  1095. //printf("bar=%3i %4s voice:%2i %2i : %7i %7i : %7i %7i : %7i : %c \n",np->meas->number,cmMidiToSciPitch(np->pitch,NULL,0),np->voice->id,fnp->voice->id,fnp->tick,fnp->tick+fnp->duration,np->tick,np->tick+np->duration, (fnp->tick+fnp->duration) - np->tick, embeddedFl ? 'E' : 'O');
  1096. // turn off embedded notes
  1097. if( embeddedFl )
  1098. {
  1099. if( np->voice->id == fnp->voice->id )
  1100. cmErrWarnMsg(&p->err,kOverlapWarnXsRC,"A time embedded note (bar=%i %s) was removed even though it overlapped with a note in the same voice.",np->meas->number,cmMidiToSciPitch(np->pitch,NULL,0));
  1101. np->flags = cmClrFlag(np->flags,kOnsetXsFl);
  1102. }
  1103. else
  1104. {
  1105. int d = (fnp->tick+fnp->tied_dur) - np->tick;
  1106. // shorten the first note
  1107. if( d > 0 && d < fnp->tied_dur )
  1108. fnp->tied_dur -= d;
  1109. // move the second note just past it
  1110. np->tick = fnp->tick + fnp->tied_dur + 1;
  1111. }
  1112. }
  1113. }
  1114. }
  1115. }
  1116. // The identical pitch may be notated to play simultaneously on different voices.
  1117. // As performed on the piano this will equate to a single sounding note.
  1118. // This function clears the onset flag on all except one of the duplicated notes.
  1119. void _cmXScoreRemoveDuplicateNotes( cmXScore_t* p )
  1120. {
  1121. cmXsPart_t* pp = p->partL;
  1122. // for each part
  1123. for(; pp!=NULL; pp=pp->link)
  1124. {
  1125. cmXsMeas_t* mp = pp->measL;
  1126. // for each measure
  1127. for(; mp!=NULL; mp=mp->link)
  1128. {
  1129. cmXsNote_t* np = mp->noteL;
  1130. // for each note in this measure
  1131. for(; np!=NULL; np=np->slink)
  1132. if( cmIsFlag(np->flags,kOnsetXsFl) )
  1133. {
  1134. cmXsNote_t* n0p = mp->noteL;
  1135. for(; n0p!=NULL; n0p=n0p->slink)
  1136. if( n0p!=np && cmIsFlag(n0p->flags,kOnsetXsFl) && np->locIdx==n0p->locIdx && np->pitch==n0p->pitch )
  1137. n0p->flags = cmClrFlag(n0p->flags,kOnsetXsFl);
  1138. }
  1139. }
  1140. }
  1141. }
  1142. void _cmXScoreSetMeasGroups( cmXScore_t* p, unsigned flag )
  1143. {
  1144. unsigned sectionId = 0;
  1145. cmXsNote_t* n0 = NULL;
  1146. cmXsPart_t* pp = p->partL;
  1147. // for each part
  1148. for(; pp!=NULL; pp=pp->link)
  1149. {
  1150. cmXsMeas_t* mp = pp->measL;
  1151. // for each measure
  1152. for(; mp!=NULL; mp=mp->link)
  1153. {
  1154. cmXsNote_t* np = mp->noteL;
  1155. // for each note in this measure
  1156. for(; np!=NULL; np=np->slink)
  1157. {
  1158. // if this note has a heel marker and we are looking for evenness events
  1159. if( cmIsFlag(flag,kEvenXsFl) && cmIsFlag(np->flags,kHeelXsFl) )
  1160. {
  1161. np->flags = cmSetFlag(np->flags,kBegGroupXsFl | kEndGroupXsFl );
  1162. np->evenGroupId = sectionId + 1;
  1163. }
  1164. // if this note is of the type we are looking for
  1165. if( cmIsFlag(np->flags,flag) )
  1166. {
  1167. if( n0 == NULL )
  1168. np->flags = cmSetFlag(np->flags,kBegGroupXsFl);
  1169. n0 = np;
  1170. }
  1171. // if this is a section marker
  1172. if( cmIsFlag(np->flags,kSectionXsFl) )
  1173. {
  1174. if( n0 != NULL )
  1175. {
  1176. np->flags = cmSetFlag(np->flags,kEndGroupXsFl);
  1177. switch( flag )
  1178. {
  1179. case kEvenXsFl: n0->evenGroupId = sectionId+1; break;
  1180. case kDynXsFl: n0->dynGroupId = sectionId+1; break;
  1181. case kTempoXsFl: n0->tempoGroupId= sectionId+1; break;
  1182. }
  1183. }
  1184. if( cmIsFlag(np->flags,kSectionXsFl) )
  1185. {
  1186. sectionId = 0;
  1187. if( np->tvalue == NULL )
  1188. cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"An apparent section label in measure '%i' is blank.",np->meas->number);
  1189. else
  1190. if( cmTextToUInt( np->tvalue, &sectionId, NULL ) != kOkTxRC )
  1191. cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"The section label '%s' is not an integer.",np->tvalue);
  1192. //sectionId = np->tvalue==NULL ? 0 : strtol(np->tvalue,NULL,10);
  1193. }
  1194. n0 = NULL;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. }
  1200. cmXsRC_t _cmXScoreWriteScorePlotFile( cmXScore_t* p, const cmChar_t* fn )
  1201. {
  1202. cmXsRC_t rc = kOkXsRC;
  1203. cmFileH_t fH = cmFileNullHandle;
  1204. double ticks_per_sec = 0;
  1205. double onset_secs = 0;
  1206. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  1207. return cmErrMsg(&p->err,kFileFailXsRC,"Unable to create the file '%s'.",cmStringNullGuard(fn));
  1208. cmXsPart_t* pp = p->partL;
  1209. for(; pp!=NULL; pp=pp->link)
  1210. {
  1211. cmXsMeas_t* mp = pp->measL;
  1212. for(; mp!=NULL; mp=mp->link)
  1213. {
  1214. cmFilePrintf(fH,"b %f %i %s B\n",onset_secs,mp->number,"bar");
  1215. cmXsNote_t* np = mp->noteL;
  1216. unsigned tick0 = 0;
  1217. for(; np!=NULL; np=np->slink)
  1218. {
  1219. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  1220. {
  1221. double bps = np->tied_dur / 60.0;
  1222. // t b t
  1223. // - = - -
  1224. // s s b
  1225. ticks_per_sec = bps * mp->divisions;
  1226. }
  1227. else
  1228. {
  1229. if( cmIsFlag(np->flags,kOnsetXsFl) )
  1230. {
  1231. onset_secs += (np->tick - tick0) / ticks_per_sec;
  1232. tick0 = np->tick;
  1233. cmFilePrintf(fH,"n %f %f %i %s %s\n",onset_secs,np->tied_dur/ticks_per_sec,np->uid,cmMidiToSciPitch(np->pitch,NULL,0),cmIsFlag(np->flags,kGraceXsFl)?"G":"N");
  1234. }
  1235. }
  1236. }
  1237. onset_secs += (mp->divisions * mp->beats - tick0) / ticks_per_sec;
  1238. }
  1239. }
  1240. cmFileClose(&fH);
  1241. return rc;
  1242. }
  1243. // Force the bar event to be the first event in the measure.
  1244. void _cmXScoreFixBarLines( cmXScore_t* p )
  1245. {
  1246. cmXsPart_t* pp = p->partL;
  1247. for(; pp!=NULL; pp=pp->link)
  1248. {
  1249. cmXsMeas_t* mp = pp->measL;
  1250. for(; mp!=NULL; mp=mp->link)
  1251. {
  1252. cmXsNote_t* np = mp->noteL;
  1253. cmXsNote_t* ep = NULL;
  1254. for(; np!=NULL; np=np->slink )
  1255. {
  1256. if( cmIsFlag(np->flags,kBarXsFl) )
  1257. {
  1258. if( ep != NULL )
  1259. np->tick = ep->tick;
  1260. break;
  1261. }
  1262. if( ep == NULL )
  1263. ep = np;
  1264. }
  1265. }
  1266. }
  1267. }
  1268. // Assign pedal down durations to pedal down events.
  1269. cmXsRC_t _cmXScoreProcessPedals( cmXScore_t* p )
  1270. {
  1271. cmXsRC_t rc = kOkXsRC;
  1272. cmXsPart_t* pp = p->partL;
  1273. for(; pp!=NULL; pp=pp->link)
  1274. {
  1275. cmXsNote_t* dnp = NULL; // pointer to last damper down event
  1276. cmXsNote_t* snp = NULL; // pointer to last sostenuto down event
  1277. cmXsMeas_t* mp = pp->measL;
  1278. for(; mp!=NULL; mp=mp->link)
  1279. {
  1280. cmXsNote_t* np = mp->noteL;
  1281. for(; np!=NULL; np=np->slink )
  1282. {
  1283. unsigned x = np->flags & (kDampDnXsFl|kDampUpXsFl|kDampUpDnXsFl|kSostUpXsFl|kSostDnXsFl);
  1284. switch( x )
  1285. {
  1286. case 0:
  1287. break;
  1288. case kDampDnXsFl:
  1289. if( dnp != NULL )
  1290. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Damper down not preceded by damper up in measure:%i.",mp->number);
  1291. else
  1292. dnp = np;
  1293. break;
  1294. case kDampUpXsFl:
  1295. if( dnp == NULL )
  1296. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Damper up not preceded by damper down in measure:%i.",mp->number);
  1297. else
  1298. {
  1299. dnp->duration = np->tick - dnp->tick;
  1300. dnp = NULL;
  1301. }
  1302. break;
  1303. case kDampUpDnXsFl:
  1304. if( dnp == NULL )
  1305. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Damper up/down not preceded by damper down in measure:%i.",mp->number);
  1306. else
  1307. {
  1308. dnp->duration = np->tick - dnp->tick;
  1309. dnp = np;
  1310. }
  1311. break;
  1312. case kSostDnXsFl:
  1313. if( snp != NULL )
  1314. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Sostenuto down not preceded by sostenuto up in measure:%i.",mp->number);
  1315. else
  1316. snp = np;
  1317. break;
  1318. case kSostUpXsFl:
  1319. if( snp == NULL )
  1320. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Sostenuto up not preceded by sostenuto down in measure:%i.",mp->number);
  1321. else
  1322. {
  1323. snp->duration = np->tick - snp->tick;
  1324. snp = NULL;
  1325. }
  1326. break;
  1327. default:
  1328. {
  1329. assert(0);
  1330. }
  1331. } // switch
  1332. } // for notes
  1333. } // for measures
  1334. if( dnp != NULL )
  1335. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Damper left down at the end of a part.");
  1336. if( snp != NULL )
  1337. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Sostenuto left down at the end of a part.");
  1338. }
  1339. _cmXScoreSort(p);
  1340. return rc;
  1341. }
  1342. void _cmXScoreInsertTime( cmXScore_t* p, cmXsMeas_t* mp, cmXsNote_t* np, unsigned expand_ticks )
  1343. {
  1344. for(; mp!=NULL; mp=mp->link)
  1345. {
  1346. if( np == NULL )
  1347. np = mp->noteL;
  1348. for(; np!=NULL; np=np->slink)
  1349. np->tick += expand_ticks;
  1350. }
  1351. }
  1352. void _cmXScoreGraceInsertTimeBase( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN, unsigned initTick )
  1353. {
  1354. cmXsNote_t* np = NULL;
  1355. unsigned expand_ticks = 0;
  1356. unsigned ticks = initTick;
  1357. unsigned t0 = 0;
  1358. unsigned i;
  1359. for(i=0; i<aN; ++i)
  1360. if( cmIsFlag(aV[i]->flags,kGraceXsFl) && aV[i]->graceGroupId == graceGroupId )
  1361. {
  1362. // if this grace note falls on the same tick as the previous grace note
  1363. if( np != NULL && aV[i]->tick == t0 )
  1364. aV[i]->tick = np->tick;
  1365. else
  1366. {
  1367. t0 = aV[i]->tick; // store the unmodified tick value of this note
  1368. aV[i]->tick = ticks; // set the new tick value
  1369. ticks += aV[i]->duration; // calc the next grace not location
  1370. expand_ticks += aV[i]->duration; // track how much we are expanding time by
  1371. }
  1372. np = aV[i];
  1373. }
  1374. np = np->slink;
  1375. if( np != NULL )
  1376. _cmXScoreInsertTime(p,np->meas,np,expand_ticks);
  1377. }
  1378. // (a) Insert the grace notes in between the first and last note in the group
  1379. // by inserting time between the first and last note.
  1380. // Note that in effect his means that the last note is pushed back
  1381. // in time by the total duration of the grace notes.
  1382. void _cmXScoreGraceInsertTime( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN )
  1383. {
  1384. _cmXScoreGraceInsertTimeBase( p, graceGroupId,aV,aN, aV[aN-1]->tick );
  1385. }
  1386. // (s) Insert the grace notes in between the first and last note in the group
  1387. // but do not insert any additional time betwee the first and last note.
  1388. // In effect time is removed from the first note and taken by the grace notes.
  1389. // The time position of the last note is therefore unchanged.
  1390. void _cmXScoreGraceOverlayTime( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN )
  1391. {
  1392. assert(aN >= 3 );
  1393. int i = (int)aN-2;
  1394. cmXsNote_t* np = aV[aN-1];
  1395. unsigned t0 = -1;
  1396. for(; i>0; --i)
  1397. if( cmIsFlag(aV[i]->flags,kGraceXsFl) && aV[i]->graceGroupId == graceGroupId )
  1398. {
  1399. if( aV[i]->tick == t0)
  1400. aV[i]->tick = np->tick;
  1401. else
  1402. {
  1403. t0 = aV[i]->tick;
  1404. aV[i]->tick = np->tick - aV[i]->duration;
  1405. }
  1406. np = aV[i];
  1407. }
  1408. }
  1409. // (A) Play the first grace at the time of the first note in the group (which is a non-grace note)
  1410. // and then expand time while inserting the other grace notes.
  1411. void _cmXScoreGraceInsertAfterFirst( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN )
  1412. {
  1413. _cmXScoreGraceInsertTimeBase( p, graceGroupId,aV,aN, aV[0]->tick );
  1414. }
  1415. // (n) Play the first grace not shortly (one grace note duration) after the first note
  1416. // in the group (which is a non-grace note) and then expand time while inserting the other
  1417. // grace notes.
  1418. void _cmXScoreGraceInsertSoonAfterFirst( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN )
  1419. {
  1420. _cmXScoreGraceInsertTimeBase( p, graceGroupId,aV,aN, aV[0]->tick + aV[1]->duration );
  1421. }
  1422. // Adjust the locations of grace notes. Note that this must be done
  1423. // after reordering so that we can be sure that the order in time of
  1424. // the notes in each group has been set prior to building the
  1425. // grace note groups - which must be in reverse time order.
  1426. cmXsRC_t _cmXScoreProcessGraceNotes( cmXScore_t* p, unsigned nextGraceGroupId )
  1427. {
  1428. cmXsRC_t rc = kOkXsRC;
  1429. unsigned graceGroupId = 1;
  1430. double graceDurSec = 1.0/15.0; // duration of all grace notes in seconds
  1431. for(; graceGroupId<nextGraceGroupId; ++graceGroupId)
  1432. {
  1433. cmXsNote_t* gn0p = NULL; // first note in the grace group
  1434. cmXsNote_t* gn1p = NULL; // last note in the grace group
  1435. unsigned gN = 0;
  1436. cmXsPart_t* pp = p->partL;
  1437. double ticksPerSec = 0;
  1438. // Build a note chain, using cmXsNote_t.grace, between gn0p and
  1439. // gn1p containing all the grace notes with
  1440. // cmXsNote_t.graceGroupId == graceGroupId.
  1441. for(; pp!=NULL; pp=pp->link)
  1442. {
  1443. cmXsMeas_t* mp = pp->measL;
  1444. for(; mp!=NULL; mp=mp->link)
  1445. {
  1446. cmXsNote_t* np = mp->noteL;
  1447. for(; np!=NULL; np=np->slink )
  1448. {
  1449. // notice change of tempo
  1450. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  1451. {
  1452. // ticks/sec = ticks/qn * qn/sec
  1453. ticksPerSec = mp->divisions * np->duration / 60.0;
  1454. }
  1455. // if this note is part of the grace note group we are searching for
  1456. if( np->graceGroupId == graceGroupId )
  1457. {
  1458. // track the first note in the grace note list
  1459. if( gn0p == NULL )
  1460. gn0p = np;
  1461. // add the note to the end of the grace note list
  1462. if( gn1p != NULL )
  1463. gn1p->grace = np;
  1464. // track the last note in the grace note list
  1465. gn1p = np;
  1466. // set each grace note to have 1/20 of a second duration
  1467. if( cmIsFlag(np->flags,kGraceXsFl) )
  1468. np->duration = np->tied_dur = floor(ticksPerSec * graceDurSec);
  1469. gN += 1;
  1470. }
  1471. } // for each note in this meassure
  1472. } // for each measure
  1473. } // for each part
  1474. // no records were found for this grace id - this grace group id was not used
  1475. if( gn0p == NULL )
  1476. continue;
  1477. // grace note groups must have at least 3 members
  1478. if( gN < 3 )
  1479. {
  1480. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"The grace note group (groupid=%i) ending in meas %i has fewer than 3 (%i) members.", gn1p->graceGroupId, gn1p->meas->number, gN );
  1481. break;
  1482. }
  1483. // gn0p is now set to the first note in th group
  1484. // gn1p is now set to the last note in the group
  1485. // verify that the first note is marked with kBegGraceXsFl
  1486. if( cmIsNotFlag(gn0p->flags,kBegGraceXsFl) )
  1487. {
  1488. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"The first note in a grace note group in meas %i tick %i groupid:%i is not marked with a 'b'.", gn0p->meas->number, gn0p->tick, graceGroupId );
  1489. break;
  1490. }
  1491. // verify that the last note is marked with kEndGraceXsFl
  1492. if( cmIsNotFlag(gn1p->flags,kEndGraceXsFl) )
  1493. {
  1494. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"The last note in a grace note group in meas %i is not marked with a valid operator character.", gn1p->meas->number );
  1495. break;
  1496. }
  1497. // Count the total number of events between gn0p and gn1p
  1498. cmXsNote_t* n0p = NULL;
  1499. cmXsNote_t* n1p = gn0p;
  1500. cmXsMeas_t* mp = gn0p->meas;
  1501. unsigned aN = 0;
  1502. for(; n0p != gn1p; n1p=n1p->slink )
  1503. {
  1504. // if we are crossing a measure boundary
  1505. if( n1p == NULL )
  1506. {
  1507. mp = mp->link;
  1508. assert(mp!=NULL);
  1509. n1p = mp->noteL;
  1510. }
  1511. if(0)
  1512. {
  1513. bool fl = n0p != NULL && n0p->tick < n1p->tick;
  1514. unsigned type = n1p->flags & (kBegGraceXsFl|kEndGraceXsFl|kAddGraceXsFl|kSubGraceXsFl|kAFirstGraceXsFl|kNFirstGraceXsFl);
  1515. printf("%3i 0x%08x %i %3i %5i %i\n",n1p->graceGroupId,type,n1p->meas->number,n1p->tick,n1p->duration,fl);
  1516. }
  1517. ++aN;
  1518. n0p = n1p;
  1519. }
  1520. // create a vector of pointers to all events between gn0p and gn1p
  1521. cmXsNote_t* aV[ aN ];
  1522. unsigned i;
  1523. n1p = gn0p;
  1524. n0p = NULL;
  1525. mp = gn0p->meas;
  1526. for(i=0; n0p != gn1p; n1p=n1p->slink )
  1527. {
  1528. // if we are crossing a measure boundary
  1529. if( n1p == NULL )
  1530. {
  1531. mp = mp->link;
  1532. assert(mp!=NULL);
  1533. n1p = mp->noteL;
  1534. }
  1535. assert(i<aN);
  1536. aV[i++] = n1p;
  1537. n0p = n1p;
  1538. }
  1539. switch( gn1p->flags & (kAddGraceXsFl | kSubGraceXsFl | kAFirstGraceXsFl | kNFirstGraceXsFl ) )
  1540. {
  1541. case kAddGraceXsFl:
  1542. _cmXScoreGraceInsertTime(p, graceGroupId, aV, aN );
  1543. break;
  1544. case kSubGraceXsFl:
  1545. _cmXScoreGraceOverlayTime(p, graceGroupId, aV, aN );
  1546. break;
  1547. case kAFirstGraceXsFl:
  1548. _cmXScoreGraceInsertAfterFirst(p,graceGroupId,aV,aN);
  1549. break;
  1550. case kNFirstGraceXsFl:
  1551. _cmXScoreGraceInsertSoonAfterFirst(p,graceGroupId,aV,aN);
  1552. break;
  1553. default:
  1554. { assert(0); }
  1555. }
  1556. }
  1557. return rc;
  1558. }
  1559. cmXsNote_t* _cmXScoreDynamicForkEndNote( cmXScore_t* p, cmXsNote_t* bnp )
  1560. {
  1561. assert( cmIsFlag(bnp->flags,kDynBegForkXsFl) );
  1562. cmXsMeas_t* mp = bnp->meas;
  1563. cmXsNote_t* np = bnp->slink;
  1564. for(; mp!=NULL; mp=mp->link,np = mp==NULL?NULL : mp->noteL)
  1565. {
  1566. for(; np!=NULL; np=np->slink)
  1567. {
  1568. if( cmIsFlag(np->flags,kDynBegForkXsFl) )
  1569. {
  1570. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The a dynamic fork begin (meas:%i loc:%i) was found prior to a matched end.",np->meas->number,np->locIdx);
  1571. return NULL;
  1572. }
  1573. if( cmIsFlag(np->flags,kDynEndForkXsFl) /*&& np->voice->id == bnp->voice->id*/ )
  1574. return np;
  1575. }
  1576. }
  1577. return NULL;
  1578. }
  1579. cmXsRC_t _cmXScoreProcessDynamicFork( cmXScore_t* p, cmXsNote_t* bnp )
  1580. {
  1581. cmXsNote_t* enp;
  1582. if((enp = _cmXScoreDynamicForkEndNote(p,bnp)) == NULL)
  1583. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The dynamic fork beginning in measure %i at tick %i voice %i loc:%i was not terminated.", bnp->meas->number,bnp->tick,bnp->voice->id,bnp->locIdx);
  1584. //cmRptPrintf( p->err.rpt, "Dynamic Fork: meas:%i tick:%i loc:%i to meas:%i tick:%i loc:%i\n", bnp->meas->number, bnp->tick, bnp->locIdx, enp->meas->number, enp->tick, enp->locIdx );
  1585. double begDynFrac = bnp->dynamics;
  1586. double begVelFrac = bnp->vel;
  1587. double endDynFrac = enp->dynamics;
  1588. double endVelFrac = enp->vel;
  1589. cmXsMeas_t* mp = bnp->meas;
  1590. cmXsNote_t* np = bnp->slink;
  1591. while( mp != NULL )
  1592. {
  1593. for(; np!=NULL; np=np->slink)
  1594. {
  1595. //Conditioning this on np->dynamics==0 will may would cause 'silent' notes to be assigned
  1596. // a non-zero dynamics value - so we condition on np->vel instead.
  1597. if( cmIsFlag(np->flags,kOnsetXsFl) && np->vel == 0 )
  1598. {
  1599. double frac = ((double)np->tick - bnp->tick) / (enp->tick - bnp->tick);
  1600. np->dynamics = lround(begDynFrac + ((endDynFrac - begDynFrac) * frac));
  1601. np->vel = lround(begVelFrac + ((endVelFrac - begVelFrac) * frac));
  1602. }
  1603. if( np == enp )
  1604. break;
  1605. }
  1606. if( np == enp)
  1607. break;
  1608. if( (mp=mp->link) != NULL )
  1609. np=mp->noteL;
  1610. }
  1611. return kOkXsRC;
  1612. }
  1613. cmXsRC_t _cmXScoreProcessDynamicForks( cmXScore_t* p )
  1614. {
  1615. cmXsRC_t rc = kOkXsRC;
  1616. cmXsPart_t* pp = p->partL;
  1617. for(; pp!=NULL; pp=pp->link)
  1618. {
  1619. cmXsMeas_t* mp = pp->measL;
  1620. for(; mp!=NULL; mp=mp->link)
  1621. {
  1622. cmXsNote_t* np = mp->noteL;
  1623. for(; np!=NULL; np=np->slink )
  1624. {
  1625. if( cmIsFlag(np->flags,kDynBegForkXsFl) )
  1626. if((rc = _cmXScoreProcessDynamicFork(p,np)) != kOkXsRC )
  1627. return rc;
  1628. }
  1629. }
  1630. }
  1631. return rc;
  1632. }
  1633. //-------------------------------------------------------------------------------------------
  1634. typedef struct
  1635. {
  1636. unsigned idx; // Fields from the reordering input file which are
  1637. unsigned voice; // used to match the reorder record to
  1638. unsigned locIdx; // the associated a cmXsNode_t record.
  1639. unsigned tick; //
  1640. unsigned durtn; //
  1641. float rval; //
  1642. unsigned midi; //
  1643. cmXsNote_t* note; // The cmXsNote_t* associated with this cmXsReorder_t record
  1644. unsigned dynIdx; // cmInvalidIdx=ignore otherwise index into _cmXScoreDynMarkArray[]
  1645. unsigned newFlags; // 0=ignore | kSostUp/DnXsFl | kDampUp/DnXsFl | kTieEndXsFl
  1646. unsigned newTick; // 0=ignore >0 new tick value
  1647. unsigned graceFlags; // 0=ignore See kXXXGraceXsFl
  1648. unsigned graceGroupId; // 0=ignore >0=grace note group id
  1649. unsigned pitch; // 0=ignore >0 new pitch
  1650. } cmXsReorder_t;
  1651. typedef struct _cmXScoreDynMark_str
  1652. {
  1653. const cmChar_t* mark; //
  1654. unsigned id; // (1-17) maps to velocity
  1655. unsigned dyn; // pppp - fff (1-9) as used by cmScore
  1656. int adjust; // {-1,0,+1}
  1657. unsigned vel; // associated MIDI velocity
  1658. } _cmXScoreDynMark_t;
  1659. _cmXScoreDynMark_t _cmXScoreDynMarkArray[] =
  1660. {
  1661. {"s", 1, 0, 0, 1}, // silent note
  1662. {"pppp-", 2, 1, -1, 3},
  1663. {"pppp", 3, 1, 0, 10},
  1664. {"pppp+", 4, 1, 1, 22},
  1665. {"ppp-", 4, 2, -1, 22},
  1666. {"ppp", 5, 2, 0, 29},
  1667. {"ppp+", 6, 2, 1, 36},
  1668. {"pp-", 6, 3, -1, 36},
  1669. {"pp", 7, 3, 0, 43},
  1670. {"pp+", 8, 3, 1, 50},
  1671. {"p-", 8, 4, -1, 50},
  1672. {"p", 9, 4, 0, 57},
  1673. {"p+", 10, 4, 1, 64},
  1674. {"mp-", 10, 5, -1, 64},
  1675. {"mp", 11, 5, 0, 71},
  1676. {"mp+", 12, 5, 1, 78},
  1677. {"mf-", 12, 6, -1, 78},
  1678. {"mf", 13, 6, 0, 85},
  1679. {"mf+", 14, 6, 1, 92},
  1680. {"f-", 14, 7, -1, 92},
  1681. {"f", 15, 7, 0, 99},
  1682. {"f+", 16, 7, 1, 106},
  1683. {"ff", 17, 8, 0, 113},
  1684. {"ff+", 18, 8, 1, 120},
  1685. {"fff", 19, 9, 0, 127},
  1686. {NULL,0,0,0,0}
  1687. };
  1688. cmXsNote_t* _cmXsReorderFindNote( cmXScore_t* p, unsigned measNumb, const cmXsReorder_t* r, unsigned iii )
  1689. {
  1690. cmXsPart_t* pp = p->partL;
  1691. for(; pp!=NULL; pp=pp->link)
  1692. {
  1693. cmXsMeas_t* mp = pp->measL;
  1694. for(; mp!=NULL; mp=mp->link)
  1695. if( mp->number == measNumb)
  1696. {
  1697. cmXsNote_t* np = mp->noteL;
  1698. int index = 0;
  1699. for(; np!=NULL; np=np->slink,++index)
  1700. {
  1701. if( 0 /*mp->number==43*/ )
  1702. printf("voice: %i %i loc:%i %i tick:%i %i pitch:%i %i idx:%i %i\n",
  1703. np->voice->id, r->voice,
  1704. np->locIdx , r->locIdx ,
  1705. np->tick , r->tick ,
  1706. np->pitch , r->midi ,
  1707. index , r->idx );
  1708. if( np->voice->id == r->voice &&
  1709. np->locIdx == r->locIdx &&
  1710. np->tick == r->tick &&
  1711. //np->duration == r->durtn &&
  1712. np->rvalue == r->rval &&
  1713. np->pitch == r->midi &&
  1714. index == r->idx )
  1715. {
  1716. //printf("Found: %i \n", r->midi);
  1717. return np;
  1718. }
  1719. }
  1720. }
  1721. }
  1722. cmErrMsg(&p->err,kSyntaxErrorXsRC,"Reorder note not found meas:%i index:%i.",measNumb,iii);
  1723. return NULL;
  1724. }
  1725. void _cmXScoreInsertPedalEvent( cmXScore_t* p, const cmXsReorder_t* r, unsigned flags )
  1726. {
  1727. // Create a new score event record
  1728. cmXsNote_t* nn = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  1729. nn->uid = p->nextUid++;
  1730. nn->voice = r->note->voice;
  1731. nn->meas = r->note->meas;
  1732. nn->flags = flags;
  1733. // Pedal down events occur after the event they are attached to
  1734. if( cmIsFlag(flags,kSostDnXsFl | kDampDnXsFl ) )
  1735. {
  1736. nn->tick = r->note->tick + 1;
  1737. _cmXScoreInsertNoteAfter(r->note,nn);
  1738. }
  1739. else
  1740. {
  1741. // Pedal up events occur before the event they are attached to
  1742. if( cmIsFlag(flags,kSostUpXsFl | kDampUpXsFl ) )
  1743. {
  1744. nn->tick = r->note->tick==0 ? 0 : r->note->tick - 1;
  1745. _cmXScoreInsertNoteBefore(r->note,nn);
  1746. }
  1747. else
  1748. { assert(0); }
  1749. }
  1750. }
  1751. cmXsRC_t _cmXScoreReorderMeas( cmXScore_t* p, unsigned measNumb, cmXsReorder_t* rV, unsigned rN )
  1752. {
  1753. unsigned i;
  1754. if( rN == 0 )
  1755. return kOkXsRC;
  1756. // set the 'note' field on each cmXsReorder_t record
  1757. for(i=0; i<rN; ++i)
  1758. if((rV[i].note = _cmXsReorderFindNote(p,measNumb,rV+i,i)) == NULL )
  1759. return kSyntaxErrorXsRC;
  1760. // remove deleted notes
  1761. for(i=0; i<rN; ++i)
  1762. if( cmIsFlag(rV[i].newFlags,kDeleteXsFl) )
  1763. _cmXScoreRemoveNote( rV[i].note );
  1764. cmXsMeas_t* mp = rV[0].note->meas;
  1765. cmXsNote_t* n0p = NULL;
  1766. assert( mp->number == measNumb );
  1767. // Reassign the slink of the cmXsNote_t records in this measure
  1768. // according to their order in rV[].
  1769. for(i=0; i<rN; ++i)
  1770. {
  1771. if( cmIsFlag(rV[i].newFlags,kDeleteXsFl) )
  1772. continue;
  1773. if( n0p == NULL )
  1774. mp->noteL = rV[i].note;
  1775. else
  1776. n0p->slink = rV[i].note;
  1777. // if a new tick was specified
  1778. if( rV[i].newTick != 0 )
  1779. rV[i].note->tick = rV[i].newTick;
  1780. // if a dynamic or velocity mark was included
  1781. if( rV[i].dynIdx != cmInvalidIdx )
  1782. {
  1783. rV[i].note->dynamics = _cmXScoreDynMarkArray[ rV[i].dynIdx ].dyn;
  1784. rV[i].note->vel = _cmXScoreDynMarkArray[ rV[i].dynIdx ].vel;
  1785. }
  1786. // Set the dynamic fork begin/end flags for later _cmXScoreProcessDynamicForks()
  1787. if( cmIsFlag(rV[i].newFlags,kDynBegForkXsFl) )
  1788. rV[i].note->flags = cmSetFlag(rV[i].note->flags,kDynBegForkXsFl);
  1789. if( cmIsFlag(rV[i].newFlags,kDynEndForkXsFl) )
  1790. rV[i].note->flags = cmSetFlag(rV[i].note->flags,kDynEndForkXsFl);
  1791. // if the tie end flag was set
  1792. if( cmIsFlag(rV[i].newFlags,kTieEndXsFl) )
  1793. {
  1794. rV[i].note->flags |= kTieEndXsFl;
  1795. rV[i].note->flags = cmClrFlag( rV[i].note->flags, kOnsetXsFl );
  1796. rV[i].newFlags = cmClrFlag( rV[i].newFlags, kTieEndXsFl);
  1797. }
  1798. // if a new note value was specified
  1799. if( rV[i].pitch != 0 )
  1800. rV[i].note->pitch = rV[i].pitch;
  1801. rV[i].note->flags |= rV[i].graceFlags;
  1802. rV[i].note->graceGroupId = rV[i].graceGroupId;
  1803. n0p = rV[i].note;
  1804. n0p->slink = NULL;
  1805. }
  1806. // Insert new note records for pedal up/dn events.
  1807. for(i=0; i<rN; ++i)
  1808. {
  1809. if( rV[i].newFlags != 0 )
  1810. {
  1811. if( cmIsFlag(rV[i].newFlags,kDampDnXsFl ) )
  1812. _cmXScoreInsertPedalEvent(p,rV + i,kDampDnXsFl);
  1813. if( cmIsFlag(rV[i].newFlags,kSostDnXsFl ) )
  1814. _cmXScoreInsertPedalEvent(p,rV + i,kSostDnXsFl);
  1815. if( cmIsFlag(rV[i].newFlags,kDampUpXsFl ) )
  1816. _cmXScoreInsertPedalEvent(p,rV + i,kDampUpXsFl);
  1817. if( cmIsFlag(rV[i].newFlags,kSostUpXsFl ) )
  1818. _cmXScoreInsertPedalEvent(p,rV + i,kSostUpXsFl);
  1819. }
  1820. }
  1821. return kOkXsRC;
  1822. }
  1823. cmXsRC_t _cmXScoreReorderParseDyn(cmXScore_t* p, const cmChar_t* b, unsigned lineNumb, unsigned* dynIdxRef, unsigned* flagsRef, int measNumb )
  1824. {
  1825. cmXsRC_t rc = kOkXsRC;
  1826. const cmChar_t* s = NULL;
  1827. bool begForkFl = false;
  1828. bool endForkFl = false;
  1829. *dynIdxRef = cmInvalidIdx;
  1830. // locate the '!' which indicates the start of a dynamic marking
  1831. if( (s = strchr(b,'!')) == NULL )
  1832. return rc;
  1833. ++s; // increment past the '!'
  1834. if( *s == 0 )
  1835. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected end-of-line on dynamics parsing on line:%i.",lineNumb);
  1836. // some dynamic markings are surrounded by parenthesis (to indicate a dynamic level with greater uncertainty)
  1837. if( *s == '(' )
  1838. ++s; // skip the paren.
  1839. if( *s == '!')
  1840. {
  1841. //printf("E %3i %5i %s\n",measNumb,lineNumb,b);
  1842. endForkFl = true;
  1843. ++s;
  1844. }
  1845. if( *s == 0 )
  1846. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected end-of-line on dynamics parsing on line:%i.",lineNumb);
  1847. // check for beg/end fork dynamic
  1848. if( isupper(*s) )
  1849. {
  1850. if( !endForkFl)
  1851. {
  1852. begForkFl=true;
  1853. //printf("B %3i %5i %s\n",measNumb,lineNumb,b);
  1854. }
  1855. }
  1856. else
  1857. {
  1858. if( endForkFl )
  1859. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"A double exclaimation point (end-of-dynamic fork) must always be followed by an upper case M,P, or F. See line:%i",lineNumb);
  1860. }
  1861. unsigned i = 0;
  1862. unsigned j = 0;
  1863. unsigned n = 6;
  1864. bool doneFl = false;
  1865. cmChar_t mark[n+1];
  1866. memset(mark,0,n+1);
  1867. for(i=0; j<n && doneFl==false; ++i)
  1868. {
  1869. char c = tolower(s[i]);
  1870. switch(c)
  1871. {
  1872. case 's':
  1873. case 'm':
  1874. case 'p':
  1875. case 'f':
  1876. case '+':
  1877. case '-':
  1878. mark[j++] = c;
  1879. break;
  1880. case ')': // ending paren.
  1881. case 0: // end of string
  1882. case ' ': // end of mark
  1883. case '\n': // end of line
  1884. default: // anything else
  1885. doneFl = true;
  1886. break;
  1887. }
  1888. }
  1889. if( !doneFl )
  1890. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal dynamic mark (%s) syntax on line:%i.",mark,lineNumb);
  1891. // look up the dynamic mark in the reference table
  1892. for(j=0; _cmXScoreDynMarkArray[j].mark!=NULL; ++j)
  1893. if( strcmp(mark,_cmXScoreDynMarkArray[j].mark) == 0 )
  1894. break;
  1895. if( _cmXScoreDynMarkArray[j].mark == NULL )
  1896. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The dynamic mark '%s' is not legal on line:%i.",mark,lineNumb);
  1897. *dynIdxRef = j;
  1898. if( begForkFl )
  1899. *flagsRef = cmSetFlag(*flagsRef,kDynBegForkXsFl);
  1900. if( endForkFl )
  1901. *flagsRef = cmSetFlag(*flagsRef,kDynEndForkXsFl);
  1902. return rc;
  1903. }
  1904. cmXsRC_t _cmXScoreReorderParseFlags(cmXScore_t* p, const cmChar_t* b, unsigned line, unsigned* newFlagsRef )
  1905. {
  1906. cmXsRC_t rc = kOkXsRC;
  1907. const cmChar_t* s;
  1908. bool doneFl = false;
  1909. unsigned i = 0;
  1910. *newFlagsRef = 0;
  1911. // tilde indicates a pedal event
  1912. if((s = strchr(b,'~')) == NULL )
  1913. return rc;
  1914. do
  1915. {
  1916. ++s;
  1917. switch( *s )
  1918. {
  1919. case 'd':
  1920. *newFlagsRef |= kSostDnXsFl; // sostenuto pedal down just after this note onset
  1921. break;
  1922. case 'u':
  1923. *newFlagsRef |= kSostUpXsFl; // sostenuto pedal up just before this event
  1924. break;
  1925. case 'x':
  1926. *newFlagsRef |= (kSostUpXsFl | kSostDnXsFl); // sostenuto pedal up just before this event and sost down just after it.
  1927. break;
  1928. case 'D':
  1929. *newFlagsRef |= kDampDnXsFl; // damper pedal down
  1930. break;
  1931. case 'U':
  1932. *newFlagsRef |= kDampUpXsFl; // damper pedal up
  1933. break;
  1934. case '_':
  1935. *newFlagsRef |= kTieEndXsFl; // set tie end flag
  1936. break;
  1937. case '&':
  1938. *newFlagsRef |= kDeleteXsFl; // delete this evetn
  1939. break;
  1940. default:
  1941. if( i == 0 )
  1942. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected flag marking '%c' on line %i.",*s,line);
  1943. doneFl = true;
  1944. }
  1945. ++i;
  1946. }while(!doneFl);
  1947. return rc;
  1948. }
  1949. cmXsRC_t _cmXScoreReorderParseTick(cmXScore_t* p, const cmChar_t* b, unsigned line, unsigned* tickRef )
  1950. {
  1951. cmXsRC_t rc = kOkXsRC;
  1952. const cmChar_t* s;
  1953. if((s = strchr(b,'@')) == NULL )
  1954. return rc;
  1955. ++s;
  1956. if(!isdigit(*s))
  1957. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected tick reorder value '%c' on line %i.",*s,line);
  1958. if(sscanf(s,"%i",tickRef) != 1 )
  1959. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"tick reorder parse failed on line %i.",line);
  1960. return rc;
  1961. }
  1962. cmXsRC_t _cmXScoreReorderParseGrace(cmXScore_t* p, const cmChar_t* b, unsigned line, cmXsReorder_t* r, unsigned* graceGroupIdRef )
  1963. {
  1964. cmXsRC_t rc = kOkXsRC;
  1965. const cmChar_t* s;
  1966. if((s = strchr(b,'%')) == NULL )
  1967. return rc;
  1968. ++s;
  1969. r->graceGroupId = *graceGroupIdRef;
  1970. while(1)
  1971. {
  1972. switch(*s)
  1973. {
  1974. case 'b': r->graceFlags |= kBegGraceXsFl; break;
  1975. case 'a': r->graceFlags |= kAddGraceXsFl | kEndGraceXsFl; break;
  1976. case 's': r->graceFlags |= kSubGraceXsFl | kEndGraceXsFl; break;
  1977. case 'A': r->graceFlags |= kAFirstGraceXsFl| kEndGraceXsFl; break;
  1978. case 'n': r->graceFlags |= kNFirstGraceXsFl| kEndGraceXsFl; break;
  1979. case 'g': break;
  1980. case '1':
  1981. r->graceGroupId += 1;
  1982. ++s;
  1983. continue;
  1984. case '%':
  1985. *graceGroupIdRef += 1;
  1986. ++s;
  1987. continue;
  1988. default:
  1989. {
  1990. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected grace note reorder character code %c on line %i.",*s,line);
  1991. assert(0);
  1992. }
  1993. }
  1994. break;
  1995. }
  1996. return rc;
  1997. }
  1998. cmXsRC_t _cmXScoreReorderParsePitch(cmXScore_t* p, const cmChar_t* b, unsigned line, unsigned* pitchRef )
  1999. {
  2000. cmXsRC_t rc = kOkXsRC;
  2001. cmChar_t* s;
  2002. cmChar_t buf[4];
  2003. unsigned i,j;
  2004. memset(buf,0,sizeof(buf));
  2005. *pitchRef = 0;
  2006. if((s = strchr(b,'$')) == NULL )
  2007. return rc;
  2008. ++s;
  2009. j=2;
  2010. for(i=0; i<j && *s; ++i,++s)
  2011. {
  2012. buf[i] = *s;
  2013. if( i==1 && (*s=='#' || *s=='b') )
  2014. j = 3;
  2015. if( i==0 && strchr("ABCDEFG",*s)==NULL )
  2016. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal pitch letter ('%c')specification line %i.",*s,line);
  2017. if( i==1 && !isdigit(*s) && *s!='#' && *s!='b' )
  2018. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal pitch level ('%c') specification line %i.",*s,line);
  2019. if( i==2 && !isdigit(*s) )
  2020. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal pitch octave ('%c') specification line %i.",*s,line);
  2021. }
  2022. unsigned pitch = cmSciPitchToMidi(buf);
  2023. if( pitch<kInvalidMidiByte)
  2024. *pitchRef = pitch;
  2025. else
  2026. {
  2027. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"Pitch conversion from '%s' failed on line %i.",buf,line);
  2028. }
  2029. return rc;
  2030. }
  2031. cmXsRC_t _cmXsApplyEditFile( cmXScore_t* p, const cmChar_t* fn )
  2032. {
  2033. typedef enum { kFindMeasStId, kFindEventStId, kReadEventStId } stateId_t;
  2034. cmXsRC_t rc = kOkXsRC;
  2035. cmFileH_t fH = cmFileNullHandle;
  2036. cmChar_t* b = NULL;
  2037. unsigned bN = 0;
  2038. unsigned ln = 0;
  2039. stateId_t stateId = kFindMeasStId;
  2040. unsigned rN = 1024;
  2041. unsigned ri = 0;
  2042. unsigned measNumb = 0;
  2043. unsigned graceGroupId = 1;
  2044. cmXsReorder_t rV[ rN ];
  2045. if( cmFileOpen(&fH,fn,kReadFileFl,p->err.rpt) != kOkFileRC )
  2046. {
  2047. rc = cmErrMsg(&p->err,kFileFailXsRC,"The reordering file '%s' could not be opened.",cmStringNullGuard(fn));
  2048. return rc;
  2049. }
  2050. for(; cmFileGetLineAuto(fH,&b,&bN)==kOkFileRC; ++ln)
  2051. {
  2052. switch( stateId )
  2053. {
  2054. case kFindEventStId: // scanning past labels to an event line
  2055. {
  2056. unsigned voice,loc;
  2057. if( sscanf(b,"%i %i",&voice,&loc) != 2 )
  2058. continue;
  2059. stateId = kReadEventStId;
  2060. }
  2061. // fall through
  2062. case kReadEventStId:
  2063. {
  2064. cmXsReorder_t r;
  2065. char pitchStr[4];
  2066. memset(&r,0,sizeof(r));
  2067. // parse an event line
  2068. if( sscanf(b,"%i %i %i %i %i %f",&r.idx,&r.voice,&r.locIdx,&r.tick,&r.durtn,&r.rval) == 6 )
  2069. {
  2070. assert( strlen(b)>=52);
  2071. int PC = 39; // text file column where first pitch char occurs
  2072. if( b[PC] == ' ')
  2073. r.midi = 0;
  2074. else
  2075. {
  2076. pitchStr[0] = b[PC+0];
  2077. pitchStr[1] = b[PC+1];
  2078. pitchStr[2] = b[PC+2];
  2079. pitchStr[3] = 0;
  2080. if( !isdigit(pitchStr[2]) )
  2081. r.midi = 0;
  2082. else
  2083. {
  2084. if( pitchStr[1] == ' ')
  2085. {
  2086. pitchStr[1] = pitchStr[2];
  2087. pitchStr[2] = 0;
  2088. }
  2089. r.midi = cmSciPitchToMidi(pitchStr);
  2090. }
  2091. }
  2092. // parse the flag edits following a '~'
  2093. if((rc = _cmXScoreReorderParseFlags(p,b,ln+1, &r.newFlags)) != kOkXsRC )
  2094. goto errLabel;
  2095. // parse the dynamic marking following a '!'
  2096. if((rc = _cmXScoreReorderParseDyn(p,b,ln+1,&r.dynIdx, &r.newFlags, measNumb)) != kOkXsRC )
  2097. goto errLabel;
  2098. // parse the @newtick marker
  2099. if((rc = _cmXScoreReorderParseTick(p, b, ln+1, &r.newTick)) != kOkXsRC )
  2100. goto errLabel;
  2101. // parse the %grace note marker
  2102. if((rc = _cmXScoreReorderParseGrace(p, b, ln+1, &r, &graceGroupId)) != kOkXsRC )
  2103. goto errLabel;
  2104. // parse the $pitch marker
  2105. if((rc = _cmXScoreReorderParsePitch(p, b, ln+1, &r.pitch )) != kOkXsRC )
  2106. goto errLabel;
  2107. // process grace notes - these need to be processed separate from
  2108. // the _cmXScoreReorderMeas() because grace notes may cross measure boundaries.
  2109. /*
  2110. if( r.graceType != 0 )
  2111. {
  2112. r.graceGroupId = graceGroupId;
  2113. // if this is an end of a grace note group
  2114. if( r.graceType != 'g' && r.graceType != 'b' )
  2115. {
  2116. graceGroupId += 1;
  2117. }
  2118. }
  2119. */
  2120. // store the record
  2121. assert( ri < rN );
  2122. rV[ri++] = r;
  2123. continue;
  2124. }
  2125. // the end of the measure was encountered -
  2126. // reorder the measure based on the cmXsReorder_t in rV[ri]
  2127. if((rc = _cmXScoreReorderMeas(p, measNumb, rV, ri )) != kOkXsRC )
  2128. goto errLabel;
  2129. ri = 0;
  2130. stateId = kFindMeasStId;
  2131. // fall through
  2132. }
  2133. case kFindMeasStId: // scanning for a bar-line
  2134. {
  2135. char colon;
  2136. if( sscanf(b,"%i %c",&measNumb,&colon) == 2 && colon == ':' )
  2137. {
  2138. //printf("meas: %i \n",measNumb);
  2139. stateId = kFindEventStId;
  2140. }
  2141. }
  2142. break;
  2143. }
  2144. }
  2145. // If reorder records remain to be processed
  2146. if( ri > 0 )
  2147. if((rc = _cmXScoreReorderMeas(p, measNumb, rV, ri )) != kOkXsRC )
  2148. goto errLabel;
  2149. // the ticks may have changed so the 'secs' and 'dsecs' must be updated
  2150. _cmXScoreSetAbsoluteTime( p );
  2151. // the bar lines should be the first event in the measure
  2152. _cmXScoreFixBarLines(p);
  2153. // resort to force the links to be correct
  2154. _cmXScoreSort(p);
  2155. // process the grace notes.
  2156. _cmXScoreProcessGraceNotes( p, graceGroupId );
  2157. // inserting grace notes may have left the score unsorted
  2158. _cmXScoreSort(p);
  2159. // process the dynamic forks
  2160. _cmXScoreProcessDynamicForks(p);
  2161. //_cmXScoreReport(p, NULL, true );
  2162. errLabel:
  2163. cmFileClose(&fH);
  2164. cmMemFree(b);
  2165. return rc;
  2166. }
  2167. cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn, const cmChar_t* editFn )
  2168. {
  2169. cmXsRC_t rc = kOkXsRC;
  2170. if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
  2171. return rc;
  2172. cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
  2173. cmErrSetup(&p->err,&ctx->rpt,"XScore");
  2174. // create a local linked heap
  2175. if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
  2176. return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
  2177. // open the music xml file
  2178. if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
  2179. {
  2180. rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
  2181. goto errLabel;
  2182. }
  2183. //cmXmlPrint(p->xmlH,&ctx->rpt);
  2184. // parse the part-list
  2185. if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
  2186. goto errLabel;
  2187. // parse each score 'part'
  2188. cmXsPart_t* pp = p->partL;
  2189. for(; pp!=NULL; pp=pp->link)
  2190. if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
  2191. goto errLabel;
  2192. // fill in the note->slink chain to link the notes in each measure in time order
  2193. _cmXScoreSort(p);
  2194. // kpl: 4/19/17 fix problem where notes ended up out of order by one tick _cmXScoreSpreadGraceNotes(p);
  2195. _cmXScoreSort(p);
  2196. _cmXScoreResolveTiesAndLoc(p);
  2197. _cmXScoreRemoveDuplicateNotes(p);
  2198. _cmXScoreSetMeasGroups(p,kEvenXsFl);
  2199. _cmXScoreSetMeasGroups(p,kDynXsFl);
  2200. _cmXScoreSetMeasGroups(p,kTempoXsFl);
  2201. //_cmXScoreResolveOctaveShift(p);
  2202. // CSV output initialize failed.
  2203. if( cmCsvInitialize(&p->csvH,ctx) != kOkCsvRC )
  2204. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output object create failed.");
  2205. if( editFn != NULL )
  2206. {
  2207. if((rc = _cmXsApplyEditFile(p,editFn)) != kOkXsRC )
  2208. {
  2209. cmErrMsg(&ctx->err,rc,"XScore reorder failed.");
  2210. goto errLabel;
  2211. }
  2212. }
  2213. // assign durations to pedal down events
  2214. _cmXScoreProcessPedals(p);
  2215. // remove some notes which share a pitch and are overlapped or embedded within another note.
  2216. _cmXScoreProcessOverlappingNotes(p);
  2217. errLabel:
  2218. if( rc != kOkXsRC )
  2219. _cmXScoreFinalize(p);
  2220. else
  2221. hp->h = p;
  2222. return rc;
  2223. }
  2224. cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
  2225. {
  2226. cmXsRC_t rc = kOkXsRC;
  2227. if( hp == NULL || cmXScoreIsValid(*hp)==false )
  2228. return kOkXsRC;
  2229. cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
  2230. if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
  2231. return rc;
  2232. hp->h = NULL;
  2233. return rc;
  2234. }
  2235. bool cmXScoreIsValid( cmXsH_t h )
  2236. { return h.h != NULL; }
  2237. /* CSV score columns
  2238. kMidiFileIdColScIdx= 0,
  2239. kTypeLabelColScIdx = 3,
  2240. kDSecsColScIdx = 4,
  2241. kSecsColScIdx = 5,
  2242. kD0ColScIdx = 9,
  2243. kD1ColScIdx = 10,
  2244. kPitchColScIdx = 11,
  2245. kBarColScIdx = 13,
  2246. kSkipColScIdx = 14,
  2247. kEvenColScIdx = 15,
  2248. kGraceColScIdx = 16,
  2249. kTempoColScIdx = 17,
  2250. kFracColScIdx = 18,
  2251. kDynColScIdx = 19,
  2252. kSectionColScIdx = 20,
  2253. kRecdPlayColScIdx = 21,
  2254. kRemarkColScIdx = 22
  2255. */
  2256. cmXsRC_t _cmXScoreWriteCsvHdr( cmXScore_t* p )
  2257. {
  2258. const cmChar_t* s[] =
  2259. {
  2260. "id","trk","evt","opcode","dticks","micros","status",
  2261. "meta","ch","d0","d1","arg0","arg1","bar","skip",
  2262. "even","grace","tempo","t frac","dyn","section","play_recd","remark",NULL
  2263. };
  2264. cmCsvCell_t* lcp = NULL;
  2265. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymText(p->csvH,s[0]), 0, 0 ) != kOkCsvRC )
  2266. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  2267. unsigned i;
  2268. for(i=1; s[i]!=NULL; ++i)
  2269. {
  2270. if( cmCsvInsertIdentColAfter(p->csvH, lcp, &lcp, s[i], 0 ) != kOkCsvRC )
  2271. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV error inserting CSV title %i.\n",i);
  2272. }
  2273. return kOkXsRC;
  2274. }
  2275. cmXsRC_t _cmXScoreWriteCsvBlankCols( cmXScore_t* p, unsigned cnt, cmCsvCell_t** leftCellPtrPtr )
  2276. {
  2277. unsigned i;
  2278. for(i=0; i<cnt; ++i)
  2279. if( cmCsvInsertIdentColAfter(p->csvH,*leftCellPtrPtr,leftCellPtrPtr,"",0) != kOkCsvRC )
  2280. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV output failed on blank column.");
  2281. return kOkCsvRC;
  2282. }
  2283. const cmChar_t* _cmXScoreTranslateDynamics( cmXScore_t* p, const cmXsNote_t* np, cmChar_t* buf, unsigned bufN )
  2284. {
  2285. if( cmIsFlag(np->flags,kDynXsFl) && np->dynamics != 0 )
  2286. {
  2287. const cmChar_t* dynStr = NULL;
  2288. switch(np->dynamics)
  2289. {
  2290. case 1: dynStr = "pppp"; break;
  2291. case 2: dynStr = "ppp"; break;
  2292. case 3: dynStr = "pp"; break;
  2293. case 4: dynStr = "p"; break;
  2294. case 5: dynStr = "mp"; break;
  2295. case 6: dynStr = "mf"; break;
  2296. case 7: dynStr = "f"; break;
  2297. case 8: dynStr = "ff"; break;
  2298. case 9: dynStr = "fff"; break;
  2299. default:
  2300. cmErrMsg(&p->err,kSyntaxErrorXsRC,"An invalid dynamic value (%i) was encountered.",np->dynamics);
  2301. goto errLabel;
  2302. }
  2303. if( np->dynGroupId == 0 )
  2304. snprintf(buf,bufN,"%s",dynStr);
  2305. else
  2306. snprintf(buf,bufN,"%s %i",dynStr,np->dynGroupId);
  2307. return buf;
  2308. }
  2309. errLabel:
  2310. return "";
  2311. }
  2312. const cmChar_t* cmXsFormatMeasurementCsvField( unsigned flags, unsigned fl, char abbrev, unsigned sectionId, char* buf, unsigned bufN )
  2313. {
  2314. assert( bufN > 1 );
  2315. buf[0] = ' ';
  2316. buf[1] = 0;
  2317. if( cmIsFlag(flags,fl) )
  2318. {
  2319. if( sectionId != 0 )
  2320. snprintf(buf,bufN-1,"%c %i%c",abbrev,sectionId, cmIsFlag(flags,kHeelXsFl)?'*':' ');
  2321. else
  2322. buf[0] = abbrev;
  2323. }
  2324. return buf;
  2325. }
  2326. cmXsRC_t _cmXScoreWriteCsvRow(
  2327. cmXScore_t* p,
  2328. unsigned rowIdx,
  2329. unsigned uid,
  2330. unsigned bar,
  2331. const cmChar_t* sectionStr,
  2332. const cmChar_t* opCodeStr,
  2333. double dsecs,
  2334. double secs,
  2335. unsigned d0,
  2336. unsigned d1,
  2337. unsigned pitch, // set to -1 if the pitch is not valid
  2338. double frac,
  2339. const cmChar_t* dynStr,
  2340. unsigned flags,
  2341. const cmChar_t* evenStr,
  2342. const cmChar_t* tempoStr)
  2343. {
  2344. cmXsRC_t rc = kOkXsRC;
  2345. cmCsvCell_t* lcp = NULL;
  2346. // append an empty row to the CSV object
  2347. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymUInt(p->csvH, rowIdx ), 0, 0 ) != kOkCsvRC )
  2348. {
  2349. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  2350. goto errLabel;
  2351. }
  2352. /*
  2353. // col 0 : blanks
  2354. if( cmCsvInsertUIntColAfter(p->csvH, lcp, &lcp, rowIdx, 0 ) != kOkCsvRC )
  2355. {
  2356. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output row index failed.");
  2357. goto errLabel;
  2358. }
  2359. */
  2360. // col 1 : track (always 1)
  2361. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,1,0) != kOkCsvRC )
  2362. {
  2363. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  2364. goto errLabel;
  2365. }
  2366. // col 2 : evt (set to event uid, or blank if uid == -1)
  2367. if( uid == -1 )
  2368. {
  2369. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2370. goto errLabel;
  2371. }
  2372. else
  2373. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,uid,0) != kOkCsvRC )
  2374. {
  2375. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  2376. goto errLabel;
  2377. }
  2378. // col 3 : output the opcode
  2379. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,opCodeStr,0) != kOkCsvRC )
  2380. {
  2381. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on opcode label.");
  2382. goto errLabel;
  2383. }
  2384. // col 4 : dsecs
  2385. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,dsecs,0) != kOkCsvRC )
  2386. {
  2387. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dsecs'.");
  2388. goto errLabel;
  2389. }
  2390. // col 5 : secs
  2391. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,secs,0) != kOkCsvRC )
  2392. {
  2393. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'secs'.");
  2394. goto errLabel;
  2395. }
  2396. // cols 6,7,8 blanks
  2397. if((rc = _cmXScoreWriteCsvBlankCols(p,3,&lcp)) != kOkXsRC )
  2398. goto errLabel;
  2399. // col 9 : d0
  2400. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d0,0) != kOkCsvRC )
  2401. {
  2402. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  2403. goto errLabel;
  2404. }
  2405. // col 10 : d1
  2406. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d1,0) != kOkCsvRC )
  2407. {
  2408. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd1'.");
  2409. goto errLabel;
  2410. }
  2411. // col 11 : pitch
  2412. if( pitch == -1 )
  2413. {
  2414. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2415. goto errLabel;
  2416. }
  2417. else
  2418. {
  2419. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmMidiToSciPitch(pitch,NULL,0),0) != kOkCsvRC )
  2420. {
  2421. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  2422. goto errLabel;
  2423. }
  2424. }
  2425. // col 12 : blanks
  2426. if((rc = _cmXScoreWriteCsvBlankCols(p,1 + (cmIsFlag(flags,kBarXsFl) ? 0 : 1), &lcp)) != kOkXsRC )
  2427. goto errLabel;
  2428. // col 13 : bar number
  2429. if( cmIsFlag(flags,kBarXsFl) )
  2430. {
  2431. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,bar,0) != kOkCsvRC )
  2432. {
  2433. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  2434. goto errLabel;
  2435. }
  2436. }
  2437. // col 14 : skip (blank for now)
  2438. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2439. goto errLabel;
  2440. // col 15: even (all grace notes are 'even' notes
  2441. if( cmCsvInsertQTextColAfter(p->csvH,lcp,&lcp, evenStr, 0) != kOkCsvRC )
  2442. {
  2443. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  2444. goto errLabel;
  2445. }
  2446. // col 16: grace
  2447. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kGraceXsFl) ? "g" : "",0) != kOkCsvRC )
  2448. {
  2449. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  2450. goto errLabel;
  2451. }
  2452. // col 17: tempo
  2453. if( cmCsvInsertQTextColAfter(p->csvH,lcp,&lcp,tempoStr,0) != kOkCsvRC )
  2454. {
  2455. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  2456. goto errLabel;
  2457. }
  2458. // col 18: frac
  2459. if( frac == 0 )
  2460. {
  2461. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2462. goto errLabel;
  2463. }
  2464. else
  2465. {
  2466. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,1.0/frac,0) != kOkCsvRC )
  2467. {
  2468. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 't frac'.");
  2469. goto errLabel;
  2470. }
  2471. }
  2472. // col 19: dynamic marking
  2473. if( cmCsvInsertQTextColAfter(p->csvH, lcp, &lcp, dynStr, 0 ) != kOkCsvRC )
  2474. {
  2475. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dynamics'.");
  2476. goto errLabel;
  2477. }
  2478. //if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2479. // goto errLabel;
  2480. // col 20: section
  2481. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,sectionStr!=NULL ? sectionStr : "",0) != kOkCsvRC )
  2482. {
  2483. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  2484. goto errLabel;
  2485. }
  2486. // col 21, 22 : recd-play, remark (blank for now)
  2487. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  2488. goto errLabel;
  2489. errLabel:
  2490. return rc;
  2491. }
  2492. cmXsRC_t cmXScoreWriteCsv( cmXsH_t h, int begMeasNumb, const cmChar_t* csvFn )
  2493. {
  2494. cmXsRC_t rc = kOkXsRC;
  2495. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  2496. unsigned rowIdx = 1;
  2497. const cmChar_t* sectionIdStr = NULL;
  2498. double baseSecs = -1;
  2499. if( !cmCsvIsValid(p->csvH) )
  2500. return cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output object is not initialized.");
  2501. if((rc = _cmXScoreWriteCsvHdr( p )) != kOkXsRC )
  2502. goto errLabel;
  2503. cmXsPart_t* pp = p->partL;
  2504. for(; pp!=NULL; pp=pp->link)
  2505. {
  2506. cmXsMeas_t* mp = pp->measL;
  2507. for(; mp!=NULL; mp=mp->link)
  2508. {
  2509. if( mp->number < begMeasNumb)
  2510. continue;
  2511. cmXsNote_t* np = mp->noteL;
  2512. if( baseSecs == -1 )
  2513. baseSecs = np->secs;
  2514. for(; np!=NULL; np=np->slink)
  2515. {
  2516. double thisSecs = np->secs - baseSecs;
  2517. // if this is a section event
  2518. if( cmIsFlag(np->flags,kSectionXsFl) )
  2519. sectionIdStr = np->tvalue;
  2520. // if this is a bar event
  2521. if( cmIsFlag(np->flags,kBarXsFl) )
  2522. {
  2523. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"bar",np->dsecs,thisSecs,0,0,-1,0,"",np->flags,"","");
  2524. sectionIdStr = NULL;
  2525. }
  2526. else
  2527. {
  2528. // if this is a pedal event
  2529. if( cmIsFlag(np->flags,kDampDnXsFl|kDampUpXsFl|kDampUpDnXsFl|kSostDnXsFl|kSostUpXsFl) )
  2530. {
  2531. unsigned d0 = cmIsFlag(np->flags,kSostDnXsFl |kSostUpXsFl) ? 66 : 64; // pedal MIDI ctl id
  2532. unsigned d1 = cmIsFlag(np->flags,kDampDnXsFl|kSostDnXsFl) ? 64 : 0; // pedal-dn: d1>=64 pedal-up:<64
  2533. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"ctl",np->dsecs,thisSecs,d0,d1,-1,0,"",np->flags,"","");
  2534. sectionIdStr = NULL;
  2535. if( cmIsFlag(np->flags,kDampUpDnXsFl) )
  2536. {
  2537. rowIdx += 1;
  2538. double millisecond = 0.0;
  2539. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"ctl",millisecond,thisSecs+millisecond,d0,64,-1,0,"",np->flags,"","");
  2540. }
  2541. }
  2542. else
  2543. {
  2544. // if this is a sounding note event
  2545. if( cmIsFlag(np->flags,kOnsetXsFl) )
  2546. {
  2547. unsigned bufN = 128;
  2548. cmChar_t ebuf[ bufN+1]; ebuf[bufN] = 0;
  2549. cmChar_t dbuf[ bufN+1]; dbuf[bufN] = 0;
  2550. cmChar_t tbuf[ bufN+1]; tbuf[bufN] = 0;
  2551. double frac = np->rvalue + (cmIsFlag(np->flags,kDotXsFl) ? (np->rvalue/2) : 0);
  2552. const cmChar_t* dyn = _cmXScoreTranslateDynamics( p, np, dbuf, bufN );
  2553. unsigned vel = np->vel==0 ? 60 : np->vel;
  2554. //
  2555. _cmXScoreWriteCsvRow(p,rowIdx,np->uid,mp->number,sectionIdStr,"non",np->dsecs,thisSecs,np->pitch,vel,np->pitch,frac,dyn,np->flags,
  2556. cmXsFormatMeasurementCsvField(np->flags, kEvenXsFl, 'e', np->evenGroupId, ebuf, bufN ),
  2557. cmXsFormatMeasurementCsvField(np->flags, kTempoXsFl,'t', np->tempoGroupId, tbuf, bufN ));
  2558. sectionIdStr = NULL;
  2559. }
  2560. }
  2561. }
  2562. rowIdx += 1;
  2563. }
  2564. }
  2565. }
  2566. // Section labels are output on the next bar/pedal/note
  2567. // but what if there is no bar pedal note-on after the section label
  2568. if( sectionIdStr != NULL )
  2569. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The section label '%s' was ignored because it was not followed by a score event.",sectionIdStr);
  2570. if( cmCsvWrite( p->csvH, csvFn ) != kOkCsvRC )
  2571. rc = cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output write failed on file '%s'.",csvFn);
  2572. errLabel:
  2573. return rc;
  2574. }
  2575. bool _cmXsIsCsvValid(cmCtx_t* ctx, cmXsH_t h, const cmChar_t* outFn)
  2576. {
  2577. bool retFl = true;
  2578. cmScH_t scH = cmScNullHandle;
  2579. double srate = 44100.0;
  2580. cmSymTblH_t stH = cmSymTblCreate(cmSymTblNullHandle, 0, ctx );
  2581. if( cmScoreInitialize( ctx, &scH, outFn, srate, NULL, 0, NULL, NULL, stH) != kOkScRC )
  2582. {
  2583. cmErrMsg(&ctx->err,kFileFailXsRC,"The generated CSV file (%s) could not be parsed.",cmStringNullGuard(outFn));
  2584. retFl = false;
  2585. }
  2586. else
  2587. {
  2588. //cmScorePrintSets(scH,&ctx->rpt);
  2589. //cmScorePrint(scH,&ctx->rpt);
  2590. cmScoreFinalize(&scH);
  2591. }
  2592. cmSymTblDestroy(&stH);
  2593. return retFl;
  2594. }
  2595. void _cmXScoreReportTitle( cmRpt_t* rpt )
  2596. {
  2597. cmRptPrintf(rpt," idx voc loc tick durtn rval flags\n");
  2598. cmRptPrintf(rpt," --- --- ----- ------- ----- ---- --- ---------------\n");
  2599. }
  2600. void _cmXScoreReportNote( cmRpt_t* rpt, const cmXsNote_t* note,unsigned index )
  2601. {
  2602. const cmChar_t* B = cmIsFlag(note->flags,kBarXsFl) ? "|" : "-";
  2603. const cmChar_t* R = cmIsFlag(note->flags,kRestXsFl) ? "R" : "-";
  2604. const cmChar_t* G = cmIsFlag(note->flags,kGraceXsFl) ? "G" : "-";
  2605. const cmChar_t* D = cmIsFlag(note->flags,kDotXsFl) ? "." : "-";
  2606. const cmChar_t* C = cmIsFlag(note->flags,kChordXsFl) ? "C" : "-";
  2607. const cmChar_t* P = cmIsFlag(note->flags,kDampDnXsFl) ? "V" : "-";
  2608. const cmChar_t* s = cmIsFlag(note->flags,kSostDnXsFl) ? "{" : "-";
  2609. const cmChar_t* S = cmIsFlag(note->flags,kSectionXsFl) ? "S" : "-";
  2610. const cmChar_t* H = cmIsFlag(note->flags,kHeelXsFl) ? "H" : "-";
  2611. const cmChar_t* T0 = cmIsFlag(note->flags,kTieBegXsFl) ? "T" : "-";
  2612. const cmChar_t* T1 = cmIsFlag(note->flags,kTieEndXsFl) ? "_" : "-";
  2613. const cmChar_t* O = cmIsFlag(note->flags,kOnsetXsFl) ? "*" : "-";
  2614. const cmChar_t* e = cmIsFlag(note->flags,kEvenXsFl) ? "e" : "-";
  2615. const cmChar_t* d = cmIsFlag(note->flags,kDynXsFl) ? "d" : "-";
  2616. const cmChar_t* t = cmIsFlag(note->flags,kTempoXsFl) ? "t" : "-";
  2617. if( cmIsFlag(note->flags,kEvenEndXsFl) ) e="E";
  2618. if( cmIsFlag(note->flags,kDynEndXsFl) ) d="D";
  2619. if( cmIsFlag(note->flags,kTempoEndXsFl) ) t="T";
  2620. P = cmIsFlag(note->flags,kDampUpXsFl) ? "^" : P;
  2621. P = cmIsFlag(note->flags,kDampUpDnXsFl) ? "X" : P;
  2622. s = cmIsFlag(note->flags,kSostUpXsFl) ? "}" : s;
  2623. //const cmChar_t* N = note->pitch==0 ? " " : cmMidiToSciPitch( note->pitch, NULL, 0 );
  2624. cmChar_t N[] = {'\0','\0','\0','\0'};
  2625. cmChar_t acc = note->alter==-1?'b':(note->alter==1?'#':' ');
  2626. snprintf(N,4,"%c%c%1i",note->step,acc,note->octave);
  2627. cmRptPrintf(rpt," %3i %3i %5i %7i %5i %4.1f %3s %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  2628. index,
  2629. note->voice->id,
  2630. note->locIdx,
  2631. note->tick,
  2632. note->tied_dur,
  2633. note->rvalue,
  2634. N,B,R,G,D,C,e,d,t,P,s,S,H,T0,T1,O);
  2635. if( cmIsFlag(note->flags,kSectionXsFl) )
  2636. cmRptPrintf(rpt," %s",cmStringNullGuard(note->tvalue));
  2637. if( cmIsFlag(note->flags,kMetronomeXsFl) )
  2638. cmRptPrintf(rpt," %i bpm",note->duration);
  2639. if( note->evenGroupId != 0 )
  2640. cmRptPrintf(rpt," e=%i",note->evenGroupId);
  2641. if( note->dynGroupId != 0 )
  2642. cmRptPrintf(rpt," d=%i",note->dynGroupId);
  2643. if( note->tempoGroupId != 0 )
  2644. cmRptPrintf(rpt," t=%i",note->tempoGroupId);
  2645. if( note->graceGroupId != 0)
  2646. cmRptPrintf(rpt," g=%i",note->graceGroupId);
  2647. if( note->dynamics != 0)
  2648. cmRptPrintf(rpt," dyn=%i %i",note->dynamics,note->vel);
  2649. /*
  2650. if( cmIsFlag(note->flags,kBegGraceXsFl) )
  2651. cmRptPrintf(rpt," B");
  2652. if( cmIsFlag(note->flags,kEndGraceXsFl) )
  2653. cmRptPrintf(rpt," E");
  2654. if( cmIsFlag(note->flags,kDynBegForkXsFl) )
  2655. cmRptPrintf(rpt," B");
  2656. if( cmIsFlag(note->flags,kDynEndForkXsFl) )
  2657. cmRptPrintf(rpt," E");
  2658. */
  2659. }
  2660. void _cmXScoreReport( cmXScore_t* p, cmRpt_t* rpt, bool sortFl )
  2661. {
  2662. if( rpt == NULL )
  2663. rpt = p->err.rpt;
  2664. cmXsPart_t* pp = p->partL;
  2665. for(; pp!=NULL; pp=pp->link)
  2666. {
  2667. cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
  2668. const cmXsMeas_t* meas = pp->measL;
  2669. for(; meas!=NULL; meas=meas->link)
  2670. {
  2671. unsigned idx = 0;
  2672. cmRptPrintf(rpt," %i : div:%i beat:%i beat-type:%i (%i)\n",meas->number,meas->divisions,meas->beats,meas->beat_type,meas->divisions*meas->beats);
  2673. _cmXScoreReportTitle(rpt);
  2674. if( sortFl )
  2675. {
  2676. const cmXsNote_t* note = meas->noteL;
  2677. unsigned t0 = 0;
  2678. unsigned t1 = 0;
  2679. for(; note!=NULL; note=note->slink,++idx)
  2680. {
  2681. _cmXScoreReportNote(rpt,note,idx);
  2682. t1 = note->slink==NULL ? note->tick : note->slink->tick;
  2683. if( !(t0 <= note->tick && note->tick <= t1) )
  2684. {
  2685. cmRptPrintf(rpt," +");
  2686. }
  2687. t0 = note->tick;
  2688. if( note->slink!=NULL || note->voice->id==0)
  2689. cmRptPrintf(rpt,"\n");
  2690. else
  2691. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  2692. }
  2693. }
  2694. else
  2695. {
  2696. const cmXsVoice_t* v = meas->voiceL;
  2697. for(; v!=NULL; v=v->link)
  2698. {
  2699. const cmXsNote_t* note = v->noteL;
  2700. cmRptPrintf(rpt," voice:%i\n",v->id);
  2701. for(; note!=NULL; note=note->mlink)
  2702. {
  2703. _cmXScoreReportNote(rpt,note,idx);
  2704. if( note->mlink!=NULL || note->voice->id==0)
  2705. cmRptPrintf(rpt,"\n");
  2706. else
  2707. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  2708. }
  2709. }
  2710. }
  2711. }
  2712. }
  2713. }
  2714. void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt, bool sortFl )
  2715. {
  2716. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  2717. return _cmXScoreReport(p,rpt,sortFl);
  2718. }
  2719. void _cmXScoreGenEditFileWrite( void* arg, const cmChar_t* text )
  2720. {
  2721. if( text != NULL && arg != NULL )
  2722. {
  2723. cmFileH_t* hp = (cmFileH_t*)arg;
  2724. cmFilePrint(*hp,text);
  2725. }
  2726. }
  2727. cmXsRC_t cmXScoreGenEditFile( cmCtx_t* ctx, const cmChar_t* xmlFn, const cmChar_t* outFn )
  2728. {
  2729. cmXsH_t xsH = cmXsNullHandle;
  2730. cmFileH_t fH = cmFileNullHandle;
  2731. cmXsRC_t rc = kOkXsRC;
  2732. cmErr_t err;
  2733. cmRpt_t rpt;
  2734. cmErrSetup(&err,&ctx->rpt,"cmXScoreGenEditFile");
  2735. cmRptSetup(&rpt,_cmXScoreGenEditFileWrite,_cmXScoreGenEditFileWrite,&fH);
  2736. if((rc = cmXScoreInitialize(ctx,&xsH,xmlFn,NULL)) != kOkXsRC )
  2737. return rc;
  2738. if( cmFileOpen(&fH,outFn,kWriteFileFl,&ctx->rpt) != kOkFileRC )
  2739. {
  2740. cmErrMsg(&err,kFileFailXsRC,"Unable to open the output file '%s'.",cmStringNullGuard(outFn));
  2741. goto errLabel;
  2742. }
  2743. cmXScoreReport(xsH,&rpt,true);
  2744. errLabel:
  2745. if( cmFileClose(&fH) != kOkFileRC )
  2746. rc = cmErrMsg(&err,kFileFailXsRC,"File close failed on '%s'.",cmStringNullGuard(outFn));
  2747. cmXScoreFinalize(&xsH);
  2748. return rc;
  2749. }
  2750. typedef struct
  2751. {
  2752. unsigned ival;
  2753. double fval;
  2754. unsigned cnt;
  2755. } cmXsHist_t;
  2756. void _cmXsHistUpdateI( cmXsHist_t* hist, unsigned histN, unsigned ival )
  2757. {
  2758. unsigned i;
  2759. for(i=0; i<histN && hist[i].cnt!=0; ++i)
  2760. if( hist[i].ival == ival )
  2761. break;
  2762. if( i==histN )
  2763. return;
  2764. hist[i].ival = ival;
  2765. hist[i].cnt += 1;
  2766. }
  2767. void _cmXsHistUpdateF( cmXsHist_t* hist, unsigned histN, double fval )
  2768. {
  2769. unsigned i;
  2770. for(i=0; i<histN && hist[i].cnt!=0; ++i)
  2771. if( hist[i].fval == fval )
  2772. break;
  2773. if( i==histN )
  2774. return;
  2775. hist[i].fval = fval;
  2776. hist[i].cnt += 1;
  2777. }
  2778. unsigned _cmXsHistValue( cmXsHist_t* hist, unsigned histN )
  2779. {
  2780. unsigned i,n;
  2781. for(i=0,n=0; i<histN && hist[i].cnt>0; ++i)
  2782. n += 1;
  2783. return n;
  2784. }
  2785. // Measure the score complexity for the the time window 'wndSecs' seconds
  2786. // prior to the note n1 and following n0.
  2787. const cmXsNote_t* _cmXsMeasComplexityInWindow( const cmXsNote_t* n0, cmXsNote_t* n1, double wndSecs )
  2788. {
  2789. const cmXsNote_t* n2 = NULL;
  2790. unsigned l_pch_0 = 0;
  2791. unsigned l_pch_value = 0;
  2792. unsigned l_pch_cnt = n1->staff==1 ? 0 : 1;
  2793. unsigned r_pch_0 = n1->staff==1 ? 1 : 0;
  2794. unsigned r_pch_value = 0;
  2795. unsigned r_pch_cnt = 0;
  2796. unsigned i = 0;
  2797. unsigned histN = 100;
  2798. cmXsHist_t velHist[ histN ];
  2799. cmXsHist_t rymHist[ histN ];
  2800. memset(velHist,0,sizeof(velHist));
  2801. memset(rymHist,0,sizeof(rymHist));
  2802. const cmXsNote_t* n = n0;
  2803. while(n!=NULL && n != n1)
  2804. {
  2805. // if this event is less than wndSecs behind 'n1' and is not a sounding note ...
  2806. if( n1->secs - n->secs <= wndSecs && cmIsFlag(n->flags,kOnsetXsFl) )
  2807. {
  2808. _cmXsHistUpdateI( velHist, histN, n->dynamics );
  2809. _cmXsHistUpdateF( rymHist, histN, n->rvalue );
  2810. switch( n->staff )
  2811. {
  2812. case 1: // treble cleff
  2813. if( i > 0 )
  2814. {
  2815. r_pch_value += r_pch_0 > n->pitch ? r_pch_0-n->pitch : n->pitch-r_pch_0;
  2816. r_pch_cnt += 1;
  2817. }
  2818. r_pch_0 = n->pitch;
  2819. break;
  2820. case 2: // bass cleff
  2821. if( i > 0 )
  2822. {
  2823. l_pch_value += l_pch_0 > n->pitch ? l_pch_0-n->pitch : n->pitch-l_pch_0;
  2824. l_pch_cnt += 1;
  2825. }
  2826. l_pch_0 = n->pitch;
  2827. break;
  2828. default:
  2829. { assert(0); }
  2830. }
  2831. // track the first note that is inside the window
  2832. if( i == 0 )
  2833. n2 = n;
  2834. // count the number of notes in the window
  2835. i += 1;
  2836. }
  2837. cmXsMeas_t* m = n->meas;
  2838. // advance th note pointer
  2839. n = n->slink;
  2840. // if we have reached the end of a measure
  2841. if( n == NULL )
  2842. {
  2843. if( m != NULL )
  2844. {
  2845. m = m->link;
  2846. if( m != NULL )
  2847. n = m->noteL;
  2848. }
  2849. }
  2850. }
  2851. // update the cplx record in n1 with the results of this window analysis
  2852. n1->cplx.sum_d_vel = _cmXsHistValue( velHist, histN );
  2853. n1->cplx.sum_d_rym = _cmXsHistValue( rymHist, histN );
  2854. n1->cplx.sum_d_lpch = l_pch_value;
  2855. n1->cplx.sum_n_lpch = l_pch_cnt;
  2856. n1->cplx.sum_d_rpch = r_pch_value;
  2857. n1->cplx.sum_n_rpch = r_pch_cnt;
  2858. return n2;
  2859. }
  2860. // Measure the score complexity and fill in the cmXsComplexity_t record associated
  2861. // with the cmXsNote_t record of each sounding note.
  2862. cmXsRC_t _cmXsMeasComplexity( cmXsH_t h, double wndSecs )
  2863. {
  2864. cmXsRC_t rc = kOkXsRC;
  2865. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  2866. cmXsPart_t* pp = p->partL;
  2867. memset(&p->cplx_max,0,sizeof(p->cplx_max));
  2868. const cmXsNote_t* n0 = NULL;
  2869. // for each part
  2870. for(; pp!=NULL; pp=pp->link)
  2871. {
  2872. cmXsMeas_t* mp = pp->measL;
  2873. // for each measure
  2874. for(; mp!=NULL; mp=mp->link)
  2875. {
  2876. cmXsNote_t* n1 = mp->noteL;
  2877. // for each note in this measure
  2878. for(; n1!=NULL; n1=n1->slink)
  2879. if( cmIsFlag(n1->flags,kOnsetXsFl) )
  2880. {
  2881. if( n0 == NULL )
  2882. n0 = n1;
  2883. else
  2884. if((n0 = _cmXsMeasComplexityInWindow(n0,n1,wndSecs)) == NULL )
  2885. n0 = n1;
  2886. // track the max value for all complexity values to allow
  2887. // eventual normalization of the complexity values
  2888. p->cplx_max.sum_d_vel = cmMax(p->cplx_max.sum_d_vel, n1->cplx.sum_d_vel);
  2889. p->cplx_max.sum_d_rym = cmMax(p->cplx_max.sum_d_rym, n1->cplx.sum_d_rym);
  2890. p->cplx_max.sum_d_lpch = cmMax(p->cplx_max.sum_d_lpch,n1->cplx.sum_d_lpch);
  2891. p->cplx_max.sum_n_lpch = cmMax(p->cplx_max.sum_n_lpch,n1->cplx.sum_n_lpch);
  2892. p->cplx_max.sum_d_rpch = cmMax(p->cplx_max.sum_d_rpch,n1->cplx.sum_d_rpch);
  2893. p->cplx_max.sum_n_rpch = cmMax(p->cplx_max.sum_n_rpch,n1->cplx.sum_n_rpch);
  2894. }
  2895. }
  2896. }
  2897. return rc;
  2898. }
  2899. cmXsRC_t _cmXsWriteMidiFile( cmCtx_t* ctx, cmXsH_t h, int beginMeasNumb, int beginBPM, const cmChar_t* dir, const cmChar_t* fn )
  2900. {
  2901. cmXsRC_t rc = kOkXsRC;
  2902. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  2903. if( p->partL==NULL || p->partL->measL == NULL )
  2904. return rc;
  2905. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  2906. unsigned trkN = 2;
  2907. unsigned ticksPerQN = p->partL->measL->divisions;
  2908. const cmChar_t* outFn = cmFsMakeFn(dir,fn,"mid",NULL);
  2909. unsigned baseTick = -1;
  2910. unsigned bpm = beginBPM==0 ? 60 : beginBPM;
  2911. //if( cmMidiFileCreate( ctx, &mfH, trkN, ticksPerQN ) != kOkMfRC )
  2912. // return cmErrMsg(&p->err,kMidiFailXsRC,"Unable to create the MIDI file object.");
  2913. cmXsPart_t* pp = p->partL;
  2914. // for each part
  2915. for(; pp!=NULL; pp=pp->link)
  2916. {
  2917. cmXsMeas_t* mp = pp->measL;
  2918. // for each measure
  2919. for(; mp!=NULL; mp=mp->link)
  2920. {
  2921. // skip all measures until we reach the first measure to output
  2922. if(mp->number < beginMeasNumb)
  2923. continue;
  2924. // if the MIDI file has not yet been created
  2925. if( !cmMidiFileIsValid(mfH) )
  2926. {
  2927. ticksPerQN = mp->divisions;
  2928. // create the MIDI file
  2929. if( cmMidiFileCreate( ctx, &mfH, trkN, ticksPerQN ) != kOkMfRC )
  2930. {
  2931. rc = cmErrMsg(&p->err,kMidiFailXsRC,"Unable to create the MIDI file object.");
  2932. goto errLabel;
  2933. }
  2934. // set the starting tempo
  2935. cmMidFileInsertTrackTempoMsg(mfH, 0, 0, bpm );
  2936. }
  2937. cmXsNote_t* np = mp->noteL;
  2938. if( baseTick == -1 )
  2939. baseTick = np->tick;
  2940. if( mp->divisions != ticksPerQN )
  2941. cmErrWarnMsg(&p->err,kMidiFailXsRC,"The 'tick per quarter note' (divisions) field in measure %i does not match the value in the first measure (%i).",mp->divisions,ticksPerQN);
  2942. unsigned ni = 0;
  2943. // for each note in this measure
  2944. for(; np!=NULL; np=np->slink,++ni)
  2945. {
  2946. unsigned thisTick = np->tick - baseTick;
  2947. switch( np->flags & (kOnsetXsFl|kMetronomeXsFl|kDampDnXsFl|kDampUpDnXsFl|kSostDnXsFl) )
  2948. {
  2949. case kOnsetXsFl:
  2950. if( cmMidiFileIsValid(mfH) )
  2951. {
  2952. if( np->tied_dur <= 0 )
  2953. cmErrWarnMsg(&p->err,kOkXsRC,"A zero length note was encountered bar:%i tick:%i (%i) %s",np->meas->number,np->tick,thisTick,cmMidiToSciPitch(np->pitch,NULL,0));
  2954. /*
  2955. if( mp->number == 20 )
  2956. {
  2957. _cmXScoreReportNote(ctx->err.rpt,np,ni);
  2958. cmRptPrintf(ctx->err.rpt,"\n");
  2959. }
  2960. */
  2961. if( np->vel == 0 )
  2962. cmErrWarnMsg(&p->err,kOkXsRC,"A sounding note with zero velocity was encountered at bar:%i tick:%i pitch:%s.",np->meas->number,np->tick,cmMidiToSciPitch(np->pitch,NULL,0));
  2963. if( cmMidiFileInsertTrackChMsg(mfH, 1, thisTick, kNoteOnMdId, np->pitch, np->vel ) != kOkMfRC
  2964. ||cmMidiFileInsertTrackChMsg(mfH, 1, thisTick + np->tied_dur, kNoteOffMdId, np->pitch, 0 ) != kOkMfRC )
  2965. {
  2966. rc = kMidiFailXsRC;
  2967. }
  2968. }
  2969. break;
  2970. case kDampDnXsFl:
  2971. case kDampUpDnXsFl:
  2972. case kSostDnXsFl:
  2973. if( cmMidiFileIsValid(mfH) )
  2974. {
  2975. if( np->duration <= 0 )
  2976. cmErrWarnMsg(&p->err,kOkXsRC,"A zero length pedal event was encountered bar:%i tick:%i (%i)",np->meas->number,np->tick,thisTick);
  2977. cmMidiByte_t d0 = cmIsFlag(np->flags,kSostDnXsFl) ? kSostenutoCtlMdId : kSustainCtlMdId;
  2978. if( (cmMidiFileInsertTrackChMsg(mfH, 1, thisTick, kCtlMdId, d0, 127 ) != kOkMfRC )
  2979. ||(cmMidiFileInsertTrackChMsg(mfH, 1, thisTick + np->duration, kCtlMdId, d0, 0 ) != kOkMfRC ) )
  2980. {
  2981. rc = kMidiFailXsRC;
  2982. }
  2983. }
  2984. break;
  2985. case kMetronomeXsFl:
  2986. bpm = np->duration;
  2987. if( cmMidiFileIsValid(mfH) )
  2988. if( cmMidFileInsertTrackTempoMsg(mfH, 0, thisTick, bpm ) != kOkMfRC )
  2989. rc = kMidiFailXsRC;
  2990. break;
  2991. case 0:
  2992. break;
  2993. default:
  2994. { assert(0); }
  2995. }
  2996. if( rc != kOkXsRC )
  2997. {
  2998. rc = cmErrMsg(&p->err,rc,"MIDI message insert failed on '%s'.",cmStringNullGuard(outFn));
  2999. goto errLabel;
  3000. }
  3001. }
  3002. }
  3003. }
  3004. if( cmMidiFileIsValid(mfH) )
  3005. if( cmMidiFileWrite(mfH,outFn) != kOkMfRC )
  3006. {
  3007. rc = cmErrMsg(&p->err,kMidiFailXsRC,"MIDI file write failed on '%s'.",cmStringNullGuard(outFn));
  3008. goto errLabel;
  3009. }
  3010. errLabel:
  3011. cmFsFreeFn(outFn);
  3012. if( cmMidiFileClose(&mfH) != kOkMfRC )
  3013. {
  3014. rc = cmErrMsg(&p->err,kMidiFailXsRC,"Unable to create the MIDI file object.");
  3015. goto errLabel;
  3016. }
  3017. return rc;
  3018. }
  3019. bool _cmXsIsMidiFileValid( cmCtx_t* ctx, cmXsH_t h, const cmChar_t* dir, const cmChar_t* fn )
  3020. {
  3021. const cmChar_t* midiFn = cmFsMakeFn(dir,fn,"mid",NULL);
  3022. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  3023. if( cmMidiFileOpen( ctx, &mfH, midiFn ) == kOkMfRC )
  3024. {
  3025. cmMidiFileClose(&mfH);
  3026. return true;
  3027. }
  3028. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  3029. cmErrMsg(&p->err,kMidiFailXsRC,"The generated MIDI file '%s' is not valid.", cmStringNullGuard(midiFn));
  3030. return false;
  3031. }
  3032. typedef struct cmXsSvgEvt_str
  3033. {
  3034. unsigned flags; // k???XsFl
  3035. unsigned tick; // start tick
  3036. unsigned durTicks; // dur-ticks
  3037. unsigned voice; // score voice number
  3038. unsigned d0; // MIDI d0 (barNumb)
  3039. unsigned d1; // MIDI d1
  3040. cmXsComplexity_t cplx;
  3041. struct cmXsSvgEvt_str* link;
  3042. } cmXsSvgEvt_t;
  3043. typedef struct cmXsMidiFile_str
  3044. {
  3045. cmXsSvgEvt_t* elist;
  3046. cmXsSvgEvt_t* eol;
  3047. unsigned pitch_min;
  3048. unsigned pitch_max;
  3049. } cmXsMidiFile_t;
  3050. void _cmXsWriteMidiSvgLegend( cmSvgH_t svgH, unsigned index, const cmChar_t* label, const cmChar_t* classStr )
  3051. {
  3052. double x = 100;
  3053. double y = 120*10 - 20*index;
  3054. cmSvgWriterText( svgH, x, y, label, "legend" );
  3055. x += 75;
  3056. cmSvgWriterLine( svgH, x, y, x+125, y, classStr );
  3057. }
  3058. void _cmXsWriteMidiSvgLinePoint( cmSvgH_t svgH, double x0, double y0, double x1, double y1, double y_max, const cmChar_t* classStr, const cmChar_t* label )
  3059. {
  3060. int bn = 255;
  3061. char b[bn+1];
  3062. double y_scale = 10;
  3063. double y_label = y1;
  3064. b[0] = 0;
  3065. y0 = (y0/y_max) * 127.0 * y_scale;
  3066. y1 = (y1/y_max) * 127.0 * y_scale;
  3067. cmSvgWriterLine(svgH, x0, y0, x1, y1, classStr );
  3068. if( y0 != y1 )
  3069. snprintf(b,bn,"%5.0f %s",y_label,label==NULL?"":label);
  3070. else
  3071. {
  3072. if( label != NULL )
  3073. snprintf(b,bn,"%s",label);
  3074. }
  3075. if( strlen(b) )
  3076. cmSvgWriterText(svgH, x1, y1, b, "pt_text");
  3077. }
  3078. cmXsRC_t _cmXsWriteMidiSvg( cmCtx_t* ctx, cmXScore_t* p, cmXsMidiFile_t* mf, const cmChar_t* dir, const cmChar_t* fn )
  3079. {
  3080. cmXsRC_t rc = kOkXsRC;
  3081. cmSvgH_t svgH = cmSvgNullHandle;
  3082. cmXsSvgEvt_t* e = mf->elist;
  3083. unsigned noteHeight = 10;
  3084. cmChar_t* fn0 = cmMemAllocStr( fn );
  3085. const cmChar_t* svgFn = cmFsMakeFn(dir,fn0 = cmTextAppendSS(fn0,"_midi_svg"),"html",NULL);
  3086. const cmChar_t* cssFn = cmFsMakeFn(NULL,"score_midi_svg","css",NULL);
  3087. cmChar_t* t0 = NULL; // temporary dynamic string
  3088. unsigned i = 0;
  3089. const cmXsSvgEvt_t* e0 = NULL;
  3090. cmMemFree(fn0);
  3091. // create the SVG writer
  3092. if( cmSvgWriterAlloc(ctx,&svgH) != kOkSvgRC )
  3093. {
  3094. rc = cmErrMsg(&p->err,kSvgFailXsRC,"Unable to create the MIDI SVG output file '%s'.",cmStringNullGuard(svgFn));
  3095. goto errLabel;
  3096. }
  3097. _cmXsWriteMidiSvgLegend( svgH, 0, "Velocity", "cplx_vel" );
  3098. _cmXsWriteMidiSvgLegend( svgH, 1, "Duration", "cplx_rym" );
  3099. _cmXsWriteMidiSvgLegend( svgH, 2, "Left Pitch", "cplx_lpch" );
  3100. _cmXsWriteMidiSvgLegend( svgH, 3, "Right Pitch", "cplx_rpch" );
  3101. _cmXsWriteMidiSvgLegend( svgH, 4, "Density", "cplx_density" );
  3102. // for each MIDI file element
  3103. for(; e!=NULL && rc==kOkXsRC; e=e->link)
  3104. {
  3105. switch( e->flags & (kOnsetXsFl|kBarXsFl|kDampDnXsFl|kDampUpDnXsFl|kSostDnXsFl))
  3106. {
  3107. // if this is a note
  3108. case kOnsetXsFl:
  3109. {
  3110. //const cmChar_t* classLabel = "note";
  3111. t0 = cmTsPrintfP(t0,"note_%i%s",e->voice, cmIsFlag(e->flags,kGraceXsFl) ? "_g":"");
  3112. //if( cmIsFlag(e->flags,kGraceXsFl) )
  3113. // classLabel = "grace";
  3114. if( cmSvgWriterRect(svgH, e->tick, e->d0 * noteHeight, e->durTicks, noteHeight-1, t0 ) != kOkSvgRC )
  3115. rc = kSvgFailXsRC;
  3116. else
  3117. {
  3118. t0 = cmTsPrintfP(t0,"%s",cmMidiToSciPitch( e->d0, NULL, 0));
  3119. if( cmSvgWriterText(svgH, e->tick + e->durTicks/2, e->d0 * noteHeight + noteHeight/2, t0, "pitch") != kOkSvgRC )
  3120. rc = kSvgFailXsRC;
  3121. else
  3122. {
  3123. if( e0 != NULL )
  3124. {
  3125. bool fl = (i % 10) == 0;
  3126. _cmXsWriteMidiSvgLinePoint(svgH, e0->tick, e0->cplx.sum_d_vel, e->tick, e->cplx.sum_d_vel, p->cplx_max.sum_d_vel, "cplx_vel",fl?"V":NULL);
  3127. _cmXsWriteMidiSvgLinePoint(svgH, e0->tick, e0->cplx.sum_d_rym, e->tick, e->cplx.sum_d_rym, p->cplx_max.sum_d_rym, "cplx_rym",fl?"D":NULL);
  3128. _cmXsWriteMidiSvgLinePoint(svgH, e0->tick, e0->cplx.sum_d_lpch, e->tick, e->cplx.sum_d_lpch, p->cplx_max.sum_d_lpch, "cplx_lpch",fl?"L":NULL);
  3129. _cmXsWriteMidiSvgLinePoint(svgH, e0->tick, e0->cplx.sum_d_rpch, e->tick, e->cplx.sum_d_rpch, p->cplx_max.sum_d_rpch, "cplx_rpch",fl?"R":NULL);
  3130. _cmXsWriteMidiSvgLinePoint(svgH, e0->tick, e0->cplx.sum_n_lpch + e0->cplx.sum_n_rpch, e->tick, e->cplx.sum_n_lpch + e->cplx.sum_n_rpch, p->cplx_max.sum_n_lpch + p->cplx_max.sum_n_rpch, "cplx_density",fl?"N":NULL);
  3131. }
  3132. e0 = e;
  3133. }
  3134. }
  3135. i+=1;
  3136. }
  3137. break;
  3138. // if this is a bar
  3139. case kBarXsFl:
  3140. {
  3141. if( cmSvgWriterLine(svgH, e->tick, 0, e->tick, 127*noteHeight, "bar") != kOkSvgRC )
  3142. rc = kSvgFailXsRC;
  3143. else
  3144. {
  3145. if( cmSvgWriterText(svgH, e->tick, 10, t0 = cmTsPrintfP(t0,"%i",e->d0), "text" ) != kOkSvgRC )
  3146. rc = kSvgFailXsRC;
  3147. }
  3148. }
  3149. break;
  3150. // if this is a pedal event
  3151. case kDampDnXsFl:
  3152. case kDampUpDnXsFl:
  3153. case kSostDnXsFl:
  3154. {
  3155. const cmChar_t* classLabel = cmIsFlag(e->flags,kSostDnXsFl) ? "sost" : "damp";
  3156. unsigned y = (128 + cmIsFlag(e->flags,kSostDnXsFl)?1:0) * noteHeight;
  3157. cmSvgWriterRect(svgH, e->tick, y, e->durTicks, noteHeight-1, classLabel);
  3158. }
  3159. break;
  3160. }
  3161. }
  3162. if( rc != kOkXsRC )
  3163. cmErrMsg(&p->err,kSvgFailXsRC,"SVG element insert failed.");
  3164. if( rc == kOkXsRC )
  3165. if( cmSvgWriterWrite(svgH,cssFn,svgFn) != kOkSvgRC )
  3166. rc = cmErrMsg(&p->err,kSvgFailXsRC,"SVG file write to '%s' failed.",cmStringNullGuard(svgFn));
  3167. errLabel:
  3168. cmSvgWriterFree(&svgH);
  3169. cmFsFreeFn(svgFn);
  3170. cmFsFreeFn(cssFn);
  3171. cmMemFree(t0);
  3172. return rc;
  3173. }
  3174. void _cmXsPushSvgEvent( cmXScore_t* p, cmXsMidiFile_t* mf, unsigned flags, unsigned tick, unsigned durTick, unsigned voice, unsigned d0, unsigned d1, const cmXsComplexity_t* cplx )
  3175. {
  3176. cmXsSvgEvt_t* e = cmLhAllocZ(p->lhH,cmXsSvgEvt_t,1);
  3177. e->flags = flags;
  3178. e->tick = tick;
  3179. e->durTicks = durTick;
  3180. e->voice = voice;
  3181. e->d0 = d0; // note=pitch bar=number pedal=ctl# metronome=BPM
  3182. e->d1 = d1;
  3183. if( cplx != NULL )
  3184. e->cplx = *cplx;
  3185. if( mf->eol != NULL )
  3186. mf->eol->link = e;
  3187. else
  3188. mf->elist = e;
  3189. // track the min/max pitch
  3190. if( cmIsFlag(flags,kOnsetXsFl) )
  3191. {
  3192. mf->pitch_min = mf->eol==NULL ? d0 : cmMin(mf->pitch_min,d0);
  3193. mf->pitch_max = mf->eol==NULL ? d0 : cmMin(mf->pitch_max,d0);
  3194. }
  3195. mf->eol = e;
  3196. }
  3197. cmXsRC_t _cmXScoreGenSvg( cmCtx_t* ctx, cmXsH_t h, int beginMeasNumb, const cmChar_t* dir, const cmChar_t* fn )
  3198. {
  3199. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  3200. cmXsPart_t* pp = p->partL;
  3201. cmXsMidiFile_t mf;
  3202. memset(&mf,0,sizeof(mf));
  3203. for(; pp!=NULL; pp=pp->link)
  3204. {
  3205. const cmXsMeas_t* meas = pp->measL;
  3206. for(; meas!=NULL; meas=meas->link)
  3207. {
  3208. if( meas->number < beginMeasNumb )
  3209. continue;
  3210. const cmXsNote_t* note = meas->noteL;
  3211. for(; note!=NULL; note=note->slink)
  3212. {
  3213. // if this is a metronome marker
  3214. if( cmIsFlag(note->flags,kMetronomeXsFl) )
  3215. {
  3216. // set BPM as d0
  3217. _cmXsPushSvgEvent(p,&mf,note->flags,note->tick,0,0,note->duration,0,NULL);
  3218. continue;
  3219. }
  3220. // if this is a sounding note
  3221. if( cmIsFlag(note->flags,kOnsetXsFl) )
  3222. {
  3223. unsigned d0 = cmSciPitchToMidiPitch( note->step, note->alter, note->octave );
  3224. unsigned durTick = note->tied_dur;
  3225. if( note->tied != NULL )
  3226. {
  3227. cmXsNote_t* tn = note->tied;
  3228. for(; tn!=NULL; tn=tn->tied)
  3229. durTick += tn->tied_dur;
  3230. }
  3231. _cmXsPushSvgEvent(p,&mf,note->flags,note->tick,durTick,note->voice->id,d0,note->vel,&note->cplx);
  3232. continue;
  3233. }
  3234. // if this is a bar event
  3235. if( cmIsFlag(note->flags,kBarXsFl) )
  3236. {
  3237. _cmXsPushSvgEvent(p,&mf,note->flags,note->tick,0,0,note->meas->number,0,NULL);
  3238. continue;
  3239. }
  3240. // if this is a pedal event
  3241. if( cmIsFlag(note->flags,kDampDnXsFl|kDampUpDnXsFl|kSostDnXsFl) )
  3242. {
  3243. unsigned d0 = cmIsFlag(note->flags,kSostDnXsFl) ? kSostenutoCtlMdId : kSustainCtlMdId;
  3244. _cmXsPushSvgEvent(p,&mf,note->flags,note->tick,note->duration,0,d0,127,NULL);
  3245. continue;
  3246. }
  3247. }
  3248. }
  3249. }
  3250. return _cmXsWriteMidiSvg( ctx, p, &mf, dir, fn );
  3251. }
  3252. cmXsRC_t cmXScoreTest(
  3253. cmCtx_t* ctx,
  3254. const cmChar_t* xmlFn,
  3255. const cmChar_t* editFn,
  3256. const cmChar_t* csvOutFn,
  3257. const cmChar_t* midiOutFn,
  3258. bool reportFl,
  3259. int beginMeasNumb,
  3260. int beginBPM )
  3261. {
  3262. cmXsRC_t rc;
  3263. cmXsH_t h = cmXsNullHandle;
  3264. if( editFn!=NULL && !cmFsIsFile(editFn) )
  3265. {
  3266. cmRptPrintf(&ctx->rpt,"The edit file %s does not exist. A new edit file will be created.",editFn);
  3267. cmXScoreGenEditFile(ctx,xmlFn,editFn);
  3268. editFn = NULL;
  3269. }
  3270. // Parse the XML file and apply the changes in editFn.
  3271. if((rc = cmXScoreInitialize( ctx, &h, xmlFn,editFn)) != kOkXsRC )
  3272. return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
  3273. if( csvOutFn != NULL )
  3274. {
  3275. cmXScoreWriteCsv(h,beginMeasNumb,csvOutFn);
  3276. _cmXsIsCsvValid(ctx,h,csvOutFn);
  3277. }
  3278. if( midiOutFn != NULL )
  3279. {
  3280. // measure the score complexity
  3281. double wndSecs = 1.0;
  3282. _cmXsMeasComplexity(h,wndSecs);
  3283. cmFileSysPathPart_t* pp = cmFsPathParts(midiOutFn);
  3284. _cmXsWriteMidiFile(ctx, h, beginMeasNumb, beginBPM, pp->dirStr, pp->fnStr );
  3285. _cmXsIsMidiFileValid(ctx, h, pp->dirStr, pp->fnStr );
  3286. _cmXScoreGenSvg( ctx, h, beginMeasNumb, pp->dirStr, pp->fnStr );
  3287. cmFsFreePathParts(pp);
  3288. }
  3289. if(reportFl)
  3290. cmXScoreReport(h,&ctx->rpt,true);
  3291. return cmXScoreFinalize(&h);
  3292. }