libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmScoreMatchGraphic.c 21KB

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