@devaloop/devalang 0.0.1-alpha.9 → 0.0.1-beta.2

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 (322) hide show
  1. package/.cargo/config.toml +2 -0
  2. package/.devalang +6 -1
  3. package/.github/workflows/ci.yml +103 -0
  4. package/Cargo.toml +81 -48
  5. package/README.md +137 -154
  6. package/docs/CHANGELOG.md +428 -1
  7. package/docs/CONTRIBUTING.md +101 -0
  8. package/docs/ROADMAP.md +14 -7
  9. package/docs/TODO.md +16 -15
  10. package/examples/automation.deva +42 -0
  11. package/examples/bank.deva +7 -0
  12. package/examples/bus.deva +10 -0
  13. package/examples/duration.deva +9 -0
  14. package/examples/effect.deva +2 -0
  15. package/examples/events.deva +12 -0
  16. package/examples/filter.deva +11 -0
  17. package/examples/function.deva +15 -0
  18. package/examples/index.deva +57 -12
  19. package/examples/lfo.deva +9 -0
  20. package/examples/loop.deva +5 -12
  21. package/examples/pattern.deva +8 -0
  22. package/examples/plugin.deva +16 -0
  23. package/examples/synth.deva +11 -1
  24. package/examples/synth_types.deva +17 -0
  25. package/examples/variables.deva +1 -1
  26. package/out-tsc/bin/index.d.ts +2 -0
  27. package/out-tsc/bin/index.js +51 -7
  28. package/out-tsc/core/functions/index.d.ts +42 -0
  29. package/out-tsc/core/functions/index.js +87 -0
  30. package/out-tsc/core/index.d.ts +6 -0
  31. package/out-tsc/core/index.js +22 -0
  32. package/out-tsc/core/types/index.d.ts +4 -0
  33. package/out-tsc/core/types/index.js +20 -0
  34. package/out-tsc/core/types/plugin.d.ts +18 -0
  35. package/out-tsc/core/types/plugin.js +2 -0
  36. package/out-tsc/core/types/result.d.ts +27 -0
  37. package/out-tsc/core/types/result.js +2 -0
  38. package/out-tsc/core/types/statement.d.ts +106 -0
  39. package/out-tsc/core/types/statement.js +2 -0
  40. package/out-tsc/core/types/value.d.ts +43 -0
  41. package/out-tsc/core/types/value.js +2 -0
  42. package/out-tsc/index.d.ts +7 -0
  43. package/out-tsc/index.js +42 -1
  44. package/out-tsc/pkg/devalang_core.d.ts +15 -0
  45. package/out-tsc/pkg/devalang_core.js +65 -0
  46. package/out-tsc/pkg/devalang_core_bg.wasm.d.ts +34 -0
  47. package/out-tsc/scripts/copy-wasm-dts.d.ts +1 -0
  48. package/out-tsc/scripts/copy-wasm-dts.js +73 -0
  49. package/out-tsc/scripts/postinstall.d.ts +1 -0
  50. package/out-tsc/scripts/postinstall.js +83 -0
  51. package/out-tsc/scripts/version/bump.d.ts +1 -0
  52. package/out-tsc/scripts/version/fetch.d.ts +1 -0
  53. package/out-tsc/scripts/version/index.d.ts +1 -0
  54. package/out-tsc/scripts/version/sync.d.ts +1 -0
  55. package/package.json +28 -7
  56. package/project-version.json +4 -4
  57. package/rust/cli/bank/api.rs +122 -0
  58. package/rust/cli/bank/commands.rs +306 -0
  59. package/rust/cli/bank/mod.rs +29 -0
  60. package/rust/cli/build/commands.rs +153 -0
  61. package/rust/cli/build/mod.rs +2 -0
  62. package/rust/cli/build/process.rs +165 -0
  63. package/rust/cli/check/mod.rs +208 -0
  64. package/rust/cli/discover/commands.rs +253 -0
  65. package/rust/cli/discover/config.rs +111 -0
  66. package/rust/cli/discover/fs.rs +19 -0
  67. package/rust/cli/discover/install.rs +103 -0
  68. package/rust/cli/discover/metadata.rs +48 -0
  69. package/rust/cli/discover/mod.rs +5 -0
  70. package/rust/cli/{init.rs → init/commands.rs} +32 -23
  71. package/rust/cli/init/mod.rs +1 -0
  72. package/rust/cli/install/addon.rs +118 -0
  73. package/rust/cli/install/bank.rs +72 -0
  74. package/rust/cli/install/commands.rs +35 -0
  75. package/rust/cli/install/mod.rs +4 -0
  76. package/rust/cli/install/plugin.rs +80 -0
  77. package/rust/cli/login/commands.rs +124 -0
  78. package/rust/cli/login/mod.rs +1 -0
  79. package/rust/cli/mod.rs +9 -202
  80. package/rust/cli/parser.rs +359 -0
  81. package/rust/cli/play/commands.rs +375 -0
  82. package/rust/cli/play/io.rs +17 -0
  83. package/rust/cli/play/mod.rs +5 -0
  84. package/rust/cli/play/process.rs +159 -0
  85. package/rust/cli/play/realtime.rs +91 -0
  86. package/rust/cli/play/utils.rs +23 -0
  87. package/rust/cli/telemetry/commands.rs +22 -0
  88. package/rust/cli/telemetry/event_creator.rs +80 -0
  89. package/rust/cli/telemetry/mod.rs +3 -0
  90. package/rust/cli/telemetry/send.rs +51 -0
  91. package/rust/cli/{template.rs → template/commands.rs} +17 -5
  92. package/rust/cli/template/mod.rs +1 -0
  93. package/rust/cli/update/commands.rs +6 -0
  94. package/rust/cli/update/mod.rs +1 -0
  95. package/rust/config/driver.rs +112 -0
  96. package/rust/config/mod.rs +3 -16
  97. package/rust/config/ops.rs +26 -0
  98. package/rust/config/settings.rs +101 -0
  99. package/rust/core/audio/engine/driver.rs +220 -0
  100. package/rust/core/audio/engine/export.rs +169 -0
  101. package/rust/core/audio/engine/helpers.rs +178 -0
  102. package/rust/core/audio/engine/mod.rs +56 -0
  103. package/rust/core/audio/engine/notes/dsp.rs +85 -0
  104. package/rust/core/audio/engine/notes/mod.rs +44 -0
  105. package/rust/core/audio/engine/notes/params.rs +294 -0
  106. package/rust/core/audio/engine/sample/insert.rs +199 -0
  107. package/rust/core/audio/engine/sample/mod.rs +40 -0
  108. package/rust/core/audio/engine/sample/padding.rs +170 -0
  109. package/rust/core/audio/evaluator/condition.rs +61 -0
  110. package/rust/core/audio/evaluator/mod.rs +9 -0
  111. package/rust/core/audio/evaluator/numeric.rs +152 -0
  112. package/rust/core/audio/evaluator/rhs.rs +16 -0
  113. package/rust/core/audio/evaluator/string_expr.rs +94 -0
  114. package/rust/core/audio/interpreter/driver.rs +574 -216
  115. package/rust/core/audio/interpreter/mod.rs +2 -12
  116. package/rust/core/audio/interpreter/statements/arrow_call/interprete.rs +175 -0
  117. package/rust/core/audio/interpreter/statements/arrow_call/methods/chord.rs +384 -0
  118. package/rust/core/audio/interpreter/statements/arrow_call/methods/mod.rs +2 -0
  119. package/rust/core/audio/interpreter/statements/arrow_call/methods/note.rs +316 -0
  120. package/rust/core/audio/interpreter/statements/arrow_call/mod.rs +3 -0
  121. package/rust/core/audio/interpreter/statements/arrow_call/types/arp.rs +192 -0
  122. package/rust/core/audio/interpreter/statements/arrow_call/types/mod.rs +24 -0
  123. package/rust/core/audio/interpreter/statements/arrow_call/types/pad.rs +116 -0
  124. package/rust/core/audio/interpreter/statements/arrow_call/types/pluck.rs +97 -0
  125. package/rust/core/audio/interpreter/statements/arrow_call/types/sub.rs +100 -0
  126. package/rust/core/audio/interpreter/statements/automate.rs +16 -0
  127. package/rust/core/audio/interpreter/statements/call.rs +295 -0
  128. package/rust/core/audio/interpreter/{condition.rs → statements/condition.rs} +72 -69
  129. package/rust/core/audio/interpreter/statements/function.rs +24 -0
  130. package/rust/core/audio/interpreter/statements/let_.rs +36 -0
  131. package/rust/core/audio/interpreter/statements/load.rs +17 -0
  132. package/rust/core/audio/interpreter/statements/loop_.rs +115 -0
  133. package/rust/core/audio/interpreter/statements/mod.rs +12 -0
  134. package/rust/core/audio/interpreter/statements/sleep.rs +28 -0
  135. package/rust/core/audio/interpreter/statements/spawn.rs +253 -0
  136. package/rust/core/audio/interpreter/statements/tempo.rs +40 -0
  137. package/rust/core/audio/interpreter/statements/trigger.rs +239 -0
  138. package/rust/core/audio/loader/mod.rs +1 -1
  139. package/rust/core/audio/loader/trigger.rs +98 -52
  140. package/rust/core/audio/mod.rs +2 -2
  141. package/rust/core/audio/player.rs +28 -12
  142. package/rust/core/audio/special/easing.rs +189 -0
  143. package/rust/core/audio/special/env.rs +45 -0
  144. package/rust/core/audio/special/math.rs +134 -0
  145. package/rust/core/audio/special/mod.rs +9 -0
  146. package/rust/core/audio/special/modulator.rs +143 -0
  147. package/rust/core/builder/mod.rs +129 -80
  148. package/rust/core/debugger/lexer.rs +4 -4
  149. package/rust/core/debugger/logs.rs +52 -0
  150. package/rust/core/debugger/mod.rs +11 -2
  151. package/rust/core/debugger/preprocessor.rs +4 -4
  152. package/rust/core/debugger/store.rs +38 -25
  153. package/rust/core/error/mod.rs +221 -12
  154. package/rust/core/lexer/driver.rs +59 -0
  155. package/rust/core/lexer/handler/arrow.rs +62 -11
  156. package/rust/core/lexer/handler/at.rs +5 -5
  157. package/rust/core/lexer/handler/brace.rs +11 -11
  158. package/rust/core/lexer/handler/colon.rs +5 -5
  159. package/rust/core/lexer/handler/comment.rs +3 -3
  160. package/rust/core/lexer/handler/dot.rs +6 -6
  161. package/rust/core/lexer/handler/driver.rs +143 -32
  162. package/rust/core/lexer/handler/identifier.rs +11 -5
  163. package/rust/core/lexer/handler/indent.rs +18 -4
  164. package/rust/core/lexer/handler/mod.rs +6 -5
  165. package/rust/core/lexer/handler/newline.rs +3 -3
  166. package/rust/core/lexer/handler/number.rs +5 -5
  167. package/rust/core/lexer/handler/operator.rs +5 -3
  168. package/rust/core/lexer/handler/parenthesis.rs +41 -0
  169. package/rust/core/lexer/handler/slash.rs +21 -0
  170. package/rust/core/lexer/handler/string.rs +3 -3
  171. package/rust/core/lexer/mod.rs +1 -49
  172. package/rust/core/lexer/token.rs +17 -12
  173. package/rust/core/mod.rs +9 -10
  174. package/rust/core/parser/driver/block.rs +111 -0
  175. package/rust/core/parser/driver/cursor.rs +82 -0
  176. package/rust/core/parser/driver/driver_impl.rs +139 -0
  177. package/rust/core/parser/driver/mod.rs +6 -0
  178. package/rust/core/parser/driver/parse_array.rs +120 -0
  179. package/rust/core/parser/driver/parse_map.rs +223 -0
  180. package/rust/core/parser/driver/parser.rs +160 -0
  181. package/rust/core/parser/handler/arrow_call.rs +277 -126
  182. package/rust/core/parser/handler/at.rs +142 -25
  183. package/rust/core/parser/handler/bank.rs +83 -20
  184. package/rust/core/parser/handler/condition.rs +14 -5
  185. package/rust/core/parser/handler/dot.rs +111 -75
  186. package/rust/core/parser/handler/identifier/automate.rs +254 -0
  187. package/rust/core/parser/handler/identifier/call.rs +74 -24
  188. package/rust/core/parser/handler/identifier/emit.rs +70 -0
  189. package/rust/core/parser/handler/identifier/function.rs +113 -0
  190. package/rust/core/parser/handler/identifier/group.rs +28 -14
  191. package/rust/core/parser/handler/identifier/let_.rs +61 -21
  192. package/rust/core/parser/handler/identifier/mod.rs +24 -20
  193. package/rust/core/parser/handler/identifier/on.rs +107 -0
  194. package/rust/core/parser/handler/identifier/print.rs +49 -0
  195. package/rust/core/parser/handler/identifier/sleep.rs +77 -14
  196. package/rust/core/parser/handler/identifier/spawn.rs +81 -31
  197. package/rust/core/parser/handler/identifier/synth.rs +102 -32
  198. package/rust/core/parser/handler/loop_.rs +144 -22
  199. package/rust/core/parser/handler/mod.rs +6 -5
  200. package/rust/core/parser/handler/pattern.rs +74 -0
  201. package/rust/core/parser/handler/tempo.rs +67 -9
  202. package/rust/core/parser/mod.rs +3 -4
  203. package/rust/core/parser/statement.rs +6 -92
  204. package/rust/core/plugin/loader.rs +137 -0
  205. package/rust/core/plugin/mod.rs +2 -0
  206. package/rust/core/plugin/runner/mod.rs +11 -0
  207. package/rust/core/plugin/runner/non_wasm.rs +297 -0
  208. package/rust/core/plugin/runner/wasm32.rs +43 -0
  209. package/rust/core/preprocessor/loader/inject.rs +278 -0
  210. package/rust/core/preprocessor/loader/loader_helpers.rs +110 -0
  211. package/rust/core/preprocessor/loader/mod.rs +235 -0
  212. package/rust/core/preprocessor/mod.rs +4 -4
  213. package/rust/core/preprocessor/module.rs +55 -50
  214. package/rust/core/preprocessor/processor/handlers.rs +107 -0
  215. package/rust/core/preprocessor/processor/mod.rs +1 -0
  216. package/rust/core/preprocessor/resolver/bank.rs +14 -12
  217. package/rust/core/preprocessor/resolver/call.rs +106 -105
  218. package/rust/core/preprocessor/resolver/condition.rs +13 -10
  219. package/rust/core/preprocessor/resolver/driver.rs +145 -48
  220. package/rust/core/preprocessor/resolver/function.rs +69 -0
  221. package/rust/core/preprocessor/resolver/group.rs +122 -61
  222. package/rust/core/preprocessor/resolver/let_.rs +13 -12
  223. package/rust/core/preprocessor/resolver/loop_.rs +240 -13
  224. package/rust/core/preprocessor/resolver/mod.rs +8 -6
  225. package/rust/core/preprocessor/resolver/pattern.rs +83 -0
  226. package/rust/core/preprocessor/resolver/spawn.rs +83 -42
  227. package/rust/core/preprocessor/resolver/synth.rs +15 -11
  228. package/rust/core/preprocessor/resolver/tempo.rs +13 -14
  229. package/rust/core/preprocessor/resolver/trigger.rs +32 -28
  230. package/rust/core/preprocessor/resolver/value.rs +111 -13
  231. package/rust/core/store/global.rs +57 -39
  232. package/rust/core/store/mod.rs +0 -3
  233. package/rust/lib.rs +323 -117
  234. package/rust/main.rs +388 -65
  235. package/rust/types/Cargo.toml +11 -0
  236. package/rust/types/src/addons.rs +55 -0
  237. package/rust/types/src/ast.rs +202 -0
  238. package/rust/types/src/config.rs +84 -0
  239. package/rust/types/src/lib.rs +15 -0
  240. package/rust/types/src/plugin.rs +20 -0
  241. package/rust/types/src/store.rs +139 -0
  242. package/rust/types/src/telemetry.rs +85 -0
  243. package/rust/utils/Cargo.toml +26 -0
  244. package/rust/utils/src/error.rs +186 -0
  245. package/rust/utils/src/file.rs +94 -0
  246. package/rust/utils/src/first_usage.rs +97 -0
  247. package/rust/utils/{mod.rs → src/lib.rs} +6 -3
  248. package/rust/utils/{logger.rs → src/logger.rs} +94 -17
  249. package/rust/utils/src/path.rs +129 -0
  250. package/rust/utils/src/signature.rs +41 -0
  251. package/rust/utils/{spinner.rs → src/spinner.rs} +7 -8
  252. package/rust/utils/src/version.rs +27 -0
  253. package/rust/utils/{watcher.rs → src/watcher.rs} +17 -4
  254. package/rust/web/api.rs +5 -0
  255. package/rust/web/cdn.rs +34 -0
  256. package/rust/web/mod.rs +3 -0
  257. package/rust/web/sso.rs +5 -0
  258. package/templates/minimal/README.md +143 -127
  259. package/templates/welcome/README.md +143 -127
  260. package/templates/welcome/src/index.deva +56 -8
  261. package/templates/welcome/src/variables.deva +2 -4
  262. package/tests/integration.rs +21 -0
  263. package/tests/rust/cli_check_build.rs +21 -0
  264. package/tests/rust/cli_help.rs +12 -0
  265. package/tests/rust/cli_template_list.rs +10 -0
  266. package/tests/rust/cli_version.rs +11 -0
  267. package/tests/typescript/index.spec.ts +136 -0
  268. package/tests/typescript/playhead.spec.ts +36 -0
  269. package/tests/typescript/render_e2e.spec.ts +77 -0
  270. package/tsconfig.json +12 -10
  271. package/typescript/bin/index.ts +19 -5
  272. package/typescript/core/functions/index.ts +94 -0
  273. package/typescript/core/index.ts +6 -0
  274. package/typescript/core/types/index.ts +4 -0
  275. package/typescript/core/types/plugin.ts +19 -0
  276. package/typescript/core/types/result.ts +29 -0
  277. package/typescript/core/types/statement.ts +47 -0
  278. package/typescript/core/types/value.ts +29 -0
  279. package/typescript/index.ts +8 -1
  280. package/typescript/pkg/devalang_core.d.ts +4 -0
  281. package/typescript/pkg/devalang_core.ts +65 -0
  282. package/typescript/scripts/copy-wasm-dts.ts +41 -0
  283. package/typescript/scripts/postinstall.ts +85 -0
  284. package/typescript/scripts/version/bump.ts +0 -1
  285. package/typescript/scripts/version/index.ts +0 -1
  286. package/docs/COMMANDS.md +0 -85
  287. package/docs/CONFIG.md +0 -30
  288. package/docs/SYNTAX.md +0 -210
  289. package/out-tsc/bin/devalang.exe +0 -0
  290. package/out-tsc/scripts/postbuild.js +0 -11
  291. package/rust/cli/build.rs +0 -137
  292. package/rust/cli/check.rs +0 -117
  293. package/rust/cli/play.rs +0 -193
  294. package/rust/config/loader.rs +0 -13
  295. package/rust/core/audio/engine.rs +0 -203
  296. package/rust/core/audio/evaluator.rs +0 -31
  297. package/rust/core/audio/interpreter/arrow_call.rs +0 -129
  298. package/rust/core/audio/interpreter/call.rs +0 -64
  299. package/rust/core/audio/interpreter/let_.rs +0 -19
  300. package/rust/core/audio/interpreter/load.rs +0 -18
  301. package/rust/core/audio/interpreter/loop_.rs +0 -67
  302. package/rust/core/audio/interpreter/sleep.rs +0 -36
  303. package/rust/core/audio/interpreter/spawn.rs +0 -66
  304. package/rust/core/audio/interpreter/tempo.rs +0 -16
  305. package/rust/core/audio/interpreter/trigger.rs +0 -69
  306. package/rust/core/audio/renderer.rs +0 -54
  307. package/rust/core/parser/driver.rs +0 -331
  308. package/rust/core/preprocessor/loader.rs +0 -193
  309. package/rust/core/preprocessor/processor.rs +0 -76
  310. package/rust/core/shared/duration.rs +0 -8
  311. package/rust/core/shared/mod.rs +0 -2
  312. package/rust/core/shared/value.rs +0 -18
  313. package/rust/core/store/export.rs +0 -28
  314. package/rust/core/store/import.rs +0 -28
  315. package/rust/core/store/variable.rs +0 -28
  316. package/rust/core/utils/mod.rs +0 -2
  317. package/rust/core/utils/path.rs +0 -31
  318. package/rust/core/utils/validation.rs +0 -37
  319. package/rust/utils/file.rs +0 -35
  320. package/rust/utils/signature.rs +0 -17
  321. package/rust/utils/version.rs +0 -15
  322. package/typescript/scripts/postbuild.ts +0 -8
@@ -1,216 +1,574 @@
1
- use crate::core::{
2
- audio::{
3
- engine::AudioEngine,
4
- interpreter::{
5
- arrow_call::interprete_call_arrow_statement,
6
- call::interprete_call_statement,
7
- condition::interprete_condition_statement,
8
- let_::interprete_let_statement,
9
- load::interprete_load_statement,
10
- loop_::interprete_loop_statement,
11
- sleep::interprete_sleep_statement,
12
- spawn::interprete_spawn_statement,
13
- tempo::interprete_tempo_statement,
14
- trigger::interprete_trigger_statement,
15
- },
16
- },
17
- parser::statement::{ Statement, StatementKind },
18
- store::variable::VariableTable,
19
- };
20
-
21
- pub fn run_audio_program(
22
- statements: &Vec<Statement>,
23
- audio_engine: AudioEngine,
24
- entry: String,
25
- output: String
26
- ) -> (AudioEngine, f32, f32) {
27
- let mut base_bpm = 120.0;
28
- let mut base_duration = 60.0 / base_bpm;
29
-
30
- let variable_table = audio_engine.variables.clone();
31
-
32
- let (updated_audio_engine, base_bpm, max_end_time) = execute_audio_block(
33
- audio_engine,
34
- variable_table.clone(),
35
- statements.clone(),
36
- base_bpm.clone(),
37
- base_duration.clone(),
38
- 0.0,
39
- 0.0
40
- );
41
-
42
- (updated_audio_engine, base_bpm, max_end_time)
43
- }
44
-
45
- pub fn execute_audio_block(
46
- mut audio_engine: AudioEngine,
47
- mut variable_table: VariableTable,
48
- mut statements: Vec<Statement>,
49
- mut base_bpm: f32,
50
- mut base_duration: f32,
51
- mut max_end_time: f32,
52
- mut cursor_time: f32
53
- ) -> (AudioEngine, f32, f32) {
54
- let initial_cursor_time = cursor_time;
55
-
56
- for stmt in statements {
57
- match &stmt.kind {
58
- StatementKind::Load { .. } => {
59
- if
60
- let Some(new_variable_table) = interprete_load_statement(
61
- &stmt,
62
- &mut variable_table
63
- )
64
- {
65
- variable_table = new_variable_table;
66
- } else {
67
- eprintln!("❌ Failed to interpret load statement: {:?}", stmt);
68
- }
69
- }
70
-
71
- StatementKind::Let { .. } => {
72
- if
73
- let Some(new_variable_table) = interprete_let_statement(
74
- &stmt,
75
- &mut variable_table
76
- )
77
- {
78
- variable_table = new_variable_table;
79
- } else {
80
- eprintln!("❌ Failed to interpret let statement: {:?}", stmt);
81
- }
82
- }
83
-
84
- StatementKind::Tempo => {
85
- if let Some((new_bpm, new_duration)) = interprete_tempo_statement(&stmt) {
86
- base_bpm = new_bpm;
87
- base_duration = new_duration;
88
- } else {
89
- eprintln!("❌ Failed to interpret tempo statement: {:?}", stmt);
90
- }
91
- }
92
-
93
- StatementKind::Trigger { .. } => {
94
- if
95
- let Some((new_cursor_time, new_max_end_time, updated_engine)) =
96
- interprete_trigger_statement(
97
- &stmt,
98
- &mut audio_engine,
99
- &variable_table,
100
- base_duration,
101
- cursor_time,
102
- max_end_time
103
- )
104
- {
105
- cursor_time = new_cursor_time;
106
- max_end_time = new_max_end_time;
107
- audio_engine = updated_engine;
108
- } else {
109
- eprintln!("❌ Failed to interpret trigger statement: {:?}", stmt);
110
- }
111
- }
112
-
113
- StatementKind::Spawn => {
114
- let mut temp_engine = AudioEngine::new(audio_engine.module_name.clone());
115
-
116
- if
117
- let Some((_cur, _max, updated_engine)) = interprete_spawn_statement(
118
- &stmt,
119
- temp_engine,
120
- &variable_table,
121
- base_bpm,
122
- base_duration,
123
- initial_cursor_time,
124
- max_end_time
125
- )
126
- {
127
- audio_engine.merge_with(updated_engine);
128
- }
129
- }
130
-
131
- StatementKind::Call => {
132
- let (call_engine, new_max, new_cursor) = interprete_call_statement(
133
- &stmt,
134
- audio_engine.clone(),
135
- variable_table.clone(),
136
- base_bpm,
137
- base_duration,
138
- max_end_time,
139
- cursor_time
140
- );
141
-
142
- audio_engine.merge_with(call_engine);
143
- }
144
-
145
- StatementKind::Sleep => {
146
- let (new_cursor, new_max) = interprete_sleep_statement(
147
- &stmt,
148
- cursor_time,
149
- max_end_time
150
- );
151
- cursor_time = new_cursor;
152
- max_end_time = new_max;
153
- }
154
-
155
- StatementKind::Loop => {
156
- let (loop_engine, new_max, new_cursor) = interprete_loop_statement(
157
- &stmt,
158
- audio_engine.clone(),
159
- variable_table.clone(),
160
- base_bpm,
161
- base_duration,
162
- max_end_time,
163
- cursor_time
164
- );
165
- audio_engine = loop_engine;
166
- cursor_time = new_cursor;
167
- max_end_time = new_max;
168
- }
169
-
170
- StatementKind::If | StatementKind::ElseIf | StatementKind::Else => {
171
- let (condition_engine, new_max, new_cursor) = interprete_condition_statement(
172
- &stmt,
173
- audio_engine.clone(),
174
- variable_table.clone(),
175
- base_bpm,
176
- base_duration,
177
- max_end_time,
178
- cursor_time
179
- );
180
-
181
- audio_engine = condition_engine;
182
- cursor_time = new_cursor;
183
- max_end_time = new_max;
184
- }
185
-
186
- StatementKind::ArrowCall { .. } => {
187
- interprete_call_arrow_statement(
188
- &stmt,
189
- &mut audio_engine,
190
- &variable_table,
191
- base_bpm,
192
- base_duration,
193
- &mut max_end_time,
194
- Some(&mut cursor_time),
195
- true
196
- );
197
- }
198
-
199
- | StatementKind::Bank
200
- | StatementKind::Import { .. }
201
- | StatementKind::Export { .. }
202
- | StatementKind::Group
203
- | StatementKind::Unknown => {
204
- // NOTE: Ignoring unsupported statement kinds for now.
205
- }
206
-
207
- _ => {
208
- eprintln!("Unsupported audio statement kind: {:?}", stmt);
209
- }
210
- }
211
- }
212
-
213
- audio_engine.set_variables(variable_table);
214
-
215
- (audio_engine, base_bpm, max_end_time)
216
- }
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
+ }