libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmScore.c 65KB

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