1 // Copyright 2021 Jean Pierre Cimalando
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // SPDX-License-Identifier: Apache-2.0
20 #include "ysfx_midi.hpp"
21 #include "ysfx_parse.hpp"
22 #include "ysfx_api_eel.hpp"
23 #include "ysfx_api_reaper.hpp"
24 #include "ysfx_api_file.hpp"
25 #include "ysfx_api_gfx.hpp"
26 #include "ysfx_utils.hpp"
27 #include "utility/sync_bitset.hpp"
28 #include "WDL/eel2/ns-eel.h"
29 #include "WDL/eel2/ns-eel-int.h"
30 #include <unordered_map>
33 YSFX_DEFINE_AUTO_PTR(NSEEL_VMCTX_u
, void, NSEEL_VM_free
); // NOTE: `NSEEL_VMCTX` is `void *`
34 YSFX_DEFINE_AUTO_PTR(NSEEL_CODEHANDLE_u
, void, NSEEL_code_free
); // NOTE: `NSEEL_CODEHANDLE` is `void *`
36 struct ysfx_source_unit_t
{
37 ysfx_toplevel_t toplevel
;
40 using ysfx_source_unit_u
= std::unique_ptr
< ysfx_source_unit_t
>;
42 enum ysfx_file_type_t
{
49 enum ysfx_thread_id_t
{
57 eel_string_context_state_u string_ctx
;
58 ysfx::mutex string_mutex
;
59 ysfx::mutex atomic_mutex
;
62 // some default values, these are not standard, just arbitrary
63 uint32_t block_size
= 128;
64 ysfx_real sample_rate
= 44100;
65 uint32_t valid_input_channels
= 2;
67 bool is_freshly_compiled
= false;
68 bool must_compute_init
= false;
69 bool must_compute_slider
= false;
71 std::unordered_map
<ysfx_real
*, uint32_t> slider_of_var
;
75 std::string main_file_path
;
76 std::string bank_path
;
77 ysfx_source_unit_u main
;
78 std::vector
<ysfx_source_unit_u
> imports
;
79 std::unordered_map
<std::string
, uint32_t> slider_alias
;
84 bool compiled
= false;
85 std::vector
<NSEEL_CODEHANDLE_u
> init
;
86 NSEEL_CODEHANDLE_u slider
;
87 NSEEL_CODEHANDLE_u block
;
88 NSEEL_CODEHANDLE_u sample
;
89 NSEEL_CODEHANDLE_u gfx
;
90 NSEEL_CODEHANDLE_u serialize
;
95 EEL_F
*spl
[ysfx_max_channels
] = {};
96 EEL_F
*slider
[ysfx_max_sliders
] = {};
97 EEL_F
*srate
= nullptr;
98 EEL_F
*num_ch
= nullptr;
99 EEL_F
*samplesblock
= nullptr;
100 EEL_F
*trigger
= nullptr;
101 EEL_F
*tempo
= nullptr;
102 EEL_F
*play_state
= nullptr;
103 EEL_F
*play_position
= nullptr;
104 EEL_F
*beat_position
= nullptr;
105 EEL_F
*ts_num
= nullptr;
106 EEL_F
*ts_denom
= nullptr;
107 EEL_F
*ext_noinit
= nullptr;
108 EEL_F
*ext_nodenorm
= nullptr;
109 EEL_F
*ext_midi_bus
= nullptr;
110 EEL_F
*midi_bus
= nullptr;
111 EEL_F
*pdc_delay
= nullptr;
112 EEL_F
*pdc_bot_ch
= nullptr;
113 EEL_F
*pdc_top_ch
= nullptr;
114 EEL_F
*pdc_midi
= nullptr;
116 EEL_F
*gfx_r
= nullptr;
117 EEL_F
*gfx_g
= nullptr;
118 EEL_F
*gfx_b
= nullptr;
119 EEL_F
*gfx_a
= nullptr;
120 EEL_F
*gfx_a2
= nullptr;
121 EEL_F
*gfx_w
= nullptr;
122 EEL_F
*gfx_h
= nullptr;
123 EEL_F
*gfx_x
= nullptr;
124 EEL_F
*gfx_y
= nullptr;
125 EEL_F
*gfx_mode
= nullptr;
126 EEL_F
*gfx_clear
= nullptr;
127 EEL_F
*gfx_texth
= nullptr;
128 EEL_F
*gfx_dest
= nullptr;
129 EEL_F
*gfx_ext_retina
= nullptr;
130 EEL_F
*mouse_x
= nullptr;
131 EEL_F
*mouse_y
= nullptr;
132 EEL_F
*mouse_cap
= nullptr;
133 EEL_F
*mouse_wheel
= nullptr;
134 EEL_F
*mouse_hwheel
= nullptr;
141 ysfx_midi_buffer_u in
;
142 ysfx_midi_buffer_u out
;
147 ysfx::sync_bitset64 automate_mask
;
148 ysfx::sync_bitset64 change_mask
;
149 ysfx::sync_bitset64 visible_mask
;
153 uint32_t triggers
= 0;
157 std::vector
<ysfx_file_u
> list
;
158 ysfx::mutex list_mutex
;
161 #if !defined(YSFX_NO_GFX)
164 ysfx_gfx_state_u state
;
166 volatile bool ready
= false;
167 volatile bool wants_retina
= false;
168 std::atomic
<bool> must_init
{false};
172 std::atomic
<uint32_t> ref_count
{1};
175 ysfx_thread_id_t
ysfx_get_thread_id();
176 void ysfx_set_thread_id(ysfx_thread_id_t id
);
177 void ysfx_unload_source(ysfx_t
*fx
);
178 void ysfx_unload_code(ysfx_t
*fx
);
179 void ysfx_first_init(ysfx_t
*fx
);
180 void ysfx_update_slider_visibility_mask(ysfx_t
*fx
);
181 void ysfx_fill_file_enums(ysfx_t
*fx
);
182 void ysfx_fix_invalid_enums(ysfx_t
*fx
);
183 ysfx_section_t
*ysfx_search_section(ysfx_t
*fx
, uint32_t type
, ysfx_toplevel_t
**origin
= nullptr);
184 std::string
ysfx_resolve_import_path(ysfx_t
*fx
, const std::string
&name
, const std::string
&origin
);
185 uint32_t ysfx_current_midi_bus(ysfx_t
*fx
);
186 void ysfx_clear_files(ysfx_t
*fx
);
187 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);
188 int32_t ysfx_insert_file(ysfx_t
*fx
, ysfx_file_t
*file
);
189 void ysfx_serialize(ysfx_t
*fx
);
190 uint32_t ysfx_get_slider_of_var(ysfx_t
*fx
, EEL_F
*var
);
191 bool ysfx_find_data_file(ysfx_t
*fx
, EEL_F
*file
, std::string
&result
);
192 ysfx_file_type_t
ysfx_detect_file_type(ysfx_t
*fx
, const char *path
, void **fmtobj
);