123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554 |
- //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
- //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
- #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 "cmLex.h"
- #include "cmCsv.h"
- #include "cmSdb.h"
- #include "cmText.h"
- #include "cmMath.h"
- #include "cmTime.h"
- #include "cmMidi.h"
- #include "cmVectOpsTemplateMain.h"
- #include "cmAudioFile.h"
- #include "cmFileSys.h"
-
- typedef enum
- {
- kUuidColIdx,
- kBaseUuidColIdx,
- kChIdxColIdx,
- kObiColIdx,
- kIbiColIdx,
- kIeiColIdx,
- kOeiColIdx,
- kSrcColIdx,
- kMidiColIdx,
- kInstrColIdx,
- kSrateColIdx,
- kChCntColIdx,
- kNotesColIdx,
- kAfnColIdx,
- kInvalidColIdx
- } cmSdbColIdx_t;
-
- struct cmSdb_str;
-
- typedef struct cmSdbSeqBlk_str
- {
- cmSdbSeqEvent_t* eV;
- unsigned cnt;
- struct cmSdbSeqBlk_str* link;
- } cmSdbSeqBlk_t;
-
- typedef struct cmSdbSeq_str
- {
- struct cmSdb_str* p;
- cmSdbSeqBlk_t* blocks;
- cmSdbSeqBlk_t* ebp;
- unsigned cnt; // total count of events in all blocks
- unsigned chCnt; // max(chIdx)+1 of all events
- double minDurSec; // min dur of all events
- double maxDurSec; // max dur of all events
- struct cmSdbSeq_str* link;
- } cmSdbSeq_t;
-
- typedef struct cmSdbRspBlk_str
- {
- unsigned* indexV; // indexV[ cmSdb_t.blkIdxAllocCnt ]
- unsigned cnt; // count of indexes used
- struct cmSdbRspBlk_str* link; // cmSdbRsp_t.blocks link
- } cmSdbRspBlk_t;
-
- typedef struct cmSdbRsp_str
- {
- struct cmSdb_str* p; //
- cmSdbRspBlk_t* blocks; // first block ptr
- cmSdbRspBlk_t* ebp; // end block ptr
- unsigned cnt; // total count of indexes
- struct cmSdbRsp_str* link; // cmSdb_t.responses link
- } cmSdbRsp_t;
-
- typedef struct cmSdb_str
- {
- cmCtx_t ctx;
- cmLHeapH_t lhH;
- cmCsvH_t csvH;
- cmSdbEvent_t* eV;
- unsigned eN;
- cmChar_t* audioDir;
- unsigned blkIdxAllocCnt;
- unsigned blkEvtAllocCnt;
- cmSdbRsp_t* responses;
- cmSdbSeq_t* seqs;
- } cmSdb_t;
-
- cmSdbH_t cmSdbNullHandle = cmSTATIC_NULL_HANDLE;
- cmSdbResponseH_t cmSdbResponseNullHandle = cmSTATIC_NULL_HANDLE;
- cmSdbSeqH_t cmSdbSeqNullHandle = cmSTATIC_NULL_HANDLE;
-
- cmSdb_t* _cmSdbHandleToPtr( cmSdbH_t h )
- {
- cmSdb_t* p = (cmSdb_t*)h.h;
- assert( p != NULL );
- return p;
- }
-
- void _cmSdbRspFree( cmSdbRsp_t* );
- cmSdbRC_t _cmSdbSeqFree( cmSdbSeq_t* );
-
- cmSdbRC_t _cmSdbDestroy( cmSdb_t* p )
- {
- cmSdbRC_t rc = kOkSdbRC;
-
- if( cmCsvFinalize(&p->csvH) != kOkCsvRC )
- rc = cmErrMsg(&p->ctx.err,kCsvFailSdbRC,"CSV file finalize failed.");
-
- while( p->responses != NULL )
- _cmSdbRspFree(p->responses);
-
- while( p->seqs != NULL )
- _cmSdbSeqFree(p->seqs);
-
- cmLHeapDestroy(&p->lhH);
- cmMemFree(p);
- return rc;
- }
-
- cmSdbRC_t cmSdbCreate( cmCtx_t* ctx, cmSdbH_t* hp, const cmChar_t* csvFn, const cmChar_t* audioDir )
- {
- cmSdbRC_t rc;
- if((rc = cmSdbDestroy(hp)) != kOkSdbRC )
- return rc;
-
- cmSdb_t* p = cmMemAllocZ(cmSdb_t,1);
- p->ctx = *ctx;
- p->blkIdxAllocCnt = 1024;
- p->blkEvtAllocCnt = 1024;
-
- cmErrSetup(&p->ctx.err,&ctx->rpt,"sdb");
-
- if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8192,ctx)) == false )
- {
- rc = cmErrMsg(&p->ctx.err,kLHeapFailSdbRC,"Linked heap mgr. allocation failed.");
- goto errLabel;
- }
-
- hp->h = p;
-
- if( csvFn != NULL )
- if((rc = cmSdbLoad(*hp,csvFn,audioDir)) != kOkSdbRC )
- goto errLabel;
-
- errLabel:
- if( rc != kOkSdbRC )
- _cmSdbDestroy(p);
-
- return rc;
- }
-
- cmSdbRC_t cmSdbDestroy( cmSdbH_t* hp )
- {
- cmSdbRC_t rc = kOkSdbRC;
-
- if( hp==NULL || cmSdbIsValid(*hp)==false )
- return rc;
-
- cmSdb_t* p = _cmSdbHandleToPtr(*hp);
-
- if((rc = _cmSdbDestroy(p)) != kOkSdbRC )
- return rc;
-
- hp->h = NULL;
-
- return rc;
- }
-
- bool cmSdbIsValid( cmSdbH_t h )
- { return h.h != NULL; }
-
-
- cmSdbRC_t _cmSdbSyntaxError(cmSdb_t* p, const cmChar_t* csvFn, unsigned rowIdx, unsigned colIdx, const cmChar_t* colLabel )
- {
- return cmErrMsg(&p->ctx.err,kSyntaxErrSdbRC,"A syntax error was found at row %i col %i (label:%s) in '%s'.",rowIdx+1,colIdx+1,cmStringNullGuard(colLabel),cmStringNullGuard(csvFn));
- }
-
-
- cmSdbRC_t cmSdbLoad( cmSdbH_t h, const cmChar_t* csvFn, const cmChar_t* audioDir )
- {
- cmSdbRC_t rc = kOkSdbRC;
- unsigned i;
-
- cmSdb_t* p = _cmSdbHandleToPtr(h);
-
- if( cmCsvInitializeFromFile(&p->csvH, csvFn, 0, &p->ctx ) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->ctx.err,kCsvFailSdbRC,"CSV file load fail on '%s'.",cmStringNullGuard(csvFn));
- goto errLabel;
- }
-
- p->eN = cmCsvRowCount(p->csvH)-1;
-
- // release all the memory held by the linked heap
- cmLHeapClear(p->lhH,true);
-
- p->eV = cmLhAllocZ(p->lhH,cmSdbEvent_t,p->eN);
-
-
- for(i=0; rc==kOkSdbRC && i<p->eN; ++i)
- {
- unsigned rowIdx = i+1;
-
- if((p->eV[i].uuid = cmCsvCellUInt(p->csvH,rowIdx,kUuidColIdx)) == UINT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kUuidColIdx,"uuid");
-
- if((p->eV[i].baseUuid = cmCsvCellUInt(p->csvH,rowIdx,kBaseUuidColIdx)) == UINT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kBaseUuidColIdx,"baseUuid");
-
- if((p->eV[i].chIdx = cmCsvCellUInt(p->csvH,rowIdx,kChIdxColIdx)) == UINT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kChIdxColIdx,"chIdx");
- else
- p->eV[i].chIdx -= 1; // CSV channel index is 1 based
-
- if((p->eV[i].obi = cmCsvCellUInt(p->csvH,rowIdx,kObiColIdx)) == UINT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kObiColIdx,"obi");
- else
- p->eV[i].obi -= 1;
-
- if((p->eV[i].ibi = cmCsvCellUInt(p->csvH,rowIdx,kIbiColIdx)) == UINT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kIbiColIdx,"ibi");
- else
- p->eV[i].ibi -= 1;
-
- if((p->eV[i].iei = cmCsvCellUInt(p->csvH,rowIdx,kIeiColIdx)) == UINT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kIeiColIdx,"obi");
- else
- p->eV[i].iei -= 1;
-
- if((p->eV[i].oei = cmCsvCellUInt(p->csvH,rowIdx,kOeiColIdx)) == UINT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kOeiColIdx,"ibi");
- else
- p->eV[i].oei -= 1;
-
- if((p->eV[i].src = cmCsvCellText(p->csvH,rowIdx,kSrcColIdx)) == NULL )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kSrcColIdx,"src");
-
- if((p->eV[i].midi = cmCsvCellInt(p->csvH,rowIdx,kMidiColIdx)) == INT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kMidiColIdx,"midi");
-
- if((p->eV[i].instr = cmCsvCellText(p->csvH,rowIdx,kInstrColIdx)) == NULL )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kInstrColIdx,"instr");
-
- if((p->eV[i].srate = cmCsvCellUInt(p->csvH,rowIdx,kSrateColIdx)) == UINT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kSrateColIdx,"srate");
-
- if((p->eV[i].chCnt = cmCsvCellUInt(p->csvH,rowIdx,kChCntColIdx)) == UINT_MAX )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kChCntColIdx,"chCnt");
-
-
- cmCsvCell_t* c;
- if((c = cmCsvCellPtr(p->csvH,rowIdx,kNotesColIdx)) == NULL )
- {
- rc = cmErrMsg(&p->ctx.err,kSyntaxErrSdbRC,"Syntax Error: No 'notes' or 'audio file name' field for row %i in '%s'.",rowIdx+1,cmStringNullGuard(csvFn));
- goto errLabel;
- }
-
- // count the number of 'notes'
- unsigned nn = 0;
- for(; c->rowPtr != NULL; c=c->rowPtr)
- ++nn;
-
- if( nn > 0 )
- {
- unsigned k = 0;
-
- // allocate the 'notes' ptr array - the last entry is set to NULL.
- p->eV[i].notesV = cmLhAllocZ(p->lhH,const cmChar_t*,nn+1);
-
- // read each note
- for(c=cmCsvCellPtr(p->csvH,rowIdx,kNotesColIdx); c!=NULL&&c->rowPtr!=NULL; c=c->rowPtr,++k)
- if(( p->eV[i].notesV[k] = cmCsvCellText(p->csvH,rowIdx,kNotesColIdx+k)) == NULL )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kNotesColIdx+k,"notes");
-
- assert(k==nn);
- }
-
- // read the audio file name
- if((p->eV[i].afn = cmCsvCellText(p->csvH,rowIdx,kNotesColIdx+nn)) == NULL )
- rc = _cmSdbSyntaxError(p,csvFn,rowIdx,kNotesColIdx+nn,"afn");
-
- }
-
- // store the audio directory
- if( cmTextLength(audioDir) )
- p->audioDir = cmLhAllocStr(p->lhH,audioDir);
- else
- {
- cmLhFree(p->lhH,&p->audioDir);
- p->audioDir = NULL;
- }
- errLabel:
-
- return rc;
-
- }
-
-
- cmSdbRC_t cmSdbSyncChPairs( cmSdbH_t h )
- {
- cmSdbRC_t rc = kOkSdbRC;
- cmSdb_t* p = _cmSdbHandleToPtr(h);
- unsigned i;
- // for each multi-channel event
- for(i=0; i<p->eN; ++i)
- if(p->eV[i].chCnt > 1 )
- {
- const cmSdbEvent_t* ep = p->eV + i;
- unsigned iV[ep->chCnt];
- unsigned j,k;
-
- // load iV[] with the event indexes of the channel pairs
- for(j=0,k=0; j<p->eN && k<ep->chCnt; ++j)
- if( p->eV[j].baseUuid == ep->baseUuid )
- {
- assert( p->eV[j].chIdx < ep->chCnt );
-
- iV[p->eV[j].chIdx] = j;
- ++k;
- }
-
- if( k != ep->chCnt )
- rc = cmErrMsg(&p->ctx.err,kChPairNotFoundSdbRC,"The channel pair associated with 'id:%i instr:%s src:%s ch index:%i could not be found.",ep->uuid,cmStringNullGuard(ep->instr),cmStringNullGuard(ep->src),ep->chIdx);
- else
- {
- unsigned mobi = ep->obi;
- unsigned mibi = ep->ibi;
- unsigned miei = ep->iei;
- unsigned moei = ep->oei;
-
- // get the min onsets and max offsets
- for(j=0; j<ep->chCnt; ++j)
- {
- mobi = cmMin(mobi,p->eV[ iV[j] ].obi);
- mibi = cmMin(mibi,p->eV[ iV[j] ].ibi);
- miei = cmMax(miei,p->eV[ iV[j] ].iei);
- moei = cmMax(moei,p->eV[ iV[j] ].oei);
- }
-
- // set the onsets to the min onset / offsets to max offsets
- for(j=0; j<ep->chCnt; ++j)
- {
- p->eV[ iV[j] ].obi = mobi;
- p->eV[ iV[j] ].ibi = mibi;
- p->eV[ iV[j] ].iei = miei;
- p->eV[ iV[j] ].oei = moei;
- }
- }
- }
-
- return rc;
- }
-
- const cmSdbEvent_t* _cmSdbEvent( cmSdb_t* p, unsigned uuid )
- {
- unsigned i;
- for(i=0; i<p->eN; ++i)
- if( p->eV[i].uuid == uuid )
- return p->eV + i;
-
- return NULL;
- }
-
- const cmSdbEvent_t* cmSdbEvent( cmSdbH_t h, unsigned uuid )
- {
- cmSdb_t* p = _cmSdbHandleToPtr(h);
- return _cmSdbEvent(p,uuid);
- }
-
- //================================================================================================================================
- cmSdbRsp_t* _cmSdbRspHandleToPtr( cmSdbResponseH_t h )
- {
- cmSdbRsp_t* p = (cmSdbRsp_t*)h.h;
- assert( p != NULL );
- return p;
- }
-
- void _cmSdbRspBlkFree( cmSdb_t* p, cmSdbRspBlk_t* bp )
- {
- cmLhFree(p->lhH, bp->indexV);
- cmLhFree(p->lhH, bp);
- }
-
-
- cmSdbRspBlk_t* _cmSdbRspBlkUnlink( cmSdbRsp_t* rp, cmSdbRspBlk_t* bp )
- {
- cmSdbRspBlk_t* dp = rp->blocks;
- cmSdbRspBlk_t* pp = NULL;
- for(; dp!=NULL; dp=dp->link)
- {
- if( dp == bp )
- {
- if( pp == NULL )
- rp->blocks = dp->link;
- else
- pp->link = dp->link;
-
- return bp;
- }
-
- pp = dp;
- }
-
- assert(0);
- return NULL;
- }
-
- void _cmSdbRspInsertIndex( cmSdb_t* p, cmSdbRsp_t* rp, unsigned evtIndex )
- {
-
- if( rp->ebp == NULL || rp->ebp->cnt == p->blkIdxAllocCnt )
- {
- cmSdbRspBlk_t* bp = cmLhAllocZ(p->lhH,cmSdbRspBlk_t,1);
- bp->indexV = cmLhAllocZ(p->lhH,unsigned,p->blkIdxAllocCnt);
-
- if( rp->ebp != NULL )
- rp->ebp->link = bp;
-
- if( rp->blocks == NULL )
- rp->blocks = bp;
-
- rp->ebp = bp;
-
- }
-
- assert( rp->ebp!=NULL && rp->ebp->cnt < p->blkIdxAllocCnt );
-
- rp->ebp->indexV[ rp->ebp->cnt++ ] = evtIndex;
- rp->cnt += 1;
- }
-
-
- cmSdbRsp_t* _cmSdbRspUnlink( cmSdbRsp_t* rp )
- {
- cmSdb_t* p = rp->p;
- cmSdbRsp_t* dp = p->responses;
- cmSdbRsp_t* pp = NULL;
-
- for(; dp!=NULL; dp=dp->link)
- {
- if( dp == rp )
- {
- if( pp == NULL )
- p->responses = dp->link;
- else
- pp->link = dp->link;
-
- return rp;
- }
-
- pp = dp;
- }
-
- assert( 0 );
-
- return NULL;
- }
-
-
- void _cmSdbRspFree( cmSdbRsp_t* rp )
- {
- _cmSdbRspUnlink(rp);
-
- while( rp->blocks != NULL )
- {
- cmSdbRspBlk_t* np = rp->blocks->link;
- cmSdbRspBlk_t* bp;
-
- if((bp = _cmSdbRspBlkUnlink(rp,rp->blocks)) != NULL )
- _cmSdbRspBlkFree(rp->p,bp);
-
- rp->blocks = np;
- }
-
- cmLhFree(rp->p->lhH,rp);
-
- }
-
- cmSdbRsp_t* _cmSdbRspAlloc( cmSdb_t* p )
- {
- cmSdbRsp_t* rp = cmLhAllocZ(p->lhH,cmSdbRsp_t,1);
- rp->p = p;
- rp->link = p->responses;
- p->responses = rp;
-
-
- return rp;
- }
-
-
- // Compare 'label' to every string in tV[i] and return true if any comparision is a match.
- // If 'subFlV[i]' is set then 'label' must only contain tV[i] as a substring to match.
- // If 'negFlV[i]' is set then return true if any comparision is a mismatch.
- bool _cmSdbSelectText( const cmSdbEvent_t* r, const cmChar_t** tV, const bool* subFlV, const bool* negFlV, const cmChar_t* label )
- {
- unsigned i;
-
- if( label == NULL )
- return false;
-
- if( tV == NULL )
- return true;
-
- for(i=0; tV[i]!=NULL; ++i)
- {
- bool matchFl = false;
- if( subFlV[i] )
- matchFl = strstr(label,tV[i]) != NULL;
- else
- matchFl = strcmp(tV[i],label)==0;
-
- if( negFlV[i] )
- matchFl = !matchFl;
-
- if(matchFl)
- return true;
- }
-
- return false;
- }
-
- unsigned _cmSdbStrVectCnt( const cmChar_t** v )
- {
- unsigned n = 0;
- unsigned i = 0;
-
- if( v == NULL )
- return 0;
-
- for(i=0; v[i]!=NULL; ++i)
- ++n;
- return n;
- }
-
- void _cmSdbStrVectFlags( const cmChar_t** v, bool* sV, bool* nV )
- {
- unsigned i = 0;
- if( v == NULL )
- return;
-
- for(i=0; v[i]!=NULL; ++i)
- {
- nV[i] = false;
- sV[i] = false;
-
- if( strncmp(v[i],"*!",2)==0 || strncmp(v[i],"!*",2)==0)
- {
- sV[i] = nV[i] = true;
- v[i] += 2;
- }
- else
- {
- if( strncmp(v[i],"!",1)==0 )
- {
- nV[i] = true;
- v[i] += 1;
- }
-
- if( strncmp(v[i],"*",1)==0 )
- {
- sV[i] = true;
- v[i] += 1;
- }
- }
-
-
- }
- }
-
- cmSdbRC_t cmSdbSelect(
- cmSdbH_t h,
- double srate,
- const cmChar_t** instrV,
- const cmChar_t** srcV,
- const cmChar_t** notesV,
- const unsigned* pitchV,
- double minDurSec,
- double maxDurSec,
- unsigned minChCnt,
- cmSdbResponseH_t* rhp )
- {
- cmSdbRC_t rc;
-
- if((rc = cmSdbResponseFree(rhp)) != kOkSdbRC )
- return rc;
-
- cmSdb_t* p = _cmSdbHandleToPtr(h);
- cmSdbRsp_t* rp = _cmSdbRspAlloc(p);
- unsigned i;
-
- // get the length of each string vector
- unsigned srcN = _cmSdbStrVectCnt(srcV);
- unsigned insN = _cmSdbStrVectCnt(instrV);
- unsigned notN = _cmSdbStrVectCnt(notesV);
-
- // allocate flag vectors
- bool srcSubFlV[ srcN ];
- bool srcNegFlV[ srcN ];
- bool insSubFlV[ insN ];
- bool insNegFlV[ insN ];
- bool notSubFlV[ notN ];
- bool notNegFlV[ notN ];
-
- // fill the flag vectors
- _cmSdbStrVectFlags(srcV, srcSubFlV,srcNegFlV);
- _cmSdbStrVectFlags(instrV,insSubFlV,insNegFlV);
- _cmSdbStrVectFlags(notesV,notSubFlV,notNegFlV);
-
- for(i=0; i<p->eN; ++i)
- {
- const cmSdbEvent_t* r = p->eV + i;
- double durSec = (double)r->srate * (r->oei - r->obi);
- unsigned j;
-
- if( srate!=0 && srate!=r->srate )
- continue;
-
- if( durSec < minDurSec || (maxDurSec!=0 && maxDurSec < durSec) )
- continue;
-
- if( minChCnt!=0 && r->chCnt > minChCnt )
- continue;
-
- if( !_cmSdbSelectText(r,srcV,srcSubFlV,srcNegFlV,r->src) )
- continue;
-
- if( !_cmSdbSelectText(r,instrV,insSubFlV,insNegFlV,r->instr) )
- continue;
-
- if( pitchV != NULL )
- {
- for(j=0; pitchV[j]!=kInvalidMidiPitch; ++j)
- if( pitchV[j] == r->midi )
- break;
-
- if( pitchV[j] != r->midi )
- continue;
- }
-
- if( r->notesV != NULL )
- {
- for(j=0; r->notesV[j]!=NULL; ++j)
- if( _cmSdbSelectText(r,notesV,notSubFlV,notNegFlV,r->notesV[j]) == true )
- break;
-
- if( r->notesV[j]==NULL )
- continue;
- }
-
-
- _cmSdbRspInsertIndex(p,rp,i);
- }
-
- rhp->h = rp;
-
- if(rc != kOkSdbRC )
- _cmSdbRspFree(rp);
-
- return rc;
- }
-
- cmSdbRC_t _cmSdbSelectChPairs( cmSdb_t* p, const cmSdbEvent_t* ep, cmSdbResponseH_t* rhp )
- {
- cmSdbRC_t rc;
- if((rc = cmSdbResponseFree(rhp)) != kOkSdbRC )
- return rc;
-
- cmSdbRsp_t* rp = _cmSdbRspAlloc(p);
- unsigned i;
-
- // for each channel of this event
- for(i=0; i<ep->chCnt; ++i)
- {
- // if i channel is not the known events channel
- if( ep->chIdx != i )
- {
- unsigned j;
-
- // examine each record
- for(j=0; j<p->eN; ++j)
- // if eV[j] shares a baseUuid but is on a different channel than *ep ...
- if( p->eV[j].baseUuid == ep->baseUuid && p->eV[j].chIdx==i )
- {
- // .. then a match has been found
- _cmSdbRspInsertIndex(p,rp,j);
- break;
- }
-
- if( j== p->eN )
- {
- rc = cmErrMsg(&p->ctx.err,kChPairNotFoundSdbRC,"The channel pair associated with 'id:%i instr:%s src:%s ch index:%i could not be found.",ep->uuid,cmStringNullGuard(ep->instr),cmStringNullGuard(ep->src),ep->chIdx);
- }
- }
- }
-
- rhp->h = rp;
-
- return rc;
- }
-
- cmSdbRC_t cmSdbSelectChPairs( cmSdbH_t h, const cmSdbEvent_t* ep, cmSdbResponseH_t* rhp )
- {
- cmSdb_t* p = _cmSdbHandleToPtr(h);
- return _cmSdbSelectChPairs( p, ep,rhp );
- }
-
- unsigned cmSdbResponseCount( cmSdbResponseH_t rh )
- {
- cmSdbRsp_t* rp = _cmSdbRspHandleToPtr(rh);
- return rp->cnt;
- }
-
- const cmSdbEvent_t* cmSdbResponseEvent( cmSdbResponseH_t rh, unsigned index )
- {
- cmSdbRsp_t* rp = _cmSdbRspHandleToPtr(rh);
-
- if( index >= rp->cnt )
- return NULL;
-
- cmSdbRspBlk_t* bp = rp->blocks;
- unsigned i;
- for(i=0; bp!=NULL; i+=bp->cnt,bp=bp->link)
- if( i <= index && index < (i + bp->cnt) )
- return rp->p->eV + bp->indexV[index-i];
-
- cmErrMsg(&rp->p->ctx.err,kInvalidRspIdxSdbRC,"Invalid query response index=%i.",index);
- return NULL;
- }
-
- bool cmSdbResponseIsValid( cmSdbResponseH_t rh )
- { return rh.h != NULL; }
-
- cmSdbRC_t cmSdbResponseFree( cmSdbResponseH_t* rhp )
- {
- cmSdbRC_t rc = kOkSdbRC;
-
- if( rhp == NULL || cmSdbResponseIsValid(*rhp)==false )
- return rc;
-
- cmSdbRsp_t* rp = _cmSdbRspHandleToPtr(*rhp);
-
- _cmSdbRspFree(rp);
-
- rhp->h = NULL;
-
- return rc;
- }
-
- void cmSdbResponsePrint( cmSdbResponseH_t rh, cmRpt_t* rpt )
- {
- unsigned n = cmSdbResponseCount(rh);
- unsigned i;
- for(i=0; i<n; ++i)
- {
- const cmSdbEvent_t* e = cmSdbResponseEvent(rh,i);
- if( e != NULL )
- cmRptPrintf(rpt,"%6i %6i %2i %12i %12i %12i %12i %2i %6i %2i %10s %15s\n",
- e->uuid,e->baseUuid,e->chIdx,e->obi,e->ibi,e->iei,e->oei,e->midi,e->srate,e->chCnt,
- cmStringNullGuard(e->src), cmStringNullGuard(e->instr) );
- }
- }
-
- //================================================================================================================================
- cmSdbSeq_t* _cmSdbSeqHandleToPtr( cmSdbSeqH_t sh )
- {
- cmSdbSeq_t* sp = (cmSdbSeq_t*)sh.h;
- assert(sp !=NULL );
- return sp;
- }
-
- void _cmSdbSeqInsertEvent( cmSdbSeq_t* sp, unsigned uuid, unsigned chIdx, double begSecs, double durSecs )
- {
- cmSdb_t* p = sp->p;
-
- // if no block has been allocated or the current block is full
- if( sp->ebp == NULL || sp->ebp->cnt >= p->blkEvtAllocCnt )
- {
- // allocate a new seq block recd
- cmSdbSeqBlk_t* bp = cmLhAllocZ(sp->p->lhH,cmSdbSeqBlk_t,1);
-
- // allocate a seq evt array
- bp->eV = cmLhAllocZ(sp->p->lhH,cmSdbSeqEvent_t,p->blkEvtAllocCnt);
-
- // link in the block recd
- if( sp->ebp != NULL )
- sp->ebp->link = bp;
-
- if( sp->blocks == NULL )
- {
- sp->blocks = bp;
- sp->minDurSec = durSecs;
- sp->maxDurSec = durSecs;
- }
-
- sp->ebp = bp;
- }
-
- assert( sp->ebp != NULL && sp->ebp->cnt < p->blkEvtAllocCnt );
-
- // get the next seq evt recd to fill
- cmSdbSeqEvent_t* ep = sp->ebp->eV + sp->ebp->cnt;
-
- // fill the seq evt recd
- ep->uuid = uuid;
- ep->begSec = begSecs;
- ep->durSec = durSecs;
- ep->outChIdx = chIdx;
- ep->gain = 1.0;
-
- // incr the seq evt cnt
- sp->ebp->cnt += 1;
- sp->cnt += 1;
- sp->chCnt = cmMax(sp->chCnt,chIdx+1);
- sp->minDurSec = cmMin(sp->minDurSec,durSecs);
- sp->maxDurSec = cmMax(sp->maxDurSec,durSecs);
- }
-
-
- // unlink a sequence record from p->seqs.
- cmSdbSeq_t* _cmSdbSeqUnlink( cmSdbSeq_t* sp )
- {
- cmSdb_t* p = sp->p;
- cmSdbSeq_t* cp = p->seqs;
- cmSdbSeq_t* pp = NULL;
-
- for(; cp!=NULL; cp=cp->link)
- {
- if( cp == sp )
- {
- if( pp == NULL )
- p->seqs = sp->link;
- else
- pp->link = sp->link;
-
- return sp;
- }
-
- pp = cp;
- }
-
- assert(0);
- return NULL;
- }
-
- // free a sequence record
- cmSdbRC_t _cmSdbSeqFree( cmSdbSeq_t* sp )
- {
- cmSdb_t* p = sp->p;
-
- // unlink this seq. record from p->seqs
- if( _cmSdbSeqUnlink(sp) == NULL )
- return cmErrMsg(&p->ctx.err,kAssertFailSdbRC,"Sequence unlink failed.");
-
- // release the seq blocks held by the sequence
- while( sp->blocks != NULL )
- {
- cmSdbSeqBlk_t* np = sp->blocks->link;
-
- cmLhFree(p->lhH,sp->blocks->eV);
- cmLhFree(p->lhH,sp->blocks);
-
- sp->blocks = np;
- }
-
-
- cmLhFree(p->lhH,sp);
-
- return kOkSdbRC;
- }
-
- // allocate a sequence record
- cmSdbSeq_t* _cmSdbSeqAlloc( cmSdb_t* p )
- {
- cmSdbSeq_t* sp = cmLhAllocZ(p->lhH,cmSdbSeq_t,1);
-
- sp->p = p;
- sp->link = p->seqs;
- p->seqs = sp;
-
- return sp;
- }
-
- cmSdbRC_t _cmSdbStoreSeqEvent(
- cmSdb_t* p,
- cmSdbSeq_t* sp,
- cmSdbResponseH_t rh,
- unsigned ri,
- unsigned seqChCnt,
- double begSecs,
- double limitEvtDurSecs,
- double* durSecsRef )
- {
- cmSdbRC_t rc = kOkSdbRC;
- double maxEvtDurSecs = 0;
-
- // retrieve the event record
- const cmSdbEvent_t* ep;
- if((ep = cmSdbResponseEvent(rh,ri)) == NULL )
- {
- rc = cmErrMsg(&p->ctx.err,kRspEvtNotFoundSdbRC,"A response event could not be found during random sequence generation.");
- goto errLabel;
- }
-
- cmSdbResponseH_t rh0 = cmSdbResponseNullHandle;
- unsigned rn0 = 0;
- unsigned ci = 0;
-
- // locate the channel pairs for 'ep'.
- if( seqChCnt>1 && ep->chCnt>1 )
- {
- if( _cmSdbSelectChPairs(p, ep, &rh0 ) != kOkSdbRC )
- {
- rc = cmErrMsg(&p->ctx.err,kChPairNotFoundSdbRC,"A response event could not find channel pairs during random sequence generation.");
- goto errLabel;
- }
-
- rn0 = cmSdbResponseCount(rh0);
- }
-
- while(1)
- {
-
- // calculate the event duration
- double durSecs = (double)(ep->oei - ep->obi)/ep->srate;
-
- // truncate the event if it is longer than limitEvtDurSecs
- if( limitEvtDurSecs!=0 && durSecs>limitEvtDurSecs )
- durSecs = cmMin(limitEvtDurSecs,durSecs);
-
- // track the longest event
- maxEvtDurSecs = cmMax(maxEvtDurSecs,durSecs);
-
- // store the sequence event
- _cmSdbSeqInsertEvent(sp,ep->uuid,ci,begSecs,durSecs);
-
- // incr the output ch index
- ++ci;
-
- // if all the out ch's are filled or the sample event has no more channels
- if( ci >= seqChCnt || ci-1 >= rn0 )
- break;
-
- // get the next channel pair
- if((ep = cmSdbResponseEvent(rh0,ci-1)) == NULL )
- {
- rc = cmErrMsg(&p->ctx.err,kRspEvtNotFoundSdbRC,"A channel pair response event could not be found during random sequence generation.");
- goto errLabel;
- }
-
- } // for each sample event pair
-
- errLabel:
-
- if( durSecsRef != NULL )
- *durSecsRef = maxEvtDurSecs;
-
- cmSdbResponseFree(&rh0);
-
- return rc;
-
- }
-
- cmSdbRC_t cmSdbSeqRand(
- cmSdbResponseH_t rh,
- unsigned seqDurSecs,
- unsigned seqChCnt,
- unsigned minEvtPerSec,
- unsigned maxEvtPerSec,
- cmSdbSeqH_t* shp )
- {
- cmSdbRC_t rc;
-
- if((rc = cmSdbSeqFree(shp)) != kOkSdbRC )
- return rc;
-
- cmSdbRsp_t* rp = _cmSdbRspHandleToPtr(rh);
- cmSdb_t* p = rp->p;
- cmSdbSeq_t* sp = _cmSdbSeqAlloc(p);
-
- if( seqChCnt < 1 )
- return cmErrMsg(&p->ctx.err,kInvalidArgSdbRC,"The random sequence generator channel count parameter must be non-zero.");
-
- if( seqDurSecs <= 0 )
- return cmErrMsg(&p->ctx.err,kInvalidArgSdbRC,"The random sequence generator signal duration must be greater than 0.");
-
- if( maxEvtPerSec < minEvtPerSec )
- return cmErrMsg(&p->ctx.err,kInvalidArgSdbRC,"The random sequence generator max. events per second must be greater or equal to the min. events per second.");
-
- if((rc = cmSdbSeqFree(shp)) != kOkSdbRC )
- return rc;
-
- unsigned rn = cmSdbResponseCount(rh);
- unsigned sec;
- for(sec=0; sec<seqDurSecs; sec+=1 )
- {
-
- // calcuate the number of events to initiate during this second
- unsigned en = cmRandUInt(minEvtPerSec,maxEvtPerSec);
- unsigned ei;
- for(ei=0; ei<en; ++ei)
- {
- // select an event index
- unsigned ri = cmRandUInt(0,rn-1);
-
- // double select a start time for this event
- double begSecs = sec + cmRandDouble(0.0,1.0);
- double maxEvtDurSecs = 0;
-
- if((rc = _cmSdbStoreSeqEvent(p,sp,rh,ri,seqChCnt,begSecs,maxEvtDurSecs,NULL)) != kOkSdbRC )
- goto errLabel;
-
- } // for each event init'd during this second
-
-
- } // for each second
-
- shp->h = sp;
-
- errLabel:
- if( rc != kOkSdbRC )
- _cmSdbSeqFree(sp);
-
- return rc;
- }
-
- cmSdbRC_t cmSdbSeqSerial(
- cmSdbResponseH_t rh,
- unsigned seqChCnt,
- double gapSec,
- double maxEvtDurSec,
- cmSdbSeqH_t* shp )
- {
- cmSdbRC_t rc;
-
- if((rc = cmSdbSeqFree(shp)) != kOkSdbRC )
- return rc;
-
- cmSdbRsp_t* rp = _cmSdbRspHandleToPtr(rh);
- cmSdb_t* p = rp->p;
- cmSdbSeq_t* sp = _cmSdbSeqAlloc(p);
- unsigned n = cmSdbResponseCount(rh);
- double begSecs = 0;
- unsigned ri;
-
- for(ri=0; ri<n; ++ri)
- {
- double durSecs = 0;
-
- if((rc = _cmSdbStoreSeqEvent(p,sp,rh,ri,seqChCnt,begSecs,maxEvtDurSec,&durSecs)) != kOkSdbRC )
- goto errLabel;
-
- // offset to next event
- begSecs += durSecs + gapSec;
-
- }
-
- shp->h = sp;
-
- errLabel:
- if(rc != kOkSdbRC )
- _cmSdbSeqFree(sp);
-
- return rc;
- }
-
- cmSdbRC_t cmSdbSeqChord(
- cmSdbResponseH_t* rhp,
- unsigned rn,
- unsigned seqChCnt,
- unsigned maxEvtDurSec,
- cmSdbSeqH_t* shp )
- {
- cmSdbRC_t rc = kOkSdbRC;
-
- assert( shp != NULL );
-
- if( rn == 0 )
- return rc;
-
- cmSdbResponseH_t rh = rhp[0];
- cmSdbRsp_t* rp = _cmSdbRspHandleToPtr(rh);
- cmSdb_t* p = rp->p;
- cmSdbSeq_t* sp = _cmSdbSeqAlloc(p);
- unsigned i;
-
- if((rc = cmSdbSeqFree(shp)) != kOkSdbRC )
- return rc;
-
- // for each chord note
- for(i=0; i<rn; ++i)
- {
- // get the query response handle for this note
- rh = rhp[i];
- rp = _cmSdbRspHandleToPtr(rh);
-
- // verify that all query responses were drawn from the same cmSdbH_t handle.
- if( rp->p != p )
- {
- rc = cmErrMsg(&p->ctx.err,kAssertFailSdbRC,"All chord query response handle must be derived from the same cmSdbH_t handle.");
- goto errLabel;
- }
-
- // pick one event at random from the response
- unsigned n = cmSdbResponseCount(rh);
- unsigned rei = cmRandUInt(0,n-1);
-
- // all notes start at time: 0.0.
- double begSecs = 0.0;
-
- // store the sequence event
- if((rc = _cmSdbStoreSeqEvent(p,sp,rh,rei,seqChCnt,begSecs,maxEvtDurSec,NULL)) != kOkSdbRC )
- goto errLabel;
-
- }
-
- shp->h = sp;
-
- errLabel:
- if(rc != kOkSdbRC )
- _cmSdbSeqFree(sp);
-
- return rc;
- }
-
-
-
- bool cmSdbSeqIsValid( cmSdbSeqH_t sh )
- { return sh.h != NULL; }
-
- cmSdbRC_t cmSdbSeqFree( cmSdbSeqH_t* shp )
- {
- cmSdbRC_t rc = kOkSdbRC;
- if( shp==NULL || cmSdbSeqIsValid(*shp)==false )
- return rc;
-
- cmSdbSeq_t* sp = _cmSdbSeqHandleToPtr(*shp);
-
- if((rc = _cmSdbSeqFree(sp)) != kOkSdbRC )
- return rc;
-
- shp->h = NULL;
-
- return rc;
- }
-
- unsigned cmSdbSeqCount( cmSdbSeqH_t sh )
- {
- if( cmSdbSeqIsValid(sh)==false )
- return 0;
-
- cmSdbSeq_t* sp = _cmSdbSeqHandleToPtr(sh);
- return sp->cnt;
- }
-
- const cmSdbSeqEvent_t* cmSdbSeqEvent( cmSdbSeqH_t sh, unsigned index )
- {
- cmSdbSeq_t* sp = _cmSdbSeqHandleToPtr(sh);
- if( index >= sp->cnt )
- return NULL;
-
- cmSdbSeqBlk_t* bp = sp->blocks;
- unsigned i;
- for(i=0; bp!=NULL; i+=bp->cnt,bp=bp->link)
- if( i <= index && index < (i + bp->cnt) )
- return bp->eV + index-i;
-
- cmErrMsg(&sp->p->ctx.err,kInvalidSeqIdxSdbRC,"Invalid sequence event index=%i.",index);
-
- return NULL;
- }
-
- const cmSdbEvent_t* cmSdbSeqSdbEvent( cmSdbSeqH_t sh, unsigned index )
- {
- const cmSdbSeqEvent_t* ep;
- if((ep = cmSdbSeqEvent(sh,index)) == NULL )
- return NULL;
-
- cmSdbSeq_t* sp = _cmSdbSeqHandleToPtr(sh);
-
- return _cmSdbEvent(sp->p,ep->uuid);
- }
-
- double cmSdbSeqDurSeconds( cmSdbSeqH_t sh )
- {
- cmSdbSeq_t* sp = _cmSdbSeqHandleToPtr(sh);
-
- cmSdbSeqBlk_t* bp = sp->blocks;
- while( bp!=NULL && bp->link!=NULL )
- bp=bp->link;
-
- if( bp == NULL )
- return 0;
-
- cmSdbSeqEvent_t* ep = bp->eV + bp->cnt - 1;
-
- return ep->begSec + ep->durSec;
- }
-
- double cmSdbSeqSampleRate( cmSdbSeqH_t sh )
- {
- unsigned n = cmSdbSeqCount(sh);
- unsigned i;
- const cmSdbEvent_t* ep;
-
- for(i=0; i<n; ++i)
- if((ep = cmSdbSeqSdbEvent(sh,i)) != NULL && ep->srate != 0 )
- return ep->srate;
-
- return 0;
- }
-
- cmSdbRC_t cmSdbSeqToAudio(
- cmSdbSeqH_t sh,
- unsigned decayMs,
- double noiseDb,
- double normFact,
- cmSample_t** signalRef,
- unsigned* sigSmpCntRef )
- {
- assert( signalRef!=NULL && sigSmpCntRef!=NULL);
-
- *signalRef = NULL;
- *sigSmpCntRef = 0;
-
- cmSdbRC_t rc = kOkSdbRC;
- cmSdbSeq_t* sp = _cmSdbSeqHandleToPtr(sh);
- cmSdb_t* p = sp->p;
- unsigned qN = cmSdbSeqCount(sh);
- double durSecs = cmSdbSeqDurSeconds(sh);
- double srate = cmSdbSeqSampleRate(sh);
-
- assert(sp->maxDurSec>=sp->minDurSec);
-
- // verify that sequence events exist
- if( qN==0 || durSecs==0 || sp->chCnt==0 || sp->maxDurSec==0)
- return rc;
-
- // validate the sample rate
- if( srate == 0 )
- return cmErrMsg(&p->ctx.err,kAssertFailSdbRC,"The sample rate of the sequence could not be determined.");
-
- unsigned sN = (unsigned)floor(srate * (durSecs + 0.25)); // output signal sample count + 1/4 second of silence
- unsigned dN = (unsigned)floor(srate * decayMs / 1000.0); // decay env. sample count
- unsigned tN = (unsigned)floor(srate * sp->maxDurSec); // length of longest audio event in samples
- cmSample_t* s = cmMemAllocZ(cmSample_t,sN*sp->chCnt); // allocate the outputsignal buffer
- cmSample_t* t = cmMemAllocZ(cmSample_t,tN*sp->chCnt); // audio event read buffer
- cmSample_t* d = NULL;
- cmSample_t* chBuf[ sp->chCnt ];
- unsigned i;
-
- // fill the channel buffers
- for(i=0; i<sp->chCnt; ++i)
- chBuf[i] = t + (i*tN);
-
- // if a decay rate was specified
- if( dN > 0 )
- {
- d = cmMemAllocZ(cmSample_t,dN); // allocate the decay env. buffer
- cmVOS_LinSpace(d,dN,1.0,0.0); // calc. a decay envelope
- cmVOS_PowVS(d,dN,4.0);
- }
-
- // if a noise floor was specified
- if( noiseDb != 0 )
- {
- // fill the signal with low level white noise
- cmVOS_Random(s,sN,-1.0,1.0);
- cmVOS_MultVS(s,sN,pow(10.0,-fabs(noiseDb)/20.0));
- }
-
- // for each sequence event
- for(i=0; rc==kOkSdbRC && i<qN; ++i)
- {
- const cmSdbSeqEvent_t* qep;
- const cmSdbEvent_t* ep;
-
- // get the sequence event record
- if((qep = cmSdbSeqEvent(sh,i)) == NULL )
- {
- rc = cmErrMsg(&p->ctx.err,kAssertFailSdbRC,"Unable to retrieve the sequence event at index %i.",i);
- goto errLabel;
- }
-
- // get the audio event record
- if((ep = _cmSdbEvent(p,qep->uuid)) == NULL)
- {
- rc = cmErrMsg(&p->ctx.err,kAssertFailSdbRC,"Unable to retrieve the sample event with uuid:%i.",qep->uuid);
- goto errLabel;
- }
-
- unsigned begFrmIdx = floor(srate * qep->begSec ); // dest. index into output signal
- unsigned frmCnt = floor(srate * qep->durSec ); // seq. event dur in samples
- const cmChar_t* afn = NULL; // audio event file name
- unsigned actFrmCnt = 0; // actual count of samples read from the audio event file
- cmAudioFileInfo_t afInfo; // audio file info. record
-
- // form the audio event file name
- if((afn = cmFsMakeFn(p->audioDir,ep->afn,NULL,NULL))==NULL)
- {
- rc = cmErrMsg(&p->ctx.err,kFileSysFailSdbRC,"Unable to form the file name for %s/%s.",cmStringNullGuard(p->audioDir),cmStringNullGuard(ep->afn));
- goto errLabel;
- }
-
- assert(ep->oei-ep->obi>0 );
-
- // read the audio event from the file into t[]
- if( cmAudioFileGetSample(afn, ep->obi, cmMin(tN,cmMin(frmCnt,ep->oei-ep->obi)), 0, ep->chCnt, chBuf, &actFrmCnt, &afInfo, p->ctx.err.rpt ) != kOkAfRC )
- {
- rc = cmErrMsg(&p->ctx.err,kFileSysFailSdbRC,"Audio event read failed for event uuid:%i in '%s'.",qep->uuid,cmStringNullGuard(afn));
- goto doneLabel;
- }
-
- // 'actFrmCnt' now holds the length of the event signal
-
- // verify that the audio event sample rate matches the sequence srate
- if( afInfo.srate != srate )
- cmErrWarnMsg(&p->ctx.err,kAssertFailSdbRC,"The sample rate (%f) of audio event uuid:%i in '%s' does not match the sequence sample rate:%f.",afInfo.srate,qep->uuid,cmStringNullGuard(afn),srate);
-
- // if a decay rate was specified
- if( dN > 0 )
- {
- unsigned ti = 0; // start of decay in t[]
- unsigned di = 0; // start of decay in d[]
-
- if( actFrmCnt > dN )
- ti = actFrmCnt - dN; // decay func is applied to end of audio event
- else
- di = dN - actFrmCnt; // decay func is longer than audio event (shorten it)
-
- unsigned mn = dN - di; // decay function length
- unsigned j;
-
- // apply the decay function
- for(j=0; j<sp->chCnt; ++j)
- cmVOS_MultVV(t + (j*tN) +ti , mn, d+di);
- }
-
- // normalize the event signal
- if( normFact != 0 )
- cmVOS_NormToAbsMax(t,actFrmCnt,normFact);
-
-
- // verify the the signal event falls inside the output signal
- if( begFrmIdx >= sN )
- rc = cmErrMsg(&p->ctx.err,kAssertFailSdbRC,"A sequence event start time falls after the end of the sequence signal. This should never happen.");
- else
- {
- // if the event signal goes past the end of the signal - truncate the event
- if( begFrmIdx + actFrmCnt > sN )
- actFrmCnt = sN - begFrmIdx;
-
- // sum the event signal into the output signal
- cmVOS_AddVV(s + (qep->outChIdx*sN) + begFrmIdx,actFrmCnt,t);
- }
-
- doneLabel:
- cmFsFreeFn(afn);
-
- }
-
- *signalRef = s;
- *sigSmpCntRef = sN;
- errLabel:
- if( rc != kOkSdbRC )
- cmMemFree(s);
-
- cmMemFree(d);
- cmMemFree(t);
- return rc;
- }
-
- cmSdbRC_t cmSdbSeqToAudioFn(
- cmSdbSeqH_t sh,
- unsigned decayMs,
- double noiseDb,
- double evtNormFact,
- double sigNormFact,
- const cmChar_t* fn,
- unsigned bitsPerSample
- )
- {
- cmSdbRC_t rc = kOkSdbRC;
- cmSample_t* s = NULL;
- unsigned sN = 0;
- cmSdbSeq_t* sp = _cmSdbSeqHandleToPtr(sh);
- cmSdb_t* p = sp->p;
- double srate = cmSdbSeqSampleRate(sh);
- unsigned i;
-
- // fill s[sN] with the sequence audio signal
- if((rc = cmSdbSeqToAudio(sh,decayMs,noiseDb,evtNormFact,&s,&sN)) != kOkSdbRC )
- return rc;
-
- // if no audio signal was created there is nothing to do
- if( sN == 0 )
- return rc;
-
- // the sample rate was already check by cmSdbSeqToAudio().
- assert(srate != 0 && s != NULL);
-
- // if requested normalize the signal
- if( sigNormFact != 0 )
- cmVOS_NormToAbsMax(s,sN*sp->chCnt,sigNormFact);
-
- // fill the channel buffer
- cmSample_t* chBuf[ sp->chCnt ];
- for(i=0; i<sp->chCnt; ++i)
- chBuf[i] = s + (i*sN);
-
- // write the signal to an audio file
- if((rc = cmAudioFileWriteFileFloat(fn, srate, bitsPerSample, sN, sp->chCnt, chBuf, p->ctx.err.rpt )) != kOkAfRC )
- {
- rc = cmErrMsg(&p->ctx.err,kAudioFileFailSdbRC,"The sequence audio file '%s' could not be created.",cmStringNullGuard(fn));
- goto errLabel;
- }
-
- errLabel:
- cmMemFree(s);
- return rc;
- }
-
-
- void cmSdbSeqPrint( cmSdbSeqH_t sh, cmRpt_t* rpt )
- {
- unsigned i;
- unsigned n = cmSdbSeqCount(sh);
- cmSdbSeq_t* sp = _cmSdbSeqHandleToPtr(sh);
- const cmSdbSeqEvent_t* ep;
-
- cmRptPrintf(rpt,"evt cnt:%i ch cnt:%i dur min:%f max:%f \n",sp->cnt,sp->chCnt,sp->minDurSec,sp->maxDurSec);
- cmRptPrintf(rpt," uuid ch beg dur gain \n");
- cmRptPrintf(rpt,"------- --- ------- ------- -------\n");
- for(i=0; i<n; ++i)
- if((ep = cmSdbSeqEvent(sh,i)) != NULL )
- cmRptPrintf(rpt,"%7i %3i %7.3f %7.3f %7.3f\n",ep->uuid,ep->outChIdx,ep->begSec,ep->durSec,ep->gain );
-
- }
-
-
-
- cmSdbRC_t cmSdbTest( cmCtx_t* ctx )
- {
- cmSdbRC_t rc = kOkSdbRC;
- cmSdbH_t h = cmSdbNullHandle;
- const cmChar_t* audioDir = "/home/kevin/media/audio";
- const cmChar_t* csvFn = "/home/kevin/temp/sdb0/sdb_master.csv";
- cmErr_t err;
-
- cmErrSetup(&err,&ctx->rpt,"sdb test");
-
- if((rc = cmSdbCreate(ctx, &h, csvFn, audioDir )) != kOkSdbRC )
- {
- rc = cmErrMsg(&err,rc,"sdb create failed.");
- goto errLabel;
- }
-
- if((rc = cmSdbSyncChPairs(h)) != kOkSdbRC )
- {
- rc = cmErrMsg(&err,rc,"sdb sync-ch-pairs failed.");
- goto errLabel;
- }
-
- if(1)
- {
- cmSdbResponseH_t rH = cmSdbResponseNullHandle;
- cmSdbSeqH_t sH = cmSdbSeqNullHandle;
-
- const cmChar_t* instrV[] = { "violin", NULL };
- const cmChar_t* srcV[] = { "ui", NULL };
- const cmChar_t* notesV[] = { "!vibrato", NULL };
-
- if((rc = cmSdbSelect(h,0,instrV,srcV,notesV,NULL,0,0,0,&rH)) != kOkSdbRC )
- {
- rc = cmErrMsg(&err,rc,"sdb query failed.");
- goto errLabel;
- }
-
- //cmSdbResponsePrint(rH,&ctx->rpt);
-
- unsigned seqDurSecs = 15;
- unsigned seqChCnt = 2;
- unsigned sel = 2;
- switch( sel )
- {
- case 0:
- {
- unsigned minEvtPerSec = 1;
- unsigned maxEvtPerSec = 5;
-
- if((rc = cmSdbSeqRand(rH,seqDurSecs,seqChCnt,minEvtPerSec,maxEvtPerSec,&sH)) != kOkSdbRC )
- {
- rc = cmErrMsg(&err,rc,"sdb random sequence generation failed.");
- goto errLabel;
- }
- }
- break;
-
- case 1:
- {
- double gapSec = 0.1;
- double maxEvtDurSec = 1.0;
-
- if((rc = cmSdbSeqSerial(rH,seqChCnt,gapSec,maxEvtDurSec,&sH)) != kOkSdbRC )
- {
- rc = cmErrMsg(&err,rc,"sdb serial sequence generation failed.");
- goto errLabel;
- }
- }
- break;
-
- case 2:
- {
- cmSdbResponseH_t rhV[] = { rH, rH, rH };
- unsigned rN = sizeof(rhV)/sizeof(rhV[0]);
- double maxEvtDurSec = 1.0;
-
- if((rc = cmSdbSeqChord(rhV,rN,seqChCnt,maxEvtDurSec,&sH)) != kOkSdbRC )
- {
- rc = cmErrMsg(&err,rc,"sdb chord sequence generation failed.");
- goto errLabel;
- }
- }
- break;
- }
-
- cmSdbSeqPrint(sH,&ctx->rpt);
-
- const cmChar_t* afn = "/home/kevin/temp/aaa.aif";
- unsigned decayMs = 50;
- double noiseDb = -70.0;
- double evtNormFact = 0; //0.7;
- double sigNormFact = 0.7; //0.7;
- unsigned bitsPerSample = 16;
-
- if((rc = cmSdbSeqToAudioFn(sH,decayMs,noiseDb,evtNormFact,sigNormFact,afn,bitsPerSample)) != kOkSdbRC )
- {
- rc = cmErrMsg(&err,rc,"sdb sequence audio file generation failed.");
- goto errLabel;
- }
-
- cmSdbSeqFree(&sH);
- cmSdbResponseFree(&rH);
- }
-
- errLabel:
- if((rc = cmSdbDestroy(&h)) != kOkSdbRC )
- rc = cmErrMsg(&err,rc,"sdb destroy failed.");
-
- return rc;
- }
|