@devaloop/devalang 0.0.1-alpha.15 → 0.0.1-alpha.16-hotfix.0
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.
- package/.devalang +2 -0
- package/.github/workflows/ci.yml +92 -0
- package/Cargo.toml +60 -58
- package/README.md +1 -1
- package/docs/CHANGELOG.md +34 -1
- package/docs/CONTRIBUTING.md +101 -1
- package/docs/ROADMAP.md +1 -1
- package/docs/TODO.md +1 -1
- package/examples/automation.deva +1 -3
- package/examples/bank.deva +4 -4
- package/examples/events.deva +12 -0
- package/examples/function.deva +4 -4
- package/examples/index.deva +3 -5
- package/examples/loop.deva +5 -11
- package/examples/pattern.deva +8 -0
- package/examples/plugin.deva +12 -11
- package/examples/variables.deva +1 -1
- package/out-tsc/bin/index.js +51 -7
- package/out-tsc/index.js +3 -1
- package/out-tsc/scripts/postbuild.js +9 -10
- package/out-tsc/scripts/postinstall.js +49 -0
- package/package.json +12 -4
- package/project-version.json +3 -3
- package/rust/cli/bank.rs +462 -455
- package/rust/cli/build.rs +252 -199
- package/rust/cli/check.rs +221 -180
- package/rust/cli/driver.rs +297 -292
- package/rust/cli/generator.rs +1 -0
- package/rust/cli/init.rs +87 -79
- package/rust/cli/install.rs +35 -32
- package/rust/cli/login.rs +127 -134
- package/rust/cli/mod.rs +13 -11
- package/rust/cli/play.rs +1123 -218
- package/rust/cli/telemetry.rs +19 -0
- package/rust/cli/template.rs +69 -57
- package/rust/cli/update.rs +6 -4
- package/rust/common/api.rs +5 -5
- package/rust/common/mod.rs +3 -3
- package/rust/config/driver.rs +118 -94
- package/rust/config/loader.rs +165 -156
- package/rust/config/mod.rs +4 -2
- package/rust/config/settings.rs +91 -0
- package/rust/config/stats.rs +257 -0
- package/rust/core/audio/engine.rs +696 -659
- package/rust/core/audio/evaluator.rs +263 -132
- package/rust/core/audio/interpreter/arrow_call.rs +198 -187
- package/rust/core/audio/interpreter/call.rs +98 -95
- package/rust/core/audio/interpreter/condition.rs +70 -71
- package/rust/core/audio/interpreter/driver.rs +487 -231
- package/rust/core/audio/interpreter/function.rs +26 -21
- package/rust/core/audio/interpreter/let_.rs +38 -26
- package/rust/core/audio/interpreter/load.rs +18 -18
- package/rust/core/audio/interpreter/loop_.rs +113 -106
- package/rust/core/audio/interpreter/mod.rs +14 -14
- package/rust/core/audio/interpreter/sleep.rs +27 -28
- package/rust/core/audio/interpreter/spawn.rs +105 -102
- package/rust/core/audio/interpreter/tempo.rs +19 -16
- package/rust/core/audio/interpreter/trigger.rs +239 -210
- package/rust/core/audio/loader/mod.rs +1 -1
- package/rust/core/audio/loader/trigger.rs +100 -94
- package/rust/core/audio/mod.rs +7 -7
- package/rust/core/audio/player.rs +64 -64
- package/rust/core/audio/renderer.rs +56 -53
- package/rust/core/audio/special/easing.rs +189 -120
- package/rust/core/audio/special/env.rs +43 -41
- package/rust/core/audio/special/math.rs +102 -92
- package/rust/core/audio/special/mod.rs +9 -9
- package/rust/core/audio/special/modulator.rs +143 -120
- package/rust/core/builder/mod.rs +80 -85
- package/rust/core/debugger/lexer.rs +27 -27
- package/rust/core/debugger/mod.rs +24 -23
- package/rust/core/debugger/module.rs +55 -47
- package/rust/core/debugger/preprocessor.rs +27 -27
- package/rust/core/debugger/store.rs +40 -39
- package/rust/core/error/mod.rs +80 -69
- package/rust/core/lexer/handler/arrow.rs +82 -82
- package/rust/core/lexer/handler/at.rs +21 -21
- package/rust/core/lexer/handler/brace.rs +41 -41
- package/rust/core/lexer/handler/colon.rs +21 -21
- package/rust/core/lexer/handler/comment.rs +30 -30
- package/rust/core/lexer/handler/dot.rs +21 -21
- package/rust/core/lexer/handler/driver.rs +337 -292
- package/rust/core/lexer/handler/identifier.rs +46 -43
- package/rust/core/lexer/handler/indent.rs +66 -66
- package/rust/core/lexer/handler/mod.rs +16 -16
- package/rust/core/lexer/handler/newline.rs +23 -23
- package/rust/core/lexer/handler/number.rs +31 -31
- package/rust/core/lexer/handler/operator.rs +46 -46
- package/rust/core/lexer/handler/parenthesis.rs +41 -41
- package/rust/core/lexer/handler/slash.rs +21 -21
- package/rust/core/lexer/handler/string.rs +63 -63
- package/rust/core/lexer/mod.rs +54 -51
- package/rust/core/lexer/token.rs +97 -94
- package/rust/core/mod.rs +11 -11
- package/rust/core/parser/driver.rs +513 -490
- package/rust/core/parser/handler/arrow_call.rs +233 -227
- package/rust/core/parser/handler/at.rs +245 -162
- package/rust/core/parser/handler/bank.rs +94 -69
- package/rust/core/parser/handler/condition.rs +80 -74
- package/rust/core/parser/handler/dot.rs +143 -135
- package/rust/core/parser/handler/identifier/automate.rs +257 -194
- package/rust/core/parser/handler/identifier/call.rs +91 -88
- package/rust/core/parser/handler/identifier/emit.rs +66 -0
- package/rust/core/parser/handler/identifier/function.rs +100 -91
- package/rust/core/parser/handler/identifier/group.rs +85 -75
- package/rust/core/parser/handler/identifier/let_.rs +158 -143
- package/rust/core/parser/handler/identifier/mod.rs +54 -56
- package/rust/core/parser/handler/identifier/on.rs +98 -0
- package/rust/core/parser/handler/identifier/print.rs +52 -29
- package/rust/core/parser/handler/identifier/sleep.rs +36 -33
- package/rust/core/parser/handler/identifier/spawn.rs +91 -88
- package/rust/core/parser/handler/identifier/synth.rs +65 -63
- package/rust/core/parser/handler/loop_.rs +170 -89
- package/rust/core/parser/handler/mod.rs +8 -8
- package/rust/core/parser/handler/tempo.rs +53 -47
- package/rust/core/parser/mod.rs +4 -4
- package/rust/core/parser/statement.rs +142 -113
- package/rust/core/plugin/loader.rs +123 -48
- package/rust/core/plugin/mod.rs +2 -1
- package/rust/core/plugin/runner.rs +296 -0
- package/rust/core/preprocessor/loader.rs +515 -326
- package/rust/core/preprocessor/mod.rs +4 -4
- package/rust/core/preprocessor/module.rs +60 -58
- package/rust/core/preprocessor/processor.rs +99 -101
- package/rust/core/preprocessor/resolver/bank.rs +51 -48
- package/rust/core/preprocessor/resolver/call.rs +100 -101
- package/rust/core/preprocessor/resolver/condition.rs +97 -97
- package/rust/core/preprocessor/resolver/driver.rs +310 -280
- package/rust/core/preprocessor/resolver/function.rs +69 -68
- package/rust/core/preprocessor/resolver/group.rs +96 -91
- package/rust/core/preprocessor/resolver/let_.rs +32 -28
- package/rust/core/preprocessor/resolver/loop_.rs +320 -121
- package/rust/core/preprocessor/resolver/mod.rs +15 -15
- package/rust/core/preprocessor/resolver/spawn.rs +76 -73
- package/rust/core/preprocessor/resolver/synth.rs +56 -50
- package/rust/core/preprocessor/resolver/tempo.rs +50 -49
- package/rust/core/preprocessor/resolver/trigger.rs +113 -115
- package/rust/core/preprocessor/resolver/value.rs +81 -81
- package/rust/core/shared/duration.rs +9 -9
- package/rust/core/shared/mod.rs +3 -3
- package/rust/core/shared/value.rs +35 -32
- package/rust/core/store/function.rs +34 -34
- package/rust/core/store/global.rs +55 -38
- package/rust/core/store/mod.rs +5 -5
- package/rust/core/store/variable.rs +37 -34
- package/rust/core/utils/mod.rs +2 -2
- package/rust/core/utils/path.rs +37 -31
- package/rust/core/utils/validation.rs +35 -36
- package/rust/installer/addon.rs +84 -80
- package/rust/installer/bank.rs +62 -65
- package/rust/installer/mod.rs +5 -5
- package/rust/installer/plugin.rs +54 -55
- package/rust/installer/utils.rs +56 -56
- package/rust/lib.rs +156 -164
- package/rust/main.rs +250 -144
- package/rust/utils/error.rs +200 -51
- package/rust/utils/file.rs +38 -35
- package/rust/utils/first_usage.rs +76 -0
- package/rust/utils/logger.rs +195 -143
- package/rust/utils/mod.rs +9 -7
- package/rust/utils/signature.rs +19 -17
- package/rust/utils/spinner.rs +22 -19
- package/rust/utils/telemetry.rs +292 -0
- package/rust/utils/watcher.rs +34 -33
- package/templates/minimal/README.md +97 -121
- package/templates/welcome/README.md +97 -121
- package/typescript/bin/index.ts +19 -5
- package/typescript/index.ts +3 -1
- package/typescript/scripts/postbuild.ts +10 -6
- package/typescript/scripts/postinstall.ts +56 -0
- package/typescript/scripts/version/bump.ts +0 -1
- package/typescript/scripts/version/index.ts +0 -1
- package/out-tsc/bin/devalang.exe +0 -0
|
@@ -1,92 +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
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
let
|
|
60
|
-
let
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
let
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
pub mod
|
|
2
|
-
pub mod
|
|
3
|
-
pub mod
|
|
4
|
-
pub mod modulator;
|
|
5
|
-
|
|
6
|
-
pub use
|
|
7
|
-
pub use
|
|
8
|
-
pub use
|
|
9
|
-
pub use modulator::find_and_eval_first_mod_call;
|
|
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;
|
|
@@ -1,120 +1,143 @@
|
|
|
1
|
-
use crate::core::store::variable::VariableTable;
|
|
2
|
-
|
|
3
|
-
fn lfo_sine(rate_per_beat: f32, beat: f32) -> f32 {
|
|
4
|
-
// Output in [-1,1]
|
|
5
|
-
(2.0 * std::f32::consts::PI * rate_per_beat * beat).sin()
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
fn lfo_triangle(rate_per_beat: f32, beat: f32) -> f32 {
|
|
9
|
-
// Triangle in [-1,1]
|
|
10
|
-
let phase = (rate_per_beat * beat).fract();
|
|
11
|
-
// Map [0,1]->[-1,1] tri
|
|
12
|
-
4.0 * (phase - 0.5).abs() - 1.0
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
fn adsr_envelope_value_t(attack: f32, decay: f32, sustain: f32, release: f32, t: f32) -> f32 {
|
|
16
|
-
let a = attack.max(0.0);
|
|
17
|
-
let d = decay.max(0.0);
|
|
18
|
-
let r = release.max(0.0);
|
|
19
|
-
let s = sustain.clamp(0.0, 1.0);
|
|
20
|
-
|
|
21
|
-
// Normalize phases so that the whole ADSR spans t in [0,1]
|
|
22
|
-
let total = (a + d + r).max(1e-6);
|
|
23
|
-
let ap = a / total;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
let
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
let
|
|
107
|
-
|
|
108
|
-
let
|
|
109
|
-
let
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
let
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
1
|
+
use crate::core::store::variable::VariableTable;
|
|
2
|
+
|
|
3
|
+
fn lfo_sine(rate_per_beat: f32, beat: f32) -> f32 {
|
|
4
|
+
// Output in [-1,1]
|
|
5
|
+
(2.0 * std::f32::consts::PI * rate_per_beat * beat).sin()
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
fn lfo_triangle(rate_per_beat: f32, beat: f32) -> f32 {
|
|
9
|
+
// Triangle in [-1,1]
|
|
10
|
+
let phase = (rate_per_beat * beat).fract();
|
|
11
|
+
// Map [0,1]->[-1,1] tri
|
|
12
|
+
4.0 * (phase - 0.5).abs() - 1.0
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
fn adsr_envelope_value_t(attack: f32, decay: f32, sustain: f32, release: f32, t: f32) -> f32 {
|
|
16
|
+
let a = attack.max(0.0);
|
|
17
|
+
let d = decay.max(0.0);
|
|
18
|
+
let r = release.max(0.0);
|
|
19
|
+
let s = sustain.clamp(0.0, 1.0);
|
|
20
|
+
|
|
21
|
+
// Normalize phases so that the whole ADSR spans t in [0,1]
|
|
22
|
+
let total = (a + d + r).max(1e-6);
|
|
23
|
+
let ap = a / total;
|
|
24
|
+
let dp = d / total;
|
|
25
|
+
let rp = r / total;
|
|
26
|
+
|
|
27
|
+
if t < ap {
|
|
28
|
+
// attack (0->1)
|
|
29
|
+
if ap > 0.0 { t / ap } else { 1.0 }
|
|
30
|
+
} else if t < ap + dp {
|
|
31
|
+
// decay (1->sustain)
|
|
32
|
+
let u = (t - ap) / dp.max(1e-6);
|
|
33
|
+
1.0 - (1.0 - s) * u
|
|
34
|
+
} else if t < 1.0 - rp {
|
|
35
|
+
// sustain
|
|
36
|
+
s
|
|
37
|
+
} else {
|
|
38
|
+
// release (sustain->0)
|
|
39
|
+
let u = (t - (1.0 - rp)) / rp.max(1e-6);
|
|
40
|
+
s * (1.0 - u)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
fn eval_mod_func(func: &str, args: &[f32], beat: f32) -> Option<f32> {
|
|
45
|
+
match func {
|
|
46
|
+
"lfo.sine" => {
|
|
47
|
+
let rate = args.get(0).copied().unwrap_or(1.0);
|
|
48
|
+
Some(lfo_sine(rate, beat))
|
|
49
|
+
}
|
|
50
|
+
"lfo.tri" | "lfo.triangle" => {
|
|
51
|
+
let rate = args.get(0).copied().unwrap_or(1.0);
|
|
52
|
+
Some(lfo_triangle(rate, beat))
|
|
53
|
+
}
|
|
54
|
+
// ADSR envelope normalized over t in [0,1]
|
|
55
|
+
// $mod.envelope(attack, decay, sustain, release, t)
|
|
56
|
+
"envelope" | "mod.envelope" => {
|
|
57
|
+
if args.len() >= 5 {
|
|
58
|
+
Some(adsr_envelope_value_t(
|
|
59
|
+
args[0],
|
|
60
|
+
args[1],
|
|
61
|
+
args[2],
|
|
62
|
+
args[3],
|
|
63
|
+
args[4].clamp(0.0, 1.0),
|
|
64
|
+
))
|
|
65
|
+
} else {
|
|
66
|
+
None
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
_ => None,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
fn parse_top_level_args(s: &str) -> Vec<&str> {
|
|
74
|
+
let mut args = Vec::new();
|
|
75
|
+
let mut depth = 0i32;
|
|
76
|
+
let mut start = 0usize;
|
|
77
|
+
for (i, ch) in s.char_indices() {
|
|
78
|
+
match ch {
|
|
79
|
+
'(' => depth += 1,
|
|
80
|
+
')' => depth -= 1,
|
|
81
|
+
',' if depth == 0 => {
|
|
82
|
+
args.push(s[start..i].trim());
|
|
83
|
+
start = i + 1;
|
|
84
|
+
}
|
|
85
|
+
_ => {}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
let last = s[start..].trim();
|
|
89
|
+
if !last.is_empty() {
|
|
90
|
+
args.push(last);
|
|
91
|
+
}
|
|
92
|
+
args
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Find and evaluate the first $mod.<fn>(...) occurrence in the string.
|
|
96
|
+
pub fn find_and_eval_first_mod_call<EvalFn>(
|
|
97
|
+
s: &str,
|
|
98
|
+
eval: EvalFn,
|
|
99
|
+
vars: &VariableTable,
|
|
100
|
+
bpm: f32,
|
|
101
|
+
beat: f32,
|
|
102
|
+
) -> Option<String>
|
|
103
|
+
where
|
|
104
|
+
EvalFn: Fn(&str, &VariableTable, f32, f32) -> Option<f32>,
|
|
105
|
+
{
|
|
106
|
+
let start = s.find("$mod.")?;
|
|
107
|
+
let open_rel = s[start..].find('(')?;
|
|
108
|
+
let open = start + open_rel;
|
|
109
|
+
let func = &s[start + 5..open];
|
|
110
|
+
|
|
111
|
+
// matching close
|
|
112
|
+
let mut depth: i32 = 0;
|
|
113
|
+
let mut close_abs: Option<usize> = None;
|
|
114
|
+
for (i, ch) in s[open..].char_indices() {
|
|
115
|
+
match ch {
|
|
116
|
+
'(' => depth += 1,
|
|
117
|
+
')' => {
|
|
118
|
+
depth -= 1;
|
|
119
|
+
if depth == 0 {
|
|
120
|
+
close_abs = Some(open + i);
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
_ => {}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
let close = close_abs?;
|
|
128
|
+
|
|
129
|
+
let inner = &s[open + 1..close];
|
|
130
|
+
let raw_args = parse_top_level_args(inner);
|
|
131
|
+
let mut args: Vec<f32> = Vec::with_capacity(raw_args.len());
|
|
132
|
+
for a in raw_args {
|
|
133
|
+
args.push(eval(a, vars, bpm, beat)?);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let result = eval_mod_func(func, &args, beat)?;
|
|
137
|
+
|
|
138
|
+
let mut replaced = String::new();
|
|
139
|
+
replaced.push_str(&s[..start]);
|
|
140
|
+
replaced.push_str(&result.to_string());
|
|
141
|
+
replaced.push_str(&s[close + 1..]);
|
|
142
|
+
Some(replaced)
|
|
143
|
+
}
|
package/rust/core/builder/mod.rs
CHANGED
|
@@ -1,85 +1,80 @@
|
|
|
1
|
-
use crate::core::audio::renderer::render_audio_with_modules;
|
|
2
|
-
use crate::core::parser::statement::Statement;
|
|
3
|
-
use crate::core::store::global::GlobalStore;
|
|
4
|
-
use
|
|
5
|
-
use std::io::Write;
|
|
6
|
-
use
|
|
7
|
-
|
|
8
|
-
pub struct Builder {}
|
|
9
|
-
|
|
10
|
-
impl Builder {
|
|
11
|
-
pub fn new() -> Self {
|
|
12
|
-
Builder {}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
pub fn build_ast(
|
|
16
|
-
&self,
|
|
17
|
-
modules: &HashMap<String, Vec<Statement>>,
|
|
18
|
-
out_dir: &str,
|
|
19
|
-
compress: bool
|
|
20
|
-
) {
|
|
21
|
-
for (name, statements) in modules {
|
|
22
|
-
let formatted_name = name.split("/").last().unwrap_or(name);
|
|
23
|
-
let formatted_name = formatted_name.replace(".deva", "");
|
|
24
|
-
|
|
25
|
-
create_dir_all(format!("{}/ast", out_dir)).expect("Failed to create AST directory");
|
|
26
|
-
|
|
27
|
-
let file_path = format!("{}/ast/{}.json", out_dir, formatted_name);
|
|
28
|
-
let mut file = std::fs::File::create(file_path).expect("Failed to create AST file");
|
|
29
|
-
let content = if compress {
|
|
30
|
-
serde_json::to_string(&statements).expect("Failed to serialize AST")
|
|
31
|
-
} else {
|
|
32
|
-
serde_json::to_string_pretty(&statements).expect("Failed to serialize AST")
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
file.write_all(content.as_bytes())
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
&normalized_output_dir,
|
|
50
|
-
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
&
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
1
|
+
use crate::core::audio::renderer::render_audio_with_modules;
|
|
2
|
+
use crate::core::parser::statement::Statement;
|
|
3
|
+
use crate::core::store::global::GlobalStore;
|
|
4
|
+
use crate::utils::logger::Logger;
|
|
5
|
+
use std::io::Write;
|
|
6
|
+
use std::{collections::HashMap, fs::create_dir_all};
|
|
7
|
+
|
|
8
|
+
pub struct Builder {}
|
|
9
|
+
|
|
10
|
+
impl Builder {
|
|
11
|
+
pub fn new() -> Self {
|
|
12
|
+
Builder {}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
pub fn build_ast(
|
|
16
|
+
&self,
|
|
17
|
+
modules: &HashMap<String, Vec<Statement>>,
|
|
18
|
+
out_dir: &str,
|
|
19
|
+
compress: bool,
|
|
20
|
+
) {
|
|
21
|
+
for (name, statements) in modules {
|
|
22
|
+
let formatted_name = name.split("/").last().unwrap_or(name);
|
|
23
|
+
let formatted_name = formatted_name.replace(".deva", "");
|
|
24
|
+
|
|
25
|
+
create_dir_all(format!("{}/ast", out_dir)).expect("Failed to create AST directory");
|
|
26
|
+
|
|
27
|
+
let file_path = format!("{}/ast/{}.json", out_dir, formatted_name);
|
|
28
|
+
let mut file = std::fs::File::create(file_path).expect("Failed to create AST file");
|
|
29
|
+
let content = if compress {
|
|
30
|
+
serde_json::to_string(&statements).expect("Failed to serialize AST")
|
|
31
|
+
} else {
|
|
32
|
+
serde_json::to_string_pretty(&statements).expect("Failed to serialize AST")
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
file.write_all(content.as_bytes())
|
|
36
|
+
.expect("Failed to write AST to file");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
pub fn build_audio(
|
|
41
|
+
&self,
|
|
42
|
+
modules: &HashMap<String, Vec<Statement>>,
|
|
43
|
+
normalized_output_dir: &str,
|
|
44
|
+
global_store: &mut GlobalStore,
|
|
45
|
+
) {
|
|
46
|
+
let logger = Logger::new();
|
|
47
|
+
|
|
48
|
+
let audio_engines =
|
|
49
|
+
render_audio_with_modules(modules.clone(), &normalized_output_dir, global_store);
|
|
50
|
+
|
|
51
|
+
create_dir_all(format!("{}/audio", normalized_output_dir))
|
|
52
|
+
.expect("Failed to create audio directory");
|
|
53
|
+
|
|
54
|
+
for (module_name, mut audio_engine) in audio_engines {
|
|
55
|
+
let formatted_module_name = module_name
|
|
56
|
+
.split('/')
|
|
57
|
+
.last()
|
|
58
|
+
.unwrap_or(&module_name)
|
|
59
|
+
.replace(".deva", "");
|
|
60
|
+
|
|
61
|
+
let output_path = format!(
|
|
62
|
+
"{}/audio/{}.wav",
|
|
63
|
+
normalized_output_dir, formatted_module_name
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
match audio_engine.generate_wav_file(&output_path) {
|
|
67
|
+
Ok(_) => {}
|
|
68
|
+
Err(msg) => {
|
|
69
|
+
logger.log_error_with_stacktrace(
|
|
70
|
+
&format!(
|
|
71
|
+
"Unable to generate WAV file for module '{}': {}",
|
|
72
|
+
formatted_module_name, msg
|
|
73
|
+
),
|
|
74
|
+
&module_name,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|