@devaloop/devalang 0.0.1-beta.1 → 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 (220) hide show
  1. package/.devalang +9 -10
  2. package/Cargo.toml +5 -4
  3. package/README.md +7 -5
  4. package/docs/CHANGELOG.md +42 -0
  5. package/docs/ROADMAP.md +5 -1
  6. package/docs/TODO.md +3 -14
  7. package/examples/bus.deva +10 -0
  8. package/examples/effect.deva +2 -0
  9. package/examples/filter.deva +11 -0
  10. package/examples/lfo.deva +9 -0
  11. package/examples/synth.deva +11 -1
  12. package/examples/synth_types.deva +17 -0
  13. package/out-tsc/core/functions/index.d.ts +5 -0
  14. package/out-tsc/core/functions/index.js +11 -0
  15. package/out-tsc/pkg/devalang_core.d.ts +2 -0
  16. package/out-tsc/pkg/devalang_core.js +17 -2
  17. package/out-tsc/pkg/devalang_core_bg.wasm.d.ts +8 -7
  18. package/package.json +1 -1
  19. package/project-version.json +3 -3
  20. package/rust/cli/bank/api.rs +122 -122
  21. package/rust/cli/bank/commands.rs +33 -2
  22. package/rust/cli/bank/mod.rs +29 -29
  23. package/rust/cli/build/commands.rs +53 -3
  24. package/rust/cli/build/mod.rs +2 -2
  25. package/rust/cli/build/process.rs +26 -7
  26. package/rust/cli/check/mod.rs +2 -2
  27. package/rust/cli/discover/commands.rs +253 -253
  28. package/rust/cli/discover/config.rs +111 -111
  29. package/rust/cli/discover/fs.rs +19 -19
  30. package/rust/cli/discover/install.rs +103 -103
  31. package/rust/cli/discover/metadata.rs +48 -48
  32. package/rust/cli/discover/mod.rs +5 -5
  33. package/rust/cli/install/addon.rs +118 -118
  34. package/rust/cli/install/bank.rs +22 -3
  35. package/rust/cli/install/commands.rs +35 -35
  36. package/rust/cli/install/mod.rs +4 -4
  37. package/rust/cli/install/plugin.rs +80 -61
  38. package/rust/cli/login/commands.rs +124 -124
  39. package/rust/cli/mod.rs +12 -12
  40. package/rust/cli/parser.rs +46 -1
  41. package/rust/cli/play/commands.rs +71 -20
  42. package/rust/cli/play/mod.rs +5 -5
  43. package/rust/cli/play/process.rs +14 -5
  44. package/rust/cli/play/realtime.rs +91 -91
  45. package/rust/cli/telemetry/commands.rs +22 -22
  46. package/rust/cli/telemetry/event_creator.rs +80 -80
  47. package/rust/cli/telemetry/mod.rs +3 -3
  48. package/rust/cli/telemetry/send.rs +51 -51
  49. package/rust/cli/template/commands.rs +69 -69
  50. package/rust/config/driver.rs +112 -103
  51. package/rust/config/mod.rs +3 -3
  52. package/rust/config/ops.rs +26 -26
  53. package/rust/config/settings.rs +101 -101
  54. package/rust/core/audio/engine/driver.rs +220 -0
  55. package/rust/core/audio/engine/export.rs +169 -0
  56. package/rust/core/audio/engine/helpers.rs +178 -170
  57. package/rust/core/audio/engine/mod.rs +51 -2
  58. package/rust/core/audio/engine/notes/dsp.rs +85 -0
  59. package/rust/core/audio/engine/notes/mod.rs +44 -0
  60. package/rust/core/audio/engine/notes/params.rs +294 -0
  61. package/rust/core/audio/engine/sample/insert.rs +199 -0
  62. package/rust/core/audio/engine/sample/mod.rs +40 -0
  63. package/rust/core/audio/engine/sample/padding.rs +170 -0
  64. package/rust/core/audio/evaluator/condition.rs +61 -0
  65. package/rust/core/audio/evaluator/mod.rs +9 -0
  66. package/rust/core/audio/{evaluator.rs → evaluator/numeric.rs} +1 -159
  67. package/rust/core/audio/evaluator/rhs.rs +16 -0
  68. package/rust/core/audio/evaluator/string_expr.rs +94 -0
  69. package/rust/core/audio/interpreter/driver.rs +55 -23
  70. package/rust/core/audio/interpreter/mod.rs +1 -13
  71. package/rust/core/audio/interpreter/statements/arrow_call/interprete.rs +175 -0
  72. package/rust/core/audio/interpreter/statements/arrow_call/methods/chord.rs +384 -0
  73. package/rust/core/audio/interpreter/statements/arrow_call/methods/mod.rs +2 -0
  74. package/rust/core/audio/interpreter/statements/arrow_call/methods/note.rs +316 -0
  75. package/rust/core/audio/interpreter/statements/arrow_call/mod.rs +3 -0
  76. package/rust/core/audio/interpreter/statements/arrow_call/types/arp.rs +192 -0
  77. package/rust/core/audio/interpreter/statements/arrow_call/types/mod.rs +24 -0
  78. package/rust/core/audio/interpreter/statements/arrow_call/types/pad.rs +116 -0
  79. package/rust/core/audio/interpreter/statements/arrow_call/types/pluck.rs +97 -0
  80. package/rust/core/audio/interpreter/statements/arrow_call/types/sub.rs +100 -0
  81. package/rust/core/audio/interpreter/{automate.rs → statements/automate.rs} +16 -18
  82. package/rust/core/audio/interpreter/{call.rs → statements/call.rs} +5 -4
  83. package/rust/core/audio/interpreter/{condition.rs → statements/condition.rs} +2 -1
  84. package/rust/core/audio/interpreter/{function.rs → statements/function.rs} +2 -4
  85. package/rust/core/audio/interpreter/{let_.rs → statements/let_.rs} +2 -4
  86. package/rust/core/audio/interpreter/{load.rs → statements/load.rs} +2 -4
  87. package/rust/core/audio/interpreter/{loop_.rs → statements/loop_.rs} +2 -1
  88. package/rust/core/audio/interpreter/statements/mod.rs +12 -0
  89. package/rust/core/audio/interpreter/{sleep.rs → statements/sleep.rs} +28 -28
  90. package/rust/core/audio/interpreter/{spawn.rs → statements/spawn.rs} +3 -2
  91. package/rust/core/audio/interpreter/{tempo.rs → statements/tempo.rs} +40 -40
  92. package/rust/core/audio/interpreter/{trigger.rs → statements/trigger.rs} +1 -1
  93. package/rust/core/audio/loader/trigger.rs +2 -1
  94. package/rust/core/audio/mod.rs +6 -7
  95. package/rust/core/audio/player.rs +70 -70
  96. package/rust/core/audio/special/easing.rs +189 -189
  97. package/rust/core/audio/special/env.rs +45 -45
  98. package/rust/core/audio/special/math.rs +134 -134
  99. package/rust/core/audio/special/mod.rs +9 -9
  100. package/rust/core/audio/special/modulator.rs +143 -143
  101. package/rust/core/builder/mod.rs +45 -2
  102. package/rust/core/debugger/lexer.rs +27 -27
  103. package/rust/core/debugger/{module.rs → logs.rs} +3 -6
  104. package/rust/core/debugger/mod.rs +30 -30
  105. package/rust/core/debugger/preprocessor.rs +27 -27
  106. package/rust/core/debugger/store.rs +2 -4
  107. package/rust/core/error/mod.rs +269 -269
  108. package/rust/core/lexer/driver.rs +59 -61
  109. package/rust/core/lexer/handler/arrow.rs +82 -82
  110. package/rust/core/lexer/handler/at.rs +21 -21
  111. package/rust/core/lexer/handler/brace.rs +41 -41
  112. package/rust/core/lexer/handler/colon.rs +21 -21
  113. package/rust/core/lexer/handler/comment.rs +30 -30
  114. package/rust/core/lexer/handler/dot.rs +21 -21
  115. package/rust/core/lexer/handler/driver.rs +337 -337
  116. package/rust/core/lexer/handler/identifier.rs +47 -47
  117. package/rust/core/lexer/handler/indent.rs +66 -66
  118. package/rust/core/lexer/handler/mod.rs +15 -15
  119. package/rust/core/lexer/handler/newline.rs +23 -23
  120. package/rust/core/lexer/handler/number.rs +31 -31
  121. package/rust/core/lexer/handler/operator.rs +46 -46
  122. package/rust/core/lexer/handler/parenthesis.rs +41 -41
  123. package/rust/core/lexer/handler/slash.rs +21 -21
  124. package/rust/core/lexer/handler/string.rs +63 -63
  125. package/rust/core/lexer/mod.rs +3 -3
  126. package/rust/core/mod.rs +0 -1
  127. package/rust/core/parser/driver/block.rs +111 -0
  128. package/rust/core/parser/driver/cursor.rs +82 -0
  129. package/rust/core/parser/driver/driver_impl.rs +139 -0
  130. package/rust/core/parser/driver/mod.rs +6 -0
  131. package/rust/core/parser/driver/parse_array.rs +120 -0
  132. package/rust/core/parser/driver/parse_map.rs +223 -0
  133. package/rust/core/parser/driver/parser.rs +160 -0
  134. package/rust/core/parser/handler/arrow_call.rs +28 -4
  135. package/rust/core/parser/handler/at.rs +279 -279
  136. package/rust/core/parser/handler/bank.rs +104 -104
  137. package/rust/core/parser/handler/condition.rs +83 -83
  138. package/rust/core/parser/handler/dot.rs +148 -148
  139. package/rust/core/parser/handler/identifier/automate.rs +254 -254
  140. package/rust/core/parser/handler/identifier/call.rs +91 -91
  141. package/rust/core/parser/handler/identifier/emit.rs +70 -70
  142. package/rust/core/parser/handler/identifier/function.rs +113 -113
  143. package/rust/core/parser/handler/identifier/group.rs +89 -89
  144. package/rust/core/parser/handler/identifier/let_.rs +173 -173
  145. package/rust/core/parser/handler/identifier/mod.rs +55 -55
  146. package/rust/core/parser/handler/identifier/on.rs +107 -107
  147. package/rust/core/parser/handler/identifier/print.rs +49 -49
  148. package/rust/core/parser/handler/identifier/sleep.rs +96 -43
  149. package/rust/core/parser/handler/identifier/spawn.rs +91 -91
  150. package/rust/core/parser/handler/identifier/synth.rs +135 -135
  151. package/rust/core/parser/handler/loop_.rs +194 -194
  152. package/rust/core/parser/handler/mod.rs +9 -9
  153. package/rust/core/parser/handler/pattern.rs +1 -1
  154. package/rust/core/parser/handler/tempo.rs +105 -57
  155. package/rust/core/parser/statement.rs +10 -11
  156. package/rust/core/plugin/loader.rs +1 -1
  157. package/rust/core/plugin/mod.rs +2 -2
  158. package/rust/core/plugin/runner/mod.rs +11 -0
  159. package/rust/core/plugin/{runner.rs → runner/non_wasm.rs} +297 -347
  160. package/rust/core/plugin/runner/wasm32.rs +43 -0
  161. package/rust/core/preprocessor/loader/inject.rs +278 -0
  162. package/rust/core/preprocessor/loader/loader_helpers.rs +110 -0
  163. package/rust/core/preprocessor/loader/mod.rs +235 -0
  164. package/rust/core/preprocessor/module.rs +2 -7
  165. package/rust/core/preprocessor/{processor.rs → processor/handlers.rs} +6 -13
  166. package/rust/core/preprocessor/processor/mod.rs +1 -0
  167. package/rust/core/preprocessor/resolver/bank.rs +49 -49
  168. package/rust/core/preprocessor/resolver/call.rs +124 -124
  169. package/rust/core/preprocessor/resolver/condition.rs +95 -95
  170. package/rust/core/preprocessor/resolver/driver.rs +324 -324
  171. package/rust/core/preprocessor/resolver/function.rs +2 -2
  172. package/rust/core/preprocessor/resolver/group.rs +46 -18
  173. package/rust/core/preprocessor/resolver/let_.rs +32 -32
  174. package/rust/core/preprocessor/resolver/loop_.rs +318 -318
  175. package/rust/core/preprocessor/resolver/mod.rs +16 -16
  176. package/rust/core/preprocessor/resolver/pattern.rs +83 -83
  177. package/rust/core/preprocessor/resolver/spawn.rs +99 -99
  178. package/rust/core/preprocessor/resolver/synth.rs +54 -54
  179. package/rust/core/preprocessor/resolver/tempo.rs +48 -48
  180. package/rust/core/preprocessor/resolver/trigger.rs +116 -116
  181. package/rust/core/preprocessor/resolver/value.rs +176 -176
  182. package/rust/core/store/global.rs +2 -6
  183. package/rust/core/store/mod.rs +1 -5
  184. package/rust/lib.rs +18 -3
  185. package/rust/main.rs +27 -3
  186. package/rust/types/Cargo.toml +1 -1
  187. package/rust/types/src/addons.rs +55 -55
  188. package/rust/types/src/config.rs +84 -74
  189. package/rust/types/src/lib.rs +15 -12
  190. package/rust/types/src/plugin.rs +20 -0
  191. package/rust/types/src/store.rs +139 -0
  192. package/rust/types/src/telemetry.rs +85 -85
  193. package/rust/utils/Cargo.toml +2 -2
  194. package/rust/utils/src/file.rs +94 -94
  195. package/rust/utils/src/first_usage.rs +97 -97
  196. package/rust/utils/src/lib.rs +9 -9
  197. package/rust/utils/src/logger.rs +200 -200
  198. package/rust/utils/src/path.rs +129 -88
  199. package/rust/utils/src/signature.rs +41 -41
  200. package/rust/utils/src/spinner.rs +20 -20
  201. package/rust/utils/src/version.rs +27 -27
  202. package/rust/utils/src/watcher.rs +46 -46
  203. package/rust/web/api.rs +5 -5
  204. package/rust/web/cdn.rs +34 -34
  205. package/rust/web/mod.rs +3 -3
  206. package/tests/integration.rs +21 -21
  207. package/typescript/core/functions/index.ts +11 -0
  208. package/typescript/pkg/devalang_core.ts +20 -4
  209. package/rust/core/audio/engine/sample.rs +0 -366
  210. package/rust/core/audio/engine/synth.rs +0 -325
  211. package/rust/core/audio/interpreter/arrow_call.rs +0 -311
  212. package/rust/core/audio/renderer.rs +0 -54
  213. package/rust/core/parser/driver.rs +0 -584
  214. package/rust/core/preprocessor/loader.rs +0 -637
  215. package/rust/core/store/export.rs +0 -28
  216. package/rust/core/store/function.rs +0 -40
  217. package/rust/core/store/import.rs +0 -28
  218. package/rust/core/store/variable.rs +0 -51
  219. package/rust/core/utils/mod.rs +0 -1
  220. package/rust/core/utils/path.rs +0 -37
@@ -0,0 +1,294 @@
1
+ use devalang_types::Value;
2
+ use std::collections::HashMap;
3
+
4
+ pub struct FilterSpec {
5
+ pub kind: String,
6
+ pub cutoff: f32,
7
+ }
8
+
9
+ pub struct FilterState {
10
+ pub prev_l: f32,
11
+ pub prev_r: f32,
12
+ pub prev_in_l: f32,
13
+ pub prev_in_r: f32,
14
+ pub prev_out_l: f32,
15
+ pub prev_out_r: f32,
16
+ }
17
+
18
+ pub struct NoteSetup {
19
+ pub sample_rate: f32,
20
+ pub channels: usize,
21
+ pub total_samples: usize,
22
+ pub start_sample: usize,
23
+ pub attack_samples: usize,
24
+ pub decay_samples: usize,
25
+ pub release_samples: usize,
26
+ pub sustain_level: f32,
27
+ pub pluck_click: f32,
28
+ pub pluck_click_samples: usize,
29
+ pub drive: f32,
30
+ pub filters: Vec<FilterSpec>,
31
+ pub filter_states: Vec<FilterState>,
32
+ pub lfo_rate: f32,
33
+ pub lfo_depth: f32,
34
+ pub lfo_target: Option<String>,
35
+ pub voices: usize,
36
+ pub unison_detune: f32,
37
+ pub volume_env: HashMap<String, Value>,
38
+ pub pan_env: HashMap<String, Value>,
39
+ pub pitch_env: HashMap<String, Value>,
40
+ }
41
+
42
+ pub fn build_note_setup(
43
+ engine: &mut crate::core::audio::engine::AudioEngine,
44
+ _waveform: &str,
45
+ freq: f32,
46
+ amp: f32,
47
+ start_time_ms: f32,
48
+ mut duration_ms: f32,
49
+ synth_params: &HashMap<String, Value>,
50
+ note_params: &HashMap<String, Value>,
51
+ automation: &Option<HashMap<String, Value>>,
52
+ ) -> NoteSetup {
53
+ use crate::core::audio::engine::helpers;
54
+
55
+ // Extract ADSR and normalize units
56
+ let attack = engine.extract_f32(synth_params, "attack").unwrap_or(0.0);
57
+ let decay = engine.extract_f32(synth_params, "decay").unwrap_or(0.0);
58
+ let sustain = engine.extract_f32(synth_params, "sustain").unwrap_or(1.0);
59
+ let release = engine.extract_f32(synth_params, "release").unwrap_or(0.0);
60
+ let _sustain_level = if sustain > 1.0 {
61
+ (sustain / 100.0).clamp(0.0, 1.0)
62
+ } else {
63
+ sustain.clamp(0.0, 1.0)
64
+ };
65
+
66
+ if let Some(g) = engine.extract_f32(note_params, "gate") {
67
+ if g > 0.0 && g <= 1.0 {
68
+ duration_ms = duration_ms * g;
69
+ } else if g > 1.0 {
70
+ duration_ms = duration_ms * (g / 100.0);
71
+ }
72
+ } else if let Some(g) = engine.extract_f32(synth_params, "gate") {
73
+ if g > 0.0 && g <= 1.0 {
74
+ duration_ms = duration_ms * g;
75
+ } else if g > 1.0 {
76
+ duration_ms = duration_ms * (g / 100.0);
77
+ }
78
+ }
79
+
80
+ let velocity = engine.extract_f32(note_params, "velocity").unwrap_or(1.0);
81
+
82
+ let detune_cents = engine
83
+ .extract_f32(note_params, "detune")
84
+ .or(engine.extract_f32(synth_params, "detune"))
85
+ .unwrap_or(0.0);
86
+
87
+ let _lowpass_cut = engine
88
+ .extract_f32(note_params, "lowpass")
89
+ .or(engine.extract_f32(synth_params, "lowpass"))
90
+ .unwrap_or(0.0);
91
+
92
+ let _amplitude = (i16::MAX as f32) * amp.clamp(0.0, 1.0) * velocity.clamp(0.0, 1.0);
93
+
94
+ let _freq_start = freq;
95
+ let mut _freq_end = freq;
96
+ let _amp_start = amp * velocity.clamp(0.0, 1.0);
97
+ let mut _amp_end = _amp_start;
98
+
99
+ let glide = engine
100
+ .extract_boolean(note_params, "glide")
101
+ .unwrap_or(false);
102
+ let slide = engine
103
+ .extract_boolean(note_params, "slide")
104
+ .unwrap_or(false);
105
+ if glide {
106
+ if let Some(Value::Number(target_freq)) = note_params.get("target_freq") {
107
+ _freq_end = *target_freq;
108
+ } else {
109
+ _freq_end = freq * 1.5;
110
+ }
111
+ }
112
+ if slide {
113
+ if let Some(Value::Number(target_amp)) = note_params.get("target_amp") {
114
+ _amp_end = *target_amp * velocity.clamp(0.0, 1.0);
115
+ } else {
116
+ _amp_end = _amp_start * 0.5;
117
+ }
118
+ }
119
+
120
+ let sample_rate = engine.sample_rate as f32;
121
+ let channels = engine.channels as usize;
122
+
123
+ let total_samples = ((duration_ms / 1000.0) * sample_rate) as usize;
124
+ let start_sample = ((start_time_ms / 1000.0) * sample_rate) as usize;
125
+
126
+ // MIDI event
127
+ let midi_note_f = 69.0 + 12.0 * (_freq_start / 440.0).log2();
128
+ let midi_note = midi_note_f.round().clamp(0.0, 127.0) as u8;
129
+ let midi_vel = (velocity.clamp(0.0, 1.0) * 127.0).round().clamp(0.0, 127.0) as u8;
130
+ engine
131
+ .midi_events
132
+ .push(crate::core::audio::engine::driver::MidiNoteEvent {
133
+ key: midi_note,
134
+ vel: midi_vel,
135
+ start_ms: start_time_ms as u32,
136
+ duration_ms: duration_ms as u32,
137
+ channel: 0,
138
+ });
139
+
140
+ let _detune_factor = (2.0_f32).powf(detune_cents / 1200.0);
141
+
142
+ let (_volume_env, _pan_env, _pitch_env) = helpers::env_maps_from_automation(automation);
143
+
144
+ let attack_s = if attack > 10.0 {
145
+ attack / 1000.0
146
+ } else {
147
+ attack
148
+ };
149
+ let decay_s = if decay > 10.0 { decay / 1000.0 } else { decay };
150
+ let release_s = if release > 10.0 {
151
+ release / 1000.0
152
+ } else {
153
+ release
154
+ };
155
+ let sustain_level = _sustain_level;
156
+
157
+ let attack_samples = (attack_s * sample_rate) as usize;
158
+ let decay_samples = (decay_s * sample_rate) as usize;
159
+ let release_samples = (release_s * sample_rate) as usize;
160
+
161
+ // optional pluck click
162
+ let pluck_click = engine
163
+ .extract_f32(note_params, "pluck_click")
164
+ .or(engine.extract_f32(synth_params, "pluck_click"))
165
+ .unwrap_or(0.0);
166
+ let pluck_click_ms = engine
167
+ .extract_f32(note_params, "pluck_click_ms")
168
+ .or(engine.extract_f32(synth_params, "pluck_click_ms"))
169
+ .unwrap_or(10.0);
170
+ let pluck_click_samples = ((pluck_click_ms / 1000.0) * sample_rate) as usize;
171
+
172
+ let drive = engine
173
+ .extract_f32(note_params, "drive")
174
+ .or(engine.extract_f32(synth_params, "drive"))
175
+ .unwrap_or(0.0);
176
+
177
+ // parse filter specs
178
+ let mut raw_filters: Vec<HashMap<String, Value>> = Vec::new();
179
+ if let Some(Value::Array(arr)) = synth_params.get("filters") {
180
+ for v in arr {
181
+ if let Value::Map(m) = v {
182
+ raw_filters.push(m.clone());
183
+ }
184
+ }
185
+ }
186
+ if let Some(Value::Array(arr)) = note_params.get("filters") {
187
+ for v in arr {
188
+ if let Value::Map(m) = v {
189
+ raw_filters.push(m.clone());
190
+ }
191
+ }
192
+ }
193
+
194
+ let mut filters: Vec<FilterSpec> = Vec::new();
195
+ let mut filter_states: Vec<FilterState> = Vec::new();
196
+ for rf in raw_filters.into_iter() {
197
+ let kind = rf
198
+ .get("type")
199
+ .and_then(|v| match v {
200
+ Value::String(s) => Some(s.clone()),
201
+ Value::Identifier(s) => Some(s.clone()),
202
+ _ => None,
203
+ })
204
+ .unwrap_or_else(|| "lowpass".to_string());
205
+ let cutoff = rf
206
+ .get("cutoff")
207
+ .and_then(|v| match v {
208
+ Value::Number(n) => Some(*n),
209
+ Value::String(s) => s.parse::<f32>().ok(),
210
+ _ => None,
211
+ })
212
+ .unwrap_or(1000.0);
213
+ filters.push(FilterSpec {
214
+ kind: kind.to_lowercase(),
215
+ cutoff,
216
+ });
217
+ filter_states.push(FilterState {
218
+ prev_l: 0.0,
219
+ prev_r: 0.0,
220
+ prev_in_l: 0.0,
221
+ prev_in_r: 0.0,
222
+ prev_out_l: 0.0,
223
+ prev_out_r: 0.0,
224
+ });
225
+ }
226
+
227
+ // LFO parsing (from synth or note) - simplified: prefer note params over synth params
228
+ let mut lfo_rate = 0.0f32;
229
+ let mut lfo_depth = 0.0f32;
230
+ let mut lfo_target: Option<String> = None;
231
+ if let Some(Value::Map(m)) = synth_params.get("lfo") {
232
+ if let Some(Value::Number(r)) = m.get("rate") {
233
+ lfo_rate = *r;
234
+ }
235
+ if let Some(Value::Number(d)) = m.get("depth") {
236
+ lfo_depth = *d;
237
+ }
238
+ if let Some(Value::String(t)) = m.get("target") {
239
+ lfo_target = Some(t.clone());
240
+ }
241
+ }
242
+ if let Some(Value::Map(m)) = note_params.get("lfo") {
243
+ if let Some(Value::Number(r)) = m.get("rate") {
244
+ lfo_rate = *r;
245
+ }
246
+ if let Some(Value::Number(d)) = m.get("depth") {
247
+ lfo_depth = *d;
248
+ }
249
+ if let Some(Value::String(t)) = m.get("target") {
250
+ lfo_target = Some(t.clone());
251
+ }
252
+ }
253
+
254
+ let voices = engine
255
+ .extract_f32(note_params, "voices")
256
+ .or(engine.extract_f32(synth_params, "voices"))
257
+ .unwrap_or(1.0)
258
+ .max(1.0)
259
+ .round() as usize;
260
+ let unison_detune = engine
261
+ .extract_f32(note_params, "unison_detune")
262
+ .or(engine.extract_f32(synth_params, "unison_detune"))
263
+ .unwrap_or(0.0);
264
+
265
+ let (volume_env, pan_env, pitch_env) = (
266
+ helpers::env_map_to_hash(&_volume_env),
267
+ helpers::env_map_to_hash(&_pan_env),
268
+ helpers::env_map_to_hash(&_pitch_env),
269
+ );
270
+
271
+ NoteSetup {
272
+ sample_rate,
273
+ channels,
274
+ total_samples,
275
+ start_sample,
276
+ attack_samples,
277
+ decay_samples,
278
+ release_samples,
279
+ sustain_level,
280
+ pluck_click,
281
+ pluck_click_samples,
282
+ drive,
283
+ filters,
284
+ filter_states,
285
+ lfo_rate,
286
+ lfo_depth,
287
+ lfo_target,
288
+ voices,
289
+ unison_detune,
290
+ volume_env,
291
+ pan_env,
292
+ pitch_env,
293
+ }
294
+ }
@@ -0,0 +1,199 @@
1
+ use devalang_types::Value;
2
+ use devalang_types::VariableTable;
3
+ use devalang_utils::path::normalize_path;
4
+ use rodio::{Decoder, Source};
5
+ use std::{collections::HashMap, fs::File, io::BufReader, path::Path};
6
+
7
+ pub fn insert_sample_impl(
8
+ engine: &mut crate::core::audio::engine::driver::AudioEngine,
9
+ filepath: &str,
10
+ time_secs: f32,
11
+ dur_sec: f32,
12
+ effects: Option<HashMap<String, Value>>,
13
+ variable_table: &VariableTable,
14
+ ) {
15
+ if filepath.is_empty() {
16
+ eprintln!("❌ Empty file path provided for audio sample.");
17
+ return;
18
+ }
19
+
20
+ let module_root = Path::new(&engine.module_name);
21
+ let root = match devalang_utils::path::get_project_root() {
22
+ Ok(p) => p,
23
+ Err(_) => std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
24
+ };
25
+ let resolved_path: String;
26
+
27
+ let mut var_path = filepath.to_string();
28
+ if let Some(Value::String(variable_path)) = variable_table.variables.get(filepath) {
29
+ var_path = variable_path.clone();
30
+ } else if let Some(Value::Sample(sample_path)) = variable_table.variables.get(filepath) {
31
+ var_path = sample_path.clone();
32
+ }
33
+
34
+ if var_path.starts_with("devalang://") {
35
+ let path_after_protocol = var_path.replace("devalang://", "");
36
+ let parts: Vec<&str> = path_after_protocol.split('/').collect();
37
+
38
+ if parts.len() < 3 {
39
+ eprintln!(
40
+ "❌ Invalid devalang:// path format. Expected devalang://<type>/<author>.<bank>/<entity>"
41
+ );
42
+ return;
43
+ }
44
+
45
+ let obj_type = parts[0];
46
+ let bank_name = parts[1];
47
+ // Rejoin the remainder as the entity path so bank entries can contain
48
+ // nested paths like "subdir/sample.wav" or plain names.
49
+ let entity_name = parts[2..].join("/");
50
+
51
+ let deva_dir = match devalang_utils::path::get_deva_dir() {
52
+ Ok(dir) => dir,
53
+ Err(e) => {
54
+ eprintln!("❌ {}", e);
55
+ return;
56
+ }
57
+ };
58
+ let subdir = match obj_type {
59
+ "bank" => "banks",
60
+ "plugin" => "plugins",
61
+ "preset" => "presets",
62
+ "template" => "templates",
63
+ other => other,
64
+ };
65
+
66
+ // Determine the bank audio base directory. Prefer an optional
67
+ // `audioPath` declared in the bank's bank.toml (supports keys
68
+ // `audioPath` or `audio_path`). If absent, fall back to `audio/`.
69
+ let mut audio_dir = deva_dir.join(subdir).join(bank_name).join("audio");
70
+ // Try to read bank.toml to get audioPath
71
+ let bank_toml = deva_dir.join(subdir).join(bank_name).join("bank.toml");
72
+ if bank_toml.exists() {
73
+ if let Ok(content) = std::fs::read_to_string(&bank_toml) {
74
+ if let Ok(parsed) = toml::from_str::<toml::Value>(&content) {
75
+ if let Some(ap) = parsed
76
+ .get("audioPath")
77
+ .or_else(|| parsed.get("audio_path"))
78
+ .and_then(|v| v.as_str())
79
+ {
80
+ // normalize separators
81
+ let ap_norm = ap.replace("\\", "/");
82
+ audio_dir = deva_dir.join(subdir).join(bank_name).join(ap_norm);
83
+ }
84
+ }
85
+ }
86
+ }
87
+ // Force looking into the computed audio_dir. If the entity_name
88
+ // already contains an extension (e.g. .wav/.mp3) or a nested path,
89
+ // preserve it as-is. Otherwise, try with a .wav extension.
90
+ let bank_base = audio_dir;
91
+ let candidate = bank_base.join(&entity_name);
92
+
93
+ if candidate.exists() {
94
+ resolved_path = candidate.to_string_lossy().to_string();
95
+ } else {
96
+ // Detect whether the provided entity already includes an extension.
97
+ let has_extension = std::path::Path::new(&entity_name).extension().is_some();
98
+
99
+ if !has_extension {
100
+ // Try appending .wav as a fallback for shorthand names without extension
101
+ let wav_candidate = bank_base.join(format!("{}.wav", entity_name));
102
+ if wav_candidate.exists() {
103
+ resolved_path = wav_candidate.to_string_lossy().to_string();
104
+ } else {
105
+ // Last resort: use the legacy location (no audio/), also with .wav
106
+ resolved_path = deva_dir
107
+ .join(subdir)
108
+ .join(bank_name)
109
+ .join(format!("{}.wav", entity_name))
110
+ .to_string_lossy()
111
+ .to_string();
112
+ }
113
+ } else {
114
+ // If an extension was specified, don't append .wav; try legacy location
115
+ let legacy_candidate = deva_dir.join(subdir).join(bank_name).join(&entity_name);
116
+
117
+ if legacy_candidate.exists() {
118
+ resolved_path = legacy_candidate.to_string_lossy().to_string();
119
+ } else {
120
+ // No file found; fall back to the audio candidate path (even if missing)
121
+ resolved_path = candidate.to_string_lossy().to_string();
122
+ }
123
+ }
124
+ }
125
+ } else {
126
+ let entry_dir = module_root.parent().unwrap_or(&root);
127
+ let absolute_path = root.join(entry_dir).join(&var_path);
128
+
129
+ resolved_path = normalize_path(absolute_path.to_string_lossy().to_string());
130
+ }
131
+
132
+ if !Path::new(&resolved_path).exists() {
133
+ eprintln!("❌ Unknown trigger or missing audio file: {}", filepath);
134
+ return;
135
+ }
136
+
137
+ let file = match File::open(&resolved_path) {
138
+ Ok(f) => BufReader::new(f),
139
+ Err(e) => {
140
+ eprintln!("❌ Failed to open audio file {}: {}", resolved_path, e);
141
+ return;
142
+ }
143
+ };
144
+
145
+ let decoder = match Decoder::new(file) {
146
+ Ok(d) => d,
147
+ Err(e) => {
148
+ eprintln!("❌ Failed to decode audio file {}: {}", resolved_path, e);
149
+ return;
150
+ }
151
+ };
152
+
153
+ // Read frames from decoder and convert to mono if needed.
154
+ let sample_rate = engine.sample_rate as f32;
155
+ let channels = engine.channels as usize;
156
+
157
+ let max_frames = (dur_sec * sample_rate) as usize;
158
+ let dec_channels = decoder.channels() as usize;
159
+ let max_raw_samples = max_frames.saturating_mul(dec_channels.max(1));
160
+ let raw_samples: Vec<i16> = decoder.convert_samples().take(max_raw_samples).collect();
161
+
162
+ // Convert interleaved channels to mono by averaging channels per frame.
163
+ // Apply a small RMS-preserving scale so mono level is similar to mixed stereo.
164
+ let actual_frames = if dec_channels > 0 {
165
+ raw_samples.len() / dec_channels
166
+ } else {
167
+ 0
168
+ };
169
+ let mut samples: Vec<i16> = Vec::with_capacity(actual_frames);
170
+ let rms_scale = (dec_channels as f32).sqrt();
171
+ for frame in 0..actual_frames {
172
+ let mut sum: i32 = 0;
173
+ for ch in 0..dec_channels {
174
+ sum += raw_samples[frame * dec_channels + ch] as i32;
175
+ }
176
+ if dec_channels > 0 {
177
+ let avg = (sum / (dec_channels as i32)) as f32;
178
+ let scaled = (avg * rms_scale).clamp(i16::MIN as f32, i16::MAX as f32) as i16;
179
+ samples.push(scaled);
180
+ } else {
181
+ samples.push(0);
182
+ }
183
+ }
184
+
185
+ if samples.is_empty() {
186
+ eprintln!("❌ No samples read from {}", resolved_path);
187
+ return;
188
+ }
189
+
190
+ let offset = (time_secs * sample_rate * (channels as f32)) as usize;
191
+ let required_len = offset + samples.len() * (channels as usize);
192
+ if engine.buffer.len() < required_len {
193
+ engine.buffer.resize(required_len, 0);
194
+ }
195
+
196
+ crate::core::audio::engine::sample::padding::pad_samples_impl(
197
+ engine, &samples, time_secs, effects,
198
+ );
199
+ }
@@ -0,0 +1,40 @@
1
+ pub mod insert;
2
+ pub mod padding;
3
+
4
+ use devalang_types::Value;
5
+ use devalang_types::VariableTable;
6
+ use std::collections::HashMap;
7
+
8
+ impl super::driver::AudioEngine {
9
+ pub fn insert_sample(
10
+ &mut self,
11
+ filepath: &str,
12
+ time_secs: f32,
13
+ dur_sec: f32,
14
+ effects: Option<HashMap<String, Value>>,
15
+ variable_table: &VariableTable,
16
+ ) {
17
+ crate::core::audio::engine::sample::insert::insert_sample_impl(
18
+ self,
19
+ filepath,
20
+ time_secs,
21
+ dur_sec,
22
+ effects,
23
+ variable_table,
24
+ );
25
+ }
26
+
27
+ pub fn pad_samples(
28
+ &mut self,
29
+ samples: &[i16],
30
+ time_secs: f32,
31
+ effects_map: Option<HashMap<String, Value>>,
32
+ ) {
33
+ crate::core::audio::engine::sample::padding::pad_samples_impl(
34
+ self,
35
+ samples,
36
+ time_secs,
37
+ effects_map,
38
+ );
39
+ }
40
+ }
@@ -0,0 +1,170 @@
1
+ use devalang_types::Value;
2
+ use std::collections::HashMap;
3
+
4
+ pub fn pad_samples_impl(
5
+ engine: &mut crate::core::audio::engine::driver::AudioEngine,
6
+ samples: &[i16],
7
+ time_secs: f32,
8
+ effects_map: Option<HashMap<String, Value>>,
9
+ ) {
10
+ let sample_rate = engine.sample_rate as f32;
11
+ let channels = engine.channels as usize;
12
+
13
+ let offset = (time_secs * (sample_rate) * (channels as f32)) as usize;
14
+ let total_samples = samples.len();
15
+
16
+ let mut gain = 1.0;
17
+ let mut pan = 0.0;
18
+ let mut fade_in = 0.0;
19
+ let mut fade_out = 0.0;
20
+ let mut pitch = 1.0;
21
+ let mut drive = 0.0;
22
+ let mut reverb = 0.0;
23
+ let mut delay = 0.0; // delay time in seconds
24
+ let delay_feedback = 0.35; // default feedback
25
+
26
+ if let Some(map) = &effects_map {
27
+ for (key, val) in map {
28
+ match (key.as_str(), val) {
29
+ ("gain", Value::Number(v)) => {
30
+ gain = *v;
31
+ }
32
+ ("pan", Value::Number(v)) => {
33
+ pan = *v;
34
+ }
35
+ ("fadeIn", Value::Number(v)) => {
36
+ fade_in = *v;
37
+ }
38
+ ("fadeOut", Value::Number(v)) => {
39
+ fade_out = *v;
40
+ }
41
+ ("pitch", Value::Number(v)) => {
42
+ pitch = *v;
43
+ }
44
+ ("drive", Value::Number(v)) => {
45
+ drive = *v;
46
+ }
47
+ ("reverb", Value::Number(v)) => {
48
+ reverb = *v;
49
+ }
50
+ ("delay", Value::Number(v)) => {
51
+ delay = *v;
52
+ }
53
+ _ => eprintln!("⚠️ Unknown or invalid effect '{}'", key),
54
+ }
55
+ }
56
+ }
57
+
58
+ let fade_in_samples = (fade_in * (sample_rate)) as usize;
59
+ let fade_out_samples = (fade_out * (sample_rate)) as usize;
60
+
61
+ // If no fade specified, apply a tiny default fade (2 ms) when sample boundaries are non-zero
62
+ let default_boundary_fade_ms = 1.0_f32; // 1 ms
63
+ let default_fade_samples = (default_boundary_fade_ms * (sample_rate)) as usize;
64
+ let mut effective_fade_in = fade_in_samples;
65
+ let mut effective_fade_out = fade_out_samples;
66
+ if effective_fade_in == 0 {
67
+ if let Some(&first) = samples.first() {
68
+ if first.abs() > 64 {
69
+ // increased threshold to detect only strong abrupt starts
70
+ effective_fade_in = default_fade_samples.max(1);
71
+ }
72
+ }
73
+ }
74
+ if effective_fade_out == 0 {
75
+ if let Some(&last) = samples.last() {
76
+ if last.abs() > 64 {
77
+ // increased threshold to detect only strong abrupt ends
78
+ effective_fade_out = default_fade_samples.max(1);
79
+ }
80
+ }
81
+ }
82
+
83
+ // Ensure fades do not exceed half the sample length to avoid silencing short samples
84
+ if total_samples > 0 {
85
+ let cap = total_samples / 2;
86
+ if effective_fade_in > cap {
87
+ effective_fade_in = cap.max(1);
88
+ }
89
+ if effective_fade_out > cap {
90
+ effective_fade_out = cap.max(1);
91
+ }
92
+ }
93
+
94
+ let delay_samples = if delay > 0.0 {
95
+ (delay * (sample_rate)) as usize
96
+ } else {
97
+ 0
98
+ };
99
+ let mut delay_buffer: Vec<f32> = vec![0.0; total_samples + delay_samples];
100
+
101
+ for i in 0..total_samples {
102
+ let pitch_index = if pitch != 1.0 {
103
+ ((i as f32) / pitch) as usize
104
+ } else {
105
+ i
106
+ };
107
+
108
+ let mut adjusted = if pitch_index < total_samples {
109
+ samples[pitch_index] as f32
110
+ } else {
111
+ 0.0
112
+ };
113
+
114
+ adjusted *= gain;
115
+
116
+ if effective_fade_in > 0 && i < effective_fade_in {
117
+ if effective_fade_in == 1 {
118
+ adjusted *= 0.0;
119
+ } else {
120
+ adjusted *= (i as f32) / (effective_fade_in as f32);
121
+ }
122
+ }
123
+ if effective_fade_out > 0 && i >= total_samples.saturating_sub(effective_fade_out) {
124
+ if effective_fade_out == 1 {
125
+ adjusted *= 0.0;
126
+ } else {
127
+ adjusted *= ((total_samples - 1 - i) as f32) / ((effective_fade_out - 1) as f32);
128
+ }
129
+ }
130
+
131
+ if drive > 0.0 {
132
+ let normalized = adjusted / (i16::MAX as f32);
133
+ let pre_gain = (10f32).powf(drive / 20.0);
134
+ let driven = (normalized * pre_gain).tanh();
135
+ adjusted = driven * (i16::MAX as f32);
136
+ }
137
+
138
+ if delay_samples > 0 && i >= delay_samples {
139
+ let echo = delay_buffer[i - delay_samples] * delay_feedback;
140
+ adjusted += echo;
141
+ }
142
+ if delay_samples > 0 {
143
+ delay_buffer[i] = adjusted;
144
+ }
145
+
146
+ if reverb > 0.0 {
147
+ let reverb_delay = (0.03 * (sample_rate)) as usize;
148
+ if i >= reverb_delay {
149
+ adjusted += (engine.buffer[offset + i - reverb_delay] as f32) * reverb;
150
+ }
151
+ }
152
+
153
+ let adjusted_sample = adjusted.round().clamp(i16::MIN as f32, i16::MAX as f32) as i16;
154
+
155
+ let (left_gain, right_gain) = crate::core::audio::engine::helpers::pan_gains(pan);
156
+
157
+ let left = ((adjusted_sample as f32) * left_gain) as i16;
158
+ let right = ((adjusted_sample as f32) * right_gain) as i16;
159
+
160
+ // For interleaved buffer with `channels` channels, each frame has `channels` samples.
161
+ // left channel at frame i is at offset + i * channels, right at +1.
162
+ let left_pos = offset + i * channels;
163
+ let right_pos = left_pos + 1;
164
+
165
+ if right_pos < engine.buffer.len() {
166
+ engine.buffer[left_pos] = engine.buffer[left_pos].saturating_add(left);
167
+ engine.buffer[right_pos] = engine.buffer[right_pos].saturating_add(right);
168
+ }
169
+ }
170
+ }