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.

361 lines
8.5KB

  1. /*******************************************************************************************************************
  2. Copyright (c) 2012 Cycling '74
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. and associated documentation files (the "Software"), to deal in the Software without restriction,
  5. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
  7. subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all copies
  9. or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  11. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  12. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  13. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  14. OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. *******************************************************************************************************************/
  16. #include "genlib.h"
  17. #include "genlib_exportfunctions.h"
  18. #include "stdlib.h"
  19. #include "stdio.h"
  20. #include "string.h"
  21. #include <cmath>
  22. #if ! DISTRHO_OS_MAC
  23. # include <malloc.h>
  24. # if DISTRHO_OS_WINDOWS
  25. # define malloc_size _msize
  26. # else
  27. # define malloc_size malloc_usable_size
  28. # endif
  29. #endif
  30. // DATA_MAXIMUM_ELEMENTS * 8 bytes = 256 mb limit
  31. #define DATA_MAXIMUM_ELEMENTS (33554432)
  32. //////////// export_genlib.cpp ////////////
  33. // export version
  34. void my_memset(void *p, int c, long size);
  35. void my_memcpy(void *dst, const void *src, long size);
  36. t_ptr sysmem_newptr(t_ptr_size size)
  37. {
  38. return (t_ptr)malloc(size);
  39. }
  40. t_ptr sysmem_newptrclear(t_ptr_size size)
  41. {
  42. t_ptr p = (t_ptr)malloc(size);
  43. if (p)
  44. my_memset(p, 0, size);
  45. return p;
  46. }
  47. t_ptr sysmem_resizeptr(void *ptr, t_ptr_size newsize)
  48. {
  49. return (t_ptr)realloc(ptr, newsize);
  50. }
  51. t_ptr sysmem_resizeptrclear(void *ptr, t_ptr_size newsize)
  52. {
  53. t_ptr_size oldsize = malloc_size(ptr);
  54. t_ptr p = (t_ptr)realloc(ptr, newsize);
  55. if (p) {
  56. if (newsize > oldsize)
  57. my_memset((char *)p + oldsize, 0, newsize - oldsize);
  58. }
  59. return p;
  60. }
  61. t_ptr_size sysmem_ptrsize(void *ptr)
  62. {
  63. return malloc_size(ptr);
  64. }
  65. void sysmem_freeptr(void *ptr)
  66. {
  67. free(ptr);
  68. }
  69. void sysmem_copyptr(const void *src, void *dst, t_ptr_size bytes)
  70. {
  71. my_memcpy(dst, src, bytes);
  72. }
  73. void my_memset(void *p, int c, long size)
  74. {
  75. char *p2 = (char *)p;
  76. int i;
  77. for (i = 0; i < size; i++, p2++)
  78. *p2 = c;
  79. }
  80. void my_memcpy(void *dst, const void *src, long size)
  81. {
  82. char *s2 = (char *)src;
  83. char *d2 = (char *)dst;
  84. int i;
  85. for (i = 0; i < size; i++, s2++, d2++)
  86. *d2 = *s2;
  87. }
  88. void set_zero64(t_sample *memory, long size)
  89. {
  90. long i;
  91. for (i = 0; i < size; i++, memory++) {
  92. *memory = 0.;
  93. }
  94. }
  95. void genlib_report_error(const char *s)
  96. {
  97. fprintf(stderr, "%s\n", s);
  98. }
  99. void genlib_report_message(const char *s)
  100. {
  101. fprintf(stdout, "%s\n", s);
  102. }
  103. unsigned long systime_ticks(void)
  104. {
  105. return 0; // Gen code can deal with this
  106. }
  107. void * genlib_obtain_reference_from_string(const char * name) {
  108. return 0; // to be implemented
  109. }
  110. // the rest is stuff to isolate gensym, attrs, atoms, buffers etc.
  111. t_genlib_buffer * genlib_obtain_buffer_from_reference(void *ref)
  112. {
  113. return 0; // to be implemented
  114. }
  115. t_genlib_err genlib_buffer_edit_begin(t_genlib_buffer *b)
  116. {
  117. return 0; // to be implemented
  118. }
  119. t_genlib_err genlib_buffer_edit_end(t_genlib_buffer *b, long valid)
  120. {
  121. return 0; // to be implemented
  122. }
  123. t_genlib_err genlib_buffer_getinfo(t_genlib_buffer *b, t_genlib_buffer_info *info)
  124. {
  125. return 0; // to be implemented
  126. }
  127. char *genlib_reference_getname(void *ref)
  128. {
  129. return 0; // to be implemented
  130. }
  131. void genlib_buffer_dirty(t_genlib_buffer *b)
  132. {
  133. // to be implemented
  134. }
  135. t_genlib_err genlib_buffer_perform_begin(t_genlib_buffer *b)
  136. {
  137. return 0; // to be implemented
  138. }
  139. void genlib_buffer_perform_end(t_genlib_buffer *b)
  140. {
  141. // to be implemented
  142. }
  143. t_sample gen_msp_pow(t_sample value, t_sample power)
  144. {
  145. return powf(value, power);
  146. }
  147. void genlib_data_setbuffer(t_genlib_data *b, void *ref) {
  148. genlib_report_error("not supported for export targets\n");
  149. }
  150. typedef struct {
  151. t_genlib_data_info info;
  152. t_sample cursor; // used by Delay
  153. //t_symbol * name;
  154. } t_dsp_gen_data;
  155. t_genlib_data * genlib_obtain_data_from_reference(void *ref)
  156. {
  157. t_dsp_gen_data * self = (t_dsp_gen_data *)malloc(sizeof(t_dsp_gen_data));
  158. self->info.dim = 0;
  159. self->info.channels = 0;
  160. self->info.data = 0;
  161. self->cursor = 0;
  162. return (t_genlib_data *)self;
  163. }
  164. t_genlib_err genlib_data_getinfo(t_genlib_data *b, t_genlib_data_info *info) {
  165. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  166. info->dim = self->info.dim;
  167. info->channels = self->info.channels;
  168. info->data = self->info.data;
  169. return GENLIB_ERR_NONE;
  170. }
  171. void genlib_data_release(t_genlib_data *b) {
  172. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  173. if (self->info.data) {
  174. genlib_sysmem_freeptr(self->info.data);
  175. self->info.data = 0;
  176. }
  177. }
  178. long genlib_data_getcursor(t_genlib_data *b) {
  179. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  180. return self->cursor;
  181. }
  182. void genlib_data_setcursor(t_genlib_data *b, long cursor) {
  183. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  184. self->cursor = cursor;
  185. }
  186. void genlib_data_resize(t_genlib_data *b, long s, long c) {
  187. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  188. size_t sz, oldsz, copysz;
  189. t_sample * old = 0;
  190. t_sample * replaced = 0;
  191. int i, j, copydim, copychannels, olddim, oldchannels;
  192. //printf("data resize %d %d\n", s, c);
  193. // cache old for copying:
  194. old = self->info.data;
  195. olddim = self->info.dim;
  196. oldchannels = self->info.channels;
  197. // limit [data] size:
  198. if (s * c > DATA_MAXIMUM_ELEMENTS) {
  199. s = DATA_MAXIMUM_ELEMENTS/c;
  200. genlib_report_message("warning: constraining [data] to < 256MB");
  201. }
  202. // bytes required:
  203. sz = sizeof(t_sample) * s * c;
  204. oldsz = sizeof(t_sample) * olddim * oldchannels;
  205. if (old && sz == oldsz) {
  206. // no need to re-allocate, just resize
  207. // careful, audio thread may still be using it:
  208. if (s > olddim) {
  209. self->info.channels = c;
  210. self->info.dim = s;
  211. } else {
  212. self->info.dim = s;
  213. self->info.channels = c;
  214. }
  215. set_zero64(self->info.data, s * c);
  216. return;
  217. } else {
  218. // allocate new:
  219. replaced = (t_sample *)sysmem_newptr(sz);
  220. // check allocation:
  221. if (replaced == 0) {
  222. genlib_report_error("allocating [data]: out of memory");
  223. // try to reallocate with a default/minimal size instead:
  224. if (s > 512 || c > 1) {
  225. genlib_data_resize((t_genlib_data *)self, 512, 1);
  226. } else {
  227. // if this fails, then Max is kaput anyway...
  228. genlib_data_resize((t_genlib_data *)self, 4, 1);
  229. }
  230. return;
  231. }
  232. // fill with zeroes:
  233. set_zero64(replaced, s * c);
  234. // copy in old data:
  235. if (old) {
  236. // frames to copy:
  237. // clamped:
  238. copydim = olddim > s ? s : olddim;
  239. // use memcpy if channels haven't changed:
  240. if (c == oldchannels) {
  241. copysz = sizeof(t_sample) * copydim * c;
  242. //post("reset resize (same channels) %p %p, %d", self->info.data, old, copysz);
  243. memcpy(replaced, old, copysz);
  244. } else {
  245. // memcpy won't work if channels have changed,
  246. // because data is interleaved.
  247. // clamp channels copied:
  248. copychannels = oldchannels > c ? c : oldchannels;
  249. //post("reset resize (different channels) %p %p, %d %d", self->info.data, old, copydim, copychannels);
  250. for (i = 0; i<copydim; i++) {
  251. for (j = 0; j<copychannels; j++) {
  252. replaced[j + i*c] = old[j + i*oldchannels];
  253. }
  254. }
  255. }
  256. }
  257. // now update info:
  258. if (old == 0) {
  259. self->info.data = replaced;
  260. self->info.dim = s;
  261. self->info.channels = c;
  262. } else {
  263. // need to be careful; the audio thread may still be using it
  264. // since dsp_gen_data is preserved through edits
  265. // the order of resizing has to be carefully done
  266. // to prevent indexing out of bounds
  267. // (or maybe I'm being too paranoid here...)
  268. if (oldsz > sz) {
  269. // shrink size first
  270. if (s > olddim) {
  271. self->info.channels = c;
  272. self->info.dim = s;
  273. } else {
  274. self->info.dim = s;
  275. self->info.channels = c;
  276. }
  277. self->info.data = replaced;
  278. } else {
  279. // shrink size after
  280. self->info.data = replaced;
  281. if (s > olddim) {
  282. self->info.channels = c;
  283. self->info.dim = s;
  284. } else {
  285. self->info.dim = s;
  286. self->info.channels = c;
  287. }
  288. }
  289. // done with old:
  290. sysmem_freeptr(old);
  291. }
  292. }
  293. }
  294. void genlib_reset_complete(void *data) {}