ysfx_api_reaper.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // Copyright 2021 Jean Pierre Cimalando
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #include "ysfx.hpp"
  18. #include "utility/sync_bitset.hpp"
  19. #include "ysfx_api_reaper.hpp"
  20. #include "ysfx_api_eel.hpp"
  21. #include "ysfx_eel_utils.hpp"
  22. #include "ysfx_utils.hpp"
  23. #include <cmath>
  24. #include <cstring>
  25. #include <cassert>
  26. #include "WDL/wdlstring.h"
  27. #define REAPER_GET_INTERFACE(opaque) ((opaque) ? (ysfx_t *)(opaque) : nullptr)
  28. static EEL_F *NSEEL_CGEN_CALL ysfx_api_spl(void *opaque, EEL_F *n_)
  29. {
  30. //NOTE: callable from @gfx thread
  31. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  32. int32_t n = ysfx_eel_round<int32_t>(*n_);
  33. if (n < 0 || n >= ysfx_max_channels) {
  34. fx->var.ret_temp = 0;
  35. return &fx->var.ret_temp;
  36. }
  37. return fx->var.spl[(uint32_t)n];
  38. }
  39. static EEL_F *NSEEL_CGEN_CALL ysfx_api_slider(void *opaque, EEL_F *n_)
  40. {
  41. //NOTE: callable from @gfx thread
  42. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  43. int32_t n = ysfx_eel_round<int32_t>(*n_);
  44. if (n < 1 || n > ysfx_max_sliders) {
  45. fx->var.ret_temp = 0;
  46. return &fx->var.ret_temp;
  47. }
  48. n -= 1;
  49. return fx->var.slider[(uint32_t)n];
  50. }
  51. static EEL_F NSEEL_CGEN_CALL ysfx_api_slider_next_chg(void *opaque, EEL_F *index_, EEL_F *val_)
  52. {
  53. //TODO frame-accurate slider changes
  54. (void)opaque;
  55. (void)index_;
  56. (void)val_;
  57. return -1;
  58. }
  59. static EEL_F NSEEL_CGEN_CALL ysfx_api_slider_automate(void *opaque, INT_PTR nparms, EEL_F **parms)
  60. {
  61. //NOTE: callable from @gfx thread
  62. if (nparms == 0) return 0;
  63. EEL_F *mask_or_slider_ = parms[0];
  64. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  65. uint32_t slider = ysfx_get_slider_of_var(fx, mask_or_slider_);
  66. uint8_t group;
  67. uint64_t mask;
  68. if (slider < ysfx_max_sliders) {
  69. group = ysfx_fetch_slider_group_index(slider);
  70. mask = ysfx_slider_mask(slider, group);
  71. } else {
  72. mask = ysfx_eel_round<uint64_t>(std::fabs(*mask_or_slider_));
  73. group = 0;
  74. }
  75. fx->slider.automate_mask[group] |= mask;
  76. fx->slider.change_mask[group] |= mask;
  77. if (nparms > 1) {
  78. if (ysfx_eel_round<int32_t>(parms[1][0])) {
  79. fx->slider.touch_mask[group] &= ~mask;
  80. } else {
  81. fx->slider.touch_mask[group] |= mask;
  82. }
  83. }
  84. return 0;
  85. }
  86. static EEL_F NSEEL_CGEN_CALL ysfx_api_sliderchange(void *opaque, EEL_F *mask_or_slider_)
  87. {
  88. //NOTE: callable from @gfx thread
  89. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  90. bool wantUndo = static_cast<int64_t>(*mask_or_slider_) == -1;
  91. if (wantUndo) {
  92. fx->want_undo = true;
  93. return 0;
  94. }
  95. uint32_t slider = ysfx_get_slider_of_var(fx, mask_or_slider_);
  96. uint8_t group;
  97. uint64_t mask;
  98. if (slider < ysfx_max_sliders) {
  99. group = ysfx_fetch_slider_group_index(slider);
  100. mask = ysfx_slider_mask(slider, group);
  101. } else {
  102. group = 0;
  103. mask = ysfx_eel_round<uint64_t>(std::fabs(*mask_or_slider_));
  104. }
  105. fx->slider.change_mask[group] |= mask;
  106. return 0;
  107. }
  108. static EEL_F NSEEL_CGEN_CALL ysfx_api_slider_show(void *opaque, EEL_F *mask_or_slider_, EEL_F *value_)
  109. {
  110. //NOTE: callable from @gfx thread
  111. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  112. uint32_t slider = ysfx_get_slider_of_var(fx, mask_or_slider_);
  113. if (ysfx_slider_exists(fx, slider)) {
  114. uint8_t group;
  115. uint64_t mask;
  116. if (slider < ysfx_max_sliders) {
  117. group = ysfx_fetch_slider_group_index(slider);
  118. mask = ysfx_slider_mask(slider, group);
  119. } else {
  120. group = 0;
  121. mask = ysfx_eel_round<uint64_t>(std::fabs(*mask_or_slider_));
  122. }
  123. if (*value_ >= (EEL_F)+0.5) {
  124. // show
  125. fx->slider.visible_mask[group] |= mask;
  126. }
  127. else if (*value_ >= (EEL_F)-0.5) {
  128. // hide
  129. mask = ~mask;
  130. fx->slider.visible_mask[group] &= mask;
  131. }
  132. else {
  133. // toggle
  134. mask = fx->slider.visible_mask[group].fetch_xor(mask) ^ mask;
  135. }
  136. return (EEL_F)mask;
  137. }
  138. return (EEL_F)0;
  139. }
  140. static EEL_F NSEEL_CGEN_CALL ysfx_api_midisend(void *opaque, INT_PTR np, EEL_F **parms)
  141. {
  142. if (ysfx_get_thread_id() != ysfx_thread_id_dsp)
  143. return 0;
  144. int32_t offset;
  145. uint8_t msg1;
  146. uint8_t msg2;
  147. uint8_t msg3;
  148. switch (np) {
  149. case 3:
  150. {
  151. offset = ysfx_eel_round<int32_t>(*parms[0]);
  152. msg1 = (uint8_t)ysfx_eel_round<int32_t>(*parms[1]);
  153. const uint32_t msg23 = ysfx_eel_round<int32_t>(*parms[2]);
  154. msg2 = (uint8_t)(msg23 & 0xff);
  155. msg3 = (uint8_t)(msg23 >> 8);
  156. break;
  157. }
  158. case 4:
  159. offset = ysfx_eel_round<int32_t>(*parms[0]);
  160. msg1 = (uint8_t)ysfx_eel_round<int32_t>(*parms[1]);
  161. msg2 = (uint8_t)ysfx_eel_round<int32_t>(*parms[2]);
  162. msg3 = (uint8_t)ysfx_eel_round<int32_t>(*parms[3]);
  163. break;
  164. default:
  165. assert(false);
  166. return 0;
  167. }
  168. if (offset < 0)
  169. offset = 0;
  170. // NOTE(jpc) correct the length of the message
  171. // in case it should be less than 3 bytes
  172. uint32_t length = ysfx_midi_sizeof(msg1);
  173. if (length == 0) // don't know what message this is
  174. length = 3;
  175. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  176. ysfx_midi_event_t event;
  177. const uint8_t data[] = {msg1, msg2, msg3};
  178. event.bus = ysfx_current_midi_bus(fx);
  179. event.offset = (uint32_t)offset;
  180. event.size = length;
  181. event.data = data;
  182. if (!ysfx_midi_push(fx->midi.out.get(), &event))
  183. return 0;
  184. return msg1;
  185. }
  186. static EEL_F NSEEL_CGEN_CALL ysfx_api_midisend_buf(void *opaque, EEL_F *offset_, EEL_F *buf_, EEL_F *len_)
  187. {
  188. if (ysfx_get_thread_id() != ysfx_thread_id_dsp)
  189. return 0;
  190. int32_t offset = ysfx_eel_round<int32_t>(*offset_);
  191. int32_t buf = ysfx_eel_round<int32_t>(*buf_);
  192. int32_t len = ysfx_eel_round<int32_t>(*len_);
  193. if (len <= 0)
  194. return 0;
  195. if (offset < 0)
  196. offset = 0;
  197. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  198. ysfx_midi_push_t mp;
  199. if (!ysfx_midi_push_begin(fx->midi.out.get(), ysfx_current_midi_bus(fx), (uint32_t)offset, &mp))
  200. return 0;
  201. ysfx_eel_ram_reader reader{fx->vm.get(), buf};
  202. for (uint32_t i = 0; i < (uint32_t)len; ++i) {
  203. uint8_t byte = (uint8_t)ysfx_eel_round<int32_t>(reader.read_next());
  204. if (!ysfx_midi_push_data(&mp, &byte, 1))
  205. break;
  206. }
  207. if (!ysfx_midi_push_end(&mp))
  208. return 0;
  209. return len;
  210. }
  211. static EEL_F NSEEL_CGEN_CALL ysfx_api_midisend_str(void *opaque, EEL_F *offset_, EEL_F *str_)
  212. {
  213. if (ysfx_get_thread_id() != ysfx_thread_id_dsp)
  214. return 0;
  215. int32_t offset = ysfx_eel_round<int32_t>(*offset_);
  216. if (offset < 0)
  217. offset = 0;
  218. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  219. ///
  220. struct process_data {
  221. ysfx_t *fx = nullptr;
  222. uint32_t offset = 0;
  223. uint32_t result = 0;
  224. };
  225. process_data pdata;
  226. pdata.offset = (uint32_t)offset;
  227. pdata.fx = fx;
  228. auto process_str = [](void *userdata, WDL_FastString &str) {
  229. process_data *pdata = (process_data *)userdata;
  230. ysfx_t *fx = pdata->fx;
  231. ysfx_midi_event_t event;
  232. event.bus = ysfx_current_midi_bus(fx);
  233. event.offset = pdata->offset;
  234. event.size = (uint32_t)str.GetLength();
  235. event.data = (const uint8_t *)str.Get();
  236. pdata->result = ysfx_midi_push(fx->midi.out.get(), &event) ? event.size : 0;
  237. };
  238. ///
  239. if (!ysfx_string_access(fx, *str_, false, +process_str, &pdata))
  240. return 0;
  241. return pdata.result;
  242. }
  243. static EEL_F NSEEL_CGEN_CALL ysfx_api_midisyx(void *opaque, EEL_F *offset_, EEL_F *buf_, EEL_F *len_)
  244. {
  245. if (ysfx_get_thread_id() != ysfx_thread_id_dsp)
  246. return 0;
  247. int32_t offset = ysfx_eel_round<int32_t>(*offset_);
  248. int32_t buf = ysfx_eel_round<int32_t>(*buf_);
  249. int32_t len = ysfx_eel_round<int32_t>(*len_);
  250. if (len <= 0)
  251. return 0;
  252. if (offset < 0)
  253. offset = 0;
  254. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  255. ysfx_midi_push_t mp;
  256. if (!ysfx_midi_push_begin(fx->midi.out.get(), ysfx_current_midi_bus(fx), (uint32_t)offset, &mp))
  257. return 0;
  258. ysfx_eel_ram_reader reader{fx->vm.get(), buf};
  259. for (uint32_t i = 0; i < (uint32_t)len; ++i) {
  260. uint8_t byte = (uint8_t)ysfx_eel_round<int32_t>(reader.read_next());
  261. const uint8_t head = 0xf0;
  262. const uint8_t tail = 0xf7;
  263. if (i == 0 && byte != head) {
  264. if (!ysfx_midi_push_data(&mp, &head, 1))
  265. break;
  266. }
  267. if (!ysfx_midi_push_data(&mp, &byte, 1))
  268. break;
  269. if (i + 1 == (uint32_t)len && byte != tail) {
  270. if (!ysfx_midi_push_data(&mp, &tail, 1))
  271. break;
  272. }
  273. }
  274. if (!ysfx_midi_push_end(&mp))
  275. return 0;
  276. return len;
  277. }
  278. static EEL_F NSEEL_CGEN_CALL ysfx_api_midirecv(void *opaque, INT_PTR np, EEL_F **parms)
  279. {
  280. if (ysfx_get_thread_id() != ysfx_thread_id_dsp)
  281. return 0;
  282. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  283. uint32_t bus = ysfx_current_midi_bus(fx);
  284. ysfx_midi_event_t event;
  285. bool have_event = ysfx_midi_get_next_from_bus(fx->midi.in.get(), bus, &event);
  286. // pass through the sysex events
  287. while (have_event && event.size > 3) {
  288. ysfx_midi_push(fx->midi.out.get(), &event);
  289. have_event = ysfx_midi_get_next_from_bus(fx->midi.in.get(), bus, &event);
  290. }
  291. if (!have_event)
  292. return 0;
  293. uint8_t msg1 = 0;
  294. uint8_t msg2 = 0;
  295. uint8_t msg3 = 0;
  296. switch (event.size) {
  297. case 3: msg3 = event.data[2]; // fall through
  298. case 2: msg2 = event.data[1]; // fall through
  299. case 1: msg1 = event.data[0]; break;
  300. }
  301. *parms[0] = (EEL_F)event.offset;
  302. *parms[1] = (EEL_F)msg1;
  303. switch (np) {
  304. case 4:
  305. *parms[2] = (EEL_F)msg2;
  306. *parms[3] = (EEL_F)msg3;
  307. break;
  308. case 3:
  309. *parms[2] = (EEL_F)(msg2 + (msg3 << 8));
  310. break;
  311. default:
  312. assert(false);
  313. return 0;
  314. }
  315. return 1;
  316. }
  317. static EEL_F NSEEL_CGEN_CALL ysfx_api_midirecv_buf(void *opaque, EEL_F *offset_, EEL_F *buf_, EEL_F *recvlen_)
  318. {
  319. if (ysfx_get_thread_id() != ysfx_thread_id_dsp)
  320. return 0;
  321. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  322. NSEEL_VMCTX vm = fx->vm.get();
  323. int32_t buf = ysfx_eel_round<int32_t>(*buf_);
  324. int32_t recvlen = ysfx_eel_round<int32_t>(*recvlen_);
  325. if (recvlen < 0)
  326. recvlen = 0;
  327. uint32_t bus = ysfx_current_midi_bus(fx);
  328. ysfx_midi_event_t event;
  329. bool have_event = ysfx_midi_get_next_from_bus(fx->midi.in.get(), bus, &event);
  330. // pass through the events larger than the buffer
  331. while (have_event && event.size > (uint32_t)recvlen) {
  332. ysfx_midi_push(fx->midi.out.get(), &event);
  333. have_event = ysfx_midi_get_next_from_bus(fx->midi.in.get(), bus, &event);
  334. }
  335. if (!have_event)
  336. return 0;
  337. *offset_ = (EEL_F)event.offset;
  338. ysfx_eel_ram_writer writer{vm, buf};
  339. for (uint32_t i = 0; i < event.size; ++i)
  340. writer.write_next(event.data[i]);
  341. return event.size;
  342. }
  343. static EEL_F NSEEL_CGEN_CALL ysfx_api_midirecv_str(void *opaque, EEL_F *offset_, EEL_F *str_)
  344. {
  345. if (ysfx_get_thread_id() != ysfx_thread_id_dsp)
  346. return 0;
  347. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  348. uint32_t bus = ysfx_current_midi_bus(fx);
  349. ysfx_midi_event_t event;
  350. bool have_event = ysfx_midi_get_next_from_bus(fx->midi.in.get(), bus, &event);
  351. // pass through the events larger than the maximum string
  352. while (have_event && event.size > ysfx_string_max_length) {
  353. ysfx_midi_push(fx->midi.out.get(), &event);
  354. have_event = ysfx_midi_get_next_from_bus(fx->midi.in.get(), bus, &event);
  355. }
  356. if (!have_event)
  357. return 0;
  358. auto process_str = [](void *userdata, WDL_FastString &str) {
  359. ysfx_midi_event_t *event = (ysfx_midi_event_t *)userdata;
  360. str.SetRaw((const char *)event->data, (int32_t)event->size);
  361. };
  362. ///
  363. if (!ysfx_string_access(fx, *str_, true, +process_str, &event))
  364. return 0;
  365. *offset_ = (EEL_F)event.offset;
  366. return event.size;
  367. }
  368. static EEL_F NSEEL_CGEN_CALL ysfx_api_strcpy_from_slider(void *opaque, EEL_F *str_, EEL_F *slider_)
  369. {
  370. int32_t handle = ysfx_eel_round<int32_t>(*slider_);
  371. if (handle < 0)
  372. return 0;
  373. ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
  374. uint32_t slider_idx = ysfx_get_slider_of_var(fx, slider_);
  375. if (slider_idx >= ysfx_max_sliders) {
  376. return 0;
  377. }
  378. if (!ysfx_slider_is_enum(fx, slider_idx)) {
  379. return 0;
  380. }
  381. uint32_t enum_idx = static_cast<uint32_t>(ysfx_slider_get_value(fx, slider_idx));
  382. const char* path = ysfx_slider_path(fx, slider_idx);
  383. std::string name{ysfx_slider_get_enum_name(fx, slider_idx, enum_idx)};
  384. std::string full_name;
  385. if (path) {
  386. std::string root{path};
  387. root.erase(0, 1);
  388. full_name = root + "/" + name;
  389. } else {
  390. full_name = name;
  391. };
  392. auto process_str = [](void *userdata, WDL_FastString &str) {
  393. std::string *value = (std::string *)userdata;
  394. str.SetRaw(value->c_str(), (int32_t)value->size());
  395. };
  396. if (!ysfx_string_access(fx, *str_, true, +process_str, &full_name))
  397. return 0;
  398. return *str_;
  399. }
  400. //------------------------------------------------------------------------------
  401. void ysfx_api_init_reaper()
  402. {
  403. NSEEL_addfunc_retptr("spl", 1, NSEEL_PProc_THIS, &ysfx_api_spl);
  404. NSEEL_addfunc_retptr("slider", 1, NSEEL_PProc_THIS, &ysfx_api_slider);
  405. NSEEL_addfunc_retval("slider_next_chg", 2, NSEEL_PProc_THIS, &ysfx_api_slider_next_chg);
  406. NSEEL_addfunc_varparm("slider_automate", 1, NSEEL_PProc_THIS, &ysfx_api_slider_automate);
  407. NSEEL_addfunc_retval("sliderchange", 1, NSEEL_PProc_THIS, &ysfx_api_sliderchange);
  408. NSEEL_addfunc_retval("slider_show", 2, NSEEL_PProc_THIS, &ysfx_api_slider_show);
  409. NSEEL_addfunc_exparms("midisend", 3, NSEEL_PProc_THIS, &ysfx_api_midisend);
  410. NSEEL_addfunc_exparms("midisend", 4, NSEEL_PProc_THIS, &ysfx_api_midisend);
  411. NSEEL_addfunc_retval("midisend_buf", 3, NSEEL_PProc_THIS, &ysfx_api_midisend_buf);
  412. NSEEL_addfunc_retval("midisend_str", 2, NSEEL_PProc_THIS, &ysfx_api_midisend_str);
  413. NSEEL_addfunc_exparms("midirecv", 3, NSEEL_PProc_THIS, &ysfx_api_midirecv);
  414. NSEEL_addfunc_exparms("midirecv", 4, NSEEL_PProc_THIS, &ysfx_api_midirecv);
  415. NSEEL_addfunc_retval("midirecv_buf", 3, NSEEL_PProc_THIS, &ysfx_api_midirecv_buf);
  416. NSEEL_addfunc_retval("midirecv_str", 2, NSEEL_PProc_THIS, &ysfx_api_midirecv_str);
  417. NSEEL_addfunc_retval("midisyx", 3, NSEEL_PProc_THIS, &ysfx_api_midisyx);
  418. NSEEL_addfunc_retval("strcpy_fromslider", 2, NSEEL_PProc_THIS, &ysfx_api_strcpy_from_slider);
  419. }