DPF with Max Gen
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.

483 lines
12KB

  1. /*******************************************************************************************************************
  2. Cycling '74 License for Max-Generated Code for Export
  3. Copyright (c) 2016 Cycling '74
  4. The code that Max generates automatically and that end users are capable of exporting and using, and any
  5. associated documentation files (the “Software”) is a work of authorship for which Cycling '74 is the author
  6. and owner for copyright purposes. A license is hereby granted, free of charge, to any person obtaining a
  7. copy of the Software (“Licensee”) to use, copy, modify, merge, publish, and distribute copies of the Software,
  8. and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  9. The Software is licensed to Licensee only for non-commercial use. Users who wish to make commercial use of the
  10. Software must contact the copyright owner to determine if a license for commercial use is available, and the
  11. terms and conditions for same, which may include fees or royalties. For commercial use, please send inquiries
  12. to licensing@cycling74.com. The determination of whether a use is commercial use or non-commercial use is based
  13. upon the use, not the user. The Software may be used by individuals, institutions, governments, corporations, or
  14. other business whether for-profit or non-profit so long as the use itself is not a commercialization of the
  15. materials or a use that generates or is intended to generate income, revenue, sales or profit.
  16. The above copyright notice and this license shall be included in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  18. THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  20. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. DEALINGS IN THE SOFTWARE.
  22. *******************************************************************************************************************/
  23. #include "genlib.h"
  24. #include "genlib_exportfunctions.h"
  25. #include <stdlib.h> // not cstdlib (causes problems with ARM embedded compiler)
  26. #include <cstdio>
  27. #include <cstring>
  28. #ifdef __APPLE__
  29. # include <malloc/malloc.h>
  30. #elif !defined(GEN_WINDOWS) // WIN32?
  31. # include <malloc.h>
  32. # define malloc_size malloc_usable_size
  33. #endif
  34. #ifdef MSP_ON_CLANG
  35. # include "json.c"
  36. # include "json_builder.c"
  37. #endif
  38. #if GENLIB_USE_JSON
  39. # include "json.h"
  40. # include "json_builder.h"
  41. #endif
  42. // DATA_MAXIMUM_ELEMENTS * 8 bytes = 256 mb limit
  43. #define DATA_MAXIMUM_ELEMENTS (33554432)
  44. //////////// export_genlib.cpp ////////////
  45. // export version
  46. void my_memset(void *p, int c, long size);
  47. void my_memcpy(void *dst, const void *src, long size);
  48. t_ptr sysmem_newptr(t_ptr_size size)
  49. {
  50. return (t_ptr)malloc(size);
  51. }
  52. t_ptr sysmem_newptrclear(t_ptr_size size)
  53. {
  54. t_ptr p = (t_ptr)malloc(size);
  55. if (p)
  56. my_memset(p, 0, size);
  57. return p;
  58. }
  59. t_ptr sysmem_resizeptr(void *ptr, t_ptr_size newsize)
  60. {
  61. return (t_ptr)realloc(ptr, newsize);
  62. }
  63. t_ptr sysmem_resizeptrclear(void *ptr, t_ptr_size newsize)
  64. {
  65. size_t oldsize = malloc_size(ptr);
  66. t_ptr p = (t_ptr)realloc(ptr, newsize);
  67. if (p) {
  68. if (newsize > oldsize)
  69. my_memset((char *)p + oldsize, 0, newsize - oldsize);
  70. }
  71. return p;
  72. }
  73. t_ptr_size sysmem_ptrsize(void *ptr)
  74. {
  75. return malloc_size(ptr);
  76. }
  77. void sysmem_freeptr(void *ptr)
  78. {
  79. free(ptr);
  80. }
  81. void sysmem_copyptr(const void *src, void *dst, t_ptr_size bytes)
  82. {
  83. my_memcpy(dst, src, bytes);
  84. }
  85. void my_memset(void *p, int c, long size)
  86. {
  87. char *p2 = (char *)p;
  88. int i;
  89. for (i = 0; i < size; i++, p2++)
  90. *p2 = char(c);
  91. }
  92. void my_memcpy(void *dst, const void *src, long size)
  93. {
  94. char *s2 = (char *)src;
  95. char *d2 = (char *)dst;
  96. int i;
  97. for (i = 0; i < size; i++, s2++, d2++)
  98. *d2 = *s2;
  99. }
  100. void set_zero64(t_sample *memory, long size)
  101. {
  102. long i;
  103. for (i = 0; i < size; i++, memory++) {
  104. *memory = 0.;
  105. }
  106. }
  107. void genlib_report_error(const char *s)
  108. {
  109. #ifndef GEN_NO_STDLIB
  110. fprintf(stderr, "%s\n", s);
  111. #endif
  112. }
  113. void genlib_report_message(const char *s)
  114. {
  115. #ifndef GEN_NO_STDLIB
  116. fprintf(stdout, "%s\n", s);
  117. #endif
  118. }
  119. unsigned long systime_ticks(void)
  120. {
  121. return 0; // Gen code can deal with this
  122. }
  123. #ifdef GEN_WINDOWS
  124. // NEED THIS FOR WINDOWS:
  125. void *operator new(size_t size) { return sysmem_newptr(size); }
  126. void *operator new[](size_t size) { return sysmem_newptr(size); }
  127. void operator delete(void *p) throw() { sysmem_freeptr(p); }
  128. void operator delete[](void *p) throw() { sysmem_freeptr(p); }
  129. #endif
  130. void *genlib_obtain_reference_from_string(const char *name)
  131. {
  132. return 0; // to be implemented
  133. }
  134. // the rest is stuff to isolate gensym, attrs, atoms, buffers etc.
  135. t_genlib_buffer *genlib_obtain_buffer_from_reference(void *ref)
  136. {
  137. return 0; // to be implemented
  138. }
  139. t_genlib_err genlib_buffer_edit_begin(t_genlib_buffer *b)
  140. {
  141. return 0; // to be implemented
  142. }
  143. t_genlib_err genlib_buffer_edit_end(t_genlib_buffer *b, long valid)
  144. {
  145. return 0; // to be implemented
  146. }
  147. t_genlib_err genlib_buffer_getinfo(t_genlib_buffer *b, t_genlib_buffer_info *info)
  148. {
  149. return 0; // to be implemented
  150. }
  151. char *genlib_reference_getname(void *ref)
  152. {
  153. return 0; // to be implemented
  154. }
  155. void genlib_buffer_dirty(t_genlib_buffer *b)
  156. {
  157. // to be implemented
  158. }
  159. t_genlib_err genlib_buffer_perform_begin(t_genlib_buffer *b)
  160. {
  161. return 0; // to be implemented
  162. }
  163. void genlib_buffer_perform_end(t_genlib_buffer *b)
  164. {
  165. // to be implemented
  166. }
  167. t_sample gen_msp_pow(t_sample value, t_sample power)
  168. {
  169. #ifdef GENLIB_USE_FLOAT32
  170. return powf(value, power);
  171. #else
  172. return pow(value, power);
  173. #endif
  174. }
  175. void genlib_data_setbuffer(t_genlib_data *b, void *ref)
  176. {
  177. genlib_report_error("not supported for export targets\n");
  178. }
  179. typedef struct {
  180. t_genlib_data_info info;
  181. t_sample cursor; // used by Delay
  182. //t_symbol * name;
  183. } t_dsp_gen_data;
  184. t_genlib_data *genlib_obtain_data_from_reference(void *ref)
  185. {
  186. t_dsp_gen_data *self = (t_dsp_gen_data *)malloc(sizeof(t_dsp_gen_data));
  187. self->info.dim = 0;
  188. self->info.channels = 0;
  189. self->info.data = 0;
  190. self->cursor = 0;
  191. return (t_genlib_data *)self;
  192. }
  193. t_genlib_err genlib_data_getinfo(t_genlib_data *b, t_genlib_data_info *info)
  194. {
  195. t_dsp_gen_data *self = (t_dsp_gen_data *)b;
  196. info->dim = self->info.dim;
  197. info->channels = self->info.channels;
  198. info->data = self->info.data;
  199. return GENLIB_ERR_NONE;
  200. }
  201. void genlib_data_release(t_genlib_data *b)
  202. {
  203. t_dsp_gen_data *self = (t_dsp_gen_data *)b;
  204. if (self->info.data) {
  205. genlib_sysmem_freeptr(self->info.data);
  206. self->info.data = 0;
  207. }
  208. }
  209. long genlib_data_getcursor(t_genlib_data *b)
  210. {
  211. t_dsp_gen_data *self = (t_dsp_gen_data *)b;
  212. return long(self->cursor);
  213. }
  214. void genlib_data_setcursor(t_genlib_data *b, long cursor)
  215. {
  216. t_dsp_gen_data *self = (t_dsp_gen_data *)b;
  217. self->cursor = t_sample(cursor);
  218. }
  219. void genlib_data_resize(t_genlib_data *b, long s, long c)
  220. {
  221. t_dsp_gen_data *self = (t_dsp_gen_data *)b;
  222. size_t sz, oldsz, copysz;
  223. t_sample *old = 0;
  224. t_sample *replaced = 0;
  225. int i, j, copydim, copychannels, olddim, oldchannels;
  226. //printf("data resize %d %d\n", s, c);
  227. // cache old for copying:
  228. old = self->info.data;
  229. olddim = self->info.dim;
  230. oldchannels = self->info.channels;
  231. // limit [data] size:
  232. if (s * c > DATA_MAXIMUM_ELEMENTS) {
  233. s = DATA_MAXIMUM_ELEMENTS/c;
  234. genlib_report_message("warning: constraining [data] to < 256MB");
  235. }
  236. // bytes required:
  237. sz = sizeof(t_sample) * s * c;
  238. oldsz = sizeof(t_sample) * olddim * oldchannels;
  239. if (old && sz == oldsz) {
  240. // no need to re-allocate, just resize
  241. // careful, audio thread may still be using it:
  242. if (s > olddim) {
  243. self->info.channels = c;
  244. self->info.dim = s;
  245. } else {
  246. self->info.dim = s;
  247. self->info.channels = c;
  248. }
  249. set_zero64(self->info.data, s * c);
  250. return;
  251. } else {
  252. // allocate new:
  253. replaced = (t_sample *)sysmem_newptr(sz);
  254. // check allocation:
  255. if (replaced == 0) {
  256. genlib_report_error("allocating [data]: out of memory");
  257. // try to reallocate with a default/minimal size instead:
  258. if (s > 512 || c > 1) {
  259. genlib_data_resize((t_genlib_data *)self, 512, 1);
  260. } else {
  261. // if this fails, then Max is kaput anyway...
  262. genlib_data_resize((t_genlib_data *)self, 4, 1);
  263. }
  264. return;
  265. }
  266. // fill with zeroes:
  267. set_zero64(replaced, s * c);
  268. // copy in old data:
  269. if (old) {
  270. // frames to copy:
  271. // clamped:
  272. copydim = olddim > s ? s : olddim;
  273. // use memcpy if channels haven't changed:
  274. if (c == oldchannels) {
  275. copysz = sizeof(t_sample) * copydim * c;
  276. //post("reset resize (same channels) %p %p, %d", self->info.data, old, copysz);
  277. memcpy(replaced, old, copysz);
  278. } else {
  279. // memcpy won't work if channels have changed,
  280. // because data is interleaved.
  281. // clamp channels copied:
  282. copychannels = oldchannels > c ? c : oldchannels;
  283. //post("reset resize (different channels) %p %p, %d %d", self->info.data, old, copydim, copychannels);
  284. for (i = 0; i < copydim; i++) {
  285. for (j = 0; j < copychannels; j++) {
  286. replaced[j + i * c] = old[j + i * oldchannels];
  287. }
  288. }
  289. }
  290. }
  291. // now update info:
  292. if (old == 0) {
  293. self->info.data = replaced;
  294. self->info.dim = s;
  295. self->info.channels = c;
  296. } else {
  297. // need to be careful; the audio thread may still be using it
  298. // since dsp_gen_data is preserved through edits
  299. // the order of resizing has to be carefully done
  300. // to prevent indexing out of bounds
  301. // (or maybe I'm being too paranoid here...)
  302. if (oldsz > sz) {
  303. // shrink size first
  304. if (s > olddim) {
  305. self->info.channels = c;
  306. self->info.dim = s;
  307. } else {
  308. self->info.dim = s;
  309. self->info.channels = c;
  310. }
  311. self->info.data = replaced;
  312. } else {
  313. // shrink size after
  314. self->info.data = replaced;
  315. if (s > olddim) {
  316. self->info.channels = c;
  317. self->info.dim = s;
  318. } else {
  319. self->info.dim = s;
  320. self->info.channels = c;
  321. }
  322. }
  323. // done with old:
  324. sysmem_freeptr(old);
  325. }
  326. }
  327. }
  328. void genlib_reset_complete(void *data)
  329. {
  330. }
  331. #if GENLIB_USE_JSON
  332. void genlib_build_json(CommonState *cself, json_value **jsonvalue, getparameter_method getmethod)
  333. {
  334. int i;
  335. *jsonvalue = json_object_new(0);
  336. for (i = 0; i < cself->numparams; i++) {
  337. t_param val;
  338. (getmethod)(cself, i, &val);
  339. json_object_push(*jsonvalue, cself->params[i].name, json_double_new(val));
  340. }
  341. }
  342. size_t genlib_getstatesize(CommonState *cself, getparameter_method getmethod)
  343. {
  344. size_t size;
  345. json_value *jsonvalue;
  346. genlib_build_json(cself, &jsonvalue, getmethod);
  347. size = json_measure(jsonvalue);
  348. json_builder_free(jsonvalue);
  349. return size;
  350. }
  351. short genlib_getstate(CommonState *cself, char *state, getparameter_method getmethod)
  352. {
  353. json_value *jsonvalue;
  354. genlib_build_json(cself, &jsonvalue, getmethod);
  355. json_serialize(state, jsonvalue);
  356. json_builder_free(jsonvalue);
  357. return 0;
  358. }
  359. static void *json_custom_alloc(size_t size, int zero, void *user_data)
  360. {
  361. return zero ? genlib_sysmem_newptrclear(size) : genlib_sysmem_newptr(size);
  362. }
  363. static void json_custom_free(void *ptr, void *user_data)
  364. {
  365. genlib_sysmem_freeptr(ptr);
  366. }
  367. short genlib_setstate(CommonState *cself, const char *state, setparameter_method setmethod)
  368. {
  369. json_settings settings;
  370. char error[256];
  371. memset(&settings, 0, sizeof(json_settings));
  372. settings.mem_alloc = &json_custom_alloc;
  373. settings.mem_free = &json_custom_free;
  374. json_value *value = json_parse_ex(&settings, state, strlen(state), error);
  375. if (value == NULL)
  376. return 1;
  377. if (value->type == json_object) {
  378. unsigned int i;
  379. for (i = 0; i < value->u.object.length; i++) {
  380. char *name = NULL;
  381. t_param val = 0;
  382. int j;
  383. if (value->u.object.values[i].value->type == json_double) {
  384. name = value->u.object.values[i].name;
  385. val = t_param(value->u.object.values[i].value->u.dbl);
  386. } else if (value->u.object.values[i].value->type == json_integer) {
  387. name = value->u.object.values[i].name;
  388. val = t_param(value->u.object.values[i].value->u.integer);
  389. }
  390. if (name) {
  391. for (j = 0; j < cself->numparams; j++) {
  392. if (!strcmp(cself->params[j].name, name)) {
  393. (setmethod)(cself, j, val, NULL);
  394. }
  395. }
  396. }
  397. }
  398. }
  399. json_value_free_ex(&settings, value);
  400. return 0;
  401. }
  402. #endif // GENLIB_USE_JSON