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.

ysfx.hpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #pragma once
  18. #include "ysfx.h"
  19. #include "ysfx_midi.hpp"
  20. #include "ysfx_parse.hpp"
  21. #include "ysfx_api_eel.hpp"
  22. #include "ysfx_api_reaper.hpp"
  23. #include "ysfx_api_file.hpp"
  24. #include "ysfx_api_gfx.hpp"
  25. #include "ysfx_utils.hpp"
  26. #include "utility/sync_bitset.hpp"
  27. #include "WDL/eel2/ns-eel.h"
  28. #include "WDL/eel2/ns-eel-int.h"
  29. #include <unordered_map>
  30. #include <atomic>
  31. YSFX_DEFINE_AUTO_PTR(NSEEL_VMCTX_u, void, NSEEL_VM_free); // NOTE: `NSEEL_VMCTX` is `void *`
  32. YSFX_DEFINE_AUTO_PTR(NSEEL_CODEHANDLE_u, void, NSEEL_code_free); // NOTE: `NSEEL_CODEHANDLE` is `void *`
  33. struct ysfx_source_unit_t {
  34. ysfx_toplevel_t toplevel;
  35. ysfx_header_t header;
  36. };
  37. using ysfx_source_unit_u = std::unique_ptr< ysfx_source_unit_t>;
  38. enum ysfx_file_type_t {
  39. ysfx_file_type_none,
  40. ysfx_file_type_txt,
  41. ysfx_file_type_raw,
  42. ysfx_file_type_audio,
  43. };
  44. enum ysfx_thread_id_t {
  45. ysfx_thread_id_none,
  46. ysfx_thread_id_dsp,
  47. ysfx_thread_id_gfx,
  48. };
  49. struct ysfx_s {
  50. ysfx_config_u config;
  51. eel_string_context_state_u string_ctx;
  52. ysfx::mutex string_mutex;
  53. ysfx::mutex atomic_mutex;
  54. NSEEL_VMCTX_u vm;
  55. // some default values, these are not standard, just arbitrary
  56. uint32_t block_size = 128;
  57. ysfx_real sample_rate = 44100;
  58. uint32_t valid_input_channels = 2;
  59. bool is_freshly_compiled = false;
  60. bool must_compute_init = false;
  61. bool must_compute_slider = false;
  62. std::unordered_map<ysfx_real *, uint32_t> slider_of_var;
  63. // source
  64. struct {
  65. std::string main_file_path;
  66. std::string bank_path;
  67. ysfx_source_unit_u main;
  68. std::vector<ysfx_source_unit_u> imports;
  69. std::unordered_map<std::string, uint32_t> slider_alias;
  70. } source;
  71. // compilation
  72. struct {
  73. bool compiled = false;
  74. std::vector<NSEEL_CODEHANDLE_u> init;
  75. NSEEL_CODEHANDLE_u slider;
  76. NSEEL_CODEHANDLE_u block;
  77. NSEEL_CODEHANDLE_u sample;
  78. NSEEL_CODEHANDLE_u gfx;
  79. NSEEL_CODEHANDLE_u serialize;
  80. } code;
  81. // VM variables
  82. struct {
  83. EEL_F *spl[ysfx_max_channels] = {};
  84. EEL_F *slider[ysfx_max_sliders] = {};
  85. EEL_F *srate = nullptr;
  86. EEL_F *num_ch = nullptr;
  87. EEL_F *samplesblock = nullptr;
  88. EEL_F *trigger = nullptr;
  89. EEL_F *tempo = nullptr;
  90. EEL_F *play_state = nullptr;
  91. EEL_F *play_position = nullptr;
  92. EEL_F *beat_position = nullptr;
  93. EEL_F *ts_num = nullptr;
  94. EEL_F *ts_denom = nullptr;
  95. EEL_F *ext_noinit = nullptr;
  96. EEL_F *ext_nodenorm = nullptr;
  97. EEL_F *ext_midi_bus = nullptr;
  98. EEL_F *midi_bus = nullptr;
  99. EEL_F *pdc_delay = nullptr;
  100. EEL_F *pdc_bot_ch = nullptr;
  101. EEL_F *pdc_top_ch = nullptr;
  102. EEL_F *pdc_midi = nullptr;
  103. // gfx variables
  104. EEL_F *gfx_r = nullptr;
  105. EEL_F *gfx_g = nullptr;
  106. EEL_F *gfx_b = nullptr;
  107. EEL_F *gfx_a = nullptr;
  108. EEL_F *gfx_a2 = nullptr;
  109. EEL_F *gfx_w = nullptr;
  110. EEL_F *gfx_h = nullptr;
  111. EEL_F *gfx_x = nullptr;
  112. EEL_F *gfx_y = nullptr;
  113. EEL_F *gfx_mode = nullptr;
  114. EEL_F *gfx_clear = nullptr;
  115. EEL_F *gfx_texth = nullptr;
  116. EEL_F *gfx_dest = nullptr;
  117. EEL_F *gfx_ext_retina = nullptr;
  118. EEL_F *mouse_x = nullptr;
  119. EEL_F *mouse_y = nullptr;
  120. EEL_F *mouse_cap = nullptr;
  121. EEL_F *mouse_wheel = nullptr;
  122. EEL_F *mouse_hwheel = nullptr;
  123. // other
  124. EEL_F ret_temp = 0;
  125. } var;
  126. // MIDI
  127. struct {
  128. ysfx_midi_buffer_u in;
  129. ysfx_midi_buffer_u out;
  130. } midi;
  131. // Slider
  132. struct {
  133. ysfx::sync_bitset64 automate_mask;
  134. ysfx::sync_bitset64 change_mask;
  135. ysfx::sync_bitset64 visible_mask;
  136. } slider;
  137. // Triggers
  138. uint32_t triggers = 0;
  139. // Files
  140. struct {
  141. std::vector<ysfx_file_u> list;
  142. ysfx::mutex list_mutex;
  143. } file;
  144. #if !defined(YSFX_NO_GFX)
  145. // Graphics
  146. struct {
  147. ysfx_gfx_state_u state;
  148. ysfx::mutex mutex;
  149. volatile bool ready = false;
  150. volatile bool wants_retina = false;
  151. std::atomic<bool> must_init{false};
  152. } gfx;
  153. #endif
  154. std::atomic<uint32_t> ref_count{1};
  155. };
  156. ysfx_thread_id_t ysfx_get_thread_id();
  157. void ysfx_set_thread_id(ysfx_thread_id_t id);
  158. void ysfx_unload_source(ysfx_t *fx);
  159. void ysfx_unload_code(ysfx_t *fx);
  160. void ysfx_first_init(ysfx_t *fx);
  161. void ysfx_update_slider_visibility_mask(ysfx_t *fx);
  162. void ysfx_fill_file_enums(ysfx_t *fx);
  163. void ysfx_fix_invalid_enums(ysfx_t *fx);
  164. ysfx_section_t *ysfx_search_section(ysfx_t *fx, uint32_t type, ysfx_toplevel_t **origin = nullptr);
  165. std::string ysfx_resolve_import_path(ysfx_t *fx, const std::string &name, const std::string &origin);
  166. uint32_t ysfx_current_midi_bus(ysfx_t *fx);
  167. void ysfx_clear_files(ysfx_t *fx);
  168. ysfx_file_t *ysfx_get_file(ysfx_t *fx, uint32_t handle, std::unique_lock<ysfx::mutex> &lock, std::unique_lock<ysfx::mutex> *list_lock = nullptr);
  169. int32_t ysfx_insert_file(ysfx_t *fx, ysfx_file_t *file);
  170. void ysfx_serialize(ysfx_t *fx);
  171. uint32_t ysfx_get_slider_of_var(ysfx_t *fx, EEL_F *var);
  172. bool ysfx_find_data_file(ysfx_t *fx, EEL_F *file, std::string &result);
  173. ysfx_file_type_t ysfx_detect_file_type(ysfx_t *fx, const char *path, void **fmtobj);