//| Copyright: (C) 2009-2020 Kevin Larke //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file. #include "cmPrefix.h" #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 "cmText.h" #include "cmFileSys.h" #include "cmSymTbl.h" #include "cmJson.h" #include "cmPrefs.h" #include "cmDspValue.h" #include "cmMsgProtocol.h" #include "cmThread.h" #include "cmUdpPort.h" #include "cmUdpNet.h" #include "cmSerialPort.h" #include "cmTime.h" #include "cmAudioSys.h" #include "cmProcObj.h" #include "cmDspCtx.h" #include "cmDspClass.h" #include "cmDspSys.h" #include "cmDspPgm.h" #include "cmDspPgmPPMain.h" #include "cmAudioFile.h" #include "cmProcObj.h" #include "cmProc.h" #include "cmProc3.h" #include "cmVectOpsTemplateMain.h" #include "cmVectOps.h" /* subnet [ aout ] = fx_chain( ain, hzV ) { cmpThreshDb = -40.0; cmpRatio = 4; cmpAtkMs = 30.0; cmpRlsMs = 100.0; dstInGain = 1.0; dstDSrate = 44100.0; dstBits = 24; eqMode = "LP"; eqHz = hzV[ _i ]; dlyMaxMs = 10000.0; dlyFb = 0.8; loop = loopRecd( ); ps = pitchShift( ); cmp = compressor( cmpThreshDb, cmpRatio, cmpAtkMs, cmpRlsMs ); dst = distortion( dstInGain, dstDSrate, dstBits ); eq = eq( eqMode, eqHz ); dly = delay( dlyMaxMs, dlyFb ); ain -> in.loop.out -> in.ps.out -> in.ps.out loop.out -> ps.in; ps.out -> cmp.in; cmp.out -> dst.in; dst.out -> eq.in; eq.out -> dly.in; dly.out -> aout } audInMapV = [ 0, 1, 0, 1]; audOutMapV = [ 0, 1 ]; hzV = [ 110 220 440 880 ]; ain = audioInput( audioInMap, 1.0 )[ chCnt ]; ef = envFollower( )[ chCnt ]; mtr = meter(0.0,1.0,0.0)[ chCnt ]; fx = fx_chain( ain )( hzV )[ chCnt ]; aout = audioOutput( audioOutMap, 1.0 )[ chCnt ]; ain.out -> ef.in ef.rms -> mtr.in */ // Use the resource int arrays 'pitchList' and 'groupList' to create // the following resource int arrays: // CDmidi - MIDI pitch of all chord notes // CD0midi - MIDI pitch of all low/high chord notes // CD1midi - MIDI pitch of all middle chord notes // NONmidi - MIDI pitch of all non-chord notes. // CDchan - channel index of all chord notes // CD0chan - channel index of all low/high chord notes // CD1chan - channel index of all middle chord notes // NONchan - channel index of all non-chord notes. // cmDspRC_t _cmDspPgm_Main_ProcRsrc( cmDspSysH_t h, cmErr_t* err ) { cmDspRC_t rc; unsigned* midiList = NULL; unsigned midiCnt = 0; unsigned* groupList = NULL; unsigned groupCnt = 0; const cmChar_t* midiListRsrcLabel = "midiList"; const cmChar_t* groupListRsrcLabel = "groupList"; cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h); unsigned i,j,k; if((rc = cmDspRsrcUIntArray(h,&midiCnt,&midiList,midiListRsrcLabel,NULL)) != kOkDspRC ) { rc = cmErrMsg(err,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",midiListRsrcLabel); goto errLabel; } if((rc = cmDspRsrcUIntArray(h,&groupCnt,&groupList,groupListRsrcLabel,NULL)) != kOkDspRC ) { rc = cmErrMsg(err,kRsrcNotFoundDspRC,"The resource '%s' could not be read.",groupListRsrcLabel); goto errLabel; } if( midiCnt != groupCnt ) { rc = cmErrMsg(err,kInvalidStateDspRC,"The resource arrays:%s and %s were not the same length.",midiListRsrcLabel,groupListRsrcLabel); goto errLabel; } for(i=0; i<4; ++i) { int midi[ midiCnt ]; int chan[ midiCnt ]; const cmChar_t* label = NULL; const cmChar_t* label1; for(j=0,k=0; jrpt, 1, midiCnt, nsChSelChV ); cmVOI_PrintL("nsChSelChIdxV", err->rpt, 1, midiCnt, nsChSelChIdxV ); cmVOI_PrintL("nsNcSelChV", err->rpt, 1, midiCnt, nsNcSelChV ); cmVOI_PrintL("nsNcSelChIdxV", err->rpt, 1, midiCnt, nsNcSelChIdxV ); } if( cmJsonInsertPairIntArray( jsH, cmJsonRoot(jsH), label1 = "nsChSelChV", midiCnt, nsChSelChV ) != kOkJsRC ) { rc = cmErrMsg(err,kJsonFailDspRC,"The note selector resource '%s' could not be created.", cmStringNullGuard(label1)); goto errLabel; } if( cmJsonInsertPairIntArray( jsH, cmJsonRoot(jsH), label1 = "nsChSelChIdxV", midiCnt, nsChSelChIdxV ) != kOkJsRC ) { rc = cmErrMsg(err,kJsonFailDspRC,"The note selector resource '%s' could not be created.", cmStringNullGuard(label1)); goto errLabel; } if( cmJsonInsertPairIntArray( jsH, cmJsonRoot(jsH), label1 = "nsNcSelChV", midiCnt, nsNcSelChV ) != kOkJsRC ) { rc = cmErrMsg(err,kJsonFailDspRC,"The note selector resource '%s' could not be created.", cmStringNullGuard(label1)); goto errLabel; } if( cmJsonInsertPairIntArray( jsH, cmJsonRoot(jsH), label1 = "nsNcSelChIdxV", midiCnt, nsNcSelChIdxV ) != kOkJsRC ) { rc = cmErrMsg(err,kJsonFailDspRC,"The note selector resource '%s' could not be created.", cmStringNullGuard(label1)); goto errLabel; } for(i=0; i15 ? j-16 : j; assert( k == nsChSelChIdxV[j] ); ++k; } if( nsNcSelChV[j] == m ) { midi[k] = midiList[j]; chan[k] = j>15 ? j-16 : j; assert( k == nsNcSelChIdxV[j] ); ++k; } } if( cmJsonInsertPairIntArray( jsH, cmJsonRoot(jsH), label1 = cmTsPrintfS("nsMidi-%i",i), k, midi ) != kOkJsRC ) { rc = cmErrMsg(err,kJsonFailDspRC,"The note selector resource '%s' could not be created.", cmStringNullGuard(label1)); goto errLabel; } if( verboseFl ) cmVOI_PrintL(label1, err->rpt, 1, k, midi ); if( cmJsonInsertPairIntArray( jsH, cmJsonRoot(jsH), label1 = cmTsPrintfS("nsChan-%i",i), k, chan ) != kOkJsRC ) { rc = cmErrMsg(err,kJsonFailDspRC,"The note selector resource '%s' could not be created.", cmStringNullGuard(label1)); goto errLabel; } if( verboseFl ) cmVOI_PrintL(label1, err->rpt, 1, k, chan ); } } errLabel: return rc; } const cmChar_t* _cmDspPP_SelCheckTitle( cmDspSysH_t h, unsigned i ) { return _cmDspPP_CircuitDesc(i)->title; } cmDspRC_t _cmDspSysPgm_Main( cmDspSysH_t h, void** userPtrPtr ) { cmDspRC_t rc = kOkDspRC; const cmChar_t* chCfgFn = NULL; cmCtx_t* cmCtx = cmDspSysPgmCtx(h); double efOnThrDb = -40; double efOffThrDb = -70; double efMaxDb = -15; unsigned circuitCnt = _cmDspPP_CircuitDescCount(); unsigned resetSymId = cmDspSysRegisterStaticSymbol(h,"_reset"); bool inFileFl = false; cmErr_t err; _cmDspPP_Ctx_t ctx; cmDspPP_CircuitSwitch_t sw; memset(&sw,0,sizeof(sw)); // set up the global context record memset(&ctx,0,sizeof(ctx)); ctx.oChCnt = 2; ctx.err = &err; cmErrSetup(&err,&cmCtx->rpt,"PP Main"); // create the individual non-networked chord detector resource arrays from the base resource arrays if((rc =_cmDspPgm_Main_ProcRsrc(h,&err)) != kOkDspRC ) goto errLabel; // create the individual networked chord detector resource arrays from the base resource arrays if((rc = _cmDspPgm_Main_NetProcRsrc(h,&err)) != kOkDspRC ) goto errLabel; // get the channel cfg configuration file name if( cmJsonPathToString( cmDspSysPgmRsrcHandle(h), NULL, NULL, "chCfgFn", &chCfgFn ) != kOkJsRC ) { rc = cmErrMsg(&err,kRsrcNotFoundDspRC,"The 'chCfgFn' resource was not found."); goto errLabel; } // get the count of channels from the ch. cfg. array if(( ctx.iChCnt = cmChCfgChannelCount(cmCtx,chCfgFn,&ctx.nsCnt)) == 0 ) { rc = cmErrMsg(&err,kPgmCfgFailDspRC,"Unable to obtain the channel count from '%s'.",cmStringNullGuard(chCfgFn)); goto errLabel; } if( rc == kOkDspRC ) { unsigned inChCnt,outChCnt; // channel cfg ctx.chCfg = cmDspSysAllocInst( h, "ChCfg", NULL, 1, chCfgFn ); // global printer ctx.print = cmDspSysAllocInst( h, "Printer", NULL, 1, ">" ); cmDspInst_t** ain; cmDspInst_t* phs; // audio input if( inFileFl ) { unsigned wtSmpCnt = (unsigned)cmDspSysSampleRate(h); unsigned wtMode = 1; //file mode const cmChar_t* fn = "/home/kevin/media/audio/gate_detect/gate_detect2.aif"; phs = cmDspSysAllocInst(h,"Phasor", NULL, 0 ); ain = cmDspSysAllocInstArray(h, ctx.iChCnt, "WaveTable", NULL, NULL, 3, wtSmpCnt, wtMode, fn ); inChCnt = ctx.iChCnt; } else { ain = cmDspSysAllocAudioInAR( h, "audioInMap", 1.0, &inChCnt ); } // envelope followers and RMS meters cmDspSysAllocLabel(h,"EF Gates",kLeftAlignDuiId ); cmDspInst_t** ef = cmDspSysAllocInstArray( h, ctx.iChCnt,"EnvFollow", NULL, NULL, 0 ); cmDspInst_t** mtr = cmDspSysAllocInstArray( h, ctx.iChCnt,"Meter", "mtr", NULL, 3, 0.0, 0.0, 1.0 ); // Level meters cmDspSysNewColumn(h,200); cmDspSysAllocLabel(h,"Level",kLeftAlignDuiId ); cmDspInst_t** lvl = cmDspSysAllocInstArray( h, ctx.iChCnt,"Meter", "lvl", NULL, 3, 0.0, 0.0, 1.0 ); assert( inChCnt == ctx.iChCnt ); // Onset count displays cmDspSysNewColumn(h,200); cmDspInst_t** onn = cmDspSysAllocInstArray( h, ctx.iChCnt,"Scalar", "on", NULL, 5, kNumberDuiId, 0.0, 10000.0, 1.0, 0.0 ); cmDspSysNewColumn(h,200); // program reset button cmDspInst_t* resetBtn = cmDspSysAllocButton( h, "reset", 0 ); cmDspSysAssignInstAttrSymbol(h,resetBtn, resetSymId ); // circuit selection check-boxes cmDspInst_t** csel = cmDspSysAllocInstArray( h, circuitCnt, "Button", NULL, _cmDspPP_SelCheckTitle, 2, kCheckDuiId, 0.0 ); cmDspSysNewColumn(h,200); cmDspSysAllocLabel(h,"Out Gains",kLeftAlignDuiId ); // output gain controls cmDspInst_t** ogain = cmDspSysAllocInstArray( h, ctx.oChCnt,"Scalar", "mgain", NULL, 5, kNumberDuiId, 0.0, 10.0, 0.01, 1.0); // envelope follower parameters cmDspInst_t* onThr = cmDspSysAllocScalar( h, "On Thresh",-100.0, 0.0, 0.1, efOnThrDb); cmDspInst_t* offThr = cmDspSysAllocScalar( h, "Off Thresh",-100.0, 0.0, 0.1, efOffThrDb); cmDspInst_t* maxDb = cmDspSysAllocScalar( h, "Max Db", -100.0, 0.0, 0.1, efMaxDb); // switcher and circuits _cmDspPP_CircuitSwitchAlloc(h, &ctx, &sw, resetBtn, csel, ain, ef); // audio output cmDspInst_t** aout = cmDspSysAllocAudioOutAR( h, "audioOutMap", 1.0, &outChCnt ); assert( outChCnt == ctx.oChCnt ); //cmDspInst_t* prt = cmDspSysAllocInst( h, "Printer", NULL, 2, "", 250 ); // check for errors if((rc = cmDspSysLastRC(h)) != kOkDspRC ) goto doneLabel; if( inFileFl ) { cmDspSysConnectAudio11N1( h, phs, "out", ain, "phs", ctx.iChCnt ); // phs -> wt cmDspSysConnectAudioN1N1( h, ain, "out", ef, "in", ctx.iChCnt ); // wt -> EF } else { cmDspSysConnectAudioN1N1( h, ain, "out", ef, "in", ctx.iChCnt ); // ain -> EF } cmDspSysInstallCb1NN1( h, ctx.chCfg, "gain", ain, "gain", ctx.iChCnt ); // chCfg -> ain (gain) cmDspSysConnectAudioN1N1( h, sw.omix, "out", aout, "in", ctx.oChCnt ); // Sw.mix -> aout cmDspSysInstallCbN1N1( h, ogain, "val", aout, "gain", ctx.oChCnt ); // gain -> aout cmDspSysInstallCb( h, resetBtn,"sym", ctx.chCfg, "sel", NULL ); // reset -> chCfg cmDspSysInstallCbN1N1( h, ef, "gate", mtr, "in", ctx.iChCnt ); // EF -> meter (gate) cmDspSysInstallCbN1N1( h, ef, "level", lvl, "in", ctx.iChCnt ); // EF -> meter (level) cmDspSysInstallCbN1N1( h, ef, "ons", onn, "val", ctx.iChCnt ); cmDspSysInstallCb11N1( h, onThr, "val", ef, "ondb", ctx.iChCnt ); // EF sensivity settings cmDspSysInstallCb11N1( h, offThr, "val", ef, "offdb", ctx.iChCnt ); // cmDspSysInstallCb11N1( h, maxDb, "val", ef, "maxdb", ctx.iChCnt ); // //cmDspSysInstallCbN111( h, ef, "level", prt, "in", ctx.iChCnt ); doneLabel: _cmDspPP_CircuitSwitchFree(h, &sw ); } errLabel: if( rc != kOkDspRC ) cmErrMsg(&err,rc,"'main' construction failed."); return rc; }