libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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