libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmTakeSeqBldr.c 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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 "cmJson.h"
  11. #include "cmTime.h"
  12. #include "cmMidi.h"
  13. #include "cmMidiFile.h"
  14. #include "cmAudioFile.h"
  15. #include "cmTimeLine.h"
  16. #include "cmSymTbl.h"
  17. #include "cmScore.h"
  18. #include "cmTakeSeqBldr.h"
  19. // Score track record: Map a score event to a MIDI event.
  20. typedef struct cmScTrkMidiTsb_str
  21. {
  22. unsigned mni; // MIDI note index as an offset from the take marker
  23. unsigned muid; // MIDI file msg unique id
  24. unsigned scEvtIdx; // score event index this note/pedal is assoc'd with or -1 if it did not match
  25. unsigned flags; // flags from cmScMatcherResult_t
  26. } cmScTrkMidiTsb_t;
  27. // Score Tracking info. from a single take (time-line marker)
  28. typedef struct cmScTrkTakeTsb_str
  29. {
  30. unsigned tlMarkerUid; // marker time line uid assoc'd with this take
  31. cmScTrkMidiTsb_t* midiV; // midiV[midiN] score to midi file map recd. array.
  32. unsigned midiN; // count of records in midiV[]
  33. unsigned minMuid; // min MIDI muid in midiV[]
  34. unsigned maxMuid; // max MIDI muid in midiV[]
  35. unsigned minScEvtIdx; // min scEvtIdx in midiV[]
  36. unsigned maxScEvtIdx; // max scEvtIdx in midiV[]
  37. bool failFl;
  38. } cmScTrkTakeTsb_t;
  39. //
  40. typedef struct cmMidiTsb_str
  41. {
  42. unsigned rid; // unique id among all object in this take
  43. unsigned srcId; // marker uid or -1 if this event was manually inserted
  44. unsigned scEvtIdx; // score event assocd with this midi event
  45. unsigned flags; // copy of cmScTrkMidiTsb_t.flags or 0 if not assoc'd with a score event
  46. struct cmMidiTsb_str* ref; // previous MIDI event in time
  47. unsigned offsetSmp; // time offset from *ref
  48. unsigned durSmp; // duration of this MIDI event
  49. unsigned status; // MIDI status value
  50. unsigned d0; // d0 MIDI channel msg data byte 0.
  51. unsigned d1; // d1 MIDI channel msg data byte 1.
  52. struct cmMidiTsb_str* link; // pointer to next MIDI event in list
  53. } cmMidiTsb_t;
  54. // This record contains all the score events and and score synchronized MIDI events
  55. // associated with a given take. Each call to cmTakeSeqBldrLoadTake() creates
  56. // one of these records.
  57. typedef struct cmTakeTsb_str
  58. {
  59. unsigned tlMarkerUid; // time-line marker uid associated with this take
  60. cmScTrkTakeTsb_t* stt; // score tracking info
  61. cmMidiTsb_t* midi; // midi events contained by this take
  62. struct cmTakeTsb_str* link;
  63. } cmTakeTsb_t;
  64. typedef struct
  65. {
  66. cmCtx_t ctx; // application context
  67. cmErr_t err; // internal error object
  68. cmJsonH_t jsH; // JSON tree used to hold score tracker info.
  69. const cmChar_t* tlFn; // time line filename
  70. const cmChar_t* scFn; // score file name
  71. const cmChar_t* tlPrefixPath; // path to time line audio and MIDI files
  72. cmTlH_t tlH; // time-line handle
  73. cmScH_t scH; // score handle
  74. cmScTrkTakeTsb_t* scTrkTakeV; // score tracker file info scTrkTakeV[ scTrkTakeN ]
  75. unsigned scTrkTakeN; // (one record per take)
  76. cmTakeTsb_t* takes; // list of loaded takes
  77. cmTakeTsb_t* manual; // list of manually inserted MIDI events
  78. cmTakeTsb_t* out; // render list
  79. cmMidiTsb_t* evt; // next event to play from the render list
  80. unsigned absPlaySmp; // absolute sample index of the clock
  81. unsigned absEvtSmp; // absolute sample index of the next event to play
  82. unsigned nextRid;
  83. cmMidiTsb_t* rend;
  84. } cmTsb_t;
  85. cmTakeSeqBldrH_t cmTakeSeqBldrNullHandle = cmSTATIC_NULL_HANDLE;
  86. cmTsb_t* _cmTsbHandleToPtr( cmTakeSeqBldrH_t h )
  87. {
  88. cmTsb_t* p = (cmTsb_t*)h.h;
  89. return p;
  90. }
  91. //-------------------------------------------------------------------------------------------------------------------------
  92. typedef struct cmOrderTsb_str
  93. {
  94. cmTakeTsb_t* t;
  95. unsigned minScEvtIdx; // take begin
  96. unsigned maxScEvtIdx; // take end
  97. unsigned begScEvtIdx; // render begin (render beg/end is always contained
  98. unsigned endScEvtIdx; // render end withing min/maxScEvtIdx)
  99. struct cmOrderTsb_str* link;
  100. } cmOrderTsb_t;
  101. // Notes:
  102. // 1) begScEvtIdx - endScEvtIdx is always contained within minScEvtIdx-maxScEvtIdx.
  103. // 2) If begScEvtIdx == cmInvalidIdx then the take was entirely contained inside the
  104. // previous take and is therefore skipped over.
  105. // Free a linked list of cmOrderTsb_t records
  106. void _cmTakeSeqBldrFreeOrder( cmOrderTsb_t* o )
  107. {
  108. while(o!=NULL)
  109. {
  110. cmOrderTsb_t* o0 = o->link;
  111. cmMemFree(o);
  112. o = o0;
  113. }
  114. }
  115. // Create a linked list of cmOrderTsb_t records from the p->takes list.
  116. cmOrderTsb_t* _cmTakeSeqBldrAllocOrder( cmTsb_t* p )
  117. {
  118. cmOrderTsb_t* o = NULL;
  119. cmOrderTsb_t* o0 = NULL;
  120. cmTakeTsb_t* t = p->takes;
  121. // create a list of order records - one per take
  122. for(; t!=NULL; t=t->link)
  123. if( t->stt != NULL )
  124. {
  125. cmOrderTsb_t* o1 = cmMemAllocZ(cmOrderTsb_t,1);
  126. o1->t = t;
  127. o1->minScEvtIdx = t->stt->minScEvtIdx;
  128. o1->maxScEvtIdx = t->stt->maxScEvtIdx;
  129. o1->begScEvtIdx = o1->minScEvtIdx;
  130. o1->endScEvtIdx = o1->maxScEvtIdx;
  131. if( o0 == NULL )
  132. o = o1;
  133. else
  134. o0->link = o1;
  135. o0 = o1;
  136. }
  137. return o;
  138. }
  139. // Sort a list of cmOrderTsb_t records on minScEvtIdx.
  140. cmOrderTsb_t* _cmTakeSeqBldrSortOrder( cmOrderTsb_t* o )
  141. {
  142. cmOrderTsb_t* beg = NULL;
  143. cmOrderTsb_t* end = NULL;
  144. cmOrderTsb_t* m = NULL;
  145. // while elements remain on the unordered list
  146. while( o != NULL )
  147. {
  148. cmOrderTsb_t* c0 = NULL;
  149. cmOrderTsb_t* c1 = o->link;
  150. m = o;
  151. // set m to point to the recd with the min minScEvtIdx
  152. for(; c1!=NULL; c1=c1->link )
  153. if( c1->minScEvtIdx < m->minScEvtIdx )
  154. m = c1;
  155. // for each recd on the unordered list
  156. for(c1=o; c1!=NULL; c1=c1->link)
  157. {
  158. // if this is the min record
  159. if( c1==m )
  160. {
  161. // remove the min recd from this list
  162. if( c0 == NULL )
  163. o = c1->link;
  164. else
  165. c0->link = c1->link;
  166. c1->link = NULL;
  167. break;
  168. }
  169. c0 = c1;
  170. }
  171. // c1 now points to the min record
  172. assert(c1==m);
  173. if( end == NULL )
  174. beg = end = m;
  175. else
  176. {
  177. end->link = m;
  178. end = m;
  179. }
  180. }
  181. return beg;
  182. }
  183. // Set the begScEvtIdx and endScEvtIdx fields to settle conflicts
  184. // between overlapping takes.
  185. void _cmTakeSeqBldrSetBegEndOrder( cmOrderTsb_t* o )
  186. {
  187. if( o == NULL )
  188. return;
  189. // Earlier takes have priority over later takes so the first
  190. // take has highest priority and therefore it's beg=min and end=max.
  191. o->begScEvtIdx = o->minScEvtIdx;
  192. o->endScEvtIdx = o->maxScEvtIdx;
  193. cmOrderTsb_t* o0 = o;
  194. cmOrderTsb_t* o1 = o->link;
  195. while( o1!=NULL)
  196. {
  197. bool skipFl = false;
  198. // if this take begins after the previous take
  199. if( o1->minScEvtIdx > o0->endScEvtIdx )
  200. {
  201. o1->begScEvtIdx = o1->minScEvtIdx;
  202. o1->endScEvtIdx = o1->maxScEvtIdx;
  203. }
  204. else
  205. {
  206. // if this take is entirely contained in the previous take
  207. // then this take is skipped.
  208. if( o0->endScEvtIdx > o1->maxScEvtIdx )
  209. {
  210. o1->begScEvtIdx = cmInvalidIdx;
  211. o1->endScEvtIdx = cmInvalidIdx;
  212. skipFl = true;
  213. }
  214. else // this take overlaps with the previous take
  215. {
  216. o1->begScEvtIdx = o0->endScEvtIdx + 1;
  217. o1->endScEvtIdx = o1->maxScEvtIdx;
  218. }
  219. }
  220. if( skipFl )
  221. {
  222. // the current take is being skipped so do not change o0
  223. o1 = o1->link;
  224. }
  225. else
  226. {
  227. // advance to the next take
  228. o0 = o1;
  229. o1 = o0->link;
  230. }
  231. }
  232. }
  233. void _cmTakeSeqBldrPrintOrder( cmOrderTsb_t* o )
  234. {
  235. int i;
  236. for(i=0; o!=NULL; o=o->link,++i)
  237. printf("%i : %p min:%i max:%i beg:%i end:%i\n",i, o->t, o->minScEvtIdx, o->maxScEvtIdx, o->begScEvtIdx, o->endScEvtIdx);
  238. printf("\n");
  239. }
  240. //-------------------------------------------------------------------------------------------------------------------------
  241. typedef struct
  242. {
  243. cmScoreEvt_t* sep;
  244. unsigned absSmp;
  245. } cmTsbTempo_t;
  246. void _cmTsbTempoPrint( cmTsbTempo_t* r, unsigned rN )
  247. {
  248. unsigned i;
  249. for(i=0; i<rN; ++i)
  250. printf("%s %i\n",cmScEvtTypeIdToLabel(r[i].sep->type),r[i].absSmp);
  251. printf("\n");
  252. }
  253. cmTsbTempo_t* _cmTsbTempoFind( cmTsbTempo_t* r, unsigned rN, unsigned scEvtIdx )
  254. {
  255. unsigned i;
  256. for(i=0; i<rN; ++i)
  257. if( r[i].sep->index == scEvtIdx )
  258. return r + i;
  259. return NULL;
  260. }
  261. // Starting at r[ri] calc the tempo for the next two bars.
  262. unsigned _cmTsbTempoCalc( cmTsbTempo_t* r, unsigned rN, unsigned ri, double srate )
  263. {
  264. unsigned durBarCnt = 2;
  265. unsigned barIdx = 0;
  266. double beats = 0;
  267. unsigned abs0Smp = -1;
  268. unsigned abs1Smp = -1;
  269. for(; ri<rN; ++ri)
  270. {
  271. // if this is a note event
  272. if( r[ri].sep->type == kNonEvtScId )
  273. {
  274. beats += r[ri].sep->frac;
  275. if( abs0Smp == -1 )
  276. abs0Smp = r[ri].absSmp;
  277. if( r[ri].absSmp != -1 )
  278. abs1Smp = r[ri].absSmp;
  279. }
  280. // if this is a bar event
  281. if( r[ri].sep->type == kBarEvtScId )
  282. {
  283. barIdx += 1;
  284. if( barIdx == durBarCnt )
  285. break;
  286. }
  287. }
  288. double durSmp = abs1Smp - abs0Smp;
  289. if( durSmp == 0 )
  290. return 0;
  291. return (unsigned)round(beats / (durSmp / (srate * 60.0)));
  292. }
  293. cmTsbRC_t _cmTsbCalcTempo( cmTsb_t* p, cmTakeTsb_t* t, unsigned* begBpSRef, unsigned* endBpSRef )
  294. {
  295. unsigned i,j;
  296. assert( begBpSRef != NULL && endBpSRef != NULL );
  297. *begBpSRef = 0;
  298. *endBpSRef = 0;
  299. if( t->stt == NULL )
  300. return cmErrMsg(&p->err,kMissingScTrkTsbRC,"The tempo of takes without score-tracking results cannot be calculated.");
  301. if( t->midi == NULL )
  302. return cmErrMsg(&p->err,kInvalidArgTsbRC,"The tempo of a take without MIDI events cannot be estimated.");
  303. // allocate an array to with one record per score event in the take
  304. unsigned rN = t->stt->maxScEvtIdx - t->stt->minScEvtIdx + 1;
  305. cmTsbTempo_t r[ rN ];
  306. // assign a score event pointer to each record
  307. for(i=0; i<rN; ++i)
  308. {
  309. r[i].sep = cmScoreEvt(p->scH,t->stt->minScEvtIdx + i);
  310. assert( r[i].sep != NULL );
  311. r[i].absSmp = -1;
  312. }
  313. // use MIDI events with score information to assign absolute
  314. // time information to as many score events as possible.
  315. unsigned absSmp = 0;
  316. cmMidiTsb_t* m = t->midi;
  317. while( m!=NULL )
  318. {
  319. cmTsbTempo_t* r0 = NULL;
  320. if( m->scEvtIdx != cmInvalidIdx && (r0 = _cmTsbTempoFind(r,rN,m->scEvtIdx)) != NULL )
  321. {
  322. assert( r0->sep->type == kNonEvtScId );
  323. r0->absSmp = absSmp;
  324. }
  325. m = m->link;
  326. if( m != NULL )
  327. absSmp += m->offsetSmp;
  328. }
  329. unsigned barCnt = 0;
  330. // assign an absolute time to each bar recd
  331. for(i=0; i<rN; ++i)
  332. if( r[i].sep->type == kBarEvtScId )
  333. {
  334. barCnt += 1; // count the number of bars found
  335. // search ahead from the bar to the next note
  336. unsigned j;
  337. for(j=i; j<rN; ++j)
  338. if( r[j].sep->type == kNonEvtScId )
  339. {
  340. r[i].absSmp = r[j].absSmp; // the bar time is the same as the next
  341. break;
  342. }
  343. }
  344. //_cmTsbTempoPrint(r,rN);
  345. unsigned tempoV[ barCnt ];
  346. for(i=0,j=0; i<rN && j<barCnt; ++i)
  347. {
  348. tempoV[j++] = _cmTsbTempoCalc(r, rN, i, cmTimeLineSampleRate(p->tlH) );
  349. for(; i<rN; ++i)
  350. if( r[i].sep->type == kBarEvtScId )
  351. break;
  352. }
  353. if( j > 0 )
  354. {
  355. *begBpSRef = tempoV[0];
  356. *endBpSRef = tempoV[j-1];
  357. }
  358. printf("Bars:%i ",barCnt);
  359. for(i=0; i<j; ++i)
  360. printf("%i ",tempoV[i]);
  361. printf("\n");
  362. return kOkTsbRC;
  363. }
  364. //-------------------------------------------------------------------------------------------------------------------------
  365. void _cmTsbPrintMidi( cmTakeTsb_t* t )
  366. {
  367. if( t==NULL )
  368. return;
  369. cmMidiTsb_t* m = t->midi;
  370. for(; m!=NULL; m=m->link)
  371. printf("mark:%4i sei:%4i fl:0x%x offs:%8i dur:%8i\n", m->srcId, m->scEvtIdx,m->flags,m->offsetSmp,m->durSmp);
  372. printf("\n");
  373. }
  374. void _cmTsbTakePrint( cmTakeTsb_t* t )
  375. {
  376. for(; t!=NULL; t=t->link)
  377. {
  378. printf("Take:%i %p\n", t->tlMarkerUid, t->stt);
  379. _cmTsbPrintMidi(t);
  380. }
  381. }
  382. cmTsbRC_t _cmTsbScoreTrkFree( cmTsb_t* p )
  383. {
  384. cmTsbRC_t rc = kOkTsbRC;
  385. unsigned i;
  386. if( cmJsonFinalize(&p->jsH) != kOkJsRC )
  387. {
  388. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"JSON object finalize failed.");
  389. goto errLabel;
  390. }
  391. if( p->scTrkTakeV != NULL )
  392. {
  393. for(i=0; i<p->scTrkTakeN; ++i)
  394. cmMemPtrFree(&p->scTrkTakeV[i].midiV);
  395. cmMemPtrFree(&p->scTrkTakeV);
  396. }
  397. if( cmTimeLineFinalize(&p->tlH) != kOkTlRC )
  398. rc = cmErrMsg(&p->err,kTimeLineFailTsbRC,"Time line object finalize failed.");
  399. if( cmScoreFinalize(&p->scH) != kOkScRC )
  400. rc = cmErrMsg(&p->err,kScoreFailTsbRC,"Score finalize failed.");
  401. errLabel:
  402. return rc;
  403. }
  404. // Free a take record. Note that the record must be unlinked
  405. // from p->takes (See _cmTakeTsbUnlink().) prior to calling this function.
  406. void _cmTsbTakeFree( cmTsb_t* p, cmTakeTsb_t** tp )
  407. {
  408. if( tp==NULL || *tp==NULL )
  409. return;
  410. cmMidiTsb_t* m = (*tp)->midi;
  411. while( m != NULL )
  412. {
  413. cmMidiTsb_t* nm = m->link;
  414. cmMemFree(m);
  415. m = nm;
  416. }
  417. cmMemPtrFree(tp);
  418. }
  419. void _cmTsbRendTakeFree( cmTsb_t* p, cmTakeTsb_t** t )
  420. {
  421. _cmTsbTakeFree(p,t);
  422. p->evt = NULL;
  423. p->absPlaySmp = 0;
  424. p->absEvtSmp = 0;
  425. p->nextRid = 0;
  426. p->rend = NULL;
  427. }
  428. // Unlink a 'take' record from p->takes.
  429. cmTakeTsb_t* _cmTsbTakeUnlink( cmTsb_t* p, cmTakeTsb_t* t )
  430. {
  431. cmTakeTsb_t* t0 = NULL;
  432. cmTakeTsb_t* t1 = p->takes;
  433. while( t1 != NULL )
  434. {
  435. if( t1 == t )
  436. {
  437. if( t0 == NULL )
  438. p->takes = t->link;
  439. else
  440. t0->link = t->link;
  441. return t;
  442. }
  443. }
  444. return NULL;
  445. }
  446. cmTsbRC_t _cmTsbFree( cmTsb_t* p )
  447. {
  448. cmTsbRC_t rc = kOkTsbRC;
  449. if((rc = _cmTsbScoreTrkFree(p)) != kOkTsbRC )
  450. goto errLabel;
  451. //_cmTsbTakePrint(p->takes);
  452. //_cmTsbTakePrint(p->out);
  453. cmTakeTsb_t* t = p->takes;
  454. while( t != NULL )
  455. {
  456. cmTakeTsb_t* nt = t->link;
  457. _cmTsbTakeFree(p,&t);
  458. t = nt;
  459. }
  460. _cmTsbRendTakeFree(p,&p->out);
  461. cmMemFree(p);
  462. errLabel:
  463. return rc;
  464. }
  465. cmScTrkTakeTsb_t* _cmTsbMarkerIdToScTrkTake( cmTsb_t* p, unsigned markerUid )
  466. {
  467. unsigned i;
  468. for(i=0; p->scTrkTakeN; ++i)
  469. if( p->scTrkTakeV[i].tlMarkerUid == markerUid )
  470. return p->scTrkTakeV + i;
  471. return NULL;
  472. }
  473. cmScTrkMidiTsb_t* _cmTsbMuidToScTrkMidi( cmScTrkTakeTsb_t* t, unsigned muid )
  474. {
  475. unsigned i;
  476. for(i=0; i<t->midiN; ++i)
  477. if( t->midiV[i].muid == muid )
  478. return t->midiV + i;
  479. return NULL;
  480. }
  481. cmTsbRC_t _cmTsbLoadScoreTrkFile( cmTsb_t* p, const cmChar_t* scoreTrkFn )
  482. {
  483. cmTsbRC_t rc = kOkTsbRC;
  484. cmJsonNode_t* tkArrObj = NULL;
  485. cmJsRC_t jsRC = kOkJsRC;
  486. const cmChar_t* errMsg = NULL;
  487. unsigned i;
  488. // initialize the TSB json object
  489. if(( rc = cmJsonInitializeFromFile(&p->jsH,scoreTrkFn,&p->ctx)) != kOkJsRC )
  490. {
  491. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"The Take Sequence Builder JSON file object could not be initialized from '%s'.",cmStringNullGuard(scoreTrkFn));
  492. goto errLabel;
  493. }
  494. // parse the header
  495. if((jsRC = cmJsonMemberValues( cmJsonRoot(p->jsH), &errMsg,
  496. "timeLineFn", kStringTId, &p->tlFn,
  497. "scoreFn", kStringTId, &p->scFn,
  498. "tlPrefixPath", kStringTId, &p->tlPrefixPath,
  499. "takeArray", kArrayTId | kOptArgJsFl, &tkArrObj,
  500. NULL )) != kOkJsRC )
  501. {
  502. if( jsRC == kNodeNotFoundJsRC && errMsg != NULL )
  503. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file header parse failed missing required field:'%s'",errMsg);
  504. else
  505. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file header parse failed.");
  506. goto errLabel;
  507. }
  508. // count of take records
  509. p->scTrkTakeN = cmJsonChildCount(tkArrObj);
  510. // array of take records
  511. p->scTrkTakeV = cmMemAllocZ(cmScTrkTakeTsb_t,p->scTrkTakeN);
  512. // for each take record
  513. for(i=0; i<p->scTrkTakeN; ++i)
  514. {
  515. cmJsonNode_t* takeObj = NULL;
  516. cmJsonNode_t* noteArrObj = NULL;
  517. cmScTrkTakeTsb_t* t = p->scTrkTakeV + i;
  518. unsigned j;
  519. // get a pointer to the take record JSON object
  520. if((takeObj = cmJsonArrayElement(tkArrObj,i)) == NULL )
  521. {
  522. rc = cmErrMsg(&p->err,kParseFailTsbRC,"Take record header at index %i access failed.",i);
  523. goto errLabel;
  524. }
  525. // parse the take record
  526. if((jsRC = cmJsonMemberValues( takeObj, &errMsg,
  527. "markerUid",kIntTId, &t->tlMarkerUid,
  528. "failFl", kIntTId, &t->failFl,
  529. "array", kArrayTId, &noteArrObj,
  530. NULL)) != kOkJsRC )
  531. {
  532. if( jsRC == kNodeNotFoundJsRC && errMsg != NULL )
  533. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file take record parse failed missing required field:'%s'",errMsg);
  534. else
  535. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file take record parse failed.");
  536. goto errLabel;
  537. }
  538. // get the count of note records
  539. t->midiN = cmJsonChildCount(noteArrObj);
  540. // allocate a note record array for this take
  541. t->midiV = cmMemAllocZ(cmScTrkMidiTsb_t, t->midiN);
  542. t->minMuid = INT_MAX;
  543. t->maxMuid = 0;
  544. t->minScEvtIdx = INT_MAX;
  545. t->maxScEvtIdx = 0;
  546. // for each note record
  547. for(j=0; j<t->midiN; ++j)
  548. {
  549. cmJsonNode_t* noteObj = NULL;
  550. // get the note record JSON object
  551. if((noteObj = cmJsonArrayElement(noteArrObj,j)) == NULL )
  552. {
  553. rc = cmErrMsg(&p->err,kParseFailTsbRC,"Access failed for note record at index %i at take index %i.",j,i);
  554. goto errLabel;
  555. }
  556. // parse the note record
  557. if((jsRC = cmJsonMemberValues( noteObj, &errMsg,
  558. "mni", kIntTId, &t->midiV[j].mni,
  559. "muid", kIntTId, &t->midiV[j].muid,
  560. "scEvtIdx", kIntTId, &t->midiV[j].scEvtIdx,
  561. "flags", kIntTId, &t->midiV[j].flags,
  562. NULL)) != kOkJsRC )
  563. {
  564. if( jsRC == kNodeNotFoundJsRC && errMsg != NULL )
  565. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file note record parse failed missing required field:'%s'",errMsg);
  566. else
  567. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file note record parse failed.");
  568. goto errLabel;
  569. }
  570. if( t->midiV[j].muid < t->minMuid )
  571. t->minMuid = t->midiV[j].muid;
  572. if( t->midiV[j].muid > t->maxMuid )
  573. t->maxMuid = t->midiV[j].muid;
  574. unsigned scEvtIdx = t->midiV[j].scEvtIdx;
  575. if( scEvtIdx!=0 && scEvtIdx!=cmInvalidIdx && scEvtIdx < t->minScEvtIdx )
  576. t->minScEvtIdx = scEvtIdx;
  577. if( scEvtIdx!=0 && scEvtIdx!=cmInvalidIdx && scEvtIdx > t->maxScEvtIdx )
  578. t->maxScEvtIdx = t->midiV[j].scEvtIdx;
  579. }
  580. }
  581. errLabel:
  582. if( rc != kOkTsbRC )
  583. rc = _cmTsbScoreTrkFree(p);
  584. return rc;
  585. }
  586. cmTsbRC_t _cmTakeSeqBldrRender( cmTsb_t* p )
  587. {
  588. cmTsbRC_t rc = kOkTsbRC;
  589. unsigned takeCnt = 0;
  590. unsigned midiCnt = 0;
  591. // delete the previous output rendering
  592. _cmTsbRendTakeFree(p,&p->out);
  593. // allocate a take order list
  594. cmOrderTsb_t* o = _cmTakeSeqBldrAllocOrder( p );
  595. // sort the list by minScEvtIdx
  596. o = _cmTakeSeqBldrSortOrder(o);
  597. // assign beg/endScEvtIdx values to each take
  598. _cmTakeSeqBldrSetBegEndOrder(o);
  599. //_cmTakeSeqBldrPrintOrder(o);
  600. // if the render take has not yet been allocated
  601. p->out = cmMemAllocZ(cmTakeTsb_t,1);
  602. p->out->tlMarkerUid = cmInvalidId;
  603. p->nextRid = 0;
  604. cmMidiTsb_t* m0 = NULL;
  605. cmOrderTsb_t* t = o;
  606. for(; t!=NULL; t=t->link)
  607. {
  608. // skip takes whose begScEvtIdx is not valid
  609. if( t->begScEvtIdx == cmInvalidIdx )
  610. continue;
  611. takeCnt += 1;
  612. //_cmTsbPrintMidi(t->t);
  613. // advance to the MIDI event assoc'd with t->begScEvtIdx
  614. cmMidiTsb_t* m = t->t->midi;
  615. for(; m!=NULL; m=m->link)
  616. if( m->scEvtIdx >= t->begScEvtIdx )
  617. break;
  618. // copy the MIDI events from the take into the render list
  619. for(; m!=NULL; m=m->link)
  620. {
  621. midiCnt += 1;
  622. // allocate a MIDI record to hold this render event
  623. cmMidiTsb_t* m1 = cmMemAllocZ(cmMidiTsb_t,1);
  624. // copy in the MIDI record
  625. *m1 = *m;
  626. //memcpy(m1,m,sizeof(cmMidiTsb_t));
  627. m1->link= NULL; // NULLify the copied link
  628. m1->ref = m0; // set prev. link
  629. m1->rid = p->nextRid++; // set the unique id
  630. // if this is not the first MIDI event ...
  631. if( m0 != NULL )
  632. m0->link = m1; // then set next link on the previous record
  633. else //
  634. { // otherwise
  635. p->out->midi = m1; // 1) set the render take event list
  636. m1->offsetSmp = 0; // 2) the first event always starts at time zero.
  637. p->evt = m1; // 3) set the first event to play
  638. p->absPlaySmp = 0; // 4) set the current play clock to 0
  639. p->absEvtSmp = 0; // 5) set the current next play event time to 0
  640. }
  641. m0 = m1;
  642. // if this is the last event in this take
  643. if( m0->scEvtIdx == t->endScEvtIdx )
  644. break;
  645. }
  646. }
  647. // free the take order list
  648. _cmTakeSeqBldrFreeOrder(o);
  649. printf("rendered takes:%i events:%i\n",takeCnt,midiCnt);
  650. //_cmTsbPrintMidi(p->out);
  651. return rc;
  652. }
  653. cmTsbRC_t cmTakeSeqBldrAlloc( cmCtx_t* ctx, cmTakeSeqBldrH_t* hp )
  654. {
  655. cmTsbRC_t rc;
  656. if((rc = cmTakeSeqBldrFree(hp)) != kOkTsbRC )
  657. return kOkTsbRC;
  658. cmTsb_t* p = cmMemAllocZ(cmTsb_t,1);
  659. cmErrSetup(&p->err,&ctx->rpt,"TakeSeqBldr");
  660. p->ctx = *ctx;
  661. hp->h = p;
  662. return rc;
  663. }
  664. cmTsbRC_t cmTakeSeqBldrAllocFn( cmCtx_t* ctx, cmTakeSeqBldrH_t* hp, const cmChar_t* scoreTrkFn )
  665. {
  666. cmTsbRC_t rc;
  667. if((rc = cmTakeSeqBldrAlloc(ctx,hp)) != kOkTsbRC )
  668. return rc;
  669. if((rc = cmTakeSeqBldrInitialize(*hp,scoreTrkFn)) != kOkTsbRC )
  670. return rc;
  671. return rc;
  672. }
  673. cmTsbRC_t cmTakeSeqBldrFree( cmTakeSeqBldrH_t* hp )
  674. {
  675. cmRC_t rc = kOkTsbRC;
  676. if( hp == NULL || cmTakeSeqBldrIsValid(*hp)==false )
  677. return kOkTsbRC;
  678. cmTsb_t* p = _cmTsbHandleToPtr(*hp);
  679. if((rc = _cmTsbFree(p)) != kOkTsbRC )
  680. return rc;
  681. hp->h = NULL;
  682. return rc;
  683. }
  684. bool cmTakeSeqBldrIsValid( cmTakeSeqBldrH_t h )
  685. { return h.h != NULL; }
  686. cmTsbRC_t cmTakeSeqBldrInitialize( cmTakeSeqBldrH_t h, const cmChar_t* scoreTrkFn )
  687. {
  688. cmTsbRC_t rc = kOkTsbRC;
  689. cmTsb_t* p = _cmTsbHandleToPtr(h);
  690. if(( rc = _cmTsbLoadScoreTrkFile( p, scoreTrkFn )) != kOkTsbRC )
  691. return rc;
  692. if( cmTimeLineInitializeFromFile(&p->ctx, &p->tlH, NULL, NULL, p->tlFn, p->tlPrefixPath ) != kOkTlRC )
  693. {
  694. rc = cmErrMsg(&p->err,kTimeLineFailTsbRC,"The time-line file '%s' could not be loaded.",p->tlFn);
  695. goto errLabel;
  696. }
  697. if( cmScoreInitialize(&p->ctx, &p->scH, p->scFn, 0, NULL, 0, NULL, NULL, cmSymTblNullHandle ) != kOkScRC )
  698. {
  699. rc = cmErrMsg(&p->err,kScoreFailTsbRC,"The score file '%s' could not be loaded.",p->scFn);
  700. goto errLabel;
  701. }
  702. errLabel:
  703. if( rc != kOkTsbRC )
  704. _cmTsbScoreTrkFree(p);
  705. return rc;
  706. }
  707. cmTakeTsb_t* _cmTsbMarkerUidToTake( cmTsb_t* p, unsigned tlMarkerUid )
  708. {
  709. cmTakeTsb_t* t = p->takes;
  710. for(; t != NULL; t=t->link)
  711. if( t->tlMarkerUid == tlMarkerUid )
  712. return t;
  713. return NULL;
  714. }
  715. cmTsbRC_t cmTakeSeqBldrLoadTake( cmTakeSeqBldrH_t h, unsigned tlMarkUid, bool overwriteFL )
  716. {
  717. cmTsbRC_t rc = kOkTsbRC;
  718. cmTsb_t* p = _cmTsbHandleToPtr(h);
  719. cmTlMarker_t* mark = NULL;
  720. cmTlMidiFile_t* mf = NULL;
  721. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  722. cmScTrkTakeTsb_t* stt = NULL;
  723. // verify that the requested take has not already been loaded
  724. if( _cmTsbMarkerUidToTake( p, tlMarkUid ) != NULL )
  725. {
  726. rc = cmErrMsg(&p->err,kInvalidArgTsbRC,"The take indicated by marker id %i has already been loaded.",tlMarkUid );
  727. goto errLabel;
  728. }
  729. // find the score tracked take for the requested marker
  730. if((stt = _cmTsbMarkerIdToScTrkTake(p,tlMarkUid )) == NULL )
  731. {
  732. rc = cmErrMsg(&p->err,kInvalidArgTsbRC,"The score tracked take indicated by marker id %i could not be found.", tlMarkUid );
  733. goto errLabel;
  734. }
  735. // get a pointer to the time-line marker object
  736. if((mark = cmTlMarkerObjPtr( p->tlH, cmTimeLineIdToObj( p->tlH, cmInvalidId, tlMarkUid))) == NULL )
  737. {
  738. rc = cmErrMsg(&p->err,kInvalidArgTsbRC,"The time-line marker uid '%i' is not valid.",tlMarkUid);
  739. goto errLabel;
  740. }
  741. // get the name of the MIDI file which contains the marker
  742. if((mf = cmTimeLineMidiFileAtTime( p->tlH, mark->obj.seqId, mark->obj.seqSmpIdx )) == NULL )
  743. {
  744. rc = cmErrMsg(&p->err,kInvalidArgTsbRC,"The time-line marker '%i' does not intersect with a MIDI file.",tlMarkUid);
  745. goto errLabel;
  746. }
  747. // open the MIDI file
  748. if( cmMidiFileOpen( cmMidiFileName(mf->h), &mfH, &p->ctx ) != kOkMfRC )
  749. {
  750. rc = cmErrMsg(&p->err,kInvalidArgTsbRC,"The MIDI file '%s' could not be opened.", cmStringNullGuard(cmMidiFileName(mf->h)));
  751. goto errLabel;
  752. }
  753. // convert the dtick field to delta samples
  754. cmMidiFileTickToSamples( mfH, cmTimeLineSampleRate(p->tlH), false );
  755. // calculate MIDI note and pedal durations (see cmMidiChMsg_t.durTicks)
  756. cmMidiFileCalcNoteDurations( mfH );
  757. unsigned i = 0;
  758. unsigned n = cmMidiFileMsgCount(mfH);
  759. const cmMidiTrackMsg_t** a = cmMidiFileMsgArray(mfH);
  760. // allocate and link a new take render record
  761. cmTakeTsb_t* t = cmMemAllocZ(cmTakeTsb_t,1);
  762. t->tlMarkerUid = tlMarkUid;
  763. t->stt = stt;
  764. t->link = p->takes;
  765. p->takes = t;
  766. unsigned rid = 0;
  767. cmMidiTsb_t* m0 = NULL;
  768. const cmMidiTrackMsg_t* mf0 = NULL;
  769. // for each MIDI message in the file
  770. for(i=0; i<n; ++i)
  771. {
  772. const cmMidiTrackMsg_t* mf1 = a[i];
  773. // we are only interested in rendering notes and control msgs
  774. switch( mf1->status )
  775. {
  776. case kNoteOnMdId:
  777. case kNoteOffMdId:
  778. case kCtlMdId:
  779. break;
  780. default:
  781. continue;
  782. }
  783. // if this MIDI message is inside the tracked region of the take
  784. if( stt->minMuid > mf1->uid || mf1->uid > stt->maxMuid )
  785. continue;
  786. // get a pointer to the tracking map for the given MIDI file event.
  787. // (Note that since control messages are not tracked so this function may return NULL.)
  788. cmScTrkMidiTsb_t* stm = _cmTsbMuidToScTrkMidi(stt, mf1->uid );
  789. // create a MIDI render event
  790. cmMidiTsb_t* m1 = cmMemAllocZ(cmMidiTsb_t,1);
  791. m1->rid = rid++;
  792. m1->srcId = tlMarkUid;
  793. m1->scEvtIdx = stm != NULL ? stm->scEvtIdx : cmInvalidIdx;
  794. m1->flags = stm != NULL ? stm->flags : 0;
  795. m1->ref = m0;
  796. m1->offsetSmp = mf0 == NULL ? 0 : mf1->dtick;
  797. m1->durSmp = mf1->u.chMsgPtr->durTicks;
  798. m1->d0 = mf1->u.chMsgPtr->d0;
  799. m1->d1 = mf1->u.chMsgPtr->d1;
  800. m1->status = mf1->status;
  801. m1->link = NULL;
  802. //printf("0x%x %f %f\n",m1->status,m1->offsetSmp/96000.0,m1->durSmp/96000.0);
  803. if( m0 != NULL )
  804. m0->link = m1;
  805. if( t->midi == NULL )
  806. t->midi = m1;
  807. m0 = m1;
  808. mf0 = mf1;
  809. }
  810. //unsigned begBpS, endBpS;
  811. //_cmTsbCalcTempo(p, t, &begBpS, &endBpS );
  812. // render the new output sequence
  813. if((rc = _cmTakeSeqBldrRender(p)) != kOkTsbRC)
  814. cmErrMsg(&p->err,rc,"Take sequence builder rendering failed.");
  815. errLabel:
  816. if( cmMidiFileClose(&mfH) != kOkMfRC )
  817. rc = cmErrMsg(&p->err,kMidiFileFailTsbRC,"MIDI file close failed.");
  818. return rc;
  819. }
  820. cmTsbRC_t cmTakeSeqBldrUnloadTake( cmTakeSeqBldrH_t h, unsigned tlMarkUid )
  821. {
  822. cmTsbRC_t rc = kOkTsbRC;
  823. cmTsb_t* p = _cmTsbHandleToPtr(h);
  824. cmTakeTsb_t* t;
  825. if((t = _cmTsbMarkerUidToTake(p, tlMarkUid )) == NULL )
  826. return cmErrMsg(&p->err,kInvalidArgTsbRC,"The take indicated by marker id %i could not be found.",tlMarkUid);
  827. t = _cmTsbTakeUnlink(p,t);
  828. assert( t != NULL );
  829. _cmTsbTakeFree(p,&t);
  830. // render the new output sequence
  831. if((rc = _cmTakeSeqBldrRender(p)) != kOkTsbRC)
  832. cmErrMsg(&p->err,rc,"Take sequence builder rendering failed.");
  833. return rc;
  834. }
  835. double cmTakeSeqBldrSampleRate( cmTakeSeqBldrH_t h )
  836. {
  837. cmTsb_t* p = _cmTsbHandleToPtr(h);
  838. if( cmTimeLineIsValid( p->tlH ) )
  839. return cmTimeLineSampleRate(p->tlH );
  840. return 0;
  841. }
  842. cmScH_t cmTakeSeqBldrScoreHandle( cmTakeSeqBldrH_t h )
  843. {
  844. cmTsb_t* p = _cmTsbHandleToPtr(h);
  845. return p->scH;
  846. }
  847. unsigned cmTakeSeqBldrScTrkTakeCount( cmTakeSeqBldrH_t h )
  848. {
  849. cmTsb_t* p = _cmTsbHandleToPtr(h);
  850. return p->scTrkTakeN;
  851. }
  852. cmTsbRC_t cmTakeSeqBldrScTrkTake( cmTakeSeqBldrH_t h, unsigned idx, cmTksbScTrkTake_t* ref )
  853. {
  854. cmTsb_t* p = _cmTsbHandleToPtr(h);
  855. assert( idx < p->scTrkTakeN );
  856. ref->minScEvtIdx = p->scTrkTakeV[ idx ].minScEvtIdx;
  857. ref->maxScEvtIdx = p->scTrkTakeV[ idx ].maxScEvtIdx;
  858. ref->tlMarkerUid = p->scTrkTakeV[idx].tlMarkerUid;
  859. return kOkTsbRC;
  860. }
  861. const cmChar_t* cmTakeSeqBldrScTrkTakeText( cmTakeSeqBldrH_t h, unsigned tlMarkerUid )
  862. {
  863. cmTsb_t* p = _cmTsbHandleToPtr(h);
  864. cmTlObj_t* tlObj = NULL;
  865. cmTlMarker_t* tlMark = NULL;
  866. if((tlObj = cmTimeLineIdToObj( p->tlH, cmInvalidId, tlMarkerUid )) == NULL )
  867. {
  868. cmErrMsg(&p->err,kTimeLineFailTsbRC,"Unable to locate the time line record for score-track-take uid: %i.",tlMarkerUid);
  869. return NULL;
  870. }
  871. if((tlMark = cmTimeLineMarkerObjPtr( p->tlH, tlObj)) == NULL )
  872. {
  873. cmErrMsg(&p->err,kTimeLineFailTsbRC,"Unable to cast a time line marker object for score-track-take %i.",tlMarkerUid);
  874. return NULL;
  875. }
  876. return tlMark->text;
  877. }
  878. cmTsbRC_t cmTakeSeqBldrPlaySeekLoc( cmTakeSeqBldrH_t h, unsigned scLocIdx )
  879. {
  880. cmTsb_t* p = _cmTsbHandleToPtr(h);
  881. if( p->out == NULL )
  882. return cmErrMsg(&p->err,kRenderSeqEmptyTsbRC,"Seek faied. The render sequence is empty.");
  883. if( scLocIdx == cmInvalidIdx )
  884. p->evt = p->out->midi;
  885. else
  886. {
  887. cmScoreLoc_t* loc;
  888. if(( loc = cmScoreLoc(p->scH, scLocIdx )) == NULL )
  889. return cmErrMsg(&p->err,kInvalidArgTsbRC,"Seek failed. The requested score location (%i) could not be found.",scLocIdx);
  890. if( loc->evtCnt == 0 || loc->evtArray[0]==NULL )
  891. return cmErrMsg(&p->err,kInvalidArgTsbRC,"Seek failed. The requested score location (%i) has no associated event.",scLocIdx);
  892. unsigned scEvtIdx = loc->evtArray[0]->index;
  893. cmMidiTsb_t* m = p->out->midi;
  894. for(; m!=NULL; m=m->link)
  895. if( m->scEvtIdx >= scEvtIdx )
  896. {
  897. p->evt = m;
  898. break;
  899. }
  900. if( m == NULL )
  901. return cmErrMsg(&p->err,kInvalidArgTsbRC,"Seek failed. The requested score event index (%i) is out of range.",scEvtIdx );
  902. }
  903. return kOkTsbRC;
  904. }
  905. cmTsbRC_t cmTakeSeqBldrPlayExec( cmTakeSeqBldrH_t h, unsigned deltaSmp, cmTakeSeqBldrPlayFunc_t cbFunc, void* cbArg )
  906. {
  907. cmTsb_t* p = _cmTsbHandleToPtr(h);
  908. if( p->evt == NULL )
  909. return kOkTsbRC;
  910. // advance the play clock
  911. p->absPlaySmp += deltaSmp;
  912. // if the next event time has elapsed
  913. while( p->evt != NULL && p->absEvtSmp <= p->absPlaySmp )
  914. {
  915. // make the event callback
  916. cmTksbEvent_t e;
  917. e.smpIdx = p->absEvtSmp;
  918. e.status = p->evt->status;
  919. e.d0 = p->evt->d0;
  920. e.d1 = p->evt->d1;
  921. cbFunc(cbArg,&e);
  922. do
  923. {
  924. // advance the current play event
  925. p->evt = p->evt->link;
  926. // the last event was not encountered and the events offset time is legal
  927. if( p->evt != NULL && p->evt->offsetSmp != cmInvalidIdx )
  928. {
  929. // advance the event absolute time
  930. p->absEvtSmp += p->evt->offsetSmp;
  931. break;
  932. }
  933. }while( p->evt != NULL );
  934. }
  935. return kOkTsbRC;
  936. }
  937. void cmTakeSeqBldrRendReset( cmTakeSeqBldrH_t h )
  938. {
  939. cmTsb_t* p = _cmTsbHandleToPtr(h);
  940. if( p->out == NULL )
  941. return;
  942. p->rend = p->out->midi;
  943. }
  944. void _cmTakeSeqBldrMidiToRend( cmTksbRend_t* r, const cmMidiTsb_t* m )
  945. {
  946. r->rid = m->rid;
  947. r->srcId = m->srcId;
  948. r->scEvtIdx = m->scEvtIdx;
  949. r->flags = m->flags;
  950. r->offsetSmp = m->offsetSmp;
  951. r->durSmp = m->durSmp;
  952. r->evt.smpIdx = 0;
  953. r->evt.status = m->status;
  954. r->evt.d0 = m->d0;
  955. r->evt.d1 = m->d1;
  956. }
  957. cmMidiTsb_t* _cmTakeSeqBldrRidToMidi( cmTsb_t* p, unsigned rid )
  958. {
  959. cmMidiTsb_t* m;
  960. if( p->out == NULL )
  961. return NULL;
  962. for(m=p->out->midi; m!=NULL; m=m->link)
  963. if( m->rid == rid )
  964. return m;
  965. return NULL;
  966. }
  967. bool cmTakeSeqBldrRendNext(cmTakeSeqBldrH_t h, cmTksbRend_t* r)
  968. {
  969. cmTsb_t* p = _cmTsbHandleToPtr(h);
  970. cmMidiTsb_t* m = p->rend;
  971. if( m == NULL )
  972. return false;
  973. p->rend = p->rend->link;
  974. _cmTakeSeqBldrMidiToRend(r,m);
  975. return true;
  976. }
  977. cmTsbRC_t cmTakeSeqBldrRendInfo( cmTakeSeqBldrH_t h, unsigned rid, cmTksbRend_t* r )
  978. {
  979. cmTsb_t* p = _cmTsbHandleToPtr(h);
  980. cmMidiTsb_t* m;
  981. if((m = _cmTakeSeqBldrRidToMidi(p, rid )) == NULL )
  982. return cmErrMsg(&p->err,kInvalidArgTsbRC,"Unable to locate the MIDI render record associated with rid:%i.",rid);
  983. _cmTakeSeqBldrMidiToRend(r,m);
  984. return kOkTsbRC;
  985. }
  986. cmTsbRC_t cmTakeSeqBldrRendDelete( cmTakeSeqBldrH_t h, unsigned rid )
  987. {
  988. cmTsb_t* p = _cmTsbHandleToPtr(h);
  989. if( p->out == NULL || p->out->midi == NULL )
  990. return kOkTsbRC;
  991. cmMidiTsb_t* m1 = p->out->midi;
  992. for(; m1!=NULL; m1=m1->link)
  993. if( m1->rid == rid )
  994. {
  995. // if there is no recd before m1
  996. if( m1->ref == NULL )
  997. {
  998. p->out->midi = m1->link;
  999. // if there is a record after m1
  1000. if( p->out->midi != NULL )
  1001. {
  1002. p->out->midi->ref = NULL;
  1003. p->out->midi->offsetSmp = 0; // the first record never has a time offset
  1004. }
  1005. }
  1006. else // there is a record before m1
  1007. {
  1008. m1->ref->link = m1->link;
  1009. // if there is a record after m1
  1010. if( m1->link != NULL )
  1011. {
  1012. m1->link->ref = m1->ref;
  1013. m1->link->offsetSmp += m1->offsetSmp; // absorb m1's offset time into the next revent
  1014. }
  1015. }
  1016. cmMemFree(m1);
  1017. break;
  1018. }
  1019. _cmTsbTakePrint(p->out);
  1020. return kOkTsbRC;
  1021. }
  1022. cmTsbRC_t cmTakeSeqBldrRendInsert( cmTakeSeqBldrH_t h, const cmTksbEvent_t* e, unsigned durSmp, unsigned* ridRef)
  1023. {
  1024. cmTsb_t* p = _cmTsbHandleToPtr(h);
  1025. if( ridRef != NULL )
  1026. *ridRef = cmInvalidId;
  1027. cmMidiTsb_t* nm = cmMemAllocZ(cmMidiTsb_t,1);
  1028. nm->rid = p->nextRid++;
  1029. nm->srcId = cmInvalidId;
  1030. nm->scEvtIdx = cmInvalidId;
  1031. nm->flags = 0;
  1032. nm->durSmp = durSmp;
  1033. nm->status = e->status;
  1034. nm->d0 = e->d0;
  1035. nm->d1 = e->d1;
  1036. if( p->out == NULL )
  1037. {
  1038. p->out = cmMemAllocZ(cmTakeTsb_t,1);
  1039. p->out->tlMarkerUid = cmInvalidId;
  1040. }
  1041. if( p->out->midi == NULL )
  1042. {
  1043. p->out->midi = nm;
  1044. goto doneLabel;
  1045. }
  1046. cmMidiTsb_t* m0 = NULL;
  1047. cmMidiTsb_t* m1 = p->out->midi;
  1048. unsigned absSmpIdx = m1==NULL ? 0 : m1->offsetSmp;
  1049. for(; m1!=NULL; m1=m1->link)
  1050. {
  1051. // absSmpIdx is the absolute time of m1
  1052. // if m1 is the recd just after the new record
  1053. if( absSmpIdx > e->smpIdx )
  1054. {
  1055. // the record prior to the new record is m0
  1056. nm->ref = m0;
  1057. // the reocrd after the new record is m1
  1058. nm->link = m1;
  1059. // the new record is before m1
  1060. m1->ref = nm;
  1061. // if the new record is first on the list
  1062. if( m0 == NULL )
  1063. {
  1064. p->out->midi = nm;
  1065. // TODO: without giving more information there is no way
  1066. // to give the old first event an offset relative to the new
  1067. // first event - so the both events will be scheduled at
  1068. // time zero.
  1069. nm->offsetSmp = 0;
  1070. }
  1071. else // the new record is between m0 and m1
  1072. {
  1073. m0->link = nm;
  1074. // offset from new record to m1
  1075. unsigned dsi = absSmpIdx - e->smpIdx;
  1076. // m1's time offset is being reduced
  1077. assert( m1->offsetSmp >= dsi );
  1078. // the offset to the new record from m0
  1079. nm->offsetSmp = m1->offsetSmp - dsi;
  1080. m1->offsetSmp = dsi;
  1081. }
  1082. break;
  1083. }
  1084. // if m1 is not the last element on the list
  1085. if( m1->link != NULL )
  1086. absSmpIdx += m1->link->offsetSmp;
  1087. else
  1088. {
  1089. // insert the new event at the end of the list
  1090. nm->ref = m1;
  1091. nm->link = NULL;
  1092. m1->link = nm;
  1093. assert( e->smpIdx > absSmpIdx );
  1094. nm->offsetSmp = e->smpIdx - absSmpIdx;
  1095. break;
  1096. }
  1097. m0 = m1;
  1098. }
  1099. doneLabel:
  1100. if( ridRef != NULL )
  1101. *ridRef = nm->rid;
  1102. //_cmTsbTakePrint(p->out);
  1103. return kOkTsbRC;
  1104. }
  1105. cmTsbRC_t cmTakeSeqBldrWrite( cmTakeSeqBldrH_t h, const cmChar_t* fn )
  1106. {
  1107. cmTsbRC_t rc = kOkTsbRC;
  1108. cmJsonH_t jsH = cmJsonNullHandle;
  1109. cmJsonNode_t* arr;
  1110. cmMidiTsb_t* m;
  1111. cmTsb_t* p = _cmTsbHandleToPtr(h);
  1112. if( p->out == NULL )
  1113. return rc;
  1114. // allocate a JSON tree
  1115. if( cmJsonInitialize(&jsH,&p->ctx) != kOkJsRC )
  1116. {
  1117. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"JSON write tree allocate failed.");
  1118. goto errLabel;
  1119. }
  1120. // insert the root object
  1121. if( cmJsonCreateObject(jsH, NULL ) != kOkJsRC )
  1122. {
  1123. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"JSON root object allocate failed.");
  1124. goto errLabel;
  1125. }
  1126. // create the header record
  1127. if( cmJsonInsertPairs(jsH, cmJsonRoot(jsH),
  1128. "tlMarkerUid", kIntTId, p->out->tlMarkerUid,
  1129. "tlFileName", kStringTId, cmTimeLineFileName(p->tlH),
  1130. NULL) != kOkJsRC )
  1131. {
  1132. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"JSON header record create failed.");
  1133. goto errLabel;
  1134. }
  1135. // create the MIDI event array
  1136. if((arr = cmJsonInsertPairArray(jsH, cmJsonRoot(jsH), "midi")) == NULL )
  1137. {
  1138. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"Create the MIDI event array.");
  1139. goto errLabel;
  1140. }
  1141. // fill the MIDI array
  1142. for(m=p->out->midi; m!=NULL; m=m->link)
  1143. {
  1144. if( cmJsonCreateFilledObject(jsH, arr,
  1145. "rid", kIntTId, m->rid,
  1146. "srcId", kIntTId, m->srcId,
  1147. "scEvtIdx", kIntTId, m->scEvtIdx,
  1148. "flags", kIntTId, m->flags,
  1149. "offsetSmp", kIntTId, m->offsetSmp,
  1150. "durSmp", kIntTId, m->durSmp,
  1151. "status", kIntTId, m->status,
  1152. "d0", kIntTId, m->d0,
  1153. "d1", kIntTId, m->d1,
  1154. NULL) == NULL )
  1155. {
  1156. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"JSON MIDI record create failed.");
  1157. goto errLabel;
  1158. }
  1159. }
  1160. // write the tree
  1161. if( cmJsonWrite( jsH, cmJsonRoot(jsH), fn ) != kOkJsRC )
  1162. {
  1163. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"JSON write to '%s' failed.",cmStringNullGuard(fn));
  1164. }
  1165. errLabel:
  1166. if( cmJsonFinalize(&jsH) != kOkJsRC )
  1167. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"JSON tree finalize failed.");
  1168. return rc;
  1169. }
  1170. cmTsbRC_t cmTakeSeqBldrRead( cmTakeSeqBldrH_t h, const cmChar_t* fn )
  1171. {
  1172. cmTsbRC_t rc = kOkTsbRC;
  1173. cmTsb_t* p = _cmTsbHandleToPtr(h);
  1174. cmJsonH_t jsH = cmJsonNullHandle;
  1175. const cmChar_t* jsErrLabel = NULL;
  1176. cmTakeTsb_t* t = NULL;
  1177. cmMidiTsb_t* m0 = NULL;
  1178. cmJsonNode_t* arr = NULL;
  1179. cmJsRC_t jsRC;
  1180. if( cmJsonInitializeFromFile( &jsH, fn, &p->ctx ) != kOkJsRC )
  1181. {
  1182. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"Unable to parse the JSON file '%s'.",cmStringNullGuard(fn));
  1183. goto errLabel;
  1184. }
  1185. t = cmMemAllocZ(cmTakeTsb_t,1);
  1186. if((jsRC = cmJsonMemberValues( cmJsonRoot(jsH), &jsErrLabel,
  1187. "tlMarkerUid", kIntTId, &t->tlMarkerUid,
  1188. "midi", kArrayTId, &arr,
  1189. NULL)) != kOkJsRC )
  1190. {
  1191. if( jsRC == kNodeNotFoundJsRC && jsErrLabel != NULL )
  1192. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file MIDI render header failed missing required field:'%s'",cmStringNullGuard(jsErrLabel));
  1193. else
  1194. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file MIDI render header parse failed.");
  1195. goto errLabel;
  1196. }
  1197. unsigned n = cmJsonChildCount(arr);
  1198. unsigned i;
  1199. for(i=0; i<n; ++i)
  1200. {
  1201. const cmJsonNode_t* e = cmJsonArrayElementC(arr, i );
  1202. cmMidiTsb_t* m = cmMemAllocZ(cmMidiTsb_t,1);
  1203. if((jsRC = cmJsonMemberValues( e, &jsErrLabel,
  1204. "rid", kIntTId, &m->rid,
  1205. "srcId", kIntTId, &m->srcId,
  1206. "scEvtIdx", kIntTId, &m->scEvtIdx,
  1207. "flags", kIntTId, &m->flags,
  1208. "offsetSmp", kIntTId, &m->offsetSmp,
  1209. "durSmp", kIntTId, &m->durSmp,
  1210. "status", kIntTId, &m->status,
  1211. "d0", kIntTId, &m->d0,
  1212. "d1", kIntTId, &m->d1,
  1213. NULL)) != kOkJsRC )
  1214. {
  1215. if( jsRC == kNodeNotFoundJsRC && jsErrLabel != NULL )
  1216. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file MIDI render element failed missing required field:'%s' on index:%i",cmStringNullGuard(jsErrLabel),i);
  1217. else
  1218. rc = cmErrMsg(&p->err,kParseFailTsbRC,"JSON file MIDI render element parse failed on index: %i.", i);
  1219. goto errLabel;
  1220. }
  1221. m->ref = m0;
  1222. if( m0 == NULL )
  1223. t->midi = m;
  1224. else
  1225. m0->link = m;
  1226. }
  1227. errLabel:
  1228. if( cmJsonFinalize(&jsH) != kOkJsRC )
  1229. rc = cmErrMsg(&p->err,kJsonFailTsbRC,"JSON finalize failed.");
  1230. if( rc != kOkTsbRC )
  1231. _cmTsbTakeFree(p,&t);
  1232. else
  1233. {
  1234. _cmTsbRendTakeFree(p,&p->out);
  1235. p->out = t;
  1236. }
  1237. return rc;
  1238. }
  1239. cmTsbRC_t cmTakeSeqBldrTest( cmCtx_t* ctx )
  1240. {
  1241. const cmChar_t* scoreTrkFn = "/home/kevin/src/cmkc/src/kc/data/takeSeqBldr0.js";
  1242. cmTakeSeqBldrH_t tsbH = cmTakeSeqBldrNullHandle;
  1243. cmTsbRC_t tsbRC = kOkTsbRC;
  1244. unsigned markerIdV[] = { 2200, 2207 };
  1245. unsigned markerN = sizeof(markerIdV)/sizeof(markerIdV[0]);
  1246. unsigned i;
  1247. if((tsbRC = cmTakeSeqBldrAllocFn(ctx, &tsbH, scoreTrkFn )) != kOkTsbRC )
  1248. return cmErrMsg(&ctx->err,tsbRC,"TSB Allocate and parse '%s' failed.",scoreTrkFn);
  1249. cmRptPrintf(&ctx->rpt, "TakeSeqBldr Allocation Completed.");
  1250. for(i=0; i<markerN; ++i)
  1251. {
  1252. if((tsbRC = cmTakeSeqBldrLoadTake(tsbH,markerIdV[i],false)) != kOkTsbRC )
  1253. cmErrMsg(&ctx->err,tsbRC,"TSB load take failed.");
  1254. cmRptPrintf(&ctx->rpt, "TakeSeqBldr Load Take %i Completed.",markerIdV[i]);
  1255. }
  1256. if((tsbRC = cmTakeSeqBldrFree(&tsbH)) != kOkTsbRC )
  1257. return cmErrMsg(&ctx->err,tsbRC,"TSB Free failed.");
  1258. return tsbRC;
  1259. }