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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. cmSmgSc_t* scV; // scV[scN] score bars and notes
  84. unsigned scN;
  85. cmSmgLoc_t* locV; // locV[locN] score locations (from the score file)
  86. unsigned locN;
  87. cmSmgLine_t* lines; // Graphic lines used to indicate that a midi event matches to multiple score notes.
  88. // (Each match after the first gets a line from the box representing the midi event
  89. // to the matching score event)
  90. cmSmgMidi_t* mV; // mV[mN] midi note-on events
  91. unsigned mN;
  92. double mfDurSecs; // midi file duration in seconds
  93. unsigned boxW; // graphic box width and height
  94. unsigned boxH;
  95. } cmSmg_t;
  96. cmSmgH_t cmSmgNullHandle = cmSTATIC_NULL_HANDLE;
  97. cmSmg_t* _cmSmgHandleToPtr( cmSmgH_t h )
  98. {
  99. cmSmg_t* p = (cmSmg_t*)h.h;
  100. assert(p!=NULL);
  101. return p;
  102. }
  103. cmSmgRC_t _cmSmgFree( cmSmg_t* p )
  104. {
  105. unsigned i;
  106. for(i=0; i<p->mN; ++i)
  107. {
  108. cmSmgMatch_t* m0 = p->mV[i].matchV;
  109. cmSmgMatch_t* m1 = NULL;
  110. while(m0!=NULL)
  111. {
  112. m1 = m0->link;
  113. cmMemFree(m0);
  114. m0 = m1;
  115. }
  116. }
  117. for(i=0; i<p->locN; ++i)
  118. {
  119. cmSmgBox_t* b0 = p->locV[i].bV;
  120. cmSmgBox_t* b1 = NULL;
  121. while(b0!=NULL)
  122. {
  123. b1 = b0->link;
  124. cmMemFree(b0->text0);
  125. cmMemFree(b0->text1);
  126. cmMemFree(b0);
  127. b0 = b1;
  128. }
  129. }
  130. cmSmgLine_t* l0 = p->lines;
  131. cmSmgLine_t* l1 = NULL;
  132. while( l0 != NULL )
  133. {
  134. l1 = l0->link;
  135. cmMemFree(l0);
  136. l0 = l1;
  137. }
  138. cmMemFree(p->scV);
  139. cmMemFree(p->mV);
  140. cmMemFree(p->locV);
  141. cmMemFree(p);
  142. return kOkSmgRC;
  143. }
  144. cmSmgBox_t* _cmSmgInsertBox( cmSmg_t* p, unsigned locIdx, unsigned flags, unsigned id, cmChar_t* text0, cmChar_t* text1 )
  145. {
  146. assert( locIdx < p->locN );
  147. cmSmgBox_t* b = cmMemAllocZ(cmSmgBox_t,1);
  148. b->flags = flags;
  149. b->id = id;
  150. b->text0 = text0;
  151. b->text1 = text1;
  152. if( p->locV[locIdx].bV == NULL )
  153. {
  154. p->locV[locIdx].bV = b;
  155. }
  156. else
  157. {
  158. cmSmgBox_t* b0 = p->locV[locIdx].bV;
  159. while( b0->link!=NULL )
  160. b0 = b0->link;
  161. b0->link = b;
  162. }
  163. return b;
  164. }
  165. cmSmgRC_t _cmSmgInitFromScore( cmCtx_t* ctx, cmSmg_t* p, const cmChar_t* scoreFn )
  166. {
  167. cmSmgRC_t rc = kOkSmgRC;
  168. cmScH_t scH = cmScNullHandle;
  169. unsigned i,j,k;
  170. if( cmScoreInitialize(ctx,&scH,scoreFn,44100.0, NULL, 0, NULL, NULL, cmSymTblNullHandle ) != kOkScRC )
  171. return cmErrMsg(&p->err,kScoreFailSmgRC,"Score initializatio failed on '%s'.",cmStringNullGuard(scoreFn));
  172. p->scN = cmScoreEvtCount(scH);
  173. p->scV = cmMemAllocZ(cmSmgSc_t,p->scN);
  174. p->locN = cmScoreLocCount(scH);
  175. p->locV = cmMemAllocZ(cmSmgLoc_t,p->locN);
  176. // for each score location
  177. for(i=0,k=0; i<cmScoreLocCount(scH); ++i)
  178. {
  179. cmScoreLoc_t* l = cmScoreLoc(scH,i);
  180. // insert the location label box
  181. _cmSmgInsertBox(p, i, kLocSmgFl, cmInvalidId, cmTsPrintfP(NULL,"%i",i), NULL );
  182. // for each event in location i
  183. for(j=0; j<l->evtCnt; ++j)
  184. {
  185. const cmScoreEvt_t* e = l->evtArray[j];
  186. switch( e->type)
  187. {
  188. case kBarEvtScId:
  189. case kNonEvtScId:
  190. {
  191. // Note: Mark all score boxes as 'no-match' - this will be cleared in cmScoreMatchGraphicInsertMidi().
  192. unsigned flags = kNoMatchSmgFl | (e->type==kNonEvtScId ? kNoteSmgFl : kBarSmgFl);
  193. cmChar_t* text = NULL;
  194. assert( k < p->scN );
  195. p->scV[k].type = e->type;
  196. p->scV[k].csvEventId = e->csvEventId;
  197. p->scV[k].locIdx = i;
  198. p->scV[k].barNumb = e->barNumb;
  199. if( e->type == kBarEvtScId )
  200. text = cmTsPrintfP(NULL,"%i",e->barNumb);
  201. else
  202. text = cmMemAllocStr( cmMidiToSciPitch( e->pitch, NULL, 0));
  203. p->scV[k].box = _cmSmgInsertBox(p, i, flags, e->csvEventId, text, NULL );
  204. k += 1;
  205. }
  206. break;
  207. }
  208. }
  209. }
  210. p->scN = k;
  211. cmScoreFinalize(&scH);
  212. return rc;
  213. }
  214. cmSmgRC_t _cmSmgInitFromMidi( cmCtx_t* ctx, cmSmg_t* p, const cmChar_t* midiFn )
  215. {
  216. cmSmgRC_t rc = kOkSmgRC;
  217. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  218. unsigned i,j;
  219. if( cmMidiFileOpen(ctx, &mfH, midiFn ) != kOkMfRC )
  220. return cmErrMsg(&p->err,kMidiFileFailSmgRC,"MIDI file open failed on '%s'.",cmStringNullGuard(midiFn));
  221. const cmMidiTrackMsg_t** mV = cmMidiFileMsgArray(mfH);
  222. unsigned mN = cmMidiFileMsgCount(mfH);
  223. p->mV = cmMemAllocZ(cmSmgMidi_t,mN);
  224. p->mN = mN;
  225. p->mfDurSecs = cmMidiFileDurSecs(mfH);
  226. for(i=0,j=0; i<mN; ++i)
  227. if( (mV[i]!=NULL) && cmMidiIsChStatus(mV[i]->status) && cmMidiIsNoteOn(mV[i]->status) && (mV[i]->u.chMsgPtr->d1>0) )
  228. {
  229. assert(j < mN);
  230. p->mV[j].secs = mV[i]->amicro / 1000000.0;
  231. p->mV[j].uid = mV[i]->uid;
  232. p->mV[j].pitch = mV[i]->u.chMsgPtr->d0;
  233. p->mV[j].vel = mV[i]->u.chMsgPtr->d1;
  234. ++j;
  235. }
  236. p->mN = j;
  237. cmMidiFileClose(&mfH);
  238. return rc;
  239. }
  240. cmSmgRC_t cmScoreMatchGraphicAlloc( cmCtx_t* ctx, cmSmgH_t* hp, const cmChar_t* scoreFn, const cmChar_t* midiFn )
  241. {
  242. cmSmgRC_t rc;
  243. if((rc = cmScoreMatchGraphicFree(hp)) != kOkSmgRC )
  244. return rc;
  245. cmSmg_t* p = cmMemAllocZ(cmSmg_t,1);
  246. cmErrSetup(&p->err,&ctx->rpt,"ScoreMatchGraphic");
  247. if((rc = _cmSmgInitFromScore(ctx,p,scoreFn)) != kOkSmgRC )
  248. goto errLabel;
  249. if((rc = _cmSmgInitFromMidi(ctx,p,midiFn)) != kOkSmgRC )
  250. goto errLabel;
  251. p->boxW = 30;
  252. p->boxH = 50;
  253. hp->h = p;
  254. errLabel:
  255. if( rc != kOkSmgRC )
  256. _cmSmgFree(p);
  257. return rc;
  258. }
  259. bool cmScoreMatchGraphicIsValid( cmSmgH_t h )
  260. { return h.h != NULL; }
  261. cmSmgRC_t cmScoreMatchGraphicFree( cmSmgH_t* hp )
  262. {
  263. cmSmgRC_t rc = kOkSmgRC;
  264. if(hp==NULL || cmScoreMatchGraphicIsValid(*hp)==false)
  265. return kOkSmgRC;
  266. cmSmg_t* p = _cmSmgHandleToPtr(*hp);
  267. if((rc = _cmSmgFree(p)) != kOkSmgRC )
  268. return rc;
  269. hp->h = NULL;
  270. return rc;
  271. }
  272. bool cmScoreMatchGraphic( cmSmgH_t h )
  273. { return h.h != NULL; }
  274. cmSmgRC_t cmScoreMatchGraphicInsertMidi( cmSmgH_t h, unsigned midiUid, unsigned midiPitch, unsigned midiVel, unsigned csvScoreEventId )
  275. {
  276. cmSmg_t* p = _cmSmgHandleToPtr(h);
  277. unsigned i,j;
  278. // if this midi event did not match any score records
  279. if( csvScoreEventId == cmInvalidId )
  280. return kOkSmgRC;
  281. assert(midiUid != cmInvalidId );
  282. assert(midiPitch<128 && midiVel<128);
  283. // find the midi file record which matches the event
  284. for(i=0; i<p->mN; ++i)
  285. if( p->mV[i].uid == midiUid )
  286. {
  287. // find the score record which matches the score event id
  288. for(j=0; j<p->scN; ++j)
  289. if( p->scV[j].csvEventId == csvScoreEventId )
  290. {
  291. // create a match record
  292. cmSmgMatch_t* m = cmMemAllocZ(cmSmgMatch_t,1);
  293. m->score = p->scV + j;
  294. // mark the box associated with this score record as 'matched' by clearing the kNoMatchSmgFl
  295. p->scV[j].box->flags = cmClrFlag(p->scV[j].box->flags,kNoMatchSmgFl);
  296. // insert the match record in the midi files match list
  297. if( p->mV[i].matchV == NULL )
  298. p->mV[i].matchV = m;
  299. else
  300. {
  301. cmSmgMatch_t* m0 = p->mV[i].matchV;
  302. while( m0->link != NULL )
  303. m0 = m0->link;
  304. m0->link = m;
  305. }
  306. return kOkSmgRC;
  307. }
  308. return cmErrMsg(&p->err,kScoreFailSmgRC,"The score csv event id %i not found,",csvScoreEventId);
  309. }
  310. return cmErrMsg(&p->err,kMidiFileFailSmgRC,"MIDI uid %i not found.",midiUid);
  311. }
  312. // Create a box for each MIDI event and a line for each
  313. // match beyond the first.
  314. void _cmSmgResolveMidi( cmSmg_t* p )
  315. {
  316. unsigned prevLocIdx = 0;
  317. unsigned i;
  318. // for each midi record
  319. for(i=0; i<p->mN; ++i)
  320. {
  321. // get the first match record for this MIDI event
  322. const cmSmgMatch_t* m = p->mV[i].matchV;
  323. // get the score location for this midi event
  324. unsigned locIdx = m==NULL ? prevLocIdx : m->score->locIdx;
  325. unsigned flags = kMidiSmgFl | (m==NULL ? kNoMatchSmgFl : 0);
  326. // set the text label for this event
  327. cmChar_t* text = cmMemAllocStr( cmMidiToSciPitch( p->mV[i].pitch, NULL, 0));
  328. // insert a box to represent this midi event
  329. cmSmgBox_t* box = _cmSmgInsertBox( p, locIdx, flags, p->mV[i].uid, text, cmTsPrintfP(NULL,"%i",p->mV[i].uid) );
  330. prevLocIdx = locIdx;
  331. // if this midi event matched to multiple score positions
  332. if( m != NULL && m->link != NULL )
  333. {
  334. // insert a line for each match after the first
  335. m = m->link;
  336. for(; m!=NULL; m=m->link )
  337. {
  338. cmSmgLine_t* l = cmMemAllocZ(cmSmgLine_t,1);
  339. l->b0 = box;
  340. l->b1 = m->score->box;
  341. l->link = p->lines;
  342. p->lines = l;
  343. }
  344. }
  345. }
  346. }
  347. void _cmSmgLayout( cmSmg_t* p )
  348. {
  349. unsigned i;
  350. unsigned bordX = 5;
  351. unsigned bordY = 5;
  352. unsigned top = p->boxH + 2*bordY;
  353. unsigned left = bordX;
  354. for(i=0; i<p->locN; ++i)
  355. {
  356. cmSmgLoc_t* l = p->locV + i;
  357. cmSmgBox_t* b = l->bV;
  358. // for each box attached to this location
  359. for(; b!=NULL; b=b->link)
  360. {
  361. // bar boxes are always drawn at the top of the column
  362. if( cmIsFlag(b->flags,kBarSmgFl) )
  363. b->top = bordY;
  364. else
  365. {
  366. b->top = top;
  367. top += p->boxH + bordY;
  368. }
  369. b->left = left;
  370. b->width = p->boxW;
  371. b->height = p->boxH;
  372. }
  373. left += p->boxW + bordX;
  374. top = p->boxH + 2*bordY;
  375. }
  376. }
  377. void _cmSmgSvgSize( cmSmg_t* p, unsigned* widthRef, unsigned* heightRef )
  378. {
  379. unsigned i;
  380. unsigned maxWidth = 0;
  381. unsigned maxHeight = 0;
  382. for(i=0; i<p->locN; ++i)
  383. {
  384. cmSmgBox_t* b = p->locV[i].bV;
  385. for(; b != NULL; b=b->link )
  386. {
  387. if( b->left + b->width > maxWidth )
  388. maxWidth = b->left + b->width;
  389. if( b->top + b->height > maxHeight )
  390. maxHeight = b->top + b->height;
  391. }
  392. }
  393. *widthRef = maxWidth;
  394. *heightRef = maxHeight;
  395. }
  396. cmSmgRC_t cmScoreMatchGraphicWrite( cmSmgH_t h, const cmChar_t* fn )
  397. {
  398. cmSmg_t* p = _cmSmgHandleToPtr(h);
  399. cmFileH_t fH = cmFileNullHandle;
  400. unsigned svgHeight = 0;
  401. unsigned svgWidth = 0;
  402. unsigned i;
  403. // BUG BUG BUG : this can only be called once
  404. // create a box for each midi event
  405. _cmSmgResolveMidi( p );
  406. // layout the boxes
  407. _cmSmgLayout( p );
  408. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  409. return cmErrMsg(&p->err,kFileFailScRC,"Graphic file create failed for '%s'.",cmStringNullGuard(fn));
  410. _cmSmgSvgSize(p,&svgWidth,&svgHeight);
  411. svgHeight += 10; // add a right and lower border
  412. svgWidth += 10;
  413. 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);
  414. for(i=0; i<p->locN; ++i)
  415. {
  416. cmSmgBox_t* b = p->locV[i].bV;
  417. for(; b != NULL; b=b->link )
  418. {
  419. const cmChar_t* classStr = "score";
  420. if( cmIsFlag(b->flags,kLocSmgFl) )
  421. classStr = "loc";
  422. if( cmIsFlag(b->flags,kMidiSmgFl) )
  423. classStr = "midi";
  424. if( cmIsFlag(b->flags,kNoMatchSmgFl) )
  425. if( cmIsFlag(b->flags,kMidiSmgFl) )
  426. classStr = "midi_miss";
  427. if( cmIsFlag(b->flags,kNoMatchSmgFl) )
  428. if( cmIsFlag(b->flags,kNoteSmgFl) )
  429. classStr = "score_miss";
  430. if( cmIsFlag(b->flags,kBarSmgFl) )
  431. classStr = "bar";
  432. 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 )
  433. return cmErrMsg(&p->err,kFileFailScRC,"File write failed on graphic file rect output.");
  434. if( b->text0 != NULL )
  435. {
  436. unsigned tx = b->left + b->width/2;
  437. unsigned ty = b->top + 20;
  438. if( cmFilePrintf(fH,"<text x=\"%i\" y=\"%i\" text-anchor=\"middle\" class=\"stext\">%s</text>\n",tx,ty,b->text0) != kOkFileRC )
  439. return cmErrMsg(&p->err,kFileFailScRC,"File write failed on graphic file text output.");
  440. }
  441. if( b->text1 != NULL )
  442. {
  443. unsigned tx = b->left + b->width/2;
  444. unsigned ty = b->top + 20 + 20;
  445. if( cmFilePrintf(fH,"<text x=\"%i\" y=\"%i\" text-anchor=\"middle\" class=\"stext\">%s</text>\n",tx,ty,b->text1) != kOkFileRC )
  446. return cmErrMsg(&p->err,kFileFailScRC,"File write failed on graphic file text output.");
  447. }
  448. }
  449. }
  450. cmSmgLine_t* l = p->lines;
  451. for(; l!=NULL; l=l->link)
  452. {
  453. unsigned x0 = l->b0->left + l->b0->width/2;
  454. unsigned y0 = l->b0->top + l->b0->height/2;
  455. unsigned x1 = l->b1->left + l->b1->width/2;
  456. unsigned y1 = l->b1->top + l->b1->height/2;
  457. if( cmFilePrintf(fH,"<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\" class=\"sline\"/>\n",x0,y0,x1,y1) != kOkFileRC )
  458. return cmErrMsg(&p->err,kFileFailScRC,"File write failed on graphic file line output.");
  459. }
  460. cmFilePrint(fH,"</svg>\n</body>\n</html>\n");
  461. cmFileClose(&fH);
  462. return kOkSmgRC;
  463. }
  464. cmSmgRC_t cmScoreMatchGraphicGenTimeLineBars( cmSmgH_t h, const cmChar_t* fn, unsigned srate )
  465. {
  466. cmSmgRC_t rc = kOkSmgRC;
  467. cmFileH_t f = cmFileNullHandle;
  468. cmSmg_t* p = _cmSmgHandleToPtr(h);
  469. unsigned i = 0;
  470. if( cmFileOpen(&f,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  471. return cmErrMsg(&p->err,kFileSmgRC,"The time-line bar file '%s' could not be created.",cmStringNullGuard(fn));
  472. for(i=0; i<p->mN; ++i)
  473. 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 )
  474. {
  475. unsigned bar = p->mV[i].matchV->score->barNumb;
  476. unsigned offset = p->mV[i].secs * srate;
  477. unsigned smpCnt = p->mfDurSecs * srate - offset;
  478. 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);
  479. }
  480. cmFileClose(&f);
  481. return rc;
  482. }