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

cmProc3.h 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. #ifndef cmProc3_h
  2. #define cmProc3_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. typedef struct
  7. {
  8. double wi;
  9. double xi;
  10. int yn;
  11. cmSample_t* y;
  12. int yii;
  13. int yoi;
  14. int ynn;
  15. } cmPitchShiftOsc_t;
  16. typedef struct
  17. {
  18. cmObj obj;
  19. unsigned procSmpCnt;
  20. double srate;
  21. int outN; // procSmpCnt
  22. int wn; //
  23. int xn; //
  24. int bn; //
  25. cmSample_t* b; // b[bn]
  26. cmSample_t* x; // x[xn]
  27. cmSample_t* wnd; // wnd[wn]
  28. cmSample_t* outV; // outV[outN];
  29. int xni; //
  30. bool cubeFl; //
  31. cmPitchShiftOsc_t osc[2]; //
  32. } cmPitchShift;
  33. cmPitchShift* cmPitchShiftAlloc( cmCtx* c, cmPitchShift* p, unsigned procSmpCnt, cmReal_t srate );
  34. cmRC_t cmPitchShiftFree( cmPitchShift** pp );
  35. cmRC_t cmPitchShiftInit( cmPitchShift* p, unsigned procSmpCnt, cmReal_t srate );
  36. cmRC_t cmPitchShiftFinal(cmPitchShift* p );
  37. cmRC_t cmPitchShiftExec( cmPitchShift* p, const cmSample_t* x, cmSample_t* y, unsigned n, double shiftRatio, bool bypassFl );
  38. //=======================================================================================================================
  39. typedef struct
  40. {
  41. double xi; // index into xV[]
  42. double wi; // index into wV[]
  43. cmSample_t u; // cross-fade window polarity
  44. } cmLoopRecdOsc;
  45. typedef struct
  46. {
  47. cmLoopRecdOsc osc[2]; //
  48. cmSample_t* xV; // xV[ xN ]
  49. int xN; // maxRecdSmpCnt
  50. int xfN; // cross-fade sample count
  51. int xii; // xV[] recording input index
  52. cmSample_t* wV; // wV[ xN ] window function (actually contains xii values after recording)
  53. } cmLoopRecdBuf;
  54. typedef struct
  55. {
  56. cmObj obj;
  57. cmSample_t* bufMem;
  58. unsigned maxRecdSmpCnt;
  59. cmLoopRecdBuf* bufArray;
  60. unsigned bufArrayCnt;
  61. cmSample_t* outV;
  62. unsigned outN;
  63. unsigned procSmpCnt;
  64. unsigned xfadeSmpCnt;
  65. unsigned recdBufIdx;
  66. unsigned playBufIdx;
  67. bool recdFl;
  68. bool playFl;
  69. } cmLoopRecord;
  70. cmLoopRecord* cmLoopRecordAlloc( cmCtx* c, cmLoopRecord* p, unsigned procSmpCnt, unsigned maxRecdSmpCnt, unsigned xfadeSmpCnt );
  71. cmRC_t cmLoopRecordFree( cmLoopRecord** pp );
  72. cmRC_t cmLoopRecordInit( cmLoopRecord* p, unsigned procSmpCnt, unsigned maxRecdSmpCnt, unsigned xfadeSmpCnt );
  73. cmRC_t cmLoopRecordFinal( cmLoopRecord* p );
  74. // rgain=recorder output gain, pgain=pass through gain
  75. 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 );
  76. //=======================================================================================================================
  77. typedef struct
  78. {
  79. cmObj obj;
  80. unsigned rmsN;
  81. cmSample_t* rmsV; // rmsV[rmsN] previous rmsN values. rmsV[rmsN-1] == rms
  82. cmSample_t rms; // RMS of last procSmpCnt samples
  83. unsigned wndN;
  84. cmSample_t* wndV;
  85. cmSample_t mean;
  86. cmSample_t d0;
  87. unsigned durSmpCnt; // duration of the current gate polarity in samples
  88. bool gateFl;
  89. bool deltaFl;
  90. cmReal_t onThreshPct;
  91. cmReal_t onThreshDb;
  92. cmReal_t offThreshDb; //
  93. } cmGateDetect;
  94. cmGateDetect* cmGateDetectAlloc( cmCtx* c, cmGateDetect* p, unsigned procSmpCnt, cmReal_t onThreshPct, cmReal_t onThreshDb, cmReal_t offThreshDb );
  95. cmRC_t cmGateDetectFree( cmGateDetect** p );
  96. cmRC_t cmGateDetectInit( cmGateDetect* p, unsigned procSmpCnt, cmReal_t onThreshPct, cmReal_t onThreshDb, cmReal_t offThreshDb );
  97. cmRC_t cmGateDetectFinal(cmGateDetect* p );
  98. cmRC_t cmGateDetectExec( cmGateDetect* p, const cmSample_t* x, unsigned xn );
  99. //=======================================================================================================================
  100. typedef struct
  101. {
  102. unsigned medCnt; // length of the median filter
  103. unsigned avgCnt; // length of the (rms - med) moving avg. filter
  104. unsigned suprCnt; // length of the supression window
  105. unsigned offCnt; // length of the offset detection window
  106. cmReal_t suprCoeff; // supression signal shape coeff
  107. cmReal_t onThreshDb; // onset threshold
  108. cmReal_t offThreshDb; // offset threshold
  109. } cmGateDetectParams;
  110. typedef struct
  111. {
  112. cmObj obj;
  113. cmGateDetectParams args;
  114. cmSample_t* medV; // medV[medCnt]
  115. unsigned medIdx;
  116. cmSample_t* avgV; // avgV[avgCnt]
  117. unsigned avgIdx;
  118. cmSample_t* fcofV; // fcofV[medCnt]
  119. cmSample_t* fdlyV; // fdlyV[medCnt]
  120. cmSample_t* suprV; // suprV[suprCnt]
  121. unsigned suprIdx;
  122. cmSample_t* pkV; // pkV[3]
  123. cmSample_t* offV; // offV[offCnt]
  124. unsigned offIdx;
  125. unsigned pkFl; //
  126. bool gateFl; // set by onset, cleared by offset
  127. bool onFl; // set if an onset was detected
  128. bool offFl; // set if an offset was detected
  129. cmReal_t onThresh;
  130. cmReal_t offThresh;
  131. cmSample_t rms; // RMS
  132. cmSample_t med; // median RMS over last medCnt exec's
  133. cmSample_t dif; // max(0, RMS - median_RMS)
  134. cmSample_t avg; // avg dif's over last avgCnt exec's
  135. cmSample_t ons; // dif - avg
  136. cmSample_t flt; // filtered(ons)
  137. cmSample_t sup; // flt w/ suppression
  138. } cmGateDetect2;
  139. cmGateDetect2* cmGateDetectAlloc2( cmCtx* c, cmGateDetect2* p, unsigned procSmpCnt, const cmGateDetectParams* args );
  140. cmRC_t cmGateDetectFree2( cmGateDetect2** p );
  141. cmRC_t cmGateDetectInit2( cmGateDetect2* p, unsigned procSmpCnt, const cmGateDetectParams* args );
  142. cmRC_t cmGateDetectFinal2(cmGateDetect2* p );
  143. cmRC_t cmGateDetectExec2( cmGateDetect2* p, const cmSample_t* x, unsigned xn );
  144. void cmGateDetectSetOnThreshDb2( cmGateDetect2* p, cmReal_t db );
  145. void cmGateDetectSetOffThreshDb2( cmGateDetect2* p, cmReal_t db );
  146. //=======================================================================================================================
  147. //
  148. // Calculate a set of automatic gain adjustments for a set of audio channels.
  149. //
  150. // 1) Call cmAutoGainInit() to reset the object.
  151. // 2) Call cmAutoGainStartCh() and provide an id to identify the channel.
  152. // 3) Call cmAutoGainProcCh() with audio from the channel identified in 2)
  153. // 4) Repeat 2) and 3) for for all channels.
  154. // 5) Call cmAutoGainCalcGains() to set the value of cmAutoGainCh.gain.
  155. //
  156. // The gain coefficents are set to balance the overall gain.
  157. // (i.e. Loud channels are decreased and quiet channels are increased.)
  158. typedef struct
  159. {
  160. unsigned id; // channel id
  161. cmReal_t gain; // gain adjustment coefficient
  162. unsigned onCnt; // count of onsets detected
  163. unsigned offCnt; // count of offsets detected
  164. cmReal_t gateMaxAvg; // average of the max values for each detected event
  165. } cmAutoGainCh;
  166. typedef struct
  167. {
  168. cmObj obj;
  169. cmShiftBuf* sbp;
  170. cmGateDetect2* gdp;
  171. cmAutoGainCh* chArray; //
  172. unsigned chCnt;
  173. cmAutoGainCh* chp;
  174. unsigned gateCnt;
  175. cmReal_t gateSum;
  176. cmReal_t gateMax;
  177. cmReal_t minRms;
  178. } cmAutoGain;
  179. cmAutoGain* cmAutoGainAlloc( cmCtx* c, cmAutoGain* p, unsigned procSmpCnt, cmReal_t srate, cmReal_t hopMs, unsigned chCnt, const cmGateDetectParams* gd_args );
  180. cmRC_t cmAutoGainFree( cmAutoGain** p );
  181. cmRC_t cmAutoGainInit( cmAutoGain* p, unsigned procSmpCnt, cmReal_t srate, cmReal_t hopMs, unsigned chCnt, const cmGateDetectParams* gd_args );
  182. cmRC_t cmAutoGainFinal( cmAutoGain* p );
  183. // Notify the object that the following calls to cmAutoGainProcCh()
  184. // should be associed with the channel 'id'.
  185. cmRC_t cmAutoGainStartCh( cmAutoGain* p, unsigned id );
  186. // Examine the signal arriving from the channel specified in the previous
  187. // call to cmAutoGainProcCh() and determine the max RMS value for each
  188. // event contained in the signal.
  189. cmRC_t cmAutoGainProcCh( cmAutoGain* p, const cmSample_t* x, unsigned xn );
  190. // Calculate the cmAutoGainCh.gain coefficient for each channel.
  191. cmRC_t cmAutoGainCalcGains( cmAutoGain* p );
  192. void cmAutoGainPrint( cmAutoGain* p, cmRpt_t* rpt );
  193. //=======================================================================================================================
  194. typedef struct
  195. {
  196. unsigned ch;
  197. unsigned ssi;
  198. const cmChar_t* pitchStr;
  199. unsigned midi;
  200. cmReal_t gain;
  201. bool nsFl; // noise shaper channel
  202. bool cdFl; // chord detector channel
  203. } cmChCfgCh;
  204. typedef struct
  205. {
  206. cmObj obj;
  207. cmChCfgCh* chArray;
  208. unsigned chCnt;
  209. unsigned nsChCnt; // count of noise-shaper channels
  210. const cmChar_t* fn;
  211. cmJsonH_t jsH;
  212. cmJsonNode_t* cap;
  213. } cmChCfg;
  214. cmChCfg* cmChCfgAlloc( cmCtx* c, cmChCfg* p, cmCtx_t* ctx, const cmChar_t* fn );
  215. cmRC_t cmChCfgFree( cmChCfg** pp );
  216. cmRC_t cmChCfgInit( cmChCfg* p, cmCtx_t* ctx, const cmChar_t* fn );
  217. cmRC_t cmChCfgFinal( cmChCfg* p );
  218. cmRC_t cmChCfgWrite( cmChCfg* p );
  219. void cmChCfgPrint( cmChCfg* p, cmRpt_t* rpt );
  220. unsigned cmChCfgChannelCount( cmCtx_t* ctx, const cmChar_t* fn, unsigned* nsChCntPtr );
  221. unsigned cmChCfgChannelIndex( cmCtx_t* ctx, const cmChar_t* fn, unsigned chIdx );
  222. //=======================================================================================================================
  223. typedef struct
  224. {
  225. bool readyFl; // This channel has received an offset since it was last a candidate.
  226. bool candFl; // This channel is a chord candidate
  227. unsigned candSmpTime; // Time that this channel became a candidate
  228. cmReal_t candRMS; // RMS when this channel became a candidate
  229. bool chordFl; // This channel is part of the current chord
  230. bool gateFl; // Previous gate state
  231. } cmChordDetectCh;
  232. typedef struct
  233. {
  234. cmObj obj;
  235. unsigned maxTimeSpanSmpCnt; // maximum time between onsets of first and last note of chord
  236. unsigned minNotesPerChord; // the min. number of notes required to form a chord
  237. unsigned chCnt; // count of channels
  238. cmChordDetectCh* chArray; // channel state array
  239. bool detectFl; // true when a new chord has been detected
  240. unsigned timeSmp; // current time
  241. cmReal_t srate;
  242. } cmChordDetect;
  243. cmChordDetect* cmChordDetectAlloc( cmCtx*c, cmChordDetect* p, cmReal_t srate, unsigned chCnt, cmReal_t maxTimeSpanMs, unsigned minNotesPerChord );
  244. cmRC_t cmChordDetectFree( cmChordDetect** pp );
  245. cmRC_t cmChordDetectInit( cmChordDetect* p, cmReal_t srate, unsigned chCnt, cmReal_t maxTimeSpanMs, unsigned minNotesPerChord );
  246. cmRC_t cmChordDetectFinal( cmChordDetect* p );
  247. cmRC_t cmChordDetectExec( cmChordDetect* p, unsigned procSmpCnt, const bool* gateV, const cmReal_t* rmsV, unsigned chCnt );
  248. cmRC_t cmChordDetectSetSpanMs( cmChordDetect* p, cmReal_t maxTimeSpanMs );
  249. //=======================================================================================================================
  250. // This object is not really a cross-fader. It is really just a multichannel
  251. // fader - which just calculates the fade gain but does not actually apply it
  252. // to the audio signal - unless you use cmXfaderExecAudio()
  253. typedef struct
  254. {
  255. cmReal_t ep_gain;
  256. cmReal_t gain;
  257. bool gateFl; // true if channel is on
  258. bool onFl; // true if gateFl transitioned to true on this cycle
  259. bool offFl; // true if gateFl transitioned to false on this cycle
  260. } cmXfaderCh;
  261. typedef struct
  262. {
  263. cmObj obj;
  264. unsigned chCnt;
  265. cmXfaderCh* chArray;
  266. unsigned fadeSmpCnt;
  267. unsigned fadeInSmpCnt;
  268. unsigned fadeOutSmpCnt;
  269. cmReal_t srate;
  270. bool gateFl; // true if any channels are on
  271. bool onFl; // true on cycle where gate transitions to 'on'.
  272. bool offFl; // true on cycle where gate transitions to 'off'.
  273. } cmXfader;
  274. cmXfader* cmXfaderAlloc( cmCtx*c, cmXfader* p, cmReal_t srate, unsigned chCnt, cmReal_t fadeTimeMs );
  275. cmRC_t cmXfaderFree( cmXfader** pp );
  276. cmRC_t cmXfaderInit( cmXfader* p, cmReal_t srate, unsigned chCnt, cmReal_t fadeTimeMs );
  277. cmRC_t cmXfaderFinal( cmXfader* p );
  278. cmRC_t cmXfaderExec( cmXfader* p, unsigned procSmpCnt, const bool* gateV, unsigned chCnt );
  279. cmRC_t cmXfaderExecAudio( cmXfader* p, unsigned procSmpCnt, const bool* gateV, unsigned chCnt, const cmSample_t* x[], cmSample_t* y );
  280. void cmXfaderSetXfadeTime( cmXfader* p, cmReal_t fadeTimeMs );
  281. void cmXfaderSetXfadeInTime( cmXfader* p, cmReal_t fadeTimeMs );
  282. void cmXfaderSetXfadeOutTime( cmXfader* p, cmReal_t fadeTimeMs );
  283. // Set all gates to false except chIdx.
  284. void cmXfaderSelectOne( cmXfader* p, unsigned chIdx );
  285. void cmXfaderAllOff( cmXfader* p );
  286. //=======================================================================================================================
  287. // This fader object accepts a gate signal. When the gate is high it increments
  288. // the gain until it reaches 1.0. When the gate is low it decrements the gain
  289. // until it reaches 0.0. The fade time is the lenght of time the gain will take
  290. // to transition from 0.0 to 1.0 or 1.0 to 0.0.
  291. typedef struct
  292. {
  293. cmObj obj;
  294. unsigned fadeSmpCnt; // time to fade from 0->1 or 1->0
  295. cmReal_t srate;
  296. cmReal_t gain;
  297. } cmFader;
  298. cmFader* cmFaderAlloc( cmCtx*c, cmFader* p, cmReal_t srate, cmReal_t fadeTimeMs );
  299. cmRC_t cmFaderFree( cmFader** pp );
  300. cmRC_t cmFaderInit( cmFader* p, cmReal_t srate, cmReal_t fadeTimeMs );
  301. cmRC_t cmFaderFinal( cmFader* p );
  302. cmRC_t cmFaderExec( cmFader* p, unsigned procSmpCnt, bool gateFl, bool mixFl, const cmSample_t* x, cmSample_t* y );
  303. void cmFaderSetFadeTime( cmFader* p, cmReal_t fadeTimeMs );
  304. //=======================================================================================================================
  305. struct cmIDelay_str;
  306. typedef struct
  307. {
  308. cmObj obj;
  309. cmReal_t srate; // system sample rate
  310. bool feedbackFl; // set if this is a feedback comb filter
  311. cmReal_t minHz; // lowest comb frequency this comb filter can support
  312. cmReal_t hz; // location of first comb
  313. cmReal_t alpha; // filter coeff.
  314. cmReal_t dN; // max length of the the cf delay line
  315. unsigned dn; // current length of cf delay line
  316. cmReal_t* d; // d[dn] filter delay line
  317. cmReal_t* b; // b[dn] feedforward coeff's
  318. cmReal_t* a; // a[dn] feedback coeff's
  319. cmReal_t b0; // feedforward coeff 0
  320. bool bypassFl; // bypass enable flag
  321. struct cmIDelay_str* idp;
  322. } cmCombFilt;
  323. cmCombFilt* cmCombFiltAlloc( cmCtx* c, cmCombFilt* p, cmReal_t srate, bool feedbackFl, cmReal_t minHz, cmReal_t alpha, cmReal_t hz, bool bypassFl );
  324. cmRC_t cmCombFiltFree( cmCombFilt** pp);
  325. cmRC_t cmCombFiltInit( cmCombFilt* p, cmReal_t srate, bool feedbackFl, cmReal_t minHz, cmReal_t alpha, cmReal_t hz, bool bypassFl );
  326. cmRC_t cmCombFiltFinal( cmCombFilt* p );
  327. cmRC_t cmCombFiltExec( cmCombFilt* p, const cmSample_t* x, cmSample_t* y, unsigned n );
  328. void cmCombFiltSetAlpha( cmCombFilt* p, cmReal_t alpha );
  329. cmRC_t cmCombFiltSetHz( cmCombFilt* p, cmReal_t hz );
  330. //=======================================================================================================================
  331. typedef struct
  332. {
  333. cmObj obj;
  334. cmReal_t d[2]; //
  335. cmReal_t b[1]; //
  336. cmReal_t a[1]; // a[dn] feedback coeff's
  337. cmReal_t b0; // feedforward coeff 0
  338. bool bypassFl;
  339. } cmDcFilt;
  340. cmDcFilt* cmDcFiltAlloc( cmCtx* c, cmDcFilt* p, bool bypassFl );
  341. cmRC_t cmDcFiltFree( cmDcFilt** pp);
  342. cmRC_t cmDcFiltInit( cmDcFilt* p, bool bypassFl );
  343. cmRC_t cmDcFiltFinal( cmDcFilt* p );
  344. cmRC_t cmDcFiltExec( cmDcFilt* p, const cmSample_t* x, cmSample_t* y, unsigned n );
  345. //=======================================================================================================================
  346. // interpolating delay - used by the comb filter
  347. typedef struct cmIDelay_str
  348. {
  349. cmObj obj;
  350. cmSample_t* d; // d[dn] delay line
  351. int dn; // sizeo of delay
  352. cmSample_t* m; // memory buffer
  353. int mn; // size of memory bufer (dn+3)
  354. int ii; // input index
  355. unsigned tn; // count of taps
  356. cmReal_t* ti; // ti[tn] tap locations (fractional delay from t->ii)
  357. cmReal_t* tff; // tg[tn] tap out gain
  358. cmReal_t* tfb;// tfb[tn] tap feedback gain
  359. cmReal_t srate;
  360. } cmIDelay;
  361. 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 );
  362. cmRC_t cmIDelayFree( cmIDelay** pp );
  363. 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 );
  364. cmRC_t cmIDelayFinal(cmIDelay* p );
  365. cmRC_t cmIDelayExec( cmIDelay* p, const cmSample_t* x, cmSample_t* y, unsigned n );
  366. cmRC_t cmIDelaySetTapMs( cmIDelay* p, unsigned tapIdx, cmReal_t tapMs );
  367. //=======================================================================================================================
  368. // This object sequentially assigns channels to groups when their gates go high.
  369. // 'chsPerGroup' channels will be assigned to each group. No channel will be
  370. // assigned to any group unless there are at least 'chsPerGroup' available
  371. // (unassigned) channels.
  372. // Channels are released from groups when one of the member channels gates goes low.
  373. //
  374. typedef struct
  375. {
  376. cmReal_t rms; // current rms of this input channel
  377. bool gateFl; // current gate state of this input channel
  378. bool readyFl; // this channel is available to be assigned to a group
  379. bool offsetFl; // the gate went low during this cycle (cleared on exec)
  380. unsigned groupIdx; // group this channel is assigned to or cmInvalidIdx if it is not assigned to any group
  381. } cmGroupSelCh;
  382. typedef struct
  383. {
  384. unsigned* chIdxArray; // chIdxArray[p->chCnt] array of indexes to channels assigned to this group
  385. unsigned chIdxCnt; // count indexes in chIdxArray[] or 0 if the channel is not in use
  386. bool releaseFl; // true during the cycle that this group was released on
  387. bool createFl; // true during the cycle that this group was created on
  388. } cmGroupSelGrp;
  389. typedef struct
  390. {
  391. cmObj obj;
  392. cmGroupSelCh* chArray; // chArray[chCnt]
  393. unsigned chCnt; // count of channels
  394. cmGroupSelGrp* groupArray; // groupArray[groupCnt]
  395. unsigned groupCnt; // count of groups - must be <= chCnt - can be changed at any time
  396. unsigned chsPerGroup; // channels per group
  397. bool updateFl; // set during exec if channels were assigned or released
  398. } cmGroupSel;
  399. cmGroupSel* cmGroupSelAlloc( cmCtx* c, cmGroupSel* p, unsigned chCnt, unsigned groupCnt, unsigned chsPerGroup );
  400. cmRC_t cmGroupSelFree( cmGroupSel** pp );
  401. cmRC_t cmGroupSelInit( cmGroupSel* p, unsigned chCnt, unsigned groupCnt, unsigned chsPerGroup );
  402. cmRC_t cmGroupSelFinal( cmGroupSel* p );
  403. cmRC_t cmGroupSetChannelGate( cmGroupSel* p, unsigned chIdx, bool gateFl );
  404. cmRC_t cmGroupSetChannelRMS( cmGroupSel* p, unsigned chIdx, cmReal_t rms );
  405. // After exec if the p->updateFl is set then iterate through
  406. // p->groupArray[]. Groups that have been created will have their 'createFl' set
  407. // and groups that will be removed on the next cycle have their 'releaseFl' set.
  408. cmRC_t cmGroupSelExec( cmGroupSel* p );
  409. //=======================================================================================================================
  410. // Route N of M input channels to N output channels.
  411. // The N channels are selected from the first N gates to go high.
  412. typedef struct cmAudioNofM_In_str
  413. {
  414. bool gateFl;
  415. bool onsetFl;
  416. bool offsetFl;
  417. unsigned outChIdx;
  418. cmFader* fader;
  419. struct cmAudioNofM_In_str* link;
  420. } cmAudioNofM_In;
  421. typedef struct
  422. {
  423. struct cmAudioNofM_In_str* list;
  424. } cmAudioNofM_Out;
  425. typedef struct
  426. {
  427. cmObj obj;
  428. unsigned iChCnt; // (M) input channel count
  429. cmAudioNofM_In* inArray; // chArray[ M ] - input channel array
  430. unsigned oChCnt; // (N) output channel count
  431. cmAudioNofM_Out* outArray; // outArray[N] - output channel array
  432. unsigned nxtOutChIdx; // ch assoc;d with the next onset gate will be assined to this output channel
  433. } cmAudioNofM;
  434. cmAudioNofM* cmAudioNofMAlloc( cmCtx* c, cmAudioNofM* p, cmReal_t srate, unsigned iChCnt, unsigned oChCnt, cmReal_t fadeTimeMs );
  435. cmRC_t cmAudioNofMFree( cmAudioNofM** pp );
  436. cmRC_t cmAudioNofMInit( cmAudioNofM* p, cmReal_t srate, unsigned iChCnt, unsigned oChCnt, cmReal_t fadeTimeMs );
  437. cmRC_t cmAudioNofMFinal( cmAudioNofM* p );
  438. cmRC_t cmAudioNofMSetChannelGate( cmAudioNofM* p, unsigned inChIdx, bool gateFl );
  439. // Sum the audio contained in x[inChCnt][n] into y[outChCnt][n] according
  440. // to the state of the object.
  441. // Notes
  442. // 1) y[n] should be zeroed by the caller as the output is summed into this buffer.
  443. // 2) inChCnt should equal p->iChCnt and outChCnt should equal p->oChCnt
  444. cmRC_t cmAudioNofMExec( cmAudioNofM* p, const cmSample_t* x[], unsigned inChCnt, cmSample_t* y[], unsigned outChCnt, unsigned n );
  445. cmRC_t cmAudioNofMSetFadeMs( cmAudioNofM* p, cmReal_t fadeTimeMs );
  446. //=======================================================================================================================
  447. enum { kDlyAdsrId, kAtkAdsrId, kDcyAdsrId, kSusAdsrId, kRlsAdsrId, kDoneAdsrId };
  448. typedef struct
  449. {
  450. cmObj obj;
  451. cmReal_t srate;
  452. bool trigModeFl; // gate on triggers start, gate-off ignored
  453. cmReal_t levelMin;
  454. cmReal_t scaleDur; //
  455. int dlySmp;
  456. int atkSmp;
  457. cmReal_t atkLevel;
  458. int dcySmp;
  459. int susSmp; // only used in trigger mode
  460. cmReal_t susLevel;
  461. int rlsSmp;
  462. unsigned state; // current state
  463. int durSmp; // time in current state
  464. cmReal_t level; // current level
  465. bool gateFl; // last gate state
  466. cmReal_t atkBegLevel; // attack starting level
  467. cmReal_t atkDurSmp; // attack duration
  468. cmReal_t rlsLevel; // release starting level
  469. cmReal_t rlsDurSmp; // release duration
  470. cmReal_t actAtkLevel;
  471. cmReal_t actSusLevel;
  472. } cmAdsr;
  473. 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 );
  474. cmRC_t cmAdsrFree( cmAdsr** pp );
  475. 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 );
  476. cmRC_t cmAdsrFinal( cmAdsr* p );
  477. cmReal_t cmAdsrExec( cmAdsr* p, unsigned procSmpCnt, bool gateFl, cmReal_t tscale, cmReal_t ascale );
  478. void cmAdsrSetTime( cmAdsr* p, cmReal_t ms, unsigned id );
  479. void cmAdsrSetLevel( cmAdsr* p, cmReal_t level, unsigned id );
  480. void cmAdsrReport( cmAdsr* p, cmRpt_t* rpt );
  481. //=======================================================================================================================
  482. enum { kAtkCompId, kRlsCompId };
  483. typedef struct
  484. {
  485. cmObj obj;
  486. cmReal_t srate; // system sample rate
  487. unsigned procSmpCnt; // samples per exec cycle
  488. cmReal_t inGain; // input gain
  489. cmReal_t threshDb; // threshold in dB (max:100 min:0)
  490. cmReal_t ratio_num; // numerator of the ratio
  491. unsigned atkSmp; // time to reduce the signal by 10.0 db
  492. unsigned rlsSmp; // time to increase the signal by 10.0 db
  493. cmReal_t outGain; // makeup gain
  494. bool bypassFl; // bypass enable
  495. cmSample_t* rmsWnd; // rmsWnd[rmsWndAllocCnt]
  496. unsigned rmsWndAllocCnt; //
  497. unsigned rmsWndCnt; // current RMS window size (rmsWndCnt must be <= rmsWndAllocCnt)
  498. unsigned rmsWndIdx; // next RMS window input index
  499. unsigned state; // env. state
  500. cmReal_t rmsDb; // current incoming signal RMS (max:100 min:0)
  501. cmReal_t gain; // current compressor gain
  502. cmReal_t timeConstDb; // the atk/rls will incr/decr by 'timeConstDb' per atkMs/rlsMs.
  503. cmReal_t pkDb; //
  504. cmReal_t accumDb; //
  505. } cmCompressor;
  506. 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, cmReal_t atkMs, cmReal_t rlsMs, cmReal_t outGain, bool bypassFl );
  507. cmRC_t cmCompressorFree( cmCompressor** pp );
  508. 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, cmReal_t atkMs, cmReal_t rlsMs, cmReal_t outGain, bool bypassFl );
  509. cmRC_t cmCompressorFinal( cmCompressor* p );
  510. cmRC_t cmCompressorExec( cmCompressor* p, const cmSample_t* x, cmSample_t* y, unsigned n );
  511. void cmCompressorSetAttackMs( cmCompressor* p, cmReal_t ms );
  512. void cmCompressorSetReleaseMs( cmCompressor* p, cmReal_t ms );
  513. void cmCompressorSetThreshDb( cmCompressor* p, cmReal_t thresh );
  514. void cmCompressorSetRmsWndMs( cmCompressor* p, cmReal_t ms );
  515. //=======================================================================================================================
  516. // BiQuad Audio Eq's based on Robert Bristow-Johnson's recipes.
  517. // http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  518. // See filter_rbj.m for equivalent octave code.
  519. enum
  520. {
  521. kLpfBqId,
  522. kHpFBqId,
  523. kBpfBqId,
  524. kNotchBqId,
  525. kAllpassBqId,
  526. kPeakBqId,
  527. kLowShelfBqId,
  528. kHighShelfBqId
  529. };
  530. typedef struct
  531. {
  532. cmObj obj;
  533. cmReal_t srate;
  534. unsigned mode;
  535. cmReal_t f0Hz;
  536. cmReal_t Q;
  537. cmReal_t gainDb;
  538. cmReal_t d[4];
  539. cmReal_t b[3];
  540. cmReal_t a[3];
  541. bool bypassFl;
  542. } cmBiQuadEq;
  543. cmBiQuadEq* cmBiQuadEqAlloc( cmCtx* c, cmBiQuadEq* p, cmReal_t srate, unsigned mode, cmReal_t f0Hz, cmReal_t Q, cmReal_t gainDb, bool bypassFl );
  544. cmRC_t cmBiQuadEqFree( cmBiQuadEq** pp );
  545. cmRC_t cmBiQuadEqInit( cmBiQuadEq* p, cmReal_t srate, unsigned mode, cmReal_t f0Hz, cmReal_t Q, cmReal_t gainDb, bool bypassFl );
  546. cmRC_t cmBiQuadEqFinal( cmBiQuadEq* p );
  547. cmRC_t cmBiQuadEqExec( cmBiQuadEq* p, const cmSample_t* x, cmSample_t* y, unsigned n );
  548. void cmBiQuadEqSet( cmBiQuadEq* p, unsigned mode, cmReal_t f0Hz, cmReal_t Q, cmReal_t gainDb );
  549. //=======================================================================================================================
  550. typedef struct
  551. {
  552. cmObj obj;
  553. cmReal_t srate;
  554. cmReal_t downSrate;
  555. cmReal_t bits;
  556. bool rectFl;
  557. bool fullFl;
  558. cmReal_t clipDb;
  559. cmReal_t inGain;
  560. cmReal_t outGain;
  561. bool bypassFl;
  562. double fracIdx;
  563. cmSample_t lastVal;
  564. cmSample_t lastY;
  565. cmSample_t lastX;
  566. } cmDistDs;
  567. 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 );
  568. cmRC_t cmDistDsFree( cmDistDs** p );
  569. 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 );
  570. cmRC_t cmDistDsFinal( cmDistDs* p );
  571. cmRC_t cmDistDsExec( cmDistDs* p, const cmSample_t* x, cmSample_t* y, unsigned n );
  572. //=======================================================================================================================
  573. /*
  574. typedef struct
  575. {
  576. cmObj obj;
  577. } cmUnitDelay;
  578. cmUnitDelay* cmUnitDelayAlloc( cmCtx* c, cmUnitDelay* p, cmReal_t srate, unsigned smpCnt, unsigned inCnt, unsigned outCnt, const cmReal_t delayMsV, unsigned delayCnt );
  579. cmRC_t cmUnitDelayFree( cmUnitDelay* p );
  580. cmRC_t cmUnitDelayInit( cmUnitDelay* p, cmReal_t srate, unsigned smpCnt, braunsigned inCnt, unsigned outCnt, const cmReal_t delayMsV, unsigned delayCnt );
  581. cmRC_t cmUnitDelayFinal( cmUnitDelay* p );
  582. cmRC_t cmUnitDelayExec( cmUnitDelay* p, const cmSample_t** x, unsigned inChCnt, cmSample_t** y, unsigned outChCnt, unsigned smpCnt );
  583. */
  584. #ifdef __cplusplus
  585. }
  586. #endif
  587. #endif