The base-application for Laborejo, Fluajho, Patroneo, Vico etc.
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.

2021 lines
85KB

  1. /*
  2. Calf Box, an open source musical instrument.
  3. Copyright (C) 2010-2011 Krzysztof Foltman
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "config-api.h"
  16. #include "dspmath.h"
  17. #include "errors.h"
  18. #include "midi.h"
  19. #include "module.h"
  20. #include "rt.h"
  21. #include "sampler.h"
  22. #include "sampler_impl.h"
  23. #include "sfzloader.h"
  24. #include <assert.h>
  25. #include <errno.h>
  26. #include <glib.h>
  27. #include <math.h>
  28. #include <memory.h>
  29. #include <sndfile.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <locale.h>
  33. static inline gboolean sampler_modulation_key_equal(const struct sampler_modulation_key *k1, const struct sampler_modulation_key *k2)
  34. {
  35. return (k1->src == k2->src && k1->src2 == k2->src2 && k1->dest == k2->dest);
  36. }
  37. static inline void sampler_modulation_dump_one(const struct sampler_modulation *sm)
  38. {
  39. printf("%d x %d -> %d : %f : %d\n", sm->key.src, sm->key.src2, sm->key.dest, sm->value.amount, sm->value.curve_id);
  40. }
  41. //////////////////////////////////////////////////////////////////////////////////////////////////
  42. static inline gboolean sampler_noteinitfunc_key_equal(const struct sampler_noteinitfunc_key *k1, const struct sampler_noteinitfunc_key *k2)
  43. {
  44. return (k1->notefunc_voice == k2->notefunc_voice && k1->variant == k2->variant);
  45. }
  46. static inline void sampler_noteinitfunc_dump_one(const struct sampler_noteinitfunc *nif)
  47. {
  48. printf("%p(%d) = %f\n", nif->key.notefunc_voice, nif->key.variant, nif->value.value);
  49. }
  50. //////////////////////////////////////////////////////////////////////////////////////////////////
  51. static inline gboolean sampler_cc_range_key_equal(const struct sampler_cc_range_key *k1, const struct sampler_cc_range_key *k2)
  52. {
  53. return k1->cc_number == k2->cc_number;
  54. }
  55. static inline void sampler_cc_range_dump_one(const struct sampler_cc_range *ccrange)
  56. {
  57. printf("CC%d in [%c%d, %c%d]\n", (int)ccrange->key.cc_number, ccrange->value.has_locc ? '!' : '.', (int)ccrange->value.locc, ccrange->value.has_hicc ? '!' : '.', (int)ccrange->value.hicc);
  58. }
  59. //////////////////////////////////////////////////////////////////////////////////////////////////
  60. static inline gboolean sampler_flex_lfo_key_equal(const struct sampler_flex_lfo_key *k1, const struct sampler_flex_lfo_key *k2)
  61. {
  62. return k1->id == k2->id;
  63. }
  64. static inline void sampler_flex_lfo_dump_one(const struct sampler_flex_lfo *lfo)
  65. {
  66. printf("LFO%d (freq %s %f, delay %s %f, fade %s %f, wave %s %d)\n",
  67. (int)lfo->key.id,
  68. lfo->value.has_freq ? "(local)" : "(inherited)", lfo->value.freq,
  69. lfo->value.has_delay ? "(local)" : "(inherited)", lfo->value.delay,
  70. lfo->value.has_fade ? "(local)" : "(inherited)", lfo->value.fade,
  71. lfo->value.has_wave ? "(local)" : "(inherited)", lfo->value.wave
  72. );
  73. }
  74. //////////////////////////////////////////////////////////////////////////////////////////////////
  75. #define SAMPLER_COLL_FUNC_DUMP(sname) \
  76. void sname##_dump(const struct sname *p) \
  77. { \
  78. for(; p; p = p->next) \
  79. sname##_dump_one(p); \
  80. }
  81. SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_DUMP)
  82. #define SAMPLER_COLL_FUNC_FIND(sname) \
  83. static struct sname *sname##_find(struct sname *list, const struct sname##_key *key) \
  84. { \
  85. for(struct sname *p = list; p; p = p->next) \
  86. { \
  87. struct sname##_key *dkey = &p->key; \
  88. if (sname##_key_equal(dkey, key)) \
  89. return p; \
  90. } \
  91. return NULL; \
  92. } \
  93. static struct sname *sname##_find2(struct sname **list_ptr, const struct sname##_key *key, struct sname ***link_ptr) \
  94. { \
  95. for(struct sname **pp = list_ptr; *pp; pp = &(*pp)->next) \
  96. { \
  97. struct sname##_key *dkey = &(*pp)->key; \
  98. if (sname##_key_equal(dkey, key)) \
  99. { \
  100. if (link_ptr) \
  101. *link_ptr = pp; \
  102. return *pp; \
  103. } \
  104. } \
  105. return NULL; \
  106. }
  107. SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_FIND)
  108. #define SAMPLER_COLL_FIELD_INIT(name, has_name, type, init_value) \
  109. d->value.name = init_value; \
  110. d->value.has_name = FALSE;
  111. #define SAMPLER_COLL_FUNC_ADD(sname) \
  112. static struct sname *sname##_add(struct sname **list_ptr, const struct sname##_key *key) \
  113. { \
  114. struct sname *d = sname##_find(*list_ptr, key); \
  115. if (d) \
  116. return d; \
  117. d = g_malloc0(sizeof(struct sname)); \
  118. d->key = *key; \
  119. SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_INIT)\
  120. d->next = *list_ptr; \
  121. *list_ptr = d; \
  122. return d; \
  123. }
  124. SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_ADD)
  125. #define SAMPLER_COLL_FUNC_DESTROY(sname) \
  126. static void sname##s_destroy(struct sname *list_ptr) \
  127. { \
  128. while(list_ptr) \
  129. { \
  130. struct sname *p = list_ptr->next; \
  131. g_free(list_ptr); \
  132. list_ptr = p; \
  133. } \
  134. }
  135. SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_DESTROY)
  136. #define SAMPLER_COLL_FIELD_ISNULL(name, has_name, type, init_value) \
  137. if (d->name != init_value || d->has_name) \
  138. return FALSE;
  139. #define SAMPLER_COLL_FUNC_ISNULL(sname) \
  140. static inline gboolean sname##_is_null(const struct sname##_value *d) \
  141. { \
  142. SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_ISNULL) \
  143. return TRUE; \
  144. }
  145. SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_ISNULL)
  146. // sampler_modulation_set_amount etc.
  147. #define SAMPLER_COLL_FIELD_SETTER(name, has_name, type, init_value, sname) \
  148. struct sname *sname##_set_##name##_by_offset(struct sampler_layer *l, uint32_t offset, const struct sname##_key *key, gboolean set_local_value, type value) \
  149. { \
  150. void *vl = &l->data; \
  151. struct sname **list_ptr = vl + offset; \
  152. struct sname *dstm = sname##_add(list_ptr, key); \
  153. if (!set_local_value && dstm->value.has_name) \
  154. return dstm; \
  155. dstm->value.has_name = set_local_value; \
  156. dstm->value.name = value; \
  157. return dstm; \
  158. }
  159. #define SAMPLER_COLL_FUNC_SETTERS(sname) \
  160. SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_SETTER, sname)
  161. SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_SETTERS)
  162. #define SAMPLER_COLL_FIELD_UNSET(name, has_name, type, init_value, sname) \
  163. if ((unset_mask & (1 << sname##_value_field_##name)) && d->value.has_name == remove_local) \
  164. { \
  165. d->value.name = parent ? parent->value.name : init_value; \
  166. d->value.has_name = FALSE; \
  167. } \
  168. #define SAMPLER_COLL_FIELD_KEY_ENUM_VALUE(name, has_name, type, init_value, sname) \
  169. sname##_value_field_##name,
  170. #define SAMPLER_COLL_FUNC_UNSET(sname) \
  171. enum { \
  172. SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_KEY_ENUM_VALUE, sname) \
  173. }; \
  174. static gboolean sname##_unset_by_offset(struct sampler_layer *l, uint32_t offset, const struct sname##_key *key, gboolean remove_local, uint32_t unset_mask) \
  175. { \
  176. void *vl = &l->data, *vp = l->parent ? &l->parent->data : NULL; \
  177. struct sname **list_ptr = vl + offset; \
  178. struct sname **parent_list_ptr = vp ? vp + offset : NULL; \
  179. struct sname **link_ptr = NULL; \
  180. struct sname *d = sname##_find2(list_ptr, key, &link_ptr); \
  181. if (!d) \
  182. return FALSE; \
  183. struct sname *parent = remove_local && *parent_list_ptr != NULL ? sname##_find(*parent_list_ptr, key) : NULL; \
  184. SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_UNSET, sname) \
  185. /* Delete if it's all default values and it's not overriding anything */ \
  186. if (sname##_is_null(&d->value)) {\
  187. *link_ptr = d->next; \
  188. g_free(d); \
  189. } \
  190. return TRUE; \
  191. }
  192. SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_UNSET)
  193. #define SAMPLER_COLL_FIELD_ZEROHASATTR(name, has_name, type, init_value) \
  194. dstv->value.has_name = FALSE;
  195. #define SAMPLER_COLL_FUNC_CLONE(sname) \
  196. static struct sname *sname##_clone(struct sname *src, gboolean copy_hasattr) \
  197. { \
  198. struct sname *dst = NULL, **last = &dst;\
  199. for(const struct sname *srcv = src; srcv; srcv = srcv->next) \
  200. { \
  201. struct sname *dstv = g_malloc(sizeof(struct sname)); \
  202. memcpy(dstv, srcv, sizeof(struct sname)); \
  203. if (!copy_hasattr) \
  204. { \
  205. SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_ZEROHASATTR) \
  206. } \
  207. *last = dstv; \
  208. dstv->next = NULL; \
  209. last = &dstv->next; \
  210. } \
  211. return dst; \
  212. }
  213. SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_CLONE)
  214. //////////////////////////////////////////////////////////////////////////////////////////////////
  215. enum sampler_layer_param_type
  216. {
  217. slpt_invalid,
  218. slpt_alias,
  219. slpt_int,
  220. slpt_uint32_t,
  221. slpt_float,
  222. slpt_dBamp,
  223. slpt_midi_note_t,
  224. slpt_enum,
  225. slpt_string,
  226. slpt_midicurve,
  227. slpt_ccrange,
  228. // modulation matrix
  229. slpt_mod_amount, // src (or CC) * src2 (or CC) -> dest
  230. slpt_mod_curveid,
  231. slpt_mod_smooth,
  232. slpt_mod_step,
  233. slpt_generic_modulation,
  234. // note init functions
  235. slpt_voice_nif,
  236. slpt_prevoice_nif,
  237. slpt_flex_lfo,
  238. slpt_reserved,
  239. };
  240. struct sampler_layer_param_entry
  241. {
  242. const char *name;
  243. size_t offset;
  244. enum sampler_layer_param_type type;
  245. double def_value;
  246. uint64_t extra_int;
  247. void *extra_ptr;
  248. void (*set_has_value)(struct sampler_layer_data *, gboolean);
  249. gboolean (*get_has_value)(struct sampler_layer_data *);
  250. };
  251. #define MODSRC_CC 0xFFF
  252. #define smsrc_CC MODSRC_CC
  253. #define ENCODE_MOD(src, src2, dst) ((((uint32_t)(src) & 0xFFFU) | (((uint32_t)(src2) & 0xFFFU) << 12U) | ((uint32_t)(dst) << 24U)))
  254. #define PROC_SUBSTRUCT_FIELD_SETHASFUNC(name, index, def_value, parent) \
  255. static void sampler_layer_data_##parent##_set_has_##name(struct sampler_layer_data *l, gboolean value) { l->has_##parent.name = value; } \
  256. static gboolean sampler_layer_data_##parent##_get_has_##name(struct sampler_layer_data *l) { return l->has_##parent.name; }
  257. #define PROC_FIELD_SETHASFUNC(type, name, default_value) \
  258. static void sampler_layer_data_set_has_##name(struct sampler_layer_data *l, gboolean value) { l->has_##name = value; } \
  259. static gboolean sampler_layer_data_get_has_##name(struct sampler_layer_data *l) { return l->has_##name; }
  260. #define PROC_FIELD_SETHASFUNC_string(name) \
  261. static void sampler_layer_data_set_has_##name(struct sampler_layer_data *l, gboolean value) { l->has_##name = value; } \
  262. static gboolean sampler_layer_data_get_has_##name(struct sampler_layer_data *l) { return l->has_##name; }
  263. #define PROC_FIELD_SETHASFUNC_dBamp(type, name, default_value) \
  264. PROC_FIELD_SETHASFUNC(type, name, default_value)
  265. #define PROC_FIELD_SETHASFUNC_enum(type, name, default_value) \
  266. PROC_FIELD_SETHASFUNC(type, name, default_value)
  267. #define PROC_FIELD_SETHASFUNC_dahdsr(field, name, default_value) \
  268. DAHDSR_FIELDS(PROC_SUBSTRUCT_FIELD_SETHASFUNC, field)
  269. #define PROC_FIELD_SETHASFUNC_lfo(field, name, default_value) \
  270. LFO_FIELDS(PROC_SUBSTRUCT_FIELD_SETHASFUNC, field)
  271. #define PROC_FIELD_SETHASFUNC_eq(field, name, default_value) \
  272. EQ_FIELDS(PROC_SUBSTRUCT_FIELD_SETHASFUNC, field)
  273. #define PROC_FIELD_SETHASFUNC_ccrange(name, parname)
  274. #define PROC_FIELD_SETHASFUNC_midicurve(name) \
  275. static gboolean sampler_layer_data_set_get_has_##name(struct sampler_layer_data *l, uint32_t index, int value) \
  276. { \
  277. if (value != -1) \
  278. l->name.has_values[index] = value; \
  279. return l->name.has_values[index]; \
  280. }
  281. SAMPLER_FIXED_FIELDS(PROC_FIELD_SETHASFUNC)
  282. #define LOFS(field) offsetof(struct sampler_layer_data, field)
  283. #define FIELD_MOD(name, param, src, src2, dest) \
  284. { name, LOFS(modulations), slpt_mod_##param, 0, ENCODE_MOD(smsrc_##src, smsrc_##src2, smdest_##dest), NULL, NULL, NULL },
  285. #define FIELD_AMOUNT(name, src, dest) \
  286. FIELD_MOD(name, amount, src, none, dest)
  287. #define FIELD_AMOUNT_CC(name, dest) \
  288. FIELD_ALIAS(name "cc#", name "_oncc#") \
  289. FIELD_MOD(name "_oncc#", amount, CC, none, dest) \
  290. FIELD_MOD(name "_curvecc#", curveid, CC, none, dest) \
  291. FIELD_MOD(name "_smoothcc#", smooth, CC, none, dest) \
  292. FIELD_MOD(name "_stepcc#", step, CC, none, dest)
  293. #define FIELD_AMOUNT_CC_(name, dest) \
  294. FIELD_ALIAS(name "_cc#", name "_oncc#") \
  295. FIELD_MOD(name "_oncc#", amount, CC, none, dest) \
  296. FIELD_MOD(name "_curvecc#", curveid, CC, none, dest) \
  297. FIELD_MOD(name "_smoothcc#", smooth, CC, none, dest) \
  298. FIELD_MOD(name "_stepcc#", step, CC, none, dest)
  299. #define FIELD_VOICE_NIF(name, nif, variant) \
  300. { name, LOFS(voice_nifs), slpt_voice_nif, 0, variant, nif, NULL, NULL },
  301. #define FIELD_PREVOICE_NIF(name, nif, variant) \
  302. { name, LOFS(prevoice_nifs), slpt_prevoice_nif, 0, variant, nif, NULL, NULL },
  303. #define FIELD_ALIAS(alias, name) \
  304. { alias, -1, slpt_alias, 0, 0, name, NULL, NULL },
  305. #define PROC_SUBSTRUCT_FIELD_DESCRIPTOR(name, index, def_value, parent, parent_name, parent_index, parent_struct) \
  306. { #parent_name "_" #name, offsetof(struct sampler_layer_data, parent) + offsetof(struct parent_struct, name), slpt_float, def_value, parent_index * 100 + index, NULL, sampler_layer_data_##parent##_set_has_##name, sampler_layer_data_##parent##_get_has_##name }, \
  307. #define PROC_SUBSTRUCT_FIELD_DESCRIPTOR_DAHDSR(name, index, def_value, parent, parent_name, parent_index, parent_struct) \
  308. { #parent_name "_" #name, offsetof(struct sampler_layer_data, parent) + offsetof(struct parent_struct, name), slpt_float, def_value, parent_index * 100 + index, NULL, sampler_layer_data_##parent##_set_has_##name, sampler_layer_data_##parent##_get_has_##name }, \
  309. FIELD_VOICE_NIF(#parent_name "_vel2" #name, sampler_nif_vel2env, (parent_index << 4) + snif_env_##name) \
  310. FIELD_AMOUNT_CC(#parent_name "_" #name, ampeg_stage + (parent_index << 4) + snif_env_##name) \
  311. #define PROC_FIELD_DESCRIPTOR(type, name, default_value) \
  312. { #name, LOFS(name), slpt_##type, default_value, 0, NULL, sampler_layer_data_set_has_##name, sampler_layer_data_get_has_##name },
  313. #define PROC_FIELD_DESCRIPTOR_dBamp(type, name, default_value) \
  314. { #name, LOFS(name), slpt_##type, default_value, 0, NULL, sampler_layer_data_set_has_##name, sampler_layer_data_get_has_##name },
  315. #define PROC_FIELD_DESCRIPTOR_string(name) \
  316. { #name, LOFS(name), slpt_string, 0, LOFS(name##_changed), NULL, sampler_layer_data_set_has_##name, sampler_layer_data_get_has_##name },
  317. #define PROC_FIELD_DESCRIPTOR_enum(enumtype, name, default_value) \
  318. { #name, LOFS(name), slpt_enum, (double)(enum enumtype)default_value, 0, enumtype##_from_string, sampler_layer_data_set_has_##name, sampler_layer_data_get_has_##name },
  319. #define PROC_FIELD_DESCRIPTOR_midicurve(name) \
  320. { #name "_#", LOFS(name), slpt_midicurve, 0, 0, (void *)sampler_layer_data_set_get_has_##name, NULL, NULL },
  321. #define FIELD_DEPTH_SET(name, dest, attrib) \
  322. FIELD_ALIAS(#name attrib "cc#", #name attrib "_oncc#") \
  323. FIELD_MOD(#name attrib "_oncc#", amount, name, CC, dest) \
  324. FIELD_MOD(#name attrib "_curvecc#", curveid, name, CC, dest) \
  325. FIELD_MOD(#name attrib "_smoothcc#", smooth, name, CC, dest) \
  326. FIELD_MOD(#name attrib "_stepcc#", step, name, CC, dest) \
  327. FIELD_MOD(#name attrib "polyaft", amount, name, polyaft, dest) \
  328. FIELD_MOD(#name attrib "chanaft", amount, name, chanaft, dest) \
  329. FIELD_MOD(#name attrib, amount, name, none, dest) \
  330. #define PROC_FIELD_DESCRIPTOR_dahdsr(field, name, index) \
  331. DAHDSR_FIELDS(PROC_SUBSTRUCT_FIELD_DESCRIPTOR_DAHDSR, field, name, index, cbox_dahdsr) \
  332. FIELD_DEPTH_SET(name, from_##name, "_depth") \
  333. FIELD_MOD(#name "_vel2depth", amount, name, vel, from_##name)
  334. #define PROC_FIELD_DESCRIPTOR_lfo(field, name, index) \
  335. LFO_FIELDS(PROC_SUBSTRUCT_FIELD_DESCRIPTOR, field, name, index, sampler_lfo_params) \
  336. FIELD_AMOUNT(#name "_freqpolyaft", polyaft, name##_freq) \
  337. FIELD_AMOUNT(#name "_freqchanaft", chanaft, name##_freq) \
  338. FIELD_AMOUNT_CC(#name "_freq", name##_freq) \
  339. FIELD_DEPTH_SET(name, from_##name, "_depth")
  340. #define PROC_FIELD_DESCRIPTOR_eq(field, name, index) \
  341. EQ_FIELDS(PROC_SUBSTRUCT_FIELD_DESCRIPTOR, field, name, index, sampler_eq_params) \
  342. FIELD_AMOUNT_CC(#name "_freq", name##_freq) \
  343. FIELD_AMOUNT_CC(#name "_bw", name##_bw) \
  344. FIELD_AMOUNT_CC(#name "_gain", name##_gain)
  345. #define PROC_FIELD_DESCRIPTOR_ccrange(field, parname) \
  346. { #parname "locc#", LOFS(field), slpt_ccrange, 0, 0, NULL, NULL, NULL }, \
  347. { #parname "hicc#", LOFS(field), slpt_ccrange, 127, 1, NULL, NULL, NULL },
  348. #define FIELD_FLEX_LFO(name, field) \
  349. { name, LOFS(flex_lfos), slpt_flex_lfo, 0, sampler_flex_lfo_value_field_##field, NULL, NULL, NULL },
  350. #define NIF_VARIANT_CC 0x01000000
  351. #define NIF_VARIANT_CURVECC 0x02000000
  352. #define NIF_VARIANT_STEPCC 0x03000000
  353. #define NIF_VARIANT_MASK 0xFF000000
  354. struct sampler_layer_param_entry sampler_layer_params[] = {
  355. SAMPLER_FIXED_FIELDS(PROC_FIELD_DESCRIPTOR)
  356. FIELD_AMOUNT("cutoff_chanaft", chanaft, cutoff)
  357. FIELD_AMOUNT("resonance_chanaft", chanaft, resonance)
  358. FIELD_AMOUNT("cutoff_polyaft", polyaft, cutoff)
  359. FIELD_AMOUNT("resonance_polyaft", polyaft, resonance)
  360. FIELD_DEPTH_SET(fileg, cutoff2, "_depth2")
  361. FIELD_MOD("fileg_vel2depth2", amount, fileg, vel, cutoff2)
  362. FIELD_DEPTH_SET(fillfo, cutoff2, "_depth2")
  363. FIELD_AMOUNT("cutoff2_chanaft", chanaft, cutoff2)
  364. FIELD_AMOUNT("resonance2_chanaft", chanaft, resonance2)
  365. FIELD_AMOUNT("cutoff2_polyaft", polyaft, cutoff2)
  366. FIELD_AMOUNT("resonance2_polyaft", polyaft, resonance2)
  367. FIELD_AMOUNT_CC_("gain", gain)
  368. FIELD_AMOUNT_CC_("cutoff", cutoff)
  369. FIELD_AMOUNT_CC_("resonance", resonance)
  370. FIELD_AMOUNT_CC_("cutoff2", cutoff2)
  371. FIELD_AMOUNT_CC_("resonance2", resonance2)
  372. FIELD_AMOUNT_CC_("pitch", pitch)
  373. FIELD_AMOUNT_CC_("tune", pitch)
  374. FIELD_AMOUNT_CC_("tonectl", tonectl)
  375. FIELD_AMOUNT_CC_("pan", pan)
  376. FIELD_AMOUNT_CC_("amplitude", amplitude)
  377. FIELD_VOICE_NIF("amp_random", sampler_nif_addrandom, 0)
  378. FIELD_VOICE_NIF("fil_random", sampler_nif_addrandom, 1)
  379. FIELD_VOICE_NIF("pitch_random", sampler_nif_addrandom, 2)
  380. FIELD_VOICE_NIF("pitch_veltrack", sampler_nif_vel2pitch, 0)
  381. FIELD_VOICE_NIF("offset_veltrack", sampler_nif_vel2offset, 0)
  382. FIELD_VOICE_NIF("reloffset_veltrack", sampler_nif_vel2reloffset, 0)
  383. FIELD_PREVOICE_NIF("delay_random", sampler_nif_addrandomdelay, 0)
  384. FIELD_PREVOICE_NIF("sync_beats", sampler_nif_syncbeats, 0)
  385. FIELD_PREVOICE_NIF("delay_cc#", sampler_nif_cc2delay, NIF_VARIANT_CC)
  386. FIELD_PREVOICE_NIF("delay_curvecc#", sampler_nif_cc2delay, NIF_VARIANT_CURVECC)
  387. FIELD_PREVOICE_NIF("delay_stepcc#", sampler_nif_cc2delay, NIF_VARIANT_STEPCC)
  388. FIELD_VOICE_NIF("reloffset_cc#", sampler_nif_cc2reloffset, NIF_VARIANT_CC)
  389. FIELD_VOICE_NIF("reloffset_curvecc#", sampler_nif_cc2reloffset, NIF_VARIANT_CURVECC)
  390. FIELD_VOICE_NIF("reloffset_stepcc#", sampler_nif_cc2reloffset, NIF_VARIANT_STEPCC)
  391. FIELD_VOICE_NIF("offset_cc#", sampler_nif_cc2offset, NIF_VARIANT_CC)
  392. FIELD_VOICE_NIF("offset_curvecc#", sampler_nif_cc2offset, NIF_VARIANT_CURVECC)
  393. FIELD_VOICE_NIF("offset_stepcc#", sampler_nif_cc2offset, NIF_VARIANT_STEPCC)
  394. FIELD_FLEX_LFO("lfo#_freq", freq)
  395. FIELD_FLEX_LFO("lfo#_delay", delay)
  396. FIELD_FLEX_LFO("lfo#_fade", fade)
  397. FIELD_FLEX_LFO("lfo#_wave", wave)
  398. FIELD_ALIAS("hilev", "hivel")
  399. FIELD_ALIAS("lolev", "lovel")
  400. FIELD_ALIAS("loopstart", "loop_start")
  401. FIELD_ALIAS("loopend", "loop_end")
  402. FIELD_ALIAS("loopmode", "loop_mode")
  403. FIELD_ALIAS("bendup", "bend_up")
  404. FIELD_ALIAS("benddown", "bend_down")
  405. FIELD_ALIAS("offby", "off_by")
  406. FIELD_ALIAS("offset_oncc#", "offset_cc#")
  407. FIELD_ALIAS("reloffset_oncc#", "reloffset_cc#")
  408. FIELD_ALIAS("delay_oncc#", "delay_cc#")
  409. { "genericmod_#_#_#_#", -1, slpt_generic_modulation, 0, 0, NULL, NULL, NULL },
  410. };
  411. #define NPARAMS (sizeof(sampler_layer_params) / sizeof(sampler_layer_params[0]))
  412. static int compare_entries(const void *p1, const void *p2)
  413. {
  414. const struct sampler_layer_param_entry *e1 = p1, *e2 = p2;
  415. return strcmp(e1->name, e2->name);
  416. }
  417. void sampler_layer_prepare_params(void)
  418. {
  419. qsort(sampler_layer_params, NPARAMS, sizeof(struct sampler_layer_param_entry), compare_entries);
  420. for (size_t i = 0; i < NPARAMS; ++i)
  421. {
  422. struct sampler_layer_param_entry *e = &sampler_layer_params[i];
  423. if (e->type == slpt_alias)
  424. {
  425. struct sampler_layer_param_entry prototype;
  426. prototype.name = e->extra_ptr;
  427. void *found = bsearch(&prototype, sampler_layer_params, NPARAMS, sizeof(sampler_layer_params[0]), compare_entries);
  428. if (!found)
  429. printf("Alias %s redirects to non-existent name (%s)\n", e->name, prototype.name);
  430. assert(found);
  431. e->extra_ptr = found;
  432. }
  433. if (i)
  434. {
  435. struct sampler_layer_param_entry *prev_e = &sampler_layer_params[i - 1];
  436. if (!strcmp(e->name, prev_e->name))
  437. {
  438. printf("Duplicate parameter %s\n", e->name);
  439. assert(FALSE);
  440. }
  441. }
  442. }
  443. }
  444. // This only works for setting. Unsetting is slightly different.
  445. static gboolean override_logic(gboolean is_equal, gboolean has_value, gboolean set_local_value)
  446. {
  447. if (!set_local_value && has_value) // Do not override locally set values
  448. return FALSE;
  449. // Override if a value or a inherited value is replaced with a local setting.
  450. return (!is_equal) || (set_local_value != has_value);
  451. }
  452. static inline void mod_key_decode(uint64_t extra_int, const uint32_t *args, struct sampler_modulation_key *mod_key)
  453. {
  454. uint32_t modsrc = (extra_int & 0xFFF);
  455. uint32_t modsrc2 = ((extra_int >> 12) & 0xFFF);
  456. if (modsrc == MODSRC_CC)
  457. modsrc = args[0];
  458. if (modsrc2 == MODSRC_CC)
  459. modsrc2 = args[0];
  460. mod_key->src = modsrc;
  461. mod_key->src2 = modsrc2;
  462. mod_key->dest = ((extra_int >> 24) & 0xFF);
  463. }
  464. static inline void nif_key_decode(uint64_t extra_int, void *extra_ptr, const uint32_t *args, struct sampler_noteinitfunc_key *nif_key)
  465. {
  466. uint32_t variant = extra_int &~ NIF_VARIANT_MASK;
  467. nif_key->notefunc_voice = extra_ptr;
  468. if (extra_int & NIF_VARIANT_MASK)
  469. {
  470. int cc = args[0] & 255;
  471. variant = cc + (variant << 8);
  472. }
  473. nif_key->variant = variant;
  474. }
  475. static inline void flex_lfo_key_decode(const uint32_t *args, struct sampler_flex_lfo_key *flex_lfo_key)
  476. {
  477. flex_lfo_key->id = args[0];
  478. }
  479. #define OVERRIDE_LOGIC(type) override_logic(!memcmp(p, data_ptr, sizeof(type)), e->get_has_value(&l->data), set_local_value)
  480. #define CAST_FLOAT_VALUE fvalue = *(double *)data_ptr
  481. gboolean sampler_layer_param_entry_set_from_ptr(const struct sampler_layer_param_entry *e, struct sampler_layer *l, gboolean set_local_value, const void *data_ptr, const uint32_t *args, GError **error)
  482. {
  483. void *p = ((uint8_t *)&l->data) + e->offset;
  484. uint32_t cc = 0;
  485. double fvalue = 0;
  486. struct sampler_modulation_key mod_key = {0, 0, 0};
  487. struct sampler_noteinitfunc_key nif_key = {{NULL}, 0};
  488. struct sampler_flex_lfo_key flex_lfo_key = {0};
  489. switch(e->type)
  490. {
  491. case slpt_midi_note_t:
  492. if (!OVERRIDE_LOGIC(midi_note_t))
  493. return TRUE;
  494. memcpy(p, data_ptr, sizeof(midi_note_t));
  495. break;
  496. case slpt_int:
  497. if (!OVERRIDE_LOGIC(int))
  498. return TRUE;
  499. memcpy(p, data_ptr, sizeof(int));
  500. break;
  501. case slpt_enum:
  502. case slpt_uint32_t:
  503. if (!OVERRIDE_LOGIC(uint32_t))
  504. return TRUE;
  505. memcpy(p, data_ptr, sizeof(uint32_t));
  506. break;
  507. case slpt_string:
  508. {
  509. char **pc = p;
  510. gboolean str_differs = (!*pc != !data_ptr) || strcmp(*pc, data_ptr);
  511. if (!override_logic(!str_differs, e->get_has_value(&l->data), set_local_value))
  512. return TRUE;
  513. if (str_differs)
  514. {
  515. free(*pc);
  516. *pc = g_strdup(data_ptr);
  517. gboolean *changed_ptr = (gboolean *)(((uint8_t *)&l->data) + e->extra_int);
  518. *changed_ptr = 1;
  519. }
  520. }
  521. break;
  522. case slpt_float:
  523. case slpt_dBamp:
  524. fvalue = *(double *)data_ptr;
  525. if (!override_logic((float)fvalue == *(float *)p, e->get_has_value(&l->data), set_local_value))
  526. return TRUE;
  527. *(float *)p = fvalue;
  528. break;
  529. case slpt_midicurve:
  530. CAST_FLOAT_VALUE;
  531. if (args[0] >= 0 && args[0] <= 127)
  532. {
  533. gboolean (*setgethasfunc)(struct sampler_layer_data *, uint32_t, int) = e->extra_ptr;
  534. float *dst = &((struct sampler_midi_curve *)p)->values[args[0]];
  535. if (!override_logic(*dst == fvalue, setgethasfunc(&l->data, args[0], -1), set_local_value))
  536. return TRUE;
  537. *dst = fvalue;
  538. setgethasfunc(&l->data, args[0], set_local_value);
  539. }
  540. else
  541. {
  542. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Curve entry index (%u) is out of range for %s", (unsigned)args[0], e->name);
  543. return FALSE;
  544. }
  545. break;
  546. case slpt_ccrange:
  547. {
  548. int number = *(int *)data_ptr;
  549. cc = args[0];
  550. switch(e->extra_int) {
  551. case 0:
  552. sampler_cc_range_set_locc_by_offset(l, e->offset, &(struct sampler_cc_range_key){cc}, set_local_value, number);
  553. break;
  554. case 1:
  555. sampler_cc_range_set_hicc_by_offset(l, e->offset, &(struct sampler_cc_range_key){cc}, set_local_value, number);
  556. break;
  557. default: assert(0);
  558. }
  559. break;
  560. }
  561. case slpt_mod_amount:
  562. CAST_FLOAT_VALUE;
  563. mod_key_decode(e->extra_int, args, &mod_key);
  564. sampler_modulation_set_amount_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
  565. break;
  566. case slpt_mod_curveid:
  567. CAST_FLOAT_VALUE;
  568. mod_key_decode(e->extra_int, args, &mod_key);
  569. sampler_modulation_set_curve_id_by_offset(l, e->offset, &mod_key, set_local_value, (int)fvalue);
  570. break;
  571. case slpt_mod_smooth:
  572. CAST_FLOAT_VALUE;
  573. mod_key_decode(e->extra_int, args, &mod_key);
  574. sampler_modulation_set_smooth_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
  575. break;
  576. case slpt_mod_step:
  577. CAST_FLOAT_VALUE;
  578. mod_key_decode(e->extra_int, args, &mod_key);
  579. sampler_modulation_set_step_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
  580. break;
  581. case slpt_generic_modulation:
  582. CAST_FLOAT_VALUE;
  583. sampler_modulation_set_amount_by_offset(l, e->offset, &(struct sampler_modulation_key){args[0], args[1], args[2]}, set_local_value, fvalue);
  584. sampler_modulation_set_curve_id_by_offset(l, e->offset, &(struct sampler_modulation_key){args[0], args[1], args[2]}, set_local_value, (int)args[3]);
  585. break;
  586. case slpt_voice_nif:
  587. case slpt_prevoice_nif:
  588. CAST_FLOAT_VALUE;
  589. nif_key_decode(e->extra_int, e->extra_ptr, args, &nif_key);
  590. switch(e->extra_int & NIF_VARIANT_MASK)
  591. {
  592. case 0:
  593. case NIF_VARIANT_CC:
  594. sampler_noteinitfunc_set_value_by_offset(l, e->offset, &nif_key, set_local_value, fvalue);
  595. break;
  596. case NIF_VARIANT_CURVECC:
  597. sampler_noteinitfunc_set_curve_id_by_offset(l, e->offset, &nif_key, set_local_value, (int)fvalue);
  598. break;
  599. case NIF_VARIANT_STEPCC:
  600. sampler_noteinitfunc_set_step_by_offset(l, e->offset, &nif_key, set_local_value, fvalue);
  601. break;
  602. }
  603. break;
  604. case slpt_flex_lfo:
  605. CAST_FLOAT_VALUE;
  606. flex_lfo_key_decode(args, &flex_lfo_key);
  607. switch(e->extra_int)
  608. {
  609. case sampler_flex_lfo_value_field_freq:
  610. sampler_flex_lfo_set_freq_by_offset(l, e->offset, &flex_lfo_key, set_local_value, fvalue);
  611. break;
  612. case sampler_flex_lfo_value_field_delay:
  613. sampler_flex_lfo_set_delay_by_offset(l, e->offset, &flex_lfo_key, set_local_value, fvalue);
  614. break;
  615. case sampler_flex_lfo_value_field_fade:
  616. sampler_flex_lfo_set_fade_by_offset(l, e->offset, &flex_lfo_key, set_local_value, fvalue);
  617. break;
  618. case sampler_flex_lfo_value_field_wave:
  619. sampler_flex_lfo_set_wave_by_offset(l, e->offset, &flex_lfo_key, set_local_value, (int)fvalue);
  620. break;
  621. }
  622. break;
  623. case slpt_reserved:
  624. case slpt_invalid:
  625. case slpt_alias:
  626. printf("Unhandled parameter type of parameter %s\n", e->name);
  627. assert(0);
  628. return FALSE;
  629. }
  630. if (e->set_has_value)
  631. e->set_has_value(&l->data, set_local_value);
  632. if (l->child_layers) {
  633. /* Propagate to children */
  634. GHashTableIter iter;
  635. g_hash_table_iter_init(&iter, l->child_layers);
  636. gpointer key, value;
  637. while(g_hash_table_iter_next(&iter, &key, &value))
  638. {
  639. struct sampler_layer *child = value;
  640. if (!sampler_layer_param_entry_set_from_ptr(e, child, FALSE, data_ptr, args, error))
  641. return FALSE;
  642. }
  643. }
  644. return TRUE;
  645. }
  646. #define VERIFY_FLOAT_VALUE do { if (!atof_C_verify(e->name, value, &fvalue, error)) return FALSE; } while(0)
  647. gboolean sampler_layer_param_entry_set_from_string(const struct sampler_layer_param_entry *e, struct sampler_layer *l, gboolean set_local_value, const char *value, const uint32_t *args, GError **error)
  648. {
  649. double fvalue;
  650. switch(e->type)
  651. {
  652. case slpt_midi_note_t:
  653. {
  654. midi_note_t note = sfz_note_from_string(value);
  655. if (note < -1)
  656. {
  657. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a valid note name for %s", value, e->name);
  658. return FALSE;
  659. }
  660. return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &note, args, error);
  661. }
  662. case slpt_int:
  663. {
  664. char *endptr;
  665. errno = 0;
  666. int number = strtol(value, &endptr, 10);
  667. if (errno || *endptr || endptr == value)
  668. {
  669. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a correct integer value for %s", value, e->name);
  670. return FALSE;
  671. }
  672. return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &number, args, error);
  673. }
  674. case slpt_enum:
  675. {
  676. gboolean (*func)(const char *, uint32_t *value);
  677. func = e->extra_ptr;
  678. uint32_t data = 0;
  679. if (!func(value, &data))
  680. {
  681. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a correct value for %s", value, e->name);
  682. return FALSE;
  683. }
  684. return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &data, args, error);
  685. }
  686. case slpt_uint32_t:
  687. {
  688. char *endptr;
  689. errno = 0;
  690. uint32_t number = (uint32_t)strtoul(value, &endptr, 10);
  691. if (errno || *endptr || endptr == value || value[0] == '-')
  692. {
  693. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a correct unsigned integer value for %s", value, e->name);
  694. return FALSE;
  695. }
  696. return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &number, args, error);
  697. }
  698. case slpt_string:
  699. return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, value, args, error);
  700. case slpt_ccrange:
  701. {
  702. char *endptr;
  703. errno = 0;
  704. int number = strtol(value, &endptr, 10);
  705. if (errno || *endptr || endptr == value || number < 0 || number > 127)
  706. {
  707. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a correct control change value for %s", value, e->name);
  708. return FALSE;
  709. }
  710. return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &number, args, error);
  711. }
  712. case slpt_float:
  713. case slpt_dBamp:
  714. default:
  715. VERIFY_FLOAT_VALUE;
  716. return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &fvalue, args, error);
  717. }
  718. }
  719. #define COPY_NUM_FROM_PARENT(case_value, type) \
  720. case case_value: \
  721. if (!unset_local_value && e->get_has_value(&l->data)) \
  722. return TRUE; \
  723. *(type *)p = pp ? *(type *)pp : (type)e->def_value; \
  724. e->set_has_value(&l->data, 0); \
  725. break;
  726. gboolean sampler_layer_param_entry_unset(const struct sampler_layer_param_entry *e, struct sampler_layer *l, gboolean unset_local_value, const uint32_t *args, GError **error)
  727. {
  728. void *p = ((uint8_t *)&l->data) + e->offset;
  729. void *pp = l->parent ? ((uint8_t *)&l->parent->data) + e->offset : NULL;
  730. uint32_t cc;
  731. struct sampler_modulation_key mod_key = {0, 0, 0};
  732. struct sampler_noteinitfunc_key nif_key = {{NULL}, 0};
  733. struct sampler_flex_lfo_key flex_lfo_key = {0};
  734. switch(e->type)
  735. {
  736. COPY_NUM_FROM_PARENT(slpt_midi_note_t, midi_note_t)
  737. COPY_NUM_FROM_PARENT(slpt_int, int)
  738. COPY_NUM_FROM_PARENT(slpt_enum, uint32_t) // XXXKF that's a kludge, enums are not guaranteed to be uint32_t (but they should be on common platforms)
  739. COPY_NUM_FROM_PARENT(slpt_uint32_t, uint32_t)
  740. COPY_NUM_FROM_PARENT(slpt_float, float)
  741. COPY_NUM_FROM_PARENT(slpt_dBamp, float)
  742. case slpt_string:
  743. {
  744. if (!unset_local_value && e->get_has_value(&l->data))
  745. return TRUE;
  746. char **pc = p;
  747. free(*pc);
  748. *pc = pp ? g_strdup(*(const char **)pp) : NULL;
  749. e->set_has_value(&l->data, 0);
  750. gboolean *changed_ptr = (gboolean *)(((uint8_t *)&l->data) + e->extra_int);
  751. *changed_ptr = 1;
  752. }
  753. return TRUE;
  754. case slpt_midicurve:
  755. if (args[0] >= 0 && args[0] <= 127)
  756. {
  757. struct sampler_midi_curve *curve = p, *parent_curve = pp;
  758. gboolean (*setgethasfunc)(struct sampler_layer_data *, uint32_t, gboolean value) = e->extra_ptr;
  759. if (setgethasfunc(&l->data, args[0], -1) && !unset_local_value)
  760. return TRUE;
  761. curve->values[args[0]] = parent_curve ? parent_curve->values[args[0]] : SAMPLER_CURVE_GAP;
  762. setgethasfunc(&l->data, args[0], 0);
  763. break;
  764. }
  765. else
  766. {
  767. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Curve entry index (%u) is out of range for %s", (unsigned)args[0], e->name);
  768. return FALSE;
  769. }
  770. case slpt_ccrange:
  771. cc = args[0];
  772. if (!sampler_cc_range_unset_by_offset(l, e->offset, &(struct sampler_cc_range_key){cc}, unset_local_value, 1 << e->extra_int))
  773. {
  774. if (!unset_local_value)
  775. return TRUE;
  776. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Controller number %d not used for %s", cc, e->name);
  777. return FALSE;
  778. }
  779. break;
  780. case slpt_mod_amount:
  781. mod_key_decode(e->extra_int, args, &mod_key);
  782. sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, 1 << sampler_modulation_value_field_amount);
  783. break;
  784. case slpt_mod_curveid:
  785. mod_key_decode(e->extra_int, args, &mod_key);
  786. sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, 1 << sampler_modulation_value_field_curve_id);
  787. break;
  788. case slpt_mod_smooth:
  789. mod_key_decode(e->extra_int, args, &mod_key);
  790. sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, 1 << sampler_modulation_value_field_smooth);
  791. break;
  792. case slpt_mod_step:
  793. mod_key_decode(e->extra_int, args, &mod_key);
  794. sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, 1 << sampler_modulation_value_field_step);
  795. break;
  796. case slpt_generic_modulation:
  797. mod_key = (struct sampler_modulation_key){args[0], args[1], args[2]};
  798. sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, (1 << sampler_modulation_value_field_amount) | (1 << sampler_modulation_value_field_curve_id));
  799. break;
  800. case slpt_voice_nif:
  801. case slpt_prevoice_nif:
  802. {
  803. nif_key_decode(e->extra_int, e->extra_ptr, args, &nif_key);
  804. static const uint32_t value_fields[] = {
  805. sampler_noteinitfunc_value_field_value, sampler_noteinitfunc_value_field_value,
  806. sampler_noteinitfunc_value_field_curve_id, sampler_noteinitfunc_value_field_step,
  807. };
  808. if (!sampler_noteinitfunc_unset_by_offset(l, e->offset, &nif_key, unset_local_value, 1 << value_fields[e->extra_int >> 24]))
  809. {
  810. if (!unset_local_value)
  811. return TRUE;
  812. if (e->extra_int & NIF_VARIANT_MASK)
  813. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Controller number %d not used for %s", args[0], e->name);
  814. else
  815. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "%s not set", e->name);
  816. return FALSE;
  817. }
  818. break;
  819. }
  820. case slpt_flex_lfo:
  821. flex_lfo_key_decode(args, &flex_lfo_key);
  822. switch(e->extra_int)
  823. {
  824. case sampler_flex_lfo_value_field_freq:
  825. sampler_flex_lfo_unset_by_offset(l, e->offset, &flex_lfo_key, unset_local_value, 1 << sampler_flex_lfo_value_field_freq);
  826. break;
  827. case sampler_flex_lfo_value_field_delay:
  828. sampler_flex_lfo_unset_by_offset(l, e->offset, &flex_lfo_key, unset_local_value, 1 << sampler_flex_lfo_value_field_delay);
  829. break;
  830. case sampler_flex_lfo_value_field_fade:
  831. sampler_flex_lfo_unset_by_offset(l, e->offset, &flex_lfo_key, unset_local_value, 1 << sampler_flex_lfo_value_field_fade);
  832. break;
  833. case sampler_flex_lfo_value_field_wave:
  834. sampler_flex_lfo_unset_by_offset(l, e->offset, &flex_lfo_key, unset_local_value, 1 << sampler_flex_lfo_value_field_wave);
  835. break;
  836. }
  837. break;
  838. case slpt_invalid:
  839. case slpt_reserved:
  840. case slpt_alias:
  841. default:
  842. printf("Unhandled parameter type of parameter %s\n", e->name);
  843. assert(0);
  844. return FALSE;
  845. }
  846. if (l->child_layers) {
  847. /* Propagate to children */
  848. GHashTableIter iter;
  849. g_hash_table_iter_init(&iter, l->child_layers);
  850. gpointer key, value;
  851. while(g_hash_table_iter_next(&iter, &key, &value))
  852. {
  853. struct sampler_layer *child = value;
  854. if (!sampler_layer_param_entry_unset(e, child, FALSE, args, error))
  855. return FALSE;
  856. }
  857. }
  858. return TRUE;
  859. }
  860. #undef COPY_NUM_FROM_PARENT
  861. // Compare against a template that uses # to represent a number, extract
  862. // any such numbers.
  863. static int templcmp(const char *key, const char *templ, uint32_t *numbers)
  864. {
  865. while(*key && *templ)
  866. {
  867. if (*templ == '#')
  868. {
  869. if (isdigit(*key)) {
  870. uint32_t num = 0;
  871. do {
  872. num = num * 10 + (unsigned char)(*key - '0');
  873. key++;
  874. } while (isdigit(*key));
  875. *numbers++ = num;
  876. templ++;
  877. continue;
  878. }
  879. }
  880. else if (*key == *templ)
  881. {
  882. templ++, key++;
  883. continue;
  884. }
  885. if (*key < *templ)
  886. return -1;
  887. else
  888. return +1;
  889. }
  890. if (*key)
  891. return +1;
  892. if (*templ)
  893. return -1;
  894. return 0;
  895. }
  896. const struct sampler_layer_param_entry *sampler_layer_param_find(const char *key, uint32_t *args)
  897. {
  898. static int prepared = 0;
  899. if (!prepared)
  900. {
  901. sampler_layer_prepare_params();
  902. prepared = 1;
  903. }
  904. int niter = 0;
  905. uint32_t lo = 0, hi = NPARAMS;
  906. while(lo < hi) {
  907. ++niter;
  908. uint32_t mid = (lo + hi) >> 1;
  909. const struct sampler_layer_param_entry *e = &sampler_layer_params[mid];
  910. int res = templcmp(key, e->name, args);
  911. if (res == 0)
  912. {
  913. // printf("%s found in %d iterations\n", key, niter);
  914. if (e->type == slpt_alias)
  915. return (const struct sampler_layer_param_entry *)e->extra_ptr;
  916. return e;
  917. }
  918. if (res < 0)
  919. hi = mid;
  920. else
  921. lo = mid + 1;
  922. }
  923. return NULL;
  924. }
  925. int sampler_layer_apply_fixed_param(struct sampler_layer *l, const char *key, const char *value, GError **error)
  926. {
  927. uint32_t args[10];
  928. const struct sampler_layer_param_entry *e = sampler_layer_param_find(key, args);
  929. if (e)
  930. return sampler_layer_param_entry_set_from_string(e, l, TRUE, value, args, error);
  931. else
  932. return -1;
  933. }
  934. int sampler_layer_unapply_fixed_param(struct sampler_layer *l, const char *key, GError **error)
  935. {
  936. uint32_t args[10];
  937. const struct sampler_layer_param_entry *e = sampler_layer_param_find(key, args);
  938. if (e)
  939. return sampler_layer_param_entry_unset(e, l, TRUE, args, error);
  940. else
  941. return -1;
  942. }
  943. static gboolean sampler_layer_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
  944. {
  945. struct sampler_layer *layer = ct->user_data;
  946. if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
  947. {
  948. if (!cbox_check_fb_channel(fb, cmd->command, error))
  949. return FALSE;
  950. if (!((!layer->parent_program || cbox_execute_on(fb, NULL, "/parent_program", "o", error, layer->parent_program)) &&
  951. (!layer->parent || cbox_execute_on(fb, NULL, "/parent", "o", error, layer->parent)) &&
  952. CBOX_OBJECT_DEFAULT_STATUS(layer, fb, error)))
  953. return FALSE;
  954. return TRUE;
  955. }
  956. if ((!strcmp(cmd->command, "/as_string") || !strcmp(cmd->command, "/as_string_full")) && !strcmp(cmd->arg_types, ""))
  957. {
  958. if (!cbox_check_fb_channel(fb, cmd->command, error))
  959. return FALSE;
  960. gchar *res = sampler_layer_to_string(layer, !strcmp(cmd->command, "/as_string_full"));
  961. gboolean result = cbox_execute_on(fb, NULL, "/value", "s", error, res[0] == ' ' ? res + 1 : res);
  962. g_free(res);
  963. return result;
  964. }
  965. if (!strcmp(cmd->command, "/set_param") && !strcmp(cmd->arg_types, "ss"))
  966. {
  967. const char *key = CBOX_ARG_S(cmd, 0);
  968. const char *value = CBOX_ARG_S(cmd, 1);
  969. if (sampler_layer_apply_param(layer, key, value, error))
  970. {
  971. sampler_layer_update(layer);
  972. sampler_program_update_layers(layer->parent_program);
  973. return TRUE;
  974. }
  975. return FALSE;
  976. }
  977. if (!strcmp(cmd->command, "/unset_param") && !strcmp(cmd->arg_types, "s"))
  978. {
  979. const char *key = CBOX_ARG_S(cmd, 0);
  980. if (sampler_layer_unapply_param(layer, key, error))
  981. {
  982. sampler_layer_update(layer);
  983. sampler_program_update_layers(layer->parent_program);
  984. return TRUE;
  985. }
  986. return FALSE;
  987. }
  988. if (!strcmp(cmd->command, "/new_child") && !strcmp(cmd->arg_types, ""))
  989. {
  990. // XXXKF needs a string argument perhaps
  991. if (layer->parent && layer->parent->parent && layer->parent->parent->parent)
  992. {
  993. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Cannot create a region within a region");
  994. return FALSE;
  995. }
  996. struct sampler_layer *l = sampler_layer_new(layer->module, layer->parent_program, layer);
  997. sampler_layer_data_finalize(&l->data, l->parent ? &l->parent->data : NULL, layer->parent_program);
  998. sampler_layer_reset_switches(l, l->module);
  999. sampler_layer_update(l);
  1000. if (l->parent && l->parent->parent && l->parent->parent->parent)
  1001. {
  1002. sampler_program_add_layer(layer->parent_program, l);
  1003. sampler_program_update_layers(layer->parent_program);
  1004. }
  1005. return cbox_execute_on(fb, NULL, "/uuid", "o", error, l);
  1006. }
  1007. if (!strcmp(cmd->command, "/get_children") && !strcmp(cmd->arg_types, ""))
  1008. {
  1009. if (!cbox_check_fb_channel(fb, cmd->command, error))
  1010. return FALSE;
  1011. GHashTableIter iter;
  1012. g_hash_table_iter_init(&iter, layer->child_layers);
  1013. gpointer key, value;
  1014. while(g_hash_table_iter_next(&iter, &key, &value))
  1015. {
  1016. if (!cbox_execute_on(fb, NULL, "/child", "o", error, key))
  1017. return FALSE;
  1018. }
  1019. return TRUE;
  1020. }
  1021. // otherwise, treat just like an command on normal (non-aux) output
  1022. return cbox_object_default_process_cmd(ct, fb, cmd, error);
  1023. }
  1024. #define PROC_FIELDS_INITIALISER(type, name, def_value) \
  1025. ld->name = def_value; \
  1026. ld->has_##name = 0;
  1027. #define PROC_FIELDS_INITIALISER_string(name) \
  1028. ld->name = NULL; \
  1029. ld->name##_changed = FALSE; \
  1030. ld->has_##name = 0;
  1031. #define PROC_FIELDS_INITIALISER_midicurve(name) \
  1032. sampler_midi_curve_init(&ld->name);
  1033. #define PROC_FIELDS_INITIALISER_enum(type, name, def_value) \
  1034. PROC_FIELDS_INITIALISER(type, name, def_value)
  1035. #define PROC_FIELDS_INITIALISER_dBamp(type, name, def_value) \
  1036. ld->name = def_value; \
  1037. ld->name##_linearized = -1; \
  1038. ld->has_##name = 0;
  1039. #define PROC_FIELDS_INITIALISER_dahdsr(name, parname, index) \
  1040. DAHDSR_FIELDS(PROC_SUBSTRUCT_RESET_FIELD, name, ld); \
  1041. DAHDSR_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, ld)
  1042. #define PROC_FIELDS_INITIALISER_lfo(name, parname, index) \
  1043. LFO_FIELDS(PROC_SUBSTRUCT_RESET_FIELD, name, ld); \
  1044. LFO_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, ld)
  1045. #define PROC_FIELDS_INITIALISER_eq(name, parname, index) \
  1046. EQ_FIELDS(PROC_SUBSTRUCT_RESET_FIELD, name, ld); \
  1047. EQ_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, ld)
  1048. #define PROC_FIELDS_INITIALISER_ccrange(name, parname) \
  1049. ld->name = NULL;
  1050. CBOX_CLASS_DEFINITION_ROOT(sampler_layer)
  1051. struct sampler_layer *sampler_layer_new(struct sampler_module *m, struct sampler_program *parent_program, struct sampler_layer *parent)
  1052. {
  1053. struct sampler_layer *l = calloc(1, sizeof(struct sampler_layer));
  1054. struct cbox_document *doc = CBOX_GET_DOCUMENT(parent_program);
  1055. memset(l, 0, sizeof(struct sampler_layer));
  1056. CBOX_OBJECT_HEADER_INIT(l, sampler_layer, doc);
  1057. cbox_command_target_init(&l->cmd_target, sampler_layer_process_cmd, l);
  1058. l->module = m;
  1059. l->child_layers = g_hash_table_new(NULL, NULL);
  1060. if (parent)
  1061. {
  1062. sampler_layer_data_clone(&l->data, &parent->data, FALSE);
  1063. l->parent_program = parent_program;
  1064. l->parent = parent;
  1065. g_hash_table_replace(parent->child_layers, l, l);
  1066. l->runtime = NULL;
  1067. CBOX_OBJECT_REGISTER(l);
  1068. return l;
  1069. }
  1070. l->parent_program = parent_program;
  1071. struct sampler_layer_data *ld = &l->data;
  1072. SAMPLER_FIXED_FIELDS(PROC_FIELDS_INITIALISER)
  1073. ld->computed.eff_waveform = NULL;
  1074. ld->computed.eff_freq = 44100;
  1075. ld->modulations = NULL;
  1076. ld->voice_nifs = NULL;
  1077. ld->prevoice_nifs = NULL;
  1078. ld->computed.eff_use_keyswitch = 0;
  1079. if (!parent)
  1080. {
  1081. // Systemwide default instead?
  1082. uint32_t mod_offset = LOFS(modulations);
  1083. sampler_modulation_set_amount_by_offset(l, mod_offset, &(struct sampler_modulation_key){74, smsrc_none, smdest_cutoff}, TRUE, 9600);
  1084. sampler_modulation_set_curve_id_by_offset(l, mod_offset, &(struct sampler_modulation_key){74, smsrc_none, smdest_cutoff}, TRUE, 1);
  1085. sampler_modulation_set_amount_by_offset(l, mod_offset, &(struct sampler_modulation_key){71, smsrc_none, smdest_resonance}, TRUE, 12);
  1086. sampler_modulation_set_curve_id_by_offset(l, mod_offset, &(struct sampler_modulation_key){71, smsrc_none, smdest_resonance}, TRUE, 1);
  1087. sampler_modulation_set_amount_by_offset(l, mod_offset, &(struct sampler_modulation_key){smsrc_pitchlfo, 1, smdest_pitch}, TRUE, 100);
  1088. }
  1089. l->runtime = NULL;
  1090. l->unknown_keys = NULL;
  1091. CBOX_OBJECT_REGISTER(l);
  1092. return l;
  1093. }
  1094. #define PROC_FIELDS_CLONE(type, name, def_value) \
  1095. dst->name = src->name; \
  1096. dst->has_##name = copy_hasattr ? src->has_##name : FALSE;
  1097. #define PROC_FIELDS_CLONE_string(name) \
  1098. dst->name = src->name ? g_strdup(src->name) : NULL; \
  1099. dst->name##_changed = src->name##_changed; \
  1100. dst->has_##name = copy_hasattr ? src->has_##name : FALSE;
  1101. #define PROC_FIELDS_CLONE_midicurve(name) \
  1102. memcpy(dst->name.values, src->name.values, sizeof(float) * 128); \
  1103. if(copy_hasattr) \
  1104. memcpy(dst->name.has_values, src->name.has_values, sizeof(src->name.has_values));
  1105. #define PROC_FIELDS_CLONE_dBamp PROC_FIELDS_CLONE
  1106. #define PROC_FIELDS_CLONE_enum PROC_FIELDS_CLONE
  1107. #define PROC_FIELDS_CLONE_dahdsr(name, parname, index) \
  1108. DAHDSR_FIELDS(PROC_SUBSTRUCT_CLONE, name, dst, src) \
  1109. if (!copy_hasattr) \
  1110. DAHDSR_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, dst)
  1111. #define PROC_FIELDS_CLONE_lfo(name, parname, index) \
  1112. LFO_FIELDS(PROC_SUBSTRUCT_CLONE, name, dst, src) \
  1113. if (!copy_hasattr) \
  1114. LFO_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, dst)
  1115. #define PROC_FIELDS_CLONE_eq(name, parname, index) \
  1116. EQ_FIELDS(PROC_SUBSTRUCT_CLONE, name, dst, src) \
  1117. if (!copy_hasattr) \
  1118. EQ_FIELDS(PROC_SUBSTRUCT_RESET_HAS_FIELD, name, dst)
  1119. #define PROC_FIELDS_CLONE_ccrange(name, parname) \
  1120. dst->name = sampler_cc_range_clone(src->name, copy_hasattr);
  1121. void sampler_layer_data_clone(struct sampler_layer_data *dst, const struct sampler_layer_data *src, gboolean copy_hasattr)
  1122. {
  1123. SAMPLER_FIXED_FIELDS(PROC_FIELDS_CLONE)
  1124. dst->modulations = sampler_modulation_clone(src->modulations, copy_hasattr);
  1125. dst->voice_nifs = sampler_noteinitfunc_clone(src->voice_nifs, copy_hasattr);
  1126. dst->prevoice_nifs = sampler_noteinitfunc_clone(src->prevoice_nifs, copy_hasattr);
  1127. dst->flex_lfos = sampler_flex_lfo_clone(src->flex_lfos, copy_hasattr);
  1128. dst->computed.eff_waveform = src->computed.eff_waveform;
  1129. if (dst->computed.eff_waveform)
  1130. cbox_waveform_ref(dst->computed.eff_waveform);
  1131. }
  1132. void sampler_midi_curve_init(struct sampler_midi_curve *curve)
  1133. {
  1134. for (uint32_t i = 0; i < 128; ++i)
  1135. curve->values[i] = SAMPLER_CURVE_GAP;
  1136. memset(curve->has_values, 0, 128);
  1137. }
  1138. void sampler_midi_curve_interpolate(const struct sampler_midi_curve *curve, float dest[128], float def_start, float def_end, gboolean is_quadratic)
  1139. {
  1140. const float *src = curve->values;
  1141. int start = 0;
  1142. float sv = src[start];
  1143. if (sv == SAMPLER_CURVE_GAP)
  1144. sv = def_start;
  1145. if (is_quadratic && sv >= 0)
  1146. sv = sqrtf(sv);
  1147. for (int i = 1; i < 128; i++)
  1148. {
  1149. float ev = src[i];
  1150. if (ev == SAMPLER_CURVE_GAP)
  1151. {
  1152. if (i < 127)
  1153. continue;
  1154. else
  1155. ev = def_end;
  1156. }
  1157. if (is_quadratic && ev >= 0)
  1158. ev = sqrtf(ev);
  1159. if (is_quadratic)
  1160. {
  1161. for (int j = start; j <= i; j++)
  1162. dest[j] = powf(sv + (ev - sv) * (j - start) / (i - start), 2.f);
  1163. }
  1164. else
  1165. {
  1166. for (int j = start; j <= i; j++)
  1167. dest[j] = sv + (ev - sv) * (j - start) / (i - start);
  1168. }
  1169. start = i;
  1170. sv = ev;
  1171. }
  1172. }
  1173. static inline int sampler_filter_num_stages(float cutoff, enum sampler_filter_type fil_type)
  1174. {
  1175. if (cutoff == -1)
  1176. return 0;
  1177. if (fil_type == sft_lp24hybrid || fil_type == sft_lp24 || fil_type == sft_lp24nr || fil_type == sft_hp24 || fil_type == sft_hp24nr || fil_type == sft_bp12)
  1178. return 2;
  1179. if (fil_type == sft_lp36)
  1180. return 3;
  1181. return 1;
  1182. }
  1183. // If veltrack > 0, then the default range goes from -84dB to 0dB
  1184. // If veltrack == 0, then the default range is all 0dB
  1185. // If veltrack < 0, then the default range goes from 0dB to -84dB
  1186. #define START_VALUE_amp_velcurve (l->amp_veltrack > 0 ? dB2gain(-l->amp_veltrack * 84.0 / 100.0) : 1)
  1187. #define END_VALUE_amp_velcurve (l->amp_veltrack < 0 ? dB2gain(l->amp_veltrack * 84.0 / 100.0) : 1)
  1188. #define IS_QUADRATIC_amp_velcurve l->velcurve_quadratic
  1189. #define PROC_FIELDS_FINALISER(type, name, def_value)
  1190. #define PROC_FIELDS_FINALISER_string(name)
  1191. #define PROC_FIELDS_FINALISER_midicurve(name) \
  1192. sampler_midi_curve_interpolate(&l->name, l->computed.eff_##name, START_VALUE_##name, END_VALUE_##name, IS_QUADRATIC_##name);
  1193. #define PROC_FIELDS_FINALISER_enum(type, name, def_value)
  1194. #define PROC_FIELDS_FINALISER_dBamp(type, name, def_value) \
  1195. l->name##_linearized = dB2gain(l->name);
  1196. #define PROC_FIELDS_FINALISER_dahdsr(name, parname, index) \
  1197. cbox_envelope_init_dahdsr(&l->name##_shape, &l->name, m->module.srate / CBOX_BLOCK_SIZE, 100.f, &l->name##_shape == &l->amp_env_shape);
  1198. #define PROC_FIELDS_FINALISER_lfo(name, parname, index) /* no finaliser required */
  1199. #define PROC_FIELDS_FINALISER_eq(name, parname, index) l->name.effective_freq = (l->name.freq ? l->name.freq : 5 * powf(10.f, 1 + (index)));
  1200. #define PROC_FIELDS_FINALISER_ccrange(name, parname) /* no finaliser required */
  1201. void sampler_layer_data_finalize(struct sampler_layer_data *l, struct sampler_layer_data *parent, struct sampler_program *p)
  1202. {
  1203. struct sampler_module *m = p->module;
  1204. SAMPLER_FIXED_FIELDS(PROC_FIELDS_FINALISER)
  1205. // Handle change of sample in the parent group without override on region level
  1206. if (parent && (l->sample_changed || parent->sample_changed))
  1207. {
  1208. struct cbox_waveform *oldwf = l->computed.eff_waveform;
  1209. if (l->sample && *l->sample)
  1210. {
  1211. GError *error = NULL;
  1212. l->computed.eff_waveform = cbox_wavebank_get_waveform(p->name, p->tarfile, p->sample_dir, l->sample, &error);
  1213. if (!l->computed.eff_waveform)
  1214. {
  1215. g_warning("Cannot load waveform \"%s\" in sample_dir \"%s\" : \"%s\"", l->sample, p->sample_dir, error ? error->message : "unknown error");
  1216. g_error_free(error);
  1217. }
  1218. }
  1219. else
  1220. l->computed.eff_waveform = NULL;
  1221. if (oldwf)
  1222. cbox_waveform_unref(oldwf);
  1223. l->computed.eff_is_silent = !l->sample || !strcmp(l->sample, "*silence");
  1224. l->sample_changed = FALSE;
  1225. }
  1226. l->computed.eff_use_keyswitch = ((l->sw_down != -1) || (l->sw_up != -1) || (l->sw_last != -1) || (l->sw_previous != -1));
  1227. l->computed.eff_use_simple_trigger_logic =
  1228. (l->seq_length == 1 && l->seq_position == 1) &&
  1229. (l->trigger != stm_first && l->trigger != stm_legato) &&
  1230. (l->lochan == 1 && l->hichan == 16) &&
  1231. (l->lorand == 0 && l->hirand == 1) &&
  1232. (l->lobend == -8192 && l->hibend == 8192) &&
  1233. (l->lochanaft == 0 && l->hichanaft == 127) &&
  1234. (l->lopolyaft == 0 && l->hipolyaft == 127) &&
  1235. (l->lobpm == 0 && l->hibpm == NO_HI_BPM_VALUE) &&
  1236. !l->cc && !l->computed.eff_use_keyswitch;
  1237. l->computed.eff_use_xfcc = l->xfin_cc || l->xfout_cc;
  1238. l->computed.eff_use_channel_mixer = l->position != 0 || l->width != 100;
  1239. l->computed.eff_freq = (l->computed.eff_waveform && l->computed.eff_waveform->info.samplerate) ? l->computed.eff_waveform->info.samplerate : 44100;
  1240. l->computed.eff_loop_mode = l->loop_mode;
  1241. l->computed.eff_use_filter_mods = l->cutoff != -1 || l->cutoff2 != -1;
  1242. if (l->loop_mode == slm_unknown)
  1243. {
  1244. if (l->computed.eff_waveform && l->computed.eff_waveform->has_loop)
  1245. l->computed.eff_loop_mode = slm_loop_continuous;
  1246. else
  1247. if (l->computed.eff_waveform)
  1248. l->computed.eff_loop_mode = l->loop_end == 0 ? slm_no_loop : slm_loop_continuous;
  1249. }
  1250. l->computed.eff_loop_start = l->loop_start;
  1251. l->computed.eff_loop_end = l->loop_end;
  1252. if (l->computed.eff_loop_mode == slm_one_shot || l->computed.eff_loop_mode == slm_no_loop || l->computed.eff_loop_mode == slm_one_shot_chokeable)
  1253. l->computed.eff_loop_start = SAMPLER_NO_LOOP;
  1254. if ((l->computed.eff_loop_mode == slm_loop_continuous || l->computed.eff_loop_mode == slm_loop_sustain) && l->computed.eff_loop_start == SAMPLER_NO_LOOP)
  1255. l->computed.eff_loop_start = 0;
  1256. if ((l->computed.eff_loop_mode == slm_loop_continuous || l->computed.eff_loop_mode == slm_loop_sustain) && l->computed.eff_loop_start == 0 && l->computed.eff_waveform && l->computed.eff_waveform->has_loop)
  1257. l->computed.eff_loop_start = l->computed.eff_waveform->loop_start;
  1258. if (l->loop_end == 0 && l->computed.eff_waveform != NULL)
  1259. l->computed.eff_loop_end = l->computed.eff_waveform->has_loop ? l->computed.eff_waveform->loop_end : l->computed.eff_waveform->info.frames;
  1260. if (l->off_mode == som_unknown)
  1261. l->off_mode = l->off_by != 0 ? som_fast : som_normal;
  1262. // XXXKF this is dodgy, needs to convert to use 'programmed vs effective' values pattern
  1263. if (l->key >= 0 && l->key <= 127)
  1264. l->lokey = l->hikey = l->pitch_keycenter = l->key;
  1265. // 'linearize' the virtual circular buffer - write 3 (or N) frames before end of the loop
  1266. // and 3 (N) frames at the start of the loop, and play it; in rare cases this will need to be
  1267. // repeated twice if output write pointer is close to CBOX_BLOCK_SIZE or playback rate is very low,
  1268. // but that's OK.
  1269. if (l->computed.eff_waveform && l->computed.eff_waveform->preloaded_frames == (size_t)l->computed.eff_waveform->info.frames)
  1270. {
  1271. int shift = l->computed.eff_waveform->info.channels == 2 ? 1 : 0;
  1272. uint32_t halfscratch = MAX_INTERPOLATION_ORDER << shift;
  1273. memcpy(&l->computed.scratch_loop[0], &l->computed.eff_waveform->data[(l->computed.eff_loop_end - MAX_INTERPOLATION_ORDER) << shift], halfscratch * sizeof(int16_t) );
  1274. memcpy(&l->computed.scratch_end[0], &l->computed.eff_waveform->data[(l->computed.eff_loop_end - MAX_INTERPOLATION_ORDER) << shift], halfscratch * sizeof(int16_t) );
  1275. memset(l->computed.scratch_end + halfscratch, 0, halfscratch * sizeof(int16_t));
  1276. if (l->computed.eff_loop_start != (uint32_t)-1)
  1277. memcpy(l->computed.scratch_loop + halfscratch, &l->computed.eff_waveform->data[l->computed.eff_loop_start << shift], halfscratch * sizeof(int16_t));
  1278. else
  1279. memset(l->computed.scratch_loop + halfscratch, 0, halfscratch * sizeof(int16_t));
  1280. }
  1281. if (l->cutoff < 20)
  1282. l->computed.logcutoff = -1;
  1283. else
  1284. l->computed.logcutoff = 1200.0 * log(l->cutoff / 440.0) / log(2) + 5700.0;
  1285. if (l->cutoff2 < 20)
  1286. l->computed.logcutoff2 = -1;
  1287. else
  1288. l->computed.logcutoff2 = 1200.0 * log(l->cutoff2 / 440.0) / log(2) + 5700.0;
  1289. l->computed.eq_bitmask = ((l->eq1.gain != 0 || l->eq1.vel2gain != 0) ? 1 : 0)
  1290. | ((l->eq2.gain != 0 || l->eq2.vel2gain != 0) ? 2 : 0)
  1291. | ((l->eq3.gain != 0 || l->eq3.vel2gain != 0) ? 4 : 0);
  1292. l->computed.mod_bitmask = 0;
  1293. for(struct sampler_modulation *mod = l->modulations; mod; mod = mod->next)
  1294. {
  1295. const struct sampler_modulation_key *mk = &mod->key;
  1296. if (mk->dest >= smdest_eg_stage_start && mk->dest <= smdest_eg_stage_end)
  1297. l->computed.mod_bitmask |= slmb_ampeg_cc << ((mk->dest >> 4) & 3);
  1298. }
  1299. l->computed.eff_use_prevoice = (l->delay || l->prevoice_nifs);
  1300. l->computed.eff_num_stages = sampler_filter_num_stages(l->cutoff, l->fil_type);
  1301. l->computed.eff_num_stages2 = sampler_filter_num_stages(l->cutoff2, l->fil2_type);
  1302. l->computed.resonance_scaled = pow(l->resonance_linearized, 1.f / l->computed.eff_num_stages);
  1303. l->computed.resonance2_scaled = pow(l->resonance2_linearized, 1.f / l->computed.eff_num_stages2);
  1304. }
  1305. void sampler_layer_reset_switches(struct sampler_layer *l, struct sampler_module *m)
  1306. {
  1307. l->current_seq_position = l->data.seq_position;
  1308. }
  1309. struct layer_foreach_struct
  1310. {
  1311. struct sampler_layer *layer;
  1312. const char *cfg_section;
  1313. };
  1314. static void layer_foreach_func(void *user_data, const char *key)
  1315. {
  1316. if (!strcmp(key, "file"))
  1317. key = "sample";
  1318. // import is handled in sampler_load_layer_overrides
  1319. if (!strcmp(key, "import"))
  1320. return;
  1321. // layer%d should be ignored, it's handled by sampler_program_new_from_cfg
  1322. if (!strncmp(key, "layer", 5) && isdigit(key[5]))
  1323. return;
  1324. struct layer_foreach_struct *lfs = user_data;
  1325. const char *value = cbox_config_get_string(lfs->cfg_section, key);
  1326. GError *error = NULL;
  1327. if (!sampler_layer_apply_param(lfs->layer, key, value, &error))
  1328. {
  1329. if (error)
  1330. g_warning("Error '%s', context: %s in section %s", error->message, key, lfs->cfg_section);
  1331. else
  1332. g_warning("Unknown sample layer parameter: %s in section %s", key, lfs->cfg_section);
  1333. }
  1334. }
  1335. void sampler_layer_load_overrides(struct sampler_layer *l, const char *cfg_section)
  1336. {
  1337. char *imp = cbox_config_get_string(cfg_section, "import");
  1338. if (imp)
  1339. sampler_layer_load_overrides(l, imp);
  1340. struct layer_foreach_struct lfs = {
  1341. .layer = l,
  1342. .cfg_section = cfg_section
  1343. };
  1344. cbox_config_foreach_key(layer_foreach_func, cfg_section, &lfs);
  1345. }
  1346. struct sampler_layer *sampler_layer_new_from_section(struct sampler_module *m, struct sampler_program *parent_program, struct sampler_layer *parent, const char *cfg_section)
  1347. {
  1348. struct sampler_layer *l = sampler_layer_new(m, parent_program, parent ? parent : parent_program->global->default_child->default_child);
  1349. sampler_layer_load_overrides(l, cfg_section);
  1350. sampler_layer_data_finalize(&l->data, l->parent ? &l->parent->data : NULL, parent_program);
  1351. sampler_layer_reset_switches(l, m);
  1352. return l;
  1353. }
  1354. static void sampler_layer_apply_unknown(struct sampler_layer *l, const char *key, const char *value)
  1355. {
  1356. if (!l->unknown_keys)
  1357. l->unknown_keys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
  1358. g_hash_table_insert(l->unknown_keys, g_strdup(key), g_strdup(value));
  1359. }
  1360. gboolean sampler_layer_apply_param(struct sampler_layer *l, const char *key, const char *value, GError **error)
  1361. {
  1362. int res = sampler_layer_apply_fixed_param(l, key, value, error);
  1363. if (res >= 0)
  1364. return res;
  1365. sampler_layer_apply_unknown(l, key, value);
  1366. //g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Unknown SFZ property key: '%s'", key);
  1367. //return FALSE;
  1368. g_warning("Unknown SFZ property key: '%s'", key);
  1369. return TRUE;
  1370. }
  1371. gboolean sampler_layer_unapply_param(struct sampler_layer *layer, const char *key, GError **error)
  1372. {
  1373. int res = sampler_layer_unapply_fixed_param(layer, key, error);
  1374. if (res >= 0)
  1375. return res;
  1376. g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Unknown SFZ property key: '%s'", key);
  1377. return FALSE;
  1378. }
  1379. #define TYPE_PRINTF_uint32_t(name, def_value) \
  1380. if (show_inherited || l->has_##name) \
  1381. g_string_append_printf(outstr, " %s=%u", #name, (unsigned)(l->name));
  1382. #define TYPE_PRINTF_int(name, def_value) \
  1383. if (show_inherited || l->has_##name) \
  1384. g_string_append_printf(outstr, " %s=%d", #name, (int)(l->name));
  1385. #define TYPE_PRINTF_midi_note_t(name, def_value) \
  1386. if (show_inherited || l->has_##name) { \
  1387. int val = l->name; \
  1388. if (val == -1) \
  1389. g_string_append_printf(outstr, " %s=-1", #name); \
  1390. else \
  1391. g_string_append_printf(outstr, " %s=%c%s%d", #name, "ccddeffggaab"[val%12], "\000#\000#\000\000#\000#\000#\000#\000"+(val%12), (val/12-1)); \
  1392. } else {}
  1393. #define TYPE_PRINTF_float(name, def_value) \
  1394. if (show_inherited || l->has_##name) \
  1395. g_string_append_printf(outstr, " %s=%s", #name, g_ascii_dtostr(floatbuf, floatbufsize, l->name));
  1396. #define PROC_FIELDS_TO_FILEPTR(type, name, def_value) \
  1397. TYPE_PRINTF_##type(name, def_value)
  1398. #define PROC_FIELDS_TO_FILEPTR_string(name) \
  1399. if (show_inherited || l->has_##name) \
  1400. g_string_append_printf(outstr, " %s=%s", #name, l->name ? l->name : "");
  1401. #define PROC_FIELDS_TO_FILEPTR_midicurve(name) \
  1402. for (uint32_t i = 0; i < 128; ++i) { \
  1403. if ((show_inherited || l->name.has_values[i]) && l->name.values[i] != SAMPLER_CURVE_GAP) \
  1404. g_string_append_printf(outstr, " %s_%u=%s", #name, (unsigned)i, g_ascii_dtostr(floatbuf, floatbufsize, l->name.values[i])); \
  1405. }
  1406. #define PROC_FIELDS_TO_FILEPTR_dBamp(type, name, def_value) \
  1407. if (show_inherited || l->has_##name) \
  1408. g_string_append_printf(outstr, " %s=%s", #name, g_ascii_dtostr(floatbuf, floatbufsize, l->name));
  1409. #define PROC_FIELDS_TO_FILEPTR_enum(enumtype, name, def_value) \
  1410. if ((show_inherited || l->has_##name) && (tmpstr = enumtype##_to_string(l->name)) != NULL) \
  1411. g_string_append_printf(outstr, " %s=%s", #name, tmpstr);
  1412. #define ENV_PARAM_OUTPUT(param, index, def_value, env, envfield, envname) \
  1413. if (show_inherited || l->has_##envfield.param) \
  1414. g_string_append_printf(outstr, " " #envname "_" #param "=%s", g_ascii_dtostr(floatbuf, floatbufsize, env.param));
  1415. #define PROC_FIELDS_TO_FILEPTR_dahdsr(name, parname, index) \
  1416. DAHDSR_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
  1417. #define PROC_FIELDS_TO_FILEPTR_lfo(name, parname, index) \
  1418. LFO_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
  1419. #define PROC_FIELDS_TO_FILEPTR_eq(name, parname, index) \
  1420. EQ_FIELDS(ENV_PARAM_OUTPUT, l->name, name, parname)
  1421. #define PROC_FIELDS_TO_FILEPTR_ccrange(name, parname) \
  1422. { \
  1423. struct sampler_cc_range *range = l->name; \
  1424. while (range) { \
  1425. if (show_inherited || range->value.has_locc) \
  1426. g_string_append_printf(outstr, " " #parname "locc%d=%d", range->key.cc_number, range->value.locc); \
  1427. if (show_inherited || range->value.has_hicc) \
  1428. g_string_append_printf(outstr, " " #parname "hicc%d=%d", range->key.cc_number, range->value.hicc); \
  1429. range = range->next; \
  1430. } \
  1431. }
  1432. static const char *addrandom_variants[] = { "amp", "fil", "pitch" };
  1433. static const char *env_stages[] = { "delay", "attack", "hold", "decay", "sustain", "release", "start" };
  1434. static const char *modsrc_names[] = { "vel", "chanaft", "polyaft", "pitch", "pitcheg", "fileg", "ampeg", "pitchlfo", "fillfo", "amplfo", "" };
  1435. static const char *moddest_names[] = { "gain", "pitch", "cutoff", "resonance", "tonectl", "pan", "amplitude", "cutoff2", "resonance2", "pitchlfo_freq", "fillfo_freq", "amplfo_freq",
  1436. "eq1_freq", "eq1_bw", "eq1_gain",
  1437. "eq2_freq", "eq2_bw", "eq2_gain",
  1438. "eq3_freq", "eq3_bw", "eq3_gain",
  1439. };
  1440. static void mod_cc_attrib_to_string(GString *outstr, const char *attrib, const struct sampler_modulation_key *md, const char *floatbuf)
  1441. {
  1442. if (md->dest >= smdest_eg_stage_start && md->dest <= smdest_eg_stage_end)
  1443. {
  1444. uint32_t param = md->dest - smdest_eg_stage_start;
  1445. g_string_append_printf(outstr, " %seg_%s%s%d=%s", addrandom_variants[(param >> 4) & 3], env_stages[param & 15], attrib, md->src, floatbuf);
  1446. }
  1447. else if (md->src < smsrc_perchan_count)
  1448. {
  1449. g_string_append_printf(outstr, " %s%s%d=%s", moddest_names[md->dest], attrib, md->src, floatbuf);
  1450. }
  1451. else if ((md->src == smsrc_amplfo && md->dest == smdest_gain) ||
  1452. (md->src == smsrc_fillfo && md->dest == smdest_cutoff) ||
  1453. (md->src == smsrc_pitchlfo && md->dest == smdest_pitch))
  1454. {
  1455. if (md->src2 < EXT_CC_COUNT)
  1456. g_string_append_printf(outstr, " %s_depth%s%d=%s", modsrc_names[md->src - smsrc_perchan_count], attrib, md->src2, floatbuf);
  1457. }
  1458. else if ((md->src == smsrc_ampenv && md->dest == smdest_gain) ||
  1459. (md->src == smsrc_filenv && md->dest == smdest_cutoff) ||
  1460. (md->src == smsrc_pitchenv && md->dest == smdest_pitch))
  1461. {
  1462. if (md->src2 < EXT_CC_COUNT)
  1463. g_string_append_printf(outstr, " %s_depth%s%d=%s", modsrc_names[md->src - smsrc_perchan_count], attrib, md->src2, floatbuf);
  1464. }
  1465. else if ((md->src == smsrc_filenv && md->dest == smdest_cutoff2) ||
  1466. (md->src == smsrc_fillfo && md->dest == smdest_cutoff2))
  1467. {
  1468. if (md->src2 < EXT_CC_COUNT)
  1469. g_string_append_printf(outstr, " %s_depth2%s%d=%s", modsrc_names[md->src - smsrc_perchan_count], attrib, md->src2, floatbuf);
  1470. }
  1471. else
  1472. assert(md->src2 >= EXT_CC_COUNT);
  1473. }
  1474. static void nif_attrib_to_string(GString *outstr, const char *attrib, const struct sampler_noteinitfunc *nd, const char *floatbuf)
  1475. {
  1476. int v = nd->key.variant;
  1477. if (nd->value.value)
  1478. g_string_append_printf(outstr, " %s_cc%d=%s", attrib, v, floatbuf);
  1479. if (nd->value.curve_id)
  1480. g_string_append_printf(outstr, " %s_curvecc%d=%d", attrib, v, nd->value.curve_id);
  1481. if (nd->value.step)
  1482. {
  1483. char floatbuf2[G_ASCII_DTOSTR_BUF_SIZE];
  1484. int floatbufsize = G_ASCII_DTOSTR_BUF_SIZE;
  1485. g_ascii_dtostr(floatbuf2, floatbufsize, nd->value.step);
  1486. g_string_append_printf(outstr, " %s_stepcc%d=%s", attrib, v, floatbuf2);
  1487. }
  1488. }
  1489. gchar *sampler_layer_to_string(struct sampler_layer *lr, gboolean show_inherited)
  1490. {
  1491. struct sampler_layer_data *l = &lr->data;
  1492. GString *outstr = g_string_sized_new(200);
  1493. const char *tmpstr;
  1494. char floatbuf[G_ASCII_DTOSTR_BUF_SIZE];
  1495. int floatbufsize = G_ASCII_DTOSTR_BUF_SIZE;
  1496. SAMPLER_FIXED_FIELDS(PROC_FIELDS_TO_FILEPTR)
  1497. for(struct sampler_noteinitfunc *nd = l->voice_nifs; nd; nd = nd->next)
  1498. {
  1499. if (!nd->value.has_value && !nd->value.has_curve && !nd->value.has_step && !show_inherited)
  1500. continue;
  1501. #define PROC_ENVSTAGE_NAME(name, index, def_value) #name,
  1502. static const char *env_stages[] = { DAHDSR_FIELDS(PROC_ENVSTAGE_NAME) "start" };
  1503. uint32_t v = nd->key.variant;
  1504. g_ascii_dtostr(floatbuf, floatbufsize, nd->value.value);
  1505. if (nd->key.notefunc_voice == sampler_nif_addrandom && v >= 0 && v <= 2)
  1506. g_string_append_printf(outstr, " %s_random=%s", addrandom_variants[v], floatbuf);
  1507. else if (nd->key.notefunc_voice == sampler_nif_vel2pitch)
  1508. g_string_append_printf(outstr, " pitch_veltrack=%s", floatbuf);
  1509. else if (nd->key.notefunc_voice == sampler_nif_vel2reloffset)
  1510. g_string_append_printf(outstr, " reloffset_veltrack=%s", floatbuf);
  1511. else if (nd->key.notefunc_voice == sampler_nif_cc2reloffset)
  1512. nif_attrib_to_string(outstr, "reloffset", nd, floatbuf);
  1513. else if (nd->key.notefunc_voice == sampler_nif_vel2offset)
  1514. g_string_append_printf(outstr, " offset_veltrack=%s", floatbuf);
  1515. else if (nd->key.notefunc_voice == sampler_nif_cc2offset)
  1516. nif_attrib_to_string(outstr, "offset", nd, floatbuf);
  1517. else if (nd->key.notefunc_voice == sampler_nif_vel2env && (v & 15) >= snif_env_delay && (v & 15) <= snif_env_start && ((v >> 4) & 3) < 3)
  1518. g_string_append_printf(outstr, " %seg_vel2%s=%s", addrandom_variants[v >> 4], env_stages[1 + (v & 15)], floatbuf);
  1519. else
  1520. assert(0); // unknown NIF
  1521. }
  1522. for(struct sampler_noteinitfunc *nd = l->prevoice_nifs; nd; nd = nd->next)
  1523. {
  1524. if (!nd->value.has_value && !nd->value.has_curve && !nd->value.has_step && !show_inherited)
  1525. continue;
  1526. g_ascii_dtostr(floatbuf, floatbufsize, nd->value.value);
  1527. if (nd->key.notefunc_prevoice == sampler_nif_cc2delay)
  1528. nif_attrib_to_string(outstr, "delay", nd, floatbuf);
  1529. else if (nd->key.notefunc_prevoice == sampler_nif_addrandomdelay)
  1530. g_string_append_printf(outstr, " delay_random=%s", floatbuf);
  1531. else
  1532. assert(0); // unknown NIF
  1533. }
  1534. for(struct sampler_flex_lfo *flfo = l->flex_lfos; flfo; flfo = flfo->next)
  1535. {
  1536. if (flfo->value.has_freq || show_inherited)
  1537. {
  1538. g_ascii_dtostr(floatbuf, floatbufsize, flfo->value.freq);
  1539. g_string_append_printf(outstr, " lfo%d_freq=%s", (int)flfo->key.id, floatbuf);
  1540. }
  1541. if (flfo->value.has_delay || show_inherited)
  1542. {
  1543. g_ascii_dtostr(floatbuf, floatbufsize, flfo->value.delay);
  1544. g_string_append_printf(outstr, " lfo%d_delay=%s", (int)flfo->key.id, floatbuf);
  1545. }
  1546. if (flfo->value.has_fade || show_inherited)
  1547. {
  1548. g_ascii_dtostr(floatbuf, floatbufsize, flfo->value.fade);
  1549. g_string_append_printf(outstr, " lfo%d_fade=%s", (int)flfo->key.id, floatbuf);
  1550. }
  1551. if (flfo->value.has_wave || show_inherited)
  1552. g_string_append_printf(outstr, " lfo%d_wave=%d", (int)flfo->key.id, flfo->value.wave);
  1553. }
  1554. for(struct sampler_modulation *md = l->modulations; md; md = md->next)
  1555. {
  1556. const struct sampler_modulation_key *mk = &md->key;
  1557. const struct sampler_modulation_value *mv = &md->value;
  1558. if (mv->has_curve || show_inherited)
  1559. {
  1560. g_ascii_dtostr(floatbuf, floatbufsize, mv->curve_id);
  1561. mod_cc_attrib_to_string(outstr, "_curvecc", mk, floatbuf);
  1562. }
  1563. if (mv->has_smooth || show_inherited)
  1564. {
  1565. g_ascii_dtostr(floatbuf, floatbufsize, mv->smooth);
  1566. mod_cc_attrib_to_string(outstr, "_smoothcc", mk, floatbuf);
  1567. }
  1568. if (mv->has_step || show_inherited)
  1569. {
  1570. g_ascii_dtostr(floatbuf, floatbufsize, mv->step);
  1571. mod_cc_attrib_to_string(outstr, "_stepcc", mk, floatbuf);
  1572. }
  1573. if (mv->has_amount || show_inherited)
  1574. {
  1575. gboolean is_egcc = mk->dest >= smdest_eg_stage_start && mk->dest <= smdest_eg_stage_end;
  1576. gboolean is_lfofreq = mk->dest >= smdest_pitchlfo_freq && mk->dest <= smdest_eq3_gain;
  1577. g_ascii_dtostr(floatbuf, floatbufsize, mv->amount);
  1578. if (mk->src2 == smsrc_none)
  1579. {
  1580. if (is_egcc)
  1581. {
  1582. uint32_t param = mk->dest - smdest_eg_stage_start;
  1583. g_string_append_printf(outstr, " %seg_%scc%d=%s", addrandom_variants[(param >> 4) & 3], env_stages[param & 15], mk->src, floatbuf);
  1584. continue;
  1585. }
  1586. if (mk->src < smsrc_perchan_count)
  1587. {
  1588. // Inconsistency: cutoff_cc5 but amplfo_freqcc5
  1589. if (is_lfofreq)
  1590. g_string_append_printf(outstr, " %scc%d=%s", moddest_names[mk->dest], mk->src, floatbuf);
  1591. else
  1592. g_string_append_printf(outstr, " %s_cc%d=%s", moddest_names[mk->dest], mk->src, floatbuf);
  1593. continue;
  1594. }
  1595. if (mk->src < smsrc_perchan_count + sizeof(modsrc_names) / sizeof(modsrc_names[0]))
  1596. {
  1597. if ((mk->src == smsrc_filenv && mk->dest == smdest_cutoff) ||
  1598. (mk->src == smsrc_pitchenv && mk->dest == smdest_pitch) ||
  1599. (mk->src == smsrc_amplfo && mk->dest == smdest_gain) ||
  1600. (mk->src == smsrc_fillfo && mk->dest == smdest_cutoff) ||
  1601. (mk->src == smsrc_pitchlfo && mk->dest == smdest_pitch))
  1602. g_string_append_printf(outstr, " %s_depth=%s", modsrc_names[mk->src - smsrc_perchan_count], floatbuf);
  1603. else if ((mk->src == smsrc_filenv && mk->dest == smdest_cutoff2) ||
  1604. (mk->src == smsrc_fillfo && mk->dest == smdest_cutoff2))
  1605. g_string_append_printf(outstr, " %s_depth2=%s", modsrc_names[mk->src - smsrc_perchan_count], floatbuf);
  1606. else if (is_lfofreq)
  1607. g_string_append_printf(outstr, " %s%s=%s", moddest_names[mk->dest], modsrc_names[mk->src - smsrc_perchan_count], floatbuf);
  1608. else
  1609. g_string_append_printf(outstr, " %s_%s=%s", moddest_names[mk->dest], modsrc_names[mk->src - smsrc_perchan_count], floatbuf);
  1610. continue;
  1611. }
  1612. }
  1613. if ((mk->src == smsrc_amplfo && mk->dest == smdest_gain) ||
  1614. (mk->src == smsrc_fillfo && mk->dest == smdest_cutoff) ||
  1615. (mk->src == smsrc_pitchlfo && mk->dest == smdest_pitch))
  1616. {
  1617. switch(mk->src2)
  1618. {
  1619. case smsrc_chanaft:
  1620. case smsrc_polyaft:
  1621. g_string_append_printf(outstr, " %s_depth%s=%s", modsrc_names[mk->src - smsrc_perchan_count], modsrc_names[mk->src2 - smsrc_perchan_count], floatbuf);
  1622. continue;
  1623. case smsrc_none:
  1624. g_string_append_printf(outstr, " %s_depth=%s", modsrc_names[mk->src - smsrc_perchan_count], floatbuf);
  1625. continue;
  1626. default:
  1627. if (mk->src2 < EXT_CC_COUNT)
  1628. {
  1629. g_string_append_printf(outstr, " %s_depthcc%d=%s", modsrc_names[mk->src - smsrc_perchan_count], mk->src2, floatbuf);
  1630. continue;
  1631. }
  1632. break;
  1633. }
  1634. }
  1635. if ((mk->src == smsrc_ampenv && mk->dest == smdest_gain) ||
  1636. (mk->src == smsrc_filenv && mk->dest == smdest_cutoff) ||
  1637. (mk->src == smsrc_pitchenv && mk->dest == smdest_pitch))
  1638. {
  1639. if (mk->src2 == smsrc_vel)
  1640. {
  1641. g_string_append_printf(outstr, " %s_vel2depth=%s", modsrc_names[mk->src - smsrc_perchan_count], floatbuf);
  1642. continue;
  1643. }
  1644. if (mk->src2 == smsrc_none)
  1645. {
  1646. g_string_append_printf(outstr, " %s_depth=%s", modsrc_names[mk->src - smsrc_perchan_count], floatbuf);
  1647. continue;
  1648. }
  1649. if (mk->src2 < EXT_CC_COUNT)
  1650. {
  1651. g_string_append_printf(outstr, " %s_depthcc%d=%s", modsrc_names[mk->src - smsrc_perchan_count], mk->src2, floatbuf);
  1652. continue;
  1653. }
  1654. }
  1655. if (mk->src == smsrc_filenv && mk->dest == smdest_cutoff2)
  1656. {
  1657. if (mk->src2 == smsrc_vel)
  1658. {
  1659. g_string_append_printf(outstr, " %s_vel2depth2=%s", modsrc_names[mk->src - smsrc_perchan_count], floatbuf);
  1660. continue;
  1661. }
  1662. assert(mk->src2 != smsrc_none);
  1663. if (mk->src2 < EXT_CC_COUNT)
  1664. {
  1665. g_string_append_printf(outstr, " %s_depth2cc%d=%s", modsrc_names[mk->src - smsrc_perchan_count], mk->src2, floatbuf);
  1666. continue;
  1667. }
  1668. }
  1669. if (mk->src == smsrc_fillfo && mk->dest == smdest_cutoff2)
  1670. {
  1671. assert(mk->src2 != smsrc_none);
  1672. if (mk->src2 < EXT_CC_COUNT)
  1673. {
  1674. g_string_append_printf(outstr, " %s_depth2cc%d=%s", modsrc_names[mk->src - smsrc_perchan_count], mk->src2, floatbuf);
  1675. continue;
  1676. }
  1677. }
  1678. g_string_append_printf(outstr, " genericmod_%d_%d_%d_%d=%s", mk->src, mk->src2, mk->dest, mv->curve_id, floatbuf);
  1679. }
  1680. }
  1681. if (lr->unknown_keys)
  1682. {
  1683. GHashTableIter hti;
  1684. gchar *key, *value;
  1685. g_hash_table_iter_init(&hti, lr->unknown_keys);
  1686. while(g_hash_table_iter_next(&hti, (gpointer *)&key, (gpointer *)&value))
  1687. g_string_append_printf(outstr, " %s=%s", key, value);
  1688. }
  1689. gchar *res = outstr->str;
  1690. g_string_free(outstr, FALSE);
  1691. return res;
  1692. }
  1693. void sampler_layer_dump(struct sampler_layer *l, FILE *f)
  1694. {
  1695. gchar *str = sampler_layer_to_string(l, FALSE);
  1696. fprintf(f, "%s\n", str);
  1697. }
  1698. void sampler_layer_data_close(struct sampler_layer_data *l)
  1699. {
  1700. sampler_flex_lfos_destroy(l->flex_lfos);
  1701. sampler_cc_ranges_destroy(l->cc);
  1702. sampler_cc_ranges_destroy(l->on_cc);
  1703. sampler_cc_ranges_destroy(l->xfin_cc);
  1704. sampler_cc_ranges_destroy(l->xfout_cc);
  1705. sampler_noteinitfuncs_destroy(l->voice_nifs);
  1706. sampler_noteinitfuncs_destroy(l->prevoice_nifs);
  1707. sampler_modulations_destroy(l->modulations);
  1708. if (l->computed.eff_waveform)
  1709. {
  1710. cbox_waveform_unref(l->computed.eff_waveform);
  1711. l->computed.eff_waveform = NULL;
  1712. }
  1713. g_free(l->sample);
  1714. }
  1715. void sampler_layer_data_destroy(struct sampler_layer_data *l)
  1716. {
  1717. sampler_layer_data_close(l);
  1718. free(l);
  1719. }
  1720. struct sampler_layer *sampler_layer_new_clone(struct sampler_layer *layer,
  1721. struct sampler_module *m, struct sampler_program *parent_program, struct sampler_layer *parent)
  1722. {
  1723. struct sampler_layer *l = sampler_layer_new(m, parent_program, parent);
  1724. sampler_layer_data_clone(&l->data, &layer->data, TRUE);
  1725. sampler_layer_reset_switches(l, m);
  1726. if (layer->unknown_keys)
  1727. {
  1728. GHashTableIter iter;
  1729. g_hash_table_iter_init(&iter, layer->unknown_keys);
  1730. gpointer key, value;
  1731. while(g_hash_table_iter_next(&iter, &key, &value))
  1732. sampler_layer_apply_param(l, (gchar *)key, (gchar *)value, NULL);
  1733. }
  1734. GHashTableIter iter;
  1735. g_hash_table_iter_init(&iter, layer->child_layers);
  1736. gpointer key, value;
  1737. gboolean is_child_a_region = layer->parent && layer->parent->parent;
  1738. while(g_hash_table_iter_next(&iter, &key, &value))
  1739. {
  1740. struct sampler_layer *chl = sampler_layer_new_clone(key, m, parent_program, l);
  1741. g_hash_table_insert(l->child_layers, chl, NULL);
  1742. if (key == layer->default_child)
  1743. l->default_child = chl;
  1744. if (is_child_a_region)
  1745. sampler_program_add_layer(parent_program, chl);
  1746. }
  1747. return l;
  1748. }
  1749. void sampler_layer_destroyfunc(struct cbox_objhdr *objhdr)
  1750. {
  1751. struct sampler_layer *l = CBOX_H2O(objhdr);
  1752. struct sampler_program *prg = l->parent_program;
  1753. assert(g_hash_table_size(l->child_layers) == 0);
  1754. if (l->parent)
  1755. {
  1756. g_hash_table_remove(l->parent->child_layers, l);
  1757. if (prg && prg->rll)
  1758. {
  1759. sampler_program_delete_layer(prg, l);
  1760. sampler_program_update_layers(l->parent_program);
  1761. }
  1762. l->parent = NULL;
  1763. }
  1764. sampler_layer_data_close(&l->data);
  1765. if (l->runtime)
  1766. sampler_layer_data_destroy(l->runtime);
  1767. if (l->unknown_keys)
  1768. g_hash_table_destroy(l->unknown_keys);
  1769. if (l->child_layers)
  1770. g_hash_table_destroy(l->child_layers);
  1771. free(l);
  1772. }
  1773. //////////////////////////////////////////////////////////////////////////
  1774. struct sampler_layer_update_cmd
  1775. {
  1776. struct sampler_module *module;
  1777. struct sampler_layer *layer;
  1778. struct sampler_layer_data *new_data;
  1779. struct sampler_layer_data *old_data;
  1780. };
  1781. static int sampler_layer_update_cmd_prepare(void *data)
  1782. {
  1783. struct sampler_layer_update_cmd *cmd = data;
  1784. cmd->old_data = cmd->layer->runtime;
  1785. cmd->new_data = calloc(1, sizeof(struct sampler_layer_data));
  1786. sampler_layer_data_clone(cmd->new_data, &cmd->layer->data, TRUE);
  1787. sampler_layer_data_finalize(cmd->new_data, cmd->layer->parent ? &cmd->layer->parent->data : NULL, cmd->layer->parent_program);
  1788. if (cmd->layer->runtime == NULL)
  1789. {
  1790. // initial update of the layer, so none of the voices need updating yet
  1791. // because the layer hasn't been allocated to any voice
  1792. cmd->layer->runtime = cmd->new_data;
  1793. free(cmd);
  1794. return 1;
  1795. }
  1796. return 0;
  1797. }
  1798. static int sampler_layer_update_cmd_execute(void *data)
  1799. {
  1800. struct sampler_layer_update_cmd *cmd = data;
  1801. for (int i = 0; i < 16; i++)
  1802. {
  1803. FOREACH_VOICE(cmd->module->channels[i].voices_running, v)
  1804. {
  1805. if (v->layer == cmd->layer->runtime)
  1806. {
  1807. v->layer = cmd->new_data;
  1808. v->layer_changed = TRUE;
  1809. sampler_voice_update_params_from_layer(v);
  1810. }
  1811. }
  1812. }
  1813. FOREACH_PREVOICE(cmd->module->prevoices_running, pv)
  1814. {
  1815. if (pv->layer_data == cmd->layer->runtime)
  1816. {
  1817. pv->layer_data = cmd->new_data;
  1818. // XXXKF when need arises
  1819. // pv->layer_changed = TRUE;
  1820. // sampler_prevoice_update_params_from_layer(v);
  1821. }
  1822. }
  1823. cmd->old_data = cmd->layer->runtime;
  1824. cmd->layer->runtime = cmd->new_data;
  1825. return 10;
  1826. }
  1827. static void sampler_layer_update_cmd_cleanup(void *data)
  1828. {
  1829. struct sampler_layer_update_cmd *cmd = data;
  1830. sampler_layer_data_destroy(cmd->old_data);
  1831. free(cmd);
  1832. }
  1833. void sampler_layer_update(struct sampler_layer *l)
  1834. {
  1835. // if changing a group, update all child regions instead
  1836. if (g_hash_table_size(l->child_layers))
  1837. {
  1838. GHashTableIter iter;
  1839. g_hash_table_iter_init(&iter, l->child_layers);
  1840. gpointer key, value;
  1841. while(g_hash_table_iter_next(&iter, &key, &value))
  1842. {
  1843. sampler_layer_data_finalize(&((struct sampler_layer *)key)->data, &l->data, l->parent_program);
  1844. sampler_layer_update((struct sampler_layer *)key);
  1845. }
  1846. return;
  1847. }
  1848. static struct cbox_rt_cmd_definition rtcmd = {
  1849. .prepare = sampler_layer_update_cmd_prepare,
  1850. .execute = sampler_layer_update_cmd_execute,
  1851. .cleanup = sampler_layer_update_cmd_cleanup,
  1852. };
  1853. struct sampler_layer_update_cmd *lcmd = malloc(sizeof(struct sampler_layer_update_cmd));
  1854. lcmd->module = l->module;
  1855. lcmd->layer = l;
  1856. lcmd->new_data = NULL;
  1857. lcmd->old_data = NULL;
  1858. cbox_rt_execute_cmd_async(l->module->module.rt, &rtcmd, lcmd);
  1859. }