@devaloop/devalang 0.0.1-beta.1 → 0.0.1-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/.devalang +9 -10
  2. package/Cargo.toml +84 -80
  3. package/README.md +10 -7
  4. package/docs/CHANGELOG.md +83 -0
  5. package/docs/ROADMAP.md +6 -2
  6. package/docs/TODO.md +3 -14
  7. package/examples/bus.deva +10 -0
  8. package/examples/chain.deva +19 -0
  9. package/examples/effect.deva +2 -0
  10. package/examples/filter.deva +11 -0
  11. package/examples/lfo.deva +9 -0
  12. package/examples/plugin.deva +10 -10
  13. package/examples/routing.deva +23 -0
  14. package/examples/synth.deva +11 -1
  15. package/examples/synth_types.deva +17 -0
  16. package/out-tsc/bin/project-version.json +6 -0
  17. package/out-tsc/core/functions/index.d.ts +5 -0
  18. package/out-tsc/core/functions/index.js +11 -0
  19. package/out-tsc/pkg/devalang_core.d.ts +2 -0
  20. package/out-tsc/pkg/devalang_core.js +17 -2
  21. package/out-tsc/pkg/devalang_core_bg.wasm.d.ts +1 -0
  22. package/out-tsc/scripts/version/copy-to-binary.d.ts +1 -0
  23. package/out-tsc/scripts/version/copy-to-binary.js +79 -0
  24. package/package.json +23 -10
  25. package/project-version.json +3 -3
  26. package/rust/bindings/Cargo.toml +9 -0
  27. package/rust/bindings/src/lib.rs +86 -0
  28. package/rust/cli/addon/commands.rs +35 -0
  29. package/rust/cli/addon/download.rs +234 -0
  30. package/rust/cli/addon/install.rs +33 -0
  31. package/rust/cli/addon/list.rs +224 -0
  32. package/rust/cli/addon/metadata.rs +124 -0
  33. package/rust/cli/addon/mod.rs +8 -0
  34. package/rust/cli/addon/remove.rs +271 -0
  35. package/rust/cli/addon/update.rs +305 -0
  36. package/rust/cli/{install/addon.rs → addon/utils.rs} +34 -43
  37. package/rust/cli/build/commands.rs +153 -103
  38. package/rust/cli/build/mod.rs +2 -2
  39. package/rust/cli/build/process.rs +165 -146
  40. package/rust/cli/check/mod.rs +208 -208
  41. package/rust/cli/discover/commands.rs +53 -31
  42. package/rust/cli/discover/config.rs +2 -4
  43. package/rust/cli/discover/install.rs +139 -28
  44. package/rust/cli/discover/metadata.rs +3 -3
  45. package/rust/cli/login/commands.rs +124 -124
  46. package/rust/cli/me/commands.rs +52 -0
  47. package/rust/cli/me/mod.rs +1 -0
  48. package/rust/cli/mod.rs +2 -2
  49. package/rust/cli/parser.rs +76 -70
  50. package/rust/cli/play/commands.rs +375 -324
  51. package/rust/cli/play/mod.rs +5 -5
  52. package/rust/cli/play/process.rs +159 -150
  53. package/rust/cli/play/realtime.rs +91 -91
  54. package/rust/cli/telemetry/commands.rs +22 -22
  55. package/rust/cli/telemetry/event_creator.rs +80 -80
  56. package/rust/cli/telemetry/mod.rs +3 -3
  57. package/rust/cli/telemetry/send.rs +51 -51
  58. package/rust/cli/template/commands.rs +69 -69
  59. package/rust/config/driver.rs +112 -103
  60. package/rust/config/mod.rs +3 -3
  61. package/rust/config/ops.rs +26 -26
  62. package/rust/config/settings.rs +101 -101
  63. package/rust/core/audio/engine/driver.rs +237 -0
  64. package/rust/core/audio/engine/export.rs +169 -0
  65. package/rust/core/audio/engine/helpers.rs +178 -170
  66. package/rust/core/audio/engine/mod.rs +56 -7
  67. package/rust/core/audio/engine/notes/dsp.rs +88 -0
  68. package/rust/core/audio/engine/notes/mod.rs +53 -0
  69. package/rust/core/audio/engine/notes/params.rs +294 -0
  70. package/rust/core/audio/engine/sample/insert.rs +300 -0
  71. package/rust/core/audio/engine/sample/mod.rs +40 -0
  72. package/rust/core/audio/engine/sample/padding.rs +170 -0
  73. package/rust/core/audio/evaluator/condition.rs +61 -0
  74. package/rust/core/audio/evaluator/mod.rs +9 -0
  75. package/rust/core/audio/{evaluator.rs → evaluator/numeric.rs} +152 -310
  76. package/rust/core/audio/evaluator/rhs.rs +16 -0
  77. package/rust/core/audio/evaluator/string_expr.rs +94 -0
  78. package/rust/core/audio/interpreter/driver.rs +574 -542
  79. package/rust/core/audio/interpreter/mod.rs +2 -14
  80. package/rust/core/audio/interpreter/statements/arrow_call/interprete.rs +179 -0
  81. package/rust/core/audio/interpreter/statements/arrow_call/methods/chord.rs +398 -0
  82. package/rust/core/audio/interpreter/statements/arrow_call/methods/effects.rs +323 -0
  83. package/rust/core/audio/interpreter/statements/arrow_call/methods/mod.rs +3 -0
  84. package/rust/core/audio/interpreter/statements/arrow_call/methods/note.rs +371 -0
  85. package/rust/core/audio/interpreter/statements/arrow_call/mod.rs +3 -0
  86. package/rust/core/audio/interpreter/statements/arrow_call/types/arp.rs +192 -0
  87. package/rust/core/audio/interpreter/statements/arrow_call/types/mod.rs +24 -0
  88. package/rust/core/audio/interpreter/statements/arrow_call/types/pad.rs +116 -0
  89. package/rust/core/audio/interpreter/statements/arrow_call/types/pluck.rs +97 -0
  90. package/rust/core/audio/interpreter/statements/arrow_call/types/sub.rs +100 -0
  91. package/rust/core/audio/interpreter/{automate.rs → statements/automate.rs} +2 -4
  92. package/rust/core/audio/interpreter/{call.rs → statements/call.rs} +36 -5
  93. package/rust/core/audio/interpreter/{condition.rs → statements/condition.rs} +72 -71
  94. package/rust/core/audio/interpreter/{function.rs → statements/function.rs} +24 -26
  95. package/rust/core/audio/interpreter/{let_.rs → statements/let_.rs} +36 -38
  96. package/rust/core/audio/interpreter/{load.rs → statements/load.rs} +17 -19
  97. package/rust/core/audio/interpreter/{loop_.rs → statements/loop_.rs} +115 -114
  98. package/rust/core/audio/interpreter/statements/mod.rs +12 -0
  99. package/rust/core/audio/interpreter/{sleep.rs → statements/sleep.rs} +28 -28
  100. package/rust/core/audio/interpreter/{spawn.rs → statements/spawn.rs} +54 -4
  101. package/rust/core/audio/interpreter/{tempo.rs → statements/tempo.rs} +40 -40
  102. package/rust/core/audio/interpreter/{trigger.rs → statements/trigger.rs} +242 -239
  103. package/rust/core/audio/loader/trigger.rs +98 -97
  104. package/rust/core/audio/mod.rs +6 -7
  105. package/rust/core/audio/special/easing.rs +189 -189
  106. package/rust/core/audio/special/env.rs +45 -45
  107. package/rust/core/audio/special/math.rs +134 -134
  108. package/rust/core/audio/special/modulator.rs +143 -143
  109. package/rust/core/builder/mod.rs +129 -86
  110. package/rust/core/debugger/{module.rs → logs.rs} +52 -55
  111. package/rust/core/debugger/mod.rs +30 -30
  112. package/rust/core/debugger/store.rs +38 -40
  113. package/rust/core/error/mod.rs +269 -269
  114. package/rust/core/lexer/driver.rs +2 -4
  115. package/rust/core/mod.rs +9 -10
  116. package/rust/core/parser/driver/block.rs +111 -0
  117. package/rust/core/parser/driver/cursor.rs +82 -0
  118. package/rust/core/parser/driver/driver_impl.rs +159 -0
  119. package/rust/core/parser/driver/mod.rs +6 -0
  120. package/rust/core/parser/driver/parse_array.rs +120 -0
  121. package/rust/core/parser/driver/parse_map.rs +247 -0
  122. package/rust/core/parser/driver/parser.rs +160 -0
  123. package/rust/core/parser/handler/arrow_call.rs +90 -15
  124. package/rust/core/parser/handler/at.rs +279 -279
  125. package/rust/core/parser/handler/bank.rs +104 -104
  126. package/rust/core/parser/handler/condition.rs +83 -83
  127. package/rust/core/parser/handler/dot.rs +148 -148
  128. package/rust/core/parser/handler/identifier/automate.rs +254 -254
  129. package/rust/core/parser/handler/identifier/call.rs +91 -91
  130. package/rust/core/parser/handler/identifier/emit.rs +70 -70
  131. package/rust/core/parser/handler/identifier/function.rs +113 -113
  132. package/rust/core/parser/handler/identifier/group.rs +89 -89
  133. package/rust/core/parser/handler/identifier/let_.rs +173 -173
  134. package/rust/core/parser/handler/identifier/mod.rs +55 -55
  135. package/rust/core/parser/handler/identifier/on.rs +107 -107
  136. package/rust/core/parser/handler/identifier/print.rs +49 -49
  137. package/rust/core/parser/handler/identifier/sleep.rs +96 -43
  138. package/rust/core/parser/handler/identifier/spawn.rs +91 -91
  139. package/rust/core/parser/handler/identifier/synth.rs +39 -3
  140. package/rust/core/parser/handler/loop_.rs +194 -194
  141. package/rust/core/parser/handler/pattern.rs +25 -2
  142. package/rust/core/parser/handler/tempo.rs +105 -57
  143. package/rust/core/parser/statement.rs +10 -11
  144. package/rust/core/plugin/loader.rs +137 -137
  145. package/rust/core/plugin/runner/mod.rs +11 -0
  146. package/rust/core/plugin/{runner.rs → runner/non_wasm.rs} +206 -72
  147. package/rust/core/plugin/runner/wasm32.rs +44 -0
  148. package/rust/core/preprocessor/loader/inject.rs +313 -0
  149. package/rust/core/preprocessor/loader/loader_helpers.rs +110 -0
  150. package/rust/core/preprocessor/loader/mod.rs +235 -0
  151. package/rust/core/preprocessor/module.rs +55 -60
  152. package/rust/core/preprocessor/{processor.rs → processor/handlers.rs} +107 -114
  153. package/rust/core/preprocessor/processor/mod.rs +1 -0
  154. package/rust/core/preprocessor/resolver/function.rs +69 -69
  155. package/rust/core/preprocessor/resolver/group.rs +122 -94
  156. package/rust/core/preprocessor/resolver/pattern.rs +14 -2
  157. package/rust/core/store/global.rs +57 -61
  158. package/rust/core/store/mod.rs +1 -5
  159. package/rust/lib.rs +323 -308
  160. package/rust/macros/Cargo.toml +14 -0
  161. package/rust/macros/src/lib.rs +52 -0
  162. package/rust/main.rs +336 -143
  163. package/rust/types/Cargo.toml +1 -1
  164. package/rust/types/src/addons.rs +57 -55
  165. package/rust/types/src/config.rs +82 -74
  166. package/rust/types/src/lib.rs +15 -12
  167. package/rust/types/src/plugin.rs +20 -0
  168. package/rust/types/src/store.rs +139 -0
  169. package/rust/types/src/telemetry.rs +85 -85
  170. package/rust/utils/Cargo.toml +5 -2
  171. package/rust/utils/src/file.rs +477 -94
  172. package/rust/utils/src/first_usage.rs +97 -97
  173. package/rust/utils/src/lib.rs +9 -9
  174. package/rust/utils/src/logger.rs +200 -200
  175. package/rust/utils/src/path.rs +158 -88
  176. package/rust/utils/src/signature.rs +41 -41
  177. package/rust/utils/src/spinner.rs +20 -20
  178. package/rust/utils/src/version.rs +58 -27
  179. package/rust/utils/src/watcher.rs +46 -46
  180. package/rust/web/api.rs +5 -5
  181. package/rust/web/auth.rs +5 -0
  182. package/rust/web/cdn.rs +34 -34
  183. package/rust/web/forge.rs +5 -0
  184. package/rust/web/mod.rs +2 -0
  185. package/tests/integration.rs +21 -21
  186. package/typescript/core/functions/index.ts +11 -0
  187. package/typescript/pkg/devalang_core.ts +20 -4
  188. package/typescript/scripts/version/copy-to-binary.ts +82 -0
  189. package/rust/cli/bank/api.rs +0 -122
  190. package/rust/cli/bank/commands.rs +0 -275
  191. package/rust/cli/bank/mod.rs +0 -29
  192. package/rust/cli/install/bank.rs +0 -53
  193. package/rust/cli/install/commands.rs +0 -35
  194. package/rust/cli/install/mod.rs +0 -4
  195. package/rust/cli/install/plugin.rs +0 -61
  196. package/rust/core/audio/engine/sample.rs +0 -366
  197. package/rust/core/audio/engine/synth.rs +0 -325
  198. package/rust/core/audio/interpreter/arrow_call.rs +0 -311
  199. package/rust/core/audio/renderer.rs +0 -54
  200. package/rust/core/parser/driver.rs +0 -584
  201. package/rust/core/preprocessor/loader.rs +0 -637
  202. package/rust/core/store/export.rs +0 -28
  203. package/rust/core/store/function.rs +0 -40
  204. package/rust/core/store/import.rs +0 -28
  205. package/rust/core/store/variable.rs +0 -51
  206. package/rust/core/utils/mod.rs +0 -1
  207. package/rust/core/utils/path.rs +0 -37
@@ -1,542 +1,574 @@
1
- use devalang_types::Value;
2
- use devalang_utils::logger::{LogLevel, Logger};
3
- use rayon::prelude::*;
4
-
5
- use crate::core::{
6
- audio::{
7
- engine::AudioEngine,
8
- interpreter::{
9
- arrow_call::interprete_call_arrow_statement, call::interprete_call_statement,
10
- function::interprete_function_statement, let_::interprete_let_statement,
11
- load::interprete_load_statement, loop_::interprete_loop_statement,
12
- sleep::interprete_sleep_statement, spawn::interprete_spawn_statement,
13
- tempo::interprete_tempo_statement, trigger::interprete_trigger_statement,
14
- },
15
- },
16
- parser::statement::{Statement, StatementKind},
17
- store::{function::FunctionTable, global::GlobalStore, variable::VariableTable},
18
- };
19
-
20
- // WASM playhead callback support (only compiled for wasm32 target)
21
- #[cfg(target_arch = "wasm32")]
22
- use serde::Serialize;
23
-
24
- #[cfg(target_arch = "wasm32")]
25
- use wasm_bindgen::prelude::JsValue;
26
-
27
- #[cfg(target_arch = "wasm32")]
28
- use std::cell::RefCell;
29
-
30
- #[cfg(target_arch = "wasm32")]
31
- thread_local! {
32
- static PLAYHEAD_CB: RefCell<Option<js_sys::Function>> = RefCell::new(None);
33
- }
34
-
35
- #[cfg(target_arch = "wasm32")]
36
- pub fn register_playhead_callback(cb: js_sys::Function) {
37
- PLAYHEAD_CB.with(|c| *c.borrow_mut() = Some(cb));
38
- }
39
-
40
- #[cfg(target_arch = "wasm32")]
41
- pub fn unregister_playhead_callback() {
42
- PLAYHEAD_CB.with(|c| *c.borrow_mut() = None);
43
- }
44
-
45
- #[cfg(target_arch = "wasm32")]
46
- fn emit_playhead(time: f32, line: usize, column: usize) {
47
- #[derive(Serialize)]
48
- struct PlayheadEvent {
49
- time: f32,
50
- line: usize,
51
- column: usize,
52
- }
53
-
54
- let ev = PlayheadEvent { time, line, column };
55
- if let Ok(v) = serde_wasm_bindgen::to_value(&ev) {
56
- PLAYHEAD_CB.with(|c| {
57
- if let Some(f) = c.borrow().as_ref() {
58
- let _ = f.call1(&JsValue::NULL, &v);
59
- }
60
- });
61
- }
62
- }
63
-
64
- pub fn run_audio_program(
65
- statements: &[Statement],
66
- audio_engine: &mut AudioEngine,
67
- _entry: String,
68
- _output: String,
69
- _module_variables: VariableTable,
70
- _module_functions: FunctionTable,
71
- global_store: &mut GlobalStore,
72
- ) -> (f32, f32) {
73
- let base_bpm = 120.0;
74
- let base_duration = 60.0 / base_bpm;
75
-
76
- let (max_end_time, cursor_time) = execute_audio_block(
77
- audio_engine,
78
- global_store,
79
- global_store.variables.clone(),
80
- global_store.functions.clone(),
81
- statements,
82
- base_bpm,
83
- base_duration,
84
- 0.0,
85
- 0.0,
86
- );
87
-
88
- (max_end_time, cursor_time)
89
- }
90
-
91
- /// Execute a block of statements and schedule audio into the provided
92
- /// AudioEngine. This function is the core of offline rendering and
93
- /// performs the following responsibilities:
94
- /// - sequential evaluation of statements (load, let, trigger, loop, etc.)
95
- /// - parallel execution of `spawn` blocks (using rayon) and merging results
96
- /// - scheduling of periodic events (beat/bar) once at the root depth
97
- /// - emitting playhead events (when compiled for wasm32) after each sequential statement
98
- pub fn execute_audio_block(
99
- audio_engine: &mut AudioEngine,
100
- global_store: &GlobalStore,
101
- mut variable_table: VariableTable,
102
- mut functions_table: FunctionTable,
103
- statements: &[Statement],
104
- mut base_bpm: f32,
105
- mut base_duration: f32,
106
- mut max_end_time: f32,
107
- mut cursor_time: f32,
108
- ) -> (f32, f32) {
109
- // Track nested depth of execute_audio_block to avoid scheduling periodic events multiple times
110
- let current_depth = match variable_table.get("__depth") {
111
- Some(Value::Number(n)) => *n,
112
- _ => 0.0,
113
- };
114
- variable_table.set("__depth".to_string(), Value::Number(current_depth + 1.0));
115
- let (spawns, others): (Vec<_>, Vec<_>) = statements
116
- .iter()
117
- .partition(|stmt| matches!(stmt.kind, StatementKind::Spawn { .. }));
118
-
119
- // Execute sequential statements first
120
- for stmt in others {
121
- match &stmt.kind {
122
- StatementKind::Load { .. } => {
123
- if let Some(new_table) = interprete_load_statement(stmt, &mut variable_table) {
124
- variable_table = new_table;
125
- }
126
- }
127
- StatementKind::On { .. } => {
128
- // already registered in global store during parsing; nothing to do at runtime
129
- }
130
- StatementKind::Emit { event, payload: _ } => {
131
- if let Some(handlers) = global_store.get_event_handlers(event) {
132
- for h in handlers {
133
- if let StatementKind::On {
134
- event: _,
135
- args,
136
- body,
137
- } = &h.kind
138
- {
139
- // Create a derived variable table with event context
140
- let mut vt = variable_table.clone();
141
- let mut ctx = std::collections::HashMap::new();
142
- ctx.insert("name".to_string(), Value::String(event.clone()));
143
- if let Some(arg_list) = args.clone() {
144
- ctx.insert("args".to_string(), Value::Array(arg_list));
145
- }
146
- // Attach payload if any on the Emit statement value
147
- ctx.insert("payload".to_string(), stmt.value.clone());
148
- vt.set("event".to_string(), Value::Map(ctx));
149
- // Mark we're inside an event handler to avoid re-scheduling periodic events recursively
150
- vt.set("__in_event".to_string(), Value::Boolean(true));
151
-
152
- let (_max, _cursor) = execute_audio_block(
153
- audio_engine,
154
- global_store,
155
- vt,
156
- functions_table.clone(),
157
- body,
158
- base_bpm,
159
- base_duration,
160
- max_end_time,
161
- cursor_time,
162
- );
163
- }
164
- }
165
- }
166
- }
167
- StatementKind::Let { .. } => {
168
- if let Some(new_table) = interprete_let_statement(stmt, &mut variable_table) {
169
- variable_table = new_table;
170
- }
171
- }
172
- StatementKind::Function { .. } => {
173
- if let Some(new_functions) =
174
- interprete_function_statement(stmt, &mut functions_table)
175
- {
176
- functions_table = new_functions;
177
- }
178
- }
179
- StatementKind::Tempo => {
180
- if let Some((new_bpm, new_duration)) = interprete_tempo_statement(stmt) {
181
- base_bpm = new_bpm;
182
- base_duration = new_duration;
183
- }
184
- }
185
- StatementKind::Trigger { .. } => {
186
- if let Some((new_cursor, new_max, _)) = interprete_trigger_statement(
187
- stmt,
188
- audio_engine,
189
- &variable_table,
190
- base_duration,
191
- cursor_time,
192
- max_end_time,
193
- ) {
194
- cursor_time = new_cursor;
195
- max_end_time = new_max;
196
- }
197
- }
198
- StatementKind::Sleep => {
199
- let (new_cursor, new_max) =
200
- interprete_sleep_statement(stmt, cursor_time, max_end_time);
201
- cursor_time = new_cursor;
202
- max_end_time = new_max;
203
- }
204
- StatementKind::Loop => {
205
- let (new_max, new_cursor) = interprete_loop_statement(
206
- stmt,
207
- audio_engine,
208
- global_store,
209
- &variable_table,
210
- &functions_table,
211
- base_bpm,
212
- base_duration,
213
- max_end_time,
214
- cursor_time,
215
- );
216
- cursor_time = new_cursor;
217
- max_end_time = new_max;
218
- }
219
- StatementKind::Call { .. } => {
220
- let (new_max, _) = interprete_call_statement(
221
- stmt,
222
- audio_engine,
223
- &variable_table,
224
- &functions_table,
225
- global_store,
226
- base_bpm,
227
- base_duration,
228
- max_end_time,
229
- cursor_time,
230
- );
231
- cursor_time = new_max;
232
- max_end_time = new_max;
233
- }
234
- StatementKind::ArrowCall { .. } => {
235
- let (new_max, new_cursor) = interprete_call_arrow_statement(
236
- stmt,
237
- audio_engine,
238
- &variable_table,
239
- global_store,
240
- base_bpm,
241
- base_duration,
242
- &mut max_end_time,
243
- Some(&mut cursor_time),
244
- true,
245
- );
246
- cursor_time = new_cursor;
247
-
248
- if new_max > max_end_time {
249
- max_end_time = new_max;
250
- }
251
- }
252
- StatementKind::Automate { .. } => {
253
- if let Some(new_table) =
254
- crate::core::audio::interpreter::automate::interprete_automate_statement(
255
- stmt,
256
- &mut variable_table,
257
- )
258
- {
259
- variable_table = new_table;
260
- }
261
- }
262
- StatementKind::Print => {
263
- // Only print in real-time mode (during playback), not during offline render.
264
- let is_realtime = matches!(variable_table.get("__rt"), Some(Value::Boolean(true)));
265
- if is_realtime {
266
- let logger = Logger::new();
267
- match &stmt.value {
268
- Value::String(s) => {
269
- let bpm = if let Some(Value::Number(n)) = variable_table.get("bpm") {
270
- *n
271
- } else {
272
- 120.0
273
- };
274
- let beat = if let Some(Value::Number(n)) = variable_table.get("beat") {
275
- *n
276
- } else {
277
- 0.0
278
- };
279
- // First try JS-like string concatenation: "str " + var + 1 + $env.*
280
- if let Some(res) =
281
- crate::core::audio::evaluator::evaluate_string_expression(
282
- s,
283
- &variable_table,
284
- bpm,
285
- beat,
286
- )
287
- {
288
- logger.log_message(LogLevel::Print, &res);
289
- } else if let Some(val) = variable_table.get(s) {
290
- logger.log_message(LogLevel::Print, &format!("{:?}", val));
291
- } else if s.contains("$env")
292
- || s.contains("$math")
293
- || s.parse::<f32>().is_ok()
294
- {
295
- let v = crate::core::audio::evaluator::evaluate_rhs_into_value(
296
- s,
297
- &variable_table,
298
- bpm,
299
- beat,
300
- );
301
- match v {
302
- Value::Number(n) => {
303
- logger.log_message(LogLevel::Print, &format!("{}", n));
304
- }
305
- _ => logger.log_message(LogLevel::Print, s),
306
- }
307
- } else {
308
- logger.log_message(LogLevel::Print, s);
309
- }
310
- }
311
- Value::Number(n) => {
312
- logger.log_message(LogLevel::Print, &format!("{}", n));
313
- }
314
- Value::Identifier(name) => {
315
- if let Some(val) = variable_table.get(name) {
316
- match val {
317
- Value::Number(n) => {
318
- logger.log_message(LogLevel::Print, &format!("{}", n));
319
- }
320
- Value::String(s) => logger.log_message(LogLevel::Print, s),
321
- Value::Boolean(b) => {
322
- logger.log_message(LogLevel::Print, &format!("{}", b));
323
- }
324
- other => {
325
- logger
326
- .log_message(LogLevel::Print, &format!("{:?}", other));
327
- }
328
- }
329
- } else {
330
- logger.log_message(LogLevel::Print, name);
331
- }
332
- }
333
- v => logger.log_message(LogLevel::Print, &format!("{:?}", v)),
334
- }
335
- }
336
- }
337
- _ => {}
338
- }
339
-
340
- // Emit playhead event for UI bindings when building real-time playback
341
- #[cfg(target_arch = "wasm32")]
342
- {
343
- emit_playhead(cursor_time, stmt.line, stmt.column);
344
- }
345
- }
346
-
347
- // Execute parallel spawns (collect results)
348
- let spawn_results: Vec<(AudioEngine, f32)> = spawns
349
- .par_iter()
350
- .map(|stmt| {
351
- let mut local_engine = AudioEngine::new(audio_engine.module_name.clone());
352
- let (spawn_max, _) = interprete_spawn_statement(
353
- stmt,
354
- &mut local_engine,
355
- &variable_table,
356
- &functions_table,
357
- global_store,
358
- base_bpm,
359
- base_duration,
360
- 0.0,
361
- 0.0,
362
- );
363
- (local_engine, spawn_max)
364
- })
365
- .collect();
366
-
367
- // Finally, merge results from all spawns
368
- for (local_engine, spawn_max) in spawn_results {
369
- audio_engine.merge_with(local_engine);
370
- if spawn_max > max_end_time {
371
- max_end_time = spawn_max;
372
- }
373
- }
374
-
375
- // Built-in periodic events (e.g., on beat(n), on bar(n))
376
- // Emit handlers across the timeline up to max_end_time.
377
- // If no audio was scheduled (max_end_time == 0.0), skip.
378
- // Don't schedule periodic events if we're already inside an event handler
379
- let in_event = matches!(variable_table.get("__in_event"), Some(Value::Boolean(true)));
380
- let depth_is_root = matches!(variable_table.get("__depth"), Some(Value::Number(n)) if (*n - 1.0).abs() < f32::EPSILON);
381
- if max_end_time > 0.0 && !in_event && depth_is_root && !global_store.events.is_empty() {
382
- // Beat-based handlers (support "beat" and "$beat")
383
- for ev_key in ["beat", "$beat"] {
384
- if let Some(handlers) = global_store.get_event_handlers(ev_key) {
385
- let mut seen: std::collections::HashSet<(usize, usize, usize)> =
386
- std::collections::HashSet::new();
387
- // Default every 1 beat if args missing
388
- for h in handlers {
389
- let key = (h.line, h.column, h.indent);
390
- if !seen.insert(key) {
391
- continue;
392
- }
393
- if let StatementKind::On { event, args, body } = &h.kind {
394
- let every: f32 = args
395
- .as_ref()
396
- .and_then(|v| v.first())
397
- .and_then(|x| {
398
- match x {
399
- Value::Number(n) => Some(*n),
400
- Value::Identifier(s) => {
401
- // Try to resolve from variables first, fallback to parsing the literal
402
- match variable_table.get(s) {
403
- Some(Value::Number(n)) => Some(*n),
404
- _ => s.parse::<f32>().ok(),
405
- }
406
- }
407
- _ => None,
408
- }
409
- })
410
- .unwrap_or(1.0)
411
- .max(0.0001);
412
- let step = base_duration * every;
413
- // Start from first full bar boundary after t=0
414
- let mut t = step;
415
- while t <= max_end_time {
416
- // Prepare event context
417
- let mut vt = variable_table.clone();
418
- let mut ctx = std::collections::HashMap::new();
419
- ctx.insert("name".to_string(), Value::String(event.clone()));
420
- if let Some(a) = args.clone() {
421
- ctx.insert("args".to_string(), Value::Array(a));
422
- }
423
- vt.set("event".to_string(), Value::Map(ctx));
424
- vt.set("beat".to_string(), Value::Number(t / base_duration));
425
- // Prevent nested scheduling
426
- vt.set("__in_event".to_string(), Value::Boolean(true));
427
-
428
- let (_m, _c) = execute_audio_block(
429
- audio_engine,
430
- global_store,
431
- vt,
432
- functions_table.clone(),
433
- body,
434
- base_bpm,
435
- base_duration,
436
- max_end_time,
437
- t,
438
- );
439
-
440
- t += step;
441
- }
442
- }
443
- }
444
- }
445
- }
446
-
447
- // Bar-based handlers (default 4/4 time => 4 beats per bar); support "bar" and "$bar"
448
- for ev_key in ["bar", "$bar"] {
449
- if let Some(handlers) = global_store.get_event_handlers(ev_key) {
450
- let mut seen: std::collections::HashSet<(usize, usize, usize)> =
451
- std::collections::HashSet::new();
452
- for h in handlers {
453
- let key = (h.line, h.column, h.indent);
454
- if !seen.insert(key) {
455
- continue;
456
- }
457
- if let StatementKind::On { event, args, body } = &h.kind {
458
- let bar_beats = 4.0f32; // TODO: time signature support
459
- let first_only = args.as_ref().and_then(|v| v.first()).is_none();
460
-
461
- let every_bar: f32 = if first_only {
462
- 1.0
463
- } else {
464
- args.as_ref()
465
- .and_then(|v| v.first())
466
- .and_then(|x| match x {
467
- Value::Number(n) => Some(*n),
468
- Value::Identifier(s) => match variable_table.get(s) {
469
- Some(Value::Number(n)) => Some(*n),
470
- _ => s.parse::<f32>().ok(),
471
- },
472
- _ => None,
473
- })
474
- .unwrap_or(1.0)
475
- .max(0.0001)
476
- };
477
-
478
- let step = base_duration * bar_beats * every_bar;
479
-
480
- if first_only {
481
- let t = step; // first full bar after t=0
482
- if t <= max_end_time {
483
- let mut vt = variable_table.clone();
484
- let mut ctx = std::collections::HashMap::new();
485
- ctx.insert("name".to_string(), Value::String(event.clone()));
486
- if let Some(a) = args.clone() {
487
- ctx.insert("args".to_string(), Value::Array(a));
488
- }
489
- vt.set("event".to_string(), Value::Map(ctx));
490
- vt.set("beat".to_string(), Value::Number(t / base_duration));
491
- // Prevent nested scheduling
492
- vt.set("__in_event".to_string(), Value::Boolean(true));
493
-
494
- let (_m, _c) = execute_audio_block(
495
- audio_engine,
496
- global_store,
497
- vt,
498
- functions_table.clone(),
499
- body,
500
- base_bpm,
501
- base_duration,
502
- max_end_time,
503
- t,
504
- );
505
- }
506
- } else {
507
- let mut t = step; // start from first full bar after t=0
508
- while t <= max_end_time {
509
- let mut vt = variable_table.clone();
510
- let mut ctx = std::collections::HashMap::new();
511
- ctx.insert("name".to_string(), Value::String(event.clone()));
512
- if let Some(a) = args.clone() {
513
- ctx.insert("args".to_string(), Value::Array(a));
514
- }
515
- vt.set("event".to_string(), Value::Map(ctx));
516
- vt.set("beat".to_string(), Value::Number(t / base_duration));
517
- // Prevent nested scheduling
518
- vt.set("__in_event".to_string(), Value::Boolean(true));
519
-
520
- let (_m, _c) = execute_audio_block(
521
- audio_engine,
522
- global_store,
523
- vt,
524
- functions_table.clone(),
525
- body,
526
- base_bpm,
527
- base_duration,
528
- max_end_time,
529
- t,
530
- );
531
-
532
- t += step;
533
- }
534
- }
535
- }
536
- }
537
- }
538
- }
539
- }
540
-
541
- (max_end_time.max(cursor_time), cursor_time)
542
- }
1
+ use devalang_types::Value;
2
+ use devalang_utils::logger::{LogLevel, Logger};
3
+ use rayon::prelude::*;
4
+
5
+ use crate::core::{
6
+ audio::{
7
+ engine::AudioEngine,
8
+ interpreter::statements::{
9
+ arrow_call::interprete::interprete_arrow_call_statement,
10
+ call::interprete_call_statement, function::interprete_function_statement,
11
+ let_::interprete_let_statement, load::interprete_load_statement,
12
+ loop_::interprete_loop_statement, sleep::interprete_sleep_statement,
13
+ spawn::interprete_spawn_statement, tempo::interprete_tempo_statement,
14
+ trigger::interprete_trigger_statement,
15
+ },
16
+ },
17
+ parser::statement::{Statement, StatementKind},
18
+ store::global::GlobalStore,
19
+ };
20
+ use devalang_types::{FunctionTable, VariableTable};
21
+
22
+ // WASM playhead callback support (only compiled for wasm32 target)
23
+ #[cfg(target_arch = "wasm32")]
24
+ use serde::Serialize;
25
+
26
+ #[cfg(target_arch = "wasm32")]
27
+ #[derive(Serialize, Clone)]
28
+ pub struct PlayheadEvent {
29
+ time: f32,
30
+ line: usize,
31
+ column: usize,
32
+ }
33
+
34
+ #[cfg(target_arch = "wasm32")]
35
+ use wasm_bindgen::prelude::JsValue;
36
+
37
+ #[cfg(target_arch = "wasm32")]
38
+ use std::cell::RefCell;
39
+
40
+ #[cfg(target_arch = "wasm32")]
41
+ thread_local! {
42
+ static PLAYHEAD_CB: RefCell<Option<js_sys::Function>> = RefCell::new(None);
43
+ static PLAYHEAD_EVENTS: RefCell<Vec<PlayheadEvent>> = RefCell::new(Vec::new());
44
+ }
45
+
46
+ #[cfg(target_arch = "wasm32")]
47
+ pub fn register_playhead_callback(cb: js_sys::Function) {
48
+ PLAYHEAD_CB.with(|c| {
49
+ *c.borrow_mut() = Some(cb);
50
+ });
51
+ }
52
+
53
+ #[cfg(target_arch = "wasm32")]
54
+ pub fn unregister_playhead_callback() {
55
+ PLAYHEAD_CB.with(|c| {
56
+ *c.borrow_mut() = None;
57
+ });
58
+ }
59
+
60
+ #[cfg(target_arch = "wasm32")]
61
+ pub fn collect_playhead_events() -> Vec<PlayheadEvent> {
62
+ PLAYHEAD_EVENTS.with(|c| c.borrow().clone())
63
+ }
64
+
65
+ #[cfg(target_arch = "wasm32")]
66
+ fn emit_playhead(time: f32, line: usize, column: usize) {
67
+ let ev = PlayheadEvent { time, line, column };
68
+ PLAYHEAD_EVENTS.with(|c| c.borrow_mut().push(ev.clone()));
69
+
70
+ if let Ok(v) = serde_wasm_bindgen::to_value(&ev) {
71
+ PLAYHEAD_CB.with(|c| {
72
+ if let Some(f) = c.borrow().as_ref() {
73
+ let _ = f.call1(&JsValue::NULL, &v);
74
+ }
75
+ });
76
+ }
77
+ }
78
+
79
+ pub fn run_audio_program(
80
+ statements: &[Statement],
81
+ audio_engine: &mut AudioEngine,
82
+ _entry: String,
83
+ _output: String,
84
+ _module_variables: VariableTable,
85
+ _module_functions: FunctionTable,
86
+ global_store: &mut GlobalStore,
87
+ ) -> (f32, f32) {
88
+ // Clear any previously collected playhead events for wasm target so each
89
+ // run starts with an empty events buffer.
90
+ #[cfg(target_arch = "wasm32")]
91
+ {
92
+ PLAYHEAD_EVENTS.with(|c| c.borrow_mut().clear());
93
+ }
94
+
95
+ let base_bpm = 120.0;
96
+ let base_duration = 60.0 / base_bpm;
97
+
98
+ let (max_end_time, cursor_time) = execute_audio_block(
99
+ audio_engine,
100
+ global_store,
101
+ global_store.variables.clone(),
102
+ global_store.functions.clone(),
103
+ statements,
104
+ base_bpm,
105
+ base_duration,
106
+ 0.0,
107
+ 0.0,
108
+ );
109
+
110
+ (max_end_time, cursor_time)
111
+ }
112
+
113
+ /// Execute a block of statements and schedule audio into the provided
114
+ /// AudioEngine. This function is the core of offline rendering and
115
+ /// performs the following responsibilities:
116
+ /// - sequential evaluation of statements (load, let, trigger, loop, etc.)
117
+ /// - parallel execution of `spawn` blocks (using rayon) and merging results
118
+ /// - scheduling of periodic events (beat/bar) once at the root depth
119
+ /// - emitting playhead events (when compiled for wasm32) after each sequential statement
120
+ pub fn execute_audio_block(
121
+ audio_engine: &mut AudioEngine,
122
+ global_store: &GlobalStore,
123
+ mut variable_table: VariableTable,
124
+ mut functions_table: FunctionTable,
125
+ statements: &[Statement],
126
+ mut base_bpm: f32,
127
+ mut base_duration: f32,
128
+ mut max_end_time: f32,
129
+ mut cursor_time: f32,
130
+ ) -> (f32, f32) {
131
+ // Track nested depth of execute_audio_block to avoid scheduling periodic events multiple times
132
+ let current_depth = match variable_table.get("__depth") {
133
+ Some(Value::Number(n)) => *n,
134
+ _ => 0.0,
135
+ };
136
+ variable_table.set("__depth".to_string(), Value::Number(current_depth + 1.0));
137
+ let (spawns, others): (Vec<_>, Vec<_>) = statements
138
+ .iter()
139
+ .partition(|stmt| matches!(stmt.kind, StatementKind::Spawn { .. }));
140
+
141
+ // Execute sequential statements first
142
+ for stmt in others {
143
+ match &stmt.kind {
144
+ StatementKind::Load { .. } => {
145
+ if let Some(new_table) = interprete_load_statement(stmt, &mut variable_table) {
146
+ variable_table = new_table;
147
+ }
148
+ }
149
+ StatementKind::On { .. } => {
150
+ // already registered in global store during parsing; nothing to do at runtime
151
+ }
152
+ StatementKind::Emit { event, payload: _ } => {
153
+ if let Some(handlers) = global_store.get_event_handlers(event) {
154
+ for h in handlers {
155
+ if let StatementKind::On {
156
+ event: _,
157
+ args,
158
+ body,
159
+ } = &h.kind
160
+ {
161
+ // Create a derived variable table with event context
162
+ let mut vt = variable_table.clone();
163
+ let mut ctx = std::collections::HashMap::new();
164
+ ctx.insert("name".to_string(), Value::String(event.clone()));
165
+ if let Some(arg_list) = args.clone() {
166
+ ctx.insert("args".to_string(), Value::Array(arg_list));
167
+ }
168
+ // Attach payload if any on the Emit statement value
169
+ ctx.insert("payload".to_string(), stmt.value.clone());
170
+ vt.set("event".to_string(), Value::Map(ctx));
171
+ // Mark we're inside an event handler to avoid re-scheduling periodic events recursively
172
+ vt.set("__in_event".to_string(), Value::Boolean(true));
173
+
174
+ let (_max, _cursor) = execute_audio_block(
175
+ audio_engine,
176
+ global_store,
177
+ vt,
178
+ functions_table.clone(),
179
+ body,
180
+ base_bpm,
181
+ base_duration,
182
+ max_end_time,
183
+ cursor_time,
184
+ );
185
+ }
186
+ }
187
+ }
188
+ }
189
+ StatementKind::Let { .. } => {
190
+ if let Some(new_table) = interprete_let_statement(stmt, &mut variable_table) {
191
+ variable_table = new_table;
192
+ }
193
+ }
194
+ StatementKind::Function { .. } => {
195
+ if let Some(new_functions) =
196
+ interprete_function_statement(stmt, &mut functions_table)
197
+ {
198
+ functions_table = new_functions;
199
+ }
200
+ }
201
+ StatementKind::Tempo => {
202
+ if let Some((new_bpm, new_duration)) = interprete_tempo_statement(stmt) {
203
+ base_bpm = new_bpm;
204
+ base_duration = new_duration;
205
+ }
206
+ }
207
+ StatementKind::Trigger { .. } => {
208
+ if let Some((new_cursor, new_max, _)) = interprete_trigger_statement(
209
+ stmt,
210
+ audio_engine,
211
+ &variable_table,
212
+ base_duration,
213
+ cursor_time,
214
+ max_end_time,
215
+ ) {
216
+ cursor_time = new_cursor;
217
+ max_end_time = new_max;
218
+ }
219
+ }
220
+ StatementKind::Sleep => {
221
+ let (new_cursor, new_max) =
222
+ interprete_sleep_statement(stmt, cursor_time, max_end_time);
223
+ cursor_time = new_cursor;
224
+ max_end_time = new_max;
225
+ }
226
+ StatementKind::Loop => {
227
+ let (new_max, new_cursor) = interprete_loop_statement(
228
+ stmt,
229
+ audio_engine,
230
+ global_store,
231
+ &variable_table,
232
+ &functions_table,
233
+ base_bpm,
234
+ base_duration,
235
+ max_end_time,
236
+ cursor_time,
237
+ );
238
+ cursor_time = new_cursor;
239
+ max_end_time = new_max;
240
+ }
241
+ StatementKind::Call { .. } => {
242
+ let (new_max, _) = interprete_call_statement(
243
+ stmt,
244
+ audio_engine,
245
+ &variable_table,
246
+ &functions_table,
247
+ global_store,
248
+ base_bpm,
249
+ base_duration,
250
+ max_end_time,
251
+ cursor_time,
252
+ );
253
+ cursor_time = new_max;
254
+ max_end_time = new_max;
255
+ }
256
+ StatementKind::ArrowCall { .. } => {
257
+ let (new_max, new_cursor) = interprete_arrow_call_statement(
258
+ stmt,
259
+ audio_engine,
260
+ &variable_table,
261
+ global_store,
262
+ base_bpm,
263
+ base_duration,
264
+ &mut max_end_time,
265
+ Some(&mut cursor_time),
266
+ true,
267
+ );
268
+ cursor_time = new_cursor;
269
+
270
+ if new_max > max_end_time {
271
+ max_end_time = new_max;
272
+ }
273
+ }
274
+ StatementKind::Automate { .. } => {
275
+ if
276
+ let Some(new_table) =
277
+ crate::core::audio::interpreter::statements::automate::interprete_automate_statement(
278
+ stmt,
279
+ &mut variable_table
280
+ )
281
+ {
282
+ variable_table = new_table;
283
+ }
284
+ }
285
+ StatementKind::Print => {
286
+ // Only print in real-time mode (during playback), not during offline render.
287
+ let is_realtime = matches!(variable_table.get("__rt"), Some(Value::Boolean(true)));
288
+ if is_realtime {
289
+ let logger = Logger::new();
290
+ match &stmt.value {
291
+ Value::String(s) => {
292
+ let bpm = if let Some(Value::Number(n)) = variable_table.get("bpm") {
293
+ *n
294
+ } else {
295
+ 120.0
296
+ };
297
+ let beat = if let Some(Value::Number(n)) = variable_table.get("beat") {
298
+ *n
299
+ } else {
300
+ 0.0
301
+ };
302
+ // First try JS-like string concatenation: "str " + var + 1 + $env.*
303
+ if let Some(res) =
304
+ crate::core::audio::evaluator::evaluate_string_expression(
305
+ s,
306
+ &variable_table,
307
+ bpm,
308
+ beat,
309
+ )
310
+ {
311
+ logger.log_message(LogLevel::Print, &res);
312
+ } else if let Some(val) = variable_table.get(s) {
313
+ logger.log_message(LogLevel::Print, &format!("{:?}", val));
314
+ } else if s.contains("$env")
315
+ || s.contains("$math")
316
+ || s.parse::<f32>().is_ok()
317
+ {
318
+ let v = crate::core::audio::evaluator::evaluate_rhs_into_value(
319
+ s,
320
+ &variable_table,
321
+ bpm,
322
+ beat,
323
+ );
324
+ match v {
325
+ Value::Number(n) => {
326
+ logger.log_message(LogLevel::Print, &format!("{}", n));
327
+ }
328
+ _ => logger.log_message(LogLevel::Print, s),
329
+ }
330
+ } else {
331
+ logger.log_message(LogLevel::Print, s);
332
+ }
333
+ }
334
+ Value::Number(n) => {
335
+ logger.log_message(LogLevel::Print, &format!("{}", n));
336
+ }
337
+ Value::Identifier(name) => {
338
+ if let Some(val) = variable_table.get(name) {
339
+ match val {
340
+ Value::Number(n) => {
341
+ logger.log_message(LogLevel::Print, &format!("{}", n));
342
+ }
343
+ Value::String(s) => logger.log_message(LogLevel::Print, s),
344
+ Value::Boolean(b) => {
345
+ logger.log_message(LogLevel::Print, &format!("{}", b));
346
+ }
347
+ other => {
348
+ logger
349
+ .log_message(LogLevel::Print, &format!("{:?}", other));
350
+ }
351
+ }
352
+ } else {
353
+ logger.log_message(LogLevel::Print, name);
354
+ }
355
+ }
356
+ v => logger.log_message(LogLevel::Print, &format!("{:?}", v)),
357
+ }
358
+ }
359
+ }
360
+ _ => {}
361
+ }
362
+
363
+ // Emit playhead event for UI bindings when building real-time playback
364
+ // Only emit for statements that are "playable" (i.e., schedule audio)
365
+ #[cfg(target_arch = "wasm32")]
366
+ {
367
+ if matches!(
368
+ stmt.kind,
369
+ StatementKind::Trigger { .. }
370
+ | StatementKind::Call { .. }
371
+ | StatementKind::Spawn { .. }
372
+ | StatementKind::Loop
373
+ ) {
374
+ emit_playhead(cursor_time, stmt.line, stmt.column);
375
+ }
376
+ }
377
+ }
378
+
379
+ // Execute parallel spawns (collect results)
380
+ let spawn_results: Vec<(AudioEngine, f32)> = spawns
381
+ .par_iter()
382
+ .map(|stmt| {
383
+ let mut local_engine = AudioEngine::new(audio_engine.module_name.clone());
384
+ let (spawn_max, _) = interprete_spawn_statement(
385
+ stmt,
386
+ &mut local_engine,
387
+ &variable_table,
388
+ &functions_table,
389
+ global_store,
390
+ base_bpm,
391
+ base_duration,
392
+ 0.0,
393
+ 0.0,
394
+ );
395
+ (local_engine, spawn_max)
396
+ })
397
+ .collect();
398
+
399
+ // Finally, merge results from all spawns
400
+ for (local_engine, spawn_max) in spawn_results {
401
+ audio_engine.merge_with(local_engine);
402
+ if spawn_max > max_end_time {
403
+ max_end_time = spawn_max;
404
+ }
405
+ }
406
+
407
+ // Built-in periodic events (e.g., on beat(n), on bar(n))
408
+ // Emit handlers across the timeline up to max_end_time.
409
+ // If no audio was scheduled (max_end_time == 0.0), skip.
410
+ // Don't schedule periodic events if we're already inside an event handler
411
+ let in_event = matches!(variable_table.get("__in_event"), Some(Value::Boolean(true)));
412
+ let depth_is_root = matches!(variable_table.get("__depth"), Some(Value::Number(n)) if (*n - 1.0).abs() < f32::EPSILON);
413
+ if max_end_time > 0.0 && !in_event && depth_is_root && !global_store.events.is_empty() {
414
+ // Beat-based handlers (support "beat" and "$beat")
415
+ for ev_key in ["beat", "$beat"] {
416
+ if let Some(handlers) = global_store.get_event_handlers(ev_key) {
417
+ let mut seen: std::collections::HashSet<(usize, usize, usize)> =
418
+ std::collections::HashSet::new();
419
+ // Default every 1 beat if args missing
420
+ for h in handlers {
421
+ let key = (h.line, h.column, h.indent);
422
+ if !seen.insert(key) {
423
+ continue;
424
+ }
425
+ if let StatementKind::On { event, args, body } = &h.kind {
426
+ let every: f32 = args
427
+ .as_ref()
428
+ .and_then(|v| v.first())
429
+ .and_then(|x| {
430
+ match x {
431
+ Value::Number(n) => Some(*n),
432
+ Value::Identifier(s) => {
433
+ // Try to resolve from variables first, fallback to parsing the literal
434
+ match variable_table.get(s) {
435
+ Some(Value::Number(n)) => Some(*n),
436
+ _ => s.parse::<f32>().ok(),
437
+ }
438
+ }
439
+ _ => None,
440
+ }
441
+ })
442
+ .unwrap_or(1.0)
443
+ .max(0.0001);
444
+ let step = base_duration * every;
445
+ // Start from first full bar boundary after t=0
446
+ let mut t = step;
447
+ while t <= max_end_time {
448
+ // Prepare event context
449
+ let mut vt = variable_table.clone();
450
+ let mut ctx = std::collections::HashMap::new();
451
+ ctx.insert("name".to_string(), Value::String(event.clone()));
452
+ if let Some(a) = args.clone() {
453
+ ctx.insert("args".to_string(), Value::Array(a));
454
+ }
455
+ vt.set("event".to_string(), Value::Map(ctx));
456
+ vt.set("beat".to_string(), Value::Number(t / base_duration));
457
+ // Prevent nested scheduling
458
+ vt.set("__in_event".to_string(), Value::Boolean(true));
459
+
460
+ let (_m, _c) = execute_audio_block(
461
+ audio_engine,
462
+ global_store,
463
+ vt,
464
+ functions_table.clone(),
465
+ body,
466
+ base_bpm,
467
+ base_duration,
468
+ max_end_time,
469
+ t,
470
+ );
471
+
472
+ t += step;
473
+ }
474
+ }
475
+ }
476
+ }
477
+ }
478
+
479
+ // Bar-based handlers (default 4/4 time => 4 beats per bar); support "bar" and "$bar"
480
+ for ev_key in ["bar", "$bar"] {
481
+ if let Some(handlers) = global_store.get_event_handlers(ev_key) {
482
+ let mut seen: std::collections::HashSet<(usize, usize, usize)> =
483
+ std::collections::HashSet::new();
484
+ for h in handlers {
485
+ let key = (h.line, h.column, h.indent);
486
+ if !seen.insert(key) {
487
+ continue;
488
+ }
489
+ if let StatementKind::On { event, args, body } = &h.kind {
490
+ let bar_beats = 4.0f32; // TODO: time signature support
491
+ let first_only = args.as_ref().and_then(|v| v.first()).is_none();
492
+
493
+ let every_bar: f32 = if first_only {
494
+ 1.0
495
+ } else {
496
+ args.as_ref()
497
+ .and_then(|v| v.first())
498
+ .and_then(|x| match x {
499
+ Value::Number(n) => Some(*n),
500
+ Value::Identifier(s) => match variable_table.get(s) {
501
+ Some(Value::Number(n)) => Some(*n),
502
+ _ => s.parse::<f32>().ok(),
503
+ },
504
+ _ => None,
505
+ })
506
+ .unwrap_or(1.0)
507
+ .max(0.0001)
508
+ };
509
+
510
+ let step = base_duration * bar_beats * every_bar;
511
+
512
+ if first_only {
513
+ let t = step; // first full bar after t=0
514
+ if t <= max_end_time {
515
+ let mut vt = variable_table.clone();
516
+ let mut ctx = std::collections::HashMap::new();
517
+ ctx.insert("name".to_string(), Value::String(event.clone()));
518
+ if let Some(a) = args.clone() {
519
+ ctx.insert("args".to_string(), Value::Array(a));
520
+ }
521
+ vt.set("event".to_string(), Value::Map(ctx));
522
+ vt.set("beat".to_string(), Value::Number(t / base_duration));
523
+ // Prevent nested scheduling
524
+ vt.set("__in_event".to_string(), Value::Boolean(true));
525
+
526
+ let (_m, _c) = execute_audio_block(
527
+ audio_engine,
528
+ global_store,
529
+ vt,
530
+ functions_table.clone(),
531
+ body,
532
+ base_bpm,
533
+ base_duration,
534
+ max_end_time,
535
+ t,
536
+ );
537
+ }
538
+ } else {
539
+ let mut t = step; // start from first full bar after t=0
540
+ while t <= max_end_time {
541
+ let mut vt = variable_table.clone();
542
+ let mut ctx = std::collections::HashMap::new();
543
+ ctx.insert("name".to_string(), Value::String(event.clone()));
544
+ if let Some(a) = args.clone() {
545
+ ctx.insert("args".to_string(), Value::Array(a));
546
+ }
547
+ vt.set("event".to_string(), Value::Map(ctx));
548
+ vt.set("beat".to_string(), Value::Number(t / base_duration));
549
+ // Prevent nested scheduling
550
+ vt.set("__in_event".to_string(), Value::Boolean(true));
551
+
552
+ let (_m, _c) = execute_audio_block(
553
+ audio_engine,
554
+ global_store,
555
+ vt,
556
+ functions_table.clone(),
557
+ body,
558
+ base_bpm,
559
+ base_duration,
560
+ max_end_time,
561
+ t,
562
+ );
563
+
564
+ t += step;
565
+ }
566
+ }
567
+ }
568
+ }
569
+ }
570
+ }
571
+ }
572
+
573
+ (max_end_time.max(cursor_time), cursor_time)
574
+ }