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

cmTimeLine.c 46KB

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