libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmScore.c 72KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmLinkedHeap.h"
  10. #include "cmTime.h"
  11. #include "cmMidi.h"
  12. #include "cmLex.h"
  13. #include "cmCsv.h"
  14. #include "cmSymTbl.h"
  15. #include "cmMidiFile.h"
  16. #include "cmAudioFile.h"
  17. #include "cmTimeLine.h"
  18. #include "cmText.h"
  19. #include "cmFile.h"
  20. #include "cmScore.h"
  21. #include "cmVectOpsTemplateMain.h"
  22. cmScH_t cmScNullHandle = cmSTATIC_NULL_HANDLE;
  23. enum
  24. {
  25. kLabelCharCnt = 7,
  26. kInvalidDynScId = 0,
  27. };
  28. enum
  29. {
  30. kMidiFileIdColScIdx= 0,
  31. kEventIdColScIdx = 2,
  32. kTypeLabelColScIdx = 3,
  33. kDSecsColScIdx = 4,
  34. kSecsColScIdx = 5,
  35. kD0ColScIdx = 9,
  36. kD1ColScIdx = 10,
  37. kPitchColScIdx = 11,
  38. kBarColScIdx = 13,
  39. kSkipColScIdx = 14,
  40. kEvenColScIdx = 15,
  41. kGraceColScIdx = 16,
  42. kTempoColScIdx = 17,
  43. kFracColScIdx = 18,
  44. kDynColScIdx = 19,
  45. kSectionColScIdx = 20,
  46. kRecdPlayColScIdx = 21,
  47. kRemarkColScIdx = 22
  48. };
  49. typedef struct
  50. {
  51. unsigned id;
  52. unsigned flag;
  53. cmChar_t label[ kLabelCharCnt + 1 ];
  54. } cmScEvtRef_t;
  55. typedef struct cmScSect_str
  56. {
  57. const cmChar_t* label;
  58. unsigned startIdx;
  59. struct cmScSect_str* link;
  60. } cmScSect_t;
  61. typedef struct cmScSetEle_str
  62. {
  63. cmChar_t* label;
  64. unsigned eleIdx;
  65. struct cmScSetEle_str* link;
  66. } cmScSetEle_t;
  67. typedef struct cmScSet_str
  68. {
  69. unsigned typeFl; // type of this set
  70. cmScSetEle_t* eles; // indexes of set elements
  71. cmScSetEle_t* sects; // application section labels
  72. bool inFl; // true if currently accepting elements
  73. struct cmScSet_str* link; //
  74. } cmScSet_t;
  75. typedef struct cmScMark_str
  76. {
  77. cmMarkScMId_t cmdId;
  78. unsigned labelSymId;
  79. unsigned scoreIdx;
  80. unsigned csvRowIdx;
  81. struct cmScMark_str* link;
  82. } cmScMark_t;
  83. typedef struct
  84. {
  85. cmErr_t err;
  86. cmCsvH_t cH;
  87. cmSymTblH_t stH;
  88. cmScCb_t cbFunc;
  89. void* cbArg;
  90. cmChar_t* fn;
  91. double srate;
  92. cmScoreEvt_t* array;
  93. unsigned cnt;
  94. cmScoreLoc_t* loc;
  95. unsigned locCnt;
  96. cmScoreSection_t* sect;
  97. unsigned sectCnt;
  98. unsigned* markLabelArray; // one symId per unique cmScoreMarker_t.labelSymId;
  99. unsigned markLabelCnt;
  100. unsigned sciPitchLexTId; // sci pitch and section id lexer token id's
  101. unsigned sectionLexTId;
  102. cmScSect_t* sectList; // lists used during parsing
  103. cmScSet_t* setList;
  104. cmScMark_t* markList;
  105. cmScoreSet_t* sets;
  106. unsigned setCnt;
  107. unsigned* dynRefArray;
  108. unsigned dynRefCnt;
  109. unsigned nxtLocIdx;
  110. unsigned minSetLocIdx;
  111. unsigned maxSetLocIdx;
  112. } cmSc_t;
  113. cmScEvtRef_t _cmScEvtRefArray[] =
  114. {
  115. { kTimeSigEvtScId, kTimeSigMdId, "tsg" },
  116. { kKeySigEvtScId, kKeySigMdId, "ksg" },
  117. { kTempoEvtScId, kTempoMdId, "tmp" },
  118. { kTrackEvtScId, kTrkNameMdId, "trk" },
  119. { kTextEvtScId, kTextMdId, "txt" },
  120. { kNameEvtScId, kInstrNameMdId,"nam" },
  121. { kEOTrackEvtScId, kEndOfTrkMdId, "eot" },
  122. { kCopyEvtScId, kCopyMdId, "cpy" },
  123. { kBlankEvtScId, 0, "blk" },
  124. { kBarEvtScId, 0, "bar" },
  125. { kPgmEvtScId, kPgmMdId, "pgm" },
  126. { kCtlEvtScId, kCtlMdId, "ctl" },
  127. { kNonEvtScId, kNoteOnMdId, "non" },
  128. { kPedalEvtScId, 0, "ped" },
  129. { kInvalidEvtScId, 0, "***" }
  130. };
  131. cmScEvtRef_t _cmScDynRefArray[] =
  132. {
  133. { 1, 0, "pppp"},
  134. { 2, 0, "ppp" },
  135. { 3, 0, "pp" },
  136. { 4, 0, "p" },
  137. { 5, 0, "mp" },
  138. { 6, 0, "mf" },
  139. { 7, 0, "f" },
  140. { 8, 0, "ff" },
  141. { 9, 0, "fff" },
  142. { kInvalidDynScId,0, "***" },
  143. };
  144. cmScEvtRef_t _cmScVarRefArray[] =
  145. {
  146. { kEvenVarScId, kEvenScFl, "e"},
  147. { kDynVarScId, kDynScFl, "d"},
  148. { kTempoVarScId,kTempoScFl,"t"},
  149. { cmInvalidId, 0, "@"}
  150. };
  151. cmSc_t* _cmScHandleToPtr( cmScH_t h )
  152. {
  153. cmSc_t* p = (cmSc_t*)h.h;
  154. assert( p != NULL );
  155. return p;
  156. }
  157. unsigned _cmScEvtTypeLabelToId( const cmChar_t* label )
  158. {
  159. cmScEvtRef_t* r = _cmScEvtRefArray;
  160. for(; r->id != kInvalidEvtScId; ++r )
  161. if( strcmp(label,r->label) == 0 )
  162. return r->id;
  163. return kInvalidEvtScId;
  164. }
  165. const cmChar_t* cmScEvtTypeIdToLabel( unsigned id )
  166. {
  167. cmScEvtRef_t* r = _cmScEvtRefArray;
  168. for(; r->id != kInvalidEvtScId; ++r )
  169. if( r->id == id )
  170. return r->label;
  171. return NULL;
  172. }
  173. const cmChar_t* cmScStatusToOpString( unsigned id )
  174. {
  175. if( id == 0 )
  176. return "<unknown>";
  177. cmScEvtRef_t* r = _cmScEvtRefArray;
  178. for(; r->id != kInvalidEvtScId; ++r )
  179. if( r->flag == id )
  180. return r->label;
  181. return NULL;
  182. }
  183. unsigned _cmScDynLabelToId( const cmChar_t* label )
  184. {
  185. cmScEvtRef_t* r = _cmScDynRefArray;
  186. for(; r->id != kInvalidEvtScId; ++r )
  187. if( strncmp(label,r->label,strlen(r->label)) == 0 )
  188. return r->id;
  189. return kInvalidDynScId;
  190. }
  191. // return the count of dynamic label/id pairs
  192. unsigned _cmScDynLabelCount( )
  193. {
  194. unsigned n = 0;
  195. cmScEvtRef_t* r = _cmScDynRefArray;
  196. for(; r->id != kInvalidEvtScId; ++r )
  197. ++n;
  198. return n;
  199. }
  200. const cmChar_t* cmScDynIdToLabel( unsigned id )
  201. {
  202. cmScEvtRef_t* r = _cmScDynRefArray;
  203. for(; r->id != kInvalidDynScId; ++r )
  204. if( r->id == id )
  205. return r->label;
  206. return NULL;
  207. }
  208. char _cmScVarFlagToChar( unsigned flags )
  209. {
  210. unsigned i;
  211. for(i=0; _cmScVarRefArray[i].id!=cmInvalidId; ++i)
  212. if( _cmScVarRefArray[i].flag == flags )
  213. return _cmScVarRefArray[i].label[0];
  214. assert(0);
  215. return ' ';
  216. }
  217. char _cmScVarIdToChar( unsigned id )
  218. {
  219. unsigned i;
  220. for(i=0; _cmScVarRefArray[i].id!=cmInvalidId; ++i)
  221. if( _cmScVarRefArray[i].id == id )
  222. return _cmScVarRefArray[i].label[0];
  223. assert(0);
  224. return ' ';
  225. }
  226. unsigned _cmScVarFlagToId( unsigned flags )
  227. {
  228. unsigned i;
  229. for(i=0; _cmScVarRefArray[i].id!=cmInvalidId; ++i)
  230. if( _cmScVarRefArray[i].flag == flags )
  231. return _cmScVarRefArray[i].id;
  232. assert(0);
  233. return cmInvalidId;
  234. }
  235. const char* _cmScFlagsToStr( unsigned flags, char* buf, int bufCharCnt )
  236. {
  237. unsigned i=0;
  238. if( cmIsFlag(flags,kEvenScFl) )
  239. {
  240. assert(i<bufCharCnt);
  241. buf[i] = 'e';
  242. ++i;
  243. }
  244. if( cmIsFlag(flags,kDynScFl) )
  245. {
  246. assert(i<bufCharCnt);
  247. buf[i] = 'd';
  248. ++i;
  249. }
  250. if( cmIsFlag(flags,kTempoScFl ))
  251. {
  252. assert(i<bufCharCnt);
  253. buf[i] = 't';
  254. ++i;
  255. }
  256. assert(i<bufCharCnt);
  257. buf[i] = 0;
  258. return buf;
  259. }
  260. unsigned _cmScLexSciPitchMatcher( const cmChar_t* cp, unsigned cn )
  261. {
  262. if( cp==NULL || cn < 2 )
  263. return 0;
  264. // first char must be "A-G"
  265. if( strspn(cp,"ABCDEFG") != 1 )
  266. return 0;
  267. unsigned i = 1;
  268. // next char could be accidental
  269. if( cp[i] == '#' || cp[i] == 'b' )
  270. ++i; // i==2
  271. // the 2nd or 3rd char must be a digit
  272. if( i>=cn || isdigit(cp[i]) == false )
  273. return 0;
  274. ++i; // i==2 or i==3
  275. // the 3rd or 4th char must be a digit or EOS
  276. if( i>=cn || isdigit(cp[i]) == false )
  277. return i;
  278. ++i;
  279. return i;
  280. }
  281. unsigned _cmScLexSectionIdMatcher( const cmChar_t* cp, unsigned cn )
  282. {
  283. if( cp==NULL || cn < 2 )
  284. return 0;
  285. // first char must be a number
  286. if( !isdigit(cp[0]) )
  287. return 0;
  288. // if 2nd char is a char then terminate
  289. if( 'a'<=cp[1] && cp[1]<='z' )
  290. return 2;
  291. // if 2nd char is digit and 3rd char is char then terminate
  292. if( isdigit(cp[1]) && cn>2 && 'a'<=cp[2] && cp[2]<='z' )
  293. return 3;
  294. return 0;
  295. }
  296. void _cmScFreeMarkList( cmScMark_t* markList )
  297. {
  298. cmScMark_t* mp = markList;
  299. while( mp!=NULL )
  300. {
  301. cmScMark_t* np = mp->link;
  302. cmMemFree(mp);
  303. mp = np;
  304. }
  305. }
  306. void _cmScFreeSetList( cmScSet_t* setList )
  307. {
  308. cmScSet_t* tp = setList;
  309. cmScSet_t* ntp = NULL;
  310. while(tp!=NULL)
  311. {
  312. ntp = tp->link;
  313. cmScSetEle_t* ep = tp->eles;
  314. while( ep != NULL )
  315. {
  316. cmScSetEle_t* nep = ep->link;
  317. cmMemFree(ep);
  318. ep = nep;
  319. }
  320. ep = tp->sects;
  321. while( ep != NULL )
  322. {
  323. cmScSetEle_t* nep = ep->link;
  324. cmMemFree(ep->label);
  325. cmMemFree(ep);
  326. ep = nep;
  327. }
  328. cmMemFree(tp);
  329. tp = ntp;
  330. }
  331. }
  332. cmScRC_t _cmScFinalize( cmSc_t* p )
  333. {
  334. cmScRC_t rc = kOkScRC;
  335. unsigned i;
  336. if( cmCsvFinalize(&p->cH) != kOkCsvRC )
  337. return rc;
  338. if( p->sets != NULL )
  339. {
  340. for(i=0; i<p->setCnt; ++i)
  341. {
  342. cmMemFree(p->sets[i].eleArray);
  343. cmMemFree(p->sets[i].sectArray);
  344. cmMemFree(p->sets[i].symArray);
  345. cmMemFree(p->sets[i].costSymArray);
  346. }
  347. cmMemFree(p->sets);
  348. }
  349. _cmScFreeSetList(p->setList);
  350. _cmScFreeMarkList(p->markList);
  351. if( p->loc != NULL )
  352. {
  353. for(i=0; i<p->locCnt; ++i)
  354. {
  355. cmMemFree(p->loc[i].evtArray);
  356. if( p->loc[i].begSectPtr != NULL )
  357. cmMemFree(p->loc[i].begSectPtr->setArray);
  358. // free the marker list assoc'd with this location
  359. cmScoreMarker_t* smp = p->loc[i].markList;
  360. while( smp!=NULL )
  361. {
  362. cmScoreMarker_t* np = smp->link;
  363. cmMemFree(smp);
  364. smp = np;
  365. }
  366. }
  367. cmMemFree(p->loc);
  368. }
  369. cmMemPtrFree(&p->dynRefArray);
  370. cmMemFree(p->markLabelArray);
  371. cmMemFree(p->sect);
  372. cmMemFree(p->fn);
  373. cmMemFree(p->array);
  374. cmMemFree(p);
  375. return rc;
  376. }
  377. cmScRC_t _cmScParseBar( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, int* barNumb )
  378. {
  379. if((*barNumb = cmCsvCellInt(p->cH,rowIdx,kBarColScIdx)) == INT_MAX )
  380. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unable to parse the bar number.");
  381. s->type = kBarEvtScId;
  382. s->secs = 0;
  383. s->barNumb = *barNumb;
  384. s->csvRowNumb = rowIdx + 1;
  385. return kOkScRC;
  386. }
  387. cmScSet_t* _cmScNewSet( cmSc_t* p, unsigned typeFl )
  388. {
  389. // create a new set record
  390. cmScSet_t* nsp = cmMemAllocZ(cmScSet_t,1);
  391. nsp->inFl = true;
  392. nsp->typeFl = typeFl;
  393. if( p->setList == NULL )
  394. p->setList = nsp;
  395. else
  396. {
  397. // go to the end of the the set list
  398. cmScSet_t* sp = p->setList;
  399. assert(sp!=NULL);
  400. while( sp->link != NULL )
  401. sp = sp->link;
  402. sp->link = nsp;
  403. }
  404. return nsp;
  405. }
  406. cmScSet_t* _cmScFindSet( cmSc_t* p, unsigned typeFl )
  407. {
  408. // locate the set currently accepting ele's for this type
  409. cmScSet_t* sp = p->setList;
  410. for(; sp != NULL; sp=sp->link )
  411. if( sp->typeFl == typeFl && sp->inFl )
  412. break;
  413. return sp;
  414. }
  415. void _cmScSetDone(cmSc_t* p, unsigned typeFl)
  416. {
  417. cmScSet_t* sp = _cmScFindSet(p,typeFl);
  418. assert( sp != NULL );
  419. sp->inFl = false;
  420. }
  421. // This function is called during parsing to
  422. // insert a set element or set section into a cmScSet_t
  423. // element or section linked list. Either the scoreIdx
  424. // or the label is valid but not both.
  425. cmScSet_t* _cmScInsertSetEle(cmSc_t* p, unsigned scoreIdx, unsigned typeFl, const cmChar_t* label, unsigned labelCharCnt)
  426. {
  427. assert( scoreIdx!=cmInvalidId || (scoreIdx==cmInvalidIdx && label!=NULL && labelCharCnt>0));
  428. cmScSet_t* sp = _cmScFindSet(p,typeFl);
  429. if( sp == NULL )
  430. sp = _cmScNewSet(p,typeFl);
  431. // allocate a new set element record
  432. cmScSetEle_t* nep = cmMemAllocZ(cmScSetEle_t,1);
  433. cmScSetEle_t** list = NULL;
  434. nep->eleIdx = scoreIdx;
  435. if( label == NULL )
  436. {
  437. // all elements must be of the same type
  438. assert( sp->typeFl == typeFl );
  439. sp->typeFl = typeFl;
  440. list = &sp->eles;
  441. }
  442. else
  443. {
  444. nep->label = cmMemAllocStrN(label,labelCharCnt);
  445. list = &sp->sects;
  446. }
  447. // *list refers to sp->sects or sp->ele's depending on the type of ele
  448. if( *list == NULL )
  449. *list = nep;
  450. else
  451. {
  452. // got to the last element in the set
  453. cmScSetEle_t* ep = *list;
  454. while( ep->link != NULL )
  455. ep = ep->link;
  456. // append the new element to the end of the list
  457. ep->link = nep;
  458. }
  459. return sp;
  460. }
  461. // Extract the next attribute section identifier.
  462. const cmChar_t* _cmScParseOneSetSection( cmSc_t* p, unsigned typeFl, const cmChar_t* c0p )
  463. {
  464. // advance white space
  465. while( *c0p && (isspace(*c0p) || *c0p==',') )
  466. ++c0p;
  467. if( *c0p==0 )
  468. return c0p;
  469. // c0p now points to a section id or an asterisk
  470. const cmChar_t* c1p = c0p;
  471. // advance past section id
  472. while( *c1p && (!isspace(*c1p) && (isdigit(*c1p) || isalpha(*c1p))))
  473. ++c1p;
  474. // if c0p pointed to an asterisk then c1p is still equal to c0p
  475. if( c1p > c0p )
  476. _cmScInsertSetEle(p,cmInvalidIdx,typeFl,c0p,c1p-c0p);
  477. return c1p;
  478. }
  479. // Parse an attribute string to extract the section
  480. // identifiers which may follow the attribute token (e,t,mf,ff,...)
  481. cmScRC_t _cmScParseAttr(cmSc_t* p, unsigned scoreIdx, const cmChar_t* text, unsigned typeFl)
  482. {
  483. const cmChar_t* cp = text;
  484. // insert a set element - all attribute's produce one element record
  485. _cmScInsertSetEle(p,scoreIdx,typeFl,NULL,0);
  486. // advance past the attribute type marking (e,t,(p,mf,f,fff,etc)) in search
  487. // of section identifiers
  488. while( *cp && !isspace(*cp) )
  489. ++cp;
  490. if( *cp )
  491. {
  492. // search for the first section identifier
  493. if((cp =_cmScParseOneSetSection(p,typeFl,cp)) != NULL )
  494. {
  495. bool asteriskFl = false;
  496. // search for the second section identifier
  497. if((cp = _cmScParseOneSetSection(p,typeFl,cp)) != NULL && *cp!=0 )
  498. asteriskFl = *cp == '*';
  499. _cmScSetDone(p,typeFl);
  500. // if the attr just parsed ended with an asterisk then it is both
  501. // the last element of the previous set and the first ele of the
  502. // next set
  503. if( asteriskFl )
  504. {
  505. // if the attr just parsed had a section id then it was the last
  506. // element in the set - create a new set record to hold the next set
  507. _cmScNewSet(p,typeFl);
  508. _cmScInsertSetEle(p,scoreIdx,typeFl,NULL,0);
  509. }
  510. }
  511. }
  512. return kOkScRC;
  513. }
  514. // Parse a record/playback string
  515. cmScRC_t _cmScParseMarkers( cmSc_t* p, unsigned scoreIdx, const cmChar_t* text, unsigned rowIdx )
  516. {
  517. const cmChar_t* cp = text;
  518. const cmChar_t* ip;
  519. const cmChar_t* ep;
  520. // if no symbol table has been registered then don't bother storing markers.
  521. // (NOTE - THIS IS A HACK BECAUSE THE SCORE OBJECT USED IN THE cmdIf DOES NOT HAVE
  522. // A SYMBOL TABLE - WE COULD EASILY ADD ONE IF IT EVENTUALLY NEEDS ACCESS TO THE MARKERS
  523. // - OR A SYMBOL TABLE COULD BE ADDED TO THE SCORE ITSELF.)
  524. if( cmSymTblIsValid(p->stH) == false )
  525. return kOkScRC;
  526. for(;(cp = cmTextNextNonWhiteC(cp)) != NULL; cp=ep )
  527. {
  528. // go to command/id space
  529. if((ip = cmTextNextWhiteOrEosC(cp)) == NULL )
  530. goto errLabel;
  531. // goto label
  532. if((ip = cmTextNextNonWhiteC(ip)) == NULL )
  533. goto errLabel;
  534. // goto end of label
  535. if((ep = cmTextNextWhiteOrEosC(ip)) == NULL )
  536. goto errLabel;
  537. else
  538. {
  539. unsigned n = (ep-ip)+1;
  540. cmChar_t markTextStr[n+1];
  541. strncpy(markTextStr,ip,n);
  542. markTextStr[n] = 0;
  543. // remove any trailing white space
  544. cmTextTrimEnd(markTextStr);
  545. cmMarkScMId_t cmdId = kInvalidScMId;
  546. switch( *cp )
  547. {
  548. case 'c': cmdId = kRecdBegScMId; break;
  549. case 'e': cmdId = kRecdEndScMId; break;
  550. case 'p': cmdId = kPlayBegScMId; break;
  551. case 'd': cmdId = kPlayEndScMId; break;
  552. case 'f': cmdId = kFadeScMId; break;
  553. default:
  554. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unrecognized marker command character '%c' at row index %i.",*cp,rowIdx);
  555. }
  556. cmScMark_t* mp = cmMemAllocZ(cmScMark_t,1);
  557. mp->cmdId = cmdId;
  558. mp->labelSymId = cmSymTblRegisterSymbol(p->stH,markTextStr);
  559. mp->scoreIdx = scoreIdx;
  560. mp->csvRowIdx = rowIdx;
  561. //printf("%i %c '%s'\n",rowIdx,*cp,markTextStr);
  562. // insert the new mark at the end of the list
  563. if( p->markList == NULL )
  564. p->markList = mp;
  565. else
  566. {
  567. cmScMark_t* ep = p->markList;
  568. while( ep->link != NULL )
  569. ep = ep->link;
  570. ep->link = mp;
  571. }
  572. }
  573. }
  574. return kOkScRC;
  575. errLabel:
  576. return cmErrMsg(&p->err,kSyntaxErrScRC,"Invalid record/playback field ('%s') on row index:%i.",cmStringNullGuard(text),rowIdx);
  577. }
  578. void _cmScPrintSets( const cmChar_t* label, cmScSet_t* setList )
  579. {
  580. printf("%s\n",label);
  581. const cmScSet_t* sp = setList;
  582. for(; sp != NULL; sp=sp->link )
  583. {
  584. const cmScSetEle_t* ep = sp->eles;
  585. for(; ep!=NULL; ep=ep->link)
  586. printf("%i ",ep->eleIdx);
  587. printf(" : ");
  588. for(ep=sp->sects; ep!=NULL; ep=ep->link)
  589. printf("%s ",cmStringNullGuard(ep->label));
  590. printf("\n");
  591. }
  592. }
  593. cmScRC_t _cmScParseNoteOn( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, unsigned scoreIdx, int barNumb, unsigned barNoteIdx )
  594. {
  595. cmScRC_t rc = kOkScRC;
  596. unsigned flags = 0;
  597. unsigned dynVal = kInvalidDynScId;
  598. const cmChar_t* sciPitch;
  599. cmMidiByte_t midiPitch;
  600. cmMidiByte_t midiVel;
  601. const cmChar_t* attr;
  602. double secs;
  603. double durSecs;
  604. unsigned eventId;
  605. const cmCsvCell_t* cell;
  606. s += scoreIdx;
  607. eventId = cmCsvCellUInt(p->cH,rowIdx,kEventIdColScIdx);
  608. // verify the scientific pitch cell was formatted correcly
  609. if((cell = cmCsvCellPtr(p->cH,rowIdx,kPitchColScIdx)) == NULL || cell->lexTId != p->sciPitchLexTId )
  610. return cmErrMsg(&p->err,kSyntaxErrScRC,"Pitch column format error.");
  611. if((sciPitch = cmCsvCellText(p->cH,rowIdx,kPitchColScIdx)) == NULL )
  612. return cmErrMsg(&p->err,kSyntaxErrScRC,"Expected a scientific pitch value");
  613. if((midiPitch = cmSciPitchToMidi(sciPitch)) == kInvalidMidiPitch)
  614. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unable to convert the scientific pitch '%s' to a MIDI value. ");
  615. if((midiVel = cmCsvCellUInt( p->cH,rowIdx,kD1ColScIdx)) >= kInvalidMidiVelocity )
  616. {
  617. //return cmErrMsg(&p->err,kSyntaxErrScRC,"An invalid MIDI velocity (%i) was encountered.",midiVel);
  618. midiVel = 64;
  619. cmErrWarnMsg(&p->err,kSyntaxErrScRC,"An invalilid MIDI velocity (%i) was encountered.",midiVel);
  620. }
  621. // get the sec's field - or DBL_MAX if it is not set
  622. if((secs = cmCsvCellDouble(p->cH, rowIdx, kSecsColScIdx )) == DBL_MAX) // Returns DBL_MAX on error.
  623. flags += kInvalidScFl;
  624. // skip attribute
  625. if((attr = cmCsvCellText(p->cH,rowIdx,kSkipColScIdx)) != NULL && *attr == 's' )
  626. flags += kSkipScFl;
  627. // evenness attribute
  628. if((attr = cmCsvCellText(p->cH,rowIdx,kEvenColScIdx)) != NULL && *attr == 'e' )
  629. {
  630. flags += kEvenScFl;
  631. _cmScParseAttr(p,scoreIdx,attr,kEvenScFl);
  632. }
  633. // grace attribute
  634. if((attr = cmCsvCellText(p->cH,rowIdx,kGraceColScIdx)) != NULL && *attr == 'g' )
  635. {
  636. flags += kGraceScFl;
  637. //if( cmIsNotFlag(flags,kEvenScFl) )
  638. // return cmErrMsg(&p->err,kSyntaxErrScRC,"All 'grace' notes should also be 'even' notes.");
  639. }
  640. // tempo attribute
  641. if((attr = cmCsvCellText(p->cH,rowIdx,kTempoColScIdx)) != NULL && *attr == 't' )
  642. {
  643. flags += kTempoScFl;
  644. _cmScParseAttr(p,scoreIdx,attr,kTempoScFl);
  645. }
  646. // dynamics attribute
  647. if((attr = cmCsvCellText(p->cH,rowIdx,kDynColScIdx)) != NULL )
  648. {
  649. if((dynVal = _cmScDynLabelToId(attr)) == kInvalidDynScId )
  650. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unknown dynamic label '%s'.",cmStringNullGuard(attr));
  651. flags += kDynScFl;
  652. _cmScParseAttr(p,scoreIdx,attr,kDynScFl);
  653. }
  654. // tempo/non-grace rythmic value
  655. if( cmIsFlag(flags,kTempoScFl) || (cmIsFlag(flags,kEvenScFl) && cmIsNotFlag(flags,kGraceScFl)) )
  656. {
  657. double frac = cmCsvCellDouble(p->cH,rowIdx,kFracColScIdx);
  658. // no 'frac' value is given for the last note of the set we must accept error
  659. // values here and validate the actual values later
  660. if( frac>0 && frac!=DBL_MAX )
  661. s->frac = frac;
  662. }
  663. // Returns DBL_MAX on error.
  664. if((durSecs = cmCsvCellDouble(p->cH, rowIdx, kDSecsColScIdx )) == DBL_MAX)
  665. durSecs = 0.25;
  666. // parse the recd/play markers
  667. if((attr = cmCsvCellText(p->cH,rowIdx,kRecdPlayColScIdx)) != NULL )
  668. {
  669. if((rc = _cmScParseMarkers(p,scoreIdx,attr,rowIdx)) != kOkScRC )
  670. return rc;
  671. }
  672. s->type = kNonEvtScId;
  673. s->secs = secs;
  674. s->pitch = midiPitch;
  675. s->vel = midiVel;
  676. s->flags = flags;
  677. s->dynVal = dynVal;
  678. s->barNumb = barNumb;
  679. s->barNoteIdx = barNoteIdx;
  680. s->durSecs = durSecs;
  681. s->csvRowNumb = rowIdx+1;
  682. s->csvEventId = eventId;
  683. return rc;
  684. }
  685. cmScRC_t _cmScParseMidiCtlMsg( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, unsigned scoreIdx, int barNumb, unsigned barNoteIdx, cmScoreEvt_t** pedalV, unsigned pedalN )
  686. {
  687. cmScRC_t rc = kOkScRC;
  688. unsigned flags = 0;
  689. const cmChar_t* attr;
  690. double secs = DBL_MAX;
  691. double durSecs = 0;
  692. const unsigned pedalBaseMidiId = 64;
  693. s += scoreIdx;
  694. // get the sec's field - or DBL_MAX if it is not set
  695. if((secs = cmCsvCellDouble(p->cH, rowIdx, kSecsColScIdx )) == DBL_MAX) // Returns DBL_MAX on error.
  696. flags += kInvalidScFl;
  697. // skip attribute
  698. if((attr = cmCsvCellText(p->cH,rowIdx,kSkipColScIdx)) != NULL && *attr == 's' )
  699. flags += kSkipScFl;
  700. // get MIDI ctl msg data byte 1
  701. unsigned d0 = cmCsvCellUInt( p->cH,rowIdx,kD0ColScIdx);
  702. // get MIDI ctl msg data byte 2
  703. unsigned d1 = cmCsvCellUInt( p->cH,rowIdx,kD1ColScIdx);
  704. // if this is a pedal event
  705. if( pedalBaseMidiId <= d0 && d0 < pedalBaseMidiId + pedalN )
  706. {
  707. bool pedalDnFl = d1 >= 64;
  708. unsigned pedalIdx = d0 - pedalBaseMidiId;
  709. // if this is a pedal-down message ...
  710. if( pedalDnFl )
  711. {
  712. flags += kPedalDnScFl;
  713. if( pedalV[pedalIdx] != NULL )
  714. cmErrWarnMsg(&p->err,kPedalInvalidScRC,"The score contains multiple pedal down messages withouth an intervening pedal up message in or near bar %i.",barNumb );
  715. else
  716. {
  717. // Don't store a pointer to a skipped pedal down msg because it will not
  718. // not exist in p->array[] when the associated 'pedal-up' message is
  719. // encountered. Note the the 'postProcFl' controlled section of
  720. // _cmScParseFile() effectively eliminates cmScoreEvt_t records from
  721. // p->array[] that are marked with kSkipScFl.
  722. if( cmIsFlag(flags,kSkipScFl) )
  723. cmErrWarnMsg(&p->err,kPedalInvalidScRC,"A 'pedal-down' msg is marked to skip in or near bar %i this will probably produce a 'missing pedal-down' warning.",barNumb );
  724. else
  725. pedalV[pedalIdx] = s; // ... store a pointer to the scEvt recd in pedalV[]
  726. }
  727. }
  728. else // ... else this is a pedal-up msg ...
  729. {
  730. flags += kPedalUpScFl;
  731. if( pedalV[pedalIdx] == NULL )
  732. cmErrWarnMsg(&p->err,kPedalInvalidScRC,"The score contains multiple pedal up messages withouth an intervening pedal down message in or near bar %i.",barNumb );
  733. else // ... update the pedal down duration in the pedal-down message assoc'd w/ this pedal-up msg.
  734. {
  735. if( secs == DBL_MAX )
  736. cmErrWarnMsg(&p->err,kPedalInvalidScRC,"A pedal-up message was encountered with an invalid time-stamp in or near bar %i the pedal down duration could therefore not be calculated.",barNumb);
  737. else
  738. {
  739. // update the pedal down event record with the pedal down duration
  740. pedalV[pedalIdx]->durSecs = secs - pedalV[pedalIdx]->secs;
  741. }
  742. pedalV[pedalIdx] = NULL;
  743. }
  744. }
  745. }
  746. s->type = kPedalEvtScId;
  747. s->secs = secs;
  748. s->pitch = d0; // store the pedal type identifer in the pitch field
  749. s->flags = flags;
  750. s->dynVal = 0;
  751. s->barNumb = barNumb;
  752. s->barNoteIdx = barNoteIdx;
  753. s->durSecs = durSecs;
  754. s->csvRowNumb = rowIdx+1;
  755. return rc;
  756. }
  757. cmScRC_t _cmScParseSectionColumn( cmSc_t* p, unsigned rowIdx, unsigned evtIdx, cmScSect_t* sectList )
  758. {
  759. const cmCsvCell_t* cell;
  760. cmScSect_t* sect;
  761. const cmChar_t* label;
  762. // most rows don't have section labels
  763. if( (cell = cmCsvCellPtr( p->cH,rowIdx,kSectionColScIdx)) == NULL
  764. || (label = cmCsvCellText(p->cH,rowIdx,kSectionColScIdx)) == NULL)
  765. return kOkScRC;
  766. // verify the section id type
  767. if( cell->lexTId != p->sectionLexTId && cell->lexTId != kIntLexTId )
  768. return cmErrMsg(&p->err,kSyntaxErrScRC,"'section' column format error.");
  769. sect = cmMemAllocZ(cmScSect_t,1);
  770. sect->label = label;
  771. sect->startIdx = evtIdx;
  772. //printf("section:%s\n",label);
  773. cmScSect_t* sp = sectList;
  774. assert( sp != NULL );
  775. while( sp->link != NULL )
  776. sp = sp->link;
  777. sp->link = sect;
  778. return kOkScRC;
  779. }
  780. cmScoreSection_t* _cmScLabelToSection( cmSc_t* p, const cmChar_t* label )
  781. {
  782. int i;
  783. for(i=0; i<p->sectCnt; ++i)
  784. if( strcmp(p->sect[i].label,label) == 0 )
  785. return p->sect + i;
  786. return NULL;
  787. }
  788. // Calculate the total number of all types of sets and
  789. // then convert each of the cmScSet_t linked list's to
  790. // a single linear cmScoreSet_t list (p->sets[]).
  791. cmScRC_t _cmScProcSets( cmSc_t* p )
  792. {
  793. cmScRC_t rc = kOkScRC;
  794. // calculate the count of all sets
  795. unsigned i;
  796. unsigned n = 0;
  797. cmScSet_t* sp = p->setList;
  798. for(n=0; sp!=NULL; sp=sp->link)
  799. if( sp->eles != NULL )
  800. ++n;
  801. // allocate the linear set array
  802. p->sets = cmMemAllocZ(cmScoreSet_t,n);
  803. p->setCnt = n;
  804. // fill in the linear set array
  805. sp = p->setList;
  806. for(i=0; sp!=NULL; sp=sp->link)
  807. if( sp->eles != NULL )
  808. {
  809. unsigned j;
  810. unsigned en;
  811. unsigned rowNumb = 0;
  812. assert( i<n );
  813. // get the count of elements assoc'd with this set
  814. cmScSetEle_t* ep = sp->eles;
  815. for(en=0; ep!=NULL; ep=ep->link)
  816. ++en;
  817. assert( en > 0 );
  818. // allocate the element array
  819. p->sets[i].eleCnt = en;
  820. p->sets[i].eleArray = cmMemAllocZ(cmScoreEvt_t*,en);
  821. // fill in the element array
  822. ep = sp->eles;
  823. unsigned graceCnt = 0;
  824. for(j=0; ep!=NULL; ep=ep->link,++j)
  825. {
  826. assert(ep->eleIdx != cmInvalidIdx && ep->eleIdx<p->cnt);
  827. p->sets[i].eleArray[j] = p->array + ep->eleIdx;
  828. assert( cmIsFlag( p->sets[i].eleArray[j]->flags, sp->typeFl) );
  829. rowNumb = p->array[ep->eleIdx].csvRowNumb;
  830. unsigned flags = p->array[ep->eleIdx].flags;
  831. // count grace notes
  832. if( cmIsFlag(flags,kGraceScFl) )
  833. ++graceCnt;
  834. // validate the 'frac' field - all but the last note in
  835. // tempo and non-grace evenness sets must have a non-zero 'frac' value.
  836. if( en>0 && j<en-1 && (sp->typeFl==kTempoScFl || (sp->typeFl==kEvenScFl && graceCnt==0)) && p->array[ep->eleIdx].frac==0)
  837. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The note on row number %i must have a non-zero 'frac' value.",p->array[ep->eleIdx].csvRowNumb);
  838. }
  839. // get the count of sections assoc'd with this set
  840. ep = sp->sects;
  841. for(en=0; ep!=NULL; ep=ep->link)
  842. ++en;
  843. // allocate the section array
  844. p->sets[i].varId = _cmScVarFlagToId(sp->typeFl);
  845. p->sets[i].sectCnt = en;
  846. p->sets[i].sectArray = cmMemAllocZ(cmScoreSection_t*,en);
  847. p->sets[i].symArray = cmMemAllocZ(unsigned,en);
  848. p->sets[i].costSymArray = cmMemAllocZ(unsigned,en);
  849. // fill in the section array with sections which this set will be applied to
  850. ep = sp->sects;
  851. for(j=0; ep!=NULL; ep=ep->link,++j)
  852. {
  853. cmScoreSection_t* sp;
  854. assert(ep->label != NULL);
  855. if((sp = _cmScLabelToSection(p, ep->label )) == NULL )
  856. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The section labelled '%s' could not be found for the set which includes row number %i.",ep->label,rowNumb);
  857. else
  858. {
  859. if( cmSymTblIsValid(p->stH) )
  860. {
  861. // THIS PRINTF PREVENTS CRASH ON OSX ???????
  862. //printf("PROCSET:%i %c %s\n",p->sets[i].varId,_cmScVarIdToChar(p->sets[i].varId),ep->label);
  863. char cc = _cmScVarIdToChar(p->sets[i].varId);
  864. int nn = snprintf(NULL,0,"%c-%s",cc,ep->label);
  865. char b[nn+3];
  866. snprintf(b,nn+1,"%c-%s",cc,ep->label);
  867. p->sets[i].symArray[j] = cmSymTblRegisterSymbol(p->stH,b);
  868. snprintf(b,nn+2,"c%c-%s",cc,ep->label);
  869. p->sets[i].costSymArray[j] = cmSymTblRegisterSymbol(p->stH,b);
  870. //p->sets[i].symArray[j] = cmSymTblRegisterFmt(p->stH,"%c-%s", cc,ep->label);
  871. //p->sets[i].costSymArray[j] = cmSymTblRegisterFmt(p->stH,"c%c-%s",cc,ep->label);
  872. }
  873. else
  874. {
  875. p->sets[i].symArray[j] = cmInvalidId;
  876. p->sets[i].costSymArray[j] = cmInvalidId;
  877. }
  878. p->sets[i].sectArray[j] = sp;
  879. sp->setArray = cmMemResizeP(cmScoreSet_t*,sp->setArray,++sp->setCnt);
  880. sp->setArray[sp->setCnt-1] = p->sets + i;
  881. }
  882. }
  883. ++i;
  884. }
  885. assert(i==n);
  886. // assign each set to the location which contains it's last element.
  887. // (this is earliest point in the score location where all the
  888. // performance information contained in the set may be valid)
  889. for(i=0; i<p->setCnt; ++i)
  890. {
  891. assert( p->sets[i].eleCnt >= 1 );
  892. // get a ptr to the last element for the ith set
  893. const cmScoreEvt_t* ep = p->sets[i].eleArray[ p->sets[i].eleCnt-1 ];
  894. unsigned j,k;
  895. // find the location which contains the last element
  896. for(j=0; j<p->locCnt; ++j)
  897. {
  898. for(k=0; k<p->loc[j].evtCnt; ++k)
  899. if( p->loc[j].evtArray[k] == ep )
  900. break;
  901. if(k<p->loc[j].evtCnt)
  902. break;
  903. }
  904. assert( j<p->locCnt );
  905. // assign the ith set to the location which contains it's last element
  906. p->sets[i].llink = p->loc[j].setList;
  907. p->loc[j].setList = p->sets + i;
  908. }
  909. return rc;
  910. }
  911. cmScRC_t _cmScProcSections( cmSc_t* p, cmScSect_t* sectList )
  912. {
  913. cmScRC_t rc = kOkScRC;
  914. unsigned i;
  915. // count the sections
  916. cmScSect_t* sp = sectList;
  917. p->sectCnt = 0;
  918. for(; sp!=NULL; sp=sp->link)
  919. if( sp->label != NULL )
  920. ++p->sectCnt;
  921. // alloc a section array
  922. p->sect = cmMemAllocZ(cmScoreSection_t,p->sectCnt);
  923. // fill the section array
  924. sp = sectList;
  925. for(i=0; sp!=NULL; sp=sp->link)
  926. if( sp->label != NULL )
  927. {
  928. p->sect[i].label = sp->label;
  929. p->sect[i].index = i;
  930. p->sect[i].begEvtIndex = sp->startIdx;
  931. ++i;
  932. }
  933. // assign the begSectPtr to each section
  934. for(i=0; i<p->sectCnt; ++i)
  935. {
  936. assert( p->sect[i].begEvtIndex < p->cnt );
  937. unsigned j,k;
  938. const cmScoreEvt_t* ep = p->array + p->sect[i].begEvtIndex;
  939. for(j=0; j<p->locCnt; ++j)
  940. {
  941. for(k=0; k<p->loc[j].evtCnt; ++k)
  942. if( p->loc[j].evtArray[k] == ep )
  943. {
  944. p->loc[j].begSectPtr = p->sect + i;
  945. p->sect[i].locPtr = p->loc + j;
  946. break;
  947. }
  948. if( k<p->loc[j].evtCnt)
  949. break;
  950. }
  951. }
  952. // release the section linked list
  953. sp = sectList;
  954. cmScSect_t* np = NULL;
  955. while(sp!=NULL)
  956. {
  957. np = sp->link;
  958. cmMemFree(sp);
  959. sp = np;
  960. }
  961. //_cmScPrintSets("Sets",p->setList );
  962. _cmScProcSets(p);
  963. return rc;
  964. }
  965. const cmScoreLoc_t* _cmScFindMarkLoc( cmSc_t* p, cmMarkScMId_t cmdId, unsigned labelSymId, const cmScoreMarker_t** markRef )
  966. {
  967. unsigned i;
  968. for(i=0; i<p->locCnt; ++i)
  969. {
  970. cmScoreMarker_t* smp = p->loc[i].markList;
  971. for(; smp!=NULL; smp=smp->link)
  972. {
  973. if( smp->markTypeId==cmdId && smp->labelSymId==labelSymId )
  974. return p->loc + i;
  975. if( markRef != NULL )
  976. *markRef = smp;
  977. }
  978. }
  979. return NULL;
  980. }
  981. unsigned _cmScMarkerLabelIndex( cmSc_t* p, unsigned labelSymId )
  982. {
  983. unsigned i;
  984. for(i=0; i<p->markLabelCnt; ++i)
  985. if( p->markLabelArray[i] == labelSymId )
  986. return i;
  987. return cmInvalidIdx;
  988. }
  989. // Verify that the record/play begin/end and fade markers fall in the correct time order.
  990. // (e.g. 'begin' must be before 'end' and 'fade' must be between and 'begin' and 'end').
  991. cmScRC_t _cmScValidateMarkers( cmSc_t* p )
  992. {
  993. cmScRC_t rc = kOkScRC;
  994. unsigned i;
  995. for(i=0; i<p->locCnt; ++i)
  996. {
  997. cmScoreMarker_t* sm0p = p->loc[i].markList;
  998. for(; sm0p!=NULL; sm0p=sm0p->link)
  999. {
  1000. const cmScoreLoc_t* sl0p;
  1001. const cmScoreLoc_t* sl1p;
  1002. switch( sm0p->markTypeId )
  1003. {
  1004. case kRecdBegScMId:
  1005. if((sl0p = _cmScFindMarkLoc(p,kRecdEndScMId, sm0p->labelSymId, NULL )) == NULL )
  1006. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record begin' marker at CSV row index %i does not have an associated 'record end' marker.",sm0p->csvRowIdx);
  1007. else
  1008. if( sl0p->index <= p->loc[i].index )
  1009. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record end' marker comes before associated with the 'record begin' marker at CSV row index %i.",sm0p->csvRowIdx);
  1010. break;
  1011. case kRecdEndScMId:
  1012. if((sl0p = _cmScFindMarkLoc(p,kRecdBegScMId, sm0p->labelSymId, NULL )) == NULL )
  1013. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record end' marker at CSV row index %i does not have an associated 'record begin' marker.",sm0p->csvRowIdx);
  1014. else
  1015. if( sl0p->index > p->loc[i].index )
  1016. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record begin' marker comes after the associated with the 'record end' marker at CSV row index %i.",sm0p->csvRowIdx);
  1017. break;
  1018. case kFadeScMId:
  1019. if((sl0p = _cmScFindMarkLoc(p,kPlayBegScMId, sm0p->labelSymId, NULL )) == NULL )
  1020. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'fade' marker at CSV row index %i does not have an associated 'play begin' marker.",sm0p->csvRowIdx);
  1021. else
  1022. if((sl1p = _cmScFindMarkLoc(p,kPlayEndScMId, sm0p->labelSymId, NULL )) == NULL )
  1023. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'fade' marker at CSV row index %i does not have an associated 'play end' marker.",sm0p->csvRowIdx);
  1024. else
  1025. if( sl0p->index > p->loc[i].index || sl1p->index < p->loc[i].index )
  1026. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'fade' marker at CSV row index %i is not between it's associated 'play begin' and 'play end' markers.",sm0p->csvRowIdx);
  1027. break;
  1028. case kPlayBegScMId:
  1029. if((sl0p = _cmScFindMarkLoc(p,kPlayEndScMId, sm0p->labelSymId, NULL )) == NULL )
  1030. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play begin' marker at CSV row index %i does not have an associated 'play end' marker.",sm0p->csvRowIdx);
  1031. else
  1032. if( sl0p->index <= p->loc[i].index )
  1033. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play end' marker comes before associated with the 'play begin' marker at CSV row index %i.",sm0p->csvRowIdx);
  1034. break;
  1035. case kPlayEndScMId:
  1036. if((sl0p = _cmScFindMarkLoc(p,kPlayBegScMId, sm0p->labelSymId, NULL )) == NULL )
  1037. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play end' marker at CSV row index %i does not have an associated 'play begin' marker.",sm0p->csvRowIdx);
  1038. else
  1039. if( sl0p->index > p->loc[i].index )
  1040. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play begin' marker comes after the associated with the 'play end' marker at CSV row index %i.",sm0p->csvRowIdx);
  1041. break;
  1042. default:
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. return rc;
  1048. }
  1049. cmScRC_t _cmScProcMarkers( cmSc_t* p )
  1050. {
  1051. // for each marker in the p->markList
  1052. // (p->markList is created by _cmScParseMarkers() during CSV file parsing.)
  1053. cmScMark_t* mp = p->markList;
  1054. for(; mp!=NULL; mp=mp->link)
  1055. {
  1056. assert( mp->scoreIdx < p->cnt );
  1057. // get the score location assoc'd with this marker
  1058. unsigned locIdx = p->array[ mp->scoreIdx ].locIdx;
  1059. assert( locIdx < p->locCnt );
  1060. cmScoreLoc_t* slp = p->loc + locIdx;
  1061. // create a cmScoreMarker record.
  1062. cmScoreMarker_t* smp = cmMemAllocZ(cmScoreMarker_t,1);
  1063. smp->markTypeId = mp->cmdId;
  1064. smp->labelSymId = mp->labelSymId;
  1065. smp->csvRowIdx = mp->csvRowIdx;
  1066. smp->scoreLocPtr = slp;
  1067. // attach the new scoreMarker record to the assoc'd score loc. recd
  1068. if( slp->markList == NULL )
  1069. slp->markList = smp;
  1070. else
  1071. {
  1072. cmScoreMarker_t* sm0p = slp->markList;
  1073. while( sm0p->link != NULL )
  1074. sm0p = sm0p->link;
  1075. sm0p->link = smp;
  1076. }
  1077. // if the id represented by this marker
  1078. if( _cmScMarkerLabelIndex(p,smp->labelSymId) == cmInvalidIdx )
  1079. {
  1080. p->markLabelArray = cmMemResizeP(unsigned,p->markLabelArray,p->markLabelCnt+1);
  1081. p->markLabelArray[p->markLabelCnt] = smp->labelSymId;
  1082. p->markLabelCnt += 1;
  1083. }
  1084. }
  1085. // validate the markers
  1086. return _cmScValidateMarkers(p);
  1087. }
  1088. cmScRC_t _cmScParseFile( cmSc_t* p, cmCtx_t* ctx, const cmChar_t* fn )
  1089. {
  1090. cmScRC_t rc = kOkScRC;
  1091. unsigned barNoteIdx = 0;
  1092. int barEvtIdx = cmInvalidIdx;
  1093. int barNumb = 0;
  1094. double secs;
  1095. double cur_secs = 0;
  1096. const unsigned pedalN = 3;
  1097. cmScoreEvt_t* pedalV[] = { NULL,NULL,NULL };
  1098. p->sectList = cmMemAllocZ(cmScSect_t,1); // section zero
  1099. //_cmScNewSet(p); // preallocate the first set
  1100. // initialize the CSV file parser
  1101. if( cmCsvInitialize(&p->cH, ctx ) != kOkCsvRC )
  1102. {
  1103. rc = cmErrMsg(&p->err,kCsvFailScRC,"Score file initialization failed.");
  1104. goto errLabel;
  1105. }
  1106. // register custom lex token matchers for sci-pitch and section id's
  1107. if( cmCsvLexRegisterMatcher(p->cH, p->sciPitchLexTId = cmCsvLexNextAvailId(p->cH)+0, _cmScLexSciPitchMatcher ) != kOkCsvRC
  1108. ||cmCsvLexRegisterMatcher(p->cH, p->sectionLexTId = cmCsvLexNextAvailId(p->cH)+1, _cmScLexSectionIdMatcher) != kOkCsvRC )
  1109. {
  1110. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV token matcher registration failed.");
  1111. goto errLabel;
  1112. }
  1113. // parse the CSV file
  1114. if( cmCsvParseFile(p->cH, fn, 0 ) != kOkCsvRC )
  1115. {
  1116. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV file parsing failed on the file '%s'.",cmStringNullGuard(fn));
  1117. goto errLabel;
  1118. }
  1119. // allocate the score event array
  1120. p->cnt = cmCsvRowCount(p->cH);
  1121. p->array = cmMemAllocZ(cmScoreEvt_t,p->cnt);
  1122. unsigned i,j;
  1123. // skip labels line - start on line 1
  1124. for(i=1,j=0; i<p->cnt && rc==kOkScRC; ++i)
  1125. {
  1126. bool postProcFl = false;
  1127. // get the row 'type' label
  1128. const char* typeLabel;
  1129. if((typeLabel = cmCsvCellText(p->cH,i,kTypeLabelColScIdx)) == NULL )
  1130. {
  1131. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"No type label.");
  1132. break;
  1133. }
  1134. // convert the row 'type' label to an id
  1135. unsigned tid;
  1136. if((tid = _cmScEvtTypeLabelToId(typeLabel)) == kInvalidEvtScId)
  1137. {
  1138. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"Unknown type '%s'.",cmStringNullGuard(typeLabel));
  1139. break;
  1140. }
  1141. // set the score file line number
  1142. p->array[j].line = i + 1;
  1143. secs = DBL_MAX;
  1144. switch(tid)
  1145. {
  1146. case kBarEvtScId: // parse bar lines
  1147. if((rc = _cmScParseBar(p,i,p->array+j,&barNumb)) == kOkScRC )
  1148. {
  1149. barNoteIdx = 0;
  1150. barEvtIdx = j;
  1151. p->array[j].index = j;
  1152. ++j;
  1153. }
  1154. break;
  1155. case kNonEvtScId: // parse note-on events
  1156. if((rc = _cmScParseNoteOn(p, i, p->array, j, barNumb, barNoteIdx )) == kOkScRC )
  1157. {
  1158. postProcFl = true;
  1159. ++barNoteIdx;
  1160. }
  1161. break;
  1162. case kCtlEvtScId:
  1163. if((rc = _cmScParseMidiCtlMsg(p, i, p->array, j, barNumb, barNoteIdx, pedalV, pedalN )) == kOkScRC )
  1164. {
  1165. postProcFl = true;
  1166. }
  1167. break;
  1168. default:
  1169. // Returns DBL_MAX on error.
  1170. secs = cmCsvCellDouble(p->cH, i, kSecsColScIdx );
  1171. break;
  1172. }
  1173. if( postProcFl )
  1174. {
  1175. // update the 'secs' according to the parsed time
  1176. secs = p->array[j].secs;
  1177. // it is possible that the parsed time field was blank ...
  1178. if( p->array[j].secs == DBL_MAX )
  1179. {
  1180. // ... so set the msg time to the last valid time
  1181. p->array[j].secs = cur_secs;
  1182. // note that 'secs' is now set to DBL_MAX so cur_secs will
  1183. // not be updated on this row iteration
  1184. }
  1185. // if this msg was marked to skip then don't advance j (and thereby
  1186. // write over this scEvt with the next note). ...
  1187. if( cmIsFlag(p->array[j].flags,kSkipScFl) == false )
  1188. {
  1189. p->array[j].index = j; // ... otherwise advance j
  1190. ++j;
  1191. }
  1192. }
  1193. if( rc == kOkScRC )
  1194. {
  1195. // if sec's is valid then update cur_secs
  1196. if( secs != DBL_MAX )
  1197. cur_secs = secs;
  1198. // form the section list
  1199. if( j > 0 )
  1200. if((rc = _cmScParseSectionColumn(p,i,j-1,p->sectList)) != kOkScRC )
  1201. break;
  1202. // the bar lines don't have times so set the time of the bar line to the
  1203. // time of the first event in the bar.
  1204. if( barEvtIdx != cmInvalidIdx && secs != DBL_MAX )
  1205. {
  1206. assert( p->array[ barEvtIdx ].type == kBarEvtScId );
  1207. p->array[ barEvtIdx ].secs = secs;
  1208. // handle the case where the previous bar had no events
  1209. // BUG BUG BUG this is a hack which will fail if the first bar does not have events.
  1210. if( barEvtIdx>=1 && p->array[ barEvtIdx-1].type == kBarEvtScId )
  1211. p->array[ barEvtIdx-1].secs = secs;
  1212. barEvtIdx = cmInvalidIdx;
  1213. }
  1214. }
  1215. }
  1216. if( rc == kSyntaxErrScRC )
  1217. {
  1218. cmErrMsg(&p->err,rc,"Syntax error on line %i in '%s'.",i+1,cmStringNullGuard(fn));
  1219. goto errLabel;
  1220. }
  1221. p->cnt = j;
  1222. errLabel:
  1223. return rc;
  1224. }
  1225. cmScRC_t _cmScInitLocArray( cmSc_t* p )
  1226. {
  1227. cmScRC_t rc = kOkScRC;
  1228. double maxDSecs = 0; // max time between events that are considered simultaneous
  1229. unsigned barNumb = 0;
  1230. int i,j,k;
  1231. if( p->cnt==0)
  1232. return rc;
  1233. p->locCnt = 1;
  1234. // count the number of unique time locations in the score
  1235. p->locCnt = 1;
  1236. for(i=1; i<p->cnt; ++i )
  1237. {
  1238. if( p->array[i].secs < p->array[i-1].secs )
  1239. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The time (%f) associated with the score entry on line %i is less than the previous line (%f).",p->array[i].csvRowNumb,p->array[i].secs,p->array[i-1].secs);
  1240. if( (p->array[i].secs - p->array[i-1].secs) > maxDSecs )
  1241. ++p->locCnt;
  1242. }
  1243. if( rc != kOkScRC )
  1244. return rc;
  1245. // allocate the loc. array
  1246. p->loc = cmMemAllocZ(cmScoreLoc_t,p->locCnt);
  1247. // fill in the location array
  1248. for(i=0,k=0; i<p->cnt; ++k)
  1249. {
  1250. j = i+1;
  1251. // get the count of events at this location
  1252. while( j<p->cnt && p->array[j].secs - p->array[j-1].secs <= maxDSecs )
  1253. ++j;
  1254. assert(k<p->locCnt);
  1255. p->loc[k].index = k;
  1256. p->loc[k].evtCnt = j-i;
  1257. p->loc[k].evtArray = cmMemAllocZ(cmScoreEvt_t*,p->loc[k].evtCnt);
  1258. // fill in the location record event pointers
  1259. for(j=0; j<p->loc[k].evtCnt; ++j)
  1260. {
  1261. p->loc[k].evtArray[j] = p->array + (i + j);
  1262. p->loc[k].evtArray[j]->locIdx = k;
  1263. if( p->array[i+j].type == kBarEvtScId )
  1264. barNumb = p->array[i+j].barNumb;
  1265. }
  1266. // fill in the location record
  1267. p->loc[k].secs = p->array[i].secs;
  1268. p->loc[k].barNumb = barNumb;
  1269. i += p->loc[k].evtCnt;
  1270. }
  1271. assert( p->locCnt == k );
  1272. return rc;
  1273. }
  1274. cmScRC_t cmScoreInitialize( cmCtx_t* ctx, cmScH_t* hp, const cmChar_t* fn, double srate, const unsigned* dynRefArray, unsigned dynRefCnt, cmScCb_t cbFunc, void* cbArg, cmSymTblH_t stH )
  1275. {
  1276. cmScRC_t rc = kOkScRC;
  1277. if((rc = cmScoreFinalize(hp)) != kOkScRC )
  1278. return rc;
  1279. cmSc_t* p = cmMemAllocZ(cmSc_t,1);
  1280. cmErrSetup(&p->err,&ctx->rpt,"Score");
  1281. p->stH = stH;
  1282. if((rc = _cmScParseFile(p,ctx,fn)) != kOkScRC )
  1283. goto errLabel;
  1284. if((rc = _cmScInitLocArray(p)) != kOkScRC )
  1285. goto errLabel;
  1286. if((rc = _cmScProcSections(p,p->sectList)) != kOkScRC )
  1287. goto errLabel;
  1288. if((rc = _cmScProcMarkers(p)) != kOkScRC )
  1289. goto errLabel;
  1290. // load the dynamic reference array
  1291. if( dynRefArray != NULL && dynRefCnt > 0)
  1292. {
  1293. unsigned n = _cmScDynLabelCount();
  1294. if( dynRefCnt != n )
  1295. {
  1296. rc = cmErrMsg(&p->err,kInvalidDynRefCntScRC,"The count of dynamics labels must be %i not %i.",n,dynRefCnt);
  1297. goto errLabel;
  1298. }
  1299. p->dynRefArray = cmMemAllocZ(unsigned,dynRefCnt);
  1300. memcpy(p->dynRefArray,dynRefArray,sizeof(unsigned)*dynRefCnt);
  1301. p->dynRefCnt = dynRefCnt;
  1302. }
  1303. p->srate = srate;
  1304. p->cbFunc = cbFunc;
  1305. p->cbArg = cbArg;
  1306. p->fn = cmMemAllocStr(fn);
  1307. p->nxtLocIdx = 0;
  1308. p->minSetLocIdx = cmInvalidIdx;
  1309. p->maxSetLocIdx = cmInvalidIdx;
  1310. hp->h = p;
  1311. cmScoreClearPerfInfo(*hp);
  1312. //cmScorePrintLoc(*hp);
  1313. errLabel:
  1314. if( rc != kOkScRC )
  1315. _cmScFinalize(p);
  1316. return rc;
  1317. }
  1318. cmScRC_t cmScoreFinalize( cmScH_t* hp )
  1319. {
  1320. cmScRC_t rc = kOkScRC;
  1321. if( hp == NULL || cmScoreIsValid(*hp) == false )
  1322. return kOkScRC;
  1323. cmSc_t* p = _cmScHandleToPtr(*hp);
  1324. if((rc = _cmScFinalize(p)) != kOkScRC )
  1325. return rc;
  1326. hp->h = NULL;
  1327. return rc;
  1328. }
  1329. const cmChar_t* cmScoreFileName( cmScH_t h )
  1330. {
  1331. cmSc_t* p = _cmScHandleToPtr(h);
  1332. return p->fn;
  1333. }
  1334. double cmScoreSampleRate( cmScH_t h )
  1335. {
  1336. cmSc_t* p = _cmScHandleToPtr(h);
  1337. return p->srate;
  1338. }
  1339. bool cmScoreIsValid( cmScH_t h )
  1340. { return h.h != NULL; }
  1341. unsigned cmScoreEvtCount( cmScH_t h )
  1342. {
  1343. cmSc_t* p = _cmScHandleToPtr(h);
  1344. return p->cnt;
  1345. }
  1346. cmScoreEvt_t* cmScoreEvt( cmScH_t h, unsigned idx )
  1347. {
  1348. cmSc_t* p = _cmScHandleToPtr(h);
  1349. if( idx >= p->cnt )
  1350. {
  1351. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i records.",idx,p->cnt);
  1352. return NULL;
  1353. }
  1354. return p->array + idx;
  1355. }
  1356. const cmScoreEvt_t* cmScoreBarEvt( cmScH_t h, unsigned barNumb )
  1357. {
  1358. cmSc_t* p = _cmScHandleToPtr(h);
  1359. unsigned i = 0;
  1360. for(; i<p->cnt; ++i)
  1361. if( p->array[i].type==kBarEvtScId && p->array[i].barNumb==barNumb )
  1362. return p->array + i;
  1363. return NULL;
  1364. }
  1365. const cmScoreEvt_t* cmScoreIdToEvt( cmScH_t h, unsigned csvEventId )
  1366. {
  1367. cmSc_t* p = _cmScHandleToPtr(h);
  1368. unsigned i = 0;
  1369. for(; i<p->cnt; ++i)
  1370. if( p->array[i].csvEventId==csvEventId )
  1371. return p->array + i;
  1372. return NULL;
  1373. }
  1374. unsigned cmScoreSectionCount( cmScH_t h )
  1375. {
  1376. cmSc_t* p = _cmScHandleToPtr(h);
  1377. return p->sectCnt;
  1378. }
  1379. cmScoreSection_t* cmScoreSection( cmScH_t h, unsigned idx )
  1380. {
  1381. cmSc_t* p = _cmScHandleToPtr(h);
  1382. assert( idx < p->sectCnt);
  1383. return p->sect + idx;
  1384. }
  1385. unsigned cmScoreLocCount( cmScH_t h )
  1386. {
  1387. cmSc_t* p = _cmScHandleToPtr(h);
  1388. return p->locCnt;
  1389. }
  1390. cmScoreLoc_t* cmScoreLoc( cmScH_t h, unsigned idx )
  1391. {
  1392. cmSc_t* p = _cmScHandleToPtr(h);
  1393. if( idx >= p->locCnt )
  1394. {
  1395. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i location records.",idx,p->locCnt);
  1396. return NULL;
  1397. }
  1398. return p->loc + idx;
  1399. }
  1400. cmScoreLoc_t* cmScoreEvtLoc( cmScH_t h, const cmScoreEvt_t* evt )
  1401. {
  1402. cmSc_t* p = _cmScHandleToPtr(h);
  1403. unsigned i;
  1404. if( evt != NULL )
  1405. for(i=0; i<p->locCnt; ++i)
  1406. {
  1407. unsigned j;
  1408. for(j=0; j<p->loc[i].evtCnt; ++j)
  1409. if( p->loc[i].evtArray[j] == evt )
  1410. return p->loc + i;
  1411. }
  1412. return NULL;
  1413. }
  1414. void cmScorePrintLoc( cmScH_t h )
  1415. {
  1416. unsigned i = 0;
  1417. cmSc_t* p = _cmScHandleToPtr(h);
  1418. unsigned colCnt = 10;
  1419. int bufCharCnt = 4;
  1420. char buf[ bufCharCnt ];
  1421. const char* emptyStr = " ";
  1422. // for each set of 'colCnt' columns
  1423. for(i=0; i<p->locCnt; i+=colCnt )
  1424. {
  1425. // print the location 'index' line
  1426. unsigned c,j,k;
  1427. printf("index: ");
  1428. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1429. printf("%7i ",j);
  1430. printf("\n");
  1431. // print the 'sectn' label line
  1432. printf("sectn: ");
  1433. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1434. if( p->loc[j].begSectPtr==NULL )
  1435. printf("%s",emptyStr);
  1436. else
  1437. printf("%7s ",p->loc[j].begSectPtr->label);
  1438. printf("\n");
  1439. // calculate the max number of simultan. events at any one location
  1440. // for this set of 'colCnt' columns.
  1441. unsigned n=0;
  1442. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1443. if( p->loc[j].evtCnt > n )
  1444. n = p->loc[j].evtCnt;
  1445. // for each 'sco' line
  1446. for(k=0; k<n; ++k)
  1447. {
  1448. printf("sco%2i: ",k);
  1449. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1450. if( k >= p->loc[j].evtCnt )
  1451. printf("%s",emptyStr);
  1452. else
  1453. {
  1454. switch(p->loc[j].evtArray[k]->type)
  1455. {
  1456. case kBarEvtScId:
  1457. printf(" |%3i ",p->loc[j].evtArray[k]->barNumb);
  1458. break;
  1459. case kNonEvtScId:
  1460. {
  1461. int bn=16;
  1462. char b[bn];
  1463. strcpy(b,cmMidiToSciPitch(p->loc[j].evtArray[k]->pitch,NULL,0));
  1464. strcat(b,_cmScFlagsToStr( p->loc[j].evtArray[k]->flags,buf,bufCharCnt));
  1465. assert(strlen(b)<bn);
  1466. printf("%7s ",b);
  1467. break;
  1468. }
  1469. }
  1470. }
  1471. printf("\n");
  1472. }
  1473. // calc the max number of set triggers which will occur on
  1474. // any one location for this set of 'colCnt' columns.
  1475. n=0;
  1476. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1477. if(p->loc[j].begSectPtr != NULL && p->loc[j].begSectPtr->setCnt > n )
  1478. n = p->loc[j].begSectPtr->setCnt;
  1479. for(k=0; k<n; ++k)
  1480. {
  1481. printf("trig%1i: ",k);
  1482. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1483. {
  1484. if( p->loc[j].begSectPtr != NULL && k<p->loc[j].begSectPtr->setCnt )
  1485. {
  1486. const cmScoreSet_t* sp = p->loc[j].begSectPtr->setArray[k];
  1487. printf(" %3s-%c ",p->loc[j].begSectPtr->label,_cmScVarIdToChar(sp->varId) );
  1488. }
  1489. else
  1490. {
  1491. printf("%s",emptyStr);
  1492. }
  1493. }
  1494. printf("\n");
  1495. }
  1496. printf("\n");
  1497. }
  1498. }
  1499. unsigned cmScoreSetCount( cmScH_t h )
  1500. {
  1501. cmSc_t* p = _cmScHandleToPtr(h);
  1502. return p->setCnt;
  1503. }
  1504. unsigned cmScoreMarkerLabelCount( cmScH_t h )
  1505. {
  1506. cmSc_t* p = _cmScHandleToPtr(h);
  1507. return p->markLabelCnt;
  1508. }
  1509. unsigned cmScoreMarkerLabelSymbolId( cmScH_t h, unsigned idx )
  1510. {
  1511. cmSc_t* p = _cmScHandleToPtr(h);
  1512. assert( idx < p->markLabelCnt );
  1513. return p->markLabelArray[idx];
  1514. }
  1515. const cmScoreMarker_t* cmScoreMarker( cmScH_t h, cmMarkScMId_t markMId, unsigned labelSymId )
  1516. {
  1517. cmSc_t* p = _cmScHandleToPtr(h);
  1518. const cmScoreMarker_t* smp = NULL;
  1519. if( _cmScFindMarkLoc(p, markMId, labelSymId, &smp ) == NULL )
  1520. return NULL;
  1521. return smp;
  1522. }
  1523. cmScRC_t cmScoreSeqNotify( cmScH_t h )
  1524. {
  1525. cmScRC_t rc = kOkScRC;
  1526. cmSc_t* p = _cmScHandleToPtr(h);
  1527. cmScMsg_t m;
  1528. unsigned i;
  1529. if( p->cbFunc != NULL )
  1530. {
  1531. memset(&m.u.evt,0,sizeof(m.u.evt));
  1532. m.typeId = kBeginMsgScId;
  1533. p->cbFunc(p->cbArg,&m,sizeof(m));
  1534. m.typeId = kEventMsgScId;
  1535. for(i=0; i<p->cnt; ++i)
  1536. {
  1537. m.u.evt = p->array[i];
  1538. p->cbFunc(p->cbArg,&m,sizeof(m));
  1539. }
  1540. m.typeId = kSectionMsgScId;
  1541. for(i=0; i<p->sectCnt; ++i)
  1542. {
  1543. m.u.sect = p->sect[i];
  1544. p->cbFunc(p->cbArg,&m,sizeof(m));
  1545. }
  1546. memset(&m.u.evt,0,sizeof(m.u.evt));
  1547. m.typeId = kEndMsgScId;
  1548. p->cbFunc(p->cbArg,&m,sizeof(m));
  1549. }
  1550. return rc;
  1551. }
  1552. cmScRC_t cmScoreSeqNotifyCb( cmScH_t h, cmScCb_t cbFunc, void* cbArg )
  1553. {
  1554. cmSc_t* p = _cmScHandleToPtr(h);
  1555. cmScRC_t rc = kOkScRC;
  1556. cmScCb_t cbf = p->cbFunc;
  1557. void* cba = p->cbArg;
  1558. p->cbFunc = cbFunc;
  1559. p->cbArg = cbArg;
  1560. rc = cmScoreSeqNotify(h);
  1561. p->cbFunc = cbf;
  1562. p->cbArg = cba;
  1563. return rc;
  1564. }
  1565. void cmScoreClearPerfInfo( cmScH_t h )
  1566. {
  1567. cmSc_t* p = _cmScHandleToPtr(h);
  1568. unsigned i;
  1569. for(i=0; i<p->cnt; ++i)
  1570. {
  1571. p->array[i].perfSmpIdx = cmInvalidIdx;
  1572. p->array[i].perfVel = 0;
  1573. p->array[i].perfDynLvl = 0;
  1574. }
  1575. for(i=0; i<p->locCnt; ++i)
  1576. {
  1577. cmScoreSet_t* sp = p->loc[i].setList;
  1578. for(; sp!=NULL; sp=sp->llink)
  1579. sp->doneFl = false;
  1580. }
  1581. for(i=0; i<p->sectCnt; ++i)
  1582. {
  1583. unsigned j;
  1584. for(j=0; j<kScVarCnt; ++j)
  1585. p->sect[i].vars[j] = DBL_MAX;
  1586. }
  1587. p->nxtLocIdx = 0;
  1588. p->minSetLocIdx = cmInvalidIdx;
  1589. p->maxSetLocIdx = cmInvalidIdx;
  1590. }
  1591. bool _cmScIsSetPerfDone( cmScoreSet_t* sp )
  1592. {
  1593. unsigned i = 0;
  1594. for(i=0; i<sp->eleCnt; ++i)
  1595. if( sp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1596. return false;
  1597. return true;
  1598. }
  1599. void _cmScPerfSortTimes( unsigned *v, unsigned n )
  1600. {
  1601. unsigned i;
  1602. bool fl = true;
  1603. while(fl && n)
  1604. {
  1605. fl = false;
  1606. for(i=1; i<n; ++i)
  1607. {
  1608. if( v[i-1] > v[i] )
  1609. {
  1610. unsigned t = v[i-1];
  1611. v[i-1] = v[i];
  1612. v[i] = t;
  1613. fl = true;
  1614. }
  1615. }
  1616. --n;
  1617. }
  1618. }
  1619. bool _cmScPerfEven(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1620. {
  1621. unsigned i = 0;
  1622. double u = 0;
  1623. double x = 0;
  1624. bool sortFl = false;
  1625. bool printFl = true;
  1626. unsigned v[ stp->eleCnt ];
  1627. unsigned d[ stp->eleCnt - 1];
  1628. assert( stp->eleCnt > 1 );
  1629. // calculate the sum of the time between events
  1630. for(i=0; i<stp->eleCnt; ++i)
  1631. {
  1632. // if this event was not received - then the set is not valid
  1633. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1634. {
  1635. if( printFl && printMissFl)
  1636. printf("EVENESS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1637. return false;
  1638. }
  1639. // load v[i]
  1640. v[i] = stp->eleArray[i]->perfSmpIdx;
  1641. // check for out of order elements
  1642. if( i> 0 )
  1643. if( v[i] < v[i-1] )
  1644. sortFl = true;
  1645. }
  1646. // sort the times in ascending order
  1647. if( sortFl )
  1648. _cmScPerfSortTimes(v,stp->eleCnt);
  1649. // calc the sum of time differences
  1650. for(i=1; i<stp->eleCnt; ++i)
  1651. u += d[i-1] = v[i] - v[i-1];
  1652. // calculate the average time between events
  1653. u /= stp->eleCnt-1;
  1654. // calculate the std-dev of the time between events
  1655. for(i=0; i<stp->eleCnt-1; ++i)
  1656. x += (d[i]-u)*(d[i]-u);
  1657. double sd = sqrt(x/(stp->eleCnt-1));
  1658. // compute the average z-score
  1659. double c = 0;
  1660. for(i=0; i<stp->eleCnt-1; ++i)
  1661. c += fabs(d[i]-u)/sd;
  1662. stp->value = c/(stp->eleCnt-1);
  1663. stp->doneFl = true;
  1664. if(printFl)
  1665. {
  1666. /*
  1667. for(i=0; i<stp->eleCnt; ++i)
  1668. {
  1669. printf("%i %i ",i,v[i]);
  1670. if( i > 0 )
  1671. printf("%i ", d[i-1]);
  1672. printf("\n");
  1673. }
  1674. */
  1675. printf("%s EVENESS:%f\n",sortFl?"SORTED ":"",stp->value);
  1676. }
  1677. return true;
  1678. }
  1679. bool _cmScPerfDyn( cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1680. {
  1681. double a = 0;
  1682. unsigned i = 0;
  1683. bool printFl = true;
  1684. for(i=0; i<stp->eleCnt; ++i)
  1685. {
  1686. unsigned j;
  1687. // if this event was not received - then the set is not valid
  1688. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1689. {
  1690. if( printFl && printMissFl )
  1691. printf("DYNAMICS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1692. return false;
  1693. }
  1694. unsigned m = 0; // lower bound for the first dyn. category
  1695. // determine the dynamic category for the performed velocity of each event
  1696. for(j=0; j<p->dynRefCnt; ++j)
  1697. {
  1698. // if the vel fall's into the jth dyn. category
  1699. if( m <= stp->eleArray[i]->perfVel && stp->eleArray[i]->perfVel < p->dynRefArray[j] )
  1700. break;
  1701. // store the min vel for the next dyn category
  1702. m = p->dynRefArray[j];
  1703. }
  1704. assert( j < p->dynRefCnt );
  1705. stp->eleArray[i]->perfDynLvl = j+1;
  1706. if( j + 1 > stp->eleArray[i]->dynVal )
  1707. a += (j+1) - stp->eleArray[i]->dynVal;
  1708. else
  1709. a += stp->eleArray[i]->dynVal - (j+1);
  1710. if( p->cbFunc != NULL )
  1711. {
  1712. cmScMsg_t m;
  1713. m.typeId = kDynMsgScId;
  1714. m.u.dyn.evtIdx = stp->eleArray[i]->index;
  1715. m.u.dyn.dynLvl = stp->eleArray[i]->perfDynLvl;
  1716. p->cbFunc(p->cbArg,&m,sizeof(m));
  1717. }
  1718. }
  1719. stp->value = a / stp->eleCnt;
  1720. stp->doneFl = true;
  1721. if( printFl )
  1722. printf("DYNAMICS:%f\n",stp->value);
  1723. return true;
  1724. }
  1725. bool _cmScPerfTempo1(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1726. {
  1727. bool printFl = true;
  1728. unsigned durSmpCnt = 0;
  1729. double durBeats = 0;
  1730. int i;
  1731. for(i=0; i<stp->eleCnt; ++i)
  1732. {
  1733. // if this event was not received - then the set is not valid
  1734. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1735. {
  1736. if( printFl && printMissFl )
  1737. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1738. return false;
  1739. }
  1740. if( i > 0 )
  1741. durSmpCnt += stp->eleArray[i]->perfSmpIdx - stp->eleArray[i-1]->perfSmpIdx;
  1742. durBeats += stp->eleArray[i]->frac;
  1743. }
  1744. stp->value = durBeats / (durSmpCnt / p->srate*60.0 );
  1745. stp->doneFl = true;
  1746. if( printFl )
  1747. printf("TEMPO:%f\n",stp->value);
  1748. return true;
  1749. }
  1750. bool _cmScPerfTempo(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1751. {
  1752. bool printFl = true;
  1753. unsigned durSmpCnt = 0;
  1754. double durBeats = 0;
  1755. int i;
  1756. unsigned bi = cmInvalidIdx;
  1757. unsigned ei = cmInvalidIdx;
  1758. unsigned missCnt = 0;
  1759. for(i=0; i<stp->eleCnt; ++i)
  1760. {
  1761. // if this event was not received - then the set is not valid
  1762. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1763. {
  1764. ++missCnt;
  1765. if( printFl && printMissFl )
  1766. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1767. }
  1768. else
  1769. {
  1770. if( bi == cmInvalidIdx )
  1771. bi = i;
  1772. ei = i;
  1773. }
  1774. }
  1775. if( ei > bi )
  1776. {
  1777. for(i=bi; i<=ei; ++i)
  1778. durBeats += stp->eleArray[i]->frac;
  1779. durSmpCnt = stp->eleArray[ei]->perfSmpIdx - stp->eleArray[bi]->perfSmpIdx;
  1780. stp->value = durBeats / (durSmpCnt / (p->srate*60.0) );
  1781. stp->doneFl = true;
  1782. }
  1783. if( printFl )
  1784. printf("TEMPO:%f bi:%i ei:%i secs:%f bts:%f\n",stp->value,bi,ei,durSmpCnt/p->srate,durBeats);
  1785. return true;
  1786. }
  1787. void _cmScPerfExec( cmSc_t* p, cmScoreSet_t* sp, bool printMissFl )
  1788. {
  1789. if( sp->doneFl == false )
  1790. {
  1791. switch( sp->varId )
  1792. {
  1793. case kEvenVarScId:
  1794. _cmScPerfEven(p,sp,printMissFl);
  1795. break;
  1796. case kDynVarScId:
  1797. _cmScPerfDyn(p,sp,printMissFl);
  1798. break;
  1799. case kTempoVarScId:
  1800. _cmScPerfTempo(p,sp,printMissFl);
  1801. break;
  1802. default:
  1803. { assert(0); }
  1804. }
  1805. }
  1806. }
  1807. void _cmScPerfExecRange( cmSc_t* p )
  1808. {
  1809. if( p->minSetLocIdx == cmInvalidIdx || p->maxSetLocIdx==cmInvalidIdx )
  1810. return;
  1811. unsigned i = p->minSetLocIdx;
  1812. for(; i<=p->maxSetLocIdx; ++i)
  1813. {
  1814. cmScoreSet_t* sp = p->loc[i].setList;
  1815. for(; sp!=NULL; sp=sp->llink)
  1816. _cmScPerfExec(p,sp,true);
  1817. }
  1818. }
  1819. bool _cmScSetPerfEvent( cmSc_t* p, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1820. {
  1821. assert(locIdx < p->locCnt );
  1822. cmScoreLoc_t* lp = p->loc + locIdx;
  1823. bool doneFl = true;
  1824. unsigned i;
  1825. #ifndef NDEBUG
  1826. bool foundFl = false;
  1827. #endif
  1828. // locate the event at the loc[locIdx]
  1829. for(i=0; i<lp->evtCnt; ++i)
  1830. {
  1831. cmScoreEvt_t* ep = lp->evtArray[i];
  1832. if( ep->type == kNonEvtScId )
  1833. {
  1834. if( ep->pitch == pitch )
  1835. {
  1836. assert( ep->perfSmpIdx == cmInvalidIdx );
  1837. ep->perfSmpIdx = smpIdx;
  1838. ep->perfVel = vel;
  1839. #ifndef NDEBUG
  1840. foundFl = true;
  1841. #endif
  1842. }
  1843. // check if all notes have arrived for this location
  1844. if( ep->perfSmpIdx == cmInvalidIdx )
  1845. doneFl = false;
  1846. }
  1847. }
  1848. // the event must always be found
  1849. assert( foundFl );
  1850. return doneFl;
  1851. }
  1852. bool cmScoreSetPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1853. {
  1854. cmSc_t* p = _cmScHandleToPtr(h);
  1855. return _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1856. }
  1857. void cmScoreExecPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1858. {
  1859. unsigned i;
  1860. cmSc_t* p = _cmScHandleToPtr(h);
  1861. bool doneFl = _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1862. unsigned printLvl = 1;
  1863. cmScoreLoc_t* lp = p->loc + locIdx;
  1864. // all events for a location must be complete to trigger attached events
  1865. if( doneFl == false )
  1866. return;
  1867. if( p->loc[locIdx].setList != NULL )
  1868. {
  1869. // set idx of most recent loc w/ a set end event
  1870. p->maxSetLocIdx = locIdx;
  1871. if( p->minSetLocIdx == cmInvalidIdx )
  1872. p->minSetLocIdx = locIdx;
  1873. }
  1874. // attempt to calculate all sets between loc[p->minSetLocIdx] and loc[p->maxSetLocIdx]
  1875. _cmScPerfExecRange(p);
  1876. // prevent event retriggering or going backwards
  1877. if( printLvl && locIdx < p->nxtLocIdx )
  1878. {
  1879. printf("----- BACK ----- \n");
  1880. return;
  1881. }
  1882. if( printLvl && locIdx > p->nxtLocIdx )
  1883. {
  1884. printf("----- SKIP ----- \n");
  1885. }
  1886. // for each location between the current and previous location
  1887. for(; p->nxtLocIdx<=locIdx; ++p->nxtLocIdx)
  1888. {
  1889. lp = p->loc + p->nxtLocIdx;
  1890. // if this location is the start of a new section - then apply
  1891. // sets that are assigned to this section
  1892. if( lp->begSectPtr != NULL && lp->begSectPtr->setCnt > 0 )
  1893. {
  1894. // notice the location of the oldest section start - once we cross this point
  1895. // it is too late to notice set completions - so incr p->inSetLocIdx
  1896. if( lp->begSectPtr->setCnt )
  1897. p->minSetLocIdx = p->nxtLocIdx+1;
  1898. for(i=0; i<lp->begSectPtr->setCnt; ++i)
  1899. {
  1900. cmScoreSet_t* stp = lp->begSectPtr->setArray[i];
  1901. // temporarily commented out for testing purposes
  1902. // if( stp->doneFl == false )
  1903. // _cmScPerfExec(p, stp, printLvl>0 );
  1904. if( stp->doneFl )
  1905. {
  1906. assert( stp->varId < kScVarCnt );
  1907. lp->begSectPtr->vars[ stp->varId ] = stp->value;
  1908. if( p->cbFunc != NULL )
  1909. {
  1910. cmScMsg_t m;
  1911. m.typeId = kVarMsgScId;
  1912. m.u.meas.varId = stp->varId;
  1913. m.u.meas.value = stp->value;
  1914. p->cbFunc(p->cbArg,&m,sizeof(m));
  1915. }
  1916. }
  1917. }
  1918. }
  1919. }
  1920. }
  1921. void cmScoreSetPerfValue( cmScH_t h, unsigned locIdx, unsigned varId, double value )
  1922. {
  1923. cmSc_t* p = _cmScHandleToPtr(h);
  1924. int li = locIdx;
  1925. for(; li>=0; --li)
  1926. if( p->loc[li].begSectPtr != NULL )
  1927. {
  1928. assert( varId < kScVarCnt );
  1929. p->loc[li].begSectPtr->vars[varId] = value;
  1930. break;
  1931. }
  1932. assert( li>=0);
  1933. }
  1934. void cmScoreSetPerfDynLevel( cmScH_t h, unsigned evtIdx, unsigned dynLvl )
  1935. {
  1936. cmSc_t* p = _cmScHandleToPtr(h);
  1937. assert(evtIdx < p->cnt );
  1938. p->array[ evtIdx ].perfDynLvl = dynLvl;
  1939. }
  1940. cmScRC_t cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* m)
  1941. {
  1942. cmScMsg_t* mp = (cmScMsg_t*)msg;
  1943. *m = *mp;
  1944. return kOkScRC;
  1945. }
  1946. void _cmScorePrintHdr( cmRpt_t* rpt )
  1947. {
  1948. cmRptPrintf(rpt,"evnt CSV bar\n");
  1949. cmRptPrintf(rpt,"index line loctn bar idx type pitch ETD Dynamic\n");
  1950. cmRptPrintf(rpt,"----- ----- ----- --- --- ----- ----- --- -------\n");
  1951. }
  1952. void _cmScorePrintEvent( const cmScoreEvt_t* r, unsigned i, cmRpt_t* rpt )
  1953. {
  1954. switch(r->type)
  1955. {
  1956. case kBarEvtScId:
  1957. cmRptPrintf(rpt,"%5i %5i %3i bar\n",
  1958. i,
  1959. r->line,
  1960. r->barNumb );
  1961. break;
  1962. case kPedalEvtScId:
  1963. case kNonEvtScId:
  1964. cmRptPrintf(rpt,"%5i %5i %5i %3i %3i %s %5s %c%c%c %s\n",
  1965. i,
  1966. r->line,
  1967. r->locIdx,
  1968. r->barNumb,
  1969. r->barNoteIdx,
  1970. cmScEvtTypeIdToLabel(r->type),
  1971. r->type==kNonEvtScId ? cmMidiToSciPitch(r->pitch,NULL,0) : (cmIsFlag(r->flags,kPedalUpScFl)? " up" : " dn"),
  1972. cmIsFlag(r->flags,kEvenScFl) ? 'e' : ' ',
  1973. cmIsFlag(r->flags,kTempoScFl) ? 't' : ' ',
  1974. cmIsFlag(r->flags,kDynScFl) ? 'd' : ' ',
  1975. cmIsFlag(r->flags,kDynScFl) ? cmScDynIdToLabel(r->dynVal) : "");
  1976. break;
  1977. default:
  1978. break;
  1979. }
  1980. }
  1981. void cmScorePrint( cmScH_t h, cmRpt_t* rpt )
  1982. {
  1983. cmSc_t* p = _cmScHandleToPtr(h);
  1984. unsigned i;
  1985. _cmScorePrintHdr(rpt);
  1986. for(i=0; i<p->cnt; ++i)
  1987. _cmScorePrintEvent(p->array+i,i,rpt);
  1988. }
  1989. void cmScorePrintSets( cmScH_t h, cmRpt_t* rpt )
  1990. {
  1991. cmSc_t* p = _cmScHandleToPtr(h);
  1992. unsigned i,j,k;
  1993. // the score must have been opened with a valid symbol table
  1994. assert( cmSymTblIsValid(p->stH) );
  1995. for(i=0,k=0; i<p->locCnt; ++i)
  1996. {
  1997. const cmScoreSet_t* s = p->loc[i].setList;
  1998. for(; s!=NULL; s=s->llink)
  1999. {
  2000. char cc = _cmScVarIdToChar(s->varId);
  2001. cmRptPrintf(rpt,"\n%i Set:%c\n",k,cc);
  2002. ++k;
  2003. _cmScorePrintHdr(rpt);
  2004. for(j=0; j<s->eleCnt; ++j)
  2005. _cmScorePrintEvent(*s->eleArray+j,j,rpt);
  2006. cmRptPrintf(rpt,"Targets Section: ");
  2007. for(j=0; j<s->sectCnt; ++j)
  2008. cmRptPrintf(rpt,"%s ",s->sectArray[j]->label);
  2009. cmRptPrintf(rpt,"\n");
  2010. cmRptPrintf(rpt,"Variables: ");
  2011. for(j=0; j<s->sectCnt; ++j)
  2012. cmRptPrintf(rpt,"%s ",cmSymTblLabel(p->stH,s->symArray[j]));
  2013. for(j=0; j<s->sectCnt; ++j)
  2014. cmRptPrintf(rpt,cmSymTblLabel(p->stH,s->costSymArray[j]));
  2015. cmRptPrintf(rpt,"\n");
  2016. }
  2017. }
  2018. }
  2019. cmScRC_t cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* scoreFn )
  2020. {
  2021. cmScRC_t rc = kOkScRC;
  2022. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  2023. cmCsvH_t csvH = cmCsvNullHandle;
  2024. cmErr_t err;
  2025. cmChar_t* titles[] = {"id","trk","evt","opcode","dticks","micros","status","meta","ch","d0","d1","arg0","arg1","bar","skip","even","grace","tempo","t frac","dyn","section","remark", NULL };
  2026. cmErrSetup(&err,&ctx->rpt,"MIDI to Score");
  2027. if( cmMidiFileOpen(ctx, &mfH, midiFn ) != kOkMfRC )
  2028. return cmErrMsg(&err,kMidiFileFailScRC,"Unable to open the MIDI file '%s'.",midiFn);
  2029. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  2030. {
  2031. cmErrMsg(&err,kCsvFailScRC,"Unable to initialize the CSV file: '%s'.",scoreFn);
  2032. goto errLabel;
  2033. }
  2034. //printf("secs:%f smps:%f\n",cmMidiFileDurSecs(mfH),cmMidiFileDurSecs(mfH)*96000);
  2035. unsigned msgCnt = cmMidiFileMsgCount(mfH);
  2036. unsigned i;
  2037. const cmMidiTrackMsg_t** tmpp = cmMidiFileMsgArray(mfH);
  2038. double acc_secs = 0;
  2039. unsigned lexTId = 0;
  2040. cmCsvCell_t* cp = NULL;
  2041. if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymText(csvH,titles[0]), kStrCsvTFl, lexTId ) != kOkCsvRC )
  2042. {
  2043. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
  2044. goto errLabel;
  2045. }
  2046. for(i=1; titles[i]!=NULL; ++i)
  2047. {
  2048. if( cmCsvInsertIdentColAfter(csvH, cp, &cp, titles[i], lexTId ) != kOkCsvRC )
  2049. {
  2050. cmErrMsg(&err,kCsvFailScRC,"Error inserting column index '%i' label in '%s'.",i,cmStringNullGuard(scoreFn));
  2051. goto errLabel;
  2052. }
  2053. }
  2054. for(i=0; i<msgCnt; ++i)
  2055. {
  2056. const cmMidiTrackMsg_t* tmp = tmpp[i];
  2057. const cmChar_t* opStr = NULL;
  2058. unsigned midiCh = 0;
  2059. unsigned d0 = 0;
  2060. unsigned d1 = 0;
  2061. unsigned metaId = 0;
  2062. double dsecs = (double)tmp->amicro / 1000000.0;
  2063. acc_secs += dsecs;
  2064. if( tmp->status == kMetaStId )
  2065. {
  2066. //opStr = cmMidiMetaStatusToLabel(tmp->metaId);
  2067. opStr = cmScStatusToOpString(tmp->metaId);
  2068. metaId = tmp->metaId;
  2069. switch( tmp->metaId )
  2070. {
  2071. case kTempoMdId:
  2072. d0 = tmp->u.iVal;
  2073. }
  2074. }
  2075. else
  2076. {
  2077. //opStr = cmMidiStatusToLabel(tmp->status);
  2078. opStr = cmScStatusToOpString(tmp->status);
  2079. if( cmMidiIsChStatus( tmp->status ) )
  2080. {
  2081. midiCh = tmp->u.chMsgPtr->ch;
  2082. d0 = tmp->u.chMsgPtr->d0;
  2083. d1 = tmp->u.chMsgPtr->d1;
  2084. }
  2085. }
  2086. cp = NULL;
  2087. // skip note-off messages
  2088. if( tmp->status == kNoteOffMdId )
  2089. continue;
  2090. if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymUInt(csvH,i), kIntCsvTFl, lexTId ) != kOkCsvRC )
  2091. {
  2092. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
  2093. goto errLabel;
  2094. }
  2095. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->trkIdx, lexTId ) != kOkCsvRC )
  2096. {
  2097. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'trk' column in '%s'.",cmStringNullGuard(scoreFn));
  2098. goto errLabel;
  2099. }
  2100. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 0, lexTId ) != kOkCsvRC )
  2101. {
  2102. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'evt' column in '%s'.",cmStringNullGuard(scoreFn));
  2103. goto errLabel;
  2104. }
  2105. if( cmCsvInsertQTextColAfter(csvH, cp, &cp, opStr, lexTId ) != kOkCsvRC )
  2106. {
  2107. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
  2108. goto errLabel;
  2109. }
  2110. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, dsecs, lexTId ) != kOkCsvRC )
  2111. {
  2112. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'dticks' column in '%s'.",cmStringNullGuard(scoreFn));
  2113. goto errLabel;
  2114. }
  2115. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, acc_secs, lexTId ) != kOkCsvRC )
  2116. {
  2117. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'micros' column in '%s'.",cmStringNullGuard(scoreFn));
  2118. goto errLabel;
  2119. }
  2120. if( cmCsvInsertHexColAfter(csvH, cp, &cp, tmp->status, lexTId ) != kOkCsvRC )
  2121. {
  2122. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'status' column in '%s'.",cmStringNullGuard(scoreFn));
  2123. goto errLabel;
  2124. }
  2125. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, metaId, lexTId ) != kOkCsvRC )
  2126. {
  2127. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'meta' column in '%s'.",cmStringNullGuard(scoreFn));
  2128. goto errLabel;
  2129. }
  2130. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, midiCh, lexTId ) != kOkCsvRC )
  2131. {
  2132. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'ch' column in '%s'.",cmStringNullGuard(scoreFn));
  2133. goto errLabel;
  2134. }
  2135. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d0, lexTId ) != kOkCsvRC )
  2136. {
  2137. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd0' column in '%s'.",cmStringNullGuard(scoreFn));
  2138. goto errLabel;
  2139. }
  2140. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d1, lexTId ) != kOkCsvRC )
  2141. {
  2142. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd1' column in '%s'.",cmStringNullGuard(scoreFn));
  2143. goto errLabel;
  2144. }
  2145. switch( tmp->status )
  2146. {
  2147. case kNoteOnMdId:
  2148. if( cmCsvInsertQTextColAfter(csvH, cp, &cp, cmMidiToSciPitch(tmp->u.chMsgPtr->d0,NULL,0), lexTId ) != kOkCsvRC )
  2149. {
  2150. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
  2151. goto errLabel;
  2152. }
  2153. case kMetaStId:
  2154. switch( tmp->metaId )
  2155. {
  2156. case kTimeSigMdId:
  2157. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->num, lexTId ) != kOkCsvRC )
  2158. {
  2159. cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. numerator column in '%s'.",cmStringNullGuard(scoreFn));
  2160. goto errLabel;
  2161. }
  2162. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->den, lexTId ) != kOkCsvRC )
  2163. {
  2164. cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. denominator column in '%s'.",cmStringNullGuard(scoreFn));
  2165. goto errLabel;
  2166. }
  2167. break;
  2168. case kTempoMdId:
  2169. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 60000000/tmp->u.iVal, lexTId ) != kOkCsvRC )
  2170. {
  2171. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'tempo' in '%s'.",cmStringNullGuard(scoreFn));
  2172. goto errLabel;
  2173. }
  2174. break;
  2175. }
  2176. }
  2177. }
  2178. if( cmCsvWrite(csvH,scoreFn) != kOkCsvRC )
  2179. {
  2180. cmErrMsg(&err,kCsvFailScRC,"The score output file '%s' could not be written.",cmStringNullGuard(scoreFn));
  2181. goto errLabel;
  2182. }
  2183. errLabel:
  2184. cmMidiFileClose(&mfH);
  2185. cmCsvFinalize(&csvH);
  2186. return rc;
  2187. }
  2188. void cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
  2189. {
  2190. cmScH_t h = cmScNullHandle;
  2191. if( cmScoreInitialize(ctx,&h,fn,0,NULL,0,NULL,NULL, cmSymTblNullHandle ) != kOkScRC )
  2192. return;
  2193. cmScorePrint(h,&ctx->rpt);
  2194. cmScoreFinalize(&h);
  2195. }
  2196. // 1. Fix absolute message time which was incorrect on original score file.
  2197. // 2.
  2198. void cmScoreFix( cmCtx_t* ctx )
  2199. {
  2200. const cmChar_t* mfn = "/home/kevin/src/cmgv/src/gv/data/ImaginaryThemes.mid";
  2201. const cmChar_t* crfn = "/home/kevin/src/cmgv/src/gv/data/mod0a.txt";
  2202. const cmChar_t* cwfn = "/home/kevin/src/cmgv/src/gv/data/mod1.csv";
  2203. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  2204. cmCsvH_t csvH = cmCsvNullHandle;
  2205. const cmMidiTrackMsg_t** msg = NULL;
  2206. double secs = 0.0;
  2207. int ci,mi,crn,mn;
  2208. bool errFl = true;
  2209. unsigned handCnt = 0;
  2210. unsigned midiMissCnt = 0;
  2211. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  2212. goto errLabel;
  2213. if( cmCsvLexRegisterMatcher(csvH, cmCsvLexNextAvailId(csvH), _cmScLexSciPitchMatcher ) != kOkCsvRC )
  2214. goto errLabel;
  2215. if( cmCsvParseFile(csvH, crfn, 0 ) != kOkCsvRC )
  2216. goto errLabel;
  2217. if( cmMidiFileOpen(ctx,&mfH,mfn) != kOkMfRC )
  2218. goto errLabel;
  2219. mn = cmMidiFileMsgCount(mfH);
  2220. msg = cmMidiFileMsgArray(mfH);
  2221. crn = cmCsvRowCount(csvH);
  2222. // for each row in the score file
  2223. for(ci=1,mi=0; ci<crn && cmCsvLastRC(csvH)==kOkCsvRC; ++ci)
  2224. {
  2225. unsigned id;
  2226. // zero the duration column
  2227. if( cmCsvCellPtr(csvH, ci, kDSecsColScIdx ) != NULL )
  2228. cmCsvSetCellUInt( csvH, ci, kDSecsColScIdx, 0 );
  2229. // get the MIDI file event id for this row
  2230. if((id = cmCsvCellUInt(csvH,ci,kMidiFileIdColScIdx)) == UINT_MAX)
  2231. {
  2232. // this is a hand-entered event - so it has no event id
  2233. ++handCnt;
  2234. }
  2235. else
  2236. {
  2237. for(; mi<mn; ++mi)
  2238. {
  2239. const cmMidiTrackMsg_t* m = msg[mi];
  2240. assert( mi+1 <= id );
  2241. secs += m->amicro/1000000.0;
  2242. if( mi+1 != id )
  2243. {
  2244. if( m->status == kNoteOnMdId && m->u.chMsgPtr->d1>0 )
  2245. {
  2246. // this MIDI note-on does not have a corresponding score event
  2247. ++midiMissCnt;
  2248. }
  2249. }
  2250. else
  2251. {
  2252. cmCsvSetCellDouble( csvH, ci, kSecsColScIdx, secs );
  2253. ++mi;
  2254. if( m->status == kNoteOnMdId )
  2255. cmCsvSetCellDouble( csvH, ci, kDSecsColScIdx, m->u.chMsgPtr->durMicros /1000000.0 );
  2256. break;
  2257. }
  2258. }
  2259. if( mi==mn)
  2260. printf("done on row:%i\n",ci);
  2261. }
  2262. }
  2263. if( cmCsvLastRC(csvH) != kOkCsvRC )
  2264. goto errLabel;
  2265. if( cmCsvWrite(csvH,cwfn) != kOkCsvRC )
  2266. goto errLabel;
  2267. errFl = false;
  2268. errLabel:
  2269. if( errFl )
  2270. printf("Score fix failed.\n");
  2271. else
  2272. printf("Score fix done! hand:%i miss:%i\n",handCnt,midiMissCnt);
  2273. cmMidiFileClose(&mfH);
  2274. cmCsvFinalize(&csvH);
  2275. }