Collection of DPF-based plugins for packaging
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.

355 lines
8.4KB

  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. #ifdef pow
  22. #undef pow
  23. #endif
  24. #include <cmath>
  25. #include <malloc.h>
  26. #define malloc_size malloc_usable_size
  27. // DATA_MAXIMUM_ELEMENTS * 8 bytes = 256 mb limit
  28. #define DATA_MAXIMUM_ELEMENTS (33554432)
  29. //////////// export_genlib.cpp ////////////
  30. // export version
  31. void my_memset(void *p, int c, long size);
  32. void my_memcpy(void *dst, const void *src, long size);
  33. t_ptr sysmem_newptr(t_ptr_size size)
  34. {
  35. return (t_ptr)malloc(size);
  36. }
  37. t_ptr sysmem_newptrclear(t_ptr_size size)
  38. {
  39. t_ptr p = (t_ptr)malloc(size);
  40. if (p)
  41. my_memset(p, 0, size);
  42. return p;
  43. }
  44. t_ptr sysmem_resizeptr(void *ptr, t_ptr_size newsize)
  45. {
  46. return (t_ptr)realloc(ptr, newsize);
  47. }
  48. t_ptr sysmem_resizeptrclear(void *ptr, t_ptr_size newsize)
  49. {
  50. long oldsize = malloc_size(ptr);
  51. t_ptr p = (t_ptr)realloc(ptr, newsize);
  52. if (p) {
  53. if (newsize > oldsize)
  54. my_memset((char *)p + oldsize, 0, newsize - oldsize);
  55. }
  56. return p;
  57. }
  58. t_ptr_size sysmem_ptrsize(void *ptr)
  59. {
  60. return malloc_size(ptr);
  61. }
  62. void sysmem_freeptr(void *ptr)
  63. {
  64. free(ptr);
  65. }
  66. void sysmem_copyptr(const void *src, void *dst, t_ptr_size bytes)
  67. {
  68. my_memcpy(dst, src, bytes);
  69. }
  70. void my_memset(void *p, int c, long size)
  71. {
  72. char *p2 = (char *)p;
  73. int i;
  74. for (i = 0; i < size; i++, p2++)
  75. *p2 = c;
  76. }
  77. void my_memcpy(void *dst, const void *src, long size)
  78. {
  79. char *s2 = (char *)src;
  80. char *d2 = (char *)dst;
  81. int i;
  82. for (i = 0; i < size; i++, s2++, d2++)
  83. *d2 = *s2;
  84. }
  85. void set_zero64(t_sample *memory, long size)
  86. {
  87. long i;
  88. for (i = 0; i < size; i++, memory++) {
  89. *memory = 0.;
  90. }
  91. }
  92. void genlib_report_error(const char *s)
  93. {
  94. fprintf(stderr, "%s\n", s);
  95. }
  96. void genlib_report_message(const char *s)
  97. {
  98. fprintf(stdout, "%s\n", s);
  99. }
  100. unsigned long systime_ticks(void)
  101. {
  102. return 0; // Gen code can deal with this
  103. }
  104. void * genlib_obtain_reference_from_string(const char * name) {
  105. return 0; // to be implemented
  106. }
  107. // the rest is stuff to isolate gensym, attrs, atoms, buffers etc.
  108. t_genlib_buffer * genlib_obtain_buffer_from_reference(void *ref)
  109. {
  110. return 0; // to be implemented
  111. }
  112. t_genlib_err genlib_buffer_edit_begin(t_genlib_buffer *b)
  113. {
  114. return 0; // to be implemented
  115. }
  116. t_genlib_err genlib_buffer_edit_end(t_genlib_buffer *b, long valid)
  117. {
  118. return 0; // to be implemented
  119. }
  120. t_genlib_err genlib_buffer_getinfo(t_genlib_buffer *b, t_genlib_buffer_info *info)
  121. {
  122. return 0; // to be implemented
  123. }
  124. char *genlib_reference_getname(void *ref)
  125. {
  126. return 0; // to be implemented
  127. }
  128. void genlib_buffer_dirty(t_genlib_buffer *b)
  129. {
  130. // to be implemented
  131. }
  132. t_genlib_err genlib_buffer_perform_begin(t_genlib_buffer *b)
  133. {
  134. return 0; // to be implemented
  135. }
  136. void genlib_buffer_perform_end(t_genlib_buffer *b)
  137. {
  138. // to be implemented
  139. }
  140. t_sample gen_msp_pow(t_sample value, t_sample power)
  141. {
  142. return pow(value, power);
  143. }
  144. void genlib_data_setbuffer(t_genlib_data *b, void *ref) {
  145. genlib_report_error("not supported for export targets\n");
  146. }
  147. typedef struct {
  148. t_genlib_data_info info;
  149. t_sample cursor; // used by Delay
  150. //t_symbol * name;
  151. } t_dsp_gen_data;
  152. t_genlib_data * genlib_obtain_data_from_reference(void *ref)
  153. {
  154. t_dsp_gen_data * self = (t_dsp_gen_data *)malloc(sizeof(t_dsp_gen_data));
  155. self->info.dim = 0;
  156. self->info.channels = 0;
  157. self->info.data = 0;
  158. self->cursor = 0;
  159. return (t_genlib_data *)self;
  160. }
  161. t_genlib_err genlib_data_getinfo(t_genlib_data *b, t_genlib_data_info *info) {
  162. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  163. info->dim = self->info.dim;
  164. info->channels = self->info.channels;
  165. info->data = self->info.data;
  166. return GENLIB_ERR_NONE;
  167. }
  168. void genlib_data_release(t_genlib_data *b) {
  169. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  170. if (self->info.data) {
  171. genlib_sysmem_freeptr(self->info.data);
  172. self->info.data = 0;
  173. }
  174. }
  175. long genlib_data_getcursor(t_genlib_data *b) {
  176. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  177. return self->cursor;
  178. }
  179. void genlib_data_setcursor(t_genlib_data *b, long cursor) {
  180. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  181. self->cursor = cursor;
  182. }
  183. void genlib_data_resize(t_genlib_data *b, long s, long c) {
  184. t_dsp_gen_data * self = (t_dsp_gen_data *)b;
  185. size_t sz, oldsz, copysz;
  186. t_sample * old = 0;
  187. t_sample * replaced = 0;
  188. int i, j, copydim, copychannels, olddim, oldchannels;
  189. //printf("data resize %d %d\n", s, c);
  190. // cache old for copying:
  191. old = self->info.data;
  192. olddim = self->info.dim;
  193. oldchannels = self->info.channels;
  194. // limit [data] size:
  195. if (s * c > DATA_MAXIMUM_ELEMENTS) {
  196. s = DATA_MAXIMUM_ELEMENTS/c;
  197. genlib_report_message("warning: constraining [data] to < 256MB");
  198. }
  199. // bytes required:
  200. sz = sizeof(t_sample) * s * c;
  201. oldsz = sizeof(t_sample) * olddim * oldchannels;
  202. if (old && sz == oldsz) {
  203. // no need to re-allocate, just resize
  204. // careful, audio thread may still be using it:
  205. if (s > olddim) {
  206. self->info.channels = c;
  207. self->info.dim = s;
  208. } else {
  209. self->info.dim = s;
  210. self->info.channels = c;
  211. }
  212. set_zero64(self->info.data, s * c);
  213. return;
  214. } else {
  215. // allocate new:
  216. replaced = (t_sample *)sysmem_newptr(sz);
  217. // check allocation:
  218. if (replaced == 0) {
  219. genlib_report_error("allocating [data]: out of memory");
  220. // try to reallocate with a default/minimal size instead:
  221. if (s > 512 || c > 1) {
  222. genlib_data_resize((t_genlib_data *)self, 512, 1);
  223. } else {
  224. // if this fails, then Max is kaput anyway...
  225. genlib_data_resize((t_genlib_data *)self, 4, 1);
  226. }
  227. return;
  228. }
  229. // fill with zeroes:
  230. set_zero64(replaced, s * c);
  231. // copy in old data:
  232. if (old) {
  233. // frames to copy:
  234. // clamped:
  235. copydim = olddim > s ? s : olddim;
  236. // use memcpy if channels haven't changed:
  237. if (c == oldchannels) {
  238. copysz = sizeof(t_sample) * copydim * c;
  239. //post("reset resize (same channels) %p %p, %d", self->info.data, old, copysz);
  240. memcpy(replaced, old, copysz);
  241. } else {
  242. // memcpy won't work if channels have changed,
  243. // because data is interleaved.
  244. // clamp channels copied:
  245. copychannels = oldchannels > c ? c : oldchannels;
  246. //post("reset resize (different channels) %p %p, %d %d", self->info.data, old, copydim, copychannels);
  247. for (i = 0; i<copydim; i++) {
  248. for (j = 0; j<copychannels; j++) {
  249. replaced[j + i*c] = old[j + i*oldchannels];
  250. }
  251. }
  252. }
  253. }
  254. // now update info:
  255. if (old == 0) {
  256. self->info.data = replaced;
  257. self->info.dim = s;
  258. self->info.channels = c;
  259. } else {
  260. // need to be careful; the audio thread may still be using it
  261. // since dsp_gen_data is preserved through edits
  262. // the order of resizing has to be carefully done
  263. // to prevent indexing out of bounds
  264. // (or maybe I'm being too paranoid here...)
  265. if (oldsz > sz) {
  266. // shrink size first
  267. if (s > olddim) {
  268. self->info.channels = c;
  269. self->info.dim = s;
  270. } else {
  271. self->info.dim = s;
  272. self->info.channels = c;
  273. }
  274. self->info.data = replaced;
  275. } else {
  276. // shrink size after
  277. self->info.data = replaced;
  278. if (s > olddim) {
  279. self->info.channels = c;
  280. self->info.dim = s;
  281. } else {
  282. self->info.dim = s;
  283. self->info.channels = c;
  284. }
  285. }
  286. // done with old:
  287. sysmem_freeptr(old);
  288. }
  289. }
  290. }
  291. void genlib_reset_complete(void *data) {}