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.

cmProc4.h 27KB

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