ysfx.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. // Copyright 2021 Jean Pierre Cimalando
  2. // Copyright 2024 Joep Vanlier
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // Modifications by Joep Vanlier, 2024
  17. //
  18. // SPDX-License-Identifier: Apache-2.0
  19. //
  20. #pragma once
  21. #if !defined(YSFX_INCLUDED_YSFX_H)
  22. #define YSFX_INCLUDED_YSFX_H
  23. #include <stdio.h>
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #if defined(_WIN32)
  28. # include <wchar.h>
  29. #endif
  30. //------------------------------------------------------------------------------
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #if !defined(YSFX_API)
  35. # if defined(_WIN32) && defined(YSFX_DLL_BUILD)
  36. # define YSFX_API __declspec(dllexport)
  37. # elif defined(__GNUC__)
  38. # define YSFX_API __attribute__((visibility("default")))
  39. # else
  40. # define YSFX_API
  41. # endif
  42. #endif
  43. //------------------------------------------------------------------------------
  44. // YSFX definitions
  45. typedef double ysfx_real;
  46. enum {
  47. ysfx_max_sliders = 256,
  48. ysfx_max_channels = 64,
  49. ysfx_max_midi_buses = 16,
  50. ysfx_max_triggers = 10,
  51. ysfx_max_slider_groups = 4, // needs to be ysfx_max_sliders / 64
  52. ysfx_max_default_vars = 1024, // needs to be bigger than max_sliders + all the built in variables
  53. };
  54. typedef enum ysfx_log_level_e {
  55. ysfx_log_info,
  56. ysfx_log_warning,
  57. ysfx_log_error,
  58. } ysfx_log_level;
  59. typedef void (ysfx_log_reporter_t)(intptr_t userdata, ysfx_log_level level, const char *message);
  60. //------------------------------------------------------------------------------
  61. // YSFX configuration
  62. typedef struct ysfx_config_s ysfx_config_t;
  63. typedef struct ysfx_audio_format_s ysfx_audio_format_t;
  64. // create a new configuration
  65. YSFX_API ysfx_config_t *ysfx_config_new();
  66. // delete a configuration
  67. YSFX_API void ysfx_config_free(ysfx_config_t *config);
  68. // increase the reference counter
  69. YSFX_API void ysfx_config_add_ref(ysfx_config_t *config);
  70. // set the path of the import root, a folder usually named "Effects"
  71. YSFX_API void ysfx_set_import_root(ysfx_config_t *config, const char *root);
  72. // set the path of the data root, a folder usually named "Data"
  73. YSFX_API void ysfx_set_data_root(ysfx_config_t *config, const char *root);
  74. // get the path of the import root, a folder usually named "Effects"
  75. YSFX_API const char *ysfx_get_import_root(ysfx_config_t *config);
  76. // get the path of the data root, a folder usually named "Data"
  77. YSFX_API const char *ysfx_get_data_root(ysfx_config_t *config);
  78. // guess the undefined root folders, based on the path to the JSFX file
  79. YSFX_API void ysfx_guess_file_roots(ysfx_config_t *config, const char *sourcepath);
  80. // register an audio format into the system
  81. YSFX_API void ysfx_register_audio_format(ysfx_config_t *config, ysfx_audio_format_t *afmt);
  82. // register the builtin audio formats (at least WAV file support)
  83. YSFX_API void ysfx_register_builtin_audio_formats(ysfx_config_t *config);
  84. // set the log reporting function
  85. YSFX_API void ysfx_set_log_reporter(ysfx_config_t *config, ysfx_log_reporter_t *reporter);
  86. // set the callback user data
  87. YSFX_API void ysfx_set_user_data(ysfx_config_t *config, intptr_t userdata);
  88. // get a string which textually represents the log level
  89. YSFX_API const char *ysfx_log_level_string(ysfx_log_level level);
  90. // LYNX: a host filter for the files a script may read (file_open, file sliders, gfx_loadimg): called with the resolved
  91. // path of an existing file; false = the script cannot open it. nullptr (the default) = every file, as upstream.
  92. // Process-wide; set it before any effect runs.
  93. typedef bool (ysfx_lynx_path_filter_t)(const char *path);
  94. YSFX_API void ysfx_lynx_set_path_filter(ysfx_lynx_path_filter_t *filter);
  95. //------------------------------------------------------------------------------
  96. // YSFX effect
  97. typedef struct ysfx_s ysfx_t;
  98. // create a new effect, taking a reference to config
  99. YSFX_API ysfx_t *ysfx_new(ysfx_config_t *config);
  100. // delete an effect
  101. YSFX_API void ysfx_free(ysfx_t *fx);
  102. // increase the reference counter
  103. YSFX_API void ysfx_add_ref(ysfx_t *fx);
  104. // get the configuration
  105. YSFX_API ysfx_config_t *ysfx_get_config(ysfx_t *fx);
  106. typedef enum ysfx_load_option_e {
  107. // skip imports; useful just for accessing header information and nothing else
  108. ysfx_load_ignoring_imports = 1,
  109. } ysfx_load_option_t;
  110. // load the source code from file without compiling
  111. YSFX_API bool ysfx_load_file(ysfx_t *fx, const char *filepath, uint32_t loadopts);
  112. // unload the source code and any compiled code
  113. YSFX_API void ysfx_unload(ysfx_t *fx);
  114. // check whether the effect is loaded
  115. YSFX_API bool ysfx_is_loaded(ysfx_t *fx);
  116. // get the name of the effect
  117. YSFX_API const char *ysfx_get_name(ysfx_t *fx);
  118. // get the path of the file which is loaded
  119. YSFX_API const char *ysfx_get_file_path(ysfx_t *fx);
  120. // get the author of the effect
  121. YSFX_API const char *ysfx_get_author(ysfx_t *fx);
  122. // get the number of tags of the effect
  123. #define ysfx_get_num_tags(fx) ysfx_get_tags((fx), NULL, 0)
  124. // get the list of tags of the effect
  125. YSFX_API uint32_t ysfx_get_tags(ysfx_t *fx, const char **dest, uint32_t destsize);
  126. // get a single tag of the effect
  127. YSFX_API const char *ysfx_get_tag(ysfx_t *fx, uint32_t index);
  128. // get the number of inputs
  129. YSFX_API uint32_t ysfx_get_num_inputs(ysfx_t *fx);
  130. // get the number of outputs
  131. YSFX_API uint32_t ysfx_get_num_outputs(ysfx_t *fx);
  132. // get the name of the input
  133. YSFX_API const char *ysfx_get_input_name(ysfx_t *fx, uint32_t index);
  134. // get the name of the output
  135. YSFX_API const char *ysfx_get_output_name(ysfx_t *fx, uint32_t index);
  136. // get whether this effect wants metering
  137. YSFX_API bool ysfx_wants_meters(ysfx_t *fx);
  138. // get requested dimensions of the graphics area; 0 means host should decide
  139. YSFX_API bool ysfx_get_gfx_dim(ysfx_t *fx, uint32_t dim[2]);
  140. // resolve an import path; note that this returns a char* string that needs to be freed with ysfx_free_resolved_path after use
  141. YSFX_API char *ysfx_resolve_path_and_allocate(ysfx_t* fx, const char* name, const char* origin);
  142. // free a path returned by ysfx_resolve_path_and_allocate
  143. YSFX_API void ysfx_free_resolved_path(char *path);
  144. typedef enum ysfx_section_type_e {
  145. ysfx_section_init = 1,
  146. ysfx_section_slider = 2,
  147. ysfx_section_block = 3,
  148. ysfx_section_sample = 4,
  149. ysfx_section_gfx = 5,
  150. ysfx_section_serialize = 6,
  151. } ysfx_section_type_t;
  152. // get whether the source has the given section
  153. YSFX_API bool ysfx_has_section(ysfx_t *fx, uint32_t type);
  154. typedef struct ysfx_slider_range_s {
  155. ysfx_real def;
  156. ysfx_real min;
  157. ysfx_real max;
  158. ysfx_real inc;
  159. } ysfx_slider_range_t;
  160. typedef struct ysfx_slider_curve_s {
  161. ysfx_real def;
  162. ysfx_real min;
  163. ysfx_real max;
  164. ysfx_real inc;
  165. uint8_t shape;
  166. ysfx_real modifier;
  167. } ysfx_slider_curve_t;
  168. // determine if slider exists; call from 0 to max-1 to scan available ones
  169. YSFX_API bool ysfx_slider_exists(ysfx_t *fx, uint32_t index);
  170. // get the name of a slider
  171. YSFX_API const char *ysfx_slider_get_name(ysfx_t *fx, uint32_t index);
  172. // get the range of a slider (deprecated: use ysfx_slider_get_curve instead)
  173. YSFX_API bool ysfx_slider_get_range(ysfx_t *fx, uint32_t index, ysfx_slider_range_t *range);
  174. // get the curve of a slider
  175. YSFX_API bool ysfx_slider_get_curve(ysfx_t *fx, uint32_t index, ysfx_slider_curve_t *curve);
  176. // get whether the slider is an enumeration
  177. YSFX_API bool ysfx_slider_is_enum(ysfx_t *fx, uint32_t index);
  178. // get the number of labels for the enumeration slider
  179. #define ysfx_slider_get_enum_size(fx, index) ysfx_slider_get_enum_names((fx), (index), NULL, 0)
  180. // get the list of labels for the enumeration slider
  181. YSFX_API uint32_t ysfx_slider_get_enum_names(ysfx_t *fx, uint32_t index, const char **dest, uint32_t destsize);
  182. // get a single label for the enumeration slider
  183. YSFX_API const char *ysfx_slider_get_enum_name(ysfx_t *fx, uint32_t slider_index, uint32_t enum_index);
  184. // get slider base path
  185. const char *ysfx_slider_path(ysfx_t *fx, uint32_t slider_index);
  186. // get whether the slider is a path (implies enumeration)
  187. YSFX_API bool ysfx_slider_is_path(ysfx_t *fx, uint32_t index);
  188. // get whether the slider is initially visible
  189. YSFX_API bool ysfx_slider_is_initially_visible(ysfx_t *fx, uint32_t index);
  190. // get the value of the slider
  191. YSFX_API ysfx_real ysfx_slider_get_value(ysfx_t *fx, uint32_t index);
  192. // set the value of the slider, and call @slider later if the value changed and we choose to notify the effect
  193. YSFX_API void ysfx_slider_set_value(ysfx_t *fx, uint32_t index, ysfx_real value, bool notify);
  194. // Note, there are two variants of these "normalized" slider values and they deal with
  195. // zero differently. In REAPER JSFX that span zero in their range, define the zero at
  196. // 0.5f when it comes to polling the value with the API. However, when automating such
  197. // a parameter, the temporal axis is skewed such that the effect of this is cancelled
  198. // again. The former is referred to as "raw" here, while the latter is regular.
  199. // get linear slider value from normalized value
  200. YSFX_API ysfx_real ysfx_slider_scale_from_normalized_linear_raw(ysfx_real value, const ysfx_slider_curve_t *curve);
  201. // get log slider value from normalized value
  202. YSFX_API ysfx_real ysfx_slider_scale_from_normalized_sqr_raw(ysfx_real value, const ysfx_slider_curve_t *curve);
  203. // get linear slider value from normalized value
  204. YSFX_API ysfx_real ysfx_slider_scale_from_normalized_linear(ysfx_real value, const ysfx_slider_curve_t *curve);
  205. // get log slider value from normalized value
  206. YSFX_API ysfx_real ysfx_slider_scale_from_normalized_log(ysfx_real value, const ysfx_slider_curve_t *curve);
  207. // get sqr slider value from normalized value
  208. YSFX_API ysfx_real ysfx_slider_scale_from_normalized_sqr(ysfx_real value, const ysfx_slider_curve_t *curve);
  209. // get normalized slider value from ysfx log value
  210. YSFX_API ysfx_real ysfx_slider_scale_to_normalized_linear_raw(ysfx_real value, const ysfx_slider_curve_t *curve);
  211. // get normalized slider value from ysfx log value
  212. YSFX_API ysfx_real ysfx_slider_scale_to_normalized_sqr_raw(ysfx_real value, const ysfx_slider_curve_t *curve);
  213. // get normalized slider value from ysfx log value
  214. YSFX_API ysfx_real ysfx_slider_scale_to_normalized_linear(ysfx_real value, const ysfx_slider_curve_t *curve);
  215. // get normalized slider value from ysfx log value
  216. YSFX_API ysfx_real ysfx_slider_scale_to_normalized_log(ysfx_real value, const ysfx_slider_curve_t *curve);
  217. // get normalized slider value from ysfx sqr value
  218. YSFX_API ysfx_real ysfx_slider_scale_to_normalized_sqr(ysfx_real value, const ysfx_slider_curve_t *curve);
  219. // convert normalized slider value to ysfx value taking into account curve shape
  220. YSFX_API ysfx_real ysfx_normalized_to_ysfx_value(ysfx_real value, const ysfx_slider_curve_t *curve);
  221. // convert normalized slider value to ysfx value taking into account curve shape
  222. YSFX_API ysfx_real ysfx_ysfx_value_to_normalized(ysfx_real value, const ysfx_slider_curve_t *curve);
  223. typedef enum ysfx_compile_option_e {
  224. // skip compiling the @serialize section
  225. ysfx_compile_no_serialize = 1 << 0,
  226. // skip compiling the @gfx section
  227. ysfx_compile_no_gfx = 1 << 1,
  228. } ysfx_compile_option_t;
  229. // compile the previously loaded source
  230. YSFX_API bool ysfx_compile(ysfx_t *fx, uint32_t compileopts);
  231. // check whether the effect is compiled
  232. YSFX_API bool ysfx_is_compiled(ysfx_t *fx);
  233. // get the block size
  234. YSFX_API uint32_t ysfx_get_block_size(ysfx_t *fx);
  235. // get the sample rate
  236. YSFX_API ysfx_real ysfx_get_sample_rate(ysfx_t *fx);
  237. // update the block size; don't forget to call @init
  238. YSFX_API void ysfx_set_block_size(ysfx_t *fx, uint32_t blocksize);
  239. // update the sample rate; don't forget to call @init
  240. YSFX_API void ysfx_set_sample_rate(ysfx_t *fx, ysfx_real samplerate);
  241. // set the capacity of the MIDI buffer
  242. YSFX_API void ysfx_set_midi_capacity(ysfx_t *fx, uint32_t capacity, bool extensible);
  243. // call init on the next cycle
  244. YSFX_API void ysfx_delayed_init(ysfx_t *fx);
  245. // activate and invoke @init
  246. YSFX_API void ysfx_init(ysfx_t *fx);
  247. // get the output latency
  248. YSFX_API ysfx_real ysfx_get_pdc_delay(ysfx_t *fx);
  249. // get the range of channels where output latency applies (end not included)
  250. YSFX_API void ysfx_get_pdc_channels(ysfx_t *fx, uint32_t channels[2]);
  251. // get whether the output latency applies to MIDI as well
  252. YSFX_API bool ysfx_get_pdc_midi(ysfx_t *fx);
  253. typedef enum ysfx_playback_state_e {
  254. ysfx_playback_stopped = 0,
  255. ysfx_playback_playing = 1,
  256. ysfx_playback_paused = 2,
  257. ysfx_playback_recording = 5,
  258. ysfx_playback_recording_paused = 6,
  259. } ysfx_playback_state_t;
  260. typedef struct ysfx_time_info_s {
  261. // tempo in beats/minute
  262. ysfx_real tempo;
  263. // state of the playback (ysfx_playback_state_t)
  264. uint32_t playback_state;
  265. // time position in seconds
  266. ysfx_real time_position;
  267. // time position in beats
  268. ysfx_real beat_position;
  269. // time signature in fraction form
  270. uint32_t time_signature[2];
  271. } ysfx_time_info_t;
  272. // update time information; do this before processing the cycle
  273. YSFX_API void ysfx_set_time_info(ysfx_t *fx, const ysfx_time_info_t *info);
  274. typedef struct ysfx_midi_event_s {
  275. // the bus number
  276. uint32_t bus;
  277. // the frame when it happens within the cycle
  278. uint32_t offset;
  279. // the size of the message
  280. uint32_t size;
  281. // the contents of the message
  282. const uint8_t *data;
  283. } ysfx_midi_event_t;
  284. // send MIDI, it will be processed during the cycle
  285. YSFX_API bool ysfx_send_midi(ysfx_t *fx, const ysfx_midi_event_t *event);
  286. // receive MIDI, after having processed the cycle
  287. YSFX_API bool ysfx_receive_midi(ysfx_t *fx, ysfx_midi_event_t *event);
  288. // receive MIDI from a single bus (do not mix with API above, use either)
  289. YSFX_API bool ysfx_receive_midi_from_bus(ysfx_t *fx, uint32_t bus, ysfx_midi_event_t *event);
  290. // send a trigger, it will be processed during the cycle
  291. YSFX_API bool ysfx_send_trigger(ysfx_t *fx, uint32_t index);
  292. // determine which group a particular slider is part of
  293. YSFX_API uint8_t ysfx_fetch_slider_group_index(uint32_t slider_number);
  294. // generate a slider bitmask for a slider with which you can extract the relevant bit
  295. YSFX_API uint64_t ysfx_slider_mask(uint32_t slider_number, uint8_t group_index);
  296. // get a bit mask of sliders whose values must be redisplayed, and clear it to zero
  297. YSFX_API uint64_t ysfx_fetch_slider_changes(ysfx_t *fx, uint8_t slider_group_index);
  298. // get a bit mask of sliders whose values must be automated, and clear it to zero
  299. YSFX_API uint64_t ysfx_fetch_slider_automations(ysfx_t *fx, uint8_t slider_group_index);
  300. // get a bit mask of sliders whose values are currently being touched
  301. YSFX_API uint64_t ysfx_fetch_slider_touches(ysfx_t *fx, uint8_t slider_group_index);
  302. // get a bit mask of sliders currently visible
  303. YSFX_API uint64_t ysfx_get_slider_visibility(ysfx_t *fx, uint8_t slider_group_index);
  304. // determine whether the plugin wants a manual undo point made and clear it to false
  305. YSFX_API bool ysfx_fetch_want_undopoint(ysfx_t *fx);
  306. // process a cycle in 32-bit float
  307. YSFX_API void ysfx_process_float(ysfx_t *fx, const float *const *ins, float *const *outs, uint32_t num_ins, uint32_t num_outs, uint32_t num_frames);
  308. // process a cycle in 64-bit float
  309. YSFX_API void ysfx_process_double(ysfx_t *fx, const double *const *ins, double *const *outs, uint32_t num_ins, uint32_t num_outs, uint32_t num_frames);
  310. typedef struct ysfx_state_slider_s {
  311. // index of the slider
  312. uint32_t index;
  313. // value of the slider
  314. ysfx_real value;
  315. } ysfx_state_slider_t;
  316. typedef struct ysfx_state_s {
  317. // values of the sliders
  318. ysfx_state_slider_t *sliders;
  319. // number of sliders
  320. uint32_t slider_count;
  321. // serialized data
  322. uint8_t *data;
  323. // size of serialized data
  324. size_t data_size;
  325. } ysfx_state_t;
  326. // load state
  327. YSFX_API bool ysfx_load_state(ysfx_t *fx, ysfx_state_t *state);
  328. // save current state; release this object when done
  329. YSFX_API ysfx_state_t *ysfx_save_state(ysfx_t *fx);
  330. // release a saved state object
  331. YSFX_API void ysfx_state_free(ysfx_state_t *state);
  332. // duplicate a state object
  333. YSFX_API ysfx_state_t *ysfx_state_dup(ysfx_state_t *state);
  334. // compare two state objects; returns true if they are the same
  335. YSFX_API bool ysfx_is_state_equal(ysfx_state_t *state1, ysfx_state_t *state2);
  336. // load only serialized state
  337. YSFX_API bool ysfx_load_serialized_state(ysfx_t *fx, ysfx_state_t *state);
  338. typedef struct ysfx_preset_s {
  339. // name of the preset
  340. char *name;
  341. // name used in the reaper blob
  342. char *blob_name;
  343. // state of the preset
  344. ysfx_state_t *state;
  345. } ysfx_preset_t;
  346. typedef struct ysfx_bank_s {
  347. // name of the bank
  348. char *name;
  349. // list of presets
  350. ysfx_preset_t *presets;
  351. // number of programs
  352. uint32_t preset_count;
  353. } ysfx_bank_t;
  354. // get the path of the RPL preset bank of the loaded JSFX, if present
  355. YSFX_API const char *ysfx_get_bank_path(ysfx_t *fx);
  356. // read a preset bank from RPL file
  357. YSFX_API ysfx_bank_t *ysfx_load_bank(const char *path);
  358. // write a preset bank to RPL file
  359. YSFX_API bool ysfx_save_bank(const char *path, ysfx_bank_t *bank);
  360. // free a preset bank
  361. YSFX_API void ysfx_bank_free(ysfx_bank_t *bank);
  362. // create empty preset bank
  363. YSFX_API ysfx_bank_t *ysfx_create_empty_bank(const char* bank_name);
  364. // add a preset to the current bank and returns a *new* bank without freeing the old bank
  365. YSFX_API ysfx_bank_t *ysfx_add_preset_to_bank(ysfx_bank_t *bank_in, const char* preset_name, ysfx_state_t *state);
  366. // returns > 0 if preset exists in bank. Preset index is given by return value - 1
  367. YSFX_API uint32_t ysfx_preset_exists(ysfx_bank_t *bank_in, const char* preset_name);
  368. // deletes a preset from the bank and returns a *new* bank without freeing the old bank
  369. YSFX_API ysfx_bank_t *ysfx_delete_preset_from_bank(ysfx_bank_t *bank_in, const char* preset_name);
  370. // renames a preset from the bank and returns a *new* bank without freeing the old bank
  371. YSFX_API ysfx_bank_t *ysfx_rename_preset_from_bank(ysfx_bank_t *bank_in, const char* preset_name, const char* new_preset_name);
  372. // swaps two presets in a bank (swaps in place!)
  373. YSFX_API void ysfx_swap_preset_in_bank(ysfx_bank_t *bank, int32_t preset_idx_1, int32_t preset_idx_2);
  374. // type of a function which can enumerate VM variables; returning 0 ends the search
  375. typedef int (ysfx_enum_vars_callback_t)(const char *name, ysfx_real *var, void *userdata);
  376. // enumerate all variables currently in the VM
  377. YSFX_API void ysfx_enum_vars(ysfx_t *fx, ysfx_enum_vars_callback_t *callback, void *userdata);
  378. // find a single variable in the VM
  379. YSFX_API ysfx_real *ysfx_find_var(ysfx_t *fx, const char *name);
  380. // read a single value from a variable in the VM
  381. YSFX_API ysfx_real ysfx_read_var(ysfx_t *fx, const char *name);
  382. // read a chunk of virtual memory from the VM
  383. YSFX_API void ysfx_read_vmem(ysfx_t *fx, uint32_t addr, ysfx_real *dest, uint32_t count);
  384. // read single value from VM RAM
  385. YSFX_API ysfx_real ysfx_read_vmem_single(ysfx_t *fx, uint32_t addr);
  386. // read how many memory slots are in use
  387. YSFX_API int ysfx_calculate_used_mem(ysfx_t *fx);
  388. //------------------------------------------------------------------------------
  389. // YSFX graphics
  390. // NOTE: all `ysfx_gfx_*` functions must be invoked from a dedicated UI thread
  391. typedef struct ysfx_gfx_config_s {
  392. // opaque user data passed to callbacks
  393. void *user_data;
  394. // the width of the frame buffer (having the scale factor applied)
  395. uint32_t pixel_width;
  396. // the height of the frame buffer (having the scale factor applied)
  397. uint32_t pixel_height;
  398. // the distance in bytes between lines; if 0, it defaults to (4*width)
  399. // currently it is required to be a multiple of 4
  400. uint32_t pixel_stride;
  401. // the pixel data of the frame buffer, of size (stride*height) bytes
  402. // the byte order in little-endian is 'BGRA', big-endian is 'ARGB'
  403. uint8_t *pixels;
  404. // the scale factor of the display; 1.0 or greater, 2.0 for Retina display
  405. ysfx_real scale_factor;
  406. // show a menu and run it synchronously; returns an item ID >= 1, or 0 if none
  407. int32_t (*show_menu)(void *user_data, const char *menu_spec, int32_t xpos, int32_t ypos);
  408. // change the cursor
  409. void (*set_cursor)(void *user_data, int32_t cursor);
  410. // if index is not -1, get the dropped file at this index (otherwise null)
  411. // if index is -1, clear the list of dropped files, and return null
  412. const char *(*get_drop_file)(void *user_data, int32_t index);
  413. } ysfx_gfx_config_t;
  414. // set up the graphics rendering
  415. YSFX_API void ysfx_gfx_setup(ysfx_t *fx, ysfx_gfx_config_t *gc);
  416. // get whether the current effect is requesting Retina support
  417. YSFX_API bool ysfx_gfx_wants_retina(ysfx_t *fx);
  418. // push a key to the input queue
  419. YSFX_API void ysfx_gfx_add_key(ysfx_t *fx, uint32_t mods, uint32_t key, bool press);
  420. // update mouse information; position is relative to canvas; wheel should be in steps normalized to ±1.0
  421. YSFX_API void ysfx_gfx_update_mouse(ysfx_t *fx, uint32_t mods, int32_t xpos, int32_t ypos, uint32_t buttons, ysfx_real wheel, ysfx_real hwheel);
  422. // update window state
  423. YSFX_API void ysfx_gfx_set_window_state(ysfx_t *fx, bool hasFocus, bool windowVisible, bool mouseOver);
  424. // invoke @gfx to paint the graphics; returns whether the framer buffer is modified
  425. YSFX_API bool ysfx_gfx_run(ysfx_t *fx);
  426. // request desired frame rate for UI refresh
  427. YSFX_API uint32_t ysfx_get_requested_framerate(ysfx_t *fx);
  428. //------------------------------------------------------------------------------
  429. // YSFX key map
  430. // these key definitions match those of pugl
  431. enum {
  432. ysfx_mod_shift = 1 << 0,
  433. ysfx_mod_ctrl = 1 << 1,
  434. ysfx_mod_alt = 1 << 2,
  435. ysfx_mod_super = 1 << 3,
  436. };
  437. enum {
  438. ysfx_key_backspace = 0x08,
  439. ysfx_key_escape = 0x1b,
  440. ysfx_key_delete = 0x7f,
  441. ysfx_key_f1 = 0xe000,
  442. ysfx_key_f2,
  443. ysfx_key_f3,
  444. ysfx_key_f4,
  445. ysfx_key_f5,
  446. ysfx_key_f6,
  447. ysfx_key_f7,
  448. ysfx_key_f8,
  449. ysfx_key_f9,
  450. ysfx_key_f10,
  451. ysfx_key_f11,
  452. ysfx_key_f12,
  453. ysfx_key_left,
  454. ysfx_key_up,
  455. ysfx_key_right,
  456. ysfx_key_down,
  457. ysfx_key_page_up,
  458. ysfx_key_page_down,
  459. ysfx_key_home,
  460. ysfx_key_end,
  461. ysfx_key_insert,
  462. };
  463. enum {
  464. ysfx_button_left = 1 << 0,
  465. ysfx_button_middle = 1 << 1,
  466. ysfx_button_right = 1 << 2,
  467. };
  468. //------------------------------------------------------------------------------
  469. // YSFX menu
  470. typedef struct ysfx_menu_insn_s ysfx_menu_insn_t;
  471. typedef struct ysfx_menu_s {
  472. // list of instructions to build a menu
  473. ysfx_menu_insn_t *insns;
  474. // number of menu instructions
  475. uint32_t insn_count;
  476. } ysfx_menu_t;
  477. typedef enum ysfx_menu_opcode_e {
  478. // appends an item
  479. ysfx_menu_item,
  480. // appends a separator
  481. ysfx_menu_separator,
  482. // appends a submenu and enters
  483. ysfx_menu_sub,
  484. // terminates a submenu and leaves
  485. ysfx_menu_endsub,
  486. } ysfx_menu_opcode_t;
  487. typedef enum ysfx_menu_item_flag_e {
  488. // whether the item is disabled (grayed out)
  489. ysfx_menu_item_disabled = 1 << 0,
  490. // whether the item is checked
  491. ysfx_menu_item_checked = 1 << 1,
  492. } ysfx_menu_item_flag_t;
  493. typedef struct ysfx_menu_insn_s {
  494. // operation code of this instruction
  495. ysfx_menu_opcode_t opcode;
  496. // unique identifier, greater than 0 (opcodes: item)
  497. uint32_t id;
  498. // name (opcodes: item, sub/endsub)
  499. const char *name;
  500. // combination of item flags (opcodes: item, sub/endsub)
  501. uint32_t item_flags;
  502. } ysfx_menu_insn_t;
  503. // parse a string which describes a popup menu (cf. `gfx_showmenu`)
  504. YSFX_API ysfx_menu_t *ysfx_parse_menu(const char *text);
  505. // free a menu
  506. YSFX_API void ysfx_menu_free(ysfx_menu_t *menu);
  507. //------------------------------------------------------------------------------
  508. // YSFX audio formats
  509. typedef struct ysfx_audio_reader_s ysfx_audio_reader_t;
  510. typedef struct ysfx_audio_file_info_s {
  511. uint32_t channels;
  512. ysfx_real sample_rate;
  513. } ysfx_audio_file_info_t;
  514. typedef struct ysfx_audio_format_s {
  515. // quickly checks if this format would be able to handle the given file
  516. bool (*can_handle)(const char *path);
  517. // open an audio file of this format for reading
  518. ysfx_audio_reader_t *(*open)(const char *path);
  519. // close the audio file
  520. void (*close)(ysfx_audio_reader_t *reader);
  521. // get the sample rate and the channel count
  522. ysfx_audio_file_info_t (*info)(ysfx_audio_reader_t *reader);
  523. // get the number of samples left to read
  524. uint64_t (*avail)(ysfx_audio_reader_t *reader);
  525. // move the read pointer back to the beginning
  526. void (*rewind)(ysfx_audio_reader_t *reader);
  527. // read the next block of samples
  528. uint64_t (*read)(ysfx_audio_reader_t *reader, ysfx_real *samples, uint64_t count);
  529. } ysfx_audio_format_t;
  530. //------------------------------------------------------------------------------
  531. #ifdef __cplusplus
  532. } // extern "C"
  533. #endif
  534. //------------------------------------------------------------------------------
  535. // YSFX RAII helpers
  536. #if defined(__cplusplus) && (__cplusplus >= 201103L || (defined(_MSC_VER) && _MSVC_LANG >= 201103L))
  537. #include <memory>
  538. #define YSFX_DEFINE_AUTO_PTR(aptr, styp, freefn) \
  539. struct aptr##_deleter { \
  540. void operator()(styp *x) const noexcept { freefn(x); } \
  541. }; \
  542. using aptr = std::unique_ptr<styp, aptr##_deleter>
  543. YSFX_DEFINE_AUTO_PTR(ysfx_config_u, ysfx_config_t, ysfx_config_free);
  544. YSFX_DEFINE_AUTO_PTR(ysfx_u, ysfx_t, ysfx_free);
  545. YSFX_DEFINE_AUTO_PTR(ysfx_state_u, ysfx_state_t, ysfx_state_free);
  546. YSFX_DEFINE_AUTO_PTR(ysfx_bank_u, ysfx_bank_t, ysfx_bank_free);
  547. YSFX_DEFINE_AUTO_PTR(ysfx_menu_u, ysfx_menu_t, ysfx_menu_free);
  548. #define YSFX_DEFINE_SHARED_PTR(sptr, styp, freefn) \
  549. struct sptr##_deleter { \
  550. void operator()(styp *x) const noexcept { freefn(x); } \
  551. }; \
  552. using sptr = std::shared_ptr<styp>; \
  553. inline sptr make_##sptr(styp *ptr) { \
  554. return sptr(ptr, sptr##_deleter()); \
  555. }
  556. YSFX_DEFINE_SHARED_PTR(ysfx_bank_shared, ysfx_bank_t, ysfx_bank_free)
  557. #endif // defined(__cplusplus) && (__cplusplus >= 201103L || (defined(_MSC_VER) && _MSVC_LANG >= 201103L))
  558. //------------------------------------------------------------------------------
  559. #endif // !defined(YSFX_INCLUDED_YSFX_H)