libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmScoreMatchGraphic.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmLinkedHeap.h"
  10. #include "cmTime.h"
  11. #include "cmMidi.h"
  12. #include "cmLex.h"
  13. #include "cmCsv.h"
  14. #include "cmSymTbl.h"
  15. #include "cmMidiFile.h"
  16. #include "cmAudioFile.h"
  17. #include "cmTimeLine.h"
  18. #include "cmText.h"
  19. #include "cmFile.h"
  20. #include "cmScore.h"
  21. #include "cmScoreMatchGraphic.h"
  22. enum
  23. {
  24. kLocSmgFl = 0x0001,
  25. kBarSmgFl = 0x0002,
  26. kNoteSmgFl = 0x0004,
  27. kMidiSmgFl = 0x0008,
  28. kNoMatchSmgFl = 0x0010
  29. };
  30. // Graphic box representing a score label or MIDI event
  31. typedef struct cmSmgBox_str
  32. {
  33. unsigned flags;
  34. unsigned id; // csvEventId or midiUid
  35. unsigned left;
  36. unsigned top;
  37. unsigned width;
  38. unsigned height;
  39. cmChar_t* text0;
  40. cmChar_t* text1;
  41. struct cmSmgBox_str* link;
  42. } cmSmgBox_t;
  43. // Graphic line linking secondary MIDI matches to score events
  44. typedef struct cmSmgLine_str
  45. {
  46. cmSmgBox_t* b0;
  47. cmSmgBox_t* b1;
  48. struct cmSmgLine_str* link;
  49. } cmSmgLine_t;
  50. // Score Location
  51. typedef struct cmSmgLoc_str
  52. {
  53. cmSmgBox_t* bV; // List of graphic boxes assigned to this score location
  54. } cmSmgLoc_t;
  55. // Score label
  56. typedef struct
  57. {
  58. unsigned type; // kBarEvtScId | kNonEvtScId
  59. unsigned barNumb;
  60. unsigned csvEventId;
  61. unsigned locIdx;
  62. cmSmgBox_t* box;
  63. } cmSmgSc_t;
  64. // Link a MIDI event to it's matched score label.
  65. typedef struct cmSmgMatch_str
  66. {
  67. cmSmgSc_t* score;
  68. struct cmSmgMatch_str* link;
  69. } cmSmgMatch_t;
  70. // MIDI file event
  71. typedef struct
  72. {
  73. double secs;
  74. unsigned uid;
  75. unsigned pitch;
  76. unsigned vel;
  77. cmSmgMatch_t* matchV; // list of matches to score events
  78. cmSmgBox_t* box;
  79. } cmSmgMidi_t;
  80. typedef struct
  81. {
  82. cmErr_t err;
  83. cmChar_t* scFn;
  84. cmSmgSc_t* scV; // scV[scN] score bars and notes
  85. unsigned scN;
  86. cmSmgLoc_t* locV; // locV[locN] score locations (from the score file)
  87. unsigned locN;
  88. cmSmgLine_t* lines; // Graphic lines used to indicate that a midi event matches to multiple score notes.
  89. // (Each match after the first gets a line from the box representing the midi event
  90. // to the matching score event)
  91. cmChar_t* mfFn; // MIDI file name
  92. cmSmgMidi_t* mV; // mV[mN] midi note-on events
  93. unsigned mN;
  94. double mfDurSecs; // midi file duration in seconds
  95. unsigned boxW; // graphic box width and height
  96. unsigned boxH;
  97. } cmSmg_t;
  98. cmSmgH_t cmSmgNullHandle = cmSTATIC_NULL_HANDLE;
  99. cmSmg_t* _cmSmgHandleToPtr( cmSmgH_t h )
  100. {
  101. cmSmg_t* p = (cmSmg_t*)h.h;
  102. assert(p!=NULL);
  103. return p;
  104. }
  105. cmSmgRC_t _cmSmgFree( cmSmg_t* p )
  106. {
  107. unsigned i;
  108. for(i=0; i<p->mN; ++i)
  109. {
  110. cmSmgMatch_t* m0 = p->mV[i].matchV;
  111. cmSmgMatch_t* m1 = NULL;
  112. while(m0!=NULL)
  113. {
  114. m1 = m0->link;
  115. cmMemFree(m0);
  116. m0 = m1;
  117. }
  118. }
  119. for(i=0; i<p->locN; ++i)
  120. {
  121. cmSmgBox_t* b0 = p->locV[i].bV;
  122. cmSmgBox_t* b1 = NULL;
  123. while(b0!=NULL)
  124. {
  125. b1 = b0->link;
  126. cmMemFree(b0->text0);
  127. cmMemFree(b0->text1);
  128. cmMemFree(b0);
  129. b0 = b1;
  130. }
  131. }
  132. cmSmgLine_t* l0 = p->lines;
  133. cmSmgLine_t* l1 = NULL;
  134. while( l0 != NULL )
  135. {
  136. l1 = l0->link;
  137. cmMemFree(l0);
  138. l0 = l1;
  139. }
  140. cmMemFree(p->scFn);
  141. cmMemFree(p->mfFn);
  142. cmMemFree(p->scV);
  143. cmMemFree(p->mV);
  144. cmMemFree(p->locV);
  145. cmMemFree(p);
  146. return kOkSmgRC;
  147. }
  148. cmSmgBox_t* _cmSmgInsertBox( cmSmg_t* p, unsigned locIdx, unsigned flags, unsigned id, cmChar_t* text0, cmChar_t* text1 )
  149. {
  150. assert( locIdx < p->locN );
  151. cmSmgBox_t* b = cmMemAllocZ(cmSmgBox_t,1);
  152. b->flags = flags;
  153. b->id = id;
  154. b->text0 = text0;
  155. b->text1 = text1;
  156. if( p->locV[locIdx].bV == NULL )
  157. {
  158. p->locV[locIdx].bV = b;
  159. }
  160. else
  161. {
  162. cmSmgBox_t* b0 = p->locV[locIdx].bV;
  163. while( b0->link!=NULL )
  164. b0 = b0->link;
  165. b0->link = b;
  166. }
  167. return b;
  168. }
  169. cmSmgRC_t _cmSmgInitFromScore( cmCtx_t* ctx, cmSmg_t* p, const cmChar_t* scoreFn )
  170. {
  171. cmSmgRC_t rc = kOkSmgRC;
  172. cmScH_t scH = cmScNullHandle;
  173. unsigned i,j,k;
  174. if( cmScoreInitialize(ctx,&scH,scoreFn,44100.0, NULL, 0, NULL, NULL, cmSymTblNullHandle ) != kOkScRC )
  175. return cmErrMsg(&p->err,kScoreFailSmgRC,"Score initializatio failed on '%s'.",cmStringNullGuard(scoreFn));
  176. p->scFn = cmMemAllocStr(scoreFn);
  177. p->scN = cmScoreEvtCount(scH);
  178. p->scV = cmMemAllocZ(cmSmgSc_t,p->scN);
  179. p->locN = cmScoreLocCount(scH);
  180. p->locV = cmMemAllocZ(cmSmgLoc_t,p->locN);
  181. // for each score location
  182. for(i=0,k=0; i<cmScoreLocCount(scH); ++i)
  183. {
  184. cmScoreLoc_t* l = cmScoreLoc(scH,i);
  185. // insert the location label box
  186. _cmSmgInsertBox(p, i, kLocSmgFl, cmInvalidId, cmTsPrintfP(NULL,"%i",i), NULL );
  187. // for each event in location i
  188. for(j=0; j<l->evtCnt; ++j)
  189. {
  190. const cmScoreEvt_t* e = l->evtArray[j];
  191. switch( e->type)
  192. {
  193. case kBarEvtScId:
  194. case kNonEvtScId:
  195. {
  196. // Note: Mark all score boxes as 'no-match' - this will be cleared in cmScoreMatchGraphicInsertMidi().
  197. unsigned flags = kNoMatchSmgFl | (e->type==kNonEvtScId ? kNoteSmgFl : kBarSmgFl);
  198. cmChar_t* text = NULL;
  199. assert( k < p->scN );
  200. p->scV[k].type = e->type;
  201. p->scV[k].csvEventId = e->csvEventId;
  202. p->scV[k].locIdx = i;
  203. p->scV[k].barNumb = e->barNumb;
  204. if( e->type == kBarEvtScId )
  205. text = cmTsPrintfP(NULL,"%i",e->barNumb);
  206. else
  207. text = cmMemAllocStr( cmMidiToSciPitch( e->pitch, NULL, 0));
  208. p->scV[k].box = _cmSmgInsertBox(p, i, flags, e->csvEventId, text, NULL );
  209. k += 1;
  210. }
  211. break;
  212. }
  213. }
  214. }
  215. p->scN = k;
  216. cmScoreFinalize(&scH);
  217. return rc;
  218. }
  219. cmSmgRC_t _cmSmgInitFromMidi( cmCtx_t* ctx, cmSmg_t* p, const cmChar_t* midiFn )
  220. {
  221. cmSmgRC_t rc = kOkSmgRC;
  222. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  223. unsigned i,j;
  224. if( cmMidiFileOpen(ctx, &mfH, midiFn ) != kOkMfRC )
  225. return cmErrMsg(&p->err,kMidiFileFailSmgRC,"MIDI file open failed on '%s'.",cmStringNullGuard(midiFn));
  226. const cmMidiTrackMsg_t** mV = cmMidiFileMsgArray(mfH);
  227. unsigned mN = cmMidiFileMsgCount(mfH);
  228. p->mV = cmMemAllocZ(cmSmgMidi_t,mN);
  229. p->mN = mN;
  230. p->mfDurSecs = cmMidiFileDurSecs(mfH);
  231. p->mfFn = cmMemAllocStr(midiFn);
  232. for(i=0,j=0; i<mN; ++i)
  233. if( (mV[i]!=NULL) && cmMidiIsChStatus(mV[i]->status) && cmMidiIsNoteOn(mV[i]->status) && (mV[i]->u.chMsgPtr->d1>0) )
  234. {
  235. assert(j < mN);
  236. p->mV[j].secs = mV[i]->amicro / 1000000.0;
  237. p->mV[j].uid = mV[i]->uid;
  238. p->mV[j].pitch = mV[i]->u.chMsgPtr->d0;
  239. p->mV[j].vel = mV[i]->u.chMsgPtr->d1;
  240. ++j;
  241. }
  242. p->mN = j;
  243. cmMidiFileClose(&mfH);
  244. return rc;
  245. }
  246. cmSmgRC_t cmScoreMatchGraphicAlloc( cmCtx_t* ctx, cmSmgH_t* hp, const cmChar_t* scoreFn, const cmChar_t* midiFn )
  247. {
  248. cmSmgRC_t rc;
  249. if((rc = cmScoreMatchGraphicFree(hp)) != kOkSmgRC )
  250. return rc;
  251. cmSmg_t* p = cmMemAllocZ(cmSmg_t,1);
  252. cmErrSetup(&p->err,&ctx->rpt,"ScoreMatchGraphic");
  253. if((rc = _cmSmgInitFromScore(ctx,p,scoreFn)) != kOkSmgRC )
  254. goto errLabel;
  255. if((rc = _cmSmgInitFromMidi(ctx,p,midiFn)) != kOkSmgRC )
  256. goto errLabel;
  257. p->boxW = 30;
  258. p->boxH = 50;
  259. hp->h = p;
  260. errLabel:
  261. if( rc != kOkSmgRC )
  262. _cmSmgFree(p);
  263. return rc;
  264. }
  265. bool cmScoreMatchGraphicIsValid( cmSmgH_t h )
  266. { return h.h != NULL; }
  267. cmSmgRC_t cmScoreMatchGraphicFree( cmSmgH_t* hp )
  268. {
  269. cmSmgRC_t rc = kOkSmgRC;
  270. if(hp==NULL || cmScoreMatchGraphicIsValid(*hp)==false)
  271. return kOkSmgRC;
  272. cmSmg_t* p = _cmSmgHandleToPtr(*hp);
  273. if((rc = _cmSmgFree(p)) != kOkSmgRC )
  274. return rc;
  275. hp->h = NULL;
  276. return rc;
  277. }
  278. bool cmScoreMatchGraphic( cmSmgH_t h )
  279. { return h.h != NULL; }
  280. cmSmgRC_t cmScoreMatchGraphicInsertMidi( cmSmgH_t h, unsigned midiUid, unsigned midiPitch, unsigned midiVel, unsigned csvScoreEventId )
  281. {
  282. cmSmg_t* p = _cmSmgHandleToPtr(h);
  283. unsigned i,j;
  284. // if this midi event did not match any score records
  285. if( csvScoreEventId == cmInvalidId )
  286. return kOkSmgRC;
  287. assert(midiUid != cmInvalidId );
  288. assert(midiPitch<128 && midiVel<128);
  289. // find the midi file record which matches the event
  290. for(i=0; i<p->mN; ++i)
  291. if( p->mV[i].uid == midiUid )
  292. {
  293. // find the score record which matches the score event id
  294. for(j=0; j<p->scN; ++j)
  295. if( p->scV[j].csvEventId == csvScoreEventId )
  296. {
  297. // create a match record
  298. cmSmgMatch_t* m = cmMemAllocZ(cmSmgMatch_t,1);
  299. m->score = p->scV + j;
  300. // mark the box associated with this score record as 'matched' by clearing the kNoMatchSmgFl
  301. p->scV[j].box->flags = cmClrFlag(p->scV[j].box->flags,kNoMatchSmgFl);
  302. // insert the match record in the midi files match list
  303. if( p->mV[i].matchV == NULL )
  304. p->mV[i].matchV = m;
  305. else
  306. {
  307. cmSmgMatch_t* m0 = p->mV[i].matchV;
  308. while( m0->link != NULL )
  309. m0 = m0->link;
  310. m0->link = m;
  311. }
  312. return kOkSmgRC;
  313. }
  314. return cmErrMsg(&p->err,kScoreFailSmgRC,"The score csv event id %i not found,",csvScoreEventId);
  315. }
  316. return cmErrMsg(&p->err,kMidiFileFailSmgRC,"MIDI uid %i not found.",midiUid);
  317. }
  318. // Create a box for each MIDI event and a line for each
  319. // match beyond the first.
  320. void _cmSmgResolveMidi( cmSmg_t* p )
  321. {
  322. unsigned prevLocIdx = 0;
  323. unsigned i;
  324. // for each midi record
  325. for(i=0; i<p->mN; ++i)
  326. {
  327. // get the first match record for this MIDI event
  328. const cmSmgMatch_t* m = p->mV[i].matchV;
  329. // get the score location for this midi event
  330. unsigned locIdx = m==NULL ? prevLocIdx : m->score->locIdx;
  331. unsigned flags = kMidiSmgFl | (m==NULL ? kNoMatchSmgFl : 0);
  332. // set the text label for this event
  333. cmChar_t* text = cmMemAllocStr( cmMidiToSciPitch( p->mV[i].pitch, NULL, 0));
  334. // insert a box to represent this midi event
  335. cmSmgBox_t* box = _cmSmgInsertBox( p, locIdx, flags, p->mV[i].uid, text, cmTsPrintfP(NULL,"%i",p->mV[i].uid) );
  336. prevLocIdx = locIdx;
  337. // if this midi event matched to multiple score positions
  338. if( m != NULL && m->link != NULL )
  339. {
  340. // insert a line for each match after the first
  341. m = m->link;
  342. for(; m!=NULL; m=m->link )
  343. {
  344. cmSmgLine_t* l = cmMemAllocZ(cmSmgLine_t,1);
  345. l->b0 = box;
  346. l->b1 = m->score->box;
  347. l->link = p->lines;
  348. p->lines = l;
  349. }
  350. }
  351. }
  352. }
  353. void _cmSmgLayout( cmSmg_t* p )
  354. {
  355. unsigned i;
  356. unsigned bordX = 5;
  357. unsigned bordY = 5;
  358. unsigned top = p->boxH + 2*bordY;
  359. unsigned left = bordX;
  360. for(i=0; i<p->locN; ++i)
  361. {
  362. cmSmgLoc_t* l = p->locV + i;
  363. cmSmgBox_t* b = l->bV;
  364. // for each box attached to this location
  365. for(; b!=NULL; b=b->link)
  366. {
  367. // bar boxes are always drawn at the top of the column
  368. if( cmIsFlag(b->flags,kBarSmgFl) )
  369. b->top = bordY;
  370. else
  371. {
  372. b->top = top;
  373. top += p->boxH + bordY;
  374. }
  375. b->left = left;
  376. b->width = p->boxW;
  377. b->height = p->boxH;
  378. }
  379. left += p->boxW + bordX;
  380. top = p->boxH + 2*bordY;
  381. }
  382. }
  383. void _cmSmgSvgSize( cmSmg_t* p, unsigned* widthRef, unsigned* heightRef )
  384. {
  385. unsigned i;
  386. unsigned maxWidth = 0;
  387. unsigned maxHeight = 0;
  388. for(i=0; i<p->locN; ++i)
  389. {
  390. cmSmgBox_t* b = p->locV[i].bV;
  391. for(; b != NULL; b=b->link )
  392. {
  393. if( b->left + b->width > maxWidth )
  394. maxWidth = b->left + b->width;
  395. if( b->top + b->height > maxHeight )
  396. maxHeight = b->top + b->height;
  397. }
  398. }
  399. *widthRef = maxWidth;
  400. *heightRef = maxHeight;
  401. }
  402. cmSmgRC_t cmScoreMatchGraphicWrite( cmSmgH_t h, const cmChar_t* fn )
  403. {
  404. cmSmg_t* p = _cmSmgHandleToPtr(h);
  405. cmFileH_t fH = cmFileNullHandle;
  406. unsigned svgHeight = 0;
  407. unsigned svgWidth = 0;
  408. unsigned i;
  409. // BUG BUG BUG : this can only be called once
  410. // create a box for each midi event
  411. _cmSmgResolveMidi( p );
  412. // layout the boxes
  413. _cmSmgLayout( p );
  414. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  415. return cmErrMsg(&p->err,kFileFailScRC,"Graphic file create failed for '%s'.",cmStringNullGuard(fn));
  416. _cmSmgSvgSize(p,&svgWidth,&svgHeight);
  417. svgHeight += 10; // add a right and lower border
  418. svgWidth += 10;
  419. cmFilePrintf(fH,"<!DOCTYPE html>\n<html>\n<head><link rel=\"stylesheet\" type=\"text/css\" href=\"score0.css\"></head><body>\n<svg width=\"%i\" height=\"%i\">\n",svgWidth,svgHeight);
  420. for(i=0; i<p->locN; ++i)
  421. {
  422. cmSmgBox_t* b = p->locV[i].bV;
  423. for(; b != NULL; b=b->link )
  424. {
  425. const cmChar_t* classStr = "score";
  426. if( cmIsFlag(b->flags,kLocSmgFl) )
  427. classStr = "loc";
  428. if( cmIsFlag(b->flags,kMidiSmgFl) )
  429. classStr = "midi";
  430. if( cmIsFlag(b->flags,kNoMatchSmgFl) )
  431. if( cmIsFlag(b->flags,kMidiSmgFl) )
  432. classStr = "midi_miss";
  433. if( cmIsFlag(b->flags,kNoMatchSmgFl) )
  434. if( cmIsFlag(b->flags,kNoteSmgFl) )
  435. classStr = "score_miss";
  436. if( cmIsFlag(b->flags,kBarSmgFl) )
  437. classStr = "bar";
  438. if( cmFilePrintf(fH,"<rect x=\"%i\" y=\"%i\" width=\"%i\" height=\"%i\" class=\"%s\"/>\n",b->left,b->top,b->width,b->height,classStr) != kOkFileRC )
  439. return cmErrMsg(&p->err,kFileFailScRC,"File write failed on graphic file rect output.");
  440. if( b->text0 != NULL )
  441. {
  442. unsigned tx = b->left + b->width/2;
  443. unsigned ty = b->top + 20;
  444. if( cmFilePrintf(fH,"<text x=\"%i\" y=\"%i\" text-anchor=\"middle\" class=\"stext\">%s</text>\n",tx,ty,b->text0) != kOkFileRC )
  445. return cmErrMsg(&p->err,kFileFailScRC,"File write failed on graphic file text output.");
  446. }
  447. if( b->text1 != NULL )
  448. {
  449. unsigned tx = b->left + b->width/2;
  450. unsigned ty = b->top + 20 + 20;
  451. if( cmFilePrintf(fH,"<text x=\"%i\" y=\"%i\" text-anchor=\"middle\" class=\"stext\">%s</text>\n",tx,ty,b->text1) != kOkFileRC )
  452. return cmErrMsg(&p->err,kFileFailScRC,"File write failed on graphic file text output.");
  453. }
  454. }
  455. }
  456. cmSmgLine_t* l = p->lines;
  457. for(; l!=NULL; l=l->link)
  458. {
  459. unsigned x0 = l->b0->left + l->b0->width/2;
  460. unsigned y0 = l->b0->top + l->b0->height/2;
  461. unsigned x1 = l->b1->left + l->b1->width/2;
  462. unsigned y1 = l->b1->top + l->b1->height/2;
  463. if( cmFilePrintf(fH,"<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\" class=\"sline\"/>\n",x0,y0,x1,y1) != kOkFileRC )
  464. return cmErrMsg(&p->err,kFileFailScRC,"File write failed on graphic file line output.");
  465. }
  466. cmFilePrint(fH,"</svg>\n</body>\n</html>\n");
  467. cmFileClose(&fH);
  468. return kOkSmgRC;
  469. }
  470. cmSmgRC_t cmScoreMatchGraphicGenTimeLineBars( cmSmgH_t h, const cmChar_t* fn, unsigned srate )
  471. {
  472. cmSmgRC_t rc = kOkSmgRC;
  473. cmFileH_t f = cmFileNullHandle;
  474. cmSmg_t* p = _cmSmgHandleToPtr(h);
  475. unsigned i = 0;
  476. if( cmFileOpen(&f,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  477. return cmErrMsg(&p->err,kFileSmgRC,"The time-line bar file '%s' could not be created.",cmStringNullGuard(fn));
  478. for(i=0; i<p->mN; ++i)
  479. if( p->mV[i].matchV != NULL && p->mV[i].matchV->score != NULL && p->mV[i].matchV->score > p->scV && p->mV[i].matchV->score[-1].type==kBarEvtScId )
  480. {
  481. unsigned bar = p->mV[i].matchV->score->barNumb;
  482. unsigned offset = p->mV[i].secs * srate;
  483. unsigned smpCnt = p->mfDurSecs * srate - offset;
  484. cmFilePrintf(f,"{ label: \"%i\" type: \"mk\" ref: \"mf-0\" offset: %8i smpCnt:%8i trackId: 0 textStr: \"Bar %3i\" bar: %3i sec:\"%3i\" }\n",bar,offset,smpCnt,bar,bar,bar);
  485. }
  486. cmFileClose(&f);
  487. return rc;
  488. }
  489. cmSmgRC_t cmScoreMatchGraphicUpdateMidiFromScore( cmCtx_t* ctx, cmSmgH_t h, const cmChar_t* newMidiFn )
  490. {
  491. cmSmgRC_t rc = kOkSmgRC;
  492. cmSmg_t* p = _cmSmgHandleToPtr(h);
  493. unsigned i = 0;
  494. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  495. cmScH_t scH = cmScNullHandle;
  496. if( cmMidiFileOpen(ctx, &mfH, p->mfFn ) != kOkMfRC )
  497. return cmErrMsg(&p->err,kMidiFileFailSmgRC,"MIDI file open failed on '%s'.",cmStringNullGuard(p->mfFn));
  498. if( cmScoreInitialize(ctx,&scH,p->scFn,44100.0, NULL, 0, NULL, NULL, cmSymTblNullHandle ) != kOkScRC )
  499. {
  500. rc = cmErrMsg(&p->err,kScoreFailSmgRC,"Score initializatio failed on '%s'.",cmStringNullGuard(p->scFn));
  501. goto errLabel;
  502. }
  503. for(i=0; i<p->mN; ++i)
  504. {
  505. cmSmgMidi_t* mr = p->mV + i;
  506. // only update midi events which were matched exactly once
  507. if( mr->matchV==NULL || mr->matchV->link!=NULL )
  508. continue;
  509. // locate the matched score event
  510. const cmScoreEvt_t* s= cmScoreIdToEvt( scH, mr->matchV->score->csvEventId );
  511. assert( s!=NULL );
  512. // assign the score velocity to the MIDI file
  513. if(cmMidiFileSetVelocity( mfH, mr->uid, s->vel ) != kOkMfRC )
  514. {
  515. rc = cmErrMsg(&p->err,kOpFailSmgRC,"Set velocify operation failed.");
  516. goto errLabel;
  517. }
  518. }
  519. // write the updated MIDI file
  520. if( cmMidiFileWrite( mfH, newMidiFn ) != kOkMfRC )
  521. {
  522. rc = cmErrMsg(&p->err,kMidiFileFailSmgRC,"MIDI file write failed on '%s'.",cmStringNullGuard(newMidiFn));
  523. goto errLabel;
  524. }
  525. errLabel:
  526. cmMidiFileClose(&mfH);
  527. cmScoreFinalize(&scH);
  528. return rc;
  529. }