libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmProc3.c 89KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmSymTbl.h"
  10. #include "cmFloatTypes.h"
  11. #include "cmComplexTypes.h"
  12. #include "cmFileSys.h"
  13. #include "cmJson.h"
  14. #include "cmProcObj.h"
  15. #include "cmProcTemplate.h"
  16. #include "cmAudioFile.h"
  17. #include "cmMath.h"
  18. #include "cmProc.h"
  19. #include "cmVectOps.h"
  20. #include "cmProc3.h"
  21. #include "cmTime.h"
  22. #include "cmMidi.h" // cmMidiToSciPitch();
  23. cmPitchShift* cmPitchShiftAlloc( cmCtx* c, cmPitchShift* p, unsigned procSmpCnt, cmReal_t srate )
  24. {
  25. cmPitchShift* op = cmObjAlloc(cmPitchShift, c, p );
  26. if( procSmpCnt != 0 )
  27. if( cmPitchShiftInit(op, procSmpCnt,srate) != cmOkRC )
  28. cmPitchShiftFree(&op);
  29. return op;
  30. }
  31. cmRC_t cmPitchShiftFinal(cmPitchShift* p )
  32. { return cmOkRC; }
  33. #define _cube_interp(x,y0,y1,y2,y3) ((y3) - (y2) - (y0) + (y1))*(x)*(x)*(x) + ((y0) - (y1) - ((y3) - (y2) - (y0) + (y1)))*(x)*(x) + ((y2) - (y0))*(x) + (y1)
  34. #define _lin_interp(x,y0,y1) (y0) + (x) * ((y1)-(y0))
  35. /*
  36. cmRC_t cmPitchShiftFree( cmPitchShift** pp )
  37. {
  38. cmRC_t rc = cmOkRC;
  39. if( pp != NULL && *pp != NULL )
  40. {
  41. cmPitchShift* p = *pp;
  42. if(( rc = cmPitchShiftFinal(p)) == cmOkRC )
  43. {
  44. cmMemPtrFree(&p->x);
  45. cmMemPtrFree(&p->y);
  46. cmMemPtrFree(&p->cf);
  47. cmObjFree(pp);
  48. }
  49. }
  50. return rc;
  51. }
  52. cmRC_t cmPitchShiftInit( cmPitchShift* p, unsigned procSmpCnt, double srate )
  53. {
  54. cmRC_t rc;
  55. if((rc = cmPitchShiftFinal(p)) != cmOkRC )
  56. return rc;
  57. p->procSmpCnt = procSmpCnt;
  58. p->srate = srate;
  59. p->wn = 4096;
  60. p->cfn = 1024;
  61. p->outN = p->procSmpCnt;
  62. p->x = cmMemResizeZ(cmSample_t, p->x,procSmpCnt + 3 );
  63. p->y = cmMemResizeZ(cmSample_t, p->y, p->wn );
  64. p->cf = cmMemResizeZ(cmSample_t, p->cf, p->cfn );
  65. p->pii = procSmpCnt + p->cfn;
  66. p->poi = 0;
  67. p->outV = p->y;
  68. p->prv_x0 = 0;
  69. p->prv_x1 = 0;
  70. p->genFl = true;
  71. p->xfi = 3;
  72. p->cfi = 0;
  73. p->cubeFl = true;
  74. p->linCfFl = true;
  75. assert( p->cfn+p->procSmpCnt < p->wn );
  76. unsigned i;
  77. for(i=0; i<p->cfn; ++i)
  78. {
  79. p->cf[i] = (sin(((double)i/p->cfn * M_PI) - (M_PI/2.0)) + 1.0) / 2.0;
  80. p->cf[i] = (double)i/p->cfn;
  81. }
  82. return rc;
  83. }
  84. // Algorithm:
  85. // 1. Generate srate converted values into p->y[] until it is full.
  86. // 2. Playback from y[] ignoring any incoming samples until less than
  87. // p->cfn + p->procSmpCnt samples remain in y[] (i.e. pii < procSmpCnt + cfn).
  88. // 3. At this point begin generating samples from the input and crossfading them
  89. // with the last cfn samples in y[].
  90. // 4. After cfn new samples have been crossfaded proceed to fill the remaining
  91. // space in y[]
  92. // 5. Goto 2.
  93. //
  94. // Note that since we are pitch shifting down (ratio<1.0) the number of samples generated
  95. // will always be greater than the number of input samples.
  96. //
  97. // Notes:
  98. // For small downward shifts there will be large time aliasing because the length of time
  99. // to fill the buffer (step 4) will because only a few extra samples
  100. // are generated on each input cycle. Try decreasing the window length as the pitch ratio increases.
  101. //
  102. // Implement smoothly varying ratio changes.
  103. //
  104. // Change model so that at ratio 1.0 the input is effectively being constantly cross faded to produce
  105. // identity output.
  106. void _cmPitchShiftDown( cmPitchShift* p, double ratio, const cmSample_t* x, unsigned xn )
  107. {
  108. // shift off the expired samples
  109. memmove(p->y,p->y + p->procSmpCnt, (p->pii - p->procSmpCnt) * sizeof(cmSample_t));
  110. p->pii -= p->procSmpCnt;
  111. // if not currently generating and there are less than cfn + procSmpCnt samples remaining ...
  112. if( p->genFl == false && p->pii <= p->procSmpCnt + p->cfn )
  113. {
  114. // ... then setup to begin generating
  115. p->cfi = 0;
  116. p->genFl = true;
  117. p->xfi = 3;
  118. }
  119. if( p->genFl )
  120. {
  121. // setup the incoming samples
  122. p->x[0] = p->prv_x0;
  123. p->x[1] = p->prv_x1;
  124. p->x[2] = p->prv_x2;
  125. memcpy(p->x+2,x,p->procSmpCnt*sizeof(cmSample_t));
  126. int n0 = p->cubeFl ? xn+1 : xn+2;
  127. // as long as there are incoming samples available and space left in the output buffer
  128. for(; p->xfi < n0 && p->pii < p->wn; p->xfi += ratio )
  129. {
  130. unsigned xii = floor(p->xfi);
  131. double frac = p->xfi - xii;
  132. // generate the value of the output sample
  133. cmSample_t value;
  134. if( p->cubeFl )
  135. value = _cube_interp(frac,p->x[xii-1],p->x[xii],p->x[xii+1],p->x[xii+2]);
  136. else
  137. value = p->x[ xii ] + frac * (p->x[ xii+1 ] - p->x[xii]);
  138. // if xfading
  139. if( p->cfi < p->cfn )
  140. {
  141. // calc index into y[] of the sample to crossfade with
  142. int idx = p->pii - p->cfn + p->cfi;
  143. p->y[idx] = (1.0 - p->cf[p->cfi]) * p->y[idx] + p->cf[p->cfi] * value;
  144. ++p->cfi;
  145. }
  146. else
  147. {
  148. // else fill the remaing space in y[]
  149. p->y[p->pii] = value;
  150. ++p->pii;
  151. }
  152. }
  153. // if the output buffer is full then stop generating
  154. if( p->pii == p->wn )
  155. p->genFl = false;
  156. }
  157. // reset the input index to the beginning of the buffer
  158. p->xfi -= xn;
  159. p->prv_x0 = x[xn-3];
  160. p->prv_x1 = x[xn-2];
  161. p->prv_x2 = x[xn-1];
  162. }
  163. cmRC_t cmPitchShiftExec( cmPitchShift* p, const cmSample_t* x, cmSample_t* y, unsigned n, double shiftRatio )
  164. {
  165. assert(n == p->procSmpCnt);
  166. if( shiftRatio >= 1 )
  167. memcpy(y,x,n*sizeof(cmSample_t));
  168. else
  169. {
  170. _cmPitchShiftDown(p,shiftRatio,x,n);
  171. if( y != NULL )
  172. memcpy(y,p->y,n*sizeof(cmSample_t));
  173. }
  174. return cmOkRC;
  175. }
  176. */
  177. cmRC_t cmPitchShiftFree( cmPitchShift** pp )
  178. {
  179. cmRC_t rc = cmOkRC;
  180. if( pp != NULL && *pp != NULL )
  181. {
  182. cmPitchShift* p = *pp;
  183. if(( rc = cmPitchShiftFinal(p)) == cmOkRC )
  184. {
  185. cmMemPtrFree(&p->b);
  186. cmMemPtrFree(&p->outV);
  187. cmMemPtrFree(&p->wnd);
  188. cmMemPtrFree(&p->osc[0].y);
  189. cmMemPtrFree(&p->osc[1].y);
  190. cmObjFree(pp);
  191. }
  192. }
  193. return rc;
  194. }
  195. /*
  196. cmRC_t cmPitchShiftInit( cmPitchShift* p, unsigned procSmpCnt, double srate)
  197. {
  198. cmRC_t rc;
  199. if((rc = cmPitchShiftFinal(p)) != cmOkRC )
  200. return rc;
  201. p->procSmpCnt = procSmpCnt;
  202. p->srate = srate;
  203. p->wn = 4096;
  204. p->in = 2*p->wn + p->procSmpCnt;
  205. p->outN = p->procSmpCnt;
  206. p->m = cmMemResizeZ(cmSample_t, p->m,p->in + 2 );
  207. p->x = p->m + 1;
  208. p->outV = cmMemResizeZ(cmSample_t, p->outV,procSmpCnt );
  209. p->wnd = cmMemResizeZ(cmSample_t, p->wnd, p->wn+1 );
  210. p->ii = 1;
  211. p->cubeFl = true;
  212. p->ei0 = -1;
  213. p->ei1 = -1;
  214. p->osc[0].ratio = 1.0;
  215. p->osc[0].wi = 0;
  216. p->osc[0].xi = 0;
  217. p->osc[0].yN = 2*p->wn;
  218. p->osc[0].y = cmMemResizeZ(cmSample_t, p->osc[0].y, p->osc[0].yN );
  219. p->osc[0].yii = 0;
  220. p->osc[0].yoi = 0;
  221. p->osc[0].ynn = 0;
  222. p->osc[1].ratio = 1.0;
  223. p->osc[1].wi = floor(p->wn/2);
  224. p->osc[1].xi = 0;
  225. p->osc[1].yN = 2*p->wn;
  226. p->osc[1].y = cmMemResizeZ(cmSample_t, p->osc[1].y, p->osc[1].yN );
  227. p->osc[1].yii = 0;
  228. p->osc[1].yoi = 0;
  229. p->osc[1].ynn = 0;
  230. assert( p->in >= p->procSmpCnt + 1 );
  231. cmVOS_Hann( p->wnd, p->wn+1 );
  232. return rc;
  233. }
  234. #define _isInNoGoZone(p,i) ( ((0 <= (i)) && ((i) <= (p)->ei0)) || (((p)->ii <= (i)) && ((i) <= (p)->ei1)) )
  235. #define _isNotInNoGoZone(p,i) (!_isInNoGoZone(p,i))
  236. void _startNewWindow( cmPitchShift* p, cmPitchShiftOsc_t* op, int li, double ratio)
  237. {
  238. op->wi = 0;
  239. if( op->ratio < 1.0 )
  240. op->xi = p->ii - p->procSmpCnt;
  241. else
  242. op->xi = p->ii + (li)*(p->procSmpCnt/ratio);
  243. while( op->xi < 0 )
  244. op->xi += p->in-1;
  245. while( op->xi > (p->in-1) )
  246. op->xi -= (p->in-1);
  247. op->xi = floor(op->xi);
  248. }
  249. void _cmPitchShiftOsc( cmPitchShift* p, cmPitchShiftOsc_t* op, double ratio )
  250. {
  251. int li = 0;
  252. bool contFl = 1;
  253. // if we are waiting to start a new window until the output buffer drains
  254. if( op->wi==-1 )
  255. {
  256. // if the output buffer has enough samples to complete this cycle - return
  257. if(op->ynn >= p->procSmpCnt)
  258. return;
  259. // otherwise start a new window
  260. _startNewWindow(p,op,li,ratio);
  261. }
  262. // iterate until all output samples have been generated
  263. for(li=0; contFl; ++li)
  264. {
  265. // iterate over one window or until output is complete
  266. while( op->wi < p->wn )
  267. {
  268. int wii = floor(op->wi);
  269. double frac = op->wi - wii;
  270. int xii = (op->xi + wii) % (p->in-1);
  271. cmSample_t wnd = p->wnd[ wii ] + frac * (p->wnd[wii+1] - p->wnd[wii]);
  272. cmSample_t val;
  273. // if enough samples have been generated and the next input will not be in the no-go zone
  274. if( (op->ynn >= p->procSmpCnt) && _isNotInNoGoZone(p,xii-1) && _isNotInNoGoZone(p,xii+2) )
  275. {
  276. contFl = false;
  277. break;
  278. }
  279. if( op->ynn >= op->yN )
  280. {
  281. printf("OVER\n");
  282. contFl = false;
  283. break;
  284. }
  285. // generate the value of the output sample
  286. if( p->cubeFl )
  287. val = _cube_interp(frac,p->x[xii-1],p->x[xii],p->x[xii+1],p->x[xii+2]);
  288. else
  289. val = p->x[ xii ] + frac * (p->x[ xii+1 ] - p->x[xii]);
  290. // apply the window function and assign to output
  291. op->y[op->yii] = wnd * val;
  292. // incr the count of samples in y[]
  293. ++op->ynn;
  294. // advance the output buffers input location
  295. op->yii = (op->yii + 1) % op->yN;
  296. op->wi += ratio; // increment the window location
  297. }
  298. // if a window completed
  299. if( op->wi >= p->wn )
  300. {
  301. if( (ratio < 1.0) && (op->ynn >= p->procSmpCnt) )
  302. {
  303. op->wi = -1;
  304. break;
  305. }
  306. _startNewWindow(p,op,li+1,ratio);
  307. }
  308. }
  309. }
  310. // in=5
  311. // m x
  312. // 0 1 2 3 4 5
  313. // 1 x x a b c d e
  314. // 2 c d e f g h i
  315. // 3 g h i j k l m
  316. //
  317. // ratio < 1 - the output oscillators must be able to generate 1 complete window prior to new samples
  318. // overwriting the oscillators location in the input buffer.
  319. cmRC_t cmPitchShiftExec( cmPitchShift* p, const cmSample_t* x, cmSample_t* y, unsigned xn, double ratio )
  320. {
  321. cmRC_t rc = cmOkRC;
  322. //memcpy(y,x,xn*sizeof(cmSample_t));
  323. //return rc;
  324. int i;
  325. // copy in the incoming samples
  326. for(i=0; i<xn; ++i)
  327. {
  328. p->x[p->ii] = x[i];
  329. ++p->ii;
  330. if( p->ii == p->in+1 )
  331. {
  332. p->x[-1] = p->x[p->in-2];
  333. p->x[ 0] = p->x[p->in-1];
  334. p->x[ 1] = p->x[p->in];
  335. p->ii = 2;
  336. }
  337. }
  338. // locate the section of the input buffer which
  339. // will be written over on the next cycle.
  340. // The oscillators have to avoid ending in this area
  341. if( (p->in+1) - p->ii >= p->procSmpCnt )
  342. {
  343. p->ei1 = p->ii + p->procSmpCnt - 1;
  344. p->ei0 = -1;
  345. }
  346. else
  347. {
  348. int n = (p->in+1) - p->ii;
  349. p->ei1 = (p->ii + n) - 1;
  350. p->ei0 = (p->procSmpCnt - n) - 1;
  351. }
  352. //memset(p->outV,0,p->procSmpCnt*sizeof(cmSample_t));
  353. _cmPitchShiftOsc(p, p->osc + 0, ratio );
  354. _cmPitchShiftOsc(p, p->osc + 1, ratio );
  355. // mix the indidual output of the two oscillators to form the final output
  356. if( p->osc[0].ynn < p->procSmpCnt || p->osc[1].ynn < p->procSmpCnt )
  357. printf("UNDER\n");
  358. for(i=0; i<p->procSmpCnt; ++i)
  359. {
  360. p->outV[i] = p->osc[0].y[p->osc[0].yoi] + p->osc[1].y[p->osc[1].yoi];
  361. p->osc[0].yoi = (p->osc[0].yoi + 1) % p->osc[0].yN;
  362. p->osc[1].yoi = (p->osc[1].yoi + 1) % p->osc[1].yN;
  363. }
  364. p->osc[0].ynn -= p->procSmpCnt;
  365. p->osc[1].ynn -= p->procSmpCnt;
  366. if( y != NULL )
  367. memcpy(y,p->outV,p->outN*sizeof(cmSample_t));
  368. return rc;
  369. }
  370. */
  371. cmRC_t cmPitchShiftInit( cmPitchShift* p, unsigned procSmpCnt, double srate)
  372. {
  373. cmRC_t rc;
  374. if((rc = cmPitchShiftFinal(p)) != cmOkRC )
  375. return rc;
  376. p->procSmpCnt = procSmpCnt;
  377. p->outN = procSmpCnt;
  378. p->outV = cmMemAllocZ(cmSample_t,procSmpCnt);
  379. p->wn = 2048;
  380. p->xn = 2*p->wn + procSmpCnt;
  381. p->bn = p->xn + 3;
  382. p->b = cmMemAllocZ(cmSample_t,p->bn);
  383. p->x = p->b + 1;
  384. p->wnd = cmMemAllocZ(cmSample_t,p->wn+1);
  385. p->xni = p->xn - procSmpCnt + 2;
  386. p->cubeFl = true;
  387. int i;
  388. for(i=0; i<2; ++i)
  389. {
  390. cmPitchShiftOsc_t* op = p->osc + i;
  391. op->xi = p->procSmpCnt;
  392. op->yn = p->wn * 2;
  393. op->y = cmMemAllocZ(cmSample_t,op->yn);
  394. op->yii= 0;
  395. op->yoi= 0;
  396. op->ynn= 0;
  397. op->wi = i==0 ? 0 : p->wn/2;
  398. }
  399. cmVOS_Hann(p->wnd,p->wn+1);
  400. return rc;
  401. }
  402. void _cmPitchShiftOscExec( cmPitchShift* p, cmPitchShiftOsc_t* op, double ratio )
  403. {
  404. int k = 0;
  405. // account for right buffer shift on input
  406. op->xi -= p->procSmpCnt;
  407. // pass through code for testing
  408. if(0)
  409. {
  410. int i;
  411. for(i=0; i<p->procSmpCnt; ++i)
  412. {
  413. op->y[op->yii] = p->x[ (int)floor(op->xi) ];
  414. op->yii = (op->yii + 1) % op->yn;
  415. op->ynn += 1;
  416. op->xi += 1.0;
  417. }
  418. return;
  419. }
  420. while(1)
  421. {
  422. int wii = floor(op->wi);
  423. double wfrac = op->wi - wii;
  424. int xii = floor(op->xi);
  425. double vfrac = op->xi - xii;
  426. cmSample_t val;
  427. // if enough output samples have been generated and we are outside the no-land zone
  428. if( (op->ynn >= p->procSmpCnt && op->xi >= p->procSmpCnt) )
  429. break;
  430. if( op->xi >= p->xn )
  431. {
  432. //printf("Wrap %f %f\n",op->xi,op->wi);
  433. }
  434. cmSample_t wnd = p->wnd[ wii ] + wfrac * (p->wnd[wii+1] - p->wnd[wii]);
  435. // generate the value of the output sample
  436. if( p->cubeFl )
  437. val = _cube_interp(vfrac,p->x[xii-1],p->x[xii],p->x[xii+1],p->x[xii+2]);
  438. else
  439. val = p->x[ xii ] + vfrac * (p->x[ xii+1 ] - p->x[xii]);
  440. // apply the window function and assign to output
  441. op->y[op->yii] = wnd * val;
  442. // incr the count of samples in y[]
  443. ++op->ynn;
  444. // advance the output buffers input location
  445. op->yii = (op->yii + 1) % op->yn;
  446. op->wi += ratio<1 ? 1 : ratio; // increment the window location
  447. op->xi += ratio;
  448. if( op->wi >= p->wn )
  449. {
  450. ++k;
  451. op->wi = 0;
  452. if( ratio < 1 )
  453. op->xi = p->xni; // begin of most recent block of procSmpCnt input samples
  454. else
  455. op->xi = k*p->procSmpCnt/ratio;
  456. }
  457. }
  458. //if( op->ynn != p->procSmpCnt )
  459. // printf("wi:%f xi:%f ynn:%i\n",op->wi,op->xi,op->ynn);
  460. }
  461. // b: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17
  462. // x: -1 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16
  463. //
  464. // On each cycle:
  465. // 1) shift x[] left by procSmpCnt samples.
  466. // 2) add new samples on right side of x[]
  467. // 3) oscillator scans from right to left.
  468. cmRC_t cmPitchShiftExec( cmPitchShift* p, const cmSample_t* x, cmSample_t* y, unsigned xn, double ratio, bool bypassFl )
  469. {
  470. if(0)
  471. {
  472. memcpy(p->outV,x,xn*sizeof(cmSample_t));
  473. if( y != NULL )
  474. memcpy(y,x,xn*sizeof(cmSample_t));
  475. return cmOkRC;
  476. }
  477. if( y == NULL )
  478. return cmOkRC;
  479. if( x == NULL )
  480. {
  481. cmVOS_Zero(y,xn);
  482. return cmOkRC;
  483. }
  484. if( bypassFl )
  485. {
  486. memcpy(p->outV,x,xn*sizeof(cmSample_t));
  487. if( y != NULL )
  488. memcpy(y,x,xn*sizeof(cmSample_t));
  489. return cmOkRC;
  490. }
  491. int i;
  492. memmove(p->x - 1, p->x+p->procSmpCnt-1,(p->xn - p->procSmpCnt + 3)*sizeof(cmSample_t));
  493. memcpy( p->x + p->xni, x, xn * sizeof(cmSample_t) );
  494. //memmove(p->x + p->procSmpCnt -1, p->x - 1, (p->xn - p->procSmpCnt + 3) * sizeof(cmSample_t));
  495. //memcpy( p->x-1, x, xn * sizeof(cmSample_t));
  496. _cmPitchShiftOscExec(p, p->osc + 0, ratio );
  497. _cmPitchShiftOscExec(p, p->osc + 1, ratio );
  498. // mix the indidual output of the two oscillators to form the final output
  499. if( p->osc[0].ynn < p->procSmpCnt || p->osc[1].ynn < p->procSmpCnt )
  500. printf("UNDER\n");
  501. for(i=0; i<p->procSmpCnt; ++i)
  502. {
  503. p->outV[i] = p->osc[0].y[p->osc[0].yoi] + p->osc[1].y[p->osc[1].yoi];
  504. p->osc[0].yoi = (p->osc[0].yoi + 1) % p->osc[0].yn;
  505. p->osc[1].yoi = (p->osc[1].yoi + 1) % p->osc[1].yn;
  506. }
  507. p->osc[0].ynn -= p->procSmpCnt;
  508. p->osc[1].ynn -= p->procSmpCnt;
  509. if( y != NULL )
  510. memcpy(y,p->outV,xn*sizeof(cmSample_t));
  511. return cmOkRC;
  512. }
  513. //=======================================================================================================================
  514. cmLoopRecord* cmLoopRecordAlloc(
  515. cmCtx* c, cmLoopRecord* p, unsigned procSmpCnt, unsigned maxRecdSmpCnt, unsigned xfadeSmpCnt )
  516. {
  517. cmLoopRecord* op = cmObjAlloc(cmLoopRecord, c, p );
  518. if( procSmpCnt != 0 )
  519. if( cmLoopRecordInit(op, procSmpCnt, maxRecdSmpCnt, xfadeSmpCnt) != cmOkRC )
  520. cmLoopRecordFree(&op);
  521. return op;
  522. }
  523. /*
  524. cmRC_t cmLoopRecordFree( cmLoopRecord** pp )
  525. {
  526. cmRC_t rc = cmOkRC;
  527. if( pp != NULL && *pp != NULL )
  528. {
  529. cmLoopRecord* p = *pp;
  530. if(( rc = cmLoopRecordFinal(p)) == cmOkRC )
  531. {
  532. cmMemPtrFree(&p->bufMem);
  533. cmMemPtrFree(&p->bufArray);
  534. cmMemPtrFree(&p->outV);
  535. cmMemPtrFree(&p->xfadeFunc);
  536. cmObjFree(pp);
  537. }
  538. }
  539. return rc;
  540. }
  541. cmRC_t cmLoopRecordInit( cmLoopRecord* p, unsigned procSmpCnt, unsigned maxRecdSmpCnt, unsigned xfadeSmpCnt )
  542. {
  543. cmRC_t rc;
  544. unsigned i;
  545. if((rc = cmLoopRecordFinal(p)) != cmOkRC )
  546. return rc;
  547. assert( xfadeSmpCnt < maxRecdSmpCnt );
  548. p->maxRecdSmpCnt = maxRecdSmpCnt;
  549. p->bufArrayCnt = 2;
  550. p->bufMem = cmMemResizeZ( cmSample_t, p->bufMem, maxRecdSmpCnt * p->bufArrayCnt );
  551. p->bufArray = cmMemResizeZ( cmLoopRecdBuf, p->bufArray, p->bufArrayCnt );
  552. p->outV = cmMemResizeZ( cmSample_t, p->outV, procSmpCnt );
  553. p->xfadeFunc = cmMemResizeZ( cmSample_t, p->xfadeFunc, xfadeSmpCnt+1 );
  554. p->outN = procSmpCnt;
  555. p->procSmpCnt = procSmpCnt;
  556. p->xfadeSmpCnt = xfadeSmpCnt;
  557. p->recdBufIdx = 0;
  558. p->playBufIdx = 1;
  559. p->recdFl = false;
  560. p->playFl = false;
  561. for(i=0; i<p->bufArrayCnt; ++i)
  562. {
  563. p->bufArray[i].bV = p->bufMem + (i * maxRecdSmpCnt);
  564. p->bufArray[i].bN = maxRecdSmpCnt;
  565. p->bufArray[i].xfi = xfadeSmpCnt;
  566. }
  567. for(i=0; i<=p->xfadeSmpCnt; ++i)
  568. p->xfadeFunc[i] = (cmReal_t)i/p->xfadeSmpCnt;
  569. return rc;
  570. }
  571. cmRC_t cmLoopRecordFinal( cmLoopRecord* p )
  572. { return cmOkRC; }
  573. cmRC_t cmLoopRecordExec( cmLoopRecord* p, const cmSample_t* x, cmSample_t* y, unsigned xn, bool recdFl, bool playFl )
  574. {
  575. unsigned i;
  576. // if the recdFl was enabled
  577. if(recdFl==true && p->recdFl==false)
  578. {
  579. p->recdFl = true;
  580. p->bufArray[p->recdBufIdx].bii = 0;
  581. p->bufArray[p->recdBufIdx].boi = 0;
  582. printf("Recd:on %i\n",p->recdBufIdx);
  583. }
  584. else
  585. // if the recdFl was disabled
  586. if( recdFl==false && p->recdFl==true )
  587. {
  588. p->recdFl = false;
  589. playFl = true;
  590. p->playBufIdx = p->recdBufIdx;
  591. p->recdBufIdx = (p->recdBufIdx + 1) % p->bufArrayCnt;
  592. printf("Recd:off\n");
  593. }
  594. // if the playFl triggered on
  595. if( playFl==true && p->playFl==false)
  596. {
  597. p->bufArray[p->playBufIdx].boi = 0;
  598. p->playFl = true;
  599. //printf("Play:on %i %i\n",p->playBufIdx,p->bufArray[p->playBufIdx].bii);
  600. }
  601. // if recording
  602. if( p->recdFl )
  603. {
  604. cmLoopRecdBuf* rp = p->bufArray + p->recdBufIdx;
  605. for(i=0; i<xn && rp->bii < rp->bN; ++i,++rp->bii)
  606. {
  607. rp->bV[ rp->bii ] = x[i];
  608. }
  609. p->recdFl = rp->bii != rp->bN;
  610. }
  611. // if playing
  612. if( p->playFl )
  613. {
  614. cmLoopRecdBuf* rp = p->bufArray + p->playBufIdx;
  615. unsigned xf0n = p->xfadeSmpCnt/2;
  616. unsigned xf1n = rp->bii - p->xfadeSmpCnt/2;
  617. unsigned xfi0 = (rp->boi + p->xfadeSmpCnt/2) % rp->bii;
  618. for(i=0; i<xn; ++i)
  619. {
  620. bool fl = rp->boi < xf0n || rp->boi > xf1n;
  621. cmSample_t env = fl ? p->xfadeFunc[ rp->xfi ] : 1;
  622. p->outV[i] = ((1.0-env) * rp->bV[ rp->boi ]) + (env * rp->bV[ xfi0 ]);
  623. rp->boi = (rp->boi + 1) % rp->bii;
  624. xfi0 = (xfi0 + 1) % rp->bii;
  625. }
  626. }
  627. if( y != NULL )
  628. memcpy(y,p->outV,xn*sizeof(cmSample_t));
  629. return cmOkRC;
  630. }
  631. */
  632. cmRC_t cmLoopRecordFree( cmLoopRecord** pp )
  633. {
  634. cmRC_t rc = cmOkRC;
  635. if( pp != NULL && *pp != NULL )
  636. {
  637. cmLoopRecord* p = *pp;
  638. if(( rc = cmLoopRecordFinal(p)) == cmOkRC )
  639. {
  640. cmMemPtrFree(&p->bufMem);
  641. cmMemPtrFree(&p->bufArray);
  642. cmMemPtrFree(&p->outV);
  643. cmObjFree(pp);
  644. }
  645. }
  646. return rc;
  647. }
  648. cmRC_t cmLoopRecordInit( cmLoopRecord* p, unsigned procSmpCnt, unsigned maxRecdSmpCnt, unsigned xfadeSmpCnt )
  649. {
  650. cmRC_t rc;
  651. unsigned i;
  652. if((rc = cmLoopRecordFinal(p)) != cmOkRC )
  653. return rc;
  654. assert( xfadeSmpCnt < maxRecdSmpCnt );
  655. p->maxRecdSmpCnt = maxRecdSmpCnt;
  656. p->bufArrayCnt = 2;
  657. p->bufMem = cmMemResizeZ( cmSample_t, p->bufMem, (maxRecdSmpCnt+3) * p->bufArrayCnt * 2 );
  658. p->bufArray = cmMemResizeZ( cmLoopRecdBuf, p->bufArray, p->bufArrayCnt );
  659. p->outV = cmMemResizeZ( cmSample_t, p->outV, procSmpCnt );
  660. p->outN = procSmpCnt;
  661. p->procSmpCnt = procSmpCnt;
  662. p->xfadeSmpCnt = xfadeSmpCnt;
  663. p->recdBufIdx = 0;
  664. p->playBufIdx = 1;
  665. p->recdFl = false;
  666. p->playFl = false;
  667. for(i=0; i<p->bufArrayCnt; ++i)
  668. {
  669. p->bufArray[i].xV = p->bufMem + ((2 * i + 0) * (maxRecdSmpCnt+3)) + 1;
  670. p->bufArray[i].xN = maxRecdSmpCnt;
  671. p->bufArray[i].xii = 0;
  672. p->bufArray[i].wV = p->bufMem + ((2 * i + 1) * (maxRecdSmpCnt+3));
  673. }
  674. return rc;
  675. }
  676. cmRC_t cmLoopRecordFinal( cmLoopRecord* p )
  677. { return cmOkRC; }
  678. void _cmLoopRecordOscExec( cmLoopRecord* p, cmLoopRecdBuf* rp, cmLoopRecdOsc* op, double ratio )
  679. {
  680. unsigned i;
  681. for(i=0; i<p->outN; ++i)
  682. {
  683. int xi = floor(op->xi);
  684. double vfrac = op->xi - xi;
  685. cmSample_t val = _cube_interp(vfrac,rp->xV[xi-1],rp->xV[xi],rp->xV[xi+1],rp->xV[xi+2]);
  686. int wi = floor(op->wi);
  687. double wfrac = op->wi - wi;
  688. cmSample_t wnd = rp->wV[ wi ] + wfrac * (rp->wV[wi+1] - rp->wV[wi]);
  689. wnd = (wnd - 0.5) * op->u + 0.5;
  690. p->outV[i] += wnd * val;
  691. op->wi += ratio;
  692. if( op->wi >= rp->xii )
  693. {
  694. op->wi -= rp->xii;
  695. op->u *= -1.0;
  696. }
  697. op->xi += ratio;
  698. if( op->xi >= rp->xii )
  699. op->xi -= rp->xii;
  700. }
  701. }
  702. //
  703. // a b c d e f g h i - osc 0 sample value
  704. // 0.0 .25 0.5 .75 1.0 1.0 .75 0.5 .25 - osc 0 window value
  705. //
  706. // e f g h i a b c d - osc 1 sample value
  707. // 1.0 .75 0.5 .25 0.0 0.0 .25 0.5 .75 - osc 1 window value
  708. //
  709. // Notes:
  710. // 1) The window values transition through zero
  711. // at the loop point.
  712. // 2)
  713. cmRC_t cmLoopRecordExec( cmLoopRecord* p, const cmSample_t* x, cmSample_t* y, unsigned xn, bool bypassFl, bool recdFl, bool playFl, double ratio, double pgain, double rgain )
  714. {
  715. int i,j;
  716. assert( xn <= p->outN );
  717. if( bypassFl )
  718. {
  719. memcpy(p->outV,x,xn*sizeof(cmSample_t));
  720. if( y != NULL )
  721. {
  722. //memcpy(y,x,xn*sizeof(cmSample_t));
  723. cmVOS_MultVVS(y,xn,x,pgain);
  724. }
  725. return cmOkRC;
  726. }
  727. // if the recdFl was enabled
  728. if(recdFl==true && p->recdFl==false)
  729. {
  730. p->recdFl = true;
  731. p->bufArray[p->recdBufIdx].xii = 0;
  732. //printf("Recd:on %i\n",p->recdBufIdx);
  733. }
  734. else
  735. // if the recdFl was disabled
  736. if( recdFl==false && p->recdFl==true )
  737. {
  738. cmLoopRecdBuf* rp = p->bufArray + p->recdBufIdx;
  739. // arrange the 'wrap-around' samples
  740. rp->xV[-1] = rp->xV[rp->xii-1];
  741. rp->xV[rp->xii ] = rp->xV[0];
  742. rp->xV[rp->xii+1] = rp->xV[1];
  743. // calc the length of the cross-fade
  744. if( rp->xii < p->xfadeSmpCnt * 2)
  745. rp->xfN = rp->xii/2;
  746. else
  747. rp->xfN = p->xfadeSmpCnt;
  748. assert( rp->xfN > 0 );
  749. // fill the crossfade window with zeros
  750. cmVOS_Zero(rp->wV,rp->xii);
  751. // fill the last xfN samples in the xfade window vector with a fade-in slope
  752. int xfi = rp->xii - rp->xfN;
  753. for(j=0; j<rp->xfN; ++j)
  754. rp->wV[xfi+j] = (cmSample_t)j/rp->xfN;
  755. // initialize the oscillators
  756. for(j=0; j<2; ++j)
  757. {
  758. //
  759. rp->osc[j].xi = j==0 ? 0.0 : rp->xfN; // oscillators are p->xfN samples out of phase
  760. rp->osc[j].u = j==0 ? 1.0 : -1.0; // set windows to have opposite polarity
  761. rp->osc[j].wi = xfi; // begin window at cross-fade
  762. }
  763. p->recdFl = false;
  764. p->playFl = true;
  765. p->playBufIdx = p->recdBufIdx;
  766. p->recdBufIdx = (p->recdBufIdx + 1) % p->bufArrayCnt;
  767. //printf("Recd:off\n");
  768. }
  769. // if recording
  770. if( p->recdFl )
  771. {
  772. cmLoopRecdBuf* rp = p->bufArray + p->recdBufIdx;
  773. for(i=0; i<xn && rp->xii < rp->xN; ++i,++rp->xii)
  774. {
  775. rp->xV[ rp->xii ] = x[i];
  776. }
  777. }
  778. if( playFl && p->bufArray[p->playBufIdx].xii > 0 )
  779. {
  780. p->playFl = !p->playFl;
  781. if( p->playFl )
  782. {
  783. // reset oscillators to start at the begin of loop
  784. }
  785. }
  786. if( p->playFl )
  787. {
  788. cmLoopRecdBuf* rp = p->bufArray + p->playBufIdx;
  789. cmVOS_Zero(p->outV,p->outN);
  790. _cmLoopRecordOscExec(p,rp, rp->osc + 0, ratio );
  791. _cmLoopRecordOscExec(p,rp, rp->osc + 1, ratio );
  792. }
  793. if( y != NULL )
  794. {
  795. //memcpy(y,p->outV,xn*sizeof(cmSample_t));
  796. for(i=0; i<p->outN; ++i)
  797. y[i] = (pgain * x[i]) + (rgain * p->outV[i]);
  798. }
  799. return cmOkRC;
  800. }
  801. //=======================================================================================================================
  802. cmGateDetect* cmGateDetectAlloc( cmCtx* c, cmGateDetect* p, unsigned procSmpCnt, cmReal_t onThreshPct, cmReal_t onThreshDb, cmReal_t offThreshDb )
  803. {
  804. cmGateDetect* op = cmObjAlloc(cmGateDetect, c, p );
  805. if( procSmpCnt != 0 )
  806. if( cmGateDetectInit(op, procSmpCnt, onThreshPct, onThreshDb, offThreshDb) != cmOkRC )
  807. cmGateDetectFree(&op);
  808. return op;
  809. }
  810. cmRC_t cmGateDetectFree( cmGateDetect** pp )
  811. {
  812. cmRC_t rc;
  813. if( pp==NULL || *pp == NULL )
  814. return cmOkRC;
  815. cmGateDetect* p = *pp;
  816. if((rc = cmGateDetectFinal(p)) != cmOkRC )
  817. return rc;
  818. cmMemPtrFree(&p->rmsV);
  819. cmMemPtrFree(&p->wndV);
  820. cmObjFree(pp);
  821. return cmOkRC;
  822. }
  823. cmRC_t cmGateDetectInit( cmGateDetect* p, unsigned procSmpCnt, cmReal_t onThreshPct, cmReal_t onThreshDb, cmReal_t offThreshDb )
  824. {
  825. cmRC_t rc;
  826. if((rc = cmGateDetectFinal(p)) != cmOkRC )
  827. return rc;
  828. p->rmsN = 3;
  829. p->rmsV = cmMemResizeZ(cmSample_t,p->rmsV,p->rmsN);
  830. p->wndN = 9;
  831. p->wndV = cmMemResizeZ(cmSample_t,p->wndV,p->wndN);
  832. p->rms = 0;
  833. p->durSmpCnt = 0;
  834. p->gateFl = false;
  835. p->deltaFl = false;
  836. p->onThreshPct = onThreshPct;
  837. p->onThreshDb = onThreshDb;
  838. p->offThreshDb = offThreshDb;
  839. return rc;
  840. }
  841. cmRC_t cmGateDetectFinal(cmGateDetect* p )
  842. {
  843. return cmOkRC;
  844. }
  845. cmRC_t cmGateDetectExec22( cmGateDetect* p, const cmSample_t* x, unsigned xn )
  846. {
  847. p->deltaFl = false;
  848. p->rms = cmVOS_RMS(x,xn,xn);
  849. cmReal_t db = p->rms==0 ? -100 : 20*log10(p->rms);
  850. //cmSample_t b0 = 0;
  851. cmSample_t b1 = 0;
  852. cmVOS_Shift(p->rmsV,p->rmsN,-1,p->rms);
  853. // if gate is on
  854. if( p->gateFl )
  855. {
  856. p->deltaFl = db < p->offThreshDb;
  857. }
  858. else
  859. {
  860. //cmVOS_Lsq1(NULL,p->rmsV,p->rmsN,&b0,&b1);
  861. // p->deltaFl = b1 > p->onThreshPct && db > p->onThreshDb;
  862. unsigned i;
  863. for(i=0; i<p->rmsN-1; ++i)
  864. b1 += p->rmsV[i+1] - p->rmsV[i];
  865. b1 /= p->rmsN;
  866. p->deltaFl = b1 > p->onThreshPct && db > p->onThreshDb;
  867. }
  868. if( p->deltaFl )
  869. {
  870. p->gateFl = !p->gateFl;
  871. p->durSmpCnt = xn;
  872. }
  873. else
  874. {
  875. p->durSmpCnt += xn;
  876. }
  877. //p->rms = b1;
  878. //printf("%f %f %i %f;\n",db,p->rms,p->deltaFl,b1);
  879. return cmOkRC;
  880. }
  881. cmRC_t cmGateDetectExec( cmGateDetect* p, const cmSample_t* x, unsigned xn )
  882. {
  883. p->deltaFl = false;
  884. p->rms = cmVOS_RMS(x,xn,xn);
  885. cmReal_t db = p->rms==0 ? -100 : 20*log10(p->rms);
  886. unsigned i;
  887. cmSample_t d = 0;
  888. // update the rms window
  889. cmVOS_Shift(p->rmsV,p->rmsN,-1,p->rms);
  890. // calc. derr of RMS
  891. for(i=0; i<p->rmsN-1; ++i)
  892. d += p->rmsV[i+1] - p->rmsV[i];
  893. d /= p->rmsN;
  894. // zero negative derr's
  895. d = d<0 ? 0 : d;
  896. // update the peak window
  897. cmVOS_Shift(p->wndV,p->wndN,-1,d);
  898. p->mean = cmVOS_Mean(p->wndV,p->wndN);
  899. // if this is an offset
  900. if( p->gateFl && db < p->offThreshDb )
  901. {
  902. p->gateFl = false;
  903. p->deltaFl = true;
  904. }
  905. else
  906. // if this may be an onset.
  907. if( db > p->onThreshDb )
  908. {
  909. i = p->wndN - 2;
  910. // if previous derr was a peak
  911. if( p->wndV[i]>0 && p->wndV[i-1] < p->wndV[i] && p->wndV[i] > p->wndV[i+1] )
  912. {
  913. // and the peak is the largest value in the exteneded window
  914. if( p->wndV[i] > p->mean )
  915. {
  916. p->deltaFl = true;
  917. p->gateFl = true;
  918. p->durSmpCnt = 0;
  919. }
  920. }
  921. }
  922. if( p->gateFl )
  923. p->durSmpCnt += xn;
  924. p->rms = p->d0;
  925. p->d0 = d;
  926. return cmOkRC;
  927. }
  928. //=======================================================================================================================
  929. cmGateDetect2* cmGateDetectAlloc2( cmCtx* c, cmGateDetect2* p, unsigned procSmpCnt, const cmGateDetectParams* args )
  930. {
  931. cmGateDetect2* op = cmObjAlloc(cmGateDetect2, c, p );
  932. if( procSmpCnt != 0 )
  933. if( cmGateDetectInit2(op, procSmpCnt, args) != cmOkRC )
  934. cmGateDetectFree2(&op);
  935. return op;
  936. }
  937. cmRC_t cmGateDetectFree2( cmGateDetect2** pp )
  938. {
  939. cmRC_t rc;
  940. if( pp==NULL || *pp == NULL )
  941. return cmOkRC;
  942. cmGateDetect2* p = *pp;
  943. if((rc = cmGateDetectFinal2(p)) != cmOkRC )
  944. return rc;
  945. cmMemPtrFree(&p->medV);
  946. cmObjFree(pp);
  947. return cmOkRC;
  948. }
  949. cmRC_t cmGateDetectInit2( cmGateDetect2* p, unsigned procSmpCnt, const cmGateDetectParams* args )
  950. {
  951. cmRC_t rc;
  952. if((rc = cmGateDetectFinal2(p)) != cmOkRC )
  953. return rc;
  954. unsigned pkCnt = 3;
  955. unsigned eleCnt = 3*args->medCnt + args->avgCnt + args->suprCnt + args->offCnt + pkCnt;
  956. unsigned i;
  957. p->args = *args;
  958. p->medV = cmMemResizeZ(cmSample_t,p->medV,eleCnt);
  959. p->fcofV = p->medV + args->medCnt;
  960. p->fdlyV = p->fcofV + args->medCnt;
  961. p->avgV = p->fdlyV + args->medCnt;
  962. p->suprV = p->avgV + args->avgCnt;
  963. p->offV = p->suprV + args->suprCnt;
  964. p->pkV = p->offV + args->offCnt;
  965. assert( p->medV + eleCnt == p->pkV + pkCnt );
  966. p->medIdx = 0;
  967. p->avgIdx = 0;
  968. p->suprIdx = args->suprCnt;
  969. p->offIdx = 0;
  970. p->pkFl = false;
  971. p->gateFl = false;
  972. p->onFl = false;
  973. p->offFl = false;
  974. cmGateDetectSetOnThreshDb2(p,args->onThreshDb);
  975. cmGateDetectSetOnThreshDb2(p,args->offThreshDb);
  976. p->fcofV[0] = 1.0;
  977. for(i=1; i<args->medCnt; ++i)
  978. {
  979. p->fcofV[i] = (1.0 - 1.0/args->medCnt) / ( pow(2.0,i-1) );
  980. //printf("%i %f ",i,p->fcofV[i]);
  981. }
  982. for(i=0; i<args->suprCnt; ++i)
  983. {
  984. p->suprV[i] = 1.0/pow(args->suprCoeff,i + 1.0);
  985. //printf("%i %f ",i,p->suprV[i]);
  986. }
  987. return rc;
  988. }
  989. cmRC_t cmGateDetectFinal2(cmGateDetect2* p )
  990. { return cmOkRC; }
  991. void _cmGateDetectPickPeak( cmGateDetect2* p, cmSample_t input )
  992. {
  993. p->pkV[0] = p->pkV[1];
  994. p->pkV[1] = p->pkV[2];
  995. p->pkV[2] = input;
  996. // if supression is active - apply it to the center pkV[]
  997. if( p->suprIdx < p->args.suprCnt )
  998. {
  999. p->pkV[1] -= p->pkV[1] * p->suprV[p->suprIdx];
  1000. p->suprIdx = (p->suprIdx + 1) % p->args.suprCnt;
  1001. }
  1002. p->sup = p->pkV[1];
  1003. // if the center value in pkV[] is a local peak and above the onset threshold
  1004. if( p->pkV[1] > p->onThresh && p->pkV[0] < p->pkV[1] && p->pkV[1] > p->pkV[2] )
  1005. {
  1006. p->suprIdx = 0;
  1007. p->gateFl = true;
  1008. p->onFl = true;
  1009. }
  1010. // update the offset buffer
  1011. p->offV[p->offIdx] = fabs(p->pkV[2]);
  1012. p->offIdx = (p->offIdx + 1) % p->args.offCnt;
  1013. // if this is an offset
  1014. if( p->gateFl==true && cmVOS_Mean(p->offV,p->args.offCnt) < p->offThresh )
  1015. {
  1016. p->gateFl = false;
  1017. p->offFl = true;
  1018. }
  1019. }
  1020. void _cmGateDetectPickPeak2( cmGateDetect2* p, cmSample_t input )
  1021. {
  1022. // if supression is active - apply it to the input
  1023. if( p->suprIdx < p->args.suprCnt )
  1024. {
  1025. input -= input * p->suprV[p->suprIdx];
  1026. ++p->suprIdx;// = (p->suprIdx + 1) % p->args.suprCnt;
  1027. }
  1028. // update the peak buffer
  1029. p->pkV[0] = p->pkV[1];
  1030. p->pkV[1] = p->pkV[2];
  1031. p->pkV[2] = input;
  1032. p->sup = input;
  1033. // if the signal increased above the threshold and is not already waiting for a peak
  1034. if( p->pkV[1] < input && input > p->onThresh && p->pkFl == false )
  1035. {
  1036. p->pkFl = true;
  1037. p->gateFl = true;
  1038. p->onFl = true;
  1039. }
  1040. // if the center value in pkV[] is a local peak and above the onset threshold ...
  1041. if( p->gateFl && p->pkFl && p->pkV[0] < p->pkV[1] && p->pkV[1] > p->pkV[2] )
  1042. {
  1043. p->suprIdx = 0; // ... turn on supression
  1044. p->pkFl = false; // ... not longer waiting for the peak
  1045. }
  1046. // update the offset buffer
  1047. p->offV[p->offIdx] = fabs(input);
  1048. p->offIdx = (p->offIdx + 1) % p->args.offCnt;
  1049. // if this is an offset
  1050. if( p->gateFl==true && cmVOS_Mean(p->offV,p->args.offCnt) < p->offThresh )
  1051. {
  1052. p->gateFl = false;
  1053. p->offFl = true;
  1054. p->pkFl = false;
  1055. }
  1056. }
  1057. cmRC_t cmGateDetectExec2( cmGateDetect2* p, const cmSample_t* x, unsigned xn )
  1058. {
  1059. p->rms = cmVOS_RMS(x,xn,xn); // take RMS of incoming window
  1060. p->medV[ p->medIdx ] = p->rms; // input RMS to median filter
  1061. p->medIdx = (p->medIdx + 1) % p->args.medCnt;
  1062. p->med = cmVOS_Median(p->medV,p->args.medCnt); // calc the median
  1063. p->dif = cmMax(0,p->rms - p->med); // dif = half_rect( rms - med )
  1064. p->avgV[ p->avgIdx ] = p->dif; // input dif to avg filter
  1065. p->avgIdx = (p->avgIdx + 1) % p->args.avgCnt;
  1066. p->avg = cmVOS_Mean(p->avgV,p->args.avgCnt); // calc the avg
  1067. p->ons = p->dif - p->avg; // ons = dif - avg
  1068. cmVOS_Shift(p->fdlyV,p->args.medCnt,1,p->ons);
  1069. p->flt = cmVOS_MultSumVV(p->fdlyV,p->fcofV,p->args.medCnt);
  1070. p->offFl = false;
  1071. p->onFl = false;
  1072. _cmGateDetectPickPeak2(p,p->flt);
  1073. return cmOkRC;
  1074. }
  1075. void _cmGateDetectSetDb2( cmGateDetect2* p, cmReal_t db, cmReal_t* dbPtr )
  1076. {
  1077. *dbPtr = pow(10.0, db/20.0 );
  1078. }
  1079. void cmGateDetectSetOnThreshDb2( cmGateDetect2* p, cmReal_t db )
  1080. { _cmGateDetectSetDb2(p,db,&p->onThresh); }
  1081. void cmGateDetectSetOffThreshDb2( cmGateDetect2* p, cmReal_t db )
  1082. { _cmGateDetectSetDb2(p,db,&p->offThresh); }
  1083. //=======================================================================================================================
  1084. cmAutoGain* cmAutoGainAlloc( cmCtx* c, cmAutoGain* p, unsigned procSmpCnt, cmReal_t srate, cmReal_t hopMs, unsigned chCnt, const cmGateDetectParams* gd_args )
  1085. {
  1086. cmAutoGain* op = cmObjAlloc(cmAutoGain, c, p );
  1087. op->sbp = cmShiftBufAlloc(c,NULL,0,0,0);
  1088. op->gdp = cmGateDetectAlloc2(c,NULL,0,NULL);
  1089. if( procSmpCnt != 0 )
  1090. if( cmAutoGainInit(op, procSmpCnt, srate, hopMs, chCnt, gd_args) != cmOkRC )
  1091. cmAutoGainFree(&op);
  1092. return op;
  1093. }
  1094. cmRC_t cmAutoGainFree( cmAutoGain** pp )
  1095. {
  1096. cmRC_t rc;
  1097. if( pp==NULL || *pp == NULL )
  1098. return cmOkRC;
  1099. cmAutoGain* p = *pp;
  1100. if((rc = cmAutoGainFinal(p)) != cmOkRC )
  1101. return rc;
  1102. cmMemPtrFree(&p->chArray);
  1103. p->chCnt = 0;
  1104. cmShiftBufFree(&p->sbp);
  1105. cmGateDetectFree2(&p->gdp);
  1106. cmObjFree(pp);
  1107. return cmOkRC;
  1108. }
  1109. cmRC_t cmAutoGainInit( cmAutoGain* p, unsigned procSmpCnt, cmReal_t srate, cmReal_t hopMs, unsigned chCnt, const cmGateDetectParams* gd_args )
  1110. {
  1111. unsigned i;
  1112. cmRC_t rc;
  1113. if((rc = cmAutoGainFinal(p)) != cmOkRC )
  1114. return rc;
  1115. unsigned hopSmpCnt = (unsigned)floor(srate * hopMs / 1000.0);
  1116. unsigned wndSmpCnt = hopSmpCnt * gd_args->medCnt;
  1117. cmShiftBufInit(p->sbp,procSmpCnt,wndSmpCnt,hopSmpCnt);
  1118. cmGateDetectInit2(p->gdp,procSmpCnt,gd_args);
  1119. p->chCnt = chCnt;
  1120. p->chArray = cmMemResizeZ(cmAutoGainCh,p->chArray,p->chCnt);
  1121. p->chp = NULL;
  1122. for(i=0; i<p->chCnt; ++i)
  1123. p->chArray[i].id = cmInvalidIdx;
  1124. return rc;
  1125. }
  1126. cmRC_t cmAutoGainFinal( cmAutoGain* p )
  1127. { return cmOkRC;}
  1128. void _cmAutoGainChannelFinish( cmAutoGain* p )
  1129. {
  1130. if( p->chp != NULL )
  1131. {
  1132. if( p->gateMax != 0 )
  1133. {
  1134. p->gateSum += p->gateMax;
  1135. p->gateCnt += 1;
  1136. }
  1137. p->chp->gateMaxAvg = p->gateCnt == 0 ? 0.0 : p->gateSum / p->gateCnt;
  1138. }
  1139. }
  1140. cmRC_t cmAutoGainStartCh( cmAutoGain* p, unsigned id )
  1141. {
  1142. cmReal_t rmsMax = 1.0;
  1143. unsigned i;
  1144. if( id == cmInvalidIdx )
  1145. return cmOkRC;
  1146. // do intermediate channel calculations on the last selected channel
  1147. if( p->chp != NULL )
  1148. _cmAutoGainChannelFinish( p );
  1149. // if 'id' has already been used
  1150. // then select the associated channel as the current channel
  1151. // otherwise select the next avail channel as the current channel
  1152. for(i=0; i<p->chCnt; ++i)
  1153. if( p->chArray[i].id == id || p->chArray[i].id == cmInvalidId )
  1154. {
  1155. p->chp = p->chArray + i;
  1156. break;
  1157. }
  1158. if( p->chp == NULL )
  1159. return cmCtxRtCondition( &p->obj, cmArgAssertRC, "All channels are in use.");
  1160. p->chp->id = id;
  1161. p->gateCnt = 0;
  1162. p->gateSum = 0;
  1163. p->gateMax = 0;
  1164. p->minRms = rmsMax;
  1165. return cmOkRC;
  1166. }
  1167. // The goal of this process is to locate the max RMS value during
  1168. // the duration of each note. When all the notes have been processed for
  1169. // a given channel the average maximum value for all of the notes is
  1170. // then calculated.
  1171. cmRC_t cmAutoGainProcCh( cmAutoGain* p, const cmSample_t* x, unsigned xn )
  1172. {
  1173. // shift the new samples into the shift buffer
  1174. while(cmShiftBufExec(p->sbp,x,xn))
  1175. {
  1176. // update the gate detector
  1177. cmGateDetectExec2(p->gdp,p->sbp->outV,p->sbp->outN);
  1178. // write the output matrix file
  1179. //if( _cmPuWriteMtxFile(p,segFl) != kOkPuRC )
  1180. // goto errLabel;
  1181. // if this frame is an RMS minimum or onset or offset
  1182. // then select it as a possible segment end.
  1183. // Note that for onsets this will effectively force the end to
  1184. // come after the onset because the onset will not be an energy minimum
  1185. // relative to subsequent frames.
  1186. if( p->gdp->rms < p->minRms || p->gdp->onFl || p->gdp->offFl )
  1187. {
  1188. p->minRms = p->gdp->rms;
  1189. // count onsets
  1190. if( p->gdp->onFl )
  1191. ++p->chp->onCnt;
  1192. // count offsets
  1193. if( p->gdp->offFl )
  1194. {
  1195. ++p->chp->offCnt;
  1196. // update the gate sum and count
  1197. p->gateSum += p->gateMax;
  1198. p->gateCnt += 1;
  1199. p->gateMax = 0;
  1200. }
  1201. }
  1202. // track the max RMS value during this gate
  1203. if( p->gdp->gateFl && p->gdp->rms > p->gateMax )
  1204. p->gateMax = p->gdp->rms;
  1205. }
  1206. return cmOkRC;
  1207. }
  1208. cmRC_t cmAutoGainCalcGains( cmAutoGain* p )
  1209. {
  1210. unsigned i;
  1211. cmReal_t avg = 0;
  1212. if( p->chCnt == 0 )
  1213. return cmOkRC;
  1214. // verify that all channels were input
  1215. for(i=0; i<p->chCnt; ++i)
  1216. if( p->chArray[i].id == cmInvalidId )
  1217. break;
  1218. if( i != p->chCnt )
  1219. return cmCtxRtCondition( &p->obj, cmArgAssertRC, "All channels must be set prior to calculating gains.");
  1220. // process the last channel
  1221. _cmAutoGainChannelFinish(p);
  1222. // p->chp isn't used again unless we restart the indidual channel processing
  1223. p->chp = NULL;
  1224. for(i=0; i<p->chCnt; ++i)
  1225. avg += p->chArray[i].gateMaxAvg;
  1226. avg /= p->chCnt;
  1227. for(i=0; i<p->chCnt; ++i)
  1228. {
  1229. cmReal_t d = p->chArray[i].gateMaxAvg==0 ? 1.0 : p->chArray[i].gateMaxAvg;
  1230. p->chArray[i].gain = avg / d;
  1231. }
  1232. return cmOkRC;
  1233. }
  1234. void cmAutoGainPrint( cmAutoGain* p, cmRpt_t* rpt )
  1235. {
  1236. unsigned i=0;
  1237. for(i=0; i<p->chCnt; ++i)
  1238. {
  1239. cmAutoGainCh* chp = p->chArray + i;
  1240. cmRptPrintf(rpt,"midi:%3i %4s on:%i off:%i avg:%5.5f gain:%5.5f\n",
  1241. chp->id,
  1242. cmStringNullGuard(cmMidiToSciPitch(chp->id,NULL,0)),
  1243. chp->onCnt, chp->offCnt, chp->gateMaxAvg, chp->gain );
  1244. }
  1245. }
  1246. //==========================================================================================================================================
  1247. cmChCfg* cmChCfgAlloc( cmCtx* c, cmChCfg* p, cmCtx_t* ctx, const cmChar_t* fn )
  1248. {
  1249. cmChCfg* op = cmObjAlloc(cmChCfg, c, p );
  1250. if( fn != NULL )
  1251. if( cmChCfgInit( op, ctx, fn ) != cmOkRC )
  1252. cmChCfgFree(&op);
  1253. return op;
  1254. }
  1255. cmRC_t cmChCfgFree( cmChCfg** pp )
  1256. {
  1257. cmRC_t rc = cmOkRC;
  1258. if( pp == NULL || *pp == NULL )
  1259. return cmOkRC;
  1260. cmChCfg* p = *pp;
  1261. if((rc = cmChCfgFinal(p)) != cmOkRC )
  1262. return rc;
  1263. cmMemPtrFree(&p->chArray);
  1264. cmFsFreeFn(p->fn);
  1265. cmObjFree(pp);
  1266. return cmOkRC;
  1267. }
  1268. enum { kChColIdx, kSsiColIdx, kPitchColIdx, kMidiColIdx, kGainColIdx, kNsFlColIdx, kCdFlColIdx, kColCnt };
  1269. cmRC_t cmChCfgInit( cmChCfg* p, cmCtx_t* ctx, const cmChar_t* fn )
  1270. {
  1271. cmRC_t rc = cmOkRC;
  1272. unsigned i,j;
  1273. const cmChar_t* ch_array_label = "ch_array";
  1274. typedef struct
  1275. {
  1276. unsigned id;
  1277. const char* label;
  1278. } map_t;
  1279. map_t map[ kColCnt ] =
  1280. {
  1281. { kChColIdx, "ch" },
  1282. { kSsiColIdx, "ssi" },
  1283. { kPitchColIdx, "pitch" },
  1284. { kMidiColIdx, "midi" },
  1285. { kGainColIdx, "gain" },
  1286. { kNsFlColIdx, "ns_fl"},
  1287. { kCdFlColIdx, "cd_fl"}
  1288. };
  1289. if((rc = cmChCfgFinal(p)) != cmOkRC )
  1290. return rc;
  1291. p->fn = cmFsMakeFn(cmFsPrefsDir(),fn,NULL,NULL);
  1292. // read the channel cfg JSON file
  1293. if( cmJsonInitializeFromFile(&p->jsH, p->fn, ctx ) != kOkJsRC )
  1294. {
  1295. rc = cmCtxRtCondition( &p->obj, cmSubSysFailRC, "JSON initialize failed on '%s'.",cmStringNullGuard(p->fn));
  1296. goto errLabel;
  1297. }
  1298. // locate the 'ch_array' node
  1299. if((p->cap = cmJsonFindValue( p->jsH, ch_array_label, cmJsonRoot(p->jsH), kArrayTId )) == NULL )
  1300. {
  1301. rc = cmCtxRtCondition( &p->obj, cmSubSysFailRC, "Unable to locate the JSON element '%s'.",ch_array_label);
  1302. goto errLabel;
  1303. }
  1304. unsigned rowCnt = cmJsonChildCount(p->cap);
  1305. // there must be at least 1 label row and 1 ch row
  1306. if( rowCnt < 2 )
  1307. {
  1308. rc = cmCtxRtCondition( &p->obj, cmSubSysFailRC, "The 'ch_array' appears to be empty.");
  1309. goto errLabel;
  1310. }
  1311. // allocate the ch array
  1312. p->chCnt = rowCnt - 1;
  1313. p->chArray = cmMemResizeZ( cmChCfgCh, p->chArray, p->chCnt );
  1314. p->nsChCnt = 0;
  1315. // read each row
  1316. for(i=0; i<rowCnt; ++i)
  1317. {
  1318. // get the array on row i
  1319. const cmJsonNode_t* np = cmJsonArrayElementC(p->cap,i);
  1320. // all row arrays contain 'kColCnt' elements
  1321. if( cmJsonChildCount(np) != kColCnt )
  1322. {
  1323. rc = cmCtxRtCondition( &p->obj, cmSubSysFailRC, "All 'ch_array' element at index %i does not contain %i values.",i,kColCnt);
  1324. goto errLabel;
  1325. }
  1326. // for each columns
  1327. for(j=0; j<kColCnt; ++j)
  1328. {
  1329. const cmJsonNode_t* cp = cmJsonArrayElementC(np,j);
  1330. // the first row contains the labels ....
  1331. if( i == 0 )
  1332. {
  1333. // verify that the column format is as expected
  1334. if( cmJsonIsString(cp)==false || strcmp(cp->u.stringVal,map[j].label) != 0 )
  1335. {
  1336. rc = cmCtxRtCondition( &p->obj, cmSubSysFailRC, "The first row column at index %i of 'ch_array' should be the label '%s'.",map[j].label);
  1337. goto errLabel;
  1338. }
  1339. }
  1340. else // ... other rows contain Ch Cfg's.
  1341. {
  1342. cmChCfgCh* chp = p->chArray + (i-1);
  1343. switch(j)
  1344. {
  1345. case kChColIdx: rc = cmJsonUIntValue( cp, &chp->ch ); break;
  1346. case kSsiColIdx: rc = cmJsonUIntValue( cp, &chp->ssi ); break;
  1347. case kPitchColIdx: rc = cmJsonStringValue( cp, &chp->pitchStr ); break;
  1348. case kMidiColIdx: rc = cmJsonUIntValue( cp, &chp->midi ); break;
  1349. case kGainColIdx: rc = cmJsonRealValue( cp, &chp->gain ); break;
  1350. case kNsFlColIdx:
  1351. rc = cmJsonBoolValue( cp, &chp->nsFl );
  1352. if( chp->nsFl )
  1353. ++p->nsChCnt;
  1354. break;
  1355. case kCdFlColIdx: rc = cmJsonBoolValue( cp, &chp->cdFl ); break;
  1356. default:
  1357. { assert(0); }
  1358. }
  1359. if( rc != kOkJsRC )
  1360. {
  1361. rc = cmCtxRtCondition( &p->obj, cmSubSysFailRC, "An error occurred while reading the '%s' column on row index:%i .", map[j].label,i);
  1362. goto errLabel;
  1363. }
  1364. }
  1365. }
  1366. }
  1367. errLabel:
  1368. return rc;
  1369. }
  1370. cmRC_t cmChCfgFinal( cmChCfg* p )
  1371. {
  1372. cmRC_t rc = cmOkRC;
  1373. if( cmJsonIsValid(p->jsH) )
  1374. if( cmJsonFinalize(&p->jsH) != kOkJsRC )
  1375. rc = cmCtxRtCondition( &p->obj, cmSubSysFailRC, "JSON finalize failed.");
  1376. return rc;
  1377. }
  1378. cmRC_t cmChCfgWrite( cmChCfg* p )
  1379. {
  1380. cmRC_t rc = cmOkRC;
  1381. unsigned i;
  1382. for(i=0; i<p->chCnt; ++i)
  1383. {
  1384. // get the array on row i+1
  1385. cmJsonNode_t* np = cmJsonArrayElement(p->cap,i+1);
  1386. // get the ele in the 'gainColIdx' column
  1387. np = cmJsonArrayElement(np,kGainColIdx);
  1388. assert( cmJsonIsReal(np) );
  1389. np->u.realVal = p->chArray[i].gain;
  1390. }
  1391. if( cmJsonWrite( p->jsH, cmJsonRoot(p->jsH), p->fn ) != kOkJsRC )
  1392. rc = cmCtxRtCondition( &p->obj, cmSubSysFailRC, "The JSON Channel Cfg file write failed on '%s'.",cmStringNullGuard(p->fn));
  1393. return rc;
  1394. }
  1395. void cmChCfgPrint( cmChCfg* p, cmRpt_t* rpt )
  1396. {
  1397. unsigned i;
  1398. for(i=0; i<p->chCnt; ++i)
  1399. {
  1400. const cmChCfgCh* chp = p->chArray + i;
  1401. cmRptPrintf(rpt,"%3i %3i %4s %3i %5.5f\n",chp->ch,chp->ssi,chp->pitchStr,chp->midi,chp->gain);
  1402. }
  1403. }
  1404. unsigned cmChCfgChannelCount( cmCtx_t* ctx, const cmChar_t* fn, unsigned* nsChCntPtr )
  1405. {
  1406. cmChCfg* ccp;
  1407. unsigned chCnt = 0;
  1408. cmCtx* c = cmCtxAlloc(NULL, &ctx->rpt, cmLHeapNullHandle, cmSymTblNullHandle );
  1409. if((ccp = cmChCfgAlloc(c, NULL, ctx, fn )) == NULL )
  1410. goto errLabel;
  1411. chCnt = ccp->chCnt;
  1412. if( nsChCntPtr != NULL )
  1413. *nsChCntPtr = ccp->nsChCnt;
  1414. cmChCfgFree(&ccp);
  1415. errLabel:
  1416. cmCtxFree(&c);
  1417. return chCnt;
  1418. }
  1419. unsigned cmChCfgChannelIndex( cmCtx_t* ctx, const cmChar_t* fn, unsigned chIdx )
  1420. {
  1421. cmChCfg* ccp;
  1422. unsigned retChIdx = -1;
  1423. cmCtx* c = cmCtxAlloc(NULL, &ctx->rpt, cmLHeapNullHandle, cmSymTblNullHandle );
  1424. if((ccp = cmChCfgAlloc(c, NULL, ctx, fn )) == NULL )
  1425. goto errLabel;
  1426. if( chIdx >= ccp->chCnt )
  1427. cmCtxRtCondition( &ccp->obj, cmArgAssertRC, "The channel index %i is not less than the channel count %i when querying '%s'.",chIdx,ccp->chCnt,cmStringNullGuard(ccp->fn));
  1428. else
  1429. retChIdx = ccp->chArray[chIdx].ch;
  1430. cmChCfgFree(&ccp);
  1431. errLabel:
  1432. cmCtxFree(&c);
  1433. return retChIdx;
  1434. }
  1435. //==========================================================================================================================================
  1436. cmChordDetect* cmChordDetectAlloc( cmCtx*c, cmChordDetect* p, cmReal_t srate, unsigned chCnt, cmReal_t maxTimeSpanMs, unsigned minNotesPerChord )
  1437. {
  1438. cmChordDetect* op = cmObjAlloc(cmChordDetect, c, p );
  1439. if( srate != 0 )
  1440. if( cmChordDetectInit( op, srate, chCnt, maxTimeSpanMs, minNotesPerChord ) != cmOkRC )
  1441. cmChordDetectFree(&op);
  1442. return op;
  1443. }
  1444. cmRC_t cmChordDetectFree( cmChordDetect** pp )
  1445. {
  1446. cmRC_t rc = cmOkRC;
  1447. if( pp == NULL || *pp == NULL )
  1448. return cmOkRC;
  1449. cmChordDetect* p = *pp;
  1450. if((rc = cmChordDetectFinal(p)) != cmOkRC )
  1451. return rc;
  1452. cmMemPtrFree(&p->chArray);
  1453. cmObjFree(pp);
  1454. return cmOkRC;
  1455. }
  1456. cmRC_t cmChordDetectInit( cmChordDetect* p, cmReal_t srate, unsigned chCnt, cmReal_t maxTimeSpanMs, unsigned minNotesPerChord )
  1457. {
  1458. cmRC_t rc;
  1459. if((rc = cmChordDetectFinal(p)) != cmOkRC )
  1460. return rc;
  1461. p->chArray = cmMemResizeZ(cmChordDetectCh, p->chArray, chCnt );
  1462. p->chCnt = chCnt;
  1463. p->minNotesPerChord = minNotesPerChord;
  1464. p->detectFl = false;
  1465. p->timeSmp = 0;
  1466. p->srate = srate;
  1467. cmChordDetectSetSpanMs(p,maxTimeSpanMs);
  1468. unsigned i;
  1469. for(i=0; i<p->chCnt; ++i)
  1470. p->chArray[i].readyFl = true;
  1471. return rc;
  1472. }
  1473. cmRC_t cmChordDetectFinal( cmChordDetect* P )
  1474. { return cmOkRC; }
  1475. cmRC_t cmChordDetectExec( cmChordDetect* p, unsigned procSmpCnt, const bool* gateV, const cmReal_t* rmsV, unsigned chCnt )
  1476. {
  1477. assert( chCnt == p->chCnt );
  1478. chCnt = cmMin(chCnt,p->chCnt);
  1479. cmRC_t rc = cmOkRC;
  1480. unsigned candCnt = 0;
  1481. cmChordDetectCh* oldCand = NULL;
  1482. unsigned i;
  1483. p->detectFl = false;
  1484. p->timeSmp += procSmpCnt;
  1485. // update the readyFl's and candFl's based on onset and offsets
  1486. for(i=0; i<chCnt; ++i)
  1487. {
  1488. cmChordDetectCh* cp = p->chArray + i;
  1489. // if onset and the channel is ready
  1490. if( gateV[i] && cp->gateFl==false && cp->readyFl )
  1491. {
  1492. cp->candSmpTime = p->timeSmp;
  1493. cp->candRMS = rmsV[i];
  1494. cp->candFl = true;
  1495. }
  1496. // if offset then this channel is again ready to be a candidate
  1497. if( gateV[i]==false && cp->gateFl )
  1498. {
  1499. cp->readyFl = true;
  1500. }
  1501. cp->gateFl = gateV[i];
  1502. cp->chordFl = false;
  1503. }
  1504. // check if we have enough channels to form a chord and
  1505. // expire channels that have passed the max time out value
  1506. for(i=0; i<chCnt; ++i)
  1507. {
  1508. cmChordDetectCh* cp = p->chArray + i;
  1509. unsigned ageSmpCnt = p->timeSmp - cp->candSmpTime;
  1510. // if this is a candiate ...
  1511. if( cp->candFl )
  1512. {
  1513. // ... that has not expired
  1514. if( ageSmpCnt <= p->maxTimeSpanSmpCnt )
  1515. {
  1516. // track the oldest candidate
  1517. if( oldCand==NULL || cp->candSmpTime < oldCand->candSmpTime )
  1518. oldCand = cp;
  1519. ++candCnt;
  1520. }
  1521. else // ... that has expired
  1522. {
  1523. cp->candFl = false;
  1524. }
  1525. }
  1526. }
  1527. // If we have enough notes and the oldest candiate will expire on the next cycle.
  1528. // (By waiting for the oldest note to almost expire we gather the maximum number of
  1529. // possible notes given the time duration. If we remove this condition then
  1530. // we will tend to trigger chord detection with fewer notes.)
  1531. if( oldCand != NULL )
  1532. {
  1533. unsigned nextAge = p->timeSmp + procSmpCnt - oldCand->candSmpTime;
  1534. if( candCnt >= p->minNotesPerChord && nextAge >= p->maxTimeSpanSmpCnt )
  1535. {
  1536. for(i=0; i<chCnt; ++i)
  1537. {
  1538. cmChordDetectCh* cp = p->chArray + i;
  1539. if( cp->candFl )
  1540. {
  1541. cp->chordFl = true;
  1542. cp->candFl = false;
  1543. }
  1544. }
  1545. p->detectFl = true;
  1546. }
  1547. }
  1548. return rc;
  1549. }
  1550. cmRC_t cmChordDetectSetSpanMs( cmChordDetect* p, cmReal_t maxTimeSpanMs )
  1551. {
  1552. p->maxTimeSpanSmpCnt = floor( maxTimeSpanMs * p->srate / 1000.0 );
  1553. return cmOkRC;
  1554. }
  1555. //==========================================================================================================================================
  1556. cmXfader* cmXfaderAlloc( cmCtx*c, cmXfader* p, cmReal_t srate, unsigned chCnt, cmReal_t fadeTimeMs )
  1557. {
  1558. cmXfader* op = cmObjAlloc(cmXfader, c, p );
  1559. if( srate != 0 )
  1560. if( cmXfaderInit( op, srate, chCnt, fadeTimeMs ) != cmOkRC )
  1561. cmXfaderFree(&op);
  1562. return op;
  1563. }
  1564. cmRC_t cmXfaderFree( cmXfader** pp )
  1565. {
  1566. cmRC_t rc = cmOkRC;
  1567. if( pp == NULL || *pp == NULL )
  1568. return cmOkRC;
  1569. cmXfader* p = *pp;
  1570. if((rc = cmXfaderFinal(p)) != cmOkRC )
  1571. return rc;
  1572. cmMemPtrFree(&p->chArray);
  1573. cmObjFree(pp);
  1574. return cmOkRC;
  1575. }
  1576. cmRC_t cmXfaderInit( cmXfader* p, cmReal_t srate, unsigned chCnt, cmReal_t fadeTimeMs )
  1577. {
  1578. cmRC_t rc;
  1579. if((rc = cmXfaderFinal(p)) != cmOkRC )
  1580. return rc;
  1581. p->chCnt = chCnt;
  1582. p->chArray = cmMemResizeZ(cmXfaderCh,p->chArray,p->chCnt);
  1583. p->srate = srate;
  1584. p->gateFl = false;
  1585. p->onFl = false;
  1586. p->offFl = false;
  1587. cmXfaderSetXfadeTime(p,fadeTimeMs);
  1588. return rc;
  1589. }
  1590. cmRC_t cmXfaderFinal( cmXfader* p )
  1591. { return cmOkRC; }
  1592. cmRC_t cmXfaderExec( cmXfader* p, unsigned procSmpCnt, const bool* chGateV, unsigned chCnt )
  1593. {
  1594. assert( chCnt == p->chCnt);
  1595. chCnt = cmMin(chCnt,p->chCnt);
  1596. bool gateFl = false;
  1597. // gain change associated with procSmpCnt
  1598. //cmReal_t dgain = (cmReal_t)procSmpCnt / p->fadeSmpCnt;
  1599. cmReal_t i_dgain = (cmReal_t)procSmpCnt / p->fadeInSmpCnt;
  1600. cmReal_t o_dgain = (cmReal_t)procSmpCnt / p->fadeOutSmpCnt;
  1601. unsigned i;
  1602. // for each channel
  1603. for(i=0; i<chCnt; ++i)
  1604. {
  1605. cmXfaderCh* cp = p->chArray + i;
  1606. cp->onFl = false;
  1607. cp->offFl = false;
  1608. if( chGateV != NULL )
  1609. {
  1610. cp->onFl = chGateV[i] && !cp->gateFl; // notice fade-in transition begin
  1611. cp->gateFl = chGateV[i];
  1612. }
  1613. cmReal_t g = cp->gain;
  1614. if( cp->gateFl )
  1615. {
  1616. cp->gain = cmMin(cp->gain + i_dgain,1.0);
  1617. cp->ep_gain = sqrt(0.5 - 0.5 * cos(3.14159*cp->gain));
  1618. }
  1619. else
  1620. {
  1621. cp->gain = cmMax(cp->gain - o_dgain,0.0);
  1622. cp->ep_gain = sqrt(0.5 - 0.5 * cos(3.14159*cp->gain));
  1623. cp->offFl = g>0.0 && cp->gain==0.0; // notice fade-out transition end
  1624. }
  1625. if( cp->gain != 0.0 )
  1626. gateFl = true;
  1627. }
  1628. p->onFl = false;
  1629. p->offFl = false;
  1630. if( p->gateFl==false && gateFl==true )
  1631. p->onFl = true;
  1632. else
  1633. if( p->gateFl==true && gateFl==false )
  1634. p->offFl = true;
  1635. p->gateFl = gateFl;
  1636. return cmOkRC;
  1637. }
  1638. cmRC_t cmXfaderExecAudio( cmXfader* p, unsigned procSmpCnt, const bool* gateV, unsigned chCnt, const cmSample_t* x[], cmSample_t* y )
  1639. {
  1640. cmRC_t rc;
  1641. assert( chCnt == p->chCnt);
  1642. chCnt = cmMin(chCnt,p->chCnt);
  1643. if((rc = cmXfaderExec(p,procSmpCnt,gateV,chCnt)) != cmOkRC )
  1644. return rc;
  1645. unsigned i;
  1646. for(i=0; i<chCnt; ++i)
  1647. if( x[i] != NULL )
  1648. cmVOS_MultVaVS(y,procSmpCnt,x[i],p->chArray[i].ep_gain);
  1649. return rc;
  1650. }
  1651. void cmXfaderSetXfadeTime( cmXfader* p, cmReal_t fadeTimeMs )
  1652. {
  1653. p->fadeSmpCnt = floor(fadeTimeMs * p->srate /1000.0);
  1654. p->fadeInSmpCnt = p->fadeSmpCnt;
  1655. p->fadeOutSmpCnt = p->fadeSmpCnt;
  1656. }
  1657. void cmXfaderSetXfadeInTime( cmXfader* p, cmReal_t fadeTimeMs )
  1658. {
  1659. p->fadeInSmpCnt = floor(fadeTimeMs * p->srate /1000.0);
  1660. }
  1661. void cmXfaderSetXfadeOutTime( cmXfader* p, cmReal_t fadeTimeMs )
  1662. {
  1663. p->fadeOutSmpCnt = floor(fadeTimeMs * p->srate /1000.0);
  1664. }
  1665. void cmXfaderSelectOne( cmXfader* p, unsigned chIdx )
  1666. {
  1667. unsigned i = 0;
  1668. for(i=0; i<p->chCnt; ++i)
  1669. p->chArray[i].gateFl = i == chIdx;
  1670. }
  1671. void cmXfaderAllOff( cmXfader* p )
  1672. {
  1673. unsigned i = 0;
  1674. for(i=0; i<p->chCnt; ++i)
  1675. p->chArray[i].gateFl = false;
  1676. }
  1677. void cmXfaderJumpToDestinationGain( cmXfader* p )
  1678. {
  1679. unsigned i = 0;
  1680. for(i=0; i<p->chCnt; ++i)
  1681. {
  1682. if( p->chArray[i].gateFl )
  1683. p->chArray[i].gain = 1.0;
  1684. else
  1685. p->chArray[i].gain = 0.0;
  1686. p->chArray[i].onFl = false;
  1687. p->chArray[i].offFl = false;
  1688. p->chArray[i].ep_gain = p->chArray[i].gain;
  1689. }
  1690. }
  1691. //==========================================================================================================================================
  1692. cmFader* cmFaderAlloc( cmCtx*c, cmFader* p, cmReal_t srate, cmReal_t fadeTimeMs )
  1693. {
  1694. cmFader* op = cmObjAlloc(cmFader, c, p );
  1695. if( srate != 0 )
  1696. if( cmFaderInit( op, srate, fadeTimeMs ) != cmOkRC )
  1697. cmFaderFree(&op);
  1698. return op;
  1699. }
  1700. cmRC_t cmFaderFree( cmFader** pp )
  1701. {
  1702. cmRC_t rc = cmOkRC;
  1703. if( pp == NULL || *pp == NULL )
  1704. return cmOkRC;
  1705. cmFader* p = *pp;
  1706. if((rc = cmFaderFinal(p)) != cmOkRC )
  1707. return rc;
  1708. cmObjFree(pp);
  1709. return cmOkRC;
  1710. }
  1711. cmRC_t cmFaderInit( cmFader* p, cmReal_t srate, cmReal_t fadeTimeMs )
  1712. {
  1713. cmRC_t rc;
  1714. if((rc = cmFaderFinal(p)) != cmOkRC )
  1715. return rc;
  1716. p->srate = srate;
  1717. p->gain = 0.0;
  1718. cmFaderSetFadeTime(p,fadeTimeMs);
  1719. return rc;
  1720. }
  1721. cmRC_t cmFaderFinal( cmFader* p )
  1722. { return cmOkRC; }
  1723. cmRC_t cmFaderExec( cmFader* p, unsigned procSmpCnt, bool gateFl, bool mixFl, const cmSample_t* x, cmSample_t* y )
  1724. {
  1725. cmReal_t d = (gateFl ? 1.0 : -1.0) * procSmpCnt / p->fadeSmpCnt;
  1726. // TODO: add fade curves
  1727. p->gain = cmMin(1.0,cmMax(0.0,p->gain + d));
  1728. if( x!=NULL && y!=NULL )
  1729. {
  1730. if( mixFl )
  1731. cmVOS_MultVaVS(y, procSmpCnt, x, 1.0/* p->gain*/);
  1732. else
  1733. cmVOS_MultVVS(y,procSmpCnt,x,p->gain);
  1734. }
  1735. return cmOkRC;
  1736. }
  1737. void cmFaderSetFadeTime( cmFader* p, cmReal_t fadeTimeMs )
  1738. {
  1739. p->fadeSmpCnt = (unsigned)floor( fadeTimeMs * p->srate / 1000.0 );
  1740. }
  1741. //=======================================================================================================================
  1742. cmCombFilt* cmCombFiltAlloc( cmCtx* c, cmCombFilt* p, cmReal_t srate, bool feedbackFl, cmReal_t minHz, cmReal_t alpha, cmReal_t hz, bool bypassFl )
  1743. {
  1744. cmCombFilt* op = cmObjAlloc(cmCombFilt, c, p );
  1745. op->idp = cmIDelayAlloc(c, NULL, 0, 0, NULL, NULL, NULL, 0 );
  1746. if( srate != 0 )
  1747. if( cmCombFiltInit( op, srate, feedbackFl, minHz, alpha, hz, bypassFl ) != cmOkRC )
  1748. cmCombFiltFree(&op);
  1749. return op;
  1750. }
  1751. cmRC_t cmCombFiltFree( cmCombFilt** pp)
  1752. {
  1753. cmRC_t rc = cmOkRC;
  1754. if( pp == NULL || *pp == NULL )
  1755. return cmOkRC;
  1756. cmCombFilt* p = *pp;
  1757. if((rc = cmCombFiltFinal(p)) != cmOkRC )
  1758. return rc;
  1759. cmMemFree(p->a);
  1760. cmMemFree(p->b);
  1761. cmMemFree(p->d);
  1762. cmIDelayFree(&p->idp);
  1763. cmObjFree(pp);
  1764. return cmOkRC;
  1765. }
  1766. cmRC_t cmCombFiltInit( cmCombFilt* p, cmReal_t srate, bool feedbackFl, cmReal_t minHz, cmReal_t alpha, cmReal_t hz, bool bypassFl )
  1767. {
  1768. cmRC_t rc;
  1769. if((rc = cmCombFiltFinal(p)) != cmOkRC )
  1770. return rc;
  1771. p->feedbackFl = feedbackFl;
  1772. p->dN = cmMax(1,floor(srate / minHz ));
  1773. p->d = cmMemResizeZ( cmReal_t, p->d, p->dN+1 );
  1774. p->a = cmMemResizeZ( cmReal_t, p->a, p->dN );
  1775. p->b = cmMemResizeZ( cmReal_t, p->b, p->dN );
  1776. p->dn = 1;
  1777. p->alpha = alpha;
  1778. p->srate = srate;
  1779. p->bypassFl = bypassFl;
  1780. cmReal_t tapMs = p->dn * 1000.0 / srate;
  1781. cmReal_t tapFf = 1.0;
  1782. cmReal_t tapFb = 1.0;
  1783. cmIDelayInit(p->idp, srate, p->dN * 1000.0 / srate, &tapMs, &tapFf, &tapFb, 1 );
  1784. cmCombFiltSetHz(p,hz);
  1785. return rc;
  1786. }
  1787. cmRC_t cmCombFiltFinal( cmCombFilt* p )
  1788. { return cmOkRC; }
  1789. cmRC_t cmCombFiltExec( cmCombFilt* p, const cmSample_t* x, cmSample_t* y, unsigned n )
  1790. {
  1791. if( y == NULL )
  1792. return cmOkRC;
  1793. if( x == NULL )
  1794. {
  1795. cmVOS_Zero(y,n);
  1796. return cmOkRC;
  1797. }
  1798. if( p->bypassFl )
  1799. cmVOS_Copy(y,n,x);
  1800. else
  1801. cmIDelayExec(p->idp,x,y,n);
  1802. return cmOkRC;
  1803. }
  1804. void cmCombFiltSetAlpha( cmCombFilt* p, cmReal_t alpha )
  1805. {
  1806. p->b0 = 1.0 - fabs(alpha); // normalization coeff (could be applied directly to the input or output)
  1807. p->a[p->dn-1] = -alpha;
  1808. p->alpha = alpha;
  1809. p->idp->tff[0] = p->b0;
  1810. p->idp->tfb[0] = -alpha;
  1811. }
  1812. cmRC_t cmCombFiltSetHz( cmCombFilt* p, cmReal_t hz )
  1813. {
  1814. if( hz < p->minHz )
  1815. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The comb filter frequency %f Hz is invalid given the minimum frequency setting of %f Hz.",hz,p->minHz );
  1816. // clear the current alpha
  1817. p->a[p->dn-1] = 0;
  1818. // change location of alpha in p->a[]
  1819. p->dn = cmMin(p->dN-1,cmMax(1,floor(p->srate / hz )));
  1820. p->hz = hz;
  1821. cmCombFiltSetAlpha(p,p->alpha); // the location of filter coeff changed - reset it here
  1822. cmIDelaySetTapMs(p->idp, 0,p->dn * 1000.0 / p->srate );
  1823. return cmOkRC;
  1824. }
  1825. //=======================================================================================================================
  1826. cmDcFilt* cmDcFiltAlloc( cmCtx* c, cmDcFilt* p, bool bypassFl )
  1827. {
  1828. cmDcFilt* op = cmObjAlloc(cmDcFilt, c, p );
  1829. if( cmDcFiltInit( op, bypassFl ) != cmOkRC )
  1830. cmDcFiltFree(&op);
  1831. return op;
  1832. }
  1833. cmRC_t cmDcFiltFree( cmDcFilt** pp)
  1834. {
  1835. cmRC_t rc = cmOkRC;
  1836. if( pp == NULL || *pp == NULL )
  1837. return cmOkRC;
  1838. cmDcFilt* p = *pp;
  1839. if((rc = cmDcFiltFinal(p)) != cmOkRC )
  1840. return rc;
  1841. cmObjFree(pp);
  1842. return cmOkRC;
  1843. }
  1844. cmRC_t cmDcFiltInit( cmDcFilt* p, bool bypassFl )
  1845. {
  1846. cmRC_t rc;
  1847. if((rc = cmDcFiltFinal(p)) != cmOkRC )
  1848. return rc;
  1849. p->b0 = 1;
  1850. p->b[0] = -1;
  1851. p->a[0] = -0.999;
  1852. p->d[0] = 0;
  1853. p->d[1] = 0;
  1854. p->bypassFl = bypassFl;
  1855. return rc;
  1856. }
  1857. cmRC_t cmDcFiltFinal( cmDcFilt* p )
  1858. { return cmOkRC; }
  1859. cmRC_t cmDcFiltExec( cmDcFilt* p, const cmSample_t* x, cmSample_t* y, unsigned n )
  1860. {
  1861. if( p->bypassFl )
  1862. cmVOS_Copy(y,n,x);
  1863. else
  1864. cmVOS_Filter(y,n,x,n,p->b0, p->b, p->a, p->d, 1 );
  1865. return cmOkRC;
  1866. }
  1867. //=======================================================================================================================
  1868. cmIDelay* cmIDelayAlloc( cmCtx* c, cmIDelay* p, cmReal_t srate, cmReal_t maxDelayMs, const cmReal_t* tapMs, const cmReal_t* tapFfGain, const cmReal_t* tapFbGain, unsigned tapCnt )
  1869. {
  1870. cmIDelay* op = cmObjAlloc(cmIDelay, c, p );
  1871. if( srate != 0 )
  1872. if( cmIDelayInit( op, srate, maxDelayMs, tapMs, tapFfGain, tapFbGain, tapCnt ) != cmOkRC )
  1873. cmIDelayFree(&op);
  1874. return op;
  1875. }
  1876. cmRC_t cmIDelayFree( cmIDelay** pp )
  1877. {
  1878. cmRC_t rc = cmOkRC;
  1879. if( pp == NULL || *pp == NULL )
  1880. return cmOkRC;
  1881. cmIDelay* p = *pp;
  1882. if((rc = cmIDelayFinal(p)) != cmOkRC )
  1883. return rc;
  1884. cmMemFree(p->m);
  1885. cmMemFree(p->ti);
  1886. cmMemFree(p->tff);
  1887. cmMemFree(p->tfb);
  1888. cmObjFree(pp);
  1889. return cmOkRC;
  1890. }
  1891. cmRC_t cmIDelayInit( cmIDelay* p, cmReal_t srate, cmReal_t maxDelayMs, const cmReal_t* tapMs, const cmReal_t* tapFfGain, const cmReal_t* tapFbGain, unsigned tapCnt )
  1892. {
  1893. cmRC_t rc;
  1894. if(( rc = cmIDelayFinal(p)) != cmOkRC )
  1895. return rc;
  1896. p->dn = (unsigned)floor( maxDelayMs * srate / 1000.0 );
  1897. p->mn = p->dn + 3;
  1898. p->m = cmMemResizeZ(cmSample_t,p->m,p->mn);
  1899. p->d = p->m + 1;
  1900. p->tn = tapCnt;
  1901. p->ti = cmMemResize(cmReal_t,p->ti,tapCnt);
  1902. p->tff = cmMemResize(cmReal_t,p->tff,tapCnt);
  1903. p->tfb = cmMemResize(cmReal_t,p->tfb,tapCnt);
  1904. p->srate = srate;
  1905. unsigned i;
  1906. for(i=0; i<tapCnt; ++i)
  1907. {
  1908. cmIDelaySetTapMs(p,i,tapMs[i]);
  1909. p->tff[i] = tapFfGain[i];
  1910. p->tfb[i] = tapFbGain[i];
  1911. }
  1912. return rc;
  1913. }
  1914. cmRC_t cmIDelayFinal(cmIDelay* p )
  1915. { return cmOkRC; }
  1916. cmRC_t cmIDelayExec( cmIDelay* p, const cmSample_t* x, cmSample_t* y, unsigned n )
  1917. {
  1918. unsigned i,j;
  1919. for(i=0; i<n; ++i)
  1920. {
  1921. cmSample_t fb = 0;
  1922. y[i] = 0;
  1923. // calculate the output sample
  1924. for(j=0; j<p->tn; ++j)
  1925. {
  1926. // get tap_j
  1927. cmReal_t tfi = p->ii - p->ti[j];
  1928. // mod it into 0:dn-1
  1929. while( tfi < 0 )
  1930. tfi += p->dn;
  1931. int ti = floor(tfi);
  1932. cmReal_t tf = tfi - ti;
  1933. cmSample_t v = _lin_interp(tf,p->d[ti],p->d[ti+1]); // _cube_interp(tf,p->d[ti-1],p->d[ti],p->d[ti+1],p->d[ti+2]);
  1934. y[i] += p->tff[j] * v;
  1935. fb += p->tfb[j] * v;
  1936. }
  1937. // insert incoming sample
  1938. p->d[p->ii] = x[i] + fb;
  1939. // advance the delay line
  1940. ++p->ii;
  1941. // handle beg/end delay sample duplication
  1942. // -1 0 1 2 .... dn-1 dn dn+1
  1943. // a b c a b c
  1944. // The samples at position -1 0 1 must be duplicated
  1945. // at positions dn-1,dn,dn+1 so that output sample calculation
  1946. // does not need to deal with buffer wrap-around issues.
  1947. if( p->ii == 1 )
  1948. p->d[p->dn] = x[i];
  1949. else
  1950. if( p->ii == 2 )
  1951. p->d[p->dn+1] = x[i];
  1952. else
  1953. if( p->ii == p->dn )
  1954. {
  1955. p->d[-1] = x[i];
  1956. p->ii = 0;
  1957. }
  1958. }
  1959. return cmOkRC;
  1960. }
  1961. cmRC_t cmIDelaySetTapMs( cmIDelay* p, unsigned tapIdx, cmReal_t tapMs )
  1962. {
  1963. cmRC_t rc = cmOkRC;
  1964. if( tapIdx < p->tn )
  1965. p->ti[tapIdx] = tapMs * p->srate / 1000.0;
  1966. else
  1967. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Tap index %i is out of range 0 - %i.",tapIdx,p->tn-1 );
  1968. return rc;
  1969. }
  1970. //=======================================================================================================================
  1971. cmGroupSel* cmGroupSelAlloc( cmCtx* c, cmGroupSel* p, unsigned chCnt, unsigned groupCnt, unsigned chsPerGroup )
  1972. {
  1973. cmGroupSel* op = cmObjAlloc(cmGroupSel, c, p );
  1974. if( chCnt != 0 )
  1975. if( cmGroupSelInit( op, chCnt, groupCnt, chsPerGroup ) != cmOkRC )
  1976. cmGroupSelFree(&op);
  1977. return op;
  1978. }
  1979. cmRC_t cmGroupSelFree( cmGroupSel** pp )
  1980. {
  1981. cmRC_t rc = cmOkRC;
  1982. if( pp == NULL || *pp == NULL )
  1983. return cmOkRC;
  1984. cmGroupSel* p = *pp;
  1985. if((rc = cmGroupSelFinal(p)) != cmOkRC )
  1986. return rc;
  1987. cmMemFree(p->groupArray);
  1988. cmMemFree(p->chArray);
  1989. cmObjFree(pp);
  1990. return cmOkRC;
  1991. }
  1992. cmRC_t cmGroupSelInit( cmGroupSel* p, unsigned chCnt, unsigned groupCnt, unsigned chsPerGroup )
  1993. {
  1994. cmRC_t rc;
  1995. if((rc = cmGroupSelFinal(p)) != cmOkRC )
  1996. return rc;
  1997. p->chArray = cmMemResizeZ(cmGroupSelCh,p->chArray,chCnt);
  1998. p->groupArray = cmMemResizeZ(cmGroupSelGrp,p->groupArray,chCnt);
  1999. p->chCnt = chCnt;
  2000. p->groupCnt = groupCnt;
  2001. p->chsPerGroup = chsPerGroup;
  2002. unsigned i;
  2003. for(i=0; i<p->chCnt; ++i)
  2004. p->chArray[i].groupIdx = cmInvalidIdx;
  2005. for(i=0; i<p->groupCnt; ++i)
  2006. {
  2007. p->groupArray[i].chIdxArray = cmMemAllocZ(unsigned, p->chCnt);
  2008. p->groupArray[i].chIdxCnt = 0;
  2009. }
  2010. return rc;
  2011. }
  2012. cmRC_t cmGroupSelFinal( cmGroupSel* p )
  2013. {
  2014. unsigned i;
  2015. for(i=0; i<p->groupCnt; ++i)
  2016. cmMemFree(p->groupArray[i].chIdxArray);
  2017. return cmOkRC;
  2018. }
  2019. cmRC_t cmGroupSetChannelGate( cmGroupSel* p, unsigned chIdx, bool gateFl )
  2020. {
  2021. assert(chIdx <p->chCnt);
  2022. cmGroupSelCh* cp = p->chArray + chIdx;
  2023. if( gateFl==true && cp->gateFl==false )
  2024. {
  2025. cp->readyFl = true;
  2026. }
  2027. if( gateFl==false && cp->gateFl==true )
  2028. {
  2029. cp->offsetFl = true;
  2030. cp->readyFl = false;
  2031. }
  2032. cp->gateFl = gateFl;
  2033. //printf("ch:%i gate:%i ready:%i offset:%i grp:%i\n",chIdx,cp->gateFl,cp->readyFl,cp->offsetFl,cp->groupIdx);
  2034. return cmOkRC;
  2035. }
  2036. cmRC_t cmGroupSetChannelRMS( cmGroupSel* p, unsigned chIdx, cmReal_t rms )
  2037. {
  2038. assert(chIdx <p->chCnt);
  2039. cmGroupSelCh* cp = p->chArray + chIdx;
  2040. cp->rms = rms;
  2041. return cmOkRC;
  2042. }
  2043. cmRC_t cmGroupSelExec( cmGroupSel* p )
  2044. {
  2045. unsigned i,j;
  2046. unsigned availGroupCnt = 0;
  2047. p->chsPerGroup = cmMin( p->chsPerGroup, p->chCnt );
  2048. p->updateFl = false;
  2049. // clear the create and release flags on each group
  2050. // and get the count of available groups
  2051. for(i=0; i<p->groupCnt; ++i)
  2052. {
  2053. cmGroupSelGrp* gp = p->groupArray + i;
  2054. gp->createFl = false;
  2055. if( gp->releaseFl )
  2056. {
  2057. // clear the groupIdx from the channels assigned to this released group
  2058. for(j=0; j<gp->chIdxCnt; ++j)
  2059. p->chArray[ gp->chIdxArray[j] ].groupIdx = cmInvalidIdx;
  2060. gp->releaseFl = false;
  2061. gp->chIdxCnt = 0;
  2062. }
  2063. if( gp->chIdxCnt == 0 )
  2064. ++availGroupCnt;
  2065. }
  2066. // count the number of ready but unassigned ch's and
  2067. // release any groups which contain a channel with a gate offset
  2068. unsigned readyChCnt = 0;
  2069. for(i=0; i<p->chCnt; ++i)
  2070. {
  2071. cmGroupSelCh* cp = p->chArray + i;
  2072. // count the number of channels that are ready to be assigned
  2073. if( cp->readyFl )
  2074. ++readyChCnt;
  2075. // if this channel offset and it had been assigned to a group
  2076. // then mark its group for release
  2077. if( cp->offsetFl && cp->groupIdx != cmInvalidIdx )
  2078. {
  2079. p->groupArray[cp->groupIdx].releaseFl = true;
  2080. p->updateFl = true;
  2081. }
  2082. }
  2083. // assign ready ch's to available groups
  2084. while( readyChCnt > p->chsPerGroup && availGroupCnt > 0 )
  2085. {
  2086. unsigned groupIdx;
  2087. // locate an available group
  2088. for(groupIdx=0; groupIdx<p->groupCnt; ++groupIdx)
  2089. if( p->groupArray[groupIdx].chIdxCnt == 0 )
  2090. break;
  2091. if( groupIdx == p->groupCnt )
  2092. break;
  2093. --availGroupCnt;
  2094. cmGroupSelGrp* gp = p->groupArray + groupIdx;
  2095. printf("gs - group:%i chs: ",groupIdx);
  2096. // assign chsPerGroup ch's to this group
  2097. for(i=0; i<p->chCnt && gp->chIdxCnt < p->chsPerGroup; ++i)
  2098. if(p->chArray[i].readyFl )
  2099. {
  2100. p->chArray[i].readyFl = false; // this ch is no longer ready because it is assigned
  2101. p->chArray[i].groupIdx = groupIdx; // set this ch's group idx
  2102. gp->chIdxArray[ gp->chIdxCnt ] = i; // assign channel to group
  2103. gp->chIdxCnt += 1; // update the group ch count
  2104. gp->createFl = true;
  2105. p->updateFl = true;
  2106. --readyChCnt;
  2107. printf("%i ",i);
  2108. }
  2109. printf("\n");
  2110. }
  2111. return cmOkRC;
  2112. }
  2113. //=======================================================================================================================
  2114. cmAudioNofM* cmAudioNofMAlloc( cmCtx* c, cmAudioNofM* p, cmReal_t srate, unsigned inChCnt, unsigned outChCnt, cmReal_t fadeTimeMs )
  2115. {
  2116. cmAudioNofM* op = cmObjAlloc(cmAudioNofM, c, p );
  2117. if( srate != 0 )
  2118. if( cmAudioNofMInit( op, srate, inChCnt, outChCnt, fadeTimeMs ) != cmOkRC )
  2119. cmAudioNofMFree(&op);
  2120. return op;
  2121. }
  2122. cmRC_t cmAudioNofMFree( cmAudioNofM** pp )
  2123. {
  2124. cmRC_t rc = cmOkRC;
  2125. if( pp == NULL || *pp == NULL )
  2126. return cmOkRC;
  2127. cmAudioNofM* p = *pp;
  2128. if((rc = cmAudioNofMFinal(p)) != cmOkRC )
  2129. return rc;
  2130. cmMemFree(p->outArray);
  2131. cmMemFree(p->inArray);
  2132. cmObjFree(pp);
  2133. return cmOkRC;
  2134. }
  2135. cmRC_t cmAudioNofMInit( cmAudioNofM* p, cmReal_t srate, unsigned iChCnt, unsigned oChCnt, cmReal_t fadeTimeMs )
  2136. {
  2137. cmRC_t rc;
  2138. if((rc = cmAudioNofMFinal(p)) != cmOkRC )
  2139. return rc;
  2140. p->iChCnt = iChCnt;
  2141. p->inArray = cmMemResizeZ(cmAudioNofM_In,p->inArray,iChCnt);
  2142. p->oChCnt = oChCnt;
  2143. p->outArray = cmMemResizeZ(cmAudioNofM_Out,p->outArray,oChCnt);
  2144. p->nxtOutChIdx = 0;
  2145. unsigned i;
  2146. for(i=0; i<p->iChCnt; ++i)
  2147. {
  2148. p->inArray[i].fader = cmFaderAlloc(p->obj.ctx, NULL, srate, fadeTimeMs );
  2149. p->inArray[i].outChIdx = cmInvalidIdx;
  2150. }
  2151. cmAudioNofMSetFadeMs( p, fadeTimeMs );
  2152. return rc;
  2153. }
  2154. cmRC_t cmAudioNofMFinal( cmAudioNofM* p )
  2155. {
  2156. unsigned i;
  2157. for(i=0; i<p->iChCnt; ++i)
  2158. cmFaderFree(&p->inArray[i].fader);
  2159. return cmOkRC;
  2160. }
  2161. cmRC_t cmAudioNofMSetChannelGate( cmAudioNofM* p, unsigned inChIdx, bool gateFl )
  2162. {
  2163. assert( inChIdx < p->iChCnt );
  2164. cmAudioNofM_In* ip = p->inArray + inChIdx;
  2165. if( ip->gateFl && gateFl == false )
  2166. ip->offsetFl = true;
  2167. if( ip->gateFl == false && gateFl )
  2168. ip->onsetFl = true;
  2169. ip->gateFl = gateFl;
  2170. printf("nom: %p ch:%i gate:%i on:%i off:%i\n",p,inChIdx,ip->gateFl,ip->offsetFl,ip->onsetFl);
  2171. return cmOkRC;
  2172. }
  2173. cmRC_t cmAudioNofMExec( cmAudioNofM* p, const cmSample_t* x[], unsigned inChCnt, cmSample_t* y[], unsigned outChCnt, unsigned n )
  2174. {
  2175. assert( inChCnt == p->iChCnt && outChCnt == p->oChCnt );
  2176. unsigned i;
  2177. // for each output channel
  2178. for(i=0; i<p->oChCnt; ++i)
  2179. {
  2180. cmAudioNofM_Out* op = p->outArray + i;
  2181. cmAudioNofM_In* ip = op->list;
  2182. cmAudioNofM_In* pp = NULL;
  2183. // for each input assigned to this output chanenl
  2184. while( ip != NULL )
  2185. {
  2186. // if the channel is no longer active
  2187. if( ip->offsetFl && ip->fader->gain == 0.0 )
  2188. {
  2189. printf("nom: off - out:%i\n",ip->outChIdx);
  2190. // remove it from the output channels list
  2191. ip->offsetFl = false;
  2192. ip->outChIdx = cmInvalidIdx;
  2193. if( pp == NULL )
  2194. op->list = ip->link;
  2195. else
  2196. pp->link = ip->link;
  2197. }
  2198. pp = ip;
  2199. ip = ip->link;
  2200. }
  2201. }
  2202. // for each input channel
  2203. for(i=0; i<inChCnt; ++i)
  2204. {
  2205. cmAudioNofM_In* ip = p->inArray + i;
  2206. // if this channel is starting
  2207. if( ip->onsetFl == true && ip->offsetFl == false )
  2208. {
  2209. // assign it to the next output channel
  2210. ip->onsetFl = false;
  2211. ip->outChIdx = p->nxtOutChIdx;
  2212. ip->link = p->outArray[ ip->outChIdx ].list;
  2213. p->outArray[ ip->outChIdx ].list = ip;
  2214. p->nxtOutChIdx = (p->nxtOutChIdx + 1) % p->oChCnt;
  2215. printf("nom: on - in:%i out:%i\n",i,ip->outChIdx);
  2216. }
  2217. // if this channel is active - then mix its input
  2218. if( ip->outChIdx != cmInvalidIdx )
  2219. {
  2220. cmFaderExec( ip->fader, n, ip->gateFl, true, x[i], y[ ip->outChIdx ] );
  2221. }
  2222. }
  2223. return cmOkRC;
  2224. }
  2225. cmRC_t cmAudioNofMSetFadeMs( cmAudioNofM* p, cmReal_t fadeTimeMs )
  2226. {
  2227. unsigned i;
  2228. for(i=0; i<p->iChCnt; ++i)
  2229. cmFaderSetFadeTime(p->inArray[i].fader,fadeTimeMs);
  2230. return cmOkRC;
  2231. }
  2232. //=======================================================================================================================
  2233. cmAdsr* cmAdsrAlloc( cmCtx* c, cmAdsr* p, cmReal_t srate, bool trigFl, cmReal_t minL, cmReal_t dlyMs, cmReal_t atkMs, cmReal_t atkL, cmReal_t dcyMs, cmReal_t susMs, cmReal_t susL, cmReal_t rlsMs )
  2234. {
  2235. cmAdsr* op = cmObjAlloc(cmAdsr, c, p );
  2236. if( srate != 0 )
  2237. if( cmAdsrInit( op, srate, trigFl, minL, dlyMs, atkMs, atkL, dcyMs, susMs, susL, rlsMs ) != cmOkRC )
  2238. cmAdsrFree(&op);
  2239. return op;
  2240. }
  2241. cmRC_t cmAdsrFree( cmAdsr** pp )
  2242. {
  2243. cmRC_t rc = cmOkRC;
  2244. if( pp == NULL || *pp == NULL )
  2245. return cmOkRC;
  2246. cmAdsr* p = *pp;
  2247. if((rc = cmAdsrFinal(p)) != cmOkRC )
  2248. return rc;
  2249. cmObjFree(pp);
  2250. return cmOkRC;
  2251. }
  2252. cmRC_t cmAdsrInit( cmAdsr* p, cmReal_t srate, bool trigFl, cmReal_t minL, cmReal_t dlyMs, cmReal_t atkMs, cmReal_t atkL, cmReal_t dcyMs, cmReal_t susMs, cmReal_t susL, cmReal_t rlsMs )
  2253. {
  2254. cmRC_t rc;
  2255. if((rc = cmAdsrFinal(p)) != cmOkRC )
  2256. return rc;
  2257. // this is a limitation of the design - the design should be replaced with one
  2258. // which increments/decrements the level until it reaches a limit instead of calculating
  2259. // durations
  2260. //assert(atkL>=0 && minL>=0);
  2261. p->srate = srate;
  2262. p->trigModeFl = trigFl;
  2263. p->levelMin = minL;
  2264. p->scaleDur = 1.0;
  2265. cmAdsrSetTime(p,dlyMs,kDlyAdsrId);
  2266. cmAdsrSetTime(p,atkMs,kAtkAdsrId);
  2267. cmAdsrSetTime(p,dcyMs,kDcyAdsrId);
  2268. cmAdsrSetTime(p,susMs,kSusAdsrId);
  2269. cmAdsrSetTime(p,rlsMs,kRlsAdsrId);
  2270. cmAdsrSetLevel(p,atkL,kAtkAdsrId);
  2271. cmAdsrSetLevel(p,susL,kSusAdsrId);
  2272. p->state = kDoneAdsrId;
  2273. p->durSmp = 0;
  2274. p->level = p->levelMin;
  2275. p->gateFl = false;
  2276. p->atkDurSmp = 0;
  2277. p->rlsDurSmp = 0;
  2278. return cmOkRC;
  2279. }
  2280. cmRC_t cmAdsrFinal( cmAdsr* p )
  2281. { return cmOkRC; }
  2282. cmReal_t cmAdsrExec( cmAdsr* p, unsigned procSmpCnt, bool gateFl, cmReal_t tscale, cmReal_t ascale )
  2283. {
  2284. double scaleAmp = ascale;
  2285. double scaleDur = tscale;
  2286. // if onset
  2287. if( p->gateFl == false && gateFl==true )
  2288. {
  2289. p->scaleDur = scaleDur==0 ? 1.0 : fabs(scaleDur);
  2290. //printf("sd:%f %f\n",scaleDur,scaleAmp);
  2291. switch( p->state )
  2292. {
  2293. case kDlyAdsrId:
  2294. // if in delay mode when the re-attack occurs don't do anything
  2295. break;
  2296. case kAtkAdsrId:
  2297. case kDcyAdsrId:
  2298. case kSusAdsrId:
  2299. case kRlsAdsrId:
  2300. // if the atk level == 0 then fall through to kDoneAdsrId
  2301. if( p->actAtkLevel != 0 )
  2302. {
  2303. // re-attak mode:
  2304. // Scale the attack time to the current level relative to the attack level.
  2305. // In general this will result in a decrease in the attack duration.
  2306. p->atkDurSmp = cmMax(1,floor(p->atkSmp * (p->actAtkLevel - p->level) / p->actAtkLevel));
  2307. p->atkBegLevel = p->level;
  2308. p->durSmp = 0;
  2309. p->state = kAtkAdsrId;
  2310. p->actAtkLevel = p->atkLevel * scaleAmp;
  2311. p->actSusLevel = p->susLevel * scaleAmp;
  2312. break;
  2313. }
  2314. case kDoneAdsrId:
  2315. p->atkBegLevel = p->levelMin;
  2316. p->atkDurSmp = p->atkSmp;
  2317. p->state = p->dlySmp == 0 ? kAtkAdsrId : kDlyAdsrId;
  2318. p->durSmp = 0;
  2319. p->actAtkLevel = p->atkLevel * scaleAmp;
  2320. p->actSusLevel = p->susLevel * scaleAmp;
  2321. break;
  2322. default:
  2323. { assert(0); }
  2324. }
  2325. }
  2326. // if an offset occurred and we are not in trigger mode - then go into release mode
  2327. if( p->trigModeFl==false && p->gateFl == true && gateFl == false )
  2328. {
  2329. switch( p->state )
  2330. {
  2331. case kDlyAdsrId:
  2332. case kAtkAdsrId:
  2333. case kDcyAdsrId:
  2334. case kSusAdsrId:
  2335. if( p->actSusLevel == 0 )
  2336. p->state = kDoneAdsrId;
  2337. else
  2338. {
  2339. // scale the release time to the current level relative to the sustain level
  2340. p->rlsDurSmp = cmMax(1,floor(p->rlsSmp * p->level / p->actSusLevel));
  2341. p->rlsLevel = p->level;
  2342. p->durSmp = 0;
  2343. }
  2344. break;
  2345. case kRlsAdsrId:
  2346. case kDoneAdsrId:
  2347. // nothing to do
  2348. break;
  2349. default:
  2350. { assert(0); }
  2351. }
  2352. p->state = kRlsAdsrId;
  2353. }
  2354. p->gateFl = gateFl;
  2355. switch( p->state )
  2356. {
  2357. case kDlyAdsrId:
  2358. p->level = p->levelMin;
  2359. if( p->durSmp >= p->dlySmp )
  2360. {
  2361. p->state = kAtkAdsrId;
  2362. p->durSmp = 0;
  2363. if( p->trigModeFl )
  2364. {
  2365. p->atkBegLevel = p->level;
  2366. p->atkDurSmp = p->atkSmp;
  2367. }
  2368. }
  2369. break;
  2370. case kAtkAdsrId:
  2371. if( p->atkDurSmp != 0 )
  2372. p->level = p->atkBegLevel + (p->actAtkLevel - p->atkBegLevel) * cmMin(p->durSmp,p->atkDurSmp) / p->atkDurSmp;
  2373. if( p->durSmp >= p->atkDurSmp || p->atkDurSmp == 0 )
  2374. {
  2375. p->state = kDcyAdsrId;
  2376. p->durSmp = 0;
  2377. }
  2378. break;
  2379. case kDcyAdsrId:
  2380. if( p->dcySmp != 0 )
  2381. p->level = p->actAtkLevel - ((p->actAtkLevel - p->actSusLevel) * cmMin(p->durSmp,p->dcySmp) / p->dcySmp );
  2382. if( p->durSmp >= p->dcySmp || p->dcySmp==0 )
  2383. {
  2384. p->state = kSusAdsrId;
  2385. p->durSmp = 0;
  2386. }
  2387. break;
  2388. case kSusAdsrId:
  2389. p->level = p->actSusLevel;
  2390. if( p->trigModeFl==true && p->durSmp >= p->susSmp )
  2391. {
  2392. p->state = kRlsAdsrId;
  2393. p->durSmp = 0;
  2394. p->rlsLevel = p->level;
  2395. p->rlsDurSmp = p->rlsSmp;
  2396. }
  2397. break;
  2398. case kRlsAdsrId:
  2399. if( p->rlsDurSmp != 0 )
  2400. p->level = p->rlsLevel - ((p->rlsLevel - p->levelMin) * cmMin(p->durSmp,p->rlsDurSmp) / p->rlsDurSmp);
  2401. if( p->durSmp >= p->rlsDurSmp || p->rlsDurSmp==0 )
  2402. {
  2403. p->state = kDoneAdsrId;
  2404. p->durSmp = 0;
  2405. }
  2406. break;
  2407. case kDoneAdsrId:
  2408. p->level = p->levelMin;
  2409. break;
  2410. default:
  2411. { assert(0); }
  2412. }
  2413. p->durSmp += floor(procSmpCnt/p->scaleDur);
  2414. return p->level;
  2415. }
  2416. void _cmAdsrSetTime( cmAdsr* p, cmReal_t ms, int* smpPtr )
  2417. {
  2418. *smpPtr = cmMax(1,floor(p->srate * ms / 1000.0 ));
  2419. }
  2420. void cmAdsrSetTime( cmAdsr* p, cmReal_t ms, unsigned id )
  2421. {
  2422. switch( id )
  2423. {
  2424. case kDlyAdsrId:
  2425. _cmAdsrSetTime(p,ms,&p->dlySmp);
  2426. break;
  2427. case kAtkAdsrId:
  2428. _cmAdsrSetTime(p,ms,&p->atkSmp);
  2429. break;
  2430. case kDcyAdsrId:
  2431. _cmAdsrSetTime(p,ms,&p->dcySmp);
  2432. break;
  2433. case kSusAdsrId:
  2434. _cmAdsrSetTime(p,ms,&p->susSmp);
  2435. break;
  2436. case kRlsAdsrId:
  2437. _cmAdsrSetTime(p,ms,&p->rlsSmp);
  2438. break;
  2439. default:
  2440. { assert(0); }
  2441. }
  2442. }
  2443. void cmAdsrSetLevel( cmAdsr* p, cmReal_t level, unsigned id )
  2444. {
  2445. switch( id )
  2446. {
  2447. case kDlyAdsrId:
  2448. p->levelMin = level;
  2449. break;
  2450. case kAtkAdsrId:
  2451. p->atkLevel = level;
  2452. p->actAtkLevel = p->atkLevel;
  2453. break;
  2454. case kSusAdsrId:
  2455. p->susLevel = level;
  2456. p->actSusLevel = p->susLevel;
  2457. break;
  2458. default:
  2459. { assert(0); }
  2460. }
  2461. }
  2462. void cmAdsrReport( cmAdsr* p, cmRpt_t* rpt )
  2463. {
  2464. cmRptPrintf(rpt,"state:%i gate:%i phs:%i d:%i a:%i (%i) d:%i s:%i r:%i (%i) - min:%f atk:%f\n",p->state,p->gateFl,p->durSmp,p->dlySmp,p->atkSmp,p->atkDurSmp,p->dcySmp,p->susSmp,p->rlsSmp,p->rlsDurSmp, p->levelMin, p->atkLevel);
  2465. }
  2466. //=======================================================================================================================
  2467. cmCompressor* cmCompressorAlloc( cmCtx* c, cmCompressor* p, cmReal_t srate, unsigned procSmpCnt, cmReal_t inGain, cmReal_t rmsWndMaxMs, cmReal_t rmsWndMs, cmReal_t threshDb, cmReal_t ratio_num, cmReal_t atkMs, cmReal_t rlsMs, cmReal_t outGain, bool bypassFl )
  2468. {
  2469. cmCompressor* op = cmObjAlloc(cmCompressor, c, p );
  2470. if( srate != 0 )
  2471. if( cmCompressorInit( op, srate, procSmpCnt, inGain, rmsWndMaxMs, rmsWndMs, threshDb, ratio_num, atkMs, rlsMs, outGain, bypassFl ) != cmOkRC )
  2472. cmCompressorFree(&op);
  2473. return op;
  2474. }
  2475. cmRC_t cmCompressorFree( cmCompressor** pp )
  2476. {
  2477. cmRC_t rc = cmOkRC;
  2478. if( pp == NULL || *pp == NULL )
  2479. return cmOkRC;
  2480. cmCompressor* p = *pp;
  2481. if((rc = cmCompressorFinal(p)) != cmOkRC )
  2482. return rc;
  2483. cmMemFree(p->rmsWnd);
  2484. cmObjFree(pp);
  2485. return cmOkRC;
  2486. }
  2487. cmRC_t cmCompressorInit( cmCompressor* p, cmReal_t srate, unsigned procSmpCnt, cmReal_t inGain, cmReal_t rmsWndMaxMs, cmReal_t rmsWndMs, cmReal_t threshDb, cmReal_t ratio_num, cmReal_t atkMs, cmReal_t rlsMs, cmReal_t outGain, bool bypassFl )
  2488. {
  2489. cmRC_t rc;
  2490. if((rc = cmCompressorFinal(p)) != cmOkRC )
  2491. return rc;
  2492. p->srate = srate;
  2493. p->procSmpCnt = procSmpCnt;
  2494. p->threshDb = threshDb;
  2495. p->ratio_num = ratio_num;
  2496. cmCompressorSetAttackMs(p,atkMs);
  2497. cmCompressorSetReleaseMs(p,rlsMs);
  2498. p->inGain = inGain;
  2499. p->outGain = outGain;
  2500. p->bypassFl = bypassFl;
  2501. p->rmsWndAllocCnt = cmMax(1,(unsigned)floor(rmsWndMaxMs * srate / (1000.0 * procSmpCnt)));
  2502. p->rmsWnd = cmMemResizeZ(cmSample_t,p->rmsWnd,p->rmsWndAllocCnt);
  2503. cmCompressorSetRmsWndMs(p, rmsWndMs );
  2504. p->rmsWndIdx = 0;
  2505. p->state = kRlsCompId;
  2506. p->timeConstDb = 10.0;
  2507. p->accumDb = p->threshDb;
  2508. return rc;
  2509. }
  2510. cmRC_t cmCompressorFinal( cmCompressor* p )
  2511. { return cmOkRC; }
  2512. /*
  2513. The ratio determines to what degree a signal above the threshold is reduced.
  2514. Given a 2:1 ratio, a signal 2dB above the threshold will be reduced to 1db above the threshold.
  2515. Given a 4:1 ratio, a signal 2dB above the threshold will be reduced to 0.25db above the threshold.
  2516. Gain_reduction_db = (thresh - signal) / ratio_numerator (difference between the threshold and signal level after reduction)
  2517. Gain Coeff = 10^(gain_reduction_db / 20);
  2518. Total_reduction_db = signal - threshold + Gain_reduc_db
  2519. (total change in signal level)
  2520. The attack can be viewed as beginning at the threshold and moving to the peak
  2521. over some period of time. In linear terms this will go from 1.0 to the max gain
  2522. reductions. In this case we step from thresh to peak at a fixed rate in dB
  2523. based on the attack time.
  2524. Db: thresh - [thesh:peak] / ratio_num
  2525. Linear: pow(10, (thresh - [thesh:peak] / ratio_num)/20 );
  2526. During attacks p->accumDb increments toward the p->pkDb.
  2527. During release p->accumDb decrements toward the threshold.
  2528. (thresh - accumDb) / ratio_num gives the signal level which will be achieved
  2529. if this value is converted to linear form and applied as a gain coeff.
  2530. See compressor.m
  2531. */
  2532. cmRC_t cmCompressorExec( cmCompressor* p, const cmSample_t* x, cmSample_t* y, unsigned n )
  2533. {
  2534. cmSample_t xx[n];
  2535. cmVOS_MultVVS(xx,n,x,p->inGain); // apply input gain
  2536. p->rmsWnd[ p->rmsWndIdx ] = cmVOS_RMS(xx, n, n ); // calc and store signal RMS
  2537. p->rmsWndIdx = (p->rmsWndIdx + 1) % p->rmsWndCnt; // advance the RMS storage buffer
  2538. cmReal_t rmsLin = cmVOS_Sum(p->rmsWnd,p->rmsWndCnt) / p->rmsWndCnt; // calc avg RMS
  2539. cmReal_t rmsDb = cmMax(-100.0,20 * log10(cmMax(0.00001,rmsLin))); // convert avg RMS to dB
  2540. rmsDb += 100.0;
  2541. // if the compressor is bypassed
  2542. if( p->bypassFl )
  2543. {
  2544. cmVOS_Copy(y,n,x); // copy through - with no input gain
  2545. return cmOkRC;
  2546. }
  2547. // if the signal is above the threshold
  2548. if( rmsDb <= p->threshDb )
  2549. p->state = kRlsCompId;
  2550. else
  2551. {
  2552. if( rmsDb > p->pkDb )
  2553. p->pkDb = rmsDb;
  2554. p->state = kAtkCompId;
  2555. }
  2556. switch( p->state )
  2557. {
  2558. case kAtkCompId:
  2559. p->accumDb = cmMin(p->pkDb, p->accumDb + p->timeConstDb * n / p->atkSmp );
  2560. break;
  2561. case kRlsCompId:
  2562. p->accumDb = cmMax(p->threshDb, p->accumDb - p->timeConstDb * n / p->rlsSmp );
  2563. break;
  2564. }
  2565. p->gain = pow(10.0,(p->threshDb - p->accumDb) / (p->ratio_num * 20.0));
  2566. cmVOS_MultVVS(y,n,xx,p->gain * p->outGain);
  2567. return cmOkRC;
  2568. }
  2569. void _cmCompressorSetMs( cmCompressor* p, cmReal_t ms, unsigned* smpPtr )
  2570. { *smpPtr = cmMax(1,(unsigned)floor(ms * p->srate / 1000.0)); }
  2571. void cmCompressorSetAttackMs( cmCompressor* p, cmReal_t ms )
  2572. { _cmCompressorSetMs(p,ms,&p->atkSmp); }
  2573. void cmCompressorSetReleaseMs( cmCompressor* p, cmReal_t ms )
  2574. { _cmCompressorSetMs(p,ms,&p->rlsSmp); }
  2575. void cmCompressorSetThreshDb( cmCompressor* p, cmReal_t threshDb )
  2576. { p->threshDb = cmMax(0.0,100 + threshDb); }
  2577. void cmCompressorSetRmsWndMs( cmCompressor* p, cmReal_t ms )
  2578. {
  2579. p->rmsWndCnt = cmMax(1,(unsigned)floor(ms * p->srate / (1000.0 * p->procSmpCnt)));
  2580. // do not allow rmsWndCnt to exceed rmsWndAllocCnt
  2581. if( p->rmsWndCnt > p->rmsWndAllocCnt )
  2582. p->rmsWndCnt = p->rmsWndAllocCnt;
  2583. }
  2584. //=======================================================================================================================
  2585. cmBiQuadEq* cmBiQuadEqAlloc( cmCtx* c, cmBiQuadEq* p, cmReal_t srate, unsigned mode, cmReal_t f0Hz, cmReal_t Q, cmReal_t gainDb, bool bypassFl )
  2586. {
  2587. cmBiQuadEq* op = cmObjAlloc(cmBiQuadEq, c, p );
  2588. if( srate != 0 )
  2589. if( cmBiQuadEqInit( op, srate, mode, f0Hz, Q, gainDb, bypassFl ) != cmOkRC )
  2590. cmBiQuadEqFree(&op);
  2591. return op;
  2592. }
  2593. cmRC_t cmBiQuadEqFree( cmBiQuadEq** pp )
  2594. {
  2595. cmRC_t rc = cmOkRC;
  2596. if( pp == NULL || *pp == NULL )
  2597. return cmOkRC;
  2598. cmBiQuadEq* p = *pp;
  2599. if((rc = cmBiQuadEqFinal(p)) != cmOkRC )
  2600. return rc;
  2601. cmObjFree(pp);
  2602. return cmOkRC;
  2603. }
  2604. void _cmBiQuadEqInit( cmBiQuadEq* p )
  2605. {
  2606. cmReal_t w0 = 2*M_PI*p->f0Hz/p->srate;
  2607. cmReal_t cos_w0 = cos(w0);
  2608. cmReal_t alpha = sin(w0)/(2*p->Q);
  2609. if( p->mode==kLowShelfBqId || p->mode==kHighShelfBqId )
  2610. {
  2611. cmReal_t c = p->mode==kLowShelfBqId ? 1.0 : -1.0;
  2612. cmReal_t A = pow(10.0,p->gainDb/40.0);
  2613. cmReal_t B = 2*sqrt(A)*alpha;
  2614. p->b[0] = A*( (A+1) - c * (A-1)*cos_w0 + B );
  2615. p->b[1] = c*2*A*( (A-1) - c * (A+1)*cos_w0);
  2616. p->b[2] = A*( (A+1) - c * (A-1)*cos_w0 - B );
  2617. p->a[0] = (A+1) + c * (A-1)*cos_w0 + B;
  2618. p->a[1] = -c*2*( (A-1) + c * (A+1)*cos_w0);
  2619. p->a[2] = (A+1) + c * (A-1)*cos_w0 - B;
  2620. }
  2621. else
  2622. {
  2623. if( p->mode != kPeakBqId )
  2624. {
  2625. p->a[0] = 1 + alpha;
  2626. p->a[1] = -2*cos_w0;
  2627. p->a[2] = 1 - alpha;
  2628. }
  2629. switch(p->mode)
  2630. {
  2631. case kLpfBqId:
  2632. case kHpFBqId:
  2633. {
  2634. cmReal_t c = p->mode==kLpfBqId ? 1.0 : -1.0;
  2635. p->b[0] = (1 - c * cos_w0)/2;
  2636. p->b[1] = c * (1 - c * cos_w0);
  2637. p->b[2] = (1 - c * cos_w0)/2;
  2638. }
  2639. break;
  2640. case kBpfBqId:
  2641. p->b[0] = p->Q*alpha;
  2642. p->b[1] = 0;
  2643. p->b[2] = -p->Q*alpha;
  2644. break;
  2645. case kNotchBqId:
  2646. p->b[0] = 1;
  2647. p->b[1] = -2*cos_w0;
  2648. p->b[2] = 1;
  2649. break;
  2650. case kAllpassBqId:
  2651. p->b[0] = 1 - alpha;
  2652. p->b[1] = -2*cos_w0;
  2653. p->b[2] = 1 + alpha;
  2654. break;
  2655. case kPeakBqId:
  2656. {
  2657. cmReal_t A = pow(10.0,p->gainDb/40.0);
  2658. p->b[0] = 1 + alpha*A;
  2659. p->b[1] = -2*cos_w0;
  2660. p->b[2] = 1 - alpha*A;
  2661. p->a[0] = 1 + alpha/A;
  2662. p->a[1] = -2*cos_w0;
  2663. p->a[2] = 1 - alpha/A;
  2664. }
  2665. break;
  2666. }
  2667. }
  2668. cmReal_t a0 = p->a[0];
  2669. unsigned i=0;
  2670. for(; i<3; ++i)
  2671. {
  2672. p->b[i]/=a0;
  2673. p->a[i]/=a0;
  2674. }
  2675. if(0)
  2676. {
  2677. printf("sr:%f mode:%i f0:%f q:%f gain:%f\n", p->srate,p->mode,p->f0Hz,p->Q,p->gainDb);
  2678. for(i=0; i<3; ++i)
  2679. printf("a[%i]=%f b[%i]=%f ",i,p->a[i],i,p->b[i]);
  2680. printf("\n");
  2681. }
  2682. }
  2683. cmRC_t cmBiQuadEqInit( cmBiQuadEq* p, cmReal_t srate, unsigned mode, cmReal_t f0Hz, cmReal_t Q, cmReal_t gainDb, bool bypassFl )
  2684. {
  2685. cmRC_t rc;
  2686. if((rc = cmBiQuadEqFinal(p)) != cmOkRC )
  2687. return rc;
  2688. memset(p->d,0,sizeof(p->d));
  2689. p->srate = srate;
  2690. cmBiQuadEqSet(p, mode, f0Hz, Q, gainDb );
  2691. _cmBiQuadEqInit(p);
  2692. return rc;
  2693. }
  2694. cmRC_t cmBiQuadEqFinal( cmBiQuadEq* p )
  2695. { return cmOkRC;}
  2696. cmRC_t cmBiQuadEqExec( cmBiQuadEq* p, const cmSample_t* x, cmSample_t* y, unsigned n )
  2697. {
  2698. if( y == NULL )
  2699. return cmOkRC;
  2700. if( x == NULL )
  2701. {
  2702. cmVOS_Zero(y,n);
  2703. return cmOkRC;
  2704. }
  2705. if( p->bypassFl )
  2706. {
  2707. cmVOS_Copy(y,n,x);
  2708. return cmOkRC;
  2709. }
  2710. // Direct Form I implementation
  2711. unsigned i=0;
  2712. for(; i<n; ++i)
  2713. {
  2714. y[i] = p->b[0]*x[i] + p->b[1]*p->d[0] + p->b[2]*p->d[1] - p->a[1]*p->d[2] - p->a[2]*p->d[3];
  2715. p->d[1] = p->d[0];
  2716. p->d[0] = x[i];
  2717. p->d[3] = p->d[2];
  2718. p->d[2] = y[i];
  2719. }
  2720. return cmOkRC;
  2721. }
  2722. void cmBiQuadEqSet( cmBiQuadEq* p, unsigned mode, cmReal_t f0Hz, cmReal_t Q, cmReal_t gainDb )
  2723. {
  2724. p->mode = mode;
  2725. p->f0Hz = f0Hz;
  2726. p->Q = Q;
  2727. p->gainDb= gainDb;
  2728. _cmBiQuadEqInit(p);
  2729. }
  2730. //=======================================================================================================================
  2731. cmDistDs* cmDistDsAlloc( cmCtx* c, cmDistDs* p, cmReal_t srate, cmReal_t inGain, cmReal_t downSrate, cmReal_t bits, bool rectFl, bool fullFl, cmReal_t clipDb, cmReal_t outGain, bool bypassFl )
  2732. {
  2733. cmDistDs* op = cmObjAlloc(cmDistDs, c, p );
  2734. if( srate != 0 )
  2735. if( cmDistDsInit( op, srate, inGain, downSrate, bits, rectFl, fullFl, clipDb, outGain, bypassFl ) != cmOkRC )
  2736. cmDistDsFree(&op);
  2737. return op;
  2738. }
  2739. cmRC_t cmDistDsFree( cmDistDs** pp )
  2740. {
  2741. cmRC_t rc = cmOkRC;
  2742. if( pp == NULL || *pp == NULL )
  2743. return cmOkRC;
  2744. cmDistDs* p = *pp;
  2745. if((rc = cmDistDsFinal(p)) != cmOkRC )
  2746. return rc;
  2747. cmObjFree(pp);
  2748. return cmOkRC;
  2749. }
  2750. cmRC_t cmDistDsInit( cmDistDs* p, cmReal_t srate, cmReal_t inGain, cmReal_t downSrate, cmReal_t bits, bool rectFl, bool fullFl, cmReal_t clipDb, cmReal_t outGain, bool bypassFl )
  2751. {
  2752. cmRC_t rc;
  2753. if((rc = cmDistDsFinal(p)) != cmOkRC )
  2754. return rc;
  2755. p->srate = srate;
  2756. p->downSrate = downSrate;
  2757. p->bits = bits;
  2758. p->rectFl = rectFl;
  2759. p->fullFl = fullFl;
  2760. p->clipDb = clipDb;
  2761. p->inGain = inGain;
  2762. p->outGain = outGain;
  2763. p->bypassFl = bypassFl;
  2764. p->fracIdx = 0;
  2765. p->lastVal = 0;
  2766. p->lastY = 0;
  2767. p->lastX = 0;
  2768. return rc;
  2769. }
  2770. cmRC_t cmDistDsFinal( cmDistDs* p )
  2771. { return cmOkRC; }
  2772. void _cmDistDsExpr0( cmDistDs* p, const cmSample_t* x, cmSample_t* y, unsigned n )
  2773. {
  2774. unsigned i= 0;
  2775. for(i=0; i<n; ++i)
  2776. {
  2777. p->lastY = fmod(p->lastY + fabs(x[i] - p->lastX),2.0);
  2778. y[i] = cmVOS_RMS(x,n,n) * x[i] * (p->lastY - 1.0);
  2779. p->lastX = x[i];
  2780. }
  2781. }
  2782. void _cmDistDsExpr1( cmDistDs* p, cmSample_t* x, cmSample_t* y, unsigned n )
  2783. {
  2784. unsigned i= 0;
  2785. for(i=0; i<n; ++i)
  2786. {
  2787. p->lastY = fmod(p->lastY + fabs(x[i] - p->lastX),2.0*M_PI);
  2788. y[i] = x[i] * sin(p->lastY);
  2789. p->lastX = x[i];
  2790. }
  2791. }
  2792. cmRC_t cmDistDsExec( cmDistDs* p, const cmSample_t* x, cmSample_t* y, unsigned n )
  2793. {
  2794. if( x == NULL )
  2795. {
  2796. if( y != NULL )
  2797. cmVOS_Zero(y,n);
  2798. return cmOkRC;
  2799. }
  2800. if( p->bypassFl )
  2801. {
  2802. cmVOS_Copy(y,n,x);
  2803. return cmOkRC;
  2804. }
  2805. unsigned maxVal = cmMax(2,(unsigned)round(pow(2.0,p->bits)));
  2806. double incr = 1.0; // p->downSrate / p->srate;
  2807. unsigned i;
  2808. enum { kNoRectId, kFullRectId, kHalfRectId };
  2809. unsigned rectCode = kNoRectId;
  2810. //cmSample_t t[n];
  2811. //_cmDistDsExpr0(p,x,t,n);
  2812. //x = t;
  2813. if( p->rectFl )
  2814. {
  2815. if( p->fullFl )
  2816. rectCode = kFullRectId;
  2817. else
  2818. rectCode = kHalfRectId;
  2819. }
  2820. double clipLevel = p->clipDb < -100.0 ? 0.0 : pow(10.0,p->clipDb/20.0);
  2821. for(i=0; i<n; ++i)
  2822. {
  2823. double ii = floor(p->fracIdx);
  2824. p->fracIdx += incr;
  2825. // if it is time to sample again
  2826. if( floor(p->fracIdx) != ii )
  2827. {
  2828. cmSample_t v = p->inGain * floor(x[i] * maxVal) / maxVal;
  2829. switch( rectCode )
  2830. {
  2831. case kFullRectId:
  2832. v = (cmSample_t)fabs(v);
  2833. break;
  2834. case kHalfRectId:
  2835. if( v < 0.0 )
  2836. v = 0.0;
  2837. break;
  2838. }
  2839. if( fabs(v) > clipLevel )
  2840. v = v<0.0 ? -clipLevel : clipLevel;
  2841. p->lastVal = v * p->outGain;
  2842. }
  2843. y[i] = p->lastVal;
  2844. }
  2845. return cmOkRC;
  2846. }