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.

cmProc2.h 55KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. #ifndef cmProc2_h
  2. #define cmProc2_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //------------------------------------------------------------------------------------------------------------
  7. // cmArray is an expandable array designed to work easily with the alloc/init/final/free model
  8. // used by this library. The arrays can be safely used by using the cmArrayAllocXXX macros
  9. // with static cmArray member fields during object allocation. cmArrayResizeXXX macros are then
  10. // used during the object initialization phase to allocate the actual array data space. Notice that
  11. // when used this way there is no need to call cmArrayFinal() prior to cmArrayResizeXXX().
  12. // The data memory used by cmArray's is allocated through the cmAllocData() and cmAllocDataZ()
  13. // macros. The resulting base memory address is therefore guaranteed to be aligned to a
  14. // 16 byte address boundary.
  15. typedef struct
  16. {
  17. cmObj obj;
  18. char* ptr;
  19. unsigned allocByteCnt;
  20. unsigned eleCnt;
  21. unsigned eleByteCnt;
  22. } cmArray;
  23. enum
  24. {
  25. kZeroArrayFl = 0x01
  26. };
  27. cmArray* cmArrayAllocate( cmCtx* c, cmArray* p, unsigned eleCnt, unsigned eleByteCnt, unsigned flags );
  28. cmRC_t cmArrayFree( cmArray** pp );
  29. cmRC_t cmArrayInit( cmArray* p, unsigned eleCnt, unsigned eleByteCnt, unsigned flags );
  30. cmRC_t cmArrayFinal( cmArray* p );
  31. char* cmArrayReallocDestroy( cmArray* p, unsigned newEleCnt, unsigned newEleByteCnt, unsigned flags );
  32. void cmArrayReallocDestroyV(cmArray* p, int eleByteCnt,unsigned flags, ... );
  33. char* cmArrayReallocPreserve(cmArray* p, unsigned newEleCnt, unsigned newEleByteCnt, unsigned flags );
  34. #define cmArrayAlloc( c, p ) cmArrayAllocate(c,p,0,0,0);
  35. #define cmArrayAllocInit( c, p, eleCnt, type ) cmArrayAllocate(c,p,eleCnt,sizeof(type),0)
  36. #define cmArrayAllocInitZ( c, p, eleCnt, type ) cmArrayAllocate(c,p,eleCnt,sizeof(type),kZeroArrayFl)
  37. #define cmArrayResize( c, p, newEleCnt, type ) (type*)cmArrayReallocDestroy(c,p,newEleCnt,sizeof(type), 0 )
  38. #define cmArrayResizeZ( c, p, newEleCnt, type ) (type*)cmArrayReallocDestroy(c,p,newEleCnt,sizeof(type), kZeroArrayFl )
  39. #define cmArrayResizePreserve( c, p, newEleCnt, type ) (type*)cmArrayReallocPreserve(c,p,newEleCnt,sizeof(type), 0 )
  40. #define cmArrayResizePreserveZ(c, p, newEleCnt, type ) (type*)cmArrayReallocPreserve(c,p,newEleCnt,sizeof(type), kZeroArrayFl )
  41. #define cmArrayResizeV( c, p, type, ... ) cmArrayReallocDestroyV(c,p,sizeof(type),0,##__VA_ARGS__)
  42. #define cmArrayResizeVZ( c, p, type, ... ) cmArrayReallocDestroyV(c,p,sizeof(type),kZeroArrayFl,##__VA_ARGS__)
  43. #define cmArrayPtr( type, p ) (type*)(p)->ptr
  44. #define cmArrayCount( p ) (p)->eleCnt
  45. //------------------------------------------------------------------------------------------------------------
  46. typedef struct
  47. {
  48. cmObj obj;
  49. cmAudioFileH_t h;
  50. unsigned chCnt;
  51. unsigned curChCnt;
  52. unsigned procSmpCnt;
  53. char* fn;
  54. cmSample_t* bufV;
  55. } cmAudioFileWr;
  56. cmAudioFileWr* cmAudioFileWrAlloc( cmCtx* c, cmAudioFileWr* p, unsigned procSmpCnt, const char* fn, double srate, unsigned chCnt, unsigned bitsPerSample );
  57. cmRC_t cmAudioFileWrFree( cmAudioFileWr** pp );
  58. cmRC_t cmAudioFileWrInit( cmAudioFileWr* p, unsigned procSmpCnt, const char* fn, double srate, unsigned chCnt, unsigned bitsPerSample );
  59. cmRC_t cmAudioFileWrFinal( cmAudioFileWr* p );
  60. cmRC_t cmAudioFileWrExec( cmAudioFileWr* p, unsigned chIdx, const cmSample_t* sp, unsigned sn );
  61. void cmAudioFileWrTest();
  62. //------------------------------------------------------------------------------------------------------------
  63. typedef struct
  64. {
  65. cmObj obj;
  66. unsigned rn;
  67. unsigned cn;
  68. cmSample_t *bufPtr;
  69. } cmMatrixBuf;
  70. /// Set p to NULL to dynamically allocate the object
  71. cmMatrixBuf* cmMatrixBufAllocFile(cmCtx* c, cmMatrixBuf* p, const char* fn );
  72. cmMatrixBuf* cmMatrixBufAllocCopy(cmCtx* c, cmMatrixBuf* p, unsigned rn, unsigned cn, const cmSample_t* sp );
  73. cmMatrixBuf* cmMatrixBufAlloc( cmCtx* c, cmMatrixBuf* p, unsigned rn, unsigned cn );
  74. cmRC_t cmMatrixBufFree( cmMatrixBuf**p );
  75. cmRC_t cmMatrixBufInitFile( cmMatrixBuf* p, const char* fn );
  76. cmRC_t cmMatrixBufInitCopy( cmMatrixBuf* p, unsigned rn, unsigned cn, const cmSample_t* sp );
  77. cmRC_t cmMatrixBufInit( cmMatrixBuf* p, unsigned rn, unsigned cn );
  78. cmRC_t cmMatrixBufFinal( cmMatrixBuf* p );
  79. cmSample_t* cmMatrixBufColPtr( cmMatrixBuf* p, unsigned ci );
  80. cmSample_t* cmMatrixBufRowPtr( cmMatrixBuf* p, unsigned ri );
  81. void cmMatrixBufTest();
  82. //------------------------------------------------------------------------------------------------------------
  83. enum
  84. {
  85. kInvalidWfId,
  86. kSineWfId,
  87. kCosWfId,
  88. kSquareWfId,
  89. kTriangleWfId,
  90. kSawtoothWfId,
  91. kWhiteWfId,
  92. kPinkWfId,
  93. kPulseWfId,
  94. kImpulseWfId,
  95. kSilenceWfId,
  96. kPhasorWfId,
  97. kSeqWfId, // always incrementing integer sequence (srate,frq,otCnt is ignored)
  98. };
  99. typedef struct
  100. {
  101. cmObj obj;
  102. unsigned wfId;
  103. unsigned overToneCnt;
  104. double fundFrqHz;
  105. cmSample_t* outV;
  106. unsigned outN; // outN == procSmpCnt
  107. unsigned phase;
  108. cmSample_t delaySmp;
  109. double srate;
  110. } cmSigGen;
  111. /// Set p to NULL to dynamically allocate the object
  112. /// The last three arguments are optional. Set wfId to kInvalidWfId to allocate the signal generator without initializint it.
  113. cmSigGen* cmSigGenAlloc( cmCtx* c, cmSigGen* p, unsigned procSmpCnt, double srate, unsigned wfId, double fundFrqHz, unsigned overToneCnt );
  114. cmRC_t cmSigGenFree( cmSigGen** p );
  115. cmRC_t cmSigGenInit( cmSigGen* p, unsigned procSmpCnt, double srate, unsigned wfId, double fundFrqHz, unsigned overToneCnt );
  116. cmRC_t cmSigGenFinal( cmSigGen* p );
  117. cmRC_t cmSigGenExec( cmSigGen* p );
  118. //------------------------------------------------------------------------------------------------------------
  119. typedef struct
  120. {
  121. cmObj* obj;
  122. cmSample_t* bufPtr;
  123. unsigned bufSmpCnt; // count of samples in the delay line (bufSmpCnt = procSmpCnt+delaySmpCnt)
  124. unsigned procSmpCnt; // maximum legal samples to receive in a single call to cmDelayExec()
  125. unsigned delaySmpCnt; // delay time in samples
  126. int delayInIdx; // index into bufPtr[] of next element to receive an incoming sample
  127. unsigned outCnt; // count of valid buffers in outV[]
  128. cmSample_t* outV[2]; // pointers to output buffers
  129. unsigned outN[2]; // length of output buffers (the sum of the length of both output buffers is always procSmpCnt)
  130. } cmDelay;
  131. cmDelay* cmDelayAlloc( cmCtx* c, cmDelay* p, unsigned procSmpCnt, unsigned delaySmpCnt );
  132. cmRC_t cmDelayFree( cmDelay** p );
  133. cmRC_t cmDelayInit( cmDelay* p, unsigned procSmpCnt, unsigned delaySmpCnt );
  134. cmRC_t cmDelayFinal( cmDelay* p );
  135. cmRC_t cmDelayCopyIn( cmDelay* p, const cmSample_t* sp, unsigned sn );
  136. cmRC_t cmDelayAdvance( cmDelay* p, unsigned sn );
  137. cmRC_t cmDelayExec( cmDelay* p, const cmSample_t* sp, unsigned sn, bool bypassFl );
  138. void cmDelayTest();
  139. //------------------------------------------------------------------------------------------------------------
  140. typedef struct
  141. {
  142. cmObj obj;
  143. double* coeffV; // FIR coefficient vector (impulse response)
  144. unsigned coeffCnt; // count of elements in coeffV
  145. double* delayV; // delay vector contains one less elements than the coeff array
  146. cmSample_t* outV; // output signal
  147. unsigned outN; // length of the output signal (outN == ctx.procSmpCnt)
  148. unsigned delayIdx; // current next sample to receive input in the the delay line
  149. } cmFIR;
  150. enum { kHighPassFIRFl = 0x01 };
  151. // Note that the relative values of passHz and stopHz do not matter
  152. // for low-pass vs high-pass filters. In practice passHz and
  153. // stopHz can be swapped with no effect on the filter in either
  154. // case. Set p to NULL to dynamically allocate the object.
  155. cmFIR* cmFIRAllocKaiser(cmCtx* c, cmFIR* p, unsigned procSmpCnt, double srate, double passHz, double stopHz, double passDb, double stopDb, unsigned flags );
  156. // Set wndV[sincSmpCnt] to NULL to use a unity window otherwise set it to a window
  157. // function of length sincSmpCnt.
  158. cmFIR* cmFIRAllocSinc( cmCtx* c, cmFIR* p, unsigned procSmpCnt, double srate, unsigned sincSmpCnt, double fcHz, unsigned flags, const double* wndV );
  159. cmRC_t cmFIRFree( cmFIR** pp );
  160. cmRC_t cmFIRInitKaiser( cmFIR* p, unsigned procSmpCnt, double srate, double passHz, double stopHz, double passDb, double stopDb, unsigned flags );
  161. cmRC_t cmFIRInitSinc( cmFIR* p, unsigned procSmpCnt, double srate, unsigned sincSmpCnt, double fcHz, unsigned flags, const double* wndV );
  162. cmRC_t cmFIRFinal( cmFIR* p );
  163. cmRC_t cmFIRExec( cmFIR* p, const cmSample_t* sp, unsigned sn );
  164. void cmFIRTest0( cmRpt_t* rpt, cmLHeapH_t lhH, cmSymTblH_t stH );
  165. void cmFIRTest1( cmCtx* ctx );
  166. //------------------------------------------------------------------------------------------------------------
  167. // Apply a generic function to a windowed signal with a one sample hop size.
  168. typedef cmSample_t (*cmFuncFiltPtr_t)( const cmSample_t* sp, unsigned sn, void* userPtr );
  169. typedef struct
  170. {
  171. cmObj obj;
  172. cmFuncFiltPtr_t funcPtr;
  173. cmShiftBuf shiftBuf;
  174. cmSample_t* outV;
  175. unsigned outN; // outN == procSmpCnt
  176. unsigned curWndSmpCnt;
  177. unsigned wndSmpCnt;
  178. void* userPtr;
  179. } cmFuncFilter;
  180. /// Set p to NULL to dynamically allocate the object.
  181. cmFuncFilter* cmFuncFilterAlloc( cmCtx* c, cmFuncFilter* p, unsigned procSmpCnt, cmFuncFiltPtr_t funcPtr, void* userPtr, unsigned wndSmpCnt );
  182. cmRC_t cmFuncFilterFree( cmFuncFilter** pp );
  183. cmRC_t cmFuncFilterInit( cmFuncFilter* p, unsigned procSmpCnt, cmFuncFiltPtr_t funcPtr, void* userPtr, unsigned wndSmpCnt );
  184. cmRC_t cmFuncFilterFinal( cmFuncFilter* p );
  185. cmRC_t cmFuncFilterExec( cmFuncFilter* p, const cmSample_t* sp, unsigned sn );
  186. void cmFuncFilterTest();
  187. //------------------------------------------------------------------------------------------------------------
  188. typedef struct
  189. {
  190. cmObj obj;
  191. unsigned stateN; // count of states
  192. unsigned symN; // count of discrete observation symbols
  193. cmReal_t* initV; // initial state probability vector init[ stateN ]
  194. cmReal_t* transM; // transition probability matrix trans[ stateN (current), stateN (next) ]
  195. cmReal_t* stsM; // state to symbol prob. matrix stsM[ stateN, symN ]
  196. } cmDhmm;
  197. cmDhmm* cmDhmmAlloc( cmCtx* c, cmDhmm* p, unsigned stateN, unsigned symN, cmReal_t* initV, cmReal_t* transM, cmReal_t* stsM );
  198. cmRC_t cmDhmmFree( cmDhmm** pp );
  199. cmRC_t cmDhmmInit( cmDhmm* p, unsigned stateN, unsigned symN, cmReal_t* initV, cmReal_t* transM, cmReal_t* stsM );
  200. cmRC_t cmDhmmFinal( cmDhmm* p );
  201. cmRC_t cmDhmmExec( cmDhmm* p );
  202. cmRC_t cmDhmmGenObsSequence( cmDhmm* p, unsigned* dbp, unsigned dn );
  203. cmRC_t cmDhmmForwardEval( cmDhmm* p, const cmReal_t* statePrV, const unsigned* obsV, unsigned obsN, cmReal_t* alphaM, unsigned flags, cmReal_t* logProbPtr );
  204. cmRC_t cmDhmmReport( cmDhmm* p );
  205. void cmDhmmTest();
  206. //------------------------------------------------------------------------------------------------------------
  207. typedef struct
  208. {
  209. cmObj obj;
  210. cmFftSR* fft;
  211. cmIFftRS* ifft;
  212. cmComplexR_t* H;
  213. unsigned hn;
  214. cmSample_t* olaV; // olaV[hn-1];
  215. cmSample_t* outV; // outV[procSmpCnt]
  216. unsigned outN; // outN == procSmpCnt
  217. } cmConvolve;
  218. // After cmConvolveExec() outV[outN] contains the first outN samples
  219. // which are complete and can be used by the application.
  220. // The tail of the convolution is held in olaV[hn-1] and will
  221. // be automatically summed with the beginning of the next convolution
  222. // frame.
  223. // BUG BUG BUG
  224. // This code seems to have a problem when hn != procSmpCnt (or maybe hn > procSmpCnt ???).
  225. // See mas/main.c convolve() where procSmpCnt must be set to wndSmpCnt size or
  226. // only the first half of the window is emitted.
  227. // h[hn] is the impulse response to convolve with
  228. cmConvolve* cmConvolveAlloc( cmCtx* c, cmConvolve* p, const cmSample_t* h, unsigned hn, unsigned procSmpCnt );
  229. cmRC_t cmConvolveFree( cmConvolve** pp );
  230. cmRC_t cmConvolveInit( cmConvolve* p, const cmSample_t* h, unsigned hn, unsigned procSmpCnt );
  231. cmRC_t cmConvolveFinal( cmConvolve* p );
  232. // xn must be <= procSmpCnt
  233. cmRC_t cmConvolveExec( cmConvolve* p, const cmSample_t* x, unsigned xn );
  234. cmRC_t cmConvolveSignal( cmCtx* c, const cmSample_t* h, unsigned hn, const cmSample_t* x, unsigned xn, cmSample_t* y, unsigned yn );
  235. cmRC_t cmConvolveTest( cmRpt_t* rpt, cmLHeapH_t lhH, cmSymTblH_t stH );
  236. //------------------------------------------------------------------------------------------------------------
  237. typedef struct
  238. {
  239. cmObj obj;
  240. cmReal_t* dctMtx; // dctMtx[ binCnt, bandCnt ]
  241. cmReal_t* filtMask; // filtMask[ bandCnt, bandCnt ]
  242. unsigned binCnt; // bin cnt of input magnitude spectrum
  243. unsigned bandCnt; // must be <= kDefaultBarkBandCnt
  244. cmReal_t* outV; // outV[binCnt]
  245. } cmBfcc;
  246. cmBfcc* cmBfccAlloc( cmCtx* ctx, cmBfcc* p, unsigned bandCnt, unsigned binCnt, double binHz );
  247. cmRC_t cmBfccFree( cmBfcc** pp );
  248. cmRC_t cmBfccInit( cmBfcc* p, unsigned bandCnt, unsigned binCnt, double binHz );
  249. cmRC_t cmBfccFinal( cmBfcc* p );
  250. cmRC_t cmBfccExec( cmBfcc* p, const cmReal_t* magV, unsigned binCnt );
  251. void cmBfccTest( cmRpt_t* rpt, cmLHeapH_t lhH, cmSymTblH_t stH );
  252. //------------------------------------------------------------------------------------------------------------
  253. typedef struct
  254. {
  255. cmObj obj;
  256. //cmIFftRR ft;
  257. unsigned dct_cn; // (binCnt-1)*2
  258. cmReal_t* dctM; // dctM[ outN, dct_cn ]
  259. unsigned binCnt; // bin cnt of input magnitude spectrum
  260. unsigned outN; // count of cepstral coeff's
  261. cmReal_t* outV; // outV[outN]
  262. } cmCeps;
  263. // outN is the number of cepstral coeff's in the output vector
  264. cmCeps* cmCepsAlloc( cmCtx* ctx, cmCeps* p, unsigned binCnt, unsigned outN );
  265. cmRC_t cmCepsFree( cmCeps** pp );
  266. cmRC_t cmCepsInit( cmCeps* p, unsigned binCnt, unsigned outN );
  267. cmRC_t cmCepsFinal( cmCeps* p );
  268. cmRC_t cmCepsExec( cmCeps* p, const cmReal_t* magV, const cmReal_t* phsV, unsigned binCnt );
  269. //------------------------------------------------------------------------------------------------------------
  270. typedef struct
  271. {
  272. cmObj obj;
  273. cmWndFunc wf;
  274. unsigned wndSmpCnt;
  275. unsigned hopSmpCnt;
  276. unsigned procSmpCnt;
  277. cmSample_t* bufV; // bufV[wndSmpCnt] overlap add buffer
  278. cmSample_t* outV; // outV[hopSmpCnt] output vector
  279. cmSample_t* outPtr; // outPtr[procSmpCnt] output vector
  280. unsigned idx; // idx of next val in bufV[] to be moved to outV[]
  281. } cmOla;
  282. // hopSmpCnt must be <= wndSmpCnt.
  283. // hopSmpCnt must be an even multiple of procSmpCnt.
  284. // Call cmOlaExecR() or cmOlaExecS() at the spectral frame rate.
  285. // Call cmOlaExecOut() at the time domain audio frame rate.
  286. // Set wndTypeId to one of the cmWndFuncXXX enumerated widnow type id's.
  287. cmOla* cmOlaAlloc( cmCtx* ctx, cmOla* p, unsigned wndSmpCnt, unsigned hopSmpCnt, unsigned procSmpCnt, unsigned wndTypeId );
  288. cmRC_t cmOlaFree( cmOla** pp );
  289. cmRC_t cmOlaInit( cmOla* p, unsigned wndSmpCnt, unsigned hopSmpCnt, unsigned procSmpCnt, unsigned wndTypeId );
  290. cmRC_t cmOlaFinal( cmOla* p );
  291. cmRC_t cmOlaExecS( cmOla* p, const cmSample_t* xV, unsigned xN );
  292. cmRC_t cmOlaExecR( cmOla* p, const cmReal_t* xV, unsigned xN );
  293. const cmSample_t* cmOlaExecOut(cmOla* p );
  294. //------------------------------------------------------------------------------------------------------------
  295. typedef struct
  296. {
  297. cmObj obj;
  298. cmReal_t* hzV; // hzV[binCnt] output vector - frequency in Hertz
  299. cmReal_t* phsV; // phsV[binCnt]
  300. cmReal_t* wV; // bin freq in rads/hop
  301. double srate;
  302. unsigned hopSmpCnt;
  303. unsigned binCnt;
  304. } cmPhsToFrq;
  305. cmPhsToFrq* cmPhsToFrqAlloc( cmCtx* c, cmPhsToFrq* p, double srate, unsigned binCnt, unsigned hopSmpCnt );
  306. cmRC_t cmPhsToFrqFree( cmPhsToFrq** p );
  307. cmRC_t cmPhsToFrqInit( cmPhsToFrq* p, double srate, unsigned binCnt, unsigned hopSmpCnt );
  308. cmRC_t cmPhsToFrqFinal(cmPhsToFrq* p );
  309. cmRC_t cmPhsToFrqExec( cmPhsToFrq* p, const cmReal_t* phsV );
  310. //------------------------------------------------------------------------------------------------------------
  311. enum
  312. {
  313. kNoCalcHzPvaFl = 0x00,
  314. kCalcHzPvaFl = 0x01
  315. };
  316. typedef struct
  317. {
  318. cmObj obj;
  319. cmShiftBuf sb;
  320. cmFftSR ft;
  321. cmWndFunc wf;
  322. cmPhsToFrq pf;
  323. unsigned flags;
  324. unsigned procSmpCnt;
  325. double srate;
  326. unsigned wndSmpCnt;
  327. unsigned hopSmpCnt;
  328. unsigned binCnt;
  329. const cmReal_t* magV; // amplitude NOT power
  330. const cmReal_t* phsV;
  331. const cmReal_t* hzV;
  332. } cmPvAnl;
  333. cmPvAnl* cmPvAnlAlloc( cmCtx* ctx, cmPvAnl* p, unsigned procSmpCnt, double srate, unsigned wndSmpCnt, unsigned hopSmpCnt, unsigned flags );
  334. cmRC_t cmPvAnlFree( cmPvAnl** pp );
  335. cmRC_t cmPvAnlInit( cmPvAnl* p, unsigned procSmpCnt, double srate, unsigned wndSmpCnt, unsigned hopSmpCnt, unsigned flags );
  336. cmRC_t cmPvAnlFinal(cmPvAnl* p );
  337. // Returns true when a new spectrum has been computed
  338. bool cmPvAnlExec( cmPvAnl* p, const cmSample_t* x, unsigned xN );
  339. //------------------------------------------------------------------------------------------------------------
  340. typedef struct
  341. {
  342. cmObj obj;
  343. cmIFftRS ft;
  344. cmWndFunc wf;
  345. cmOla ola;
  346. cmReal_t* minRphV;
  347. cmReal_t* maxRphV;
  348. cmReal_t* itrV;
  349. cmReal_t* phs0V;
  350. cmReal_t* mag0V;
  351. cmReal_t* phsV;
  352. cmReal_t* magV;
  353. double outSrate;
  354. unsigned procSmpCnt;
  355. unsigned wndSmpCnt;
  356. unsigned hopSmpCnt;
  357. unsigned binCnt;
  358. } cmPvSyn;
  359. cmPvSyn* cmPvSynAlloc( cmCtx* ctx, cmPvSyn* p, unsigned procSmpCnt, double outSrate, unsigned wndSmpCnt, unsigned hopSmpCnt,unsigned wndTypeId );
  360. cmRC_t cmPvSynFree( cmPvSyn** pp );
  361. cmRC_t cmPvSynInit( cmPvSyn* p, unsigned procSmpCnt, double outSrate, unsigned wndSmpCnt, unsigned hopSmpCnt,unsigned wndTypeId );
  362. cmRC_t cmPvSynFinal(cmPvSyn* p );
  363. cmRC_t cmPvSynExec( cmPvSyn* p, const cmReal_t* magV, const cmReal_t* phsV );
  364. const cmSample_t* cmPvSynExecOut(cmPvSyn* p );
  365. //------------------------------------------------------------------------------------------------------------
  366. // callback selector values
  367. enum
  368. {
  369. kAttackMsId,
  370. kReleaseMsId,
  371. kDspMsId // return 0 if the voice is no longer active
  372. };
  373. // voice flags
  374. enum
  375. {
  376. kActiveMsFl = 0x01, // set if the voice is active
  377. kKeyGateMsFl = 0x02, // set if the key is down for this note
  378. };
  379. struct cmMidiSynth_str;
  380. struct cmMidiSynthCh_str;
  381. struct cmMidiVoice_str;
  382. // voice update callback - use voicePtr->pgm.cbDataPtr to get voice specific data
  383. typedef int (*cmMidiSynthCb_t)( struct cmMidiVoice_str* voicePtr, unsigned sel, cmSample_t* outChArray[], unsigned outChCnt );
  384. typedef struct
  385. {
  386. cmMidiByte_t pgm; // MIDI pgm number
  387. cmMidiSynthCb_t cbPtr; // voice update callback
  388. void* cbDataPtr; // user data pointer
  389. } cmMidiSynthPgm;
  390. typedef struct cmMidiVoice_str
  391. {
  392. unsigned index; // voice index
  393. unsigned flags; // see kXXXMsFl above
  394. cmMidiByte_t pitch; // note-on pitch
  395. cmMidiByte_t velocity; // note-on/off veloctiy
  396. cmMidiSynthPgm pgm; // pgm associated with this voice
  397. struct cmMidiSynthCh_str* chPtr; // pointer to owning ch
  398. struct cmMidiVoice_str* link; // link to next active/avail voice in chain
  399. } cmMidiVoice;
  400. typedef struct cmMidiSynthCh_str
  401. {
  402. cmMidiByte_t midiCtl[ kMidiCtlCnt ]; // current ctl values
  403. short pitchBend; // current pitch bend value
  404. cmMidiByte_t pgm; // last pgm received
  405. cmMidiVoice* active; // first active voice on this channel
  406. struct cmMidiSynth_str* synthPtr; // owning synth
  407. } cmMidiSynthCh;
  408. typedef struct cmMidiSynth_str
  409. {
  410. cmObj obj;
  411. cmMidiSynthCh chArray[ kMidiChCnt ]; // midi channel array
  412. unsigned voiceCnt; // count of voice records
  413. cmMidiVoice* avail; // avail voice chain
  414. unsigned activeVoiceCnt; // current count of active voices
  415. unsigned voiceStealCnt; // count of times voice stealing was required
  416. cmMidiVoice* voiceArray; // array of voice records
  417. cmMidiSynthPgm pgmArray[ kMidiPgmCnt ]; // array of pgm records
  418. unsigned procSmpCnt; // samples per DSP cycle
  419. unsigned outChCnt; // count of output channels
  420. cmSample_t* outM; // outM[ procSmpCnt, outChCnt ] output buffer
  421. cmSample_t** outChArray; // buffer of pointers to each output channel
  422. cmReal_t srate; // output signal sample rate
  423. } cmMidiSynth;
  424. cmMidiSynth* cmMidiSynthAlloc( cmCtx* ctx, cmMidiSynth* p, const cmMidiSynthPgm* pgmArray, unsigned pgmCnt, unsigned voiceCnt, unsigned procSmpCnt, unsigned outChCnt, cmReal_t srate );
  425. cmRC_t cmMidiSynthFree( cmMidiSynth** pp );
  426. cmRC_t cmMidiSynthInit( cmMidiSynth* p, const cmMidiSynthPgm* pgmArray, unsigned pgmCnt, unsigned voiceCnt, unsigned procSmpCnt, unsigned outChCnt, cmReal_t srate );
  427. cmRC_t cmMidiSynthFinal( cmMidiSynth* p );
  428. cmRC_t cmMidiSynthOnMidi(cmMidiSynth* p, const cmMidiPacket_t* pktArray, unsigned pktCnt );
  429. cmRC_t cmMidiSynthExec( cmMidiSynth* p, cmSample_t** outChArray, unsigned outChCnt );
  430. //------------------------------------------------------------------------------------------------------------
  431. // state id's
  432. enum
  433. {
  434. kOffWtId,
  435. kAtkWtId,
  436. kDcyWtId,
  437. kSusWtId,
  438. kRlsWtId
  439. };
  440. typedef struct
  441. {
  442. cmObj obj;
  443. cmReal_t hz; // current frq in Hz
  444. cmReal_t level; // current gain (0.0 to 1.0)
  445. cmReal_t phase; // osc phase (radians)
  446. unsigned durSmpCnt; // count of samples generated so far
  447. unsigned state; // osc state - see kXXXWtId above
  448. cmSample_t* outV; // signal output vector
  449. unsigned outN; // samples in outV[]
  450. } cmWtVoice;
  451. cmWtVoice* cmWtVoiceAlloc( cmCtx* ctx, cmWtVoice* p, unsigned procSmpCnt, cmReal_t hz );
  452. cmRC_t cmWtVoiceFree( cmWtVoice** pp );
  453. cmRC_t cmWtVoiceInit( cmWtVoice* p, unsigned procSmpCnt, cmReal_t hz );
  454. cmRC_t cmWtVoiceFinal( cmWtVoice* p );
  455. // 'sel' values are cmMidiSynthExec (kXXXMsId) values
  456. // Set outChArray[] to NULL to use internal audio buffer.
  457. int cmWtVoiceExec( cmWtVoice* p, struct cmMidiVoice_str* voicePtr, unsigned sel, cmSample_t* outChArray[], unsigned outChCnt );
  458. //------------------------------------------------------------------------------------------------------------
  459. typedef struct
  460. {
  461. cmObj obj;
  462. cmWtVoice** voiceArray; // osc state array
  463. unsigned voiceCnt;
  464. cmSample_t* buf;
  465. cmSample_t** chArray;
  466. unsigned chCnt;
  467. unsigned procSmpCnt; // count of samples in each chArray[i] sample vector
  468. double srate; // synth sample rate
  469. } cmWtVoiceBank;
  470. cmWtVoiceBank* cmWtVoiceBankAlloc( cmCtx* ctx, cmWtVoiceBank* p, double srate, unsigned procSmpCnt, unsigned voiceCnt, unsigned chCnt );
  471. cmRC_t cmWtVoiceBankFree( cmWtVoiceBank** pp );
  472. cmRC_t cmWtVoiceBankInit( cmWtVoiceBank* p, double srate, unsigned procSmpCnt, unsigned voiceCnt, unsigned chCnt );
  473. cmRC_t cmWtVoiceBankFinal( cmWtVoiceBank* p );
  474. // 'sel' values are cmMidiSynthExec (kXXXMsId) values
  475. // Set outChArray[] to NULL to use internal audio buffer.
  476. // Return 0 if the voice has gone inactive otherwise return 1.
  477. int cmWtVoiceBankExec( cmWtVoiceBank* p, struct cmMidiVoice_str* voicePtr, unsigned sel, cmSample_t* chArray[], unsigned chCnt );
  478. //------------------------------------------------------------------------------------------------------------
  479. typedef struct
  480. {
  481. cmObj obj;
  482. cmSample_t* bufV; // bufV[ bufN ]
  483. unsigned bufN;
  484. cmAudioFileInfo_t info;
  485. unsigned begSmpIdx;
  486. unsigned chIdx;
  487. char* fn;
  488. } cmAudioFileBuf;
  489. // set 'durSmpCnt' to cmInvalidCnt to include all samples to the end of the file
  490. cmAudioFileBuf* cmAudioFileBufAlloc( cmCtx* ctx, cmAudioFileBuf* p, unsigned procSmpCnt, const char* fn, unsigned chIdx, unsigned begSmpIdx, unsigned durSmpCnt );
  491. cmRC_t cmAudioFileBufFree( cmAudioFileBuf** pp );
  492. cmRC_t cmAudioFileBufInit( cmAudioFileBuf* p, unsigned procSmpCnt, const char* fn, unsigned chIdx, unsigned begSmpIdx, unsigned durSmpCnt );
  493. cmRC_t cmAudioFileBufFinal(cmAudioFileBuf* p );
  494. // Returns the count of samples copied into outV or 0 if smpIdx >= p->bufN.
  495. // If less than outN samples are available then the remaining samples are set to 0.
  496. unsigned cmAudioFileBufExec( cmAudioFileBuf* p, unsigned smpIdx, cmSample_t* outV, unsigned outN, bool sumIntoOutFl );
  497. //------------------------------------------------------------------------------------------------------------
  498. // Multi-delay. Each of the taps of this delay operates as a independent delay with feedback.
  499. // Delay line specification.
  500. typedef struct
  501. {
  502. cmReal_t delayGain; // delay gain
  503. cmReal_t delayMs; // delay time in milliseconds
  504. cmReal_t delaySmpFrac; // delay time in samples (next fractional delay index = inIdx - delaySmpFrac)
  505. cmSample_t* delayBuf; // delayBuf[delayBufSmpCnt] delay line memory
  506. int delayBufSmpCnt; // delay buffer length in samples
  507. int inIdx; // next delay input index
  508. } cmMDelayHead;
  509. typedef struct
  510. {
  511. cmObj obj;
  512. unsigned delayCnt; // count of taps
  513. cmMDelayHead* delayArray; // tap specs
  514. cmSample_t* outV; // outV[outN] output buffer
  515. unsigned outN; // procSmpCnt
  516. cmReal_t fbCoeff; // feedback coeff.
  517. cmReal_t srate; // system sample rate
  518. } cmMDelay;
  519. cmMDelay* cmMDelayAlloc( cmCtx* ctx, cmMDelay* p, unsigned procSmpCnt, cmReal_t srate, cmReal_t fbCoeff, unsigned delayCnt, const cmReal_t* delayMsArray, const cmReal_t* delayGainArray );
  520. cmRC_t cmMDelayFree( cmMDelay** pp );
  521. cmRC_t cmMDelayInit( cmMDelay* p, unsigned procSmpCnt, cmReal_t srate, cmReal_t fbCoeff, unsigned delayCnt, const cmReal_t* delayMsArray, const cmReal_t* delayGainArray );
  522. cmRC_t cmMDelayFinal( cmMDelay* p );
  523. cmRC_t cmMDelayExec( cmMDelay* p, const cmSample_t* sigV, cmSample_t* outV, unsigned sigN, bool bypassFl );
  524. void cmMDelaySetTapMs( cmMDelay* p, unsigned tapIdx, cmReal_t ms );
  525. void cmMDelaySetTapGain(cmMDelay* p, unsigned tapIdx, cmReal_t gain );
  526. void cmMDelayReport( cmMDelay* p, cmRpt_t* rpt );
  527. //------------------------------------------------------------------------------------------------------------
  528. enum
  529. {
  530. kEnableAspFl = 0x01,
  531. kDelAspFl = 0x02
  532. };
  533. typedef struct cmAudioSeg_str
  534. {
  535. cmAudioFileBuf* bufPtr; // pointer to the audio file buffer this segment is contained in
  536. unsigned id; // id (unique amoung segments)
  537. unsigned smpIdx; // offset into audioBuf[] of first sample
  538. unsigned smpCnt; // total count of samples to play
  539. unsigned outChIdx; // output buffer channel
  540. unsigned outSmpIdx; // outSmpIdx + smpIdx == next sample to play
  541. unsigned flags; // see kXXXAspFl
  542. } cmAudioSeg;
  543. typedef struct
  544. {
  545. cmObj obj;
  546. unsigned segCnt;
  547. cmAudioSeg* segArray;
  548. unsigned procSmpCnt;
  549. cmSample_t** outChArray;
  550. unsigned outChCnt;
  551. cmSample_t* outM;
  552. } cmAudioSegPlayer;
  553. cmAudioSegPlayer* cmAudioSegPlayerAlloc( cmCtx* ctx, cmAudioSegPlayer* p, unsigned procSmpCnt, unsigned outChCnt );
  554. cmRC_t cmAudioSegPlayerFree( cmAudioSegPlayer** pp );
  555. cmRC_t cmAudioSegPlayerInit( cmAudioSegPlayer* p, unsigned procSmpCnt, unsigned outChCnt );
  556. cmRC_t cmAudioSegPlayerFinal( cmAudioSegPlayer* p );
  557. cmRC_t cmAudioSegPlayerInsert( cmAudioSegPlayer* p, unsigned id, cmAudioFileBuf* bufPtr, unsigned smpIdx, unsigned smpCnt, unsigned outChIdx );
  558. cmRC_t cmAudioSegPlayerEdit( cmAudioSegPlayer* p, unsigned id, cmAudioFileBuf* bufPtr, unsigned smpIdx, unsigned smpCnt, unsigned outChIdx );
  559. cmRC_t cmAudioSegPlayerRemove( cmAudioSegPlayer* p, unsigned id, bool delFl );
  560. cmRC_t cmAudioSegPlayerEnable( cmAudioSegPlayer* p, unsigned id, bool enableFl, unsigned outSmpIdx );
  561. cmRC_t cmAudioSegPlayerReset( cmAudioSegPlayer* p );
  562. cmRC_t cmAudioSegPlayerExec( cmAudioSegPlayer* p, cmSample_t** outChPtr, unsigned chCnt, unsigned outSmpCnt );
  563. //------------------------------------------------------------------------------------------------------------
  564. /*
  565. cmReal_t (*cmCluster0DistFunc_t)( void* userPtr, const cmReal_t* v0, const cmReal_t* v1, unsigned binCnt );
  566. typedef struct
  567. {
  568. cmObj obj;
  569. unsigned flags;
  570. unsigned stateCnt;
  571. unsigned binCnt;
  572. cmReal_t* oM; // oM[ binCnt, stateCnt ]
  573. unsigned* tM; // tM[ stateCnt, stateCnt ]
  574. cmReal_t* dV; // dV[ state
  575. cmCluster0DistFunc_t distFunc;
  576. void* distUserPtr;
  577. unsigned cnt;
  578. } cmCluster0;
  579. enum
  580. {
  581. kCalcTransFl = 0x01,
  582. kCalcDurFl = 0x02
  583. };
  584. cmCluster0* cmCluster0Alloc( cmCtx* ctx, cmCluster0* ap, unsigned stateCnt, unsigned binCnt, unsigned flags, cmCluster0DistFunc_t distFunc, void* dstUserPtr );
  585. cmRC_t cmCluster0Free( cmCluster0** pp );
  586. cmRC_t cmCluster0Init( cmCluster0* p, unsigned stateCnt, unsigned binCnt, unsigned flags, cmCluster0DistFunc_t distFunc, void* dstUserPtr );
  587. cmRC_t cmCluster0Final( cmCluster0* p );
  588. cmRC_t cmCluster0Exec( cmCluster0* p, const cmReal_t* v, unsigned vn );
  589. */
  590. //------------------------------------------------------------------------------------------------------------
  591. typedef struct
  592. {
  593. cmObj obj;
  594. unsigned n;
  595. unsigned m;
  596. unsigned r;
  597. unsigned maxIterCnt;
  598. unsigned convergeCnt;
  599. cmReal_t* V; // V[n,m]
  600. cmReal_t* W; // W[n,r]
  601. cmReal_t* H; // H[r,m]
  602. cmReal_t* tr;
  603. cmReal_t* x;
  604. cmReal_t* t0nm;
  605. cmReal_t* t1nm;
  606. cmReal_t* Wt;
  607. cmReal_t* Ht;
  608. cmReal_t* trm;
  609. unsigned* crm;
  610. cmReal_t* tnr;
  611. unsigned* c0;
  612. unsigned* c1;
  613. unsigned* c0m;
  614. unsigned* c1m;
  615. unsigned* idxV;
  616. } cmNmf_t;
  617. cmNmf_t* cmNmfAlloc( cmCtx* ctx, cmNmf_t* ap, unsigned n, unsigned m, unsigned r, unsigned maxIterCnt, unsigned convergeCnt );
  618. cmRC_t cmNmfFree( cmNmf_t** pp );
  619. cmRC_t cmNmfInit( cmNmf_t* p, unsigned n, unsigned m, unsigned r, unsigned maxIterCnt, unsigned convergeCnt );
  620. cmRC_t cmNmfFinal(cmNmf_t* p );
  621. //
  622. cmRC_t cmNmfExec( cmNmf_t* p, const cmReal_t* v, unsigned cn );
  623. //------------------------------------------------------------------------------------------------------------
  624. // cmVectArray buffers row vectors of arbitrary length in memory.
  625. // The buffers may then be access using the cmVectArrayGetXXX() functions.
  626. // The entire contents of the file may be written to a file using atVectArrayWrite().
  627. // The file may then be read in back into memory using cmVectArrayAllocFromFile()
  628. // or in octave via readVectArray.m.
  629. // A rectantular matrix in memory may be written to a VectArray file in one operation
  630. // via the function cmVectArrayWriteMatrixXXX().
  631. typedef struct cmVectArrayVect_str
  632. {
  633. unsigned n; // length of this vector in values (not bytes)
  634. union
  635. {
  636. char* v; // raw memory vector pointer
  637. double* dV; // dV[n] vector of doubles
  638. float* fV; // fV[n] vecotr of floats
  639. cmSample_t* sV; // sV[n] vector of cmSample_t
  640. int* iV;
  641. unsigned* uV;
  642. } u;
  643. struct cmVectArrayVect_str* link; // link to next element record
  644. } cmVectArrayVect_t;
  645. enum
  646. {
  647. kDoubleVaFl = 0x01,
  648. kRealVaFl = 0x01,
  649. kFloatVaFl = 0x02,
  650. kSampleVaFl = 0x02,
  651. kIntVaFl = 0x04,
  652. kUIntVaFl = 0x08,
  653. kVaMask = 0x0f
  654. };
  655. typedef struct
  656. {
  657. cmObj obj;
  658. cmVectArrayVect_t* bp; // first list element
  659. cmVectArrayVect_t* ep; // last list element
  660. unsigned vectCnt; // count of elements in linked list
  661. unsigned flags; // data vector type (See: kFloatVaFl, kDoubleVaFl, ... )
  662. unsigned typeByteCnt; // size of a single data vector value (e.g. 4=float 8=double)
  663. unsigned maxEleCnt; // length of the longest data vector
  664. double* tempV;
  665. cmVectArrayVect_t* cur;
  666. } cmVectArray_t;
  667. // Flags must be set to one of the kXXXVAFl flag values.
  668. cmVectArray_t* cmVectArrayAlloc( cmCtx* ctx, unsigned flags );
  669. cmVectArray_t* cmVectArrayAllocFromFile(cmCtx* ctx, const char* fn );
  670. cmRC_t cmVectArrayFree( cmVectArray_t** pp );
  671. // Release all the stored vectors but do not release the object.
  672. cmRC_t cmVectArrayClear( cmVectArray_t* p );
  673. // Return the count of vectors contained in the vector array.
  674. cmRC_t cmVectArrayCount( const cmVectArray_t* p );
  675. // Return the maximum element count among all rows.
  676. unsigned cmVectArrayMaxRowCount( const cmVectArray_t* p );
  677. // Store a new vector by appending it to the end of the internal vector list.
  678. // Note:
  679. // 1. The true type of v[] in the call to cmVectArrayAppendV() must match
  680. // the data type set in p->flags.
  681. // 2. The 'vn' argument to atVectArrayAppendV() is an element count not
  682. // a byte count. The size of each element is determined by the data type
  683. // as set by atVectArrayAlloc().
  684. cmRC_t cmVectArrayAppendV( cmVectArray_t* p, const void* v, unsigned vn );
  685. cmRC_t cmVectArrayAppendS( cmVectArray_t* p, const cmSample_t* v, unsigned vn );
  686. cmRC_t cmVectArrayAppendR( cmVectArray_t* p, const cmReal_t* v, unsigned vn );
  687. cmRC_t cmVectArrayAppendF( cmVectArray_t* p, const float* v, unsigned vn );
  688. cmRC_t cmVectArrayAppendD( cmVectArray_t* p, const double* v, unsigned vn );
  689. cmRC_t cmVectArrayAppendI( cmVectArray_t* p, const int* v, unsigned vn );
  690. cmRC_t cmVectArrayAppendU( cmVectArray_t* p, const unsigned* v, unsigned vn );
  691. // Write a vector array in a format that can be read by readVectArray.m.
  692. cmRC_t cmVectArrayWrite( cmVectArray_t* p, const char* fn );
  693. cmRC_t cmVectArrayWriteDirFn(cmVectArray_t* p, const char* dir, const char* fn );
  694. // Print the vector array to rpt.
  695. cmRC_t cmVectArrayPrint( cmVectArray_t* p, cmRpt_t* rpt );
  696. typedef cmRC_t (*cmVectArrayForEachFuncS_t)( void* arg, unsigned idx, const cmSample_t* xV, unsigned xN );
  697. unsigned cmVectArrayForEachS( cmVectArray_t* p, unsigned idx, unsigned cnt, cmVectArrayForEachFuncS_t func, void* arg );
  698. // Write the vector v[vn] in the VectArray file format.
  699. // Note:
  700. // 1. The true type of v[] in cmVectArrayWriteVectoV() must match the
  701. // data type set in the 'flags' parameter.
  702. // 2. The 'vn' argument to atVectArrayWriteVectorV() is an element count not
  703. // a byte count. The size of each element is determined by the data type
  704. // as set by atVectArrayAlloc().
  705. cmRC_t cmVectArrayWriteVectorV( cmCtx* ctx, const char* fn, const void* v, unsigned vn, unsigned flags );
  706. cmRC_t cmVectArrayWriteVectorS( cmCtx* ctx, const char* fn, const cmSample_t* v, unsigned vn );
  707. cmRC_t cmVectArrayWriteVectorR( cmCtx* ctx, const char* fn, const cmReal_t* v, unsigned vn );
  708. cmRC_t cmVectArrayWriteVectorD( cmCtx* ctx, const char* fn, const double* v, unsigned vn );
  709. cmRC_t cmVectArrayWriteVectorF( cmCtx* ctx, const char* fn, const float* v, unsigned vn );
  710. cmRC_t cmVectArrayWriteVectorI( cmCtx* ctx, const char* fn, const int* v, unsigned vn );
  711. cmRC_t cmVectArrayWriteVectorU( cmCtx* ctx, const char* fn, const unsigned* v, unsigned vn );
  712. // Write the column-major matrix m[rn,cn] to the file 'fn'.
  713. // Notes:
  714. // 1. The true type of m[] in cmVectArrayWriteMatrixV() must match the
  715. // data type set in the 'flags' parameter.
  716. // 2. The 'rn','cn' arguments to atVectWriteMatrixV() is are element counts not
  717. // byte counts. The size of each element is determined by the data type
  718. // as set by atVectArrayAlloc().
  719. cmRC_t cmVectArrayWriteMatrixV( cmCtx* ctx, const char* fn, const void* m, unsigned rn, unsigned cn, unsigned flags );
  720. cmRC_t cmVectArrayWriteMatrixS( cmCtx* ctx, const char* fn, const cmSample_t* m, unsigned rn, unsigned cn );
  721. cmRC_t cmVectArrayWriteMatrixR( cmCtx* ctx, const char* fn, const cmReal_t* m, unsigned rn, unsigned cn );
  722. cmRC_t cmVectArrayWriteMatrixD( cmCtx* ctx, const char* fn, const double* m, unsigned rn, unsigned cn );
  723. cmRC_t cmVectArrayWriteMatrixF( cmCtx* ctx, const char* fn, const float* m, unsigned rn, unsigned cn );
  724. cmRC_t cmVectArrayWriteMatrixI( cmCtx* ctx, const char* fn, const int* m, unsigned rn, unsigned cn );
  725. cmRC_t cmVectArrayWriteMatrixU( cmCtx* ctx, const char* fn, const unsigned* m, unsigned rn, unsigned cn );
  726. // Read a VectArray file and return it as a matrix.
  727. // The returned memory must be released with a subsequent call to cmMemFree().
  728. // Note that the true type of the pointer address 'mRef' in the call to
  729. // cmVectArrayReadMatrixV() must match the data type of the cmVectArray_t
  730. // specified by 'fn'.
  731. cmRC_t cmVectArrayReadMatrixV( cmCtx* ctx, const char* fn, void** mRef, unsigned* rnRef, unsigned* cnRef );
  732. cmRC_t cmVectArrayReadMatrixS( cmCtx* ctx, const char* fn, cmSample_t** mRef, unsigned* rnRef, unsigned* cnRef );
  733. cmRC_t cmVectArrayReadMatrixR( cmCtx* ctx, const char* fn, cmReal_t** mRef, unsigned* rnRef, unsigned* cnRef );
  734. cmRC_t cmVectArrayReadMatrixD( cmCtx* ctx, const char* fn, double** mRef, unsigned* rnRef, unsigned* cnRef );
  735. cmRC_t cmVectArrayReadMatrixF( cmCtx* ctx, const char* fn, float** mRef, unsigned* rnRef, unsigned* cnRef );
  736. cmRC_t cmVectArrayReadMatrixI( cmCtx* ctx, const char* fn, int** mRef, unsigned* rnRef, unsigned* cnRef );
  737. cmRC_t cmVectArrayReadMatrixU( cmCtx* ctx, const char* fn, unsigned** mRef, unsigned* rnRef, unsigned* cnRef );
  738. // Row iteration control functions.
  739. cmRC_t cmVectArrayRewind( cmVectArray_t* p );
  740. cmRC_t cmVectArrayAdvance( cmVectArray_t* p, unsigned n );
  741. bool cmVectArrayIsEOL( const cmVectArray_t* p );
  742. unsigned cmVectArrayEleCount( const cmVectArray_t* p );
  743. // Copy the current row vector to v[].
  744. // Note that the true type of v[] in cmVectArrayGetV() must match the data type of 'p'.
  745. cmRC_t cmVectArrayGetV( cmVectArray_t* p, void* v, unsigned* vnRef );
  746. cmRC_t cmVectArrayGetS( cmVectArray_t* p, cmSample_t* v, unsigned* vnRef );
  747. cmRC_t cmVectArrayGetR( cmVectArray_t* p, cmReal_t* v, unsigned* vnRef );
  748. cmRC_t cmVectArrayGetD( cmVectArray_t* p, double* v, unsigned* vnRef );
  749. cmRC_t cmVectArrayGetF( cmVectArray_t* p, float* v, unsigned* vnRef );
  750. cmRC_t cmVectArrayGetI( cmVectArray_t* p, int* v, unsigned* vnRef );
  751. cmRC_t cmVectArrayGetU( cmVectArray_t* p, unsigned* v, unsigned* vnRef );
  752. // Set *resultFlRef to true if m[rn,cn] is equal to the cmVectArray_t specified by 'fn'.
  753. // Note that the true type of 'm[]' in the call to cmVectArrayMatrixIsEqualV()
  754. // must match the data type set in 'flags'.
  755. cmRC_t cmVectArrayMatrixIsEqualV( cmCtx* ctx, const char* fn, const void* m, unsigned rn, unsigned cn, bool* resultFlRef, unsigned flags );
  756. cmRC_t cmVectArrayMatrixIsEqualS( cmCtx* ctx, const char* fn, const cmSample_t* m, unsigned rn, unsigned cn, bool* resultFlRef );
  757. cmRC_t cmVectArrayMatrixIsEqualR( cmCtx* ctx, const char* fn, const cmReal_t* m, unsigned rn, unsigned cn, bool* resultFlRef );
  758. cmRC_t cmVectArrayMatrixIsEqualD( cmCtx* ctx, const char* fn, const double* m, unsigned rn, unsigned cn, bool* resultFlRef );
  759. cmRC_t cmVectArrayMatrixIsEqualF( cmCtx* ctx, const char* fn, const float* m, unsigned rn, unsigned cn, bool* resultFlRef );
  760. cmRC_t cmVectArrayMatrixIsEqualI( cmCtx* ctx, const char* fn, const int* m, unsigned rn, unsigned cn, bool* resultFlRef );
  761. cmRC_t cmVectArrayMatrixIsEqualU( cmCtx* ctx, const char* fn, const unsigned* m, unsigned rn, unsigned cn, bool* resultFlRef );
  762. // If a vector array is composed of repeating blocks of 'groupCnt' sub-vectors
  763. // where the concatenated ith sub-vectors in each group form a single super-vector then
  764. // this function will return the super-vector. Use cmMemFree(*vRef) to release
  765. // the returned super-vector.
  766. cmRC_t cmVectArrayFormVectR( cmVectArray_t* p, unsigned groupIdx, unsigned groupCnt, cmReal_t** vRef, unsigned* vnRef );
  767. cmRC_t cmVectArrayFormVectF( cmVectArray_t* p, unsigned groupIdx, unsigned groupCnt, float** vRef, unsigned* vnRef );
  768. cmRC_t cmVectArrayFormVectColF( cmVectArray_t* p, unsigned groupIdx, unsigned groupCnt, unsigned colIdx, float** vRef, unsigned* vnRef );
  769. cmRC_t cmVectArrayFormVectColU( cmVectArray_t* p, unsigned groupIdx, unsigned groupCnt, unsigned colIdx, unsigned** vRef, unsigned* vnRef );
  770. cmRC_t cmVectArrayTest( cmCtx* ctx, const char* fn, bool genFl );
  771. //-----------------------------------------------------------------------------------------------------------------------
  772. // Spectral whitening filter.
  773. // Based on: Klapuri, A., 2006: Multiple fundamental frequency estimation by summing
  774. // harmonic amplitudes.
  775. typedef struct
  776. {
  777. cmObj obj;
  778. unsigned binCnt; //
  779. cmReal_t binHz; //
  780. unsigned bandCnt; //
  781. cmReal_t coeff; //
  782. cmReal_t* whiV; // whiV[bandCnt+2] - fractional bin index of each center frequency
  783. cmReal_t* whM; // whM[binCnt,bandCnt]
  784. cmReal_t* iV; // iV[ binCnt ] - working memory
  785. } cmWhFilt;
  786. cmWhFilt* cmWhFiltAlloc( cmCtx* c, cmWhFilt* p, unsigned binCnt, cmReal_t binHz, cmReal_t coeff, cmReal_t maxHz );
  787. cmRC_t cmWhFiltFree( cmWhFilt** pp );
  788. cmRC_t cmWhFiltInit( cmWhFilt* p, unsigned binCnt, cmReal_t binHz, cmReal_t coeff, cmReal_t maxHz );
  789. cmRC_t cmWhFiltFinal( cmWhFilt* p );
  790. cmRC_t cmWhFiltExec( cmWhFilt* p, const cmReal_t* xV, cmReal_t* yV, unsigned xyN );
  791. //-----------------------------------------------------------------------------------------------------------------------
  792. typedef enum
  793. {
  794. kNoStateFrqTrkId,
  795. kDlyFrqTrkId,
  796. kAtkFrqTrkId,
  797. kSusFrqTrkId,
  798. kDcyFrqTrkId
  799. } cmFrqTrkAttenStateId_t;
  800. typedef struct
  801. {
  802. double srate; // system sample rate
  803. unsigned chCnt; // tracking channel count
  804. unsigned binCnt; // count of spectrum elements passed in each call to cmFrqTrkExec()
  805. unsigned hopSmpCnt; // phase vocoder hop count in samples
  806. cmReal_t stRange; // maximum allowable semi-tones between a tracker and a peak
  807. cmReal_t wndSecs; // duration of the
  808. cmReal_t minTrkSec; // minimum track length before track is considered stable
  809. cmReal_t maxTrkDeadSec; // maximum length of time a tracker may fail to connect to a peak before being declared disconnected.
  810. cmReal_t pkThreshDb; // minimum amplitide in Decibels of a selected spectral peak.
  811. cmReal_t pkAtkThreshDb; // minimum amplitude in Decibels for the first frame of a new track.
  812. cmReal_t pkMaxHz; // maximum frequency to track
  813. cmReal_t whFiltCoeff;
  814. cmReal_t attenThresh;
  815. cmReal_t attenGain;
  816. cmReal_t attenDlySec;
  817. cmReal_t attenAtkSec;
  818. const char* logFn; // log file name or NULL if no file is to be written
  819. const char* levelFn; // level file name or NULL if no file is to be written
  820. const char* specFn; // spectrum file name or NULL if no file is to be written
  821. const char* attenFn;
  822. } cmFrqTrkArgs_t;
  823. typedef struct
  824. {
  825. bool activeFl;
  826. unsigned id;
  827. unsigned tN; // age of this track in frames
  828. unsigned dN; // count of consecutive times this ch has not connected
  829. cmReal_t hz; // current center frequency
  830. cmReal_t db; // current magnitude
  831. cmReal_t* dbV; // dbV[]
  832. cmReal_t* hzV; // hzV[]
  833. unsigned si;
  834. unsigned sn;
  835. cmReal_t db_mean;
  836. cmReal_t db_std;
  837. cmReal_t hz_mean;
  838. cmReal_t hz_std;
  839. cmReal_t score;
  840. cmFrqTrkAttenStateId_t state;
  841. int attenPhsIdx;
  842. cmReal_t attenGain;
  843. } cmFrqTrkCh_t;
  844. struct cmBinMtxFile_str;
  845. typedef struct cmFrqTrk_str
  846. {
  847. cmObj obj;
  848. cmFrqTrkArgs_t a;
  849. cmFrqTrkCh_t* ch; // ch[ a.chCnt ]
  850. unsigned hN; // count of magnitude buffer frames
  851. unsigned sN; // count of frames in channel statistics buffers
  852. unsigned bN; // count of bins in peak matrices
  853. cmReal_t* dbM; // dbM[ hN, bN ]
  854. unsigned hi; // next row of dbM to fill
  855. unsigned fN; // total count of frames processed.
  856. cmReal_t binHz;
  857. cmReal_t* dbV;
  858. unsigned* pkiV;
  859. unsigned deadN_max; // max. count of hops a tracker may fail to connect before being set to inactive
  860. unsigned minTrkN; // minimum track length in hops
  861. unsigned nextTrkId;
  862. unsigned newTrkCnt;
  863. unsigned curTrkCnt;
  864. unsigned deadTrkCnt;
  865. cmReal_t* aV;
  866. int attenDlyPhsMax;
  867. int attenPhsMax;
  868. cmWhFilt* wf;
  869. cmVectArray_t* logVa;
  870. cmVectArray_t* levelVa;
  871. cmVectArray_t* specVa;
  872. cmVectArray_t* attenVa;
  873. cmChar_t* logFn;
  874. cmChar_t* levelFn;
  875. cmChar_t* specFn;
  876. cmChar_t* attenFn;
  877. } cmFrqTrk;
  878. //
  879. // 1. Calculate the mean spectral magnitude profile over the last hN frames.
  880. // 2. Locate the peaks in the profile.
  881. // 3. Allow each active tracker to select the closest peak to extend its life.
  882. // a) The distance between the trackers current location and a given
  883. // peak is measured based on magnitude and frequency over time.
  884. // b) There is a frequency range limit outside of which a given track-peak
  885. // connection may not go.
  886. // c) There is an amplitude threshold below which a track may not fall.
  887. cmFrqTrk* cmFrqTrkAlloc( cmCtx* c, cmFrqTrk* p, const cmFrqTrkArgs_t* a );
  888. cmRC_t cmFrqTrkFree( cmFrqTrk** pp );
  889. cmRC_t cmFrqTrkInit( cmFrqTrk* p, const cmFrqTrkArgs_t* a );
  890. cmRC_t cmFrqTrkFinal( cmFrqTrk* p );
  891. cmRC_t cmFrqTrkExec( cmFrqTrk* p, const cmReal_t* magV, const cmReal_t* phsV, const cmReal_t* hzV );
  892. void cmFrqTrkPrint( cmFrqTrk* p );
  893. //------------------------------------------------------------------------------------------------------------
  894. typedef struct
  895. {
  896. double srate;
  897. unsigned binCnt;
  898. unsigned hopSmpCnt;
  899. unsigned bufMs;
  900. cmReal_t maxHz;
  901. } cmFbCtlArgs_t;
  902. typedef struct
  903. {
  904. cmObj obj;
  905. cmFbCtlArgs_t a;
  906. unsigned binCnt;
  907. unsigned frmCnt;
  908. cmReal_t* bM; // bM[ frmCnt, binCnt ];
  909. unsigned bfi; // current buffer frame (column) index
  910. unsigned bfN; // currrent count of frames in the buffer
  911. cmReal_t* rmsV; // rmsV[ frmCnt ];
  912. cmReal_t* sV; // sV[ binCnt ]
  913. cmReal_t* uV;
  914. cmVectArray_t* sva;
  915. cmVectArray_t* uva;
  916. } cmFbCtl_t;
  917. cmFbCtl_t* cmFbCtlAlloc( cmCtx* c, cmFbCtl_t* p, const cmFbCtlArgs_t* a );
  918. cmRC_t cmFbCtlFree( cmFbCtl_t** pp );
  919. cmRC_t cmFbCtlInit( cmFbCtl_t* p, const cmFbCtlArgs_t* a );
  920. cmRC_t cmFbCtlFinal(cmFbCtl_t* p );
  921. cmRC_t cmFbCtlExec( cmFbCtl_t* p, const cmReal_t* xV );
  922. //-----------------------------------------------------------------------------------------------------------------------
  923. typedef struct
  924. {
  925. cmObj obj;
  926. cmReal_t* rmsV; // rmsV[rmsN]
  927. unsigned rmsN; //
  928. unsigned rmsIdx;//
  929. cmReal_t rmsValue; // last RMS value
  930. cmSample_t* envV; // envV[envN]
  931. unsigned envN; // atkSmp + rlsSmp;
  932. unsigned threshN;
  933. unsigned threshIdx;
  934. float threshLvl;
  935. float rlsLvl;
  936. unsigned envIdx;
  937. double gain;
  938. unsigned atkCnt;
  939. } cmExpander;
  940. cmExpander* cmExpanderAlloc( cmCtx* c, cmExpander* p, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs );
  941. cmRC_t cmExpanderFree( cmExpander** pp );
  942. cmRC_t cmExpanderInit( cmExpander* p, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs );
  943. cmRC_t cmExpanderFinal( cmExpander* p );
  944. cmRC_t cmExpanderExec( cmExpander* p, cmSample_t* x, cmSample_t* y, unsigned xyN );
  945. cmRC_t cmExpanderExecD( cmExpander* p, double* x, double* y, unsigned xyN );
  946. //-----------------------------------------------------------------------------------------------------------------------
  947. typedef struct
  948. {
  949. cmObj obj;
  950. cmExpander** b; // b[bandN]
  951. unsigned bandN;
  952. double rmsValue;
  953. unsigned atkCnt;
  954. } cmExpanderBank;
  955. cmExpanderBank* cmExpanderBankAlloc( cmCtx* c, cmExpanderBank* p, unsigned bandN, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs );
  956. cmRC_t cmExpanderBankFree( cmExpanderBank** pp );
  957. cmRC_t cmExpanderBankInit( cmExpanderBank* p, unsigned bandN, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs );
  958. cmRC_t cmExpanderBankFinal( cmExpanderBank* p );
  959. cmRC_t cmExpanderBankExec( cmExpanderBank* p, cmSample_t* x, unsigned bandN );
  960. cmRC_t cmExpanderBankExecD( cmExpanderBank* p, double* x, unsigned bandN );
  961. //------------------------------------------------------------------------------------------------------------
  962. enum
  963. {
  964. kBypassModeSdId, // 0 - no effect
  965. kBasicModeSdId, // 1 - fixed thresh
  966. kSpecCentSdId, // 2 - thresh = max magn - (offset * spec_cent)
  967. kAmpEnvSdId, // 3 - thresh = max magn - offset
  968. kBumpSdId,
  969. kModeSdCnt
  970. };
  971. typedef struct
  972. {
  973. cmObj obj;
  974. double srate;
  975. unsigned wndSmpCnt;
  976. unsigned hopFcmt;
  977. unsigned hopSmpCnt;
  978. unsigned procSmpCnt;
  979. cmPvAnl* pva;
  980. cmPvSyn* pvs;
  981. cmFrqTrk* ft;
  982. cmFbCtl_t* fbc;
  983. cmExpanderBank* exb;
  984. unsigned mode;
  985. double thresh;
  986. double uprSlope;
  987. double lwrSlope;
  988. double offset;
  989. bool invertFl;
  990. double spcBwHz; // spectral centroid bandwidth in Hz
  991. double spcSmArg; // spectral centroid smoothing
  992. double spcMin;
  993. double spcMax;
  994. unsigned spcBinCnt; // count of bins used in the spectral centroid
  995. cmReal_t* hzV; // hzV[spcBinCnt];
  996. cmReal_t spc;
  997. unsigned spcCnt;
  998. cmReal_t spcSum;
  999. cmReal_t spcSqSum;
  1000. cmReal_t aeSmMax; // smoothed max bin magn - used by spectral centroid
  1001. cmReal_t aeSmOffs; // smoothed offset
  1002. cmReal_t ae;
  1003. cmReal_t aeMin;
  1004. cmReal_t aeMax;
  1005. cmReal_t aeUnit;
  1006. cmReal_t ogain;
  1007. cmReal_t ogain0;
  1008. unsigned phaseModIndex;
  1009. unsigned fi; // total count of frames processed by cmSpecDistExec()
  1010. unsigned hN;
  1011. unsigned hi;
  1012. cmReal_t* iSpecM; // iSpecMtx[hN binN]
  1013. cmReal_t* iSpecV; // mean of rows of iSpecM
  1014. cmVectArray_t* iSpecVa;
  1015. cmReal_t* oSpecM; // oSpecMtx[hN binN]
  1016. cmReal_t* oSpecV; // mean of rows of oSpecM
  1017. cmVectArray_t* oSpecVa;
  1018. cmVectArray_t* statVa;
  1019. } cmSpecDist_t;
  1020. cmSpecDist_t* cmSpecDistAlloc( cmCtx* ctx,cmSpecDist_t* ap, unsigned procSmpCnt, double srate, unsigned wndSmpCnt, unsigned hopFcmt, unsigned olaWndTypeId );
  1021. cmRC_t cmSpecDistFree( cmSpecDist_t** pp );
  1022. cmRC_t cmSpecDistInit( cmSpecDist_t* p, unsigned procSmpCnt, double srate, unsigned wndSmpCnt, unsigned hopFcmt, unsigned olaWndTypeId );
  1023. cmRC_t cmSpecDistFinal(cmSpecDist_t* p );
  1024. cmRC_t cmSpecDistExec( cmSpecDist_t* p, const cmSample_t* sp, unsigned sn );
  1025. const cmSample_t* cmSpecDistOut( cmSpecDist_t* p );
  1026. //------------------------------------------------------------------------------------------------------------
  1027. // Write a binary matrix file in the format acceppted by the octave function readBinFile.m
  1028. typedef struct cmBinMtxFile_str
  1029. {
  1030. cmObj obj;
  1031. cmFileH_t fh;
  1032. unsigned rowCnt;
  1033. unsigned maxRowEleCnt;
  1034. unsigned eleByteCnt;
  1035. } cmBinMtxFile_t;
  1036. cmBinMtxFile_t* cmBinMtxFileAlloc( cmCtx* ctx, cmBinMtxFile_t* ap, const cmChar_t* fn );
  1037. cmRC_t cmBinMtxFileFree( cmBinMtxFile_t** pp );
  1038. cmRC_t cmBinMtxFileInit( cmBinMtxFile_t* p, const cmChar_t* fn );
  1039. cmRC_t cmBinMtxFileFinal( cmBinMtxFile_t* p );
  1040. // Write one row of 'xn' columns to the matrix file.
  1041. cmRC_t cmBinMtxFileExecS( cmBinMtxFile_t* p, const cmSample_t* x, unsigned xn );
  1042. cmRC_t cmBinMtxFileExecR( cmBinMtxFile_t* p, const cmReal_t* x, unsigned xn );
  1043. bool cmBinMtxFileIsValid( cmBinMtxFile_t* p );
  1044. // Write a binary matrix file.
  1045. // The matrix data is provided as sp[rowCnt,colCnt] or rp[rowCnt,colCnt].
  1046. // The matrix is assumed to be in column major order (like all matrices in the cm library)
  1047. // Either 'sp' or 'rp' must be given but not both.
  1048. // 'ctx' is optional and defaults to NULL.
  1049. // If 'ctx' is not provided then 'rpt' must be provided.
  1050. // If 'ctx' is provided then 'rpt' is not used.
  1051. // See cmAudioFileReadWriteTest() in cmProcTest.c for an example usage.
  1052. cmRC_t cmBinMtxFileWrite( const cmChar_t* fn, unsigned rowCnt, unsigned colCnt, const cmSample_t* sp, const cmReal_t* rp, cmCtx* ctx, cmRpt_t* rpt );
  1053. // Return the matrix file geometry.
  1054. // rowCntPtr,colCntPtr and eleByteCntPtr are optional
  1055. cmRC_t cmBinMtxFileSize( cmCtx_t* ctx, const cmChar_t* fn, unsigned* rowCntPtr, unsigned* colCntPtr, unsigned* eleByteCntPtr );
  1056. // Fill buf[rowCnt*colCnt*byteEleCnt] buffer from the binary matrix file 'fn'.
  1057. // rowCnt,colCnt,eleByteCnt must be exactly the same as the actual file.
  1058. // Use cmBinMtxFileSize() to determine the buffer size prior to calling this function.
  1059. // colCntV[colCnt] is optional.
  1060. cmRC_t cmBinMtxFileRead( cmCtx_t* ctx, const cmChar_t* fn, unsigned rowCnt, unsigned colCnt, unsigned eleByteCnt, void* buf, unsigned* colCntV );
  1061. #ifdef __cplusplus
  1062. }
  1063. #endif
  1064. #endif