libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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