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

cmScoreMatchGraphic.c 18KB

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