libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmScore.c 49KB

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