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 47KB

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