libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmProc3.c 89KB

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