123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680 |
- #include "cmGlobal.h"
- #include "cmFloatTypes.h"
- #include "cmRpt.h"
- #include "cmErr.h"
- #include "cmCtx.h"
- #include "cmMem.h"
- #include "cmMallocDebug.h"
- #include "cmLinkedHeap.h"
- #include "cmJson.h"
- #include "cmAudioFile.h"
- #include "cmMidi.h"
- #include "cmMidiFile.h"
- #include "cmTimeLine.h"
-
- // id's used to track the type of a serialized object
- enum
- {
- kMsgTlId,
- kObjTlId
- };
-
-
- //
- typedef struct _cmTlMsg_str
- {
- unsigned typeId; // always set to kMsgTlId
- cmTlUiMsgTypeId_t msgId; //
- double srate;
- unsigned seqCnt;
- unsigned seqId;
- } _cmTlMsg_t;
-
-
- typedef struct _cmTlObj_str
- {
- void* mem;
- unsigned memByteCnt;
- cmTlObj_t* obj;
- struct _cmTlObj_str* prev;
- struct _cmTlObj_str* next;
- } _cmTlObj_t;
-
- typedef struct
- {
- _cmTlObj_t* first;
- _cmTlObj_t* last;
- } _cmTlSeq_t;
-
- typedef struct
- {
- cmErr_t err;
- cmCtx_t ctx;
- cmLHeapH_t lH;
- double srate;
- unsigned nextSeqId;
- cmTlCb_t cbFunc;
- void* cbArg;
- unsigned nextUId;
- char* tmpBuf;
- unsigned seqCnt;
- _cmTlSeq_t* seq; // seq[seqCnt]
- const cmChar_t* filename;
- } _cmTl_t;
-
- typedef struct
- {
- char label[8];
- unsigned id;
- } _cmTlId_t;
-
- _cmTlId_t _cmTlIdArray[] =
- {
- { "mf", kMidiFileTlId },
- { "me", kMidiEvtTlId },
- { "af", kAudioFileTlId },
- { "ae", kAudioEvtTlId },
- { "mk", kMarkerTlId },
- { "", cmInvalidId }
- };
-
- cmTlH_t cmTimeLineNullHandle = cmSTATIC_NULL_HANDLE;
-
- _cmTl_t* _cmTlHandleToPtr( cmTlH_t h )
- {
- _cmTl_t* p = (_cmTl_t*)h.h;
- assert( p != NULL );
- return p;
- }
-
- _cmTlId_t* _cmTlIdLabelToRecd( _cmTl_t* p, const cmChar_t* label )
- {
- unsigned i;
- if( label != NULL )
- for(i=0; _cmTlIdArray[i].id != cmInvalidId; ++i)
- if( strcmp(_cmTlIdArray[i].label,label) == 0 )
- return _cmTlIdArray + i;
- return NULL;
- }
-
- _cmTlId_t* _cmTlIdToRecd( _cmTl_t* p, unsigned id )
- {
- unsigned i;
- for(i=0; _cmTlIdArray[i].id != cmInvalidId; ++i)
- if( _cmTlIdArray[i].id == id )
- return _cmTlIdArray + i;
- return NULL;
- }
-
- const cmChar_t* _cmTlIdToLabel( _cmTl_t* p, unsigned id )
- {
- _cmTlId_t* rp;
- if((rp = _cmTlIdToRecd(p,id)) != NULL )
- return rp->label;
- return "";
- }
-
- unsigned _cmTlIdLabelToId( _cmTl_t* p, const cmChar_t* label )
- {
- _cmTlId_t* rp;
- if((rp = _cmTlIdLabelToRecd(p,label)) != NULL )
- return rp->id;
- return cmInvalidId;
- }
-
- // cast a generic object to a midi file object
- cmTlMidiFile_t* _cmTlMidiFileObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
- {
- if( op->typeId != kMidiFileTlId )
- {
- if( errFl && p != NULL)
- cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
- return NULL;
- }
-
- return (cmTlMidiFile_t*)op;
- }
-
- // cast a generic object to a midi event object
- cmTlMidiEvt_t* _cmTlMidiEvtObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
- {
- if( op->typeId != kMidiEvtTlId )
- {
- if( errFl && p != NULL )
- cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
- return NULL;
- }
-
- return (cmTlMidiEvt_t*)op;
- }
-
- // case a generic object to an audio file object
- cmTlAudioFile_t* _cmTlAudioFileObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
- {
- if( op->typeId != kAudioFileTlId )
- {
- if( errFl && p != NULL)
- cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
- return NULL;
- }
-
- return (cmTlAudioFile_t*)op;
- }
-
- // cast a generic object an audio event object to
- cmTlAudioEvt_t* _cmTlAudioEvtObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
- {
- if( op->typeId != kAudioEvtTlId )
- {
- if( errFl && p != NULL)
- cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
- return NULL;
- }
- return (cmTlAudioEvt_t*)op;
- }
-
-
- // cast a generic object to a marker object
- cmTlMarker_t* _cmTlMarkerObjPtr( _cmTl_t* p, cmTlObj_t* op, bool errFl )
- {
- if( op->typeId != kMarkerTlId )
- {
- if( errFl && p != NULL)
- cmErrMsg(&p->err,kTypeCvtFailTlRC,"A time line object type promotion failed.");
- return NULL;
- }
- return (cmTlMarker_t*)op;
- }
-
- cmTlMidiFile_t* _cmTimeLineMidiFileObjPtr( _cmTl_t* p, cmTlObj_t* op )
- { return _cmTlMidiFileObjPtr(p,op,true); }
-
- cmTlMidiEvt_t* _cmTimeLineMidiEvtObjPtr( _cmTl_t* p, cmTlObj_t* op )
- { return _cmTlMidiEvtObjPtr(p,op,true); }
-
- cmTlAudioFile_t* _cmTimeLineAudioFileObjPtr( _cmTl_t* p, cmTlObj_t* op )
- { return _cmTlAudioFileObjPtr(p,op,true);}
-
- cmTlAudioEvt_t* _cmTimeLineAudioEvtObjPtr( _cmTl_t* p, cmTlObj_t* op )
- { return _cmTlAudioEvtObjPtr(p,op,true);}
-
- cmTlMarker_t* _cmTimeLineMarkerObjPtr( _cmTl_t* p, cmTlObj_t* op )
- { return _cmTlMarkerObjPtr(p,op,true);}
-
-
- // Locate a record which matches 'name' and (optionally) 'seqId'
- _cmTlObj_t* _cmTlFindRecd( _cmTl_t* p, unsigned seqId, const cmChar_t* name )
- {
- if( name == NULL )
- return NULL;
-
- unsigned i;
-
- for(i=0; i<p->seqCnt; ++i)
- if( seqId==cmInvalidId || seqId == i )
- {
- _cmTlObj_t* op = p->seq[i].first;
- while(op != NULL)
- {
- if( strcmp(op->obj->name,name) == 0 )
- return op;
-
- op = op->next;
- }
- }
-
- return NULL;
- }
-
- // Returns true if 'op' is a child of 'ref'.
- bool _cmTlIsChild( _cmTlObj_t* ref, _cmTlObj_t* op )
- {
- // if 'op' is not active then it can't be a child
- if( op->obj == NULL )
- return false;
-
- // if 'ref' is NULL then match obj's which do not have a parent
- if( ref == NULL )
- return op->obj->ref == NULL;
-
- // if 'ref' is the parent of 'op'.
- return op->obj->ref == ref->obj;
- }
-
- // calc the absolute start time of this object by adding the
- // time time offsets of all ancestors.
- int _cmTlStartTime( const cmTlObj_t* obj )
- {
- assert( obj != NULL );
-
- int t = 0;
-
- do
- {
- t += obj->begSmpIdx;
- obj=obj->ref;
- }while( obj != NULL);
-
-
- return t;
- }
-
-
-
- // Locate the closest record which is before 'np'.
- // When multiple records have the same distance to 'np' then the last one inserted
- // is taken as the closest. This way records with equal time values will be
- // secondarily sequenced on their order of insertion.
- _cmTlObj_t* _cmTlFindRecdBefore( _cmTl_t* p, const _cmTlObj_t* np )
- {
- assert( np->obj!=NULL && np->obj->seqId < p->seqCnt );
-
- // calc the absolute time of this object
- //int absSmpIdx = _cmTlStartTime(np->obj);
- int rsi;
- _cmTlObj_t* rp = NULL;
- _cmTlObj_t* op = p->seq[np->obj->seqId].first;
-
- //printf("type:%i %i\n",np->obj->typeId,absSmpIdx);
-
- // for each object in the list ...
- while( op != NULL )
- {
- int csi;
-
- //if( op!=np && op->obj!=NULL && (csi = _cmTlStartTime(op->obj)) <= absSmpIdx )
- if( (op!=np) && (op->obj!=NULL) && ((csi = op->obj->seqSmpIdx) <= np->obj->seqSmpIdx) )
- {
- if( rp == NULL || csi >= rsi )
- {
- rp = op;
- rsi = csi;
- }
- }
- op = op->next;
- }
-
- return rp;
- }
-
-
-
-
- // Mark 'op' and all children of 'op' for deletion.
- // Note that this function is recursive.
- cmTlRC_t _cmTlDeleteDependentRecds( _cmTl_t* p, _cmTlObj_t* op )
- {
- assert( op->obj!=NULL && op->obj->seqId < p->seqCnt );
-
- cmTlRC_t rc = kOkTlRC;
- _cmTlObj_t* dp = p->seq[op->obj->seqId].first;
-
- // mark all recd's that are children of 'op' for deletion
- while( dp != NULL )
- {
- // if 'dp' is a child of 'op'.
- if( _cmTlIsChild(op,dp) )
- if(( rc = _cmTlDeleteDependentRecds(p,dp)) != kOkTlRC )
- return rc;
-
- dp = dp->next;
- }
-
- // release any resources held by 'op'.
- switch(op->obj->typeId)
- {
- case kMidiFileTlId:
- {
- cmTlMidiFile_t* mp = _cmTimeLineMidiFileObjPtr(p,op->obj);
- cmMidiFileClose(&mp->h);
- }
- break;
-
- case kMidiEvtTlId:
- break;
-
- case kAudioFileTlId:
- {
- cmTlAudioFile_t* ap = _cmTimeLineAudioFileObjPtr(p,op->obj);
- cmAudioFileDelete(&ap->h);
- }
- break;
-
- case kAudioEvtTlId:
- break;
-
- case kMarkerTlId:
- break;
-
- default:
- return cmErrMsg(&p->err,kUnknownRecdTypeTlRC,"An unknown time line object type (%i) was encounterned during object deletion.",op->obj->typeId);
-
- }
-
- // mark 'op' as deleted by setting op->obj to NULL
- op->obj = NULL;
-
- return kOkTlRC;
- }
-
-
- // Delete 'op' and all of its dependents.
- cmTlRC_t _cmTlDeleteRecd( _cmTl_t* p, _cmTlObj_t* op )
- {
- if( op == NULL )
- return kOkTlRC;
-
- assert( op->obj!=NULL && op->obj->seqId < p->seqCnt );
- cmTlRC_t rc;
-
- _cmTlSeq_t* s = p->seq + op->obj->seqId;
-
- // mark this object and any objecs which are dependent on it for deletion
- if((rc =_cmTlDeleteDependentRecds(p,op)) != kOkTlRC )
- return rc;
-
-
- // unlink and delete and records marked for deletion
- op = s->first;
-
- while( op )
- {
- _cmTlObj_t* tp = op->next;
-
- // if this object is marked for deletion unlink it from this master list
- if( op->obj == NULL )
- {
- if( op->next != NULL )
- op->next->prev = op->prev;
- else
- {
- assert( s->last == op );
- s->last = op->prev;
-
- }
-
- if( op->prev != NULL )
- op->prev->next = op->next;
- else
- {
- assert( s->first == op );
- s->first = op->next;
- }
-
- // free the record
- cmLhFreePtr(p->lH,(void**)&op->mem);
-
- }
-
- op = tp;
- }
-
- return rc;
- }
-
- // Insert 'op' after 'bp'.
- // If 'bp' is NULL then 'op' is inserted as p->seq[op->seqId].first.
- void _cmTlInsertAfter( _cmTl_t* p, _cmTlObj_t* bp, _cmTlObj_t* op )
- {
- assert( op->obj!=NULL && op->obj->seqId < p->seqCnt );
- _cmTlSeq_t* s = p->seq + op->obj->seqId;
-
- op->prev = bp;
-
- // if op is being inserted at the beginning of the list
- if( bp == NULL )
- {
- op->next = s->first;
-
- if( s->first != NULL )
- s->first->prev = op;
-
- s->first = op;
-
- }
- else // op is being inserted in the middle or end of the list
- {
- op->next = bp->next;
-
- if( bp->next != NULL )
- bp->next->prev = op;
- else
- {
- // insertion at end
- assert( bp == s->last );
- s->last = op;
- }
-
- bp->next = op;
- }
-
- if( s->last == NULL )
- s->last = op;
-
- if( s->first == NULL )
- s->first = op;
- }
-
- // Allocate an object record
- cmTlRC_t _cmTlAllocRecd2(
- _cmTl_t* p,
- const cmChar_t* nameStr, // NULL, empty or unique name
- _cmTlObj_t* refPtr, // parent object
- int begSmpIdx, // start time
- unsigned durSmpCnt, // duration
- unsigned typeId, // object type id
- unsigned seqId, // owning seq
- unsigned recdByteCnt, // byte for type specific data to follow _cmTlObj_t data
- _cmTlObj_t** opp ) // return pointer
- {
- *opp = NULL;
-
- if( nameStr == NULL )
- nameStr = "";
-
- // get the length of the recd name field
- unsigned nameByteCnt = strlen(nameStr)+1;
-
- // verify that the name was not already used by another recd
- if( nameByteCnt>1 && _cmTlFindRecd(p,seqId,nameStr) != NULL )
- return cmErrMsg(&p->err,kDuplNameTlRC,"The object identifier '%s' was already used.",nameStr);
-
- assert( refPtr==NULL || refPtr->obj !=NULL );
-
- if( refPtr != NULL && refPtr->obj->seqId != seqId )
- 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));
-
- if( seqId >= p->seqCnt )
- {
- // assume the sequence id's arrive in increasing order
- assert( seqId == p->seqCnt );
- assert( refPtr == NULL );
- p->seqCnt = seqId+1;
- p->seq = cmMemResizePZ(_cmTlSeq_t,p->seq,p->seqCnt);
- }
-
- // calc the total size of the recd. memory layout: [name /0 _cmTlObj_t cmTlObj_t <type specific fields> ]
- unsigned byteCnt = sizeof(unsigned) + sizeof(unsigned) + nameByteCnt + sizeof(_cmTlObj_t) + recdByteCnt;
- void* mem = cmLHeapAllocZ( p->lH, byteCnt );
- unsigned* tidPtr = (unsigned*)mem;
- unsigned* parentIdPtr = tidPtr + 1;
- cmChar_t* name = (cmChar_t*)(parentIdPtr + 1);
- _cmTlObj_t* op = (_cmTlObj_t*)(name + nameByteCnt);
- cmTlObj_t* tp = (cmTlObj_t*)(op+1);
-
- // The entire object is contained in mem[]
- // Memory Layout:
- // kObjTlId parentId name[] \0 _cmTlObj_t cmTlObj_t [recdByteCnt - sizeof(cmTlObj_t)]
-
- strcpy(name,nameStr);
-
- // the first element in the mem[] buffer must be kObjTlId - this allows
- // mem[] to be used directly as the serialized version of the buffer.
- *tidPtr = kObjTlId;
- *parentIdPtr = refPtr==NULL ? cmInvalidId : refPtr->obj->uid;
- tp->reserved = op;
- tp->seqId = seqId;
- tp->name = name;
- tp->uid = p->nextUId++;
- tp->typeId = typeId;
- tp->ref = refPtr==NULL ? NULL : refPtr->obj;
- tp->begSmpIdx = refPtr==NULL ? 0 : begSmpIdx;
- tp->durSmpCnt = durSmpCnt;
- tp->seqSmpIdx = refPtr==NULL ? 0 : refPtr->obj->seqSmpIdx + begSmpIdx;
- tp->flags = 0;
- tp->text = NULL;
-
- op->obj = tp;
- op->mem = mem;
- op->memByteCnt = byteCnt;
- op->next = NULL;
- op->prev = NULL;
-
-
- //if( seqId == 4 )
- // 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) );
-
- _cmTlInsertAfter(p, _cmTlFindRecdBefore(p,op), op );
-
-
- *opp = op;
-
- return kOkTlRC;
- }
-
- 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 )
- {
- // locate the obj recd that this recd is part of (the parent recd)
- _cmTlObj_t* refPtr = _cmTlFindRecd(p,seqId,refIdStr);
-
- // if this obj has a parent but it was not found
- if( refPtr == NULL && refIdStr!=NULL && strlen(refIdStr)>0 )
- return cmErrMsg(&p->err,kRefNotFoundTlRC,"Reference identifier '%s' not found for object '%s'.",refIdStr,cmStringNullGuard(nameStr));
-
- return _cmTlAllocRecd2(p,nameStr,refPtr,begSmpIdx,durSmpCnt,typeId,seqId,recdByteCnt,opp);
- }
-
- void _cmTlNotifyListener( _cmTl_t* p, cmTlUiMsgTypeId_t msgTypeId, _cmTlObj_t* op, unsigned seqId )
- {
- if( p->cbFunc == NULL )
- return;
-
- switch( msgTypeId )
- {
- case kInitMsgTlId:
- case kFinalMsgTlId:
- case kDoneMsgTlId:
- {
- _cmTlMsg_t m;
- m.typeId = kMsgTlId;
- m.msgId = msgTypeId;
- m.srate = p->srate;
- m.seqCnt = p->seqCnt;
- m.seqId = seqId;
- p->cbFunc( p->cbArg, &m, sizeof(m) );
- }
- break;
-
- case kInsertMsgTlId:
- if( op != NULL )
- p->cbFunc( p->cbArg, op->mem, op->memByteCnt );
- break;
-
- default:
- { assert(0); }
- }
- }
-
- cmTlRC_t _cmTlAllocAudioFileRecd( _cmTl_t* p, const cmChar_t* nameStr, const cmChar_t* refIdStr, int begSmpIdx, unsigned seqId, const cmChar_t* fn )
- {
- cmAudioFileH_t afH = cmNullAudioFileH;
- cmAudioFileInfo_t info;
- cmRC_t afRC = cmOkRC;
- cmTlRC_t rc;
- _cmTlObj_t* op = NULL;
- unsigned recdByteCnt = sizeof(cmTlAudioFile_t) + strlen(fn) + 1;
-
- if( cmAudioFileIsValid( afH = cmAudioFileNewOpen(fn, &info, &afRC, p->err.rpt )) == false )
- return cmErrMsg(&p->err,kAudioFileFailTlRC,"The time line audio file '%s' could not be opened.",cmStringNullGuard(fn));
-
- if((rc = _cmTlAllocRecd(p,nameStr,refIdStr,begSmpIdx,info.frameCnt,kAudioFileTlId,seqId,recdByteCnt,&op)) != kOkTlRC )
- goto errLabel;
-
- assert(op != NULL && fn != NULL );
-
- cmTlAudioFile_t* ap = _cmTimeLineAudioFileObjPtr(p,op->obj);
- char* cp = (char*)ap;
-
- assert(ap != NULL );
-
- ap->h = afH;
- ap->info = info;
- ap->fn = cp + sizeof(cmTlAudioFile_t);
- strcpy(ap->fn,fn); // copy the file name into the extra memory
-
- assert( ap->fn + strlen(fn) + 1 == cp + recdByteCnt );
-
- op->obj->text = ap->fn;
-
- // notify listeners that an new object was created by sending a kObjTlId msg
- // notifiy any listeners that a midi file object was created
- //_cmTlNotifyListener(p, kInsertMsgTlId, op );
-
- errLabel:
- if( rc != kOkTlRC )
- cmAudioFileDelete(&afH);
-
- return rc;
- }
-
-
- cmTlRC_t _cmTlProcMidiFile( _cmTl_t* p, _cmTlObj_t* op, cmMidiFileH_t mfH )
- {
- cmTlRC_t rc = kOkTlRC;
- cmTlMidiFile_t* mfp = _cmTimeLineMidiFileObjPtr(p,op->obj);
- unsigned mn = cmMidiFileMsgCount(mfH);
- const cmMidiTrackMsg_t** mapp = cmMidiFileMsgArray(mfH);
- unsigned mi = 0;
- //double accum = 0;
- _cmTlObj_t* refOp = op;
- //bool fl = false;
- unsigned dtick = 0;
- mfp->noteOnCnt = 0;
-
- // for each midi message
- for(; mi<mn; ++mi)
- {
- _cmTlObj_t* meop = NULL;
- const cmMidiTrackMsg_t* mp = mapp[mi];
-
- dtick = mp->dtick;
-
- //if( fl )
- //{
- // dtick = 0;
- // fl = mp->dtick == 0;
- //}
-
- //accum += dtick * p->srate / 1000000;
-
- //int begSmpIdx = floor(accum_micros * p->srate / 1000000);
- int begSmpIdx = dtick; //floor( dtick * p->srate / 1000000.0 );
- int durSmpCnt = 0;
- unsigned midiTrkMsgByteCnt = cmMidiFilePackTrackMsgBufByteCount( mp );
- unsigned recdByteCnt = sizeof(cmTlMidiEvt_t) + midiTrkMsgByteCnt;
-
- //if( mfp->obj.seqId==4 && mi<=25 )
- // 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));
-
- // count the note-on messages
- if( mp->status == kNoteOnMdId )
- {
- //durSmpCnt = floor(mp->u.chMsgPtr->durTicks * p->srate / 1000000.0 );
- durSmpCnt = mp->u.chMsgPtr->durTicks;
- ++mfp->noteOnCnt;
- }
-
- // allocate the generic time-line object record
- if((rc = _cmTlAllocRecd2(p, NULL, refOp, begSmpIdx, durSmpCnt, kMidiEvtTlId, mfp->obj.seqId, recdByteCnt, &meop)) != kOkTlRC )
- goto errLabel;
-
- assert( meop != NULL );
-
- cmTlMidiEvt_t* mep = _cmTimeLineMidiEvtObjPtr(p,meop->obj);
- char* cp = (char*)mep;
-
- assert( mep != NULL );
-
- // Set the cmTlMidiEvt_t.msg cmMidiTrkMsg_t pointer to point to the
- // extra memory allocated just past the cmTlMidiEvt_t recd.
- mep->msg = (cmMidiTrackMsg_t*)(cp + sizeof(cmTlMidiEvt_t));
- mep->midiFileObjId = mfp->obj.uid;
-
- // Do not write MIDI objects that are part of a MIDI file. They will be automatically
- // loaded when the time line is loaded and therefore do not need to be save
- // explicitely in the time line data.
- meop->obj->flags = cmSetFlag(meop->obj->flags,kNoWriteTlFl);
-
- // Pack the cmMidiTrackMsg_t record into the extra memory
- cmMidiFilePackTrackMsg( mp, (char*)mep->msg, midiTrkMsgByteCnt );
-
- // verify that the memory allocation was calculated correctly
- assert( cp + recdByteCnt == ((char*)mep->msg) + midiTrkMsgByteCnt);
-
- // notify any listeners that a new midi event was created by sending a kObjTlId msg.
- //_cmTlNotifyListener(p, kInsertMsgTlId, meop );
-
- // this midi event is the ref. for the next midi evt
- refOp = meop;
- }
-
- errLabel:
- return rc;
- }
-
- cmTlRC_t _cmTlAllocMidiFileRecd( _cmTl_t* p, const cmChar_t* nameStr, const cmChar_t* refIdStr, int begSmpIdx, unsigned seqId, const cmChar_t* fn )
- {
- cmMidiFileH_t mfH = cmMidiFileNullHandle;
- cmTlRC_t rc = kOkTlRC;
- _cmTlObj_t* op = NULL;
-
- // open the midi file
- if( cmMidiFileOpen(fn, &mfH, &p->ctx ) != kOkMfRC )
- return cmErrMsg(&p->err,kMidiFileFailTlRC,"The time line midi file '%s' could not be opened.",cmStringNullGuard(fn));
-
- // force the first msg to occurr one quarter note into the file
- cmMidiFileSetDelay(mfH, cmMidiFileTicksPerQN(mfH) );
-
- unsigned durSmpCnt = floor(cmMidiFileDurSecs(mfH)*p->srate);
-
- // convert the midi file from ticks to microseconds
- cmMidiFileTickToSamples(mfH,p->srate,false);
-
- // assign note durations to all note-on msg's
- cmMidiFileCalcNoteDurations(mfH);
-
- unsigned recdByteCnt = sizeof(cmTlMidiFile_t) + strlen(fn) + 1;
-
- // allocate the midi file time line object
- if((rc = _cmTlAllocRecd(p,nameStr,refIdStr,begSmpIdx,durSmpCnt,kMidiFileTlId,seqId,recdByteCnt,&op)) != kOkTlRC )
- goto errLabel;
-
- assert( op != NULL && fn != NULL );
-
- cmTlMidiFile_t* mp = _cmTimeLineMidiFileObjPtr(p,op->obj);
- char* cp = (char*)mp;
-
- assert(mp != NULL );
-
- mp->h = mfH;
- mp->fn = cp + sizeof(cmTlMidiFile_t);
- strcpy(mp->fn,fn); // copy the filename into the extra memory
-
- assert( mp->fn + strlen(mp->fn) + 1 == cp + recdByteCnt );
-
- op->obj->text = mp->fn;
-
- // notifiy any listeners that a midi file object was created
- //_cmTlNotifyListener(p, kInsertMsgTlId, op );
-
- // insert the events in the midi file as individual time line objects
- if((rc = _cmTlProcMidiFile(p, op, mfH)) != kOkTlRC )
- goto errLabel;
-
-
- errLabel:
- if( rc != kOkTlRC )
- {
- cmMidiFileClose(&mfH);
-
- _cmTlDeleteRecd(p, op);
-
- }
-
- return rc;
- }
-
- cmTlRC_t _cmTlAllocMarkerRecd( _cmTl_t* p, const cmChar_t* nameStr, const cmChar_t* refIdStr, int begSmpIdx, unsigned durSmpCnt, unsigned seqId, const cmChar_t* text )
- {
- cmTlRC_t rc = kOkTlRC;
- _cmTlObj_t* op = NULL;
- const cmChar_t* textStr = text==NULL ? "" : text;
-
- // add memory at the end of the the cmTlMarker_t record to hold the text string.
- unsigned recdByteCnt = sizeof(cmTlMarker_t) + strlen(textStr) + 1;
-
- if((rc = _cmTlAllocRecd(p,nameStr,refIdStr,begSmpIdx,durSmpCnt,kMarkerTlId,seqId,recdByteCnt,&op)) != kOkTlRC )
- goto errLabel;
-
- assert(op != NULL);
-
- cmTlMarker_t* mp = _cmTimeLineMarkerObjPtr(p,op->obj);
-
- assert(mp != NULL );
-
- // copy the marker text string into the memory just past the cmTlMarker_t recd.
- cmChar_t* tp = (cmChar_t*)(mp + 1);
- strcpy(tp,textStr);
-
- mp->text = tp;
- op->obj->text = tp;
-
- // notify listeners
- //_cmTlNotifyListener(p, kInsertMsgTlId, op );
-
- errLabel:
- if( op != NULL && rc != kOkTlRC )
- _cmTlDeleteRecd(p,op);
-
- return rc;
- }
-
- cmTlRC_t _cmTlAllocAudioEvtRecd( _cmTl_t* p, const cmChar_t* nameStr, const cmChar_t* refIdStr, int begSmpIdx, unsigned durSmpCnt, unsigned seqId, const cmChar_t* text )
- {
- cmTlRC_t rc = kOkTlRC;
- _cmTlObj_t* op = NULL;
- const cmChar_t* textStr = text==NULL ? "" : text;
-
- unsigned recdByteCnt = sizeof(cmTlAudioEvt_t) + strlen(textStr) + 1;
-
- if((rc = _cmTlAllocRecd(p,nameStr,refIdStr,begSmpIdx,durSmpCnt,kAudioEvtTlId,seqId,recdByteCnt,&op)) != kOkTlRC )
- goto errLabel;
-
- assert(op != NULL);
-
- cmTlAudioEvt_t* mp = _cmTimeLineAudioEvtObjPtr(p,op->obj);
-
- assert(mp != NULL );
-
- // copy the marker text string into the memory just past the cmTlAudioEvt_t recd.
- cmChar_t* tp = (cmChar_t*)(mp + 1);
- strcpy(tp,textStr);
-
- mp->text = tp;
- op->obj->text = tp;
-
- // notify listeners
- //_cmTlNotifyListener(p, kInsertMsgTlId, op );
-
- errLabel:
- if( op != NULL && rc != kOkTlRC )
- _cmTlDeleteRecd(p,op);
-
- return rc;
- }
-
- 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)
- {
- cmTlRC_t rc = kOkTlRC;
- unsigned typeId = _cmTlIdLabelToId(p,typeIdStr);
-
- switch( typeId )
- {
- case kAudioFileTlId: rc = _cmTlAllocAudioFileRecd(p,nameStr,refIdStr,begSmpIdx, seqId,textStr); break;
- case kMidiFileTlId: rc = _cmTlAllocMidiFileRecd( p,nameStr,refIdStr,begSmpIdx, seqId,textStr); break;
- case kMarkerTlId: rc = _cmTlAllocMarkerRecd( p,nameStr,refIdStr,begSmpIdx,durSmpCnt,seqId,textStr); break;
- case kAudioEvtTlId: rc = _cmTlAllocAudioEvtRecd( p,nameStr,refIdStr,begSmpIdx,durSmpCnt,seqId,textStr); break;
- default:
- rc = cmErrMsg(&p->err,kParseFailTlRC,"'%s' is not a valid 'objArray' record type.",cmStringNullGuard(typeIdStr));
- }
-
- return rc;
- }
-
- cmTlRC_t _cmTimeLineFinalize( _cmTl_t* p )
- {
- cmTlRC_t rc = kOkTlRC;
- unsigned i;
-
- for(i=0; i<p->seqCnt; ++i)
- while( p->seq[i].first != NULL )
- {
- if((rc = _cmTlDeleteRecd(p,p->seq[i].first)) != kOkTlRC )
- goto errLabel;
- }
-
- cmMemFree(p->seq);
-
- cmLHeapDestroy(&p->lH);
-
- //_cmTlNotifyListener(p, kFinalMsgTlId, NULL );
-
- cmMemPtrFree(&p->tmpBuf);
-
- cmMemPtrFree(&p);
-
-
- return kOkTlRC;
-
- errLabel:
- return cmErrMsg(&p->err,kFinalizeFailTlRC,"Finalize failed.");
- }
-
- cmTlRC_t cmTimeLineInitialize( cmCtx_t* ctx, cmTlH_t* hp, cmTlCb_t cbFunc, void* cbArg )
- {
- cmTlRC_t rc;
-
- if((rc = cmTimeLineFinalize(hp)) != kOkTlRC )
- return rc;
-
- _cmTl_t* p = cmMemAllocZ( _cmTl_t, 1 );
-
- cmErrSetup(&p->err,&ctx->rpt,"Time Line");
- p->ctx = *ctx;
- p->cbFunc = cbFunc;
- p->cbArg = cbArg;
-
- if(cmLHeapIsValid( p->lH = cmLHeapCreate( 8192, ctx )) == false )
- {
- rc = cmErrMsg(&p->err,kLHeapFailTlRC,"The linked heap allocation failed.");
- goto errLabel;
- }
-
- hp->h = p;
-
- return rc;
-
- errLabel:
- _cmTimeLineFinalize(p);
- return rc;
- }
-
- cmTlRC_t cmTimeLineInitializeFromFile( cmCtx_t* ctx, cmTlH_t* hp, cmTlCb_t cbFunc, void* cbArg, const cmChar_t* fn )
- {
- cmTlRC_t rc;
- if((rc = cmTimeLineInitialize(ctx,hp,cbFunc,cbArg)) != kOkTlRC )
- return rc;
-
- return cmTimeLineReadJson(*hp,fn);
- }
-
- const cmChar_t* cmTimeLineFileName( cmTlH_t h )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return p->filename;
- }
-
- cmTlRC_t cmTimeLineFinalize( cmTlH_t* hp )
- {
- cmTlRC_t rc;
- if( hp == NULL || cmTimeLineIsValid(*hp) == false )
- return kOkTlRC;
-
- _cmTl_t* p = _cmTlHandleToPtr(*hp);
-
-
- if((rc = _cmTimeLineFinalize(p)) != kOkTlRC )
- return rc;
-
- hp->h = NULL;
-
- return rc;
- }
-
- bool cmTimeLineIsValid( cmTlH_t h )
- { return h.h != NULL; }
-
- double cmTimeLineSampleRate( cmTlH_t h )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return p->srate;
- }
-
- int cmTimeLineSeqToLocalSampleIndex( int seqSmpIdx, cmTlObj_t* localObjPtr )
- {
- return seqSmpIdx - localObjPtr->seqSmpIdx;
- }
-
- cmTlObj_t* _cmTimeLineIdToObj( _cmTl_t* p, unsigned seqId, unsigned id )
- {
- assert( seqId < p->seqCnt );
-
- _cmTlObj_t* op = p->seq[seqId].first;
- for(; op != NULL; op=op->next )
- if( op->obj->uid == id )
- return op->obj;
- return NULL;
- }
-
- cmTlObj_t* cmTimeLineIdToObj( cmTlH_t h, unsigned seqId, unsigned id )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- cmTlObj_t* op = NULL;
-
- if( seqId != cmInvalidId )
- op = _cmTimeLineIdToObj(p,seqId,id);
- else
- {
- for(seqId=0; seqId<p->seqCnt; ++seqId)
- if((op = _cmTimeLineIdToObj(p,seqId,id) ) != NULL )
- break;
- }
-
- return op;
- }
-
-
- cmTlObj_t* cmTimeLineNextObj( cmTlH_t h, cmTlObj_t* tp, unsigned seqId )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- _cmTlObj_t* op;
-
- assert( seqId < p->seqCnt );
- if( seqId >= p->seqCnt )
- return NULL;
-
- // if tp is NULL then start at the begin of the obj list ...
- if( tp == NULL )
- op = p->seq[seqId].first;
- else
- {
- // ... otherwise advance to the obj after tp
- op = (_cmTlObj_t*)tp->reserved;
- assert( op != NULL );
- op = op->next;
- }
-
- // if the list is empty
- if( op == NULL )
- return NULL;
-
- // return the next object which matches seqId or
- // the next object if seqId == cmInvalidId
- for(; op != NULL; op = op->next)
- if( (seqId == cmInvalidId) || (op->obj->seqId == seqId) )
- return op->obj;
-
- return NULL;
- }
-
- cmTlObj_t* cmTimeLineNextTypeObj( cmTlH_t h, cmTlObj_t* p, unsigned seqId, unsigned typeId )
- {
- cmTlObj_t* tp = p;
- while( (tp = cmTimeLineNextObj(h,tp,seqId)) != NULL )
- if( typeId == cmInvalidId || tp->typeId == typeId )
- return tp;
-
- return NULL;
- }
-
- cmTlMidiFile_t* cmTlNextMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
- {
- if((op = cmTimeLineNextTypeObj(h, op, seqId, kMidiFileTlId )) == NULL )
- return NULL;
- return cmTimeLineMidiFileObjPtr(h,op);
- }
-
- cmTlAudioFile_t* cmTlNextAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
- {
- if((op = cmTimeLineNextTypeObj(h, op, seqId, kAudioFileTlId )) == NULL )
- return NULL;
- return cmTimeLineAudioFileObjPtr(h,op);
- }
-
- cmTlMidiEvt_t* cmTlNextMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
- {
- if((op = cmTimeLineNextTypeObj(h, op, seqId, kMidiEvtTlId )) == NULL )
- return NULL;
- return cmTimeLineMidiEvtObjPtr(h,op);
- }
-
- cmTlAudioEvt_t* cmTlNextAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
- {
- if((op = cmTimeLineNextTypeObj(h, op, seqId, kAudioEvtTlId )) == NULL )
- return NULL;
- return cmTimeLineAudioEvtObjPtr(h,op);
- }
-
- cmTlMarker_t* cmTlNextMarkerObjPtr( cmTlH_t h, cmTlObj_t* op, unsigned seqId )
- {
- if((op = cmTimeLineNextTypeObj(h, op, seqId, kMarkerTlId )) == NULL )
- return NULL;
- return cmTimeLineMarkerObjPtr(h,op);
- }
-
- cmTlObj_t* cmTlIdToObjPtr( cmTlH_t h, unsigned uid )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- unsigned i;
- for(i=0; i<p->seqCnt; ++i)
- //if( p->seq[i].first->obj->uid <= uid && uid <= p->seq[i].last->obj->uid )
- {
- _cmTlObj_t* op = p->seq[i].first;
- for(; op != NULL; op=op->next )
- if( op->obj->uid == uid )
- return op->obj;
- }
-
- return NULL;
- }
-
-
- cmTlMidiFile_t* cmTimeLineMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTimeLineMidiFileObjPtr(p,op);
- }
- cmTlAudioFile_t* cmTimeLineAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTimeLineAudioFileObjPtr(p,op);
- }
- cmTlMidiEvt_t* cmTimeLineMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTimeLineMidiEvtObjPtr(p,op);
- }
- cmTlAudioEvt_t* cmTimeLineAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTimeLineAudioEvtObjPtr(p,op);
- }
- cmTlMarker_t* cmTimeLineMarkerObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTimeLineMarkerObjPtr(p,op);
- }
-
- cmTlMidiFile_t* cmTlMidiFileObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTlMidiFileObjPtr(p,op,false);
- }
- cmTlAudioFile_t* cmTlAudioFileObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTlAudioFileObjPtr(p,op,false);
- }
- cmTlMidiEvt_t* cmTlMidiEvtObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTlMidiEvtObjPtr(p,op,false);
- }
- cmTlAudioEvt_t* cmTlAudioEvtObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTlAudioEvtObjPtr(p,op,false);
- }
- cmTlMarker_t* cmTlMarkerObjPtr( cmTlH_t h, cmTlObj_t* op )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return _cmTlMarkerObjPtr(p,op,false);
- }
-
- cmTlRC_t cmTimeLineInsert( cmTlH_t h, const cmChar_t* nameStr, unsigned typeId,
- const cmChar_t* fn, int begSmpIdx, unsigned durSmpCnt, const cmChar_t* refObjNameStr, unsigned seqId )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
-
- return _cmTlAllocRecdFromJson(p, nameStr, _cmTlIdToLabel(p,typeId), refObjNameStr, begSmpIdx, durSmpCnt, seqId, fn);
- }
-
- cmTlObj_t* _cmTimeLineFindFile( _cmTl_t* p, const cmChar_t* fn, unsigned typeId )
- {
- unsigned i;
- for(i=0; i<p->seqCnt; ++i)
- {
- _cmTlObj_t* op = p->seq[i].first;
- for(; op != NULL; op=op->next )
- if( op->obj->typeId == typeId )
- {
- const cmChar_t* objFn = NULL;
-
- switch( typeId )
- {
- case kAudioFileTlId:
- objFn = ((cmTlAudioFile_t*)(op->obj))->fn;
- break;
-
- case kMidiFileTlId:
- objFn = ((cmTlMidiFile_t*)(op->obj))->fn;
- break;
-
- default:
- { assert(0); }
- }
-
- if( strcmp(objFn,fn) == 0 )
- return op->obj;
-
- }
- }
-
- return NULL;
- }
-
- cmTlAudioFile_t* cmTimeLineFindAudioFile( cmTlH_t h, const cmChar_t* fn )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- cmTlObj_t* op;
- if((op = _cmTimeLineFindFile(p,fn,kAudioFileTlId)) != NULL )
- return _cmTlAudioFileObjPtr(p,op,true);
- return NULL;
- }
-
- cmTlMidiFile_t* cmTimeLineFindMidiFile( cmTlH_t h, const cmChar_t* fn )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- cmTlObj_t* op;
- if((op = _cmTimeLineFindFile(p,fn,kMidiFileTlId)) != NULL )
- return _cmTlMidiFileObjPtr(p,op,true);
- return NULL;
- }
-
- _cmTlObj_t* _cmTimeLineObjAtTime( _cmTl_t* p, unsigned seqId, unsigned seqSmpIdx, unsigned typeId )
- {
- assert( seqId < p->seqCnt );
- _cmTlObj_t* op = p->seq[seqId].first;
- _cmTlObj_t* min_op = NULL;
- unsigned minDist = UINT_MAX;
-
- for(; op!=NULL; op=op->next)
- if( typeId==cmInvalidId || op->obj->typeId == typeId )
- {
- // if seqSmpIdx is inside this object - then return it as the solution
- if((op->obj->seqSmpIdx <= seqSmpIdx && seqSmpIdx < (op->obj->seqSmpIdx + op->obj->durSmpCnt)))
- return op;
-
- // measure the distance from seqSmpIdx to the begin and end of this object
- unsigned d0 = op->obj->seqSmpIdx < seqSmpIdx ? seqSmpIdx - op->obj->seqSmpIdx : op->obj->seqSmpIdx - seqSmpIdx;
- unsigned d1 = op->obj->seqSmpIdx+op->obj->durSmpCnt < seqSmpIdx ? seqSmpIdx - op->obj->seqSmpIdx+op->obj->durSmpCnt : op->obj->seqSmpIdx+op->obj->durSmpCnt - seqSmpIdx;
-
- // d0 and d1 should decrease as the cur object approaches seqSmpIdx
- // If they do not then the search is over - return the closest point.
- if( d0>minDist && d1>minDist)
- break;
-
- // track the min dist and the assoc'd obj
- if( d0 < minDist )
- {
- minDist = d0;
- min_op = op;
- }
-
- if( d1 < minDist )
- {
- minDist = d1;
- min_op = op;
- }
-
- }
-
- return min_op;
- }
-
- cmTlAudioFile_t* cmTimeLineAudioFileAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- _cmTlObj_t* op;
- if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kAudioFileTlId)) == NULL )
- return NULL;
-
- return _cmTlAudioFileObjPtr(p,op->obj,false);
- }
-
- cmTlMidiFile_t* cmTimeLineMidiFileAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- _cmTlObj_t* op;
- if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kMidiFileTlId)) == NULL )
- return NULL;
-
- return _cmTlMidiFileObjPtr(p,op->obj,false);
- }
-
- cmTlMidiEvt_t* cmTimeLineMidiEvtAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- _cmTlObj_t* op;
- if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kMidiEvtTlId)) == NULL )
- return NULL;
-
- return _cmTlMidiEvtObjPtr(p,op->obj,false);
- }
-
- cmTlMarker_t* cmTimeLineMarkerAtTime( cmTlH_t h, unsigned seqId, unsigned seqSmpIdx )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- _cmTlObj_t* op;
- if((op = _cmTimeLineObjAtTime(p,seqId,seqSmpIdx,kMarkerTlId)) == NULL )
- return NULL;
-
- return _cmTlMarkerObjPtr(p,op->obj,false);
- }
-
-
- cmTlRC_t _cmTlParseErr( cmErr_t* err, const cmChar_t* errLabelPtr, unsigned idx, const cmChar_t* fn )
- {
- cmTlRC_t rc;
-
- if( errLabelPtr != NULL )
- 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));
- else
- rc = cmErrMsg(err,kParseFailTlRC,"The time_line configuration parse failed on the record at index %i in '%s'.",idx,cmStringNullGuard(fn));
-
- return rc;
- }
-
- cmTlRC_t cmTimeLineReadJson( cmTlH_t h, const cmChar_t* ifn )
- {
- cmTlRC_t rc = kOkTlRC;
- cmJsonH_t jsH = cmJsonNullHandle;
- cmJsonNode_t* jnp;
- const cmChar_t* errLabelPtr;
- int i;
-
- _cmTl_t* p = _cmTlHandleToPtr(h);
-
- // open the json file
- if( cmJsonInitializeFromFile(&jsH, ifn, &p->ctx ) != kOkJsRC )
- return cmErrMsg(&p->err,kJsonFailTlRC,"JSON file initialization failed.");
-
- if((jnp = cmJsonFindValue(jsH,"time_line",cmJsonRoot(jsH),kObjectTId)) == NULL)
- {
- rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' object was not found in '%s'.",cmStringNullGuard(ifn));
- goto errLabel;
- }
-
- if( cmJsonMemberValues(jnp,&errLabelPtr,
- "srate",kRealTId,&p->srate,
- "objArray",kArrayTId,&jnp,
- NULL) != kOkJsRC )
- {
- rc = cmErrMsg(&p->err,kParseFailTlRC,"The JSON 'time_line' object header parse failed in '%s'.",cmStringNullGuard(ifn));
- goto errLabel;
- }
-
- for(i=0; i<cmJsonChildCount(jnp); ++i)
- {
- const cmJsonNode_t* rp = cmJsonArrayElementC(jnp,i);
- const cmChar_t* nameStr;
- const cmChar_t* typeIdStr;
- const cmChar_t* refIdStr;
- int begSmpIdx;
- unsigned durSmpCnt;
- unsigned seqId;
- const cmChar_t* textStr;
-
- if( cmJsonMemberValues(rp,&errLabelPtr,
- "label",kStringTId,&nameStr,
- "type", kStringTId,&typeIdStr,
- "ref", kStringTId,&refIdStr,
- "offset",kIntTId,&begSmpIdx,
- "smpCnt",kIntTId,&durSmpCnt,
- "trackId",kIntTId,&seqId,
- "textStr",kStringTId,&textStr,
- NULL) != kOkJsRC )
- {
- rc = _cmTlParseErr(&p->err, errLabelPtr, i, ifn );
- goto errLabel;
- }
-
-
- if((rc = _cmTlAllocRecdFromJson(p,nameStr,typeIdStr,refIdStr,begSmpIdx,durSmpCnt,seqId,textStr)) != kOkTlRC )
- goto errLabel;
-
- }
-
- p->filename = cmLhAllocStr(p->lH,ifn);
-
- errLabel:
- if( rc != kOkTlRC )
- _cmTimeLineFinalize(p);
-
- cmJsonFinalize(&jsH);
- return rc;
- }
-
- unsigned cmTimeLineSeqCount( cmTlH_t h )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- return p->seqCnt;
- }
-
- cmTlRC_t cmTimeLineSeqNotify( cmTlH_t h, unsigned seqId )
- {
- cmTlRC_t rc = kOkTlRC;
- _cmTl_t* p = _cmTlHandleToPtr(h);
-
- assert( seqId < p->seqCnt );
-
- _cmTlNotifyListener(p,kInitMsgTlId,NULL,seqId);
-
- _cmTlObj_t* op = p->seq[seqId].first;
- for(; op!=NULL; op=op->next)
- if( op->obj->seqId == seqId )
- _cmTlNotifyListener(p, kInsertMsgTlId, op, cmInvalidId );
-
- _cmTlNotifyListener(p,kDoneMsgTlId,NULL,seqId);
-
- return rc;
- }
-
- void _cmTimeLinePrintObj(_cmTl_t* p, _cmTlObj_t* op, cmRpt_t* rpt )
- {
-
- 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);
- switch(op->obj->typeId )
- {
- case kMidiFileTlId:
- {
- cmTlMidiFile_t* rp = _cmTimeLineMidiFileObjPtr(p,op->obj);
- cmRptPrintf(rpt,"%s ", cmMidiFileName(rp->h));
- }
- break;
-
- case kMidiEvtTlId:
- {
- cmTlMidiEvt_t* rp = _cmTimeLineMidiEvtObjPtr(p,op->obj);
- if( op->obj->ref != NULL )
- cmRptPrintf(rpt,"%s ",op->obj->ref->name);
- cmRptPrintf(rpt,"%s ",cmMidiStatusToLabel(rp->msg->status));
- }
- break;
-
- case kAudioFileTlId:
- {
- cmTlAudioFile_t* rp = _cmTimeLineAudioFileObjPtr(p,op->obj);
- cmRptPrintf(rpt,"%s ", cmAudioFileName(rp->h));
- }
- break;
-
- case kAudioEvtTlId:
- {
- cmTlAudioEvt_t* rp = _cmTimeLineAudioEvtObjPtr(p,op->obj);
- cmRptPrintf(rpt,"%s",rp->text);
- }
- break;
-
- case kMarkerTlId:
- {
- cmTlMarker_t* rp = _cmTimeLineMarkerObjPtr(p,op->obj);
- cmRptPrintf(rpt,"%s ", rp->text );
- }
- break;
- }
- cmRptPrint(rpt,"\n");
- }
-
- cmTlRC_t _cmTimeLineWriteRecd( _cmTl_t* p, cmJsonH_t jsH, cmJsonNode_t* np, cmTlObj_t* obj )
- {
- cmTlRC_t rc = kOkTlRC;
-
- // if this recd is writable and has not been previously written
- if( cmIsNotFlag(obj->flags,kNoWriteTlFl) && cmIsNotFlag(obj->flags,kReservedTlFl) )
- {
- const cmChar_t* refStr = NULL;
-
- if( obj->ref != NULL )
- {
- refStr = obj->ref->name;
-
- // if the ref obj was not previously written then write it (recursively) first
- if( cmIsNotFlag(obj->ref->flags, kReservedTlFl) )
- if((rc = _cmTimeLineWriteRecd(p,jsH,np,obj->ref)) != kOkTlRC )
- return rc;
- }
-
- // create the time-line recd
- if( cmJsonInsertPairs( jsH, cmJsonCreateObject(jsH,np),
- "label", kStringTId, obj->name,
- "type", kStringTId, _cmTlIdToLabel(p, obj->typeId ),
- "ref", kStringTId, refStr==NULL ? "" : refStr,
- "offset", kIntTId, obj->begSmpIdx,
- "smpCnt", kIntTId, obj->durSmpCnt,
- "trackId", kIntTId, obj->seqId,
- "textStr", kStringTId, obj->text==NULL ? "" : obj->text,
- NULL ) != kOkJsRC )
- {
- 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));
- }
-
- obj->flags = cmSetFlag(obj->flags, kReservedTlFl);
- }
-
- return rc;
- }
-
- cmTlRC_t cmTimeLineWrite( cmTlH_t h, const cmChar_t* fn )
- {
- cmTlRC_t rc = kOkTlRC;
- _cmTl_t* p = _cmTlHandleToPtr(h);
- cmJsonH_t jsH = cmJsonNullHandle;
- cmJsonNode_t* np;
-
- // initialize a JSON tree
- if( cmJsonInitialize(&jsH,&p->ctx) != kOkJsRC )
- return cmErrMsg(&p->err,kJsonFailTlRC,"JSON object initialization failed.");
-
- // create the root object
- if((np = cmJsonCreateObject(jsH,NULL)) == NULL )
- {
- rc = cmErrMsg(&p->err,kJsonFailTlRC,"Time line root object create failed while creating the time line file '%s.",cmStringNullGuard(fn));
- goto errLabel;
- }
-
- // create the time-line object
- if((np = cmJsonInsertPairObject(jsH, np, "time_line" )) == NULL )
- {
- rc = cmErrMsg(&p->err,kJsonFailTlRC,"'time_line' object created failed while creating the time line file '%s.",cmStringNullGuard(fn));
- goto errLabel;
- }
-
- // write the sample rate
- if( cmJsonInsertPairReal(jsH, np, "srate", p->srate ) != kOkJsRC )
- {
- rc = cmErrMsg(&p->err,kJsonFailTlRC,"'sample rate' output failed while creating the time line file '%s.",cmStringNullGuard(fn));
- goto errLabel;
- }
-
- // create the time-line object array
- if((np = cmJsonInsertPairArray(jsH, np, "objArray")) == NULL )
- {
- rc = cmErrMsg(&p->err,kJsonFailTlRC,"'objArray' output failed while creating the time line file '%s.",cmStringNullGuard(fn));
- goto errLabel;
- }
-
-
- unsigned i;
- // write each time-line object
- for(i=0; i<p->seqCnt; ++i)
- {
- _cmTlObj_t* op = p->seq[i].first;
- for(; op != NULL; op=op->next )
- if((rc = _cmTimeLineWriteRecd(p,jsH,np,op->obj)) != kOkTlRC )
- goto errLabel;
- }
-
- if( cmJsonErrorCode(jsH) == kOkJsRC )
- if( cmJsonWrite(jsH,NULL,fn) != kOkJsRC )
- {
- rc = cmErrMsg(&p->err,kJsonFailTlRC,"JSON write failed.");
- goto errLabel;
- }
-
-
- errLabel:
-
- if( cmJsonFinalize(&jsH) != kOkJsRC )
- {
- rc = cmErrMsg(&p->err,kJsonFailTlRC,"JSON finalize failed.");
- goto errLabel;
- }
-
- return rc;
- }
-
- cmTlRC_t cmTimeLinePrint( cmTlH_t h, cmRpt_t* rpt )
- {
- _cmTl_t* p = _cmTlHandleToPtr(h);
- unsigned i;
- for(i=0; i<p->seqCnt; ++i)
- {
- _cmTlObj_t* op = p->seq[i].first;
- while(op != NULL)
- {
- _cmTimeLinePrintObj(p,op,rpt);
- op = op->next;
- }
- }
- return kOkTlRC;
- }
-
- cmTlRC_t cmTimeLinePrintFn( cmCtx_t* ctx, const cmChar_t* fn, cmRpt_t* rpt )
- {
- cmTlRC_t rc;
- cmTlH_t h = cmTimeLineNullHandle;
-
- if((rc = cmTimeLineInitializeFromFile(ctx,&h,NULL,NULL,fn)) != kOkTlRC )
- return rc;
-
- cmTimeLinePrint(h,rpt);
-
- return cmTimeLineFinalize(&h);
- }
-
-
- cmTlRC_t cmTimeLineTest( cmCtx_t* ctx, const cmChar_t* jsFn )
- {
- cmTlRC_t rc = kOkTlRC;
- cmTlH_t tlH = cmTimeLineNullHandle;
-
- if((rc = cmTimeLineInitialize(ctx,&tlH,NULL,NULL)) != kOkTlRC )
- return rc;
-
- if((rc = cmTimeLineReadJson(tlH,jsFn)) != kOkTlRC )
- goto errLabel;
-
- if((rc = cmTimeLineInsert(tlH,"Mark",kMarkerTlId,"My Marker",10,0,NULL,0)) != kOkTlRC )
- goto errLabel;
-
- cmTimeLinePrint(tlH, &ctx->rpt );
-
- errLabel:
- cmTimeLineFinalize(&tlH);
-
- return rc;
- }
-
- cmTlRC_t _cmTimeLineDecodeObj( const void* msg, unsigned msgByteCnt, cmTlUiMsg_t* r )
- {
- cmTlRC_t rc = kOkTlRC;
- unsigned* typeIdPtr = (unsigned*)msg;
- unsigned* parentIdPtr = typeIdPtr + 1;
- const char* text = (const char*)(parentIdPtr + 1);
- _cmTlObj_t* op = (_cmTlObj_t*)(text + strlen(text) + 1);
- cmTlObj_t* tp = (cmTlObj_t*)(op + 1 );
-
- r->msgId = kInsertMsgTlId;
- r->objId = tp->uid;
- //r->parentObjId = *parentIdPtr;
- r->seqId = tp->seqId;
- //r->typeId = tp->typeId;
- //r->begSmpIdx = tp->begSmpIdx;
- //r->durSmpCnt = tp->durSmpCnt;
- //r->label = strlen(text)==0 ? NULL : text;
- r->srate = 0;
- //r->midiTrkMsg = NULL;
- //r->textStr = NULL;
- /*
- switch( tp->typeId )
- {
- case kMidiFileTlId:
- r->textStr = _cmTimeLineMidiFileObjPtr(NULL,tp)->fn;
- break;
-
- case kMidiEvtTlId:
- {
- cmTlMidiEvt_t* mep;
- if((mep = _cmTimeLineMidiEvtObjPtr(NULL,tp)) != NULL )
- {
- r->midiFileObjId = mep->midiFileObjId;
- r->midiTrkMsg = mep->msg;
- }
- }
- break;
-
- case kAudioFileTlId:
- r->textStr = _cmTimeLineAudioFileObjPtr(NULL,tp)->fn;
- break;
-
- case kAudioEvtTlId:
- break;
-
- case kMarkerTlId:
- r->textStr = _cmTimeLineMarkerObjPtr(NULL,tp)->text;
- break;
-
- default:
- { assert(0); }
- }
- */
- return rc;
- }
-
- cmTlRC_t cmTimeLineDecode( const void* msg, unsigned msgByteCnt, cmTlUiMsg_t* r )
- {
- cmTlRC_t rc = kOkTlRC;
- _cmTlMsg_t* m = (_cmTlMsg_t*)msg;
-
- switch( m->typeId )
- {
- case kMsgTlId:
- r->msgId = m->msgId;
- r->srate = m->srate;
- r->seqCnt= m->seqCnt;
- r->seqId = m->seqId;
- break;
-
- case kObjTlId:
- r->msgId = kInsertMsgTlId;
- rc = _cmTimeLineDecodeObj(msg,msgByteCnt,r);
- break;
-
- default:
- assert(0);
- }
-
- return rc;
-
- }
|