@devaloop/devalang 0.0.1-alpha.14 → 0.0.1-alpha.16

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 (177) hide show
  1. package/.devalang +10 -8
  2. package/.github/workflows/ci.yml +92 -0
  3. package/Cargo.toml +60 -58
  4. package/README.md +32 -15
  5. package/docs/CHANGELOG.md +93 -1
  6. package/docs/CONTRIBUTING.md +101 -1
  7. package/docs/ROADMAP.md +2 -2
  8. package/docs/TODO.md +1 -1
  9. package/examples/automation.deva +42 -0
  10. package/examples/bank.deva +4 -4
  11. package/examples/events.deva +12 -0
  12. package/examples/function.deva +4 -4
  13. package/examples/index.deva +39 -25
  14. package/examples/loop.deva +5 -11
  15. package/examples/pattern.deva +8 -0
  16. package/examples/plugin.deva +16 -0
  17. package/examples/variables.deva +1 -1
  18. package/out-tsc/bin/index.js +51 -7
  19. package/out-tsc/index.js +3 -1
  20. package/out-tsc/scripts/postbuild.js +9 -10
  21. package/out-tsc/scripts/postinstall.js +49 -0
  22. package/package.json +12 -4
  23. package/project-version.json +3 -3
  24. package/rust/cli/bank.rs +462 -456
  25. package/rust/cli/build.rs +252 -199
  26. package/rust/cli/check.rs +221 -180
  27. package/rust/cli/driver.rs +297 -292
  28. package/rust/cli/generator.rs +1 -0
  29. package/rust/cli/init.rs +87 -79
  30. package/rust/cli/install.rs +35 -32
  31. package/rust/cli/login.rs +127 -134
  32. package/rust/cli/mod.rs +13 -11
  33. package/rust/cli/play.rs +1123 -218
  34. package/rust/cli/telemetry.rs +19 -0
  35. package/rust/cli/template.rs +69 -57
  36. package/rust/cli/update.rs +6 -4
  37. package/rust/common/api.rs +5 -8
  38. package/rust/common/cdn.rs +3 -6
  39. package/rust/common/mod.rs +3 -3
  40. package/rust/common/sso.rs +3 -6
  41. package/rust/config/driver.rs +118 -94
  42. package/rust/config/loader.rs +165 -156
  43. package/rust/config/mod.rs +4 -2
  44. package/rust/config/settings.rs +91 -0
  45. package/rust/config/stats.rs +257 -0
  46. package/rust/core/audio/engine.rs +696 -518
  47. package/rust/core/audio/evaluator.rs +263 -31
  48. package/rust/core/audio/interpreter/arrow_call.rs +198 -161
  49. package/rust/core/audio/interpreter/automate.rs +18 -0
  50. package/rust/core/audio/interpreter/call.rs +98 -95
  51. package/rust/core/audio/interpreter/condition.rs +70 -71
  52. package/rust/core/audio/interpreter/driver.rs +487 -198
  53. package/rust/core/audio/interpreter/function.rs +26 -21
  54. package/rust/core/audio/interpreter/let_.rs +38 -19
  55. package/rust/core/audio/interpreter/load.rs +18 -18
  56. package/rust/core/audio/interpreter/loop_.rs +113 -73
  57. package/rust/core/audio/interpreter/mod.rs +14 -13
  58. package/rust/core/audio/interpreter/sleep.rs +27 -30
  59. package/rust/core/audio/interpreter/spawn.rs +105 -102
  60. package/rust/core/audio/interpreter/tempo.rs +19 -16
  61. package/rust/core/audio/interpreter/trigger.rs +239 -210
  62. package/rust/core/audio/loader/mod.rs +1 -1
  63. package/rust/core/audio/loader/trigger.rs +100 -97
  64. package/rust/core/audio/mod.rs +7 -6
  65. package/rust/core/audio/player.rs +64 -64
  66. package/rust/core/audio/renderer.rs +56 -53
  67. package/rust/core/audio/special/easing.rs +189 -0
  68. package/rust/core/audio/special/env.rs +43 -0
  69. package/rust/core/audio/special/math.rs +102 -0
  70. package/rust/core/audio/special/mod.rs +9 -0
  71. package/rust/core/audio/special/modulator.rs +143 -0
  72. package/rust/core/builder/mod.rs +80 -85
  73. package/rust/core/debugger/lexer.rs +27 -27
  74. package/rust/core/debugger/mod.rs +24 -23
  75. package/rust/core/debugger/module.rs +55 -47
  76. package/rust/core/debugger/preprocessor.rs +27 -27
  77. package/rust/core/debugger/store.rs +40 -39
  78. package/rust/core/error/mod.rs +80 -66
  79. package/rust/core/lexer/handler/arrow.rs +82 -31
  80. package/rust/core/lexer/handler/at.rs +21 -21
  81. package/rust/core/lexer/handler/brace.rs +41 -41
  82. package/rust/core/lexer/handler/colon.rs +21 -21
  83. package/rust/core/lexer/handler/comment.rs +30 -30
  84. package/rust/core/lexer/handler/dot.rs +21 -21
  85. package/rust/core/lexer/handler/driver.rs +337 -263
  86. package/rust/core/lexer/handler/identifier.rs +46 -42
  87. package/rust/core/lexer/handler/indent.rs +66 -66
  88. package/rust/core/lexer/handler/mod.rs +16 -16
  89. package/rust/core/lexer/handler/newline.rs +23 -23
  90. package/rust/core/lexer/handler/number.rs +31 -31
  91. package/rust/core/lexer/handler/operator.rs +46 -44
  92. package/rust/core/lexer/handler/parenthesis.rs +41 -41
  93. package/rust/core/lexer/handler/slash.rs +21 -21
  94. package/rust/core/lexer/handler/string.rs +63 -63
  95. package/rust/core/lexer/mod.rs +54 -51
  96. package/rust/core/lexer/token.rs +97 -91
  97. package/rust/core/mod.rs +11 -11
  98. package/rust/core/parser/driver.rs +513 -408
  99. package/rust/core/parser/handler/arrow_call.rs +233 -211
  100. package/rust/core/parser/handler/at.rs +245 -162
  101. package/rust/core/parser/handler/bank.rs +94 -69
  102. package/rust/core/parser/handler/condition.rs +80 -74
  103. package/rust/core/parser/handler/dot.rs +143 -135
  104. package/rust/core/parser/handler/identifier/automate.rs +257 -0
  105. package/rust/core/parser/handler/identifier/call.rs +91 -88
  106. package/rust/core/parser/handler/identifier/emit.rs +66 -0
  107. package/rust/core/parser/handler/identifier/function.rs +100 -92
  108. package/rust/core/parser/handler/identifier/group.rs +85 -75
  109. package/rust/core/parser/handler/identifier/let_.rs +158 -127
  110. package/rust/core/parser/handler/identifier/mod.rs +54 -52
  111. package/rust/core/parser/handler/identifier/on.rs +98 -0
  112. package/rust/core/parser/handler/identifier/print.rs +52 -0
  113. package/rust/core/parser/handler/identifier/sleep.rs +36 -33
  114. package/rust/core/parser/handler/identifier/spawn.rs +91 -88
  115. package/rust/core/parser/handler/identifier/synth.rs +65 -65
  116. package/rust/core/parser/handler/loop_.rs +170 -72
  117. package/rust/core/parser/handler/mod.rs +8 -8
  118. package/rust/core/parser/handler/tempo.rs +53 -47
  119. package/rust/core/parser/mod.rs +4 -4
  120. package/rust/core/parser/statement.rs +142 -108
  121. package/rust/core/plugin/loader.rs +123 -48
  122. package/rust/core/plugin/mod.rs +2 -1
  123. package/rust/core/plugin/runner.rs +296 -0
  124. package/rust/core/preprocessor/loader.rs +515 -326
  125. package/rust/core/preprocessor/mod.rs +4 -4
  126. package/rust/core/preprocessor/module.rs +60 -58
  127. package/rust/core/preprocessor/processor.rs +99 -101
  128. package/rust/core/preprocessor/resolver/bank.rs +51 -49
  129. package/rust/core/preprocessor/resolver/call.rs +100 -100
  130. package/rust/core/preprocessor/resolver/condition.rs +97 -97
  131. package/rust/core/preprocessor/resolver/driver.rs +310 -278
  132. package/rust/core/preprocessor/resolver/function.rs +69 -78
  133. package/rust/core/preprocessor/resolver/group.rs +96 -91
  134. package/rust/core/preprocessor/resolver/let_.rs +32 -28
  135. package/rust/core/preprocessor/resolver/loop_.rs +320 -91
  136. package/rust/core/preprocessor/resolver/mod.rs +15 -15
  137. package/rust/core/preprocessor/resolver/spawn.rs +76 -92
  138. package/rust/core/preprocessor/resolver/synth.rs +56 -50
  139. package/rust/core/preprocessor/resolver/tempo.rs +50 -49
  140. package/rust/core/preprocessor/resolver/trigger.rs +113 -116
  141. package/rust/core/preprocessor/resolver/value.rs +81 -87
  142. package/rust/core/shared/bank.rs +1 -1
  143. package/rust/core/shared/duration.rs +9 -9
  144. package/rust/core/shared/mod.rs +3 -3
  145. package/rust/core/shared/value.rs +35 -32
  146. package/rust/core/store/function.rs +34 -34
  147. package/rust/core/store/global.rs +55 -38
  148. package/rust/core/store/mod.rs +5 -5
  149. package/rust/core/store/variable.rs +37 -34
  150. package/rust/core/utils/mod.rs +2 -2
  151. package/rust/core/utils/path.rs +37 -31
  152. package/rust/core/utils/validation.rs +35 -37
  153. package/rust/installer/addon.rs +84 -80
  154. package/rust/installer/bank.rs +62 -65
  155. package/rust/installer/mod.rs +5 -5
  156. package/rust/installer/plugin.rs +54 -55
  157. package/rust/installer/utils.rs +56 -56
  158. package/rust/lib.rs +156 -164
  159. package/rust/main.rs +250 -145
  160. package/rust/utils/error.rs +200 -0
  161. package/rust/utils/file.rs +38 -35
  162. package/rust/utils/first_usage.rs +76 -0
  163. package/rust/utils/logger.rs +195 -139
  164. package/rust/utils/mod.rs +9 -50
  165. package/rust/utils/signature.rs +19 -17
  166. package/rust/utils/spinner.rs +22 -19
  167. package/rust/utils/telemetry.rs +292 -0
  168. package/rust/utils/watcher.rs +34 -33
  169. package/templates/minimal/README.md +97 -121
  170. package/templates/welcome/README.md +97 -121
  171. package/typescript/bin/index.ts +19 -5
  172. package/typescript/index.ts +3 -1
  173. package/typescript/scripts/postbuild.ts +10 -6
  174. package/typescript/scripts/postinstall.ts +56 -0
  175. package/typescript/scripts/version/bump.ts +0 -1
  176. package/typescript/scripts/version/index.ts +0 -1
  177. package/out-tsc/bin/devalang.exe +0 -0
@@ -1,6 +1,7 @@
1
- pub mod engine;
2
- pub mod interpreter;
3
- pub mod loader;
4
- pub mod player;
5
- pub mod renderer;
6
- pub mod evaluator;
1
+ pub mod engine;
2
+ pub mod evaluator;
3
+ pub mod interpreter;
4
+ pub mod loader;
5
+ pub mod player;
6
+ pub mod renderer;
7
+ pub mod special;
@@ -1,64 +1,64 @@
1
- use rodio::{ Decoder, OutputStream, OutputStreamHandle, Sink, Source };
2
- use std::{ fs::File, io::BufReader };
3
-
4
- pub struct AudioPlayer {
5
- _stream: OutputStream,
6
- handle: OutputStreamHandle,
7
- sink: Sink,
8
- last_path: Option<String>,
9
- }
10
-
11
- impl AudioPlayer {
12
- pub fn new() -> Self {
13
- let (stream, handle) = OutputStream::try_default().unwrap();
14
- let sink = Sink::try_new(&handle).unwrap();
15
-
16
- Self {
17
- _stream: stream,
18
- handle,
19
- sink,
20
- last_path: None,
21
- }
22
- }
23
-
24
- fn load_source(&self, path: &str) -> Option<impl Source<Item = f32> + Send + 'static> {
25
- if let Ok(file) = File::open(path) {
26
- let reader = BufReader::new(file);
27
- match Decoder::new(reader) {
28
- Ok(decoder) => Some(decoder.convert_samples()),
29
- Err(e) => {
30
- eprintln!("❌ Failed to decode audio file '{}': {}", path, e);
31
- None
32
- }
33
- }
34
- } else {
35
- eprintln!("❌ Could not open audio file: {}", path);
36
- None
37
- }
38
- }
39
-
40
- pub fn play_file_once(&mut self, path: &str) {
41
- self.sink.stop();
42
- self.sink = Sink::try_new(&self.handle).unwrap();
43
- self.sink.set_volume(1.0);
44
-
45
- if let Some(source) = self.load_source(path) {
46
- self.sink.append(source);
47
- self.last_path = Some(path.to_string());
48
- } else {
49
- eprintln!("⚠️ Skipping playback: failed to load '{}'", path);
50
- }
51
- }
52
-
53
- pub fn replay_last(&mut self) {
54
- if let Some(path) = self.last_path.clone() {
55
- self.play_file_once(&path);
56
- } else {
57
- eprintln!("⚠️ No previous audio to replay.");
58
- }
59
- }
60
-
61
- pub fn wait_until_end(&self) {
62
- self.sink.sleep_until_end();
63
- }
64
- }
1
+ use rodio::{Decoder, OutputStream, OutputStreamHandle, Sink, Source};
2
+ use std::{fs::File, io::BufReader};
3
+
4
+ pub struct AudioPlayer {
5
+ _stream: OutputStream,
6
+ handle: OutputStreamHandle,
7
+ sink: Sink,
8
+ last_path: Option<String>,
9
+ }
10
+
11
+ impl AudioPlayer {
12
+ pub fn new() -> Self {
13
+ let (stream, handle) = OutputStream::try_default().unwrap();
14
+ let sink = Sink::try_new(&handle).unwrap();
15
+
16
+ Self {
17
+ _stream: stream,
18
+ handle,
19
+ sink,
20
+ last_path: None,
21
+ }
22
+ }
23
+
24
+ fn load_source(&self, path: &str) -> Option<impl Source<Item = f32> + Send + 'static> {
25
+ if let Ok(file) = File::open(path) {
26
+ let reader = BufReader::new(file);
27
+ match Decoder::new(reader) {
28
+ Ok(decoder) => Some(decoder.convert_samples()),
29
+ Err(e) => {
30
+ eprintln!("❌ Failed to decode audio file '{}': {}", path, e);
31
+ None
32
+ }
33
+ }
34
+ } else {
35
+ eprintln!("❌ Could not open audio file: {}", path);
36
+ None
37
+ }
38
+ }
39
+
40
+ pub fn play_file_once(&mut self, path: &str) {
41
+ self.sink.stop();
42
+ self.sink = Sink::try_new(&self.handle).unwrap();
43
+ self.sink.set_volume(1.0);
44
+
45
+ if let Some(source) = self.load_source(path) {
46
+ self.sink.append(source);
47
+ self.last_path = Some(path.to_string());
48
+ } else {
49
+ eprintln!("⚠️ Skipping playback: failed to load '{}'", path);
50
+ }
51
+ }
52
+
53
+ pub fn replay_last(&mut self) {
54
+ if let Some(path) = self.last_path.clone() {
55
+ self.play_file_once(&path);
56
+ } else {
57
+ eprintln!("⚠️ No previous audio to replay.");
58
+ }
59
+ }
60
+
61
+ pub fn wait_until_end(&self) {
62
+ self.sink.sleep_until_end();
63
+ }
64
+ }
@@ -1,53 +1,56 @@
1
- use std::collections::HashMap;
2
- use crate::{
3
- core::{
4
- audio::{ engine::AudioEngine, interpreter::driver::run_audio_program },
5
- parser::statement::Statement,
6
- store::global::GlobalStore,
7
- },
8
- utils::logger::{ LogLevel, Logger },
9
- };
10
-
11
- pub fn render_audio_with_modules(
12
- modules: HashMap<String, Vec<Statement>>,
13
- output_dir: &str,
14
- global_store: &mut GlobalStore
15
- ) -> HashMap<String, AudioEngine> {
16
- let mut result = HashMap::new();
17
-
18
- for (module_name, statements) in modules {
19
- let mut global_max_end_time: f32 = 0.0;
20
- let mut audio_engine = AudioEngine::new(module_name.clone());
21
-
22
- // Apply global variables to the initial engine
23
- if let Some(module) = global_store.get_module(&module_name) {
24
- // interprete statements to fill the audio buffer
25
- let (module_max_end_time, cursor_time) = run_audio_program(
26
- &statements,
27
- &mut audio_engine,
28
- module_name.clone(),
29
- output_dir.to_string(),
30
- module.variable_table.clone(),
31
- module.function_table.clone(),
32
- global_store
33
- );
34
-
35
- // Verify if the buffer is silent (all samples are zero)
36
- if audio_engine.buffer.iter().all(|&s| s == 0) {
37
- let logger = Logger::new();
38
- logger.log_message(
39
- LogLevel::Warning,
40
- &format!("Module '{}' ignored: silent buffer (no non-zero samples)", module_name)
41
- );
42
- }
43
-
44
- // Determines the maximum end time for the module
45
- global_max_end_time = global_max_end_time.max(module_max_end_time);
46
- audio_engine.set_duration(global_max_end_time);
47
-
48
- result.insert(module_name, audio_engine);
49
- }
50
- }
51
-
52
- result
53
- }
1
+ use crate::{
2
+ core::{
3
+ audio::{engine::AudioEngine, interpreter::driver::run_audio_program},
4
+ parser::statement::Statement,
5
+ store::global::GlobalStore,
6
+ },
7
+ utils::logger::{LogLevel, Logger},
8
+ };
9
+ use std::collections::HashMap;
10
+
11
+ pub fn render_audio_with_modules(
12
+ modules: HashMap<String, Vec<Statement>>,
13
+ output_dir: &str,
14
+ global_store: &mut GlobalStore,
15
+ ) -> HashMap<String, AudioEngine> {
16
+ let mut result = HashMap::new();
17
+
18
+ for (module_name, statements) in modules {
19
+ let mut global_max_end_time: f32 = 0.0;
20
+ let mut audio_engine = AudioEngine::new(module_name.clone());
21
+
22
+ // Apply global variables to the initial engine
23
+ if let Some(module) = global_store.get_module(&module_name) {
24
+ // interprete statements to fill the audio buffer
25
+ let (module_max_end_time, _cursor_time) = run_audio_program(
26
+ &statements,
27
+ &mut audio_engine,
28
+ module_name.clone(),
29
+ output_dir.to_string(),
30
+ module.variable_table.clone(),
31
+ module.function_table.clone(),
32
+ global_store,
33
+ );
34
+
35
+ // Verify if the buffer is silent (all samples are zero)
36
+ if audio_engine.buffer.iter().all(|&s| s == 0) {
37
+ let logger = Logger::new();
38
+ logger.log_message(
39
+ LogLevel::Warning,
40
+ &format!(
41
+ "Module '{}' ignored: silent buffer (no non-zero samples)",
42
+ module_name
43
+ ),
44
+ );
45
+ }
46
+
47
+ // Determines the maximum end time for the module
48
+ global_max_end_time = global_max_end_time.max(module_max_end_time);
49
+ audio_engine.set_duration(global_max_end_time);
50
+
51
+ result.insert(module_name, audio_engine);
52
+ }
53
+ }
54
+
55
+ result
56
+ }
@@ -0,0 +1,189 @@
1
+ use crate::core::store::variable::VariableTable;
2
+
3
+ // Basic easing functions operating on t in [0,1]
4
+ fn easing_value(func: &str, t: f32) -> Option<f32> {
5
+ let x = t.clamp(0.0, 1.0);
6
+ match func {
7
+ "linear" => Some(x),
8
+ "easeInQuad" => Some(x * x),
9
+ "easeOutQuad" => Some(x * (2.0 - x)),
10
+ "easeInOutQuad" => {
11
+ if x < 0.5 {
12
+ Some(2.0 * x * x)
13
+ } else {
14
+ Some(-1.0 + (4.0 - 2.0 * x) * x)
15
+ }
16
+ }
17
+ // Cubic
18
+ "easeInCubic" => Some(x * x * x),
19
+ "easeOutCubic" => Some(1.0 - (1.0 - x).powi(3)),
20
+ "easeInOutCubic" => {
21
+ if x < 0.5 {
22
+ Some(4.0 * x * x * x)
23
+ } else {
24
+ Some(1.0 - (-2.0 * x + 2.0).powi(3) / 2.0)
25
+ }
26
+ }
27
+ // Quartic
28
+ "easeInQuart" => Some(x.powi(4)),
29
+ "easeOutQuart" => Some(1.0 - (1.0 - x).powi(4)),
30
+ "easeInOutQuart" => {
31
+ if x < 0.5 {
32
+ Some(8.0 * x.powi(4))
33
+ } else {
34
+ Some(1.0 - (-2.0 * x + 2.0).powi(4) / 2.0)
35
+ }
36
+ }
37
+ // Exponential
38
+ "easeInExpo" => Some(if x <= 0.0 {
39
+ 0.0
40
+ } else {
41
+ 2.0_f32.powf(10.0 * x - 10.0)
42
+ }),
43
+ "easeOutExpo" => Some(if x >= 1.0 {
44
+ 1.0
45
+ } else {
46
+ 1.0 - 2.0_f32.powf(-10.0 * x)
47
+ }),
48
+ "easeInOutExpo" => Some(if x <= 0.0 {
49
+ 0.0
50
+ } else if x >= 1.0 {
51
+ 1.0
52
+ } else if x < 0.5 {
53
+ 2.0_f32.powf(20.0 * x - 10.0) / 2.0
54
+ } else {
55
+ (2.0 - 2.0_f32.powf(-20.0 * x + 10.0)) / 2.0
56
+ }),
57
+ // Back (overshoot c ~ 1.70158)
58
+ "easeInBack" => {
59
+ let c = 1.70158;
60
+ Some((c + 1.0) * x * x * x - c * x * x)
61
+ }
62
+ "easeOutBack" => {
63
+ let c = 1.70158;
64
+ let y = 1.0 - x;
65
+ Some(1.0 - ((c + 1.0) * y * y * y - c * y * y))
66
+ }
67
+ "easeInOutBack" => {
68
+ let c1 = 1.70158;
69
+ let c2 = c1 * 1.525;
70
+ let x2 = x * 2.0;
71
+ if x2 < 1.0 {
72
+ Some((x2 * x2 * ((c2 + 1.0) * x2 - c2)) / 2.0)
73
+ } else {
74
+ let x2 = x2 - 2.0;
75
+ Some((x2 * x2 * ((c2 + 1.0) * x2 + c2)) / 2.0 + 1.0)
76
+ }
77
+ }
78
+ // Elastic
79
+ "easeInElastic" => {
80
+ if x == 0.0 {
81
+ Some(0.0)
82
+ } else if x == 1.0 {
83
+ Some(1.0)
84
+ } else {
85
+ let c = 2.0 * std::f32::consts::PI / 3.0;
86
+ Some(-(2.0_f32.powf(10.0 * x - 10.0)) * ((x * 10.0 - 10.75) * c).sin())
87
+ }
88
+ }
89
+ "easeOutElastic" => {
90
+ if x == 0.0 {
91
+ Some(0.0)
92
+ } else if x == 1.0 {
93
+ Some(1.0)
94
+ } else {
95
+ let c = 2.0 * std::f32::consts::PI / 3.0;
96
+ Some(2.0_f32.powf(-10.0 * x) * ((x * 10.0 - 0.75) * c).sin() + 1.0)
97
+ }
98
+ }
99
+ "easeInOutElastic" => {
100
+ if x == 0.0 {
101
+ Some(0.0)
102
+ } else if x == 1.0 {
103
+ Some(1.0)
104
+ } else {
105
+ let c = 2.0 * std::f32::consts::PI / 4.5;
106
+ if x < 0.5 {
107
+ Some(-(2.0_f32.powf(20.0 * x - 10.0)) * ((20.0 * x - 11.125) * c).sin() / 2.0)
108
+ } else {
109
+ Some(
110
+ 2.0_f32.powf(-20.0 * x + 10.0) * ((20.0 * x - 11.125) * c).sin() / 2.0
111
+ + 1.0,
112
+ )
113
+ }
114
+ }
115
+ }
116
+ // Bounce helpers
117
+ "easeInBounce" => Some(1.0 - bounce_out(1.0 - x)),
118
+ "easeOutBounce" => Some(bounce_out(x)),
119
+ "easeInOutBounce" => Some(if x < 0.5 {
120
+ (1.0 - bounce_out(1.0 - 2.0 * x)) / 2.0
121
+ } else {
122
+ (1.0 + bounce_out(2.0 * x - 1.0)) / 2.0
123
+ }),
124
+ _ => None,
125
+ }
126
+ }
127
+
128
+ fn bounce_out(x: f32) -> f32 {
129
+ let n1 = 7.5625;
130
+ let d1 = 2.75;
131
+ if x < 1.0 / d1 {
132
+ n1 * x * x
133
+ } else if x < 2.0 / d1 {
134
+ let x = x - 1.5 / d1;
135
+ n1 * x * x + 0.75
136
+ } else if x < 2.5 / d1 {
137
+ let x = x - 2.25 / d1;
138
+ n1 * x * x + 0.9375
139
+ } else {
140
+ let x = x - 2.625 / d1;
141
+ n1 * x * x + 0.984375
142
+ }
143
+ }
144
+
145
+ // Find and evaluate the first $easing.<fn>(...) occurrence in the string.
146
+ // Accepts a single argument expression producing t in [0,1].
147
+ pub fn find_and_eval_first_easing_call<EvalFn>(
148
+ s: &str,
149
+ eval: EvalFn,
150
+ vars: &VariableTable,
151
+ bpm: f32,
152
+ beat: f32,
153
+ ) -> Option<String>
154
+ where
155
+ EvalFn: Fn(&str, &VariableTable, f32, f32) -> Option<f32>,
156
+ {
157
+ let start = s.find("$easing.")?;
158
+ let open_rel = s[start..].find('(')?;
159
+ let open = start + open_rel;
160
+ let func = &s[start + 9..open];
161
+
162
+ // Find matching close parenthesis
163
+ let mut depth: i32 = 0;
164
+ let mut close_abs: Option<usize> = None;
165
+ for (i, ch) in s[open..].char_indices() {
166
+ match ch {
167
+ '(' => depth += 1,
168
+ ')' => {
169
+ depth -= 1;
170
+ if depth == 0 {
171
+ close_abs = Some(open + i);
172
+ break;
173
+ }
174
+ }
175
+ _ => {}
176
+ }
177
+ }
178
+ let close = close_abs?;
179
+
180
+ let inner = &s[open + 1..close];
181
+ let t = eval(inner, vars, bpm, beat)?;
182
+ let result = easing_value(func, t)?;
183
+
184
+ let mut replaced = String::new();
185
+ replaced.push_str(&s[..start]);
186
+ replaced.push_str(&result.to_string());
187
+ replaced.push_str(&s[close + 1..]);
188
+ Some(replaced)
189
+ }
@@ -0,0 +1,43 @@
1
+ use crate::core::store::variable::VariableTable;
2
+ use std::sync::OnceLock;
3
+ use std::time::{SystemTime, UNIX_EPOCH};
4
+
5
+ static SESSION_SEED: OnceLock<f32> = OnceLock::new();
6
+
7
+ pub fn get_session_seed() -> f32 {
8
+ *SESSION_SEED.get_or_init(|| {
9
+ let now = SystemTime::now()
10
+ .duration_since(UNIX_EPOCH)
11
+ .unwrap_or_default();
12
+ // Build a stable 0..1 seed from nanos
13
+ let nanos = now.subsec_nanos();
14
+ ((nanos as f32) / 1_000_000_000.0).clamp(0.0, 1.0)
15
+ })
16
+ }
17
+
18
+ // Resolve special environment variables like $env.bpm, $env.beat, $env.position
19
+ // For now, $env.position is treated as an alias of beat.
20
+ pub fn resolve_env_atom(atom: &str, bpm: f32, beat: f32) -> Option<f32> {
21
+ match atom {
22
+ "$env.bpm" => Some(bpm),
23
+ "$env.beat" => Some(beat),
24
+ "$env.position" => Some(beat),
25
+ // Optional seed for deterministic randomness
26
+ "$env.seed" => Some(get_session_seed()),
27
+ _ => None,
28
+ }
29
+ }
30
+
31
+ // Utility: resolve an identifier or numeric literal to f32 using the variable table
32
+ pub fn resolve_atom_or_var(atom: &str, vars: &VariableTable, bpm: f32, beat: f32) -> Option<f32> {
33
+ if let Some(v) = resolve_env_atom(atom, bpm, beat) {
34
+ return Some(v);
35
+ }
36
+ if let Ok(n) = atom.parse::<f32>() {
37
+ return Some(n);
38
+ }
39
+ if let Some(crate::core::shared::value::Value::Number(n)) = vars.get(atom) {
40
+ return Some(*n);
41
+ }
42
+ None
43
+ }
@@ -0,0 +1,102 @@
1
+ use crate::core::store::variable::VariableTable;
2
+
3
+ // Parse comma-separated arguments at top level (no nested parentheses split)
4
+ fn parse_top_level_args(s: &str) -> Vec<&str> {
5
+ let mut args = Vec::new();
6
+ let mut depth = 0i32;
7
+ let mut start = 0usize;
8
+ for (i, ch) in s.char_indices() {
9
+ match ch {
10
+ '(' => depth += 1,
11
+ ')' => depth -= 1,
12
+ ',' if depth == 0 => {
13
+ args.push(s[start..i].trim());
14
+ start = i + 1;
15
+ }
16
+ _ => {}
17
+ }
18
+ }
19
+ let last = s[start..].trim();
20
+ if !last.is_empty() {
21
+ args.push(last);
22
+ }
23
+ args
24
+ }
25
+
26
+ fn eval_math_func(func: &str, args: &[f32], fallback_seed: f32) -> Option<f32> {
27
+ match func {
28
+ "sin" => args.get(0).copied().map(f32::sin),
29
+ "cos" => args.get(0).copied().map(f32::cos),
30
+ "random" => {
31
+ // deterministic pseudo-random based on provided seed or a fallback session seed
32
+ let seed = args.get(0).copied().unwrap_or(fallback_seed);
33
+ let x = (seed * 12.9898).sin() * 43758.5453;
34
+ Some((x.fract() * 2.0 - 1.0).clamp(-1.0, 1.0))
35
+ }
36
+ "lerp" => {
37
+ if args.len() >= 3 {
38
+ Some(args[0] + (args[1] - args[0]) * args[2])
39
+ } else {
40
+ None
41
+ }
42
+ }
43
+ _ => None,
44
+ }
45
+ }
46
+
47
+ // Find and evaluate the first $math.<fn>(...) occurrence in the string, replacing it with a number.
48
+ // Supports multi-argument functions by splitting on top-level commas.
49
+ pub fn find_and_eval_first_math_call<EvalFn>(
50
+ s: &str,
51
+ eval: EvalFn,
52
+ vars: &VariableTable,
53
+ bpm: f32,
54
+ beat: f32,
55
+ ) -> Option<String>
56
+ where
57
+ EvalFn: Fn(&str, &VariableTable, f32, f32) -> Option<f32>,
58
+ {
59
+ let start = s.find("$math.")?;
60
+ let open_rel = s[start..].find('(')?;
61
+ let open = start + open_rel;
62
+ let func = &s[start + 6..open];
63
+
64
+ // Find matching close parenthesis, handling nesting
65
+ let mut depth: i32 = 0;
66
+ let mut close_abs: Option<usize> = None;
67
+ for (i, ch) in s[open..].char_indices() {
68
+ match ch {
69
+ '(' => depth += 1,
70
+ ')' => {
71
+ depth -= 1;
72
+ if depth == 0 {
73
+ close_abs = Some(open + i);
74
+ break;
75
+ }
76
+ }
77
+ _ => {}
78
+ }
79
+ }
80
+ let close = close_abs?;
81
+
82
+ let inner = &s[open + 1..close];
83
+ let raw_args = parse_top_level_args(inner);
84
+ let mut args: Vec<f32> = Vec::with_capacity(raw_args.len());
85
+ for a in raw_args {
86
+ if let Some(v) = eval(a, vars, bpm, beat) {
87
+ args.push(v);
88
+ } else {
89
+ return None;
90
+ }
91
+ }
92
+
93
+ // If no explicit seed is provided, use $env.seed via fallback
94
+ let fallback_seed = eval("$env.seed", vars, bpm, beat).unwrap_or(0.0);
95
+ let result = eval_math_func(func, &args, fallback_seed)?;
96
+
97
+ let mut replaced = String::new();
98
+ replaced.push_str(&s[..start]);
99
+ replaced.push_str(&result.to_string());
100
+ replaced.push_str(&s[close + 1..]);
101
+ Some(replaced)
102
+ }
@@ -0,0 +1,9 @@
1
+ pub mod easing;
2
+ pub mod env;
3
+ pub mod math;
4
+ pub mod modulator;
5
+
6
+ pub use easing::find_and_eval_first_easing_call;
7
+ pub use env::resolve_env_atom;
8
+ pub use math::find_and_eval_first_math_call;
9
+ pub use modulator::find_and_eval_first_mod_call;