libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmTimeLine.c 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. #include "cmGlobal.h"
  2. #include "cmFloatTypes.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmJson.h"
  10. #include "cmText.h"
  11. #include "cmAudioFile.h"
  12. #include "cmTime.h"
  13. #include "cmMidi.h"
  14. #include "cmMidiFile.h"
  15. #include "cmFileSys.h"
  16. #include "cmTimeLine.h"
  17. #include "cmOnset.h"
  18. // id's used to track the type of a serialized object
  19. enum
  20. {
  21. kMsgTlId,
  22. kObjTlId
  23. };
  24. //
  25. typedef struct _cmTlMsg_str
  26. {
  27. unsigned typeId; // always set to kMsgTlId
  28. cmTlUiMsgTypeId_t msgId; //
  29. double srate;
  30. unsigned seqCnt;
  31. unsigned seqId;
  32. } _cmTlMsg_t;
  33. typedef struct _cmTlObj_str
  34. {
  35. void* mem;
  36. unsigned memByteCnt;
  37. cmTlObj_t* obj;
  38. struct _cmTlObj_str* prev;
  39. struct _cmTlObj_str* next;
  40. } _cmTlObj_t;
  41. typedef struct
  42. {
  43. _cmTlObj_t* first;
  44. _cmTlObj_t* last;
  45. } _cmTlSeq_t;
  46. typedef struct
  47. {
  48. cmErr_t err;
  49. cmCtx_t ctx;
  50. cmLHeapH_t lH;
  51. double srate;
  52. unsigned nextSeqId;
  53. cmTlCb_t cbFunc;
  54. void* cbArg;
  55. cmChar_t* prefixPath;
  56. unsigned nextUId;
  57. char* tmpBuf;
  58. unsigned seqCnt;
  59. _cmTlSeq_t* seq; // seq[seqCnt]
  60. const cmChar_t* filename;
  61. cmOnsetCfg_t onsetCfg;
  62. } _cmTl_t;
  63. typedef struct
  64. {
  65. char label[8];
  66. unsigned id;
  67. } _cmTlId_t;
  68. _cmTlId_t _cmTlIdArray[] =
  69. {
  70. { "mf", kMidiFileTlId },
  71. { "me", kMidiEvtTlId },
  72. { "af", kAudioFileTlId },
  73. { "ae", kAudioEvtTlId },
  74. { "mk", kMarkerTlId },
  75. { "", cmInvalidId }
  76. };
  77. cmTlH_t cmTimeLineNullHandle = cmSTATIC_NULL_HANDLE;
  78. _cmTl_t* _cmTlHandleToPtr( cmTlH_t h )
  79. {
  80. _cmTl_t* p = (_cmTl_t*)h.h;
  81. assert( p != NULL );
  82. return p;
  83. }
  84. _cmTlId_t* _cmTlIdLabelToRecd( _cmTl_t* p, const cmChar_t* label )
  85. {
  86. unsigned i;
  87. if( label != NULL )
  88. for(i=0; _cmTlIdArray[i].id != cmInvalidId; ++i)
  89. if( strcmp(_cmTlIdArray[i].label,label) == 0 )
  90. return _cmTlIdArray + i;
  91. return NULL;
  92. }
  93. _cmTlId_t* _cmTlIdToRecd( _cmTl_t* p, unsigned id )
  94. {
  95. unsigned i;
  96. for(i=0; _cmTlIdArray[i].id != cmInvalidId; ++i)
  97. if( _cmTlIdArray[i].id == id )
  98. return _cmTlIdArray + i;
  99. return NULL;
  100. }
  101. const cmChar_t* _cmTlIdToLabel( _cmTl_t* p, unsigned id )
  102. {
  103. _cmTlId_t* rp;
  104. if((rp = _cmTlIdToRecd(p,id)) != NULL )
  105. return rp->label;
  106. return "";
  107. }
  108. unsigned _cmTlIdLabelToId( _cmTl_t* p, const cmChar_t* label )
  109. {
  110. _cmTlId_t* rp;
  111. if((rp = _cmTlIdLabelToRecd(p,label)) != NULL )
  112. return rp->id;
  113. return cmInvalidId;
  114. }
  115. // cast a generic object to a midi file object
  116. cmTlMidiFile_t* _cmTlMidiFileObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
  117. {
  118. if( op==NULL || op->typeId != kMidiFileTlId )
  119. {
  120. if( errFl && p != NULL)
  121. cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
  122. return NULL;
  123. }
  124. return (cmTlMidiFile_t*)op;
  125. }
  126. // cast a generic object to a midi event object
  127. cmTlMidiEvt_t* _cmTlMidiEvtObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
  128. {
  129. if( op==NULL || op->typeId != kMidiEvtTlId )
  130. {
  131. if( errFl && p != NULL )
  132. cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
  133. return NULL;
  134. }
  135. return (cmTlMidiEvt_t*)op;
  136. }
  137. // case a generic object to an audio file object
  138. cmTlAudioFile_t* _cmTlAudioFileObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
  139. {
  140. if( op==NULL || op->typeId != kAudioFileTlId )
  141. {
  142. if( errFl && p != NULL)
  143. cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
  144. return NULL;
  145. }
  146. return (cmTlAudioFile_t*)op;
  147. }
  148. // cast a generic object an audio event object to
  149. cmTlAudioEvt_t* _cmTlAudioEvtObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
  150. {
  151. if( op==NULL || op->typeId != kAudioEvtTlId )
  152. {
  153. if( errFl && p != NULL)
  154. cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
  155. return NULL;
  156. }
  157. return (cmTlAudioEvt_t*)op;
  158. }
  159. // cast a generic object to a marker object
  160. cmTlMarker_t* _cmTlMarkerObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
  161. {
  162. if( op==NULL || op->typeId != kMarkerTlId )
  163. {
  164. if( errFl && p != NULL)
  165. cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
  166. return NULL;
  167. }
  168. return (cmTlMarker_t*)op;
  169. }
  170. cmTlMidiFile_t* _cmTimeLineMidiFileObjPtr( _cmTl_t* p, cmTlObj_t* op )
  171. { return _cmTlMidiFileObjPtr(p,op,true); }
  172. cmTlMidiEvt_t* _cmTimeLineMidiEvtObjPtr( _cmTl_t* p, cmTlObj_t* op )
  173. { return _cmTlMidiEvtObjPtr(p,op,true); }
  174. cmTlAudioFile_t* _cmTimeLineAudioFileObjPtr( _cmTl_t* p, cmTlObj_t* op )
  175. { return _cmTlAudioFileObjPtr(p,op,true);}
  176. cmTlAudioEvt_t* _cmTimeLineAudioEvtObjPtr( _cmTl_t* p, cmTlObj_t* op )
  177. { return _cmTlAudioEvtObjPtr(p,op,true);}
  178. cmTlMarker_t* _cmTimeLineMarkerObjPtr( _cmTl_t* p, cmTlObj_t* op )
  179. { return _cmTlMarkerObjPtr(p,op,true);}
  180. // Locate a record which matches 'name' and (optionally) 'seqId'
  181. _cmTlObj_t* _cmTlFindRecd( _cmTl_t* p, unsigned seqId, const cmChar_t* name )
  182. {
  183. if( name == NULL )
  184. return NULL;
  185. unsigned i;
  186. for(i=0; i<p->seqCnt; ++i)
  187. if( seqId==cmInvalidId || seqId == i )
  188. {
  189. _cmTlObj_t* op = p->seq[i].first;
  190. while(op != NULL)
  191. {
  192. if( strcmp(op->obj->name,name) == 0 )
  193. return op;
  194. op = op->next;
  195. }
  196. }
  197. return NULL;
  198. }
  199. // Returns true if 'op' is a child of 'ref'.
  200. bool _cmTlIsChild( _cmTlObj_t* ref, _cmTlObj_t* op )
  201. {
  202. // if 'op' is not active then it can't be a child
  203. if( op->obj == NULL )
  204. return false;
  205. // if 'ref' is NULL then match obj's which do not have a parent
  206. if( ref == NULL )
  207. return op->obj->ref == NULL;
  208. // if 'ref' is the parent of 'op'.
  209. return op->obj->ref == ref->obj;
  210. }
  211. // calc the absolute start time of this object by adding the
  212. // time time offsets of all ancestors.
  213. int _cmTlStartTime( const cmTlObj_t* obj )
  214. {
  215. assert( obj != NULL );
  216. int t = 0;
  217. do
  218. {
  219. t += obj->begSmpIdx;
  220. obj=obj->ref;
  221. }while( obj != NULL);
  222. return t;
  223. }
  224. // Locate the closest record which is before 'np'.
  225. // When multiple records have the same distance to 'np' then the last one inserted
  226. // is taken as the closest. This way records with equal time values will be
  227. // secondarily sequenced on their order of insertion.
  228. _cmTlObj_t* _cmTlFindRecdBefore( _cmTl_t* p, const _cmTlObj_t* np )
  229. {
  230. assert( np->obj!=NULL && np->obj->seqId < p->seqCnt );
  231. // calc the absolute time of this object
  232. //int absSmpIdx = _cmTlStartTime(np->obj);
  233. int rsi = 0;
  234. _cmTlObj_t* rp = NULL;
  235. _cmTlObj_t* op = p->seq[np->obj->seqId].first;
  236. //printf("type:%i %i\n",np->obj->typeId,absSmpIdx);
  237. // for each object in the list ...
  238. while( op != NULL )
  239. {
  240. int csi;
  241. //if( op!=np && op->obj!=NULL && (csi = _cmTlStartTime(op->obj)) <= absSmpIdx )
  242. if( (op!=np) && (op->obj!=NULL) && ((csi = op->obj->seqSmpIdx) <= np->obj->seqSmpIdx) )
  243. {
  244. if( rp == NULL || csi >= rsi )
  245. {
  246. rp = op;
  247. rsi = csi;
  248. }
  249. }
  250. op = op->next;
  251. }
  252. return rp;
  253. }
  254. // Mark 'op' and all children of 'op' for deletion.
  255. // Note that this function is recursive.
  256. cmTlRC_t _cmTlDeleteDependentRecds( _cmTl_t* p, _cmTlObj_t* op )
  257. {
  258. assert( op->obj!=NULL && op->obj->seqId < p->seqCnt );
  259. cmTlRC_t rc = kOkTlRC;
  260. _cmTlObj_t* dp = p->seq[op->obj->seqId].first;
  261. // mark all recd's that are children of 'op' for deletion
  262. while( dp != NULL )
  263. {
  264. // if 'dp' is a child of 'op'.
  265. if( _cmTlIsChild(op,dp) )
  266. if(( rc = _cmTlDeleteDependentRecds(p,dp)) != kOkTlRC )
  267. return rc;
  268. dp = dp->next;
  269. }
  270. // release any resources held by 'op'.
  271. switch(op->obj->typeId)
  272. {
  273. case kMidiFileTlId:
  274. {
  275. cmTlMidiFile_t* mp = _cmTimeLineMidiFileObjPtr(p,op->obj);
  276. cmMidiFileClose(&mp->h);
  277. }
  278. break;
  279. case kMidiEvtTlId:
  280. break;
  281. case kAudioFileTlId:
  282. {
  283. cmTlAudioFile_t* ap = _cmTimeLineAudioFileObjPtr(p,op->obj);
  284. cmAudioFileDelete(&ap->h);
  285. }
  286. break;
  287. case kAudioEvtTlId:
  288. break;
  289. case kMarkerTlId:
  290. break;
  291. default:
  292. return cmErrMsg(&p->err,kUnknownRecdTypeTlRC,"An unknown time line object type (%i) was encounterned during object deletion.",op->obj->typeId);
  293. }
  294. // mark 'op' as deleted by setting op->obj to NULL
  295. op->obj = NULL;
  296. return kOkTlRC;
  297. }
  298. // Delete 'op' and all of its dependents.
  299. cmTlRC_t _cmTlDeleteRecd( _cmTl_t* p, _cmTlObj_t* op )
  300. {
  301. if( op == NULL )
  302. return kOkTlRC;
  303. assert( op->obj!=NULL && op->obj->seqId < p->seqCnt );
  304. cmTlRC_t rc;
  305. _cmTlSeq_t* s = p->seq + op->obj->seqId;
  306. // mark this object and any objecs which are dependent on it for deletion
  307. if((rc =_cmTlDeleteDependentRecds(p,op)) != kOkTlRC )
  308. return rc;
  309. // unlink and delete and records marked for deletion
  310. op = s->first;
  311. while( op )
  312. {
  313. _cmTlObj_t* tp = op->next;
  314. // if this object is marked for deletion unlink it from this master list
  315. if( op->obj == NULL )
  316. {
  317. if( op->next != NULL )
  318. op->next->prev = op->prev;
  319. else
  320. {
  321. assert( s->last == op );
  322. s->last = op->prev;
  323. }
  324. if( op->prev != NULL )
  325. op->prev->next = op->next;
  326. else
  327. {
  328. assert( s->first == op );
  329. s->first = op->next;
  330. }
  331. // free the record
  332. cmLhFreePtr(p->lH,(void**)&op->mem);
  333. }
  334. op = tp;
  335. }
  336. return rc;
  337. }
  338. // Insert 'op' after 'bp'.
  339. // If 'bp' is NULL then 'op' is inserted as p->seq[op->seqId].first.
  340. void _cmTlInsertAfter( _cmTl_t* p, _cmTlObj_t* bp, _cmTlObj_t* op )
  341. {
  342. assert( op->obj!=NULL && op->obj->seqId < p->seqCnt );
  343. _cmTlSeq_t* s = p->seq + op->obj->seqId;
  344. op->prev = bp;
  345. // if op is being inserted at the beginning of the list
  346. if( bp == NULL )
  347. {
  348. op->next = s->first;
  349. if( s->first != NULL )
  350. s->first->prev = op;
  351. s->first = op;
  352. }
  353. else // op is being inserted in the middle or end of the list
  354. {
  355. op->next = bp->next;
  356. if( bp->next != NULL )
  357. bp->next->prev = op;
  358. else
  359. {
  360. // insertion at end
  361. assert( bp == s->last );
  362. s->last = op;
  363. }
  364. bp->next = op;
  365. }
  366. if( s->last == NULL )
  367. s->last = op;
  368. if( s->first == NULL )
  369. s->first = op;
  370. }
  371. // Allocate an object record
  372. cmTlRC_t _cmTlAllocRecd2(
  373. _cmTl_t* p,
  374. const cmChar_t* nameStr, // NULL, empty or unique name
  375. _cmTlObj_t* refPtr, // parent object
  376. int begSmpIdx, // start time
  377. unsigned durSmpCnt, // duration
  378. unsigned typeId, // object type id
  379. unsigned seqId, // owning seq
  380. unsigned recdByteCnt, // byte for type specific data to follow _cmTlObj_t data
  381. _cmTlObj_t** opp ) // return pointer
  382. {
  383. *opp = NULL;
  384. if( nameStr == NULL )
  385. nameStr = "";
  386. // get the length of the recd name field
  387. unsigned nameByteCnt = strlen(nameStr)+1;
  388. // verify that the name was not already used by another recd
  389. if( nameByteCnt>1 && _cmTlFindRecd(p,seqId,nameStr) != NULL )
  390. return cmErrMsg(&p->err,kDuplNameTlRC,"The object identifier '%s' was already used.",nameStr);
  391. assert( refPtr==NULL || refPtr->obj !=NULL );
  392. if( refPtr != NULL && refPtr->obj->seqId != seqId )
  393. return cmErrMsg(&p->err,kInvalidSeqIdTlRC,"The sequence id of the reference object (%i) does not match the sequence id (%i) of the new object (label:%s).",refPtr->obj->seqId,seqId,cmStringNullGuard(nameStr));
  394. if( seqId >= p->seqCnt )
  395. {
  396. // assume the sequence id's arrive in increasing order
  397. assert( seqId == p->seqCnt );
  398. assert( refPtr == NULL );
  399. p->seqCnt = seqId+1;
  400. p->seq = cmMemResizePZ(_cmTlSeq_t,p->seq,p->seqCnt);
  401. }
  402. // calc the total size of the recd. memory layout: [name /0 _cmTlObj_t cmTlObj_t <type specific fields> ]
  403. unsigned byteCnt = sizeof(unsigned) + sizeof(unsigned) + nameByteCnt + sizeof(_cmTlObj_t) + recdByteCnt;
  404. void* mem = cmLHeapAllocZ( p->lH, byteCnt );
  405. unsigned* tidPtr = (unsigned*)mem;
  406. unsigned* parentIdPtr = tidPtr + 1;
  407. cmChar_t* name = (cmChar_t*)(parentIdPtr + 1);
  408. _cmTlObj_t* op = (_cmTlObj_t*)(name + nameByteCnt);
  409. cmTlObj_t* tp = (cmTlObj_t*)(op+1);
  410. // The entire object is contained in mem[]
  411. // Memory Layout:
  412. // kObjTlId parentId name[] \0 _cmTlObj_t cmTlObj_t [recdByteCnt - sizeof(cmTlObj_t)]
  413. strcpy(name,nameStr);
  414. // the first element in the mem[] buffer must be kObjTlId - this allows
  415. // mem[] to be used directly as the serialized version of the buffer.
  416. *tidPtr = kObjTlId;
  417. *parentIdPtr = refPtr==NULL ? cmInvalidId : refPtr->obj->uid;
  418. tp->reserved = op;
  419. tp->seqId = seqId;
  420. tp->name = name;
  421. tp->uid = p->nextUId++;
  422. tp->typeId = typeId;
  423. tp->ref = refPtr==NULL ? NULL : refPtr->obj;
  424. tp->begSmpIdx = refPtr==NULL ? 0 : begSmpIdx;
  425. tp->durSmpCnt = durSmpCnt;
  426. tp->seqSmpIdx = refPtr==NULL ? 0 : refPtr->obj->seqSmpIdx + begSmpIdx;
  427. tp->flags = 0;
  428. tp->text = NULL;
  429. op->obj = tp;
  430. op->mem = mem;
  431. op->memByteCnt = byteCnt;
  432. op->next = NULL;
  433. op->prev = NULL;
  434. //if( seqId == 4 )
  435. // printf("seq:%i id:%i type:%i accum:%i ref:%i offs:%i %f\n",seqId, tp->uid, tp->typeId, tp->seqSmpIdx, refPtr==NULL?-1:refPtr->obj->uid, begSmpIdx, begSmpIdx/(96000.0*60.0) );
  436. _cmTlInsertAfter(p, _cmTlFindRecdBefore(p,op), op );
  437. *opp = op;
  438. return kOkTlRC;
  439. }
  440. cmTlRC_t _cmTlAllocRecd( _cmTl_t* p, const cmChar_t* nameStr, const cmChar_t* refIdStr, int begSmpIdx, unsigned durSmpCnt, unsigned typeId, unsigned seqId, unsigned recdByteCnt, _cmTlObj_t** opp )
  441. {
  442. // locate the obj recd that this recd is part of (the parent recd)
  443. _cmTlObj_t* refPtr = _cmTlFindRecd(p,seqId,refIdStr);
  444. // if this obj has a parent but it was not found
  445. if( refPtr == NULL && refIdStr!=NULL && strlen(refIdStr)>0 )
  446. return cmErrMsg(&p->err,kRefNotFoundTlRC,"Reference identifier '%s' not found for object '%s'.",refIdStr,cmStringNullGuard(nameStr));
  447. return _cmTlAllocRecd2(p,nameStr,refPtr,begSmpIdx,durSmpCnt,typeId,seqId,recdByteCnt,opp);
  448. }
  449. void _cmTlNotifyListener( _cmTl_t* p, cmTlUiMsgTypeId_t msgTypeId, _cmTlObj_t* op, unsigned seqId )
  450. {
  451. if( p->cbFunc == NULL )
  452. return;
  453. switch( msgTypeId )
  454. {
  455. case kInitMsgTlId:
  456. case kFinalMsgTlId:
  457. case kDoneMsgTlId:
  458. {
  459. _cmTlMsg_t m;
  460. m.typeId = kMsgTlId;
  461. m.msgId = msgTypeId;
  462. m.srate = p->srate;
  463. m.seqCnt = p->seqCnt;
  464. m.seqId = seqId;
  465. p->cbFunc( p->cbArg, &m, sizeof(m) );
  466. }
  467. break;
  468. case kInsertMsgTlId:
  469. if( op != NULL )
  470. p->cbFunc( p->cbArg, op->mem, op->memByteCnt );
  471. break;
  472. default:
  473. { assert(0); }
  474. }
  475. }
  476. cmTlRC_t _cmTlAllocAudioFileRecd( _cmTl_t* p, const cmChar_t* nameStr, const cmChar_t* refIdStr, int begSmpIdx, unsigned seqId, const cmChar_t* fn )
  477. {
  478. cmAudioFileH_t afH = cmNullAudioFileH;
  479. cmAudioFileInfo_t info;
  480. cmRC_t afRC = cmOkRC;
  481. cmTlRC_t rc;
  482. _cmTlObj_t* op = NULL;
  483. // prepend the time-line path prefix to the filename
  484. fn = cmFsMakeFn( p->prefixPath, fn, NULL, NULL );
  485. unsigned recdByteCnt = sizeof(cmTlAudioFile_t) + strlen(fn) + 1;
  486. if( cmAudioFileIsValid( afH = cmAudioFileNewOpen(fn, &info, &afRC, p->err.rpt )) == false )
  487. {
  488. rc = cmErrMsg(&p->err,kAudioFileFailTlRC,"The time line audio file '%s' could not be opened.",cmStringNullGuard(fn));
  489. goto errLabel;
  490. }
  491. if((rc = _cmTlAllocRecd(p,nameStr,refIdStr,begSmpIdx,info.frameCnt,kAudioFileTlId,seqId,recdByteCnt,&op)) != kOkTlRC )
  492. goto errLabel;
  493. assert(op != NULL && fn != NULL );
  494. cmTlAudioFile_t* ap = _cmTimeLineAudioFileObjPtr(p,op->obj);
  495. char* cp = (char*)ap;
  496. assert(ap != NULL );
  497. ap->h = afH;
  498. ap->info = info;
  499. ap->fn = cp + sizeof(cmTlAudioFile_t);
  500. strcpy(ap->fn,fn); // copy the file name into the extra memory
  501. assert( ap->fn + strlen(fn) + 1 == cp + recdByteCnt );
  502. op->obj->text = ap->fn;
  503. // notify listeners that an new object was created by sending a kObjTlId msg
  504. // notifiy any listeners that a midi file object was created
  505. //_cmTlNotifyListener(p, kInsertMsgTlId, op );
  506. errLabel:
  507. cmFsFreeFn(fn);
  508. if( rc != kOkTlRC )
  509. cmAudioFileDelete(&afH);
  510. return rc;
  511. }
  512. cmTlRC_t _cmTlProcMidiFile( _cmTl_t* p, _cmTlObj_t* op, cmMidiFileH_t mfH )
  513. {
  514. cmTlRC_t rc = kOkTlRC;
  515. cmTlMidiFile_t* mfp = _cmTimeLineMidiFileObjPtr(p,op->obj);
  516. unsigned mn = cmMidiFileMsgCount(mfH);
  517. const cmMidiTrackMsg_t** mapp = cmMidiFileMsgArray(mfH);
  518. unsigned mi = 0;
  519. _cmTlObj_t* refOp = op;
  520. mfp->noteOnCnt = 0;
  521. // for each midi message
  522. for(; mi<mn; ++mi)
  523. {
  524. _cmTlObj_t* meop = NULL;
  525. const cmMidiTrackMsg_t* mp = mapp[mi];
  526. int begSmpIdx = mp->dtick;
  527. int durSmpCnt = 0;
  528. unsigned midiTrkMsgByteCnt = cmMidiFilePackTrackMsgBufByteCount( mp );
  529. unsigned recdByteCnt = sizeof(cmTlMidiEvt_t) + midiTrkMsgByteCnt;
  530. //if( mfp->obj.seqId==4 && mi<=25 )
  531. // printf("%s: bsi:%9i acc:%f smp acc:%f min %s\n", mp->status == kNoteOnMdId?"non":" ", begSmpIdx, accum, accum / (p->srate * 60),cmStringNullGuard(mfp->obj.name));
  532. // count the note-on messages
  533. if( cmMidiIsNoteOn(mp->status) )
  534. {
  535. durSmpCnt = mp->u.chMsgPtr->durTicks;
  536. ++mfp->noteOnCnt;
  537. }
  538. if( cmMidiIsCtl(mp->status) && cmMidiIsSustainPedal(mp->status,mp->u.chMsgPtr->d0) )
  539. durSmpCnt = mp->u.chMsgPtr->durTicks;
  540. // allocate the generic time-line object record
  541. if((rc = _cmTlAllocRecd2(p, NULL, refOp, begSmpIdx, durSmpCnt, kMidiEvtTlId, mfp->obj.seqId, recdByteCnt, &meop)) != kOkTlRC )
  542. goto errLabel;
  543. assert( meop != NULL );
  544. cmTlMidiEvt_t* mep = _cmTimeLineMidiEvtObjPtr(p,meop->obj);
  545. char* cp = (char*)mep;
  546. assert( mep != NULL );
  547. // Set the cmTlMidiEvt_t.msg cmMidiTrkMsg_t pointer to point to the
  548. // extra memory allocated just past the cmTlMidiEvt_t recd.
  549. mep->msg = (cmMidiTrackMsg_t*)(cp + sizeof(cmTlMidiEvt_t));
  550. mep->midiFileObjId = mfp->obj.uid;
  551. // Do not write MIDI objects that are part of a MIDI file. They will be automatically
  552. // loaded when the time line is loaded and therefore do not need to be save
  553. // explicitely in the time line data.
  554. meop->obj->flags = cmSetFlag(meop->obj->flags,kNoWriteTlFl);
  555. // Pack the cmMidiTrackMsg_t record into the extra memory
  556. cmMidiFilePackTrackMsg( mp, (char*)mep->msg, midiTrkMsgByteCnt );
  557. // verify that the memory allocation was calculated correctly
  558. assert( cp + recdByteCnt == ((char*)mep->msg) + midiTrkMsgByteCnt);
  559. // this midi event is the ref. for the next midi evt
  560. refOp = meop;
  561. }
  562. errLabel:
  563. return rc;
  564. }
  565. cmTlRC_t _cmTlAllocMidiFileRecd( _cmTl_t* p, const cmChar_t* nameStr, const cmChar_t* refIdStr, int begSmpIdx, unsigned seqId, const cmChar_t* fn )
  566. {
  567. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  568. cmTlRC_t rc = kOkTlRC;
  569. _cmTlObj_t* op = NULL;
  570. // prepend the time-line path prefix to the filename
  571. fn = cmFsMakeFn( p->prefixPath, fn, NULL, NULL );
  572. // open the midi file
  573. if( cmMidiFileOpen(fn, &mfH, &p->ctx ) != kOkMfRC )
  574. return cmErrMsg(&p->err,kMidiFileFailTlRC,"The time line midi file '%s' could not be opened.",cmStringNullGuard(fn));
  575. // force the first msg to occurr one quarter note into the file
  576. cmMidiFileSetDelay(mfH, cmMidiFileTicksPerQN(mfH) );
  577. unsigned durSmpCnt = floor(cmMidiFileDurSecs(mfH)*p->srate);
  578. // convert the midi file from delta ticks to delta samples
  579. cmMidiFileTickToSamples(mfH,p->srate,false);
  580. // assign note durations to all note-on msg's
  581. cmMidiFileCalcNoteDurations(mfH);
  582. unsigned recdByteCnt = sizeof(cmTlMidiFile_t) + strlen(fn) + 1;
  583. // allocate the midi file time line object
  584. if((rc = _cmTlAllocRecd(p,nameStr,refIdStr,begSmpIdx,durSmpCnt,kMidiFileTlId,seqId,recdByteCnt,&op)) != kOkTlRC )
  585. goto errLabel;
  586. assert( op != NULL && fn != NULL );
  587. cmTlMidiFile_t* mp = _cmTimeLineMidiFileObjPtr(p,op->obj);
  588. char* cp = (char*)mp;
  589. assert(mp != NULL );
  590. mp->h = mfH;
  591. mp->fn = cp + sizeof(cmTlMidiFile_t);
  592. strcpy(mp->fn,fn); // copy the filename into the extra memory
  593. assert( mp->fn + strlen(mp->fn) + 1 == cp + recdByteCnt );
  594. op->obj->text = mp->fn;
  595. // insert the events in the midi file as individual time line objects
  596. if((rc = _cmTlProcMidiFile(p, op, mfH)) != kOkTlRC )
  597. goto errLabel;
  598. errLabel:
  599. cmFsFreeFn(fn);
  600. if( rc != kOkTlRC )
  601. {
  602. cmMidiFileClose(&mfH);
  603. _cmTlDeleteRecd(p, op);
  604. }
  605. return rc;
  606. }
  607. cmTlRC_t _cmTlAllocMarkerRecd( _cmTl_t* p, const cmChar_t* nameStr, const cmChar_t* refIdStr, int begSmpIdx, unsigned durSmpCnt, unsigned seqId, const cmChar_t* text, unsigned bar, const cmChar_t* sectionStr, unsigned markerTypeId, cmTlObj_t* refObjPtr )
  608. {
  609. cmTlRC_t rc = kOkTlRC;
  610. _cmTlObj_t* op = NULL;
  611. const cmChar_t* textStr = text==NULL ? "" : text;
  612. const cmChar_t* sectStr = sectionStr==NULL ? "" : sectionStr;
  613. // add memory at the end of the the cmTlMarker_t record to hold the text string.
  614. unsigned recdByteCnt = sizeof(cmTlMarker_t) + strlen(textStr) + sizeof(bar) + strlen(sectStr) + 2;
  615. if(refIdStr == NULL )
  616. rc = _cmTlAllocRecd2(p,nameStr,(_cmTlObj_t*)refObjPtr->reserved,begSmpIdx,durSmpCnt,kMarkerTlId,seqId,recdByteCnt,&op);
  617. else
  618. rc = _cmTlAllocRecd(p,nameStr,refIdStr,begSmpIdx,durSmpCnt,kMarkerTlId,seqId,recdByteCnt,&op);
  619. if(rc != kOkTlRC )
  620. goto errLabel;
  621. assert(op != NULL);
  622. // get a ptr to the marker part of the object
  623. cmTlMarker_t* mp = _cmTimeLineMarkerObjPtr(p,op->obj);
  624. assert(mp != NULL );
  625. // copy the marker text string into the memory just past the cmTlMarker_t recd.
  626. cmChar_t* tp = (cmChar_t*)(mp + 1);
  627. strcpy(tp,textStr);
  628. // copy the section label string into memory just past the markers text string
  629. cmChar_t* sp = tp + strlen(tp) + 1;
  630. strcpy(sp,sectStr);
  631. // onset markers are generated but not written
  632. if( markerTypeId != kAudioMarkTlId )
  633. mp->obj.flags = cmSetFlag(mp->obj.flags,kNoWriteTlFl);
  634. mp->typeId = markerTypeId;
  635. mp->text = tp;
  636. mp->sectionStr = sp;
  637. mp->bar = bar;
  638. op->obj->text = tp;
  639. errLabel:
  640. if( op != NULL && rc != kOkTlRC )
  641. _cmTlDeleteRecd(p,op);
  642. return rc;
  643. }
  644. cmTlRC_t _cmTlAllocAudioEvtRecd( _cmTl_t* p, const cmChar_t* nameStr, const cmChar_t* refIdStr, int begSmpIdx, unsigned durSmpCnt, unsigned seqId, const cmChar_t* text )
  645. {
  646. cmTlRC_t rc = kOkTlRC;
  647. _cmTlObj_t* op = NULL;
  648. const cmChar_t* textStr = text==NULL ? "" : text;
  649. unsigned recdByteCnt = sizeof(cmTlAudioEvt_t) + strlen(textStr) + 1;
  650. if((rc = _cmTlAllocRecd(p,nameStr,refIdStr,begSmpIdx,durSmpCnt,kAudioEvtTlId,seqId,recdByteCnt,&op)) != kOkTlRC )
  651. goto errLabel;
  652. assert(op != NULL);
  653. cmTlAudioEvt_t* mp = _cmTimeLineAudioEvtObjPtr(p,op->obj);
  654. assert(mp != NULL );
  655. // copy the marker text string into the memory just past the cmTlAudioEvt_t recd.
  656. cmChar_t* tp = (cmChar_t*)(mp + 1);
  657. strcpy(tp,textStr);
  658. mp->text = tp;
  659. op->obj->text = tp;
  660. errLabel:
  661. if( op != NULL && rc != kOkTlRC )
  662. _cmTlDeleteRecd(p,op);
  663. return rc;
  664. }
  665. cmTlRC_t _cmTlAllocRecdFromJson(_cmTl_t* p,const cmChar_t* nameStr, const cmChar_t* typeIdStr,const cmChar_t* refIdStr, int begSmpIdx, unsigned durSmpCnt, unsigned seqId, const cmChar_t* textStr, unsigned bar, const cmChar_t* sectionStr)
  666. {
  667. cmTlRC_t rc = kOkTlRC;
  668. unsigned typeId = _cmTlIdLabelToId(p,typeIdStr);
  669. switch( typeId )
  670. {
  671. case kAudioFileTlId: rc = _cmTlAllocAudioFileRecd(p,nameStr,refIdStr,begSmpIdx, seqId,textStr); break;
  672. case kMidiFileTlId: rc = _cmTlAllocMidiFileRecd( p,nameStr,refIdStr,begSmpIdx, seqId,textStr); break;
  673. case kMarkerTlId: rc = _cmTlAllocMarkerRecd( p,nameStr,refIdStr,begSmpIdx,durSmpCnt,seqId,textStr,bar,sectionStr,kAudioMarkTlId,NULL); break;
  674. case kAudioEvtTlId: rc = _cmTlAllocAudioEvtRecd( p,nameStr,refIdStr,begSmpIdx,durSmpCnt,seqId,textStr); break;
  675. default:
  676. rc = cmErrMsg(&p->err,kParseFailTlRC,"'%s' is not a valid 'objArray' record type.",cmStringNullGuard(typeIdStr));
  677. }
  678. return rc;
  679. }
  680. cmTlRC_t _cmTimeLineFinalize( _cmTl_t* p )
  681. {
  682. cmTlRC_t rc = kOkTlRC;
  683. unsigned i;
  684. for(i=0; i<p->seqCnt; ++i)
  685. while( p->seq[i].first != NULL )
  686. {
  687. if((rc = _cmTlDeleteRecd(p,p->seq[i].first)) != kOkTlRC )
  688. goto errLabel;
  689. }
  690. cmMemFree(p->seq);
  691. cmLHeapDestroy(&p->lH);
  692. cmMemPtrFree(&p->tmpBuf);
  693. cmMemPtrFree(&p->prefixPath);
  694. cmMemPtrFree(&p);
  695. return kOkTlRC;
  696. errLabel:
  697. return cmErrMsg(&p->err,kFinalizeFailTlRC,"Finalize failed.");
  698. }
  699. cmTlRC_t cmTimeLineInitialize( cmCtx_t* ctx, cmTlH_t* hp, cmTlCb_t cbFunc, void* cbArg, const cmChar_t* prefixPath )
  700. {
  701. cmTlRC_t rc;
  702. if((rc = cmTimeLineFinalize(hp)) != kOkTlRC )
  703. return rc;
  704. _cmTl_t* p = cmMemAllocZ( _cmTl_t, 1 );
  705. cmErrSetup(&p->err,&ctx->rpt,"Time Line");
  706. p->ctx = *ctx;
  707. p->cbFunc = cbFunc;
  708. p->cbArg = cbArg;
  709. p->prefixPath = cmMemAllocStr(prefixPath);
  710. if(cmLHeapIsValid( p->lH = cmLHeapCreate( 8192, ctx )) == false )
  711. {
  712. rc = cmErrMsg(&p->err,kLHeapFailTlRC,"The linked heap allocation failed.");
  713. goto errLabel;
  714. }
  715. hp->h = p;
  716. return rc;
  717. errLabel:
  718. _cmTimeLineFinalize(p);
  719. return rc;
  720. }
  721. cmTlRC_t cmTimeLineInitializeFromFile( cmCtx_t* ctx, cmTlH_t* hp, cmTlCb_t cbFunc, void* cbArg, const cmChar_t* fn, const cmChar_t* prefixPath )
  722. {
  723. cmTlRC_t rc;
  724. if((rc = cmTimeLineInitialize(ctx,hp,cbFunc,cbArg,prefixPath)) != kOkTlRC )
  725. return rc;
  726. return cmTimeLineReadJson(hp,fn);
  727. }
  728. const cmChar_t* cmTimeLineFileName( cmTlH_t h )
  729. {
  730. _cmTl_t* p = _cmTlHandleToPtr(h);
  731. return p->filename;
  732. }
  733. const cmChar_t* cmTimeLinePrefixPath( cmTlH_t h )
  734. {
  735. _cmTl_t* p = _cmTlHandleToPtr(h);
  736. return p->prefixPath;
  737. }
  738. cmTlRC_t cmTimeLineFinalize( cmTlH_t* hp )
  739. {
  740. cmTlRC_t rc;
  741. if( hp == NULL || cmTimeLineIsValid(*hp) == false )
  742. return kOkTlRC;
  743. _cmTl_t* p = _cmTlHandleToPtr(*hp);
  744. if((rc = _cmTimeLineFinalize(p)) != kOkTlRC )
  745. return rc;
  746. hp->h = NULL;
  747. return rc;
  748. }
  749. bool cmTimeLineIsValid( cmTlH_t h )
  750. { return h.h != NULL; }
  751. double cmTimeLineSampleRate( cmTlH_t h )
  752. {
  753. _cmTl_t* p = _cmTlHandleToPtr(h);
  754. return p->srate;
  755. }
  756. int cmTimeLineSeqToLocalSampleIndex( int seqSmpIdx, cmTlObj_t* localObjPtr )
  757. {
  758. return seqSmpIdx - localObjPtr->seqSmpIdx;
  759. }
  760. cmTlObj_t* _cmTimeLineIdToObj( _cmTl_t* p, unsigned seqId, unsigned id )
  761. {
  762. assert( seqId < p->seqCnt );
  763. _cmTlObj_t* op = p->seq[seqId].first;
  764. for(; op != NULL; op=op->next )
  765. if( op->obj->uid == id )
  766. return op->obj;
  767. return NULL;
  768. }
  769. cmTlObj_t* cmTimeLineIdToObj( cmTlH_t h, unsigned seqId, unsigned id )
  770. {
  771. _cmTl_t* p = _cmTlHandleToPtr(h);
  772. cmTlObj_t* op = NULL;
  773. if( seqId != cmInvalidId )
  774. op = _cmTimeLineIdToObj(p,seqId,id);
  775. else
  776. {
  777. for(seqId=0; seqId<p->seqCnt; ++seqId)
  778. if((op = _cmTimeLineIdToObj(p,seqId,id) ) != NULL )
  779. break;
  780. }
  781. return op;
  782. }
  783. cmTlObj_t* cmTimeLineNextObj( cmTlH_t h, cmTlObj_t* tp, unsigned seqId )
  784. {
  785. _cmTl_t* p = _cmTlHandleToPtr(h);
  786. _cmTlObj_t* op;
  787. assert( seqId < p->seqCnt );
  788. if( seqId >= p->seqCnt )
  789. return NULL;
  790. // if tp is NULL then start at the begin of the obj list ...
  791. if( tp == NULL )
  792. op = p->seq[seqId].first;
  793. else
  794. {
  795. // ... otherwise advance to the obj after tp
  796. op = (_cmTlObj_t*)tp->reserved;
  797. assert( op != NULL );
  798. op = op->next;
  799. }
  800. // if the list is empty
  801. if( op == NULL )
  802. return NULL;
  803. // return the next object which matches seqId or
  804. // the next object if seqId == cmInvalidId
  805. for(; op != NULL; op = op->next)
  806. if( (seqId == cmInvalidId) || (op->obj->seqId == seqId) )
  807. return op->obj;
  808. return NULL;
  809. }
  810. cmTlObj_t* cmTimeLineNextTypeObj( cmTlH_t h, cmTlObj_t* p, unsigned seqId, unsigned typeId )
  811. {
  812. cmTlObj_t* tp = p;
  813. while( (tp = cmTimeLineNextObj(h,tp,seqId)) != NULL )
  814. if( typeId == cmInvalidId || tp->typeId == typeId )
  815. return tp;
  816. return NULL;
  817. }
  818. cmTlMidiFile_t* cmTlNextMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
  819. {
  820. if((op = cmTimeLineNextTypeObj(h, op, seqId, kMidiFileTlId )) == NULL )
  821. return NULL;
  822. return cmTimeLineMidiFileObjPtr(h,op);
  823. }
  824. cmTlAudioFile_t* cmTlNextAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
  825. {
  826. if((op = cmTimeLineNextTypeObj(h, op, seqId, kAudioFileTlId )) == NULL )
  827. return NULL;
  828. return cmTimeLineAudioFileObjPtr(h,op);
  829. }
  830. cmTlMidiEvt_t* cmTlNextMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
  831. {
  832. if((op = cmTimeLineNextTypeObj(h, op, seqId, kMidiEvtTlId )) == NULL )
  833. return NULL;
  834. return cmTimeLineMidiEvtObjPtr(h,op);
  835. }
  836. cmTlAudioEvt_t* cmTlNextAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
  837. {
  838. if((op = cmTimeLineNextTypeObj(h, op, seqId, kAudioEvtTlId )) == NULL )
  839. return NULL;
  840. return cmTimeLineAudioEvtObjPtr(h,op);
  841. }
  842. cmTlMarker_t* cmTlNextMarkerObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
  843. {
  844. if((op = cmTimeLineNextTypeObj(h, op, seqId, kMarkerTlId )) == NULL )
  845. return NULL;
  846. return cmTimeLineMarkerObjPtr(h,op);
  847. }
  848. cmTlObj_t* cmTlIdToObjPtr( cmTlH_t h, unsigned uid )
  849. {
  850. _cmTl_t* p = _cmTlHandleToPtr(h);
  851. unsigned i;
  852. for(i=0; i<p->seqCnt; ++i)
  853. //if( p->seq[i].first->obj->uid <= uid && uid <= p->seq[i].last->obj->uid )
  854. {
  855. _cmTlObj_t* op = p->seq[i].first;
  856. for(; op != NULL; op=op->next )
  857. if( op->obj->uid == uid )
  858. return op->obj;
  859. }
  860. return NULL;
  861. }
  862. cmTlMidiFile_t* cmTimeLineMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op )
  863. {
  864. _cmTl_t* p = _cmTlHandleToPtr(h);
  865. return _cmTimeLineMidiFileObjPtr(p,op);
  866. }
  867. cmTlAudioFile_t* cmTimeLineAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op )
  868. {
  869. _cmTl_t* p = _cmTlHandleToPtr(h);
  870. return _cmTimeLineAudioFileObjPtr(p,op);
  871. }
  872. cmTlMidiEvt_t* cmTimeLineMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op )
  873. {
  874. _cmTl_t* p = _cmTlHandleToPtr(h);
  875. return _cmTimeLineMidiEvtObjPtr(p,op);
  876. }
  877. cmTlAudioEvt_t* cmTimeLineAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op )
  878. {
  879. _cmTl_t* p = _cmTlHandleToPtr(h);
  880. return _cmTimeLineAudioEvtObjPtr(p,op);
  881. }
  882. cmTlMarker_t* cmTimeLineMarkerObjPtr( cmTlH_t h, cmTlObj_t* op )
  883. {
  884. _cmTl_t* p = _cmTlHandleToPtr(h);
  885. return _cmTimeLineMarkerObjPtr(p,op);
  886. }
  887. cmTlMidiFile_t* cmTlMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op )
  888. {
  889. _cmTl_t* p = _cmTlHandleToPtr(h);
  890. return _cmTlMidiFileObjPtr(p,op,false);
  891. }
  892. cmTlAudioFile_t* cmTlAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op )
  893. {
  894. _cmTl_t* p = _cmTlHandleToPtr(h);
  895. return _cmTlAudioFileObjPtr(p,op,false);
  896. }
  897. cmTlMidiEvt_t* cmTlMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op )
  898. {
  899. _cmTl_t* p = _cmTlHandleToPtr(h);
  900. return _cmTlMidiEvtObjPtr(p,op,false);
  901. }
  902. cmTlAudioEvt_t* cmTlAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op )
  903. {
  904. _cmTl_t* p = _cmTlHandleToPtr(h);
  905. return _cmTlAudioEvtObjPtr(p,op,false);
  906. }
  907. cmTlMarker_t* cmTlMarkerObjPtr( cmTlH_t h, cmTlObj_t* op )
  908. {
  909. _cmTl_t* p = _cmTlHandleToPtr(h);
  910. return _cmTlMarkerObjPtr(p,op,false);
  911. }
  912. cmTlRC_t cmTimeLineInsert( cmTlH_t h, const cmChar_t* nameStr, unsigned typeId,
  913. const cmChar_t* fn, int begSmpIdx, unsigned durSmpCnt, const cmChar_t* refObjNameStr, unsigned seqId )
  914. {
  915. _cmTl_t* p = _cmTlHandleToPtr(h);
  916. return _cmTlAllocRecdFromJson(p, nameStr, _cmTlIdToLabel(p,typeId), refObjNameStr, begSmpIdx, durSmpCnt, seqId, fn, 0, NULL);
  917. }
  918. cmTlObj_t* _cmTimeLineFindFile( _cmTl_t* p, const cmChar_t* fn, unsigned typeId )
  919. {
  920. unsigned i;
  921. for(i=0; i<p->seqCnt; ++i)
  922. {
  923. _cmTlObj_t* op = p->seq[i].first;
  924. for(; op != NULL; op=op->next )
  925. if( op->obj->typeId == typeId )
  926. {
  927. const cmChar_t* objFn = NULL;
  928. switch( typeId )
  929. {
  930. case kAudioFileTlId:
  931. objFn = ((cmTlAudioFile_t*)(op->obj))->fn;
  932. break;
  933. case kMidiFileTlId:
  934. objFn = ((cmTlMidiFile_t*)(op->obj))->fn;
  935. break;
  936. default:
  937. { assert(0); }
  938. }
  939. if( strcmp(objFn,fn) == 0 )
  940. return op->obj;
  941. }
  942. }
  943. return NULL;
  944. }
  945. cmTlAudioFile_t* cmTimeLineFindAudioFile( cmTlH_t h, const cmChar_t* fn )
  946. {
  947. _cmTl_t* p = _cmTlHandleToPtr(h);
  948. cmTlObj_t* op;
  949. if((op = _cmTimeLineFindFile(p,fn,kAudioFileTlId)) != NULL )
  950. return _cmTlAudioFileObjPtr(p,op,true);
  951. return NULL;
  952. }
  953. cmTlMidiFile_t* cmTimeLineFindMidiFile( cmTlH_t h, const cmChar_t* fn )
  954. {
  955. _cmTl_t* p = _cmTlHandleToPtr(h);
  956. cmTlObj_t* op;
  957. if((op = _cmTimeLineFindFile(p,fn,kMidiFileTlId)) != NULL )
  958. return _cmTlMidiFileObjPtr(p,op,true);
  959. return NULL;
  960. }
  961. _cmTlObj_t* _cmTimeLineObjAtTime( _cmTl_t* p, unsigned seqId, unsigned seqSmpIdx, unsigned typeId )
  962. {
  963. assert( seqId < p->seqCnt );
  964. _cmTlObj_t* op = p->seq[seqId].first;
  965. _cmTlObj_t* min_op = NULL;
  966. unsigned minDist = UINT_MAX;
  967. for(; op!=NULL; op=op->next)
  968. if( typeId==cmInvalidId || op->obj->typeId == typeId )
  969. {
  970. // if seqSmpIdx is inside this object - then return it as the solution
  971. if((op->obj->seqSmpIdx <= seqSmpIdx && seqSmpIdx < (op->obj->seqSmpIdx + op->obj->durSmpCnt)))
  972. return op;
  973. // measure the distance from seqSmpIdx to the begin and end of this object
  974. unsigned d0 = op->obj->seqSmpIdx < seqSmpIdx ? seqSmpIdx - op->obj->seqSmpIdx : op->obj->seqSmpIdx - seqSmpIdx;
  975. unsigned d1 = op->obj->seqSmpIdx+op->obj->durSmpCnt < seqSmpIdx ? seqSmpIdx - op->obj->seqSmpIdx+op->obj->durSmpCnt : op->obj->seqSmpIdx+op->obj->durSmpCnt - seqSmpIdx;
  976. // d0 and d1 should decrease as the cur object approaches seqSmpIdx
  977. // If they do not then the search is over - return the closest point.
  978. if( d0>minDist && d1>minDist)
  979. break;
  980. // track the min dist and the assoc'd obj
  981. if( d0 < minDist )
  982. {
  983. minDist = d0;
  984. min_op = op;
  985. }
  986. if( d1 < minDist )
  987. {
  988. minDist = d1;
  989. min_op = op;
  990. }
  991. }
  992. return min_op;
  993. }
  994. cmTlAudioFile_t* cmTimeLineAudioFileAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
  995. {
  996. _cmTl_t* p = _cmTlHandleToPtr(h);
  997. _cmTlObj_t* op;
  998. if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kAudioFileTlId)) == NULL )
  999. return NULL;
  1000. return _cmTlAudioFileObjPtr(p,op->obj,false);
  1001. }
  1002. cmTlMidiFile_t* cmTimeLineMidiFileAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
  1003. {
  1004. _cmTl_t* p = _cmTlHandleToPtr(h);
  1005. _cmTlObj_t* op;
  1006. if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kMidiFileTlId)) == NULL )
  1007. return NULL;
  1008. return _cmTlMidiFileObjPtr(p,op->obj,false);
  1009. }
  1010. cmTlMidiEvt_t* cmTimeLineMidiEvtAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
  1011. {
  1012. _cmTl_t* p = _cmTlHandleToPtr(h);
  1013. _cmTlObj_t* op;
  1014. if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kMidiEvtTlId)) == NULL )
  1015. return NULL;
  1016. return _cmTlMidiEvtObjPtr(p,op->obj,false);
  1017. }
  1018. cmTlMarker_t* cmTimeLineMarkerAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
  1019. {
  1020. _cmTl_t* p = _cmTlHandleToPtr(h);
  1021. _cmTlObj_t* op;
  1022. if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kMarkerTlId)) == NULL )
  1023. return NULL;
  1024. return _cmTlMarkerObjPtr(p,op->obj,false);
  1025. }
  1026. cmTlMarker_t* cmTimeLineMarkerFind( cmTlH_t h, const cmChar_t* markText )
  1027. {
  1028. unsigned i;
  1029. unsigned n = cmTimeLineSeqCount(h);
  1030. cmTlMarker_t* mop = NULL;
  1031. for(i=0; i<n && mop==NULL; ++i)
  1032. {
  1033. cmTlObj_t* op = NULL;
  1034. mop = NULL;
  1035. while((mop = cmTlNextMarkerObjPtr(h,op,i)) != NULL )
  1036. {
  1037. if( mop->obj.name!=NULL && strcmp(mop->obj.name,markText)==0 )
  1038. break;
  1039. op = &mop->obj;
  1040. }
  1041. }
  1042. return mop;
  1043. }
  1044. cmTlRC_t _cmTlParseErr( cmErr_t* err, const cmChar_t* errLabelPtr, unsigned idx, const cmChar_t* fn )
  1045. {
  1046. cmTlRC_t rc;
  1047. if( errLabelPtr != NULL )
  1048. rc = cmErrMsg(err,kParseFailTlRC,"The required time line configuration field %s was not found in the record at index %i in '%s'.",cmStringNullGuard(errLabelPtr),idx,cmStringNullGuard(fn));
  1049. else
  1050. rc = cmErrMsg(err,kParseFailTlRC,"The time_line configuration parse failed on the record at index %i in '%s'.",idx,cmStringNullGuard(fn));
  1051. return rc;
  1052. }
  1053. cmTlRC_t cmTimeLineReadJson( cmTlH_t* hp, const cmChar_t* ifn )
  1054. {
  1055. cmTlRC_t rc = kOkTlRC;
  1056. cmJsRC_t jsRC = kOkJsRC;
  1057. cmJsonH_t jsH = cmJsonNullHandle;
  1058. cmJsonNode_t* jnp = NULL;
  1059. cmJsonNode_t* cnp = NULL;
  1060. const cmChar_t* errLabelPtr = NULL;
  1061. int i;
  1062. _cmTl_t* p = _cmTlHandleToPtr(*hp);
  1063. // open the json file
  1064. if( cmJsonInitializeFromFile(&jsH, ifn, &p->ctx ) != kOkJsRC )
  1065. return cmErrMsg(&p->err,kJsonFailTlRC,"JSON file initialization failed.");
  1066. if((jnp = cmJsonFindValue(jsH,"time_line",cmJsonRoot(jsH),kObjectTId)) == NULL)
  1067. {
  1068. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' object was not found in '%s'.",cmStringNullGuard(ifn));
  1069. goto errLabel;
  1070. }
  1071. if( cmJsonMemberValues(jnp,&errLabelPtr,
  1072. "srate",kRealTId,&p->srate,
  1073. "onset",kObjectTId,&cnp,
  1074. "objArray",kArrayTId,&jnp,
  1075. NULL) != kOkJsRC )
  1076. {
  1077. if(jsRC == kNodeNotFoundJsRC )
  1078. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' header required field:'%s' was not found in '%s'.",errLabelPtr,cmStringNullGuard(ifn));
  1079. else
  1080. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' object header parse failed in '%s'.",cmStringNullGuard(ifn));
  1081. goto errLabel;
  1082. }
  1083. if((jsRC = cmJsonMemberValues(cnp,&errLabelPtr,
  1084. "wndMs", kRealTId, &p->onsetCfg.wndMs,
  1085. "hopFact", kIntTId, &p->onsetCfg.hopFact,
  1086. "audioChIdx", kIntTId, &p->onsetCfg.audioChIdx,
  1087. "wndFrmCnt", kIntTId, &p->onsetCfg.wndFrmCnt,
  1088. "preWndMult", kRealTId, &p->onsetCfg.preWndMult,
  1089. "threshold", kRealTId, &p->onsetCfg.threshold,
  1090. "maxFrqHz", kRealTId, &p->onsetCfg.maxFrqHz,
  1091. "filtCoeff", kRealTId, &p->onsetCfg.filtCoeff,
  1092. "medFiltWndMs", kRealTId, &p->onsetCfg.medFiltWndMs,
  1093. "filterId", kIntTId, &p->onsetCfg.filterId,
  1094. "preDelayMs", kRealTId, &p->onsetCfg.preDelayMs,
  1095. NULL)) != kOkJsRC )
  1096. {
  1097. if(jsRC == kNodeNotFoundJsRC )
  1098. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' onset analysizer cfg. required field:'%s' was not found in '%s'.",errLabelPtr,cmStringNullGuard(ifn));
  1099. else
  1100. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' onset analyzer cfg. in '%s'.",cmStringNullGuard(ifn));
  1101. goto errLabel;
  1102. }
  1103. for(i=0; i<cmJsonChildCount(jnp); ++i)
  1104. {
  1105. const cmJsonNode_t* rp = cmJsonArrayElementC(jnp,i);
  1106. const cmChar_t* nameStr;
  1107. const cmChar_t* typeIdStr;
  1108. const cmChar_t* refIdStr;
  1109. int begSmpIdx;
  1110. unsigned durSmpCnt;
  1111. unsigned seqId;
  1112. const cmChar_t* textStr;
  1113. unsigned bar = 0;
  1114. const cmChar_t* sectStr = NULL;
  1115. if( cmJsonMemberValues(rp,&errLabelPtr,
  1116. "label",kStringTId,&nameStr,
  1117. "type", kStringTId,&typeIdStr,
  1118. "ref", kStringTId,&refIdStr,
  1119. "offset",kIntTId,&begSmpIdx,
  1120. "smpCnt",kIntTId,&durSmpCnt,
  1121. "trackId",kIntTId,&seqId,
  1122. "textStr",kStringTId,&textStr,
  1123. "bar", kIntTId | kOptArgJsFl,&bar,
  1124. "sectStr",kStringTId | kOptArgJsFl,&sectStr,
  1125. NULL) != kOkJsRC )
  1126. {
  1127. rc = _cmTlParseErr(&p->err, errLabelPtr, i, ifn );
  1128. goto errLabel;
  1129. }
  1130. if((rc = _cmTlAllocRecdFromJson(p,nameStr,typeIdStr,refIdStr,begSmpIdx,durSmpCnt,seqId,textStr,bar,sectStr)) != kOkTlRC )
  1131. goto errLabel;
  1132. }
  1133. p->filename = cmLhAllocStr(p->lH,ifn);
  1134. errLabel:
  1135. if( rc != kOkTlRC )
  1136. cmTimeLineFinalize(hp);
  1137. cmJsonFinalize(&jsH);
  1138. return rc;
  1139. }
  1140. unsigned cmTimeLineSeqCount( cmTlH_t h )
  1141. {
  1142. _cmTl_t* p = _cmTlHandleToPtr(h);
  1143. return p->seqCnt;
  1144. }
  1145. cmTlRC_t cmTimeLineSeqNotify( cmTlH_t h, unsigned seqId )
  1146. {
  1147. cmTlRC_t rc = kOkTlRC;
  1148. _cmTl_t* p = _cmTlHandleToPtr(h);
  1149. assert( seqId < p->seqCnt );
  1150. _cmTlNotifyListener(p,kInitMsgTlId,NULL,seqId);
  1151. _cmTlObj_t* op = p->seq[seqId].first;
  1152. for(; op!=NULL; op=op->next)
  1153. if( op->obj->seqId == seqId )
  1154. _cmTlNotifyListener(p, kInsertMsgTlId, op, cmInvalidId );
  1155. _cmTlNotifyListener(p,kDoneMsgTlId,NULL,seqId);
  1156. return rc;
  1157. }
  1158. cmTlRC_t cmTimeLineGenOnsetMarks( cmTlH_t h, unsigned seqId )
  1159. {
  1160. cmTlRC_t rc = kOkTlRC;
  1161. _cmTl_t* p = _cmTlHandleToPtr(h);
  1162. cmTlObj_t* op = NULL;
  1163. cmOnH_t onsH = cmOnsetNullHandle;
  1164. cmChar_t* textStr = "";
  1165. cmChar_t* sectionStr = "";
  1166. unsigned bar = cmInvalidIdx;
  1167. unsigned i,j;
  1168. unsigned smpIdx;
  1169. // initialize the audio onset analyzer
  1170. if( cmOnsetInitialize(&p->ctx, &onsH ) != kOkOnRC )
  1171. {
  1172. rc = cmErrMsg(&p->err,kOnsetFailTlRC,"Audio onset analyzer initialization failed.");
  1173. goto errLabel;
  1174. }
  1175. // for each audio file
  1176. for(i=0; (op = cmTimeLineNextTypeObj(h, op, seqId, kAudioFileTlId )) != NULL; ++i)
  1177. {
  1178. cmTlAudioFile_t* aop = NULL;
  1179. // get a pointer to this audio file object
  1180. if((aop = cmTlAudioFileObjPtr(h, op )) == NULL )
  1181. {
  1182. rc = cmErrMsg(&p->err,kTypeCvtFailTlRC,"Unexpected audio file object type conversion failure.");
  1183. goto errLabel;
  1184. }
  1185. // analyze the onsets in this audio file
  1186. if( cmOnsetProc(onsH,&p->onsetCfg,aop->fn) != kOkOnRC)
  1187. {
  1188. rc = cmErrMsg(&p->err,kOnsetFailTlRC,"Audio onset analyzer failed during processing.");
  1189. goto errLabel;
  1190. }
  1191. // for each detected onset
  1192. for(j=0; (smpIdx = cmOnsetSampleIndex(onsH,j))!=cmInvalidIdx; ++j)
  1193. {
  1194. cmChar_t* labelStr = NULL;
  1195. labelStr = cmTsPrintfP(labelStr,"a-%i-%i",i,j);
  1196. // generate a marker record
  1197. rc = _cmTlAllocMarkerRecd(p, labelStr, aop->obj.name, smpIdx, cmOnsetHopSampleCount(onsH), seqId, textStr, bar, sectionStr, kAudioOnsetMarkTlId,NULL );
  1198. cmMemFree(labelStr);
  1199. if( rc != kOkTlRC )
  1200. goto errLabel;
  1201. }
  1202. }
  1203. unsigned intervalSmpCnt = 0;
  1204. cmTlMidiEvt_t* max_eop = NULL;
  1205. cmTlObj_t* fop = NULL;
  1206. unsigned midiFileObjId = cmInvalidId;
  1207. op = NULL;
  1208. for(i=0; (op = cmTimeLineNextTypeObj(h, op, seqId, kMidiEvtTlId )) != NULL; ++i)
  1209. {
  1210. cmTlMidiEvt_t* eop = NULL;
  1211. // get a pointer to this event object
  1212. if((eop = cmTlMidiEvtObjPtr(h, op )) == NULL )
  1213. {
  1214. rc = cmErrMsg(&p->err,kTypeCvtFailTlRC,"Unexpected MIDI event object type conversion failure.");
  1215. goto errLabel;
  1216. }
  1217. // if this is the first event in a new MIDI file
  1218. if(eop->midiFileObjId != midiFileObjId)
  1219. {
  1220. // get the MIDI file object
  1221. if((fop = cmTimeLineIdToObj(h,seqId,eop->midiFileObjId)) == NULL )
  1222. {
  1223. rc = cmErrMsg(&p->err,kAssertFailTlRC,"The MIDi file associated with a MIDI event could not be found.");
  1224. goto errLabel;
  1225. }
  1226. midiFileObjId = eop->midiFileObjId;
  1227. max_eop = NULL;
  1228. intervalSmpCnt = 0;
  1229. j = 0;
  1230. }
  1231. // if we have reviewed one second of MIDI events
  1232. if( intervalSmpCnt > p->srate )
  1233. {
  1234. // rewind the interval accumulator to less than one second
  1235. while(intervalSmpCnt >= p->srate )
  1236. intervalSmpCnt -= p->srate;
  1237. // if a max veloctiy event was selected ...
  1238. if( max_eop != NULL )
  1239. {
  1240. cmChar_t* labelStr = NULL;
  1241. labelStr = cmTsPrintfP(labelStr,"%i-%i",i,j);
  1242. // ... generate a marker record
  1243. rc = _cmTlAllocMarkerRecd(p, labelStr, NULL, 0, cmOnsetHopSampleCount(onsH), seqId, textStr, bar, sectionStr, kMidiOnsetMarkTlId, &max_eop->obj );
  1244. cmMemFree(labelStr);
  1245. if( rc != kOkTlRC )
  1246. goto errLabel;
  1247. max_eop = NULL;
  1248. ++j;
  1249. }
  1250. }
  1251. // update the max velocity event
  1252. if( max_eop==NULL || (eop->msg->status == kNoteOnMdId && eop->msg->u.chMsgPtr->d1>0 && max_eop->msg->u.chMsgPtr->d1 < eop->msg->u.chMsgPtr->d1) )
  1253. max_eop = eop;
  1254. // increment the interval accumulator
  1255. intervalSmpCnt += eop->obj.begSmpIdx;
  1256. }
  1257. errLabel:
  1258. if( cmOnsetFinalize(&onsH) != kOkOnRC )
  1259. rc = cmErrMsg(&p->err,kOnsetFailTlRC,"Audio onset analyzer finalization failed.");
  1260. return rc;
  1261. }
  1262. cmTlRC_t cmTimeLineDeleteOnsetMarks( cmTlH_t h, unsigned seqId )
  1263. {
  1264. return kOkTlRC;
  1265. }
  1266. void _cmTimeLinePrintObj(_cmTl_t* p, _cmTlObj_t* op, cmRpt_t* rpt )
  1267. {
  1268. cmRptPrintf(rpt,"%2i %5i %9i %9i %s %10s ",op->obj->seqId, op->obj->uid, op->obj->seqSmpIdx, op->obj->begSmpIdx, _cmTlIdToLabel(p,op->obj->typeId),op->obj->name);
  1269. switch(op->obj->typeId )
  1270. {
  1271. case kMidiFileTlId:
  1272. {
  1273. cmTlMidiFile_t* rp = _cmTimeLineMidiFileObjPtr(p,op->obj);
  1274. cmRptPrintf(rpt,"%s ", cmMidiFileName(rp->h));
  1275. }
  1276. break;
  1277. case kMidiEvtTlId:
  1278. {
  1279. cmTlMidiEvt_t* rp = _cmTimeLineMidiEvtObjPtr(p,op->obj);
  1280. if( op->obj->ref != NULL )
  1281. cmRptPrintf(rpt,"%s ",op->obj->ref->name);
  1282. cmRptPrintf(rpt,"%s ",cmMidiStatusToLabel(rp->msg->status));
  1283. }
  1284. break;
  1285. case kAudioFileTlId:
  1286. {
  1287. cmTlAudioFile_t* rp = _cmTimeLineAudioFileObjPtr(p,op->obj);
  1288. cmRptPrintf(rpt,"%s ", cmAudioFileName(rp->h));
  1289. }
  1290. break;
  1291. case kAudioEvtTlId:
  1292. {
  1293. cmTlAudioEvt_t* rp = _cmTimeLineAudioEvtObjPtr(p,op->obj);
  1294. cmRptPrintf(rpt,"%s",rp->text);
  1295. }
  1296. break;
  1297. case kMarkerTlId:
  1298. {
  1299. cmTlMarker_t* rp = _cmTimeLineMarkerObjPtr(p,op->obj);
  1300. cmRptPrintf(rpt,"%s ", rp->text );
  1301. }
  1302. break;
  1303. }
  1304. cmRptPrint(rpt,"\n");
  1305. }
  1306. cmTlRC_t _cmTimeLineWriteRecd( _cmTl_t* p, cmJsonH_t jsH, cmJsonNode_t* np, cmTlObj_t* obj )
  1307. {
  1308. cmTlRC_t rc = kOkTlRC;
  1309. // if this recd is writable and has not been previously written
  1310. if( cmIsNotFlag(obj->flags,kNoWriteTlFl) && cmIsNotFlag(obj->flags,kReservedTlFl) )
  1311. {
  1312. const cmChar_t* refStr = NULL;
  1313. if( obj->ref != NULL )
  1314. {
  1315. refStr = obj->ref->name;
  1316. // if the ref obj was not previously written then write it (recursively) first
  1317. if( cmIsNotFlag(obj->ref->flags, kReservedTlFl) )
  1318. if((rc = _cmTimeLineWriteRecd(p,jsH,np,obj->ref)) != kOkTlRC )
  1319. return rc;
  1320. }
  1321. // create the time-line recd
  1322. if( cmJsonInsertPairs( jsH, cmJsonCreateObject(jsH,np),
  1323. "label", kStringTId, obj->name,
  1324. "type", kStringTId, _cmTlIdToLabel(p, obj->typeId ),
  1325. "ref", kStringTId, refStr==NULL ? "" : refStr,
  1326. "offset", kIntTId, obj->begSmpIdx,
  1327. "smpCnt", kIntTId, obj->durSmpCnt,
  1328. "trackId", kIntTId, obj->seqId,
  1329. "textStr", kStringTId, obj->text==NULL ? "" : obj->text,
  1330. NULL ) != kOkJsRC )
  1331. {
  1332. return cmErrMsg(&p->err,kJsonFailTlRC,"A time line insertion failed on label:%s type:%s text:%s.",cmStringNullGuard(obj->name),cmStringNullGuard(_cmTlIdToLabel(p, obj->typeId )), cmStringNullGuard(obj->text));
  1333. }
  1334. obj->flags = cmSetFlag(obj->flags, kReservedTlFl);
  1335. }
  1336. return rc;
  1337. }
  1338. cmTlRC_t cmTimeLineWrite( cmTlH_t h, const cmChar_t* fn )
  1339. {
  1340. cmTlRC_t rc = kOkTlRC;
  1341. _cmTl_t* p = _cmTlHandleToPtr(h);
  1342. cmJsonH_t jsH = cmJsonNullHandle;
  1343. cmJsonNode_t* np;
  1344. // initialize a JSON tree
  1345. if( cmJsonInitialize(&jsH,&p->ctx) != kOkJsRC )
  1346. return cmErrMsg(&p->err,kJsonFailTlRC,"JSON object initialization failed.");
  1347. // create the root object
  1348. if((np = cmJsonCreateObject(jsH,NULL)) == NULL )
  1349. {
  1350. rc = cmErrMsg(&p->err,kJsonFailTlRC,"Time line root object create failed while creating the time line file '%s.",cmStringNullGuard(fn));
  1351. goto errLabel;
  1352. }
  1353. // create the time-line object
  1354. if((np = cmJsonInsertPairObject(jsH, np, "time_line" )) == NULL )
  1355. {
  1356. rc = cmErrMsg(&p->err,kJsonFailTlRC,"'time_line' object created failed while creating the time line file '%s.",cmStringNullGuard(fn));
  1357. goto errLabel;
  1358. }
  1359. // write the sample rate
  1360. if( cmJsonInsertPairReal(jsH, np, "srate", p->srate ) != kOkJsRC )
  1361. {
  1362. rc = cmErrMsg(&p->err,kJsonFailTlRC,"'sample rate' output failed while creating the time line file '%s.",cmStringNullGuard(fn));
  1363. goto errLabel;
  1364. }
  1365. // create the time-line object array
  1366. if((np = cmJsonInsertPairArray(jsH, np, "objArray")) == NULL )
  1367. {
  1368. rc = cmErrMsg(&p->err,kJsonFailTlRC,"'objArray' output failed while creating the time line file '%s.",cmStringNullGuard(fn));
  1369. goto errLabel;
  1370. }
  1371. unsigned i;
  1372. // write each time-line object
  1373. for(i=0; i<p->seqCnt; ++i)
  1374. {
  1375. _cmTlObj_t* op = p->seq[i].first;
  1376. for(; op != NULL; op=op->next )
  1377. if((rc = _cmTimeLineWriteRecd(p,jsH,np,op->obj)) != kOkTlRC )
  1378. goto errLabel;
  1379. }
  1380. if( cmJsonErrorCode(jsH) == kOkJsRC )
  1381. if( cmJsonWrite(jsH,NULL,fn) != kOkJsRC )
  1382. {
  1383. rc = cmErrMsg(&p->err,kJsonFailTlRC,"JSON write failed.");
  1384. goto errLabel;
  1385. }
  1386. errLabel:
  1387. if( cmJsonFinalize(&jsH) != kOkJsRC )
  1388. {
  1389. rc = cmErrMsg(&p->err,kJsonFailTlRC,"JSON finalize failed.");
  1390. goto errLabel;
  1391. }
  1392. return rc;
  1393. }
  1394. cmTlRC_t cmTimeLinePrint( cmTlH_t h, cmRpt_t* rpt )
  1395. {
  1396. _cmTl_t* p = _cmTlHandleToPtr(h);
  1397. unsigned i;
  1398. for(i=0; i<p->seqCnt; ++i)
  1399. {
  1400. _cmTlObj_t* op = p->seq[i].first;
  1401. while(op != NULL)
  1402. {
  1403. _cmTimeLinePrintObj(p,op,rpt);
  1404. op = op->next;
  1405. }
  1406. }
  1407. return kOkTlRC;
  1408. }
  1409. cmTlRC_t cmTimeLinePrintFn( cmCtx_t* ctx, const cmChar_t* fn, const cmChar_t* prefixPath, cmRpt_t* rpt )
  1410. {
  1411. cmTlRC_t rc;
  1412. cmTlH_t h = cmTimeLineNullHandle;
  1413. if((rc = cmTimeLineInitializeFromFile(ctx,&h,NULL,NULL,fn,prefixPath)) != kOkTlRC )
  1414. return rc;
  1415. cmTimeLinePrint(h,rpt);
  1416. return cmTimeLineFinalize(&h);
  1417. }
  1418. cmTlRC_t cmTimeLineTest( cmCtx_t* ctx, const cmChar_t* jsFn, const cmChar_t* prefixPath )
  1419. {
  1420. cmTlRC_t rc = kOkTlRC;
  1421. cmTlH_t tlH = cmTimeLineNullHandle;
  1422. if((rc = cmTimeLineInitialize(ctx,&tlH,NULL,NULL,prefixPath)) != kOkTlRC )
  1423. return rc;
  1424. if((rc = cmTimeLineReadJson(&tlH,jsFn)) != kOkTlRC )
  1425. goto errLabel;
  1426. if((rc = cmTimeLineInsert(tlH,"Mark",kMarkerTlId,"My Marker",10,0,NULL,0)) != kOkTlRC )
  1427. goto errLabel;
  1428. cmTimeLinePrint(tlH, &ctx->rpt );
  1429. errLabel:
  1430. cmTimeLineFinalize(&tlH);
  1431. return rc;
  1432. }
  1433. cmTlRC_t _cmTimeLineDecodeObj( const void* msg, unsigned msgByteCnt, cmTlUiMsg_t* r )
  1434. {
  1435. cmTlRC_t rc = kOkTlRC;
  1436. unsigned* typeIdPtr = (unsigned*)msg;
  1437. unsigned* parentIdPtr = typeIdPtr + 1;
  1438. const char* text = (const char*)(parentIdPtr + 1);
  1439. _cmTlObj_t* op = (_cmTlObj_t*)(text + strlen(text) + 1);
  1440. cmTlObj_t* tp = (cmTlObj_t*)(op + 1 );
  1441. r->msgId = kInsertMsgTlId;
  1442. r->objId = tp->uid;
  1443. r->seqId = tp->seqId;
  1444. r->srate = 0;
  1445. return rc;
  1446. }
  1447. cmTlRC_t cmTimeLineDecode( const void* msg, unsigned msgByteCnt, cmTlUiMsg_t* r )
  1448. {
  1449. cmTlRC_t rc = kOkTlRC;
  1450. _cmTlMsg_t* m = (_cmTlMsg_t*)msg;
  1451. switch( m->typeId )
  1452. {
  1453. case kMsgTlId:
  1454. r->msgId = m->msgId;
  1455. r->srate = m->srate;
  1456. r->seqCnt= m->seqCnt;
  1457. r->seqId = m->seqId;
  1458. break;
  1459. case kObjTlId:
  1460. r->msgId = kInsertMsgTlId;
  1461. rc = _cmTimeLineDecodeObj(msg,msgByteCnt,r);
  1462. break;
  1463. default:
  1464. assert(0);
  1465. }
  1466. return rc;
  1467. }