libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmTimeLine.c 45KB

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