Audio plugin host https://kx.studio/carla
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.

port-sugar.h 15KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) 2013 Mark McCurry
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #ifndef RTOSC_PORT_SUGAR
  25. #define RTOSC_PORT_SUGAR
  26. //Hack to workaround old incomplete decltype implementations
  27. #ifdef __GNUC__
  28. #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ <= 7)
  29. template<typename T>
  30. struct rtosc_hack_decltype_t
  31. {
  32. typedef T type;
  33. };
  34. #define decltype(expr) rtosc_hack_decltype_t<decltype(expr)>::type
  35. #endif
  36. #endif
  37. //General macro utilities
  38. #define STRINGIFY2(a) #a
  39. #define STRINGIFY(a) STRINGIFY2(a)
  40. //Helper for documenting varargs
  41. #define IMPL(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,N, ...) N
  42. #define LAST_IMP(...) IMPL(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0,0,0)
  43. #define DOC_IMP9(a,b,c,d,e,f,g,h,i) a b c d e f g h rDoc(i)
  44. #define DOC_IMP8(a,b,c,d,e,f,g,h) a b c d e f g rDoc(h)
  45. #define DOC_IMP7(a,b,c,d,e,f,g) a b c d e f rDoc(g)
  46. #define DOC_IMP6(a,b,c,d,e,f) a b c d e rDoc(f)
  47. #define DOC_IMP5(a,b,c,d,e) a b c d rDoc(e)
  48. #define DOC_IMP4(a,b,c,d) a b c rDoc(d)
  49. #define DOC_IMP3(a,b,c) a b rDoc(c)
  50. #define DOC_IMP2(a,b) a rDoc(b)
  51. #define DOC_IMP1(a) rDoc(a)
  52. #define DOC_IMP0() YOU_MUST_DOCUMENT_YOUR_PORTS
  53. #define DOC_IMP(count, ...) DOC_IMP ##count(__VA_ARGS__)
  54. #define DOC_I(count, ...) DOC_IMP(count,__VA_ARGS__)
  55. #define DOC(...) DOC_I(LAST_IMP(__VA_ARGS__), __VA_ARGS__)
  56. //XXX Currently unused macro
  57. #define MAC_EACH_0(mac, x, ...) INSUFFICIENT_ARGUMENTS_PROVIDED_TO_MAC_EACH
  58. #define MAC_EACH_1(mac, x, ...) mac(x)
  59. #define MAC_EACH_2(mac, x, ...) mac(x) MAC_EACH_1(mac, __VA_ARGS__)
  60. #define MAC_EACH_3(mac, x, ...) mac(x) MAC_EACH_2(mac, __VA_ARGS__)
  61. #define MAC_EACH_4(mac, x, ...) mac(x) MAC_EACH_3(mac, __VA_ARGS__)
  62. #define MAC_EACH_5(mac, x, ...) mac(x) MAC_EACH_4(mac, __VA_ARGS__)
  63. #define MAC_EACH_6(mac, x, ...) mac(x) MAC_EACH_5(mac, __VA_ARGS__)
  64. #define MAC_EACH_7(mac, x, ...) mac(x) MAC_EACH_6(mac, __VA_ARGS__)
  65. #define MAC_EACH_8(mac, x, ...) mac(x) MAC_EACH_7(mac, __VA_ARGS__)
  66. #define MAC_EACH_9(mac, x, ...) mac(x) MAC_EACH_8(mac, __VA_ARGS__)
  67. #define MAC_EACH_IMP(mac, count, ...) MAC_EACH_ ##count(mac,__VA_ARGS__)
  68. #define MAC_EACH_I(mac, count, ...) MAC_EACH_IMP(mac, count, __VA_ARGS__)
  69. #define MAC_EACH(mac, ...) MAC_EACH_I(mac, LAST_IMP(__VA_ARGS__), __VA_ARGS__)
  70. // 1 2 3 4 5 6 7 8 910111213141516
  71. #define OPTIONS_IMP16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
  72. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  73. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)rOpt(12,m)rOpt(13,n)\
  74. rOpt(14,o)rOpt(15,p)
  75. #define OPTIONS_IMP15(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) \
  76. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  77. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)rOpt(12,m)rOpt(13,n)\
  78. rOpt(14,o)
  79. #define OPTIONS_IMP14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) \
  80. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  81. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)rOpt(12,m)rOpt(13,n)
  82. #define OPTIONS_IMP13(a,b,c,d,e,f,g,h,i,j,k,l,m) \
  83. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  84. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)rOpt(12,m)rOpt(13,n)
  85. #define OPTIONS_IMP12(a,b,c,d,e,f,g,h,i,j,k,l) \
  86. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  87. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)
  88. #define OPTIONS_IMP11(a,b,c,d,e,f,g,h,i,j,k) \
  89. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  90. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)
  91. #define OPTIONS_IMP10(a,b,c,d,e,f,g,h,i,j) \
  92. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  93. rOpt(7,h) rOpt(8,i) rOpt(9,j)
  94. #define OPTIONS_IMP9(a,b,c,d,e,f,g,h,i) \
  95. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  96. rOpt(7,h) rOpt(8,i)
  97. #define OPTIONS_IMP8(a,b,c,d,e,f,g,h) \
  98. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  99. rOpt(7,h)
  100. #define OPTIONS_IMP7(a,b,c,d,e,f,g) \
  101. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g)
  102. #define OPTIONS_IMP6(a,b,c,d,e,f) \
  103. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f)
  104. #define OPTIONS_IMP5(a,b,c,d,e) \
  105. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e)
  106. #define OPTIONS_IMP4(a,b,c,d) \
  107. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d)
  108. #define OPTIONS_IMP3(a,b,c) \
  109. rOpt(0,a) rOpt(1,b) rOpt(2,c)
  110. #define OPTIONS_IMP2(a,b) \
  111. rOpt(0,a) rOpt(1,b)
  112. #define OPTIONS_IMP1(a) \
  113. rOpt(0,a)
  114. #define OPTIONS_IMP0() YOU_MUST_PROVIDE_OPTIONS
  115. #define OPTIONS_IMP(count, ...) OPTIONS_IMP ##count(__VA_ARGS__)
  116. #define OPTIONS_I(count, ...) OPTIONS_IMP(count, __VA_ARGS__)
  117. #define OPTIONS(...) OPTIONS_I(LAST_IMP(__VA_ARGS__), __VA_ARGS__)
  118. //Additional Change Callback (after parameters have been changed)
  119. //This can be used to queue up interpolation or parameter regen
  120. #define rChangeCb
  121. //Normal parameters
  122. #define rParam(name, ...) \
  123. {STRINGIFY(name) "::c", rProp(parameter) rMap(min, 0) rMap(max, 127) DOC(__VA_ARGS__), NULL, rParamCb(name)}
  124. #define rParamF(name, ...) \
  125. {STRINGIFY(name) "::f", rProp(parameter) DOC(__VA_ARGS__), NULL, rParamFCb(name)}
  126. #define rParamI(name, ...) \
  127. {STRINGIFY(name) "::i", rProp(parameter) DOC(__VA_ARGS__), NULL, rParamICb(name)}
  128. #define rToggle(name, ...) \
  129. {STRINGIFY(name) "::T:F",rProp(parameter) DOC(__VA_ARGS__), NULL, rToggleCb(name)}
  130. #define rOption(name, ...) \
  131. {STRINGIFY(name) "::i:c",rProp(parameter) rProp(enumerated) DOC(__VA_ARGS__), NULL, rOptionCb(name)}
  132. //Array operators
  133. #define rArrayF(name, length, ...) \
  134. {STRINGIFY(name) "#" STRINGIFY(length) "::f", rProp(parameter) DOC(__VA_ARGS__), NULL, rArrayFCb(name)}
  135. #define rArray(name, length, ...) \
  136. {STRINGIFY(name) "#" STRINGIFY(length) "::c", rProp(parameter) DOC(__VA_ARGS__), NULL, rArrayCb(name)}
  137. #define rArrayT(name, length, ...) \
  138. {STRINGIFY(name) "#" STRINGIFY(length) "::T:F", rProp(parameter) DOC(__VA_ARGS__), NULL, rArrayTCb(name)}
  139. #define rArrayI(name, length, ...) \
  140. {STRINGIFY(name) "#" STRINGIFY(length) "::i", rProp(parameter) DOC(__VA_ARGS__), NULL, rArrayICb(name)}
  141. //Method callback Actions
  142. #define rAction(name, ...) \
  143. {STRINGIFY(name) ":", DOC(__VA_ARGS__), NULL, rActionCb(name)}
  144. #define rActioni(name, ...) \
  145. {STRINGIFY(name) ":i", DOC(__VA_ARGS__), NULL, rActioniCb(name)}
  146. //Alias operators
  147. #define rParams(name, length, ...) \
  148. rArray(name, length, __VA_ARGS__), \
  149. {STRINGIFY(name) ":", rProp(alias) rDoc("get all data from aliased array"), NULL, rParamsCb(name, length)}
  150. template<class T> constexpr T spice(T*t) {return *t;}
  151. //Recursion [two ports in one for pointer manipulation]
  152. #define rRecur(name, ...) \
  153. {STRINGIFY(name) "/", DOC(__VA_ARGS__), &decltype(rObject::name)::ports, rRecurCb(name)}, \
  154. {STRINGIFY(name) ":", rProp(internal) rDoc("get obj pointer"), NULL, rRecurPtrCb(name)}
  155. #define rRecurp(name, ...) \
  156. {STRINGIFY(name) "/", DOC(__VA_ARGS__), \
  157. &decltype(spice(rObject::name))::ports, \
  158. rRecurpCb(name)}
  159. #define rRecurs(name, length, ...) \
  160. {STRINGIFY(name) "#" STRINGIFY(length)"/", DOC(__VA_ARGS__), \
  161. &decltype(spice(&rObject::name[0]))::ports, \
  162. rRecursCb(name, length)}
  163. //Technically this is a pointer pointer method...
  164. #define rRecursp(name, length, ...) \
  165. {STRINGIFY(name)"#" STRINGIFY(length) "/", DOC(__VA_ARGS__), \
  166. &decltype(spice(rObject::name[0]))::ports, \
  167. rRecurspCb(name)}
  168. //{STRINGIFY(name) ":", rProp(internal), NULL, rRecurPtrCb(name)}
  169. //Misc
  170. #define rDummy(name, ...) {STRINIFY(name), rProp(dummy), NULL, [](msg_t, rtosc::RtData &){}}
  171. #define rString(name, len, ...) \
  172. {STRINGIFY(name) "::s", rMap(length, len) rProp(parameter) DOC(__VA_ARGS__), NULL, rStringCb(name,len)}
  173. //General property operators
  174. #define rMap(name, value) ":" STRINGIFY(name) "\0=" STRINGIFY(value) "\0"
  175. #define rProp(name) ":" STRINGIFY(name) "\0"
  176. //Scaling property
  177. #define rLinear(min_, max_) rMap(min, min_) rMap(max, max_) rMap(scale, linear)
  178. #define rLog(min_, max_) rMap(min, min_) rMap(max, max_) rMap(scale, logarithmic)
  179. //Special values
  180. #define rSpecial(doc) ":special\0" STRINGIFY(doc) "\0"
  181. #define rCentered ":centered\0"
  182. //Misc properties
  183. #define rDoc(doc) ":documentation\0=" doc "\0"
  184. #define rOpt(numeric,symbolic) rMap(map numeric, symbolic)
  185. #define rOptions(...) OPTIONS(__VA_ARGS__)
  186. //Callback Implementations
  187. #define rBOIL_BEGIN [](const char *msg, rtosc::RtData &data) { \
  188. (void) msg; (void) data; \
  189. rObject *obj = (rObject*) data.obj;(void) obj; \
  190. const char *args = rtosc_argument_string(msg); (void) args;\
  191. const char *loc = data.loc; (void) loc;\
  192. auto prop = data.port->meta(); (void) prop;
  193. #define rBOIL_END }
  194. #define rLIMIT(var, convert) \
  195. if(prop["min"] && var < (decltype(var))convert(prop["min"])) \
  196. var = convert(prop["min"]);\
  197. if(prop["max"] && var > (decltype(var))convert(prop["max"])) \
  198. var = convert(prop["max"]);
  199. #define rTYPE(n) decltype(obj->n)
  200. #define rAPPLY(n,t) if(obj->n != var) data.reply("undo_change", "s" #t #t, data.loc, obj->n, var); obj->n = var;
  201. #define rParamCb(name) rBOIL_BEGIN \
  202. if(!strcmp("", args)) {\
  203. data.reply(loc, "c", obj->name); \
  204. } else { \
  205. rTYPE(name) var = rtosc_argument(msg, 0).i; \
  206. rLIMIT(var, atoi) \
  207. rAPPLY(name, c) \
  208. data.broadcast(loc, "c", obj->name);\
  209. rChangeCb \
  210. } rBOIL_END
  211. #define rParamFCb(name) rBOIL_BEGIN \
  212. if(!strcmp("", args)) {\
  213. data.reply(loc, "f", obj->name); \
  214. } else { \
  215. rTYPE(name) var = rtosc_argument(msg, 0).f; \
  216. rLIMIT(var, atof) \
  217. rAPPLY(name, f) \
  218. data.broadcast(loc, "f", obj->name);\
  219. rChangeCb \
  220. } rBOIL_END
  221. #define rParamICb(name) rBOIL_BEGIN \
  222. if(!strcmp("", args)) {\
  223. data.reply(loc, "i", obj->name); \
  224. } else { \
  225. rTYPE(name) var = rtosc_argument(msg, 0).i; \
  226. rLIMIT(var, atoi) \
  227. rAPPLY(name, i) \
  228. data.broadcast(loc, "i", obj->name);\
  229. rChangeCb \
  230. } rBOIL_END
  231. //TODO finish me (include string mapper action?)
  232. #define rOptionCb(name) rBOIL_BEGIN \
  233. if(!strcmp("", args)) {\
  234. data.reply(loc, "i", obj->name); \
  235. } else { \
  236. rTYPE(name) var = rtosc_argument(msg, 0).i; \
  237. rLIMIT(var, atoi) \
  238. rAPPLY(name, i) \
  239. data.broadcast(loc, rtosc_argument_string(msg), obj->name);\
  240. rChangeCb \
  241. } rBOIL_END
  242. #define rToggleCb(name) rBOIL_BEGIN \
  243. if(!strcmp("", args)) {\
  244. data.reply(loc, obj->name ? "T" : "F"); \
  245. } else { \
  246. if(obj->name != rtosc_argument(msg, 0).T) { \
  247. data.broadcast(loc, args);\
  248. obj->name = rtosc_argument(msg, 0).T; \
  249. rChangeCb \
  250. } \
  251. } rBOIL_END
  252. #define SNIP \
  253. while(*msg && *msg!='/') ++msg; \
  254. msg = *msg ? msg+1 : msg;
  255. #define rRecurCb(name) rBOIL_BEGIN \
  256. data.obj = &obj->name; \
  257. SNIP \
  258. decltype(obj->name)::ports.dispatch(msg, data); \
  259. rBOIL_END
  260. #define rRecurPtrCb(name) rBOIL_BEGIN \
  261. void *ptr = &obj->name; \
  262. data.reply(loc, "b", sizeof(void*), &ptr); \
  263. rBOIL_END
  264. #define rRecurpCb(name) rBOIL_BEGIN \
  265. if(obj->name == NULL) return; \
  266. data.obj = obj->name; \
  267. SNIP \
  268. decltype(spice(rObject::name))::ports.dispatch(msg, data); \
  269. rBOIL_END
  270. #define rRecursCb(name, length) rBOILS_BEGIN \
  271. data.obj = &obj->name[idx]; \
  272. SNIP \
  273. decltype(spice(rObject::name))::ports.dispatch(msg, data); \
  274. rBOILS_END
  275. #define rRecurspCb(name) rBOILS_BEGIN \
  276. data.obj = obj->name[idx]; \
  277. SNIP \
  278. decltype(spice(rObject::name[0]))::ports.dispatch(msg, data); \
  279. rBOILS_END
  280. #define rActionCb(name) rBOIL_BEGIN obj->name(); rBOIL_END
  281. #define rActioniCb(name) rBOIL_BEGIN \
  282. obj->name(rtosc_argument(msg,0).i); rBOIL_END
  283. //Array ops
  284. #define rBOILS_BEGIN rBOIL_BEGIN \
  285. const char *mm = msg; \
  286. while(*mm && !isdigit(*mm)) ++mm; \
  287. unsigned idx = atoi(mm);
  288. #define rBOILS_END rBOIL_END
  289. #define rArrayCb(name) rBOILS_BEGIN \
  290. if(!strcmp("", args)) {\
  291. data.reply(loc, "c", obj->name[idx]); \
  292. } else { \
  293. char var = rtosc_argument(msg, 0).i; \
  294. rLIMIT(var, atoi) \
  295. rAPPLY(name[idx], c) \
  296. data.broadcast(loc, "c", obj->name[idx]);\
  297. rChangeCb \
  298. } rBOILS_END
  299. #define rArrayFCb(name) rBOILS_BEGIN \
  300. if(!strcmp("", args)) {\
  301. data.reply(loc, "f", obj->name[idx]); \
  302. } else { \
  303. float var = rtosc_argument(msg, 0).f; \
  304. rLIMIT(var, atof) \
  305. rAPPLY(name[idx], f) \
  306. data.broadcast(loc, "f", obj->name[idx]);\
  307. } rBOILS_END
  308. #define rArrayTCb(name) rBOILS_BEGIN \
  309. if(!strcmp("", args)) {\
  310. data.reply(loc, obj->name[idx] ? "T" : "F"); \
  311. } else { \
  312. if(obj->name[idx] != rtosc_argument(msg, 0).T) { \
  313. data.broadcast(loc, args);\
  314. rChangeCb \
  315. } \
  316. obj->name[idx] = rtosc_argument(msg, 0).T; \
  317. } rBOILS_END
  318. #define rArrayICb(name) rBOILS_BEGIN \
  319. if(!strcmp("", args)) {\
  320. data.reply(loc, "i", obj->name[idx]); \
  321. } else { \
  322. char var = rtosc_argument(msg, 0).i; \
  323. rLIMIT(var, atoi) \
  324. rAPPLY(name[idx], i) \
  325. data.broadcast(loc, "i", obj->name[idx]);\
  326. rChangeCb \
  327. } rBOILS_END
  328. #define rParamsCb(name, length) rBOIL_BEGIN \
  329. data.reply(loc, "b", length, obj->name); rBOIL_END
  330. #define rStringCb(name, length) rBOIL_BEGIN \
  331. if(!strcmp("", args)) {\
  332. data.reply(loc, "s", obj->name); \
  333. } else { \
  334. strncpy(obj->name, rtosc_argument(msg, 0).s, length); \
  335. data.broadcast(loc, "s", obj->name);\
  336. rChangeCb \
  337. } rBOIL_END
  338. #endif