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

cmProc2.h 55KB

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