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

cmDspKr.c 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmComplexTypes.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmFile.h"
  12. #include "cmSymTbl.h"
  13. #include "cmJson.h"
  14. #include "cmPrefs.h"
  15. #include "cmDspValue.h"
  16. #include "cmMsgProtocol.h"
  17. #include "cmThread.h"
  18. #include "cmUdpPort.h"
  19. #include "cmUdpNet.h"
  20. #include "cmAudioSys.h"
  21. #include "cmDspCtx.h"
  22. #include "cmDspClass.h"
  23. #include "cmDspUi.h"
  24. #include "cmOp.h"
  25. #include "cmMath.h"
  26. #include "cmAudioFile.h"
  27. #include "cmFileSys.h"
  28. #include "cmProcObj.h"
  29. #include "cmProcTemplateMain.h"
  30. #include "cmProc.h"
  31. #include "cmMidi.h"
  32. #include "cmProc2.h"
  33. #include "cmVectOpsTemplateMain.h"
  34. enum
  35. {
  36. kWndSmpCntKrId,
  37. kHopFactKrId,
  38. kModeKrId,
  39. kThreshKrId,
  40. kLwrSlopeKrId,
  41. kUprSlopeKrId,
  42. kOffsetKrId,
  43. kInvertKrId,
  44. kAudioInKrId,
  45. kAudioOutKrId
  46. };
  47. typedef struct
  48. {
  49. cmDspInst_t inst;
  50. cmCtx* ctx;
  51. cmSpecDist_t* sdp;
  52. } cmDspKr_t;
  53. cmDspClass_t _cmKrDC;
  54. // cm console output function
  55. void _cmKrCmRptFunc( void* userDataPtr, const cmChar_t* fmt, va_list vl )
  56. {
  57. cmCtx_t* p = (cmCtx_t*)userDataPtr;
  58. if( p == NULL )
  59. vprintf(fmt,vl);
  60. else
  61. cmRptVPrintf(&p->rpt,fmt,vl);
  62. }
  63. // initialize the cm library
  64. /*
  65. cmDspRC_t cmDspKrCmLibInit(cmCtx_t* cmCtx )
  66. {
  67. bool debugFl = false;
  68. #ifdef NDEBUG
  69. debugFl = true;
  70. #endif
  71. unsigned memPadByteCnt = cmCtx->guardByteCnt;
  72. unsigned memAlignByteCnt = cmCtx->alignByteCnt;
  73. unsigned memAutoBlockByteCnt = 0;
  74. unsigned memFlags = cmCtx->mmFlags;
  75. if( cmMallocDebugIsInit() == false )
  76. cmMallocDebugInitialize( memPadByteCnt, memAlignByteCnt, memAutoBlockByteCnt, memFlags, _cmKrCmRptFunc, cmCtx );
  77. return kOkDspRC;
  78. }
  79. // free the cm library
  80. cmDspRC_t cmDspKrCmLibFinal()
  81. {
  82. #ifdef NDEBUG
  83. cmMallocDebugReport( vPrintF, _rptUserPtr, 0);
  84. #endif
  85. if( cmMallocDebugIsInit() )
  86. cmMallocDebugFinalize();
  87. return kOkDspRC;
  88. }
  89. */
  90. cmDspInst_t* _cmDspKrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  91. {
  92. cmDspVarArg_t args[] =
  93. {
  94. { "wndn", kWndSmpCntKrId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Window sample count" },
  95. { "hopf", kHopFactKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Hop factor" },
  96. { "mode", kModeKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Mode 0=bypass 1=basic 2=spec cnt 3=amp env" },
  97. { "thrh", kThreshKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Threshold" },
  98. { "lwrs", kLwrSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Lower Slope"},
  99. { "uprs", kUprSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Upper Slope"},
  100. { "offs", kOffsetKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Offset"},
  101. { "invt", kInvertKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Invert"},
  102. { "in", kAudioInKrId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio Input" },
  103. { "out", kAudioOutKrId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio Output" },
  104. { NULL, 0, 0, 0, 0 }
  105. };
  106. cmDspKr_t* p = cmDspInstAlloc(cmDspKr_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  107. unsigned defWndSmpCnt = cmDspDefaultUInt(&p->inst,kWndSmpCntKrId);
  108. unsigned wndSmpCnt = cmNextPowerOfTwo( defWndSmpCnt );
  109. cmDspSetDefaultUInt( ctx,&p->inst, kWndSmpCntKrId, defWndSmpCnt, wndSmpCnt );
  110. cmDspSetDefaultUInt( ctx,&p->inst, kHopFactKrId, 0, 4 );
  111. cmDspSetDefaultUInt( ctx,&p->inst, kModeKrId, 0, kBasicModeSdId );
  112. cmDspSetDefaultDouble( ctx,&p->inst, kThreshKrId, 0, 60.0 );
  113. cmDspSetDefaultDouble( ctx,&p->inst, kLwrSlopeKrId, 0, 2.0 );
  114. cmDspSetDefaultDouble( ctx,&p->inst, kUprSlopeKrId, 0, 0.0 );
  115. cmDspSetDefaultDouble( ctx,&p->inst, kOffsetKrId, 0, 30.0);
  116. cmDspSetDefaultUInt( ctx,&p->inst, kInvertKrId, 0, 0 );
  117. //_cmDspKrCmInit(ctx,p); // initialize the cm library
  118. p->ctx = cmCtxAlloc(NULL,ctx->rpt,ctx->lhH,ctx->stH);
  119. return &p->inst;
  120. }
  121. cmDspRC_t _cmDspKrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  122. {
  123. cmDspRC_t rc = kOkDspRC;
  124. cmDspKr_t* p = (cmDspKr_t*)inst;
  125. cmSpecDistFree(&p->sdp);
  126. cmCtxFree(&p->ctx);
  127. //_cmDspKrCmFinal(ctx,p); // finalize the cm library
  128. return rc;
  129. }
  130. cmDspRC_t _cmDspKrSetup(cmDspCtx_t* ctx, cmDspKr_t* p )
  131. {
  132. cmDspRC_t rc = kOkDspRC;
  133. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndSmpCntKrId);
  134. unsigned hopFact = cmDspUInt(&p->inst,kHopFactKrId);
  135. unsigned olaWndTypeId = kHannWndId;
  136. cmSpecDistFree(&p->sdp);
  137. p->sdp = cmSpecDistAlloc(p->ctx, NULL, cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), wndSmpCnt, hopFact, olaWndTypeId);
  138. assert(p->sdp != NULL );
  139. if((rc = cmDspZeroAudioBuf(ctx,&p->inst,kAudioOutKrId)) != kOkDspRC )
  140. return rc;
  141. return rc;
  142. }
  143. cmDspRC_t _cmDspKrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  144. {
  145. cmDspKr_t* p = (cmDspKr_t*)inst;
  146. cmDspRC_t rc;
  147. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  148. return rc;
  149. return _cmDspKrSetup(ctx,p);
  150. }
  151. cmDspRC_t _cmDspKrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  152. {
  153. cmDspKr_t* p = (cmDspKr_t*)inst;
  154. cmDspRC_t rc = kOkDspRC;
  155. unsigned iChIdx = 0;
  156. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kAudioInKrId,iChIdx);
  157. unsigned iSmpCnt = cmDspVarRows(inst,kAudioInKrId);
  158. unsigned oChIdx = 0;
  159. cmSample_t* op = cmDspAudioBuf(ctx,inst,kAudioOutKrId,oChIdx);
  160. unsigned oSmpCnt = cmDspVarRows(inst,kAudioOutKrId);
  161. const cmSample_t* sp;
  162. cmSpecDistExec(p->sdp,ip,iSmpCnt);
  163. if((sp = cmSpecDistOut(p->sdp)) != NULL )
  164. vs_Copy(op,sp,oSmpCnt);
  165. return rc;
  166. }
  167. cmDspRC_t _cmDspKrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  168. {
  169. cmDspKr_t* p = (cmDspKr_t*)inst;
  170. cmDspRC_t rc = kOkDspRC;
  171. cmDspSetEvent(ctx,inst,evt);
  172. switch( evt->dstVarId )
  173. {
  174. case kWndSmpCntKrId:
  175. case kHopFactKrId:
  176. _cmDspKrSetup(ctx,p);
  177. printf("wsn:%i hsn:%i\n",p->sdp->wndSmpCnt,p->sdp->hopSmpCnt);
  178. break;
  179. case kModeKrId:
  180. p->sdp->mode = cmDspUInt(inst,kModeKrId);
  181. printf("mode:%i\n",p->sdp->mode);
  182. break;
  183. case kThreshKrId:
  184. p->sdp->thresh = cmDspDouble(inst,kThreshKrId);
  185. break;
  186. case kUprSlopeKrId:
  187. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKrId);
  188. printf("upr slope:%f\n",p->sdp->uprSlope);
  189. break;
  190. case kLwrSlopeKrId:
  191. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKrId);
  192. printf("upr slope:%f\n",p->sdp->lwrSlope);
  193. break;
  194. case kOffsetKrId:
  195. p->sdp->offset = cmDspDouble(inst,kOffsetKrId);
  196. break;
  197. case kInvertKrId:
  198. p->sdp->invertFl = cmDspUInt(inst,kInvertKrId)!=0;
  199. break;
  200. default:
  201. { assert(0); }
  202. }
  203. return rc;
  204. }
  205. struct cmDspClass_str* cmKrClassCons( cmDspCtx_t* ctx )
  206. {
  207. cmDspClassSetup(&_cmKrDC,ctx,"Kr",
  208. NULL,
  209. _cmDspKrAlloc,
  210. _cmDspKrFree,
  211. _cmDspKrReset,
  212. _cmDspKrExec,
  213. _cmDspKrRecv,
  214. NULL,NULL,
  215. "Fourier based non-linear transformer.");
  216. return &_cmKrDC;
  217. }