libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmTimeLine.c 50KB

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