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

cmProc4.h 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 muid; // MIDI file event msg unique id (See cmMidiTrackMsg_t.uid)
  127. unsigned smpIdx; // time stamp of this event
  128. unsigned pitch; // MIDI note pitch
  129. unsigned vel; // " " velocity
  130. unsigned locIdx; // location assoc'd with this MIDI evt (cmInvalidIdx if not a matching or non-matching 'substitute')
  131. unsigned scEvtIdx; // cmScore event index assoc'd with this event
  132. } cmScMatchMidi_t;
  133. typedef struct
  134. {
  135. cmObj obj; //
  136. cmScH_t scH; // cmScore handle
  137. unsigned locN; //
  138. cmScMatchLoc_t* loc; // loc[locN]
  139. unsigned mrn; // max m[] row count (midi)
  140. unsigned rn; // cur m[] row count
  141. unsigned mcn; // max m[] column count (score)
  142. unsigned cn; // cur m[] column count
  143. unsigned mmn; // max length of midiBuf[] (mrn-1)
  144. unsigned msn; // max length of score window (mcn-1)
  145. cmScMatchVal_t* m; // m[mrn,mcn] DP matrix
  146. unsigned pn; // mrn+mcn
  147. cmScMatchPath_t* p_mem; // pmem[ 2*pn ] - path memory
  148. cmScMatchPath_t* p_avl; // available path record linked list
  149. cmScMatchPath_t* p_cur; // current path linked list
  150. cmScMatchPath_t* p_opt; // p_opt[pn] - current best alignment as a linked list
  151. double opt_cost; // last p_opt cost set by cmScMatchExec()
  152. } cmScMatch;
  153. /*
  154. 1) This matcher cannot handle multiple instances of the same pitch occuring
  155. at the same 'location'.
  156. 2) Because each note of a chord is spread out over multiple locations, and
  157. there is no way to indicate that a note in the chord is already 'in-use'.
  158. If a MIDI note which is part of the chord is repeated, in error, it will
  159. appear to be correct (a positive match will be assigned to
  160. the second (and possible successive notes)).
  161. */
  162. cmScMatch* cmScMatchAlloc( cmCtx* c, cmScMatch* p, cmScH_t scH, unsigned maxScWndN, unsigned maxMidiWndN );
  163. cmRC_t cmScMatchFree( cmScMatch** pp );
  164. cmRC_t cmScMatchInit( cmScMatch* p, cmScH_t scH, unsigned maxScWndN, unsigned maxMidiWndN );
  165. cmRC_t cmScMatchFinal( cmScMatch* p );
  166. // Locate the position in p->loc[locIdx:locIdx+locN-1] which bests
  167. // matches midiV[midiN].
  168. // The result of this function is to update p_opt[]
  169. // The optimal path p_opt[] will only be updated if the edit_cost associated 'midiV[midiN]'.
  170. // with the best match is less than 'min_cost'.
  171. // Set 'min_cost' to DBL_MAX to force p_opt[] to be updated.
  172. // Returns cmEofRC if locIdx + locN > p->locN - note that this is not
  173. // necessarily an error.
  174. cmRC_t cmScMatchExec( cmScMatch* p, unsigned locIdx, unsigned locN, const cmScMatchMidi_t* midiV, unsigned midiN, double min_cost );
  175. //=======================================================================================================================
  176. typedef struct
  177. {
  178. unsigned locIdx; // index into cmScMatch_t.loc[]
  179. unsigned scEvtIdx; // score event index
  180. unsigned mni; // index of the performed MIDI event associated with this score location
  181. unsigned smpIdx; // sample time index of performed MIDI event
  182. unsigned muid; // MIDI file event msg unique id (See cmMidiTrackMsg_t.uid)
  183. unsigned pitch; // performed pitch
  184. unsigned vel; // performed velocity
  185. unsigned flags; // smTruePosFl | smFalsePosFl
  186. } cmScMatcherResult_t;
  187. struct cmScMatcher_str;
  188. typedef void (*cmScMatcherCb_t)( struct cmScMatcher_str* p, void* arg, cmScMatcherResult_t* rp );
  189. typedef struct cmScMatcher_str
  190. {
  191. cmObj obj;
  192. cmScMatcherCb_t cbFunc;
  193. void* cbArg;
  194. cmScMatch* mp;
  195. unsigned mn;
  196. cmScMatchMidi_t* midiBuf; // midiBuf[mn]
  197. cmScMatcherResult_t* res; // res[rn]
  198. unsigned rn; // length of res[] (set to 2*score event count)
  199. unsigned ri; // next avail res[] recd.
  200. double s_opt; //
  201. unsigned missCnt; // current count of consecutive trailing non-matches
  202. unsigned ili; // index into loc[] to start scan following reset
  203. unsigned eli; // index into loc[] of the last positive match.
  204. unsigned mni; // current count of MIDI events since the last call to cmScMatcherReset()
  205. unsigned mbi; // index of oldest MIDI event in midiBuf[]; stays at 0 when the buffer is full.
  206. unsigned begSyncLocIdx; // start of score window, in mp->loc[], of best match in previous scan
  207. unsigned initHopCnt; // max window hops during the initial (when the MIDI buffer fills for first time) sync scan
  208. unsigned stepCnt; // count of forward/backward score loc's to examine for a match during cmScMatcherStep().
  209. unsigned maxMissCnt; // max. number of consecutive non-matches during step prior to executing a scan.
  210. unsigned scanCnt; // current count of times a resync-scan was executed during cmScMatcherStep()
  211. bool printFl;
  212. } cmScMatcher;
  213. cmScMatcher* cmScMatcherAlloc(
  214. cmCtx* c, // Program context.
  215. cmScMatcher* p, // Existing cmScMatcher to reallocate or NULL to allocate a new cmScMatcher.
  216. double srate, // System sample rate.
  217. cmScH_t scH, // Score handle. See cmScore.h.
  218. unsigned scWndN, // Length of the scores active search area. ** See Notes.
  219. unsigned midiWndN, // Length of the MIDI active note buffer. ** See Notes.
  220. cmScMatcherCb_t cbFunc, // A cmScMatcherCb_t function to be called to notify the recipient of changes in the score matcher status.
  221. void* cbArg ); // User argument to 'cbFunc'.
  222. // Notes:
  223. // The cmScMatcher maintains an internal cmScMatch object which is used to attempt to find the
  224. // best match between the current MIDI active note buffer and the current score search area.
  225. // 'scWndN' is used to set the cmScMatch 'locN' argument.
  226. // 'midiWndN' sets the length of the MIDI FIFO which is used to match to the score with
  227. // each recceived MIDI note.
  228. // 'midiWndN' must be <= 'scWndN'.
  229. cmRC_t cmScMatcherFree( cmScMatcher** pp );
  230. cmRC_t cmScMatcherInit( cmScMatcher* p, double srate, cmScH_t scH, unsigned scWndN, unsigned midiWndN, cmScMatcherCb_t cbFunc, void* cbArg );
  231. cmRC_t cmScMatcherFinal( cmScMatcher* p );
  232. // 'scLocIdx' is a score index as used by cmScoreLoc(scH) not into p->mp->loc[].
  233. cmRC_t cmScMatcherReset( cmScMatcher* p, unsigned scLocIdx );
  234. // Slide a score window 'hopCnt' times, beginning at 'bli' (an
  235. // index into p->mp->loc[]) looking for the best match to p->midiBuf[].
  236. // The score window contain scWndN (p->mp->mcn-1) score locations.
  237. // Returns the index into p->mp->loc[] of the start of the best
  238. // match score window. The score associated
  239. // with this match is stored in s_opt.
  240. unsigned cmScMatcherScan( cmScMatcher* p, unsigned bli, unsigned hopCnt );
  241. // Step forward/back by p->stepCnt from p->eli.
  242. // p->eli must therefore be valid prior to calling this function.
  243. // If more than p->maxMissCnt consecutive MIDI events are
  244. // missed then automatically run cmScAlignScan().
  245. // Return cmEofRC if the end of the score is encountered.
  246. // Return cmSubSysFailRC if an internal scan resync. failed.
  247. cmRC_t cmScMatcherStep( cmScMatcher* p );
  248. // This function calls cmScMatcherScan() and cmScMatcherStep() internally.
  249. // If 'status' is not kNonMidiMdId then the function returns without changing the
  250. // state of the object. In other words the matcher only recognizes MIDI note-on messages.
  251. // If the MIDI note passed by the call results in a successful match then
  252. // p->eli will be updated to the location in p->mp->loc[] of the latest
  253. // match, the MIDI note in p->midiBuf[] associated with this match
  254. // will be assigned a valid locIdx and scLocIdx values, and *scLocIdxPtr
  255. // will be set with the matched scLocIdx of the match.
  256. // If this call does not result in a successful match *scLocIdxPtr is set
  257. // to cmInvalidIdx.
  258. // 'muid' is the unique id associated with this MIDI event under the circumstances
  259. // that the event came from a MIDI file. See cmMidiFile.h cmMidiTrackMsg_t.uid.
  260. // Return:
  261. // cmOkRC - Continue processing MIDI events.
  262. // cmEofRC - The end of the score was encountered.
  263. // cmInvalidArgRC - scan failed or the object was in an invalid state to attempt a match.
  264. // cmSubSysFailRC - a scan resync failed in cmScMatcherStep().
  265. cmRC_t cmScMatcherExec( cmScMatcher* p, unsigned smpIdx, unsigned muid, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1, unsigned* scLocIdxPtr );
  266. void cmScMatcherPrint( cmScMatcher* p );
  267. //=======================================================================================================================
  268. typedef struct
  269. {
  270. cmScoreSet_t* sp; // ptr to this set in the score
  271. unsigned bsei; // begin score event index
  272. unsigned esei; // end score event index
  273. unsigned bsli; // beg score loc index
  274. unsigned esli; // end score loc index
  275. unsigned bli; //
  276. unsigned eli; //
  277. double value; // DBL_MAX if the value has not yet been set
  278. double tempo; //
  279. double match_cost; // cost of the match to the performance divided by sp->eleCnt
  280. } cmScMeasSet_t;
  281. typedef struct
  282. {
  283. cmObj obj;
  284. double srate; //
  285. cmScMatch* mp; //
  286. unsigned mii; // next avail recd in midiBuf[]
  287. unsigned mn; // length of of midiBuf[] (init. to 2*cmScoreEvtCount())
  288. cmScMatchMidi_t* midiBuf; // midiBuf[mn]
  289. unsigned sn; // length of set[] (init. to cmScoreSetCount())
  290. cmScMeasSet_t* set; // set[sn]
  291. unsigned dn; // length of dynRef[]
  292. unsigned* dynRef; // dynRef[dn]
  293. unsigned nsi; // next set index to fill (this is the set[] we are waiting to complete)
  294. unsigned nsli; // next score location index we are expecting to receive
  295. unsigned vsi; // set[vsi:nsi-1] indicates sets with new values following a call to cmScMeasExec()
  296. unsigned vsli; // vsli:nsli-1 indicates cmScore loc's to check for section triggers following a call to cmScMeasExec()
  297. } cmScMeas;
  298. //
  299. // Notes:
  300. //
  301. // 1) midiBuf[] stores all MIDI notes for the duration of the performance
  302. // it is initialized to 2*score_event_count.
  303. //
  304. // 2) dynRef[] is the gives the MIDI velocity range for each dynamics
  305. // category: pppp-fff
  306. //
  307. // 3) See a cmDspKr.c _cmScFolMatcherCb() for an example of how
  308. // cmScMeas.vsi and cmScMeas.vsli are used to act on the results of
  309. // a call to cmMeasExec().
  310. cmScMeas* cmScMeasAlloc( cmCtx* c, cmScMeas* p, cmScH_t scH, double srate, const unsigned* dynRefArray, unsigned dynRefCnt );
  311. cmRC_t cmScMeasFree( cmScMeas** pp );
  312. cmRC_t cmScMeasInit( cmScMeas* p, cmScH_t scH, double srate, const unsigned* dynRefArray, unsigned dynRefCnt );
  313. cmRC_t cmScMeasFinal( cmScMeas* p );
  314. // Empty MIDI buffer and set the next set nsi and nsli to zero.
  315. cmRC_t cmScMeasReset( cmScMeas* p );
  316. // This function is called for each input MIDI note which is assigned a
  317. // score location by cmScMatcher.
  318. // 'mni' is the MIDI event index which uniquely identifies this MIDI event.
  319. // 'locIdx' is the location index into cmScMatcher.mp->loc[] associated with
  320. // this event.
  321. cmRC_t cmScMeasExec( cmScMeas* p, unsigned mni, unsigned locIdx, unsigned scEvtIdx, unsigned flags, unsigned smpIdx, unsigned pitch, unsigned vel );
  322. //=======================================================================================================================
  323. unsigned cmScAlignScanToTimeLineEvent( cmScMatcher* p, cmTlH_t tlH, cmTlObj_t* top, unsigned endSmpIdx );
  324. // Given a score, a time-line, and a marker on the time line scan the
  325. // entire score looking for the best match between the first 'midiN'
  326. // notes in each marker region and the score.
  327. void cmScAlignScanMarkers( cmRpt_t* rpt, cmTlH_t tlH, cmScH_t scH );
  328. //=======================================================================================================================
  329. /*
  330. Syntax: <loc> <mod> <var> <type> <params>
  331. <loc> - score location
  332. <mod> - name of the modulator
  333. <var> - variable name
  334. <type> - type of operation
  335. <params>
  336. <min> - set a variable min value
  337. <max> - set a variable max value
  338. <rate> - limit how often a variable is transmitted while it is ramping
  339. <val> - type dependent value - see 'Types' below.
  340. <end> - ending value for a ramping variable
  341. <dur> - determines the length of time to get to the ending value
  342. The value of parameters may be literal numeric values or may refer to
  343. variables by their name.
  344. Types:
  345. set = set <var> to <val> which may be a literal or another variable.
  346. line = ramp from its current value to <val> over <dur> seconds
  347. sline = set <var> to <val> and ramp to <end> over <dur> seconds
  348. post = send a 'post' msg after each transmission (can be used to change the cross-fader after each msg)
  349. */
  350. enum
  351. {
  352. kInvalidModTId,
  353. kDeclModTId, // declare a variable but do not associate a value with it (allows a variable to be connected to w/o sending a value)
  354. kSetModTId, // set variable to parray[0] at scLocIdx
  355. kLineModTId, // linear ramp variable to parray[0] over parray[1] seconds
  356. kSetLineModTId, // set variable to parray[0] and ramp to parray[1] over parray[2] seconds
  357. kPostModTId, //
  358. };
  359. enum
  360. {
  361. kActiveModFl = 0x01, // this variable is on the 'active' list
  362. kCalcModFl = 0x02 // when this variable is used as a parameter it's value must be calculated rather than used directly.
  363. };
  364. struct cmScModEntry_str;
  365. typedef enum
  366. {
  367. kInvalidModPId,
  368. kLiteralModPId, // this is a literal value
  369. kSymbolModPId //
  370. } cmScModPId_t;
  371. typedef struct cmScModParam_str
  372. {
  373. cmScModPId_t pid; // parameter type: literal or symbol
  374. unsigned symId; // symbol of external and internal variables
  375. double val; // value of literals
  376. } cmScModParam_t;
  377. typedef struct cmScModVar_str
  378. {
  379. unsigned flags; // see kXXXModFl flags above.
  380. unsigned varSymId; // variable name
  381. unsigned outVarId; // output var id
  382. double value; // current value of this variable
  383. double v0; // reserved internal variable
  384. unsigned phase; // cycle phase since activation
  385. double min;
  386. double max;
  387. double rate; // output rate in milliseconds (use
  388. struct cmScModEntry_str* entry; // last entry assoc'd with this value
  389. struct cmScModVar_str* vlink; // p->vlist link
  390. struct cmScModVar_str* alink; // p->alist link
  391. } cmScModVar_t;
  392. // Each entry gives a time tagged location and some parameters
  393. // for an algorthm which is used to set/modulate a value.
  394. typedef struct cmScModEntry_str
  395. {
  396. unsigned scLocIdx; // entry start time
  397. unsigned typeId; // variable type
  398. cmScModParam_t beg; // parameter values
  399. cmScModParam_t end; //
  400. cmScModParam_t dur; //
  401. cmScModParam_t min; // min value for this variable
  402. cmScModParam_t max; // max value for this variable
  403. cmScModParam_t rate; // update rate in milliseconds (DBL_MAX to disable)
  404. cmScModVar_t* varPtr; // target variable
  405. } cmScModEntry_t;
  406. typedef void (*cmScModCb_t)( void* cbArg, unsigned varSymId, double value, bool postFl );
  407. typedef struct
  408. {
  409. cmObj obj;
  410. cmChar_t* fn; // modulator score file
  411. unsigned modSymId; // modulator name
  412. cmSymTblH_t stH; // symbol table used by this modulator
  413. cmScModCb_t cbFunc; // active value callback function
  414. void* cbArg; // first arg to cbFunc()
  415. unsigned samplesPerCycle; // interval in samples between calls to cmScModulatorExec()
  416. double srate; // system sample rate
  417. cmScModEntry_t* earray; // earray[en] - entry array sorted on ascending cmScModEntry_t.scLocIdx
  418. unsigned en; // count
  419. cmScModVar_t* vlist; // variable list
  420. cmScModVar_t* alist; // active variable list
  421. cmScModVar_t* elist; // last element on the active list
  422. unsigned nei; // next entry index
  423. unsigned outVarCnt; // count of unique vars that are targets of entry recds
  424. bool postFl; // send a 'post' msg after each transmission
  425. } cmScModulator;
  426. 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 );
  427. cmRC_t cmScModulatorFree( cmScModulator** pp );
  428. 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 );
  429. cmRC_t cmScModulatorFinal( cmScModulator* p );
  430. // Return count of variables.
  431. unsigned cmScModulatorOutVarCount( cmScModulator* p );
  432. // Return a pointer to the variable at vlist[idx].
  433. cmScModVar_t* cmScModulatorOutVar( cmScModulator* p, unsigned idx );
  434. cmRC_t cmScModulatorSetValue( cmScModulator* p, unsigned varSymId, double value, double min, double max );
  435. cmRC_t cmScModulatorReset( cmScModulator* p, cmCtx_t* ctx, unsigned scLocIdx );
  436. cmRC_t cmScModulatorExec( cmScModulator* p, unsigned scLocIdx );
  437. cmRC_t cmScModulatorDump( cmScModulator* p );
  438. //=======================================================================================================================
  439. //
  440. // Record fragments of audio, store them, and play them back at a later time.
  441. //
  442. typedef struct cmRecdPlayFrag_str
  443. {
  444. unsigned labelSymId; // this fragments label
  445. cmSample_t** chArray; // record buffer chArray[cmRecdPlay.chCnt][allocCnt]
  446. unsigned allocCnt; // count of samples allocated to each channel
  447. unsigned playIdx; // index of next sample to play
  448. unsigned recdIdx; // index of next sample to receieve audio (count of full samples)
  449. double fadeDbPerSec; // fade rate in dB per second
  450. unsigned fadeSmpIdx;
  451. struct cmRecdPlayFrag_str* rlink; // cmRecdPlay.rlist link
  452. struct cmRecdPlayFrag_str* plink; // cmRecdPlay.plist link
  453. } cmRecdPlayFrag;
  454. typedef struct
  455. {
  456. cmObj obj;
  457. cmRecdPlayFrag* frags; // frags[fragCnt] fragment array
  458. unsigned fragCnt; // count of fragments
  459. double srate; // system sample rate
  460. unsigned chCnt; // count of input and output audio channels
  461. double initFragSecs; // size initial memory allocated to each frag in seconds
  462. unsigned maxLaSmpCnt; // samples allocated to each channel of the look-ahead buffers.
  463. unsigned curLaSmpCnt; // current look-ahead time in samples (curLaSmpCnt<=maxLaSmpCnt)
  464. cmSample_t** laChs; // laChs[chCnt][maxLaSmpCnt] - look-ahead buffers
  465. int laSmpIdx; // next look-ahead buffer index to receive a sample
  466. cmRecdPlayFrag* plist; // currently playing frags
  467. cmRecdPlayFrag* rlist; // currently recording frags
  468. } cmRecdPlay;
  469. // srate - system sample rate
  470. // fragCnt - total count of samples to record
  471. // chCnt - count of input and output audio channels.
  472. // initFragSecs - amount of memory to pre-allocate for each fragment.
  473. // maxLaSecs - maximum value for curLaSecs
  474. // curLaSecs - current duration of look-ahead buffer
  475. //
  476. // The look-ahead buffer is a circular buffer which hold the previous 'curLaSecs' seconds
  477. // of incoming audio. When recording is enabled with via cmRecdPlayBeginRecord() the
  478. // look ahead buffer is automatically prepended to the fragment.
  479. cmRecdPlay* cmRecdPlayAlloc( cmCtx* c, cmRecdPlay* p, double srate, unsigned fragCnt, unsigned chCnt, double initFragSecs, double maxLaSecs, double curLaSecs );
  480. cmRC_t cmRecdPlayFree( cmRecdPlay** pp );
  481. cmRC_t cmRecdPlayInit( cmRecdPlay* p, double srate, unsigned flagCnt, unsigned chCnt, double initFragSecs, double maxLaSecs, double curLaSecs );
  482. cmRC_t cmRecdPlayFinal( cmRecdPlay* p );
  483. cmRC_t cmRecdPlayRegisterFrag( cmRecdPlay* p, unsigned fragIdx, unsigned labelSymId );
  484. cmRC_t cmRecdPlaySetLaSecs( cmRecdPlay* p, double curLaSecs );
  485. // Deactivates all active recorders and players, zeros the look-ahead buffer and
  486. // rewinds all fragment play positions. This function does not clear the audio from
  487. // frabments that have already been recorded.
  488. cmRC_t cmRecdPlayRewind( cmRecdPlay* p );
  489. cmRC_t cmRecdPlayBeginRecord( cmRecdPlay* p, unsigned labelSymId );
  490. cmRC_t cmRecdPlayEndRecord( cmRecdPlay* p, unsigned labelSymId );
  491. cmRC_t cmRecdPlayInsertRecord(cmRecdPlay* p, unsigned labelSymId, const cmChar_t* wavFn );
  492. cmRC_t cmRecdPlayBeginPlay( cmRecdPlay* p, unsigned labelSymId );
  493. cmRC_t cmRecdPlayEndPlay( cmRecdPlay* p, unsigned labelSymId );
  494. // Begin fading out the specified fragment at a rate deteremined by 'dbPerSec'.
  495. cmRC_t cmRecdPlayBeginFade( cmRecdPlay* p, unsigned labelSymId, double fadeDbPerSec );
  496. cmRC_t cmRecdPlayExec( cmRecdPlay* p, const cmSample_t** iChs, cmSample_t** oChs, unsigned chCnt, unsigned smpCnt );
  497. #ifdef __cplusplus
  498. }
  499. #endif
  500. #endif