libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmProc4.h 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. #ifndef cmProc4_h
  2. #define cmProc4_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //=======================================================================================================================
  7. //
  8. // Simplified string alignment function based on Levenshtein edit distance.
  9. //
  10. enum { kEdMinIdx, kEdSubIdx, kEdDelIdx, kEdInsIdx, kEdCnt };
  11. typedef struct
  12. {
  13. unsigned v[kEdCnt];
  14. bool matchFl;
  15. bool transFl;
  16. } ed_val;
  17. typedef struct ed_path_str
  18. {
  19. unsigned code;
  20. unsigned ri;
  21. unsigned ci;
  22. bool matchFl;
  23. bool transFl;
  24. struct ed_path_str* next;
  25. } ed_path;
  26. /*
  27. Backtracking:
  28. m[rn,cn] is organized to indicate the mutation operations
  29. on s0[0:rn-1] or s1[0:cn-1] during backtracking.
  30. Backtracking begins at cell m[rn-1,cn-1] and proceeds
  31. up and left toward m[0,0]. The action to perform during
  32. backtracking is determined by examinging which values
  33. int m[].v[1:3] match m[].v[0].
  34. Match Next Cell
  35. Index Operation Location
  36. ----- ------------------------ ------------------------
  37. 1 Substitute char s0[ri-1] move diagonally; up-left
  38. 2 Delete char s0[ri-1] move up.
  39. 3 Delete char s1[ci-1] move left.
  40. (same as inserting blank
  41. into after s[ri-1]
  42. Note that more than one value in m[].v[1:3] may match
  43. m[].v[0]. In this case the candidate solution branches
  44. at this point in the candidate selection processes.
  45. */
  46. typedef struct
  47. {
  48. const char* s0; // forms rows of m[] - mutate to match s1 - rn=strlen(s0)
  49. const char* s1; // forms columns of m[] - target string - cn=strlen(s1)
  50. unsigned rn; // length of s0 + 1
  51. unsigned cn; // length of s1 + 1
  52. ed_val* m; // m[rn,cn]
  53. unsigned pn; // rn+cn
  54. ed_path* p_mem; // pmem[ 2*pn ];
  55. ed_path* p_avl; // available path record linked list
  56. ed_path* p_cur; // current path linked list
  57. ed_path* p_opt; // p_opt[pn] current best alignment
  58. double s_opt; // score of the current best alignment
  59. } ed_r;
  60. // print the DP matrix ed_r.m[rn,cn].
  61. void ed_print_mtx( ed_r* r );
  62. // Initialize ed_r.
  63. void ed_init( ed_r* r, const char* s0, const char* s1 );
  64. // Fill in the DP matrix.
  65. void ed_calc_mtx( ed_r* r );
  66. // Traverse the possible alignments in the DP matrix and determine the optimal alignment.
  67. void ed_align( ed_r* r );
  68. // Print the optimal alignment p_opt[]
  69. void ed_print_opt( ed_r* r );
  70. // Free resource allocated by ed_init().
  71. void ed_free(ed_r* r);
  72. // Main test function.
  73. void ed_main();
  74. //=======================================================================================================================
  75. enum
  76. {
  77. kSmMinIdx, //
  78. kSmSubIdx, // 'substitute' - may or may not match
  79. kSmDelIdx, // 'delete' - delete a MIDI note
  80. kSmInsIdx, // 'insert' - insert a space in the score
  81. kSmCnt
  82. };
  83. enum
  84. {
  85. kSmMatchFl = 0x01,
  86. kSmTransFl = 0x02,
  87. kSmTruePosFl = 0x04,
  88. kSmFalsePosFl = 0x08,
  89. kSmBarFl = 0x10,
  90. kSmNoteFl = 0x20
  91. };
  92. // Dynamic Programming (DP) matrix element
  93. typedef struct
  94. {
  95. unsigned v[kSmCnt]; // cost for each operation
  96. unsigned flags; // cmSmMatchFl | cmSmTransFl
  97. unsigned scEvtIdx;
  98. } cmScMatchVal_t;
  99. // List record used to track a path through the DP matrix p->m[,]
  100. typedef struct cmScMatchPath_str
  101. {
  102. unsigned code; // kSmXXXIdx
  103. unsigned ri; // matrix row index
  104. unsigned ci; // matrix col index
  105. unsigned flags; // cmSmMatchFl | cmSmTransFl
  106. unsigned locIdx; // p->loc index or cmInvalidIdx
  107. unsigned scEvtIdx; // scScore event index
  108. struct cmScMatchPath_str* next; //
  109. } cmScMatchPath_t;
  110. typedef struct cmScMatchEvt_str
  111. {
  112. unsigned pitch; //
  113. unsigned scEvtIdx; // scScore event index
  114. } cmScMatchEvt_t;
  115. // Score location record.
  116. typedef struct
  117. {
  118. unsigned evtCnt; // count of score events at this location (i.e. a chord will have more than one event at a given location)
  119. cmScMatchEvt_t* evtV; // evtV[evtCnt]
  120. unsigned scLocIdx; // scH score location index
  121. int barNumb; // bar number of this location
  122. } cmScMatchLoc_t;
  123. typedef struct
  124. {
  125. unsigned mni; // unique identifier for this MIDI note - used to recognize when the cmScMatcher backtracks.
  126. unsigned smpIdx; // time stamp of this event
  127. unsigned pitch; // MIDI note pitch
  128. unsigned vel; // " " velocity
  129. unsigned locIdx; // location assoc'd with this MIDI evt (cmInvalidIdx if not a matching or non-matching 'substitute')
  130. unsigned scEvtIdx; // cmScore event index assoc'd with this event
  131. } cmScMatchMidi_t;
  132. typedef struct
  133. {
  134. cmObj obj; //
  135. cmScH_t scH; // cmScore handle
  136. unsigned locN; //
  137. cmScMatchLoc_t* loc; // loc[locN]
  138. unsigned mrn; // max m[] row count (midi)
  139. unsigned rn; // cur m[] row count
  140. unsigned mcn; // max m[] column count (score)
  141. unsigned cn; // cur m[] column count
  142. unsigned mmn; // max length of midiBuf[] (mrn-1)
  143. unsigned msn; // max length of score window (mcn-1)
  144. cmScMatchVal_t* m; // m[mrn,mcn] DP matrix
  145. unsigned pn; // mrn+mcn
  146. cmScMatchPath_t* p_mem; // pmem[ 2*pn ] - path memory
  147. cmScMatchPath_t* p_avl; // available path record linked list
  148. cmScMatchPath_t* p_cur; // current path linked list
  149. cmScMatchPath_t* p_opt; // p_opt[pn] - current best alignment as a linked list
  150. double opt_cost; // last p_opt cost set by cmScMatchExec()
  151. } cmScMatch;
  152. /*
  153. 1) This matcher cannot handle multiple instances of the same pitch occuring
  154. at the same 'location'.
  155. 2) Because each note of a chord is spread out over multiple locations, and
  156. there is no way to indicate that a note in the chord is already 'in-use'.
  157. If a MIDI note which is part of the chord is repeated, in error, it will
  158. appear to be correct (a positive match will be assigned to
  159. the second (and possible successive notes)).
  160. */
  161. cmScMatch* cmScMatchAlloc( cmCtx* c, cmScMatch* p, cmScH_t scH, unsigned maxScWndN, unsigned maxMidiWndN );
  162. cmRC_t cmScMatchFree( cmScMatch** pp );
  163. cmRC_t cmScMatchInit( cmScMatch* p, cmScH_t scH, unsigned maxScWndN, unsigned maxMidiWndN );
  164. cmRC_t cmScMatchFinal( cmScMatch* p );
  165. // Locate the position in p->loc[locIdx:locIdx+locN-1] which bests
  166. // matches midiV[midiN].
  167. // The result of this function is to update p_opt[]
  168. // The optimal path p_opt[] will only be updated if the edit_cost associated 'midiV[midiN]'.
  169. // with the best match is less than 'min_cost'.
  170. // Set 'min_cost' to DBL_MAX to force p_opt[] to be updated.
  171. // Returns cmEofRC if locIdx + locN > p->locN - note that this is not
  172. // necessarily an error.
  173. cmRC_t cmScMatchExec( cmScMatch* p, unsigned locIdx, unsigned locN, const cmScMatchMidi_t* midiV, unsigned midiN, double min_cost );
  174. //=======================================================================================================================
  175. typedef struct
  176. {
  177. unsigned locIdx; // index into cmScMatch_t.loc[]
  178. unsigned scEvtIdx; // score event index
  179. unsigned mni; // index of the performed MIDI event associated with this score location
  180. unsigned smpIdx; // sample time index of performed MIDI event
  181. unsigned pitch; // performed pitch
  182. unsigned vel; // performed velocity
  183. unsigned flags; // smTruePosFl | smFalsePosFl
  184. } cmScMatcherResult_t;
  185. struct cmScMatcher_str;
  186. typedef void (*cmScMatcherCb_t)( struct cmScMatcher_str* p, void* arg, cmScMatcherResult_t* rp );
  187. typedef struct cmScMatcher_str
  188. {
  189. cmObj obj;
  190. cmScMatcherCb_t cbFunc;
  191. void* cbArg;
  192. cmScMatch* mp;
  193. unsigned mn;
  194. cmScMatchMidi_t* midiBuf; // midiBuf[mn]
  195. cmScMatcherResult_t* res; // res[rn]
  196. unsigned rn; // length of res[] (set to 2*score event count)
  197. unsigned ri; // next avail res[] recd.
  198. double s_opt; //
  199. unsigned missCnt; // current count of consecutive trailing non-matches
  200. unsigned ili; // index into loc[] to start scan following reset
  201. unsigned eli; // index into loc[] of the last positive match.
  202. unsigned mni; // current count of MIDI events since the last call to cmScMatcherReset()
  203. unsigned mbi; // index of oldest MIDI event in midiBuf[]; stays at 0 when the buffer is full.
  204. unsigned begSyncLocIdx; // start of score window, in mp->loc[], of best match in previous scan
  205. unsigned initHopCnt; // max window hops during the initial (when the MIDI buffer fills for first time) sync scan
  206. unsigned stepCnt; // count of forward/backward score loc's to examine for a match during cmScMatcherStep().
  207. unsigned maxMissCnt; // max. number of consecutive non-matches during step prior to executing a scan.
  208. unsigned scanCnt; // current count of times a resync-scan was executed during cmScMatcherStep()
  209. bool printFl;
  210. } cmScMatcher;
  211. cmScMatcher* cmScMatcherAlloc(
  212. cmCtx* c, // Program context.
  213. cmScMatcher* p, // Existing cmScMatcher to reallocate or NULL to allocate a new cmScMatcher.
  214. double srate, // System sample rate.
  215. cmScH_t scH, // Score handle. See cmScore.h.
  216. unsigned scWndN, // Length of the scores active search area. ** See Notes.
  217. unsigned midiWndN, // Length of the MIDI active note buffer. ** See Notes.
  218. cmScMatcherCb_t cbFunc, // A cmScMatcherCb_t function to be called to notify the recipient of changes in the score matcher status.
  219. void* cbArg ); // User argument to 'cbFunc'.
  220. // Notes:
  221. // The cmScMatcher maintains an internal cmScMatch object which is used to attempt to find the
  222. // best match between the current MIDI active note buffer and the current score search area.
  223. // 'scWndN' is used to set the cmScMatch 'locN' argument.
  224. // 'midiWndN' sets the length of the MIDI FIFO which is used to match to the score with
  225. // each recceived MIDI note.
  226. // 'midiWndN' must be <= 'scWndN'.
  227. cmRC_t cmScMatcherFree( cmScMatcher** pp );
  228. cmRC_t cmScMatcherInit( cmScMatcher* p, double srate, cmScH_t scH, unsigned scWndN, unsigned midiWndN, cmScMatcherCb_t cbFunc, void* cbArg );
  229. cmRC_t cmScMatcherFinal( cmScMatcher* p );
  230. // 'scLocIdx' is a score index as used by cmScoreLoc(scH) not into p->mp->loc[].
  231. cmRC_t cmScMatcherReset( cmScMatcher* p, unsigned scLocIdx );
  232. // Slide a score window 'hopCnt' times, beginning at 'bli' (an
  233. // index into p->mp->loc[]) looking for the best match to p->midiBuf[].
  234. // The score window contain scWndN (p->mp->mcn-1) score locations.
  235. // Returns the index into p->mp->loc[] of the start of the best
  236. // match score window. The score associated
  237. // with this match is stored in s_opt.
  238. unsigned cmScMatcherScan( cmScMatcher* p, unsigned bli, unsigned hopCnt );
  239. // Step forward/back by p->stepCnt from p->eli.
  240. // p->eli must therefore be valid prior to calling this function.
  241. // If more than p->maxMissCnt consecutive MIDI events are
  242. // missed then automatically run cmScAlignScan().
  243. // Return cmEofRC if the end of the score is encountered.
  244. // Return cmSubSysFailRC if an internal scan resync. failed.
  245. cmRC_t cmScMatcherStep( cmScMatcher* p );
  246. // This function calls cmScMatcherScan() and cmScMatcherStep() internally.
  247. // If 'status' is not kNonMidiMdId then the function returns without changing the
  248. // state of the object. In other words the matcher only recognizes MIDI note-on messages.
  249. // If the MIDI note passed by the call results in a successful match then
  250. // p->eli will be updated to the location in p->mp->loc[] of the latest
  251. // match, the MIDI note in p->midiBuf[] associated with this match
  252. // will be assigned a valid locIdx and scLocIdx values, and *scLocIdxPtr
  253. // will be set with the matched scLocIdx of the match.
  254. // If this call does not result in a successful match *scLocIdxPtr is set
  255. // to cmInvalidIdx.
  256. // Return:
  257. // cmOkRC - Continue processing MIDI events.
  258. // cmEofRC - The end of the score was encountered.
  259. // cmInvalidArgRC - scan failed or the object was in an invalid state to attempt a match.
  260. // cmSubSysFailRC - a scan resync failed in cmScMatcherStep().
  261. cmRC_t cmScMatcherExec( cmScMatcher* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1, unsigned* scLocIdxPtr );
  262. void cmScMatcherPrint( cmScMatcher* p );
  263. //=======================================================================================================================
  264. typedef struct
  265. {
  266. cmScoreSet_t* sp; // ptr to this set in the score
  267. unsigned bsei; // begin score event index
  268. unsigned esei; // end score event index
  269. unsigned bsli; // beg score loc index
  270. unsigned esli; // end score loc index
  271. unsigned bli; //
  272. unsigned eli; //
  273. double value; // DBL_MAX if the value has not yet been set
  274. double tempo; //
  275. double match_cost; // cost of the match to the performance divided by sp->eleCnt
  276. } cmScMeasSet_t;
  277. typedef struct
  278. {
  279. cmObj obj;
  280. double srate; //
  281. cmScMatch* mp; //
  282. unsigned mii; // next avail recd in midiBuf[]
  283. unsigned mn; // length of of midiBuf[] (init. to 2*cmScoreEvtCount())
  284. cmScMatchMidi_t* midiBuf; // midiBuf[mn]
  285. unsigned sn; // length of set[] (init. to cmScoreSetCount())
  286. cmScMeasSet_t* set; // set[sn]
  287. unsigned dn; // length of dynRef[]
  288. unsigned* dynRef; // dynRef[dn]
  289. unsigned nsi; // next set index to fill (this is the set[] we are waiting to complete)
  290. unsigned nsli; // next score location index we are expecting to receive
  291. unsigned vsi; // set[vsi:nsi-1] indicates sets with new values following a call to cmScMeasExec()
  292. unsigned vsli; // vsli:nsli-1 indicates cmScore loc's to check for section triggers following a call to cmScMeasExec()
  293. } cmScMeas;
  294. //
  295. // Notes:
  296. //
  297. // 1) midiBuf[] stores all MIDI notes for the duration of the performance
  298. // it is initialized to 2*score_event_count.
  299. //
  300. // 2) dynRef[] is the gives the MIDI velocity range for each dynamics
  301. // category: pppp-fff
  302. //
  303. // 3) See a cmDspKr.c _cmScFolMatcherCb() for an example of how
  304. // cmScMeas.vsi and cmScMeas.vsli are used to act on the results of
  305. // a call to cmMeasExec().
  306. cmScMeas* cmScMeasAlloc( cmCtx* c, cmScMeas* p, cmScH_t scH, double srate, const unsigned* dynRefArray, unsigned dynRefCnt );
  307. cmRC_t cmScMeasFree( cmScMeas** pp );
  308. cmRC_t cmScMeasInit( cmScMeas* p, cmScH_t scH, double srate, const unsigned* dynRefArray, unsigned dynRefCnt );
  309. cmRC_t cmScMeasFinal( cmScMeas* p );
  310. // Empty MIDI buffer and set the next set nsi and nsli to zero.
  311. cmRC_t cmScMeasReset( cmScMeas* p );
  312. // This function is called for each input MIDI note which is assigned a
  313. // score location by cmScMatcher.
  314. // 'mni' is the MIDI event index which uniquely identifies this MIDI event.
  315. // 'locIdx' is the location index into cmScMatcher.mp->loc[] associated with
  316. // this event.
  317. cmRC_t cmScMeasExec( cmScMeas* p, unsigned mni, unsigned locIdx, unsigned scEvtIdx, unsigned flags, unsigned smpIdx, unsigned pitch, unsigned vel );
  318. //=======================================================================================================================
  319. unsigned cmScAlignScanToTimeLineEvent( cmScMatcher* p, cmTlH_t tlH, cmTlObj_t* top, unsigned endSmpIdx );
  320. // Given a score, a time-line, and a marker on the time line scan the
  321. // entire score looking for the best match between the first 'midiN'
  322. // notes in each marker region and the score.
  323. void cmScAlignScanMarkers( cmRpt_t* rpt, cmTlH_t tlH, cmScH_t scH );
  324. //=======================================================================================================================
  325. /*
  326. Syntax: <loc> <mod> <var> <type> <params>
  327. <loc> - score location
  328. <mod> - name of the modulator
  329. <var> - variable name
  330. <type> - type of operation
  331. <params>
  332. <min> - set a variable min value
  333. <max> - set a variable max value
  334. <rate> - limit how often a variable is transmitted while it is ramping
  335. <val> - type dependent value - see 'Types' below.
  336. <end> - ending value for a ramping variable
  337. <dur> - determines the length of time to get to the ending value
  338. The value of parameters may be literal numeric values or may refer to
  339. variables by their name.
  340. Types:
  341. set = set <var> to <val> which may be a literal or another variable.
  342. line = ramp from its current value to <val> over <dur> seconds
  343. sline = set <var> to <val> and ramp to <end> over <dur> seconds
  344. post = send a 'post' msg after each transmission (can be used to change the cross-fader after each msg)
  345. */
  346. enum
  347. {
  348. kInvalidModTId,
  349. kSetModTId, // set variable to parray[0] at scLocIdx
  350. kLineModTId, // linear ramp variable to parray[0] over parray[1] seconds
  351. kSetLineModTId, // set variable to parray[0] and ramp to parray[1] over parray[2] seconds
  352. kPostModTId, //
  353. };
  354. enum
  355. {
  356. kActiveModFl = 0x01, // this variable is on the 'active' list
  357. kCalcModFl = 0x02 // when this variable is used as a parameter it's value must be calculated rather than used directly.
  358. };
  359. struct cmScModEntry_str;
  360. typedef enum
  361. {
  362. kInvalidModPId,
  363. kLiteralModPId, // this is a literal value
  364. kSymbolModPId //
  365. } cmScModPId_t;
  366. typedef struct cmScModParam_str
  367. {
  368. cmScModPId_t pid; // parameter type: literal or symbol
  369. unsigned symId; // symbol of external and internal variables
  370. double val; // value of literals
  371. } cmScModParam_t;
  372. typedef struct cmScModVar_str
  373. {
  374. unsigned flags; // see kXXXModFl flags above.
  375. unsigned varSymId; // variable name
  376. unsigned outVarId; // output var id
  377. double value; // current value of this variable
  378. double v0; // reserved internal variable
  379. unsigned phase; // cycle phase since activation
  380. double min;
  381. double max;
  382. double rate; // output rate in milliseconds (use
  383. struct cmScModEntry_str* entry; // last entry assoc'd with this value
  384. struct cmScModVar_str* vlink; // p->vlist link
  385. struct cmScModVar_str* alink; // p->alist link
  386. } cmScModVar_t;
  387. // Each entry gives a time tagged location and some parameters
  388. // for an algorthm which is used to set/modulate a value.
  389. typedef struct cmScModEntry_str
  390. {
  391. unsigned scLocIdx; // entry start time
  392. unsigned typeId; // variable type
  393. cmScModParam_t beg; // parameter values
  394. cmScModParam_t end; //
  395. cmScModParam_t dur; //
  396. cmScModParam_t min; // min value for this variable
  397. cmScModParam_t max; // max value for this variable
  398. cmScModParam_t rate; // update rate in milliseconds (DBL_MAX to disable)
  399. cmScModVar_t* varPtr; // target variable
  400. } cmScModEntry_t;
  401. typedef void (*cmScModCb_t)( void* cbArg, unsigned varSymId, double value, bool postFl );
  402. typedef struct
  403. {
  404. cmObj obj;
  405. cmChar_t* fn; // modulator score file
  406. unsigned modSymId; // modulator name
  407. cmSymTblH_t stH; // symbol table used by this modulator
  408. cmScModCb_t cbFunc; // active value callback function
  409. void* cbArg; // first arg to cbFunc()
  410. unsigned samplesPerCycle; // interval in samples between calls to cmScModulatorExec()
  411. double srate; // system sample rate
  412. cmScModEntry_t* earray; // earray[en] - entry array sorted on ascending cmScModEntry_t.scLocIdx
  413. unsigned en; // count
  414. cmScModVar_t* vlist; // variable list
  415. cmScModVar_t* alist; // active variable list
  416. cmScModVar_t* elist; // last element on the active list
  417. unsigned nei; // next entry index
  418. unsigned outVarCnt; // count of unique vars that are targets of entry recds
  419. bool postFl; // send a 'post' msg after each transmission
  420. } cmScModulator;
  421. cmScModulator* cmScModulatorAlloc( cmCtx* c, cmScModulator* p, cmCtx_t* ctx, cmSymTblH_t stH, double srate, unsigned samplesPerCycle, const cmChar_t* fn, const cmChar_t* modLabel, cmScModCb_t cbFunc, void* cbArg );
  422. cmRC_t cmScModulatorFree( cmScModulator** pp );
  423. cmRC_t cmScModulatorInit( cmScModulator* p, cmCtx_t* ctx, cmSymTblH_t stH, double srate, unsigned samplesPerCycle, const cmChar_t* fn, const cmChar_t* modLabel, cmScModCb_t cbFunc, void* cbArg );
  424. cmRC_t cmScModulatorFinal( cmScModulator* p );
  425. // Return count of variables.
  426. unsigned cmScModulatorOutVarCount( cmScModulator* p );
  427. // Return a pointer to the variable at vlist[idx].
  428. cmScModVar_t* cmScModulatorOutVar( cmScModulator* p, unsigned idx );
  429. cmRC_t cmScModulatorSetValue( cmScModulator* p, unsigned varSymId, double value, double min, double max );
  430. cmRC_t cmScModulatorReset( cmScModulator* p, cmCtx_t* ctx, unsigned scLocIdx );
  431. cmRC_t cmScModulatorExec( cmScModulator* p, unsigned scLocIdx );
  432. cmRC_t cmScModulatorDump( cmScModulator* p );
  433. //=======================================================================================================================
  434. //
  435. // Record fragments of audio, store them, and play them back at a later time.
  436. //
  437. typedef struct cmRecdPlayFrag_str
  438. {
  439. unsigned labelSymId; // this fragments label
  440. cmSample_t** chArray; // record buffer chArray[cmRecdPlay.chCnt][allocCnt]
  441. unsigned allocCnt; // count of samples allocated to each channel
  442. unsigned playIdx; // index of next sample to play
  443. unsigned recdIdx; // index of next sample to receieve audio (count of full samples)
  444. double fadeDbPerSec; // fade rate in dB per second
  445. unsigned fadeSmpIdx;
  446. struct cmRecdPlayFrag_str* rlink; // cmRecdPlay.rlist link
  447. struct cmRecdPlayFrag_str* plink; // cmRecdPlay.plist link
  448. } cmRecdPlayFrag;
  449. typedef struct
  450. {
  451. cmObj obj;
  452. cmRecdPlayFrag* frags; // frags[fragCnt] fragment array
  453. unsigned fragCnt; // count of fragments
  454. double srate; // system sample rate
  455. unsigned chCnt; // count of input and output audio channels
  456. double initFragSecs; // size initial memory allocated to each frag in seconds
  457. unsigned maxLaSmpCnt; // samples allocated to each channel of the look-ahead buffers.
  458. unsigned curLaSmpCnt; // current look-ahead time in samples (curLaSmpCnt<=maxLaSmpCnt)
  459. cmSample_t** laChs; // laChs[chCnt][maxLaSmpCnt] - look-ahead buffers
  460. int laSmpIdx; // next look-ahead buffer index to receive a sample
  461. cmRecdPlayFrag* plist; // currently playing frags
  462. cmRecdPlayFrag* rlist; // currently recording frags
  463. } cmRecdPlay;
  464. // srate - system sample rate
  465. // fragCnt - total count of samples to record
  466. // chCnt - count of input and output audio channels.
  467. // initFragSecs - amount of memory to pre-allocate for each fragment.
  468. // maxLaSecs - maximum value for curLaSecs
  469. // curLaSecs - current duration of look-ahead buffer
  470. //
  471. // The look-ahead buffer is a circular buffer which hold the previous 'curLaSecs' seconds
  472. // of incoming audio. When recording is enabled with via cmRecdPlayBeginRecord() the
  473. // look ahead buffer is automatically prepended to the fragment.
  474. cmRecdPlay* cmRecdPlayAlloc( cmCtx* c, cmRecdPlay* p, double srate, unsigned fragCnt, unsigned chCnt, double initFragSecs, double maxLaSecs, double curLaSecs );
  475. cmRC_t cmRecdPlayFree( cmRecdPlay** pp );
  476. cmRC_t cmRecdPlayInit( cmRecdPlay* p, double srate, unsigned flagCnt, unsigned chCnt, double initFragSecs, double maxLaSecs, double curLaSecs );
  477. cmRC_t cmRecdPlayFinal( cmRecdPlay* p );
  478. cmRC_t cmRecdPlayRegisterFrag( cmRecdPlay* p, unsigned fragIdx, unsigned labelSymId );
  479. cmRC_t cmRecdPlaySetLaSecs( cmRecdPlay* p, double curLaSecs );
  480. // Deactivates all active recorders and players, zeros the look-ahead buffer and
  481. // rewinds all fragment play positions. This function does not clear the audio from
  482. // frabments that have already been recorded.
  483. cmRC_t cmRecdPlayRewind( cmRecdPlay* p );
  484. cmRC_t cmRecdPlayBeginRecord( cmRecdPlay* p, unsigned labelSymId );
  485. cmRC_t cmRecdPlayEndRecord( cmRecdPlay* p, unsigned labelSymId );
  486. cmRC_t cmRecdPlayInsertRecord(cmRecdPlay* p, unsigned labelSymId, const cmChar_t* wavFn );
  487. cmRC_t cmRecdPlayBeginPlay( cmRecdPlay* p, unsigned labelSymId );
  488. cmRC_t cmRecdPlayEndPlay( cmRecdPlay* p, unsigned labelSymId );
  489. // Begin fading out the specified fragment at a rate deteremined by 'dbPerSec'.
  490. cmRC_t cmRecdPlayBeginFade( cmRecdPlay* p, unsigned labelSymId, double fadeDbPerSec );
  491. cmRC_t cmRecdPlayExec( cmRecdPlay* p, const cmSample_t** iChs, cmSample_t** oChs, unsigned chCnt, unsigned smpCnt );
  492. #ifdef __cplusplus
  493. }
  494. #endif
  495. #endif