libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmTimeLine.c 41KB

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