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

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