Beast - Music Synthesizer and Composer  0.11.1+10.g2da35
bseengine.hh
Go to the documentation of this file.
1  // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
2 #ifndef __BSE_ENGINE_H__
3 #define __BSE_ENGINE_H__
4 
5 #include <bse/bsedefs.hh>
6 
7 /* --- constants --- */
8 #define BSE_STREAM_MAX_VALUES (1024 /* power of 2 and <= 16384 */)
9 #define BSE_MODULE_N_OSTREAMS(module) ((module)->klass->n_ostreams)
10 #define BSE_MODULE_N_ISTREAMS(module) ((module)->klass->n_istreams)
11 #define BSE_MODULE_N_JSTREAMS(module) ((module)->klass->n_jstreams)
12 #define BSE_MODULE_ISTREAM(module, stream) ((module)->istreams[(stream)])
13 #define BSE_MODULE_JSTREAM(module, stream) ((module)->jstreams[(stream)])
14 #define BSE_MODULE_OSTREAM(module, stream) ((module)->ostreams[(stream)])
15 #define BSE_MODULE_IBUFFER(module, stream) (BSE_MODULE_ISTREAM ((module), (stream)).values)
16 #define BSE_MODULE_JBUFFER(module, stream, con) (BSE_MODULE_JSTREAM ((module), (stream)).values[con])
17 #define BSE_MODULE_OBUFFER(module, stream) (BSE_MODULE_OSTREAM ((module), (stream)).values)
18 #define BSE_ENGINE_MAX_POLLFDS (128)
19 
20 
21 /* --- typedefs --- */
22 typedef struct _BseJob BseJob;
23 /* bsedefs.hh:
24  * typedef void (*BseEngineAccessFunc) (BseModule *module,
25  * gpointer data);
26  * typedef void (*BseFreeFunc) (gpointer data);
27  */
28 typedef gboolean (*BseEnginePollFunc) (gpointer data,
29  guint n_values,
30  glong *timeout_p,
31  guint n_fds,
32  const GPollFD *fds,
33  gboolean revents_filled);
34 typedef gboolean (*BseEngineTimerFunc) (gpointer data,
35  guint64 tick_stamp);
36 typedef void (*BseEngineProbeFunc) (gpointer data,
37  guint n_values, /* bse_engine_block_size() */
38  guint64 tick_stamp,
39  guint n_ostreams, /* ENGINE_NODE_N_OSTREAMS() */
40  BseOStream **ostreams_p);
41 typedef void (*BseProcessFunc) (BseModule *module,
42  guint n_values);
43 typedef guint (*BseProcessDeferFunc) (BseModule *module,
44  guint n_ivalues,
45  guint n_ovalues);
46 typedef void (*BseModuleResetFunc) (BseModule *module);
47 typedef void (*BseModuleFreeFunc) (gpointer data,
48  const BseModuleClass *klass);
49 
50 typedef enum /*< skip >*/
51 {
52  BSE_COST_NORMAL = 0,
53  BSE_COST_CHEAP = 1 << 0,
54  BSE_COST_EXPENSIVE = 1 << 1
55 } BseCostType;
56 /* class, filled out by user */
58 {
59  guint n_istreams;
60  guint n_jstreams;
61  guint n_ostreams;
62  BseProcessFunc process; /* EngineThread */
63  BseProcessDeferFunc process_defer; /* EngineThread */
64  BseModuleResetFunc reset; /* EngineThread */
65  BseModuleFreeFunc free; /* UserThread */
66  BseCostType mflags;
67 };
68 /* module, constructed by engine */
69 struct _BseModule
70 {
71  const BseModuleClass *klass;
72  gpointer user_data;
73  BseIStream *istreams; /* input streams */
74  BseJStream *jstreams; /* joint (multiconnect) input streams */
75  BseOStream *ostreams; /* output streams */
76 };
77 /* streams, constructed by engine */
79 {
80  const gfloat **values;
81  guint n_connections; /* scheduler update */
82  /*< private >*/
83  guint jcount; /* internal field */
84 };
86 {
87  const gfloat *values;
88  gboolean connected; /* scheduler update */
89 };
91 {
92  gfloat *values;
93  gboolean connected;
94 };
95 
96 
97 /* --- interface (UserThread functions) --- */
98 BseModule* bse_module_new (const BseModuleClass *klass,
99  gpointer user_data);
100 BseModule* bse_module_new_virtual (guint n_iostreams,
101  gpointer user_data,
102  BseFreeFunc free_data);
103 guint64 bse_module_tick_stamp (BseModule *module);
104 gboolean bse_module_has_source (BseModule *module,
105  guint istream);
106 gboolean bse_module_is_scheduled (BseModule *module);
107 BseJob* bse_job_connect (BseModule *src_module,
108  guint src_ostream,
109  BseModule *dest_module,
110  guint dest_istream);
111 BseJob* bse_job_jconnect (BseModule *src_module,
112  guint src_ostream,
113  BseModule *dest_module,
114  guint dest_jstream);
115 BseJob* bse_job_disconnect (BseModule *dest_module,
116  guint dest_istream);
117 BseJob* bse_job_jdisconnect (BseModule *dest_module,
118  guint dest_jstream,
119  BseModule *src_module,
120  guint src_ostream);
121 BseJob* bse_job_kill_inputs (BseModule *module);
122 BseJob* bse_job_kill_outputs (BseModule *module);
123 BseJob* bse_job_integrate (BseModule *module);
124 BseJob* bse_job_discard (BseModule *module);
125 BseJob* bse_job_force_reset (BseModule *module);
126 BseJob* bse_job_set_consumer (BseModule *module,
127  gboolean is_toplevel_consumer);
128 BseJob* bse_job_suspend_now (BseModule *module);
129 BseJob* bse_job_resume_at (BseModule *module,
130  guint64 tick_stamp);
131 BseJob* bse_job_debug (const gchar *debug);
132 BseJob* bse_job_nop (void);
133 BseJob* bse_job_add_poll (BseEnginePollFunc poll_func,
134  gpointer data,
135  BseFreeFunc free_func,
136  guint n_fds,
137  const GPollFD *fds);
138 BseJob* bse_job_remove_poll (BseEnginePollFunc poll_func,
139  gpointer data);
140 BseJob* bse_job_add_timer (BseEngineTimerFunc timer_func,
141  gpointer data,
142  BseFreeFunc free_func);
143 BseJob* bse_job_access (BseModule *module,
144  BseEngineAccessFunc access_func, /* EngineThread */
145  gpointer data,
146  BseFreeFunc free_func); /* UserThread */
147 BseJob* bse_job_probe_request (BseModule *module,
148  BseEngineProbeFunc probe, /* UserThread */
149  gpointer data);
150 BseJob* bse_job_flow_access (BseModule *module,
151  guint64 tick_stamp,
152  BseEngineAccessFunc access_func, /* EngineThread */
153  gpointer data,
154  BseFreeFunc free_func); /* UserThread */
155 BseJob* bse_job_boundary_access (BseModule *module,
156  guint64 tick_stamp,
157  BseEngineAccessFunc access_func, /* EngineThread */
158  gpointer data,
159  BseFreeFunc free_func); /* UserThread */
160 BseJob* bse_job_boundary_discard (BseModule *module);
161 BseTrans* bse_trans_open (void);
162 void bse_trans_add (BseTrans *trans,
163  BseJob *job);
164 BseTrans* bse_trans_merge (BseTrans *trans1,
165  BseTrans *trans2);
166 guint64 bse_trans_commit (BseTrans *trans);
167 void bse_trans_commit_delayed (BseTrans *trans,
168  guint64 tick_stamp);
169 void bse_trans_dismiss (BseTrans *trans);
170 void bse_transact (BseJob *job,
171  ...) G_GNUC_NULL_TERMINATED;
172 
173 /* --- module utilities (EngineThread functions) --- */
174 float* bse_engine_const_values (float value);
175 
176 /* --- initialization & main loop --- */
177 void bse_engine_constrain (guint latency_ms,
178  guint sample_freq,
179  guint control_freq,
180  guint *block_size_p,
181  guint *control_raster_p);
182 void bse_engine_init ();
183 gboolean bse_engine_configure (guint latency_ms,
184  guint sample_freq,
185  guint control_freq);
186 
187 /* --- miscellaneous --- */
188 float* bse_engine_const_zeros (uint smaller_than_BSE_STREAM_MAX_VALUES);
189 gboolean bse_engine_has_garbage (void);
191 void bse_engine_free_ostreams (guint n_ostreams,
192  BseOStream *ostreams);
193 void bse_engine_add_user_callback (gpointer data,
194  BseFreeFunc free_func); /* UserThread */
195 void bse_engine_wait_on_trans (void);
196 guint64 bse_engine_tick_stamp_from_systime (guint64 systime);
197 #define bse_engine_block_size() (0 + (const uint) bse_engine_exvar_block_size)
198 #define bse_engine_sample_freq() (0 + (const uint) bse_engine_exvar_sample_freq)
199 #define bse_engine_control_raster() (1 + (const uint) bse_engine_exvar_control_mask)
200 #define bse_engine_control_mask() (0 + (const uint) bse_engine_exvar_control_mask)
201 #define BSE_CONTROL_CHECK(index) ((bse_engine_control_mask() & (index)) == 0)
202 
203 /* --- thread handling --- */
204 typedef struct
205 {
206  glong timeout;
207  gboolean fds_changed;
208  guint n_fds;
209  GPollFD *fds;
210  gboolean revents_filled;
211 } BseEngineLoop;
212 
213 gboolean bse_engine_prepare (BseEngineLoop *loop);
214 gboolean bse_engine_check (const BseEngineLoop *loop);
215 void bse_engine_dispatch (void);
216 
217 /*< private >*/
218 extern uint bse_engine_exvar_block_size;
219 extern uint bse_engine_exvar_sample_freq;
220 extern uint bse_engine_exvar_control_mask;
221 
222 #endif /* __BSE_ENGINE_H__ */
void bse_trans_add(BseTrans *trans, BseJob *job)
Definition: bseengine.cc:900
gboolean bse_engine_configure(guint latency_ms, guint sample_freq, guint control_freq)
Definition: bseengine.cc:1284
Definition: bseengine.hh:90
BseJob * bse_job_jconnect(BseModule *src_module, guint src_ostream, BseModule *dest_module, guint dest_jstream)
Definition: bseengine.cc:272
gboolean bse_module_is_scheduled(BseModule *module)
Definition: bseengine.cc:128
BseTrans * bse_trans_open(void)
Definition: bseengine.cc:878
BseJob * bse_job_probe_request(BseModule *module, BseEngineProbeFunc probe, gpointer data)
Definition: bseengine.cc:515
Definition: bseengine.hh:78
BseJob * bse_job_remove_poll(BseEnginePollFunc poll_func, gpointer data)
Definition: bseengine.cc:782
void debug(const char *conditional, const char *format, const Args &...args) RAPICORN_ALWAYS_INLINE
Issue a printf-like debugging message if conditional is enabled by $BSE_DEBUG.
Definition: bcore.hh:96
void(* BseEngineProbeFunc)(gpointer data, guint n_values, guint64 tick_stamp, guint n_ostreams, BseOStream **ostreams_p)
Definition: bseengine.hh:36
BseJob * bse_job_jdisconnect(BseModule *dest_module, guint dest_jstream, BseModule *src_module, guint src_ostream)
Definition: bseengine.cc:339
void bse_engine_user_thread_collect(void)
BSE Engine user thread function.
Definition: bseengineutils.cc:356
guint64 bse_engine_tick_stamp_from_systime(guint64 systime)
Definition: bseengine.cc:1420
BseJob * bse_job_discard(BseModule *module)
Definition: bseengine.cc:166
void bse_engine_init()
Initialize the BSE audio processing engine.
Definition: bseengine.cc:1356
BseJob * bse_job_flow_access(BseModule *module, guint64 tick_stamp, BseEngineAccessFunc access_func, gpointer data, BseFreeFunc free_func)
Definition: bseengine.cc:558
Definition: bseengine.hh:204
BseJob * bse_job_resume_at(BseModule *module, guint64 tick_stamp)
Definition: bseengine.cc:701
BseJob * bse_job_kill_inputs(BseModule *module)
Definition: bseengine.cc:188
BseJob * bse_job_connect(BseModule *src_module, guint src_ostream, BseModule *dest_module, guint dest_istream)
Definition: bseengine.cc:237
BseTrans * bse_trans_merge(BseTrans *trans1, BseTrans *trans2)
Definition: bseengine.cc:926
BseJob * bse_job_force_reset(BseModule *module)
Definition: bseengine.cc:391
BseJob * bse_job_boundary_access(BseModule *module, guint64 tick_stamp, BseEngineAccessFunc access_func, gpointer data, BseFreeFunc free_func)
Definition: bseengine.cc:599
void bse_trans_commit_delayed(BseTrans *trans, guint64 tick_stamp)
Definition: bseengine.cc:1023
gboolean bse_module_has_source(BseModule *module, guint istream)
Definition: bseengine.cc:108
Definition: bseengine.hh:69
Definition: bseenginenode.hh:59
BseJob * bse_job_debug(const gchar *debug)
Definition: bseengine.cc:839
BseJob * bse_job_integrate(BseModule *module)
Definition: bseengine.cc:143
void(* BseEngineAccessFunc)(BseModule *module, gpointer data)
Definition: bsedefs.hh:130
void bse_engine_add_user_callback(gpointer data, BseFreeFunc free_func)
Definition: bseengine.cc:458
BseJob * bse_job_nop(void)
Definition: bseengine.cc:860
guint64 bse_module_tick_stamp(BseModule *module)
Definition: bseengine.cc:86
BseModule * bse_module_new_virtual(guint n_iostreams, gpointer user_data, BseFreeFunc free_data)
Definition: bseengine.cc:1147
void bse_engine_dispatch(void)
Perform necessary work the engine has to handle in the user thread.
Definition: bseengine.cc:1403
BseJob * bse_job_kill_outputs(BseModule *module)
Definition: bseengine.cc:210
void bse_transact(BseJob *job,...) G_GNUC_NULL_TERMINATED
Definition: bseengine.cc:1071
BseJob * bse_job_boundary_discard(BseModule *module)
Definition: bseengine.cc:644
BseModule * bse_module_new(const BseModuleClass *klass, gpointer user_data)
Definition: bseengine.cc:35
guint64 bse_trans_commit(BseTrans *trans)
Definition: bseengine.cc:965
BseJob * bse_job_disconnect(BseModule *dest_module, guint dest_istream)
Definition: bseengine.cc:305
BseJob * bse_job_suspend_now(BseModule *module)
Definition: bseengine.cc:675
void bse_engine_wait_on_trans(void)
Wait until all pending transactions have been processed by the BSE Engine.
Definition: bseengine.cc:1465
void bse_trans_dismiss(BseTrans *trans)
Definition: bseengine.cc:1053
BseJob * bse_job_access(BseModule *module, BseEngineAccessFunc access_func, gpointer data, BseFreeFunc free_func)
Definition: bseengine.cc:428
Definition: bseengine.hh:85
void bse_engine_constrain(guint latency_ms, guint sample_freq, guint control_freq, guint *block_size_p, guint *control_raster_p)
Definition: bseengine.cc:1220
gboolean(* BseEnginePollFunc)(gpointer data, guint n_values, glong *timeout_p, guint n_fds, const GPollFD *fds, gboolean revents_filled)
Definition: bseengine.hh:28
Definition: bseengine.hh:57
BseJob * bse_job_add_timer(BseEngineTimerFunc timer_func, gpointer data, BseFreeFunc free_func)
Definition: bseengine.cc:812
BseJob * bse_job_add_poll(BseEnginePollFunc poll_func, gpointer data, BseFreeFunc free_func, guint n_fds, const GPollFD *fds)
Definition: bseengine.cc:749