libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmTimeLine.c 51KB

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