libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmTimeLine.c 51KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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 to MIDI file 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 to MIDI event 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 to audio event 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 to marker object 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(&p->ctx, &mfH, fn ) != 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. bool inFl = false;
  968. // for each object in the requested sequence
  969. for(; op!=NULL; op=op->next)
  970. if( typeId==cmInvalidId || op->obj->typeId == typeId )
  971. {
  972. bool in0Fl = false;
  973. // if seqSmpIdx is inside this object - then the returned object must contain seqSmpIdx
  974. // (but the ideal point to return is the one which contains seqSmpIdx and also has
  975. // a begin or end point very close to seqSmpIdx - so this defer selecting an object
  976. // until all objects which may contain seqSmpIdx have been examined
  977. if((op->obj->seqSmpIdx <= seqSmpIdx && seqSmpIdx < (op->obj->seqSmpIdx + op->obj->durSmpCnt)))
  978. {
  979. inFl = true; // the returned object must contain seqSmpIdx
  980. in0Fl = true; // this object contains seqSmpIdx
  981. }
  982. // measure the distance from seqSmpIdx to the begin and end of this object
  983. unsigned d0 = op->obj->seqSmpIdx < seqSmpIdx ? seqSmpIdx - op->obj->seqSmpIdx : op->obj->seqSmpIdx - seqSmpIdx;
  984. unsigned d1 = op->obj->seqSmpIdx+op->obj->durSmpCnt < seqSmpIdx ? seqSmpIdx - op->obj->seqSmpIdx+op->obj->durSmpCnt : op->obj->seqSmpIdx+op->obj->durSmpCnt - seqSmpIdx;
  985. // track the min dist and the assoc'd obj
  986. if( d0 < minDist && (inFl==false || inFl==in0Fl))
  987. {
  988. minDist = d0;
  989. min_op = op;
  990. }
  991. if( d1 < minDist && (inFl==false || inFl==in0Fl))
  992. {
  993. minDist = d1;
  994. min_op = op;
  995. }
  996. }
  997. return min_op;
  998. }
  999. cmTlAudioFile_t* cmTimeLineAudioFileAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
  1000. {
  1001. _cmTl_t* p = _cmTlHandleToPtr(h);
  1002. _cmTlObj_t* op;
  1003. if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kAudioFileTlId)) == NULL )
  1004. return NULL;
  1005. return _cmTlAudioFileObjPtr(p,op->obj,false);
  1006. }
  1007. cmTlMidiFile_t* cmTimeLineMidiFileAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
  1008. {
  1009. _cmTl_t* p = _cmTlHandleToPtr(h);
  1010. _cmTlObj_t* op;
  1011. if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kMidiFileTlId)) == NULL )
  1012. return NULL;
  1013. return _cmTlMidiFileObjPtr(p,op->obj,false);
  1014. }
  1015. cmTlMidiEvt_t* cmTimeLineMidiEvtAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
  1016. {
  1017. _cmTl_t* p = _cmTlHandleToPtr(h);
  1018. _cmTlObj_t* op;
  1019. if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kMidiEvtTlId)) == NULL )
  1020. return NULL;
  1021. return _cmTlMidiEvtObjPtr(p,op->obj,false);
  1022. }
  1023. cmTlMarker_t* cmTimeLineMarkerAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
  1024. {
  1025. _cmTl_t* p = _cmTlHandleToPtr(h);
  1026. _cmTlObj_t* op;
  1027. if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kMarkerTlId)) == NULL )
  1028. return NULL;
  1029. return _cmTlMarkerObjPtr(p,op->obj,false);
  1030. }
  1031. cmTlMarker_t* cmTimeLineMarkerFind( cmTlH_t h, const cmChar_t* markText )
  1032. {
  1033. unsigned i;
  1034. unsigned n = cmTimeLineSeqCount(h);
  1035. cmTlMarker_t* mop = NULL;
  1036. for(i=0; i<n && mop==NULL; ++i)
  1037. {
  1038. cmTlObj_t* op = NULL;
  1039. mop = NULL;
  1040. while((mop = cmTlNextMarkerObjPtr(h,op,i)) != NULL )
  1041. {
  1042. if( mop->obj.name!=NULL && strcmp(mop->obj.name,markText)==0 )
  1043. break;
  1044. op = &mop->obj;
  1045. }
  1046. }
  1047. return mop;
  1048. }
  1049. cmTlRC_t _cmTlParseErr( cmErr_t* err, const cmChar_t* errLabelPtr, unsigned idx, const cmChar_t* fn )
  1050. {
  1051. cmTlRC_t rc;
  1052. if( errLabelPtr != NULL )
  1053. 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));
  1054. else
  1055. rc = cmErrMsg(err,kParseFailTlRC,"The time_line configuration parse failed on the record at index %i in '%s'.",idx,cmStringNullGuard(fn));
  1056. return rc;
  1057. }
  1058. cmTlRC_t cmTimeLineReadJson( cmTlH_t* hp, const cmChar_t* ifn )
  1059. {
  1060. cmTlRC_t rc = kOkTlRC;
  1061. cmJsRC_t jsRC = kOkJsRC;
  1062. cmJsonH_t jsH = cmJsonNullHandle;
  1063. cmJsonNode_t* jnp = NULL;
  1064. cmJsonNode_t* cnp = NULL;
  1065. const cmChar_t* errLabelPtr = NULL;
  1066. int i;
  1067. _cmTl_t* p = _cmTlHandleToPtr(*hp);
  1068. // open the json file
  1069. if( cmJsonInitializeFromFile(&jsH, ifn, &p->ctx ) != kOkJsRC )
  1070. return cmErrMsg(&p->err,kJsonFailTlRC,"JSON file initialization failed.");
  1071. if((jnp = cmJsonFindValue(jsH,"time_line",cmJsonRoot(jsH),kObjectTId)) == NULL)
  1072. {
  1073. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' object was not found in '%s'.",cmStringNullGuard(ifn));
  1074. goto errLabel;
  1075. }
  1076. if( cmJsonMemberValues(jnp,&errLabelPtr,
  1077. "srate",kRealTId,&p->srate,
  1078. "onset",kObjectTId | kOptArgJsFl,&cnp,
  1079. "objArray",kArrayTId,&jnp,
  1080. NULL) != kOkJsRC )
  1081. {
  1082. if(jsRC == kNodeNotFoundJsRC )
  1083. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' header required field:'%s' was not found in '%s'.",errLabelPtr,cmStringNullGuard(ifn));
  1084. else
  1085. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' object header parse failed in '%s'.",cmStringNullGuard(ifn));
  1086. goto errLabel;
  1087. }
  1088. if( cnp != NULL )
  1089. {
  1090. if((jsRC = cmJsonMemberValues(cnp,&errLabelPtr,
  1091. "wndMs", kRealTId, &p->onsetCfg.wndMs,
  1092. "hopFact", kIntTId, &p->onsetCfg.hopFact,
  1093. "audioChIdx", kIntTId, &p->onsetCfg.audioChIdx,
  1094. "wndFrmCnt", kIntTId, &p->onsetCfg.wndFrmCnt,
  1095. "preWndMult", kRealTId, &p->onsetCfg.preWndMult,
  1096. "threshold", kRealTId, &p->onsetCfg.threshold,
  1097. "maxFrqHz", kRealTId, &p->onsetCfg.maxFrqHz,
  1098. "filtCoeff", kRealTId, &p->onsetCfg.filtCoeff,
  1099. "medFiltWndMs", kRealTId, &p->onsetCfg.medFiltWndMs,
  1100. "filterId", kIntTId, &p->onsetCfg.filterId,
  1101. "preDelayMs", kRealTId, &p->onsetCfg.preDelayMs,
  1102. NULL)) != kOkJsRC )
  1103. {
  1104. if(jsRC == kNodeNotFoundJsRC )
  1105. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' onset analysizer cfg. required field:'%s' was not found in '%s'.",errLabelPtr,cmStringNullGuard(ifn));
  1106. else
  1107. rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' onset analyzer cfg. in '%s'.",cmStringNullGuard(ifn));
  1108. goto errLabel;
  1109. }
  1110. }
  1111. for(i=0; i<cmJsonChildCount(jnp); ++i)
  1112. {
  1113. const cmJsonNode_t* rp = cmJsonArrayElementC(jnp,i);
  1114. const cmChar_t* nameStr;
  1115. const cmChar_t* typeIdStr;
  1116. const cmChar_t* refIdStr;
  1117. int begSmpIdx;
  1118. unsigned durSmpCnt;
  1119. unsigned seqId;
  1120. const cmChar_t* textStr;
  1121. unsigned bar = 0;
  1122. const cmChar_t* sectStr = NULL;
  1123. if( cmJsonMemberValues(rp,&errLabelPtr,
  1124. "label",kStringTId,&nameStr,
  1125. "type", kStringTId,&typeIdStr,
  1126. "ref", kStringTId,&refIdStr,
  1127. "offset",kIntTId,&begSmpIdx,
  1128. "smpCnt",kIntTId,&durSmpCnt,
  1129. "trackId",kIntTId,&seqId,
  1130. "textStr",kStringTId,&textStr,
  1131. "bar", kIntTId | kOptArgJsFl,&bar,
  1132. "sectStr",kStringTId | kOptArgJsFl,&sectStr,
  1133. NULL) != kOkJsRC )
  1134. {
  1135. rc = _cmTlParseErr(&p->err, errLabelPtr, i, ifn );
  1136. goto errLabel;
  1137. }
  1138. if((rc = _cmTlAllocRecdFromJson(p,nameStr,typeIdStr,refIdStr,begSmpIdx,durSmpCnt,seqId,textStr,bar,sectStr)) != kOkTlRC )
  1139. goto errLabel;
  1140. }
  1141. p->filename = cmLhAllocStr(p->lH,ifn);
  1142. errLabel:
  1143. if( rc != kOkTlRC )
  1144. cmTimeLineFinalize(hp);
  1145. cmJsonFinalize(&jsH);
  1146. return rc;
  1147. }
  1148. unsigned cmTimeLineSeqCount( cmTlH_t h )
  1149. {
  1150. _cmTl_t* p = _cmTlHandleToPtr(h);
  1151. return p->seqCnt;
  1152. }
  1153. cmTlRC_t cmTimeLineSeqNotify( cmTlH_t h, unsigned seqId )
  1154. {
  1155. cmTlRC_t rc = kOkTlRC;
  1156. _cmTl_t* p = _cmTlHandleToPtr(h);
  1157. assert( seqId < p->seqCnt );
  1158. _cmTlNotifyListener(p,kInitMsgTlId,NULL,seqId);
  1159. _cmTlObj_t* op = p->seq[seqId].first;
  1160. for(; op!=NULL; op=op->next)
  1161. if( op->obj->seqId == seqId )
  1162. _cmTlNotifyListener(p, kInsertMsgTlId, op, cmInvalidId );
  1163. _cmTlNotifyListener(p,kDoneMsgTlId,NULL,seqId);
  1164. return rc;
  1165. }
  1166. cmTlRC_t cmTimeLineGenOnsetMarks( cmTlH_t h, unsigned seqId )
  1167. {
  1168. cmTlRC_t rc = kOkTlRC;
  1169. _cmTl_t* p = _cmTlHandleToPtr(h);
  1170. cmTlObj_t* op = NULL;
  1171. cmOnH_t onsH = cmOnsetNullHandle;
  1172. cmChar_t* textStr = "";
  1173. cmChar_t* sectionStr = "";
  1174. unsigned bar = cmInvalidIdx;
  1175. unsigned i,j;
  1176. unsigned smpIdx;
  1177. if( p->onsetCfg.wndMs == 0 )
  1178. {
  1179. rc = cmErrMsg(&p->err,kOnsetFailTlRC,"Audio onset analyzer not-configured.");
  1180. goto errLabel;
  1181. }
  1182. // initialize the audio onset analyzer
  1183. if( cmOnsetInitialize(&p->ctx, &onsH ) != kOkOnRC )
  1184. {
  1185. rc = cmErrMsg(&p->err,kOnsetFailTlRC,"Audio onset analyzer initialization failed.");
  1186. goto errLabel;
  1187. }
  1188. // for each audio file
  1189. for(i=0; (op = cmTimeLineNextTypeObj(h, op, seqId, kAudioFileTlId )) != NULL; ++i)
  1190. {
  1191. cmTlAudioFile_t* aop = NULL;
  1192. // get a pointer to this audio file object
  1193. if((aop = cmTlAudioFileObjPtr(h, op )) == NULL )
  1194. {
  1195. rc = cmErrMsg(&p->err,kTypeCvtFailTlRC,"Unexpected audio file object type conversion failure.");
  1196. goto errLabel;
  1197. }
  1198. // analyze the onsets in this audio file
  1199. if( cmOnsetProc(onsH,&p->onsetCfg,aop->fn) != kOkOnRC)
  1200. {
  1201. rc = cmErrMsg(&p->err,kOnsetFailTlRC,"Audio onset analyzer failed during processing.");
  1202. goto errLabel;
  1203. }
  1204. // for each detected onset
  1205. for(j=0; (smpIdx = cmOnsetSampleIndex(onsH,j))!=cmInvalidIdx; ++j)
  1206. {
  1207. cmChar_t* labelStr = NULL;
  1208. labelStr = cmTsPrintfP(labelStr,"a-%i-%i",i,j);
  1209. // generate a marker record
  1210. rc = _cmTlAllocMarkerRecd(p, labelStr, aop->obj.name, smpIdx, cmOnsetHopSampleCount(onsH), seqId, textStr, bar, sectionStr, kAudioOnsetMarkTlId,NULL );
  1211. cmMemFree(labelStr);
  1212. if( rc != kOkTlRC )
  1213. goto errLabel;
  1214. }
  1215. }
  1216. unsigned intervalSmpCnt = 0;
  1217. cmTlMidiEvt_t* max_eop = NULL;
  1218. cmTlObj_t* fop = NULL;
  1219. unsigned midiFileObjId = cmInvalidId;
  1220. op = NULL;
  1221. for(i=0; (op = cmTimeLineNextTypeObj(h, op, seqId, kMidiEvtTlId )) != NULL; ++i)
  1222. {
  1223. cmTlMidiEvt_t* eop = NULL;
  1224. // get a pointer to this event object
  1225. if((eop = cmTlMidiEvtObjPtr(h, op )) == NULL )
  1226. {
  1227. rc = cmErrMsg(&p->err,kTypeCvtFailTlRC,"Unexpected MIDI event object type conversion failure.");
  1228. goto errLabel;
  1229. }
  1230. // if this is the first event in a new MIDI file
  1231. if(eop->midiFileObjId != midiFileObjId)
  1232. {
  1233. // get the MIDI file object
  1234. if((fop = cmTimeLineIdToObj(h,seqId,eop->midiFileObjId)) == NULL )
  1235. {
  1236. rc = cmErrMsg(&p->err,kAssertFailTlRC,"The MIDi file associated with a MIDI event could not be found.");
  1237. goto errLabel;
  1238. }
  1239. midiFileObjId = eop->midiFileObjId;
  1240. max_eop = NULL;
  1241. intervalSmpCnt = 0;
  1242. j = 0;
  1243. }
  1244. // if we have reviewed one second of MIDI events
  1245. if( intervalSmpCnt > p->srate )
  1246. {
  1247. // rewind the interval accumulator to less than one second
  1248. while(intervalSmpCnt >= p->srate )
  1249. intervalSmpCnt -= p->srate;
  1250. // if a max veloctiy event was selected ...
  1251. if( max_eop != NULL )
  1252. {
  1253. cmChar_t* labelStr = NULL;
  1254. labelStr = cmTsPrintfP(labelStr,"%i-%i",i,j);
  1255. // ... generate a marker record
  1256. rc = _cmTlAllocMarkerRecd(p, labelStr, NULL, 0, cmOnsetHopSampleCount(onsH), seqId, textStr, bar, sectionStr, kMidiOnsetMarkTlId, &max_eop->obj );
  1257. cmMemFree(labelStr);
  1258. if( rc != kOkTlRC )
  1259. goto errLabel;
  1260. max_eop = NULL;
  1261. ++j;
  1262. }
  1263. }
  1264. // update the max velocity event
  1265. 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) )
  1266. max_eop = eop;
  1267. // increment the interval accumulator
  1268. intervalSmpCnt += eop->obj.begSmpIdx;
  1269. }
  1270. errLabel:
  1271. if( cmOnsetFinalize(&onsH) != kOkOnRC )
  1272. rc = cmErrMsg(&p->err,kOnsetFailTlRC,"Audio onset analyzer finalization failed.");
  1273. return rc;
  1274. }
  1275. cmTlRC_t cmTimeLineDeleteOnsetMarks( cmTlH_t h, unsigned seqId )
  1276. {
  1277. return kOkTlRC;
  1278. }
  1279. void _cmTimeLinePrintObj(_cmTl_t* p, _cmTlObj_t* op, cmRpt_t* rpt )
  1280. {
  1281. 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);
  1282. switch(op->obj->typeId )
  1283. {
  1284. case kMidiFileTlId:
  1285. {
  1286. cmTlMidiFile_t* rp = _cmTimeLineMidiFileObjPtr(p,op->obj);
  1287. cmRptPrintf(rpt,"%s ", cmMidiFileName(rp->h));
  1288. }
  1289. break;
  1290. case kMidiEvtTlId:
  1291. {
  1292. cmTlMidiEvt_t* rp = _cmTimeLineMidiEvtObjPtr(p,op->obj);
  1293. if( op->obj->ref != NULL )
  1294. cmRptPrintf(rpt,"%s ",op->obj->ref->name);
  1295. cmRptPrintf(rpt,"%s ",cmMidiStatusToLabel(rp->msg->status));
  1296. }
  1297. break;
  1298. case kAudioFileTlId:
  1299. {
  1300. cmTlAudioFile_t* rp = _cmTimeLineAudioFileObjPtr(p,op->obj);
  1301. cmRptPrintf(rpt,"%s ", cmAudioFileName(rp->h));
  1302. }
  1303. break;
  1304. case kAudioEvtTlId:
  1305. {
  1306. cmTlAudioEvt_t* rp = _cmTimeLineAudioEvtObjPtr(p,op->obj);
  1307. cmRptPrintf(rpt,"%s",rp->text);
  1308. }
  1309. break;
  1310. case kMarkerTlId:
  1311. {
  1312. cmTlMarker_t* rp = _cmTimeLineMarkerObjPtr(p,op->obj);
  1313. cmRptPrintf(rpt,"%s ", rp->text );
  1314. }
  1315. break;
  1316. }
  1317. cmRptPrint(rpt,"\n");
  1318. }
  1319. cmTlRC_t _cmTimeLineWriteRecd( _cmTl_t* p, cmJsonH_t jsH, cmJsonNode_t* np, cmTlObj_t* obj )
  1320. {
  1321. cmTlRC_t rc = kOkTlRC;
  1322. // if this recd is writable and has not been previously written
  1323. if( cmIsNotFlag(obj->flags,kNoWriteTlFl) && cmIsNotFlag(obj->flags,kReservedTlFl) )
  1324. {
  1325. const cmChar_t* refStr = NULL;
  1326. if( obj->ref != NULL )
  1327. {
  1328. refStr = obj->ref->name;
  1329. // if the ref obj was not previously written then write it (recursively) first
  1330. if( cmIsNotFlag(obj->ref->flags, kReservedTlFl) )
  1331. if((rc = _cmTimeLineWriteRecd(p,jsH,np,obj->ref)) != kOkTlRC )
  1332. return rc;
  1333. }
  1334. // create the time-line recd
  1335. if( cmJsonInsertPairs( jsH, cmJsonCreateObject(jsH,np),
  1336. "label", kStringTId, obj->name,
  1337. "type", kStringTId, _cmTlIdToLabel(p, obj->typeId ),
  1338. "ref", kStringTId, refStr==NULL ? "" : refStr,
  1339. "offset", kIntTId, obj->begSmpIdx,
  1340. "smpCnt", kIntTId, obj->durSmpCnt,
  1341. "trackId", kIntTId, obj->seqId,
  1342. "textStr", kStringTId, obj->text==NULL ? "" : obj->text,
  1343. NULL ) != kOkJsRC )
  1344. {
  1345. 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));
  1346. }
  1347. obj->flags = cmSetFlag(obj->flags, kReservedTlFl);
  1348. }
  1349. return rc;
  1350. }
  1351. cmTlRC_t cmTimeLineWrite( cmTlH_t h, const cmChar_t* fn )
  1352. {
  1353. cmTlRC_t rc = kOkTlRC;
  1354. _cmTl_t* p = _cmTlHandleToPtr(h);
  1355. cmJsonH_t jsH = cmJsonNullHandle;
  1356. cmJsonNode_t* np;
  1357. // initialize a JSON tree
  1358. if( cmJsonInitialize(&jsH,&p->ctx) != kOkJsRC )
  1359. return cmErrMsg(&p->err,kJsonFailTlRC,"JSON object initialization failed.");
  1360. // create the root object
  1361. if((np = cmJsonCreateObject(jsH,NULL)) == NULL )
  1362. {
  1363. rc = cmErrMsg(&p->err,kJsonFailTlRC,"Time line root object create failed while creating the time line file '%s.",cmStringNullGuard(fn));
  1364. goto errLabel;
  1365. }
  1366. // create the time-line object
  1367. if((np = cmJsonInsertPairObject(jsH, np, "time_line" )) == NULL )
  1368. {
  1369. rc = cmErrMsg(&p->err,kJsonFailTlRC,"'time_line' object created failed while creating the time line file '%s.",cmStringNullGuard(fn));
  1370. goto errLabel;
  1371. }
  1372. // write the sample rate
  1373. if( cmJsonInsertPairReal(jsH, np, "srate", p->srate ) != kOkJsRC )
  1374. {
  1375. rc = cmErrMsg(&p->err,kJsonFailTlRC,"'sample rate' output failed while creating the time line file '%s.",cmStringNullGuard(fn));
  1376. goto errLabel;
  1377. }
  1378. // create the time-line object array
  1379. if((np = cmJsonInsertPairArray(jsH, np, "objArray")) == NULL )
  1380. {
  1381. rc = cmErrMsg(&p->err,kJsonFailTlRC,"'objArray' output failed while creating the time line file '%s.",cmStringNullGuard(fn));
  1382. goto errLabel;
  1383. }
  1384. unsigned i;
  1385. // write each time-line object
  1386. for(i=0; i<p->seqCnt; ++i)
  1387. {
  1388. _cmTlObj_t* op = p->seq[i].first;
  1389. for(; op != NULL; op=op->next )
  1390. if((rc = _cmTimeLineWriteRecd(p,jsH,np,op->obj)) != kOkTlRC )
  1391. goto errLabel;
  1392. }
  1393. if( cmJsonErrorCode(jsH) == kOkJsRC )
  1394. if( cmJsonWrite(jsH,NULL,fn) != kOkJsRC )
  1395. {
  1396. rc = cmErrMsg(&p->err,kJsonFailTlRC,"JSON write failed.");
  1397. goto errLabel;
  1398. }
  1399. errLabel:
  1400. if( cmJsonFinalize(&jsH) != kOkJsRC )
  1401. {
  1402. rc = cmErrMsg(&p->err,kJsonFailTlRC,"JSON finalize failed.");
  1403. goto errLabel;
  1404. }
  1405. return rc;
  1406. }
  1407. cmTlRC_t cmTimeLinePrint( cmTlH_t h, cmRpt_t* rpt )
  1408. {
  1409. _cmTl_t* p = _cmTlHandleToPtr(h);
  1410. unsigned i;
  1411. for(i=0; i<p->seqCnt; ++i)
  1412. {
  1413. _cmTlObj_t* op = p->seq[i].first;
  1414. while(op != NULL)
  1415. {
  1416. _cmTimeLinePrintObj(p,op,rpt);
  1417. op = op->next;
  1418. }
  1419. }
  1420. return kOkTlRC;
  1421. }
  1422. cmTlRC_t cmTimeLinePrintFn( cmCtx_t* ctx, const cmChar_t* fn, const cmChar_t* prefixPath, cmRpt_t* rpt )
  1423. {
  1424. cmTlRC_t rc;
  1425. cmTlH_t h = cmTimeLineNullHandle;
  1426. if((rc = cmTimeLineInitializeFromFile(ctx,&h,NULL,NULL,fn,prefixPath)) != kOkTlRC )
  1427. return rc;
  1428. cmTimeLinePrint(h,rpt);
  1429. return cmTimeLineFinalize(&h);
  1430. }
  1431. cmTlRC_t cmTimeLineTest( cmCtx_t* ctx, const cmChar_t* jsFn, const cmChar_t* prefixPath )
  1432. {
  1433. cmTlRC_t rc = kOkTlRC;
  1434. cmTlH_t tlH = cmTimeLineNullHandle;
  1435. if((rc = cmTimeLineInitialize(ctx,&tlH,NULL,NULL,prefixPath)) != kOkTlRC )
  1436. return rc;
  1437. if((rc = cmTimeLineReadJson(&tlH,jsFn)) != kOkTlRC )
  1438. goto errLabel;
  1439. if((rc = cmTimeLineInsert(tlH,"Mark",kMarkerTlId,"My Marker",10,0,NULL,0)) != kOkTlRC )
  1440. goto errLabel;
  1441. cmTimeLinePrint(tlH, &ctx->rpt );
  1442. errLabel:
  1443. cmTimeLineFinalize(&tlH);
  1444. return rc;
  1445. }
  1446. cmTlRC_t _cmTimeLineDecodeObj( const void* msg, unsigned msgByteCnt, cmTlUiMsg_t* r )
  1447. {
  1448. cmTlRC_t rc = kOkTlRC;
  1449. unsigned* typeIdPtr = (unsigned*)msg;
  1450. unsigned* parentIdPtr = typeIdPtr + 1;
  1451. const char* text = (const char*)(parentIdPtr + 1);
  1452. _cmTlObj_t* op = (_cmTlObj_t*)(text + strlen(text) + 1);
  1453. cmTlObj_t* tp = (cmTlObj_t*)(op + 1 );
  1454. r->msgId = kInsertMsgTlId;
  1455. r->objId = tp->uid;
  1456. r->seqId = tp->seqId;
  1457. r->srate = 0;
  1458. return rc;
  1459. }
  1460. cmTlRC_t cmTimeLineDecode( const void* msg, unsigned msgByteCnt, cmTlUiMsg_t* r )
  1461. {
  1462. cmTlRC_t rc = kOkTlRC;
  1463. _cmTlMsg_t* m = (_cmTlMsg_t*)msg;
  1464. switch( m->typeId )
  1465. {
  1466. case kMsgTlId:
  1467. r->msgId = m->msgId;
  1468. r->srate = m->srate;
  1469. r->seqCnt= m->seqCnt;
  1470. r->seqId = m->seqId;
  1471. break;
  1472. case kObjTlId:
  1473. r->msgId = kInsertMsgTlId;
  1474. rc = _cmTimeLineDecodeObj(msg,msgByteCnt,r);
  1475. break;
  1476. default:
  1477. assert(0);
  1478. }
  1479. return rc;
  1480. }