@devaloop/devalang 0.0.1-beta.2 → 0.1.1
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/README.md +177 -146
- package/out-tsc/api.d.ts +180 -0
- package/out-tsc/api.d.ts.map +1 -0
- package/out-tsc/api.js +286 -0
- package/out-tsc/api.js.map +1 -0
- package/out-tsc/bin/index.d.ts +12 -0
- package/out-tsc/bin/index.d.ts.map +1 -0
- package/out-tsc/bin/index.js +20 -54
- package/out-tsc/bin/index.js.map +1 -0
- package/out-tsc/examples/basic-usage.d.ts +8 -0
- package/out-tsc/examples/basic-usage.d.ts.map +1 -0
- package/out-tsc/examples/basic-usage.js +113 -0
- package/out-tsc/examples/basic-usage.js.map +1 -0
- package/out-tsc/index.d.ts +19 -5
- package/out-tsc/index.d.ts.map +1 -0
- package/out-tsc/index.js +24 -6
- package/out-tsc/index.js.map +1 -0
- package/out-tsc/scripts/copy-wasm-dts.d.ts +7 -0
- package/out-tsc/scripts/copy-wasm-dts.d.ts.map +1 -0
- package/out-tsc/scripts/copy-wasm-dts.js +36 -32
- package/out-tsc/scripts/copy-wasm-dts.js.map +1 -0
- package/out-tsc/scripts/postinstall.d.ts +1 -0
- package/out-tsc/scripts/postinstall.d.ts.map +1 -0
- package/out-tsc/scripts/postinstall.js +4 -1
- package/out-tsc/scripts/postinstall.js.map +1 -0
- package/out-tsc/scripts/version/bump.d.ts +5 -1
- package/out-tsc/scripts/version/bump.d.ts.map +1 -0
- package/out-tsc/scripts/version/bump.js +114 -44
- package/out-tsc/scripts/version/bump.js.map +1 -0
- package/out-tsc/scripts/version/fetch.d.ts +12 -1
- package/out-tsc/scripts/version/fetch.d.ts.map +1 -0
- package/out-tsc/scripts/version/fetch.js +68 -24
- package/out-tsc/scripts/version/fetch.js.map +1 -0
- package/out-tsc/scripts/version/index.d.ts +6 -0
- package/out-tsc/scripts/version/index.d.ts.map +1 -0
- package/out-tsc/scripts/version/index.js +44 -22
- package/out-tsc/scripts/version/index.js.map +1 -0
- package/out-tsc/scripts/version/sync.d.ts +5 -1
- package/out-tsc/scripts/version/sync.d.ts.map +1 -0
- package/out-tsc/scripts/version/sync.js +78 -29
- package/out-tsc/scripts/version/sync.js.map +1 -0
- package/out-tsc/types.d.ts +68 -0
- package/out-tsc/types.d.ts.map +1 -0
- package/out-tsc/{core/types/value.js → types.js} +4 -0
- package/out-tsc/types.js.map +1 -0
- package/out-tsc/wasm.d.ts +8 -0
- package/out-tsc/wasm.d.ts.map +1 -0
- package/out-tsc/{core/index.js → wasm.js} +9 -6
- package/out-tsc/wasm.js.map +1 -0
- package/package.json +50 -37
- package/.cargo/config.toml +0 -2
- package/.devalang +0 -9
- package/.github/workflows/ci.yml +0 -103
- package/Cargo.toml +0 -81
- package/docs/CHANGELOG.md +0 -581
- package/docs/CONTRIBUTING.md +0 -101
- package/docs/ROADMAP.md +0 -38
- package/docs/TODO.md +0 -71
- package/examples/automation.deva +0 -42
- package/examples/bank.deva +0 -7
- package/examples/bus.deva +0 -10
- package/examples/condition.deva +0 -20
- package/examples/duration.deva +0 -9
- package/examples/effect.deva +0 -2
- package/examples/events.deva +0 -12
- package/examples/filter.deva +0 -11
- package/examples/function.deva +0 -15
- package/examples/group.deva +0 -12
- package/examples/index.deva +0 -63
- package/examples/lfo.deva +0 -9
- package/examples/loop.deva +0 -10
- package/examples/pattern.deva +0 -8
- package/examples/plugin.deva +0 -16
- package/examples/samples/hat-808.wav +0 -0
- package/examples/samples/kick-808.wav +0 -0
- package/examples/synth.deva +0 -24
- package/examples/synth_types.deva +0 -17
- package/examples/variables.deva +0 -9
- package/out-tsc/core/functions/index.d.ts +0 -42
- package/out-tsc/core/functions/index.js +0 -87
- package/out-tsc/core/index.d.ts +0 -6
- package/out-tsc/core/types/index.d.ts +0 -4
- package/out-tsc/core/types/index.js +0 -20
- package/out-tsc/core/types/plugin.d.ts +0 -18
- package/out-tsc/core/types/plugin.js +0 -2
- package/out-tsc/core/types/result.d.ts +0 -27
- package/out-tsc/core/types/result.js +0 -2
- package/out-tsc/core/types/statement.d.ts +0 -106
- package/out-tsc/core/types/statement.js +0 -2
- package/out-tsc/core/types/value.d.ts +0 -43
- package/out-tsc/pkg/devalang_core.d.ts +0 -15
- package/out-tsc/pkg/devalang_core.js +0 -65
- package/out-tsc/pkg/devalang_core_bg.wasm.d.ts +0 -34
- package/project-version.json +0 -6
- package/rust/cli/bank/api.rs +0 -122
- package/rust/cli/bank/commands.rs +0 -306
- package/rust/cli/bank/mod.rs +0 -29
- package/rust/cli/build/commands.rs +0 -153
- package/rust/cli/build/mod.rs +0 -2
- package/rust/cli/build/process.rs +0 -165
- package/rust/cli/check/mod.rs +0 -208
- package/rust/cli/discover/commands.rs +0 -253
- package/rust/cli/discover/config.rs +0 -111
- package/rust/cli/discover/fs.rs +0 -19
- package/rust/cli/discover/install.rs +0 -103
- package/rust/cli/discover/metadata.rs +0 -48
- package/rust/cli/discover/mod.rs +0 -5
- package/rust/cli/init/commands.rs +0 -88
- package/rust/cli/init/mod.rs +0 -1
- package/rust/cli/install/addon.rs +0 -118
- package/rust/cli/install/bank.rs +0 -72
- package/rust/cli/install/commands.rs +0 -35
- package/rust/cli/install/mod.rs +0 -4
- package/rust/cli/install/plugin.rs +0 -80
- package/rust/cli/login/commands.rs +0 -124
- package/rust/cli/login/mod.rs +0 -1
- package/rust/cli/mod.rs +0 -12
- package/rust/cli/parser.rs +0 -359
- package/rust/cli/play/commands.rs +0 -375
- package/rust/cli/play/io.rs +0 -17
- package/rust/cli/play/mod.rs +0 -5
- package/rust/cli/play/process.rs +0 -159
- package/rust/cli/play/realtime.rs +0 -91
- package/rust/cli/play/utils.rs +0 -23
- package/rust/cli/telemetry/commands.rs +0 -22
- package/rust/cli/telemetry/event_creator.rs +0 -80
- package/rust/cli/telemetry/mod.rs +0 -3
- package/rust/cli/telemetry/send.rs +0 -51
- package/rust/cli/template/commands.rs +0 -69
- package/rust/cli/template/mod.rs +0 -1
- package/rust/cli/update/commands.rs +0 -6
- package/rust/cli/update/mod.rs +0 -1
- package/rust/config/driver.rs +0 -112
- package/rust/config/mod.rs +0 -3
- package/rust/config/ops.rs +0 -26
- package/rust/config/settings.rs +0 -101
- package/rust/core/audio/engine/driver.rs +0 -220
- package/rust/core/audio/engine/export.rs +0 -169
- package/rust/core/audio/engine/helpers.rs +0 -178
- package/rust/core/audio/engine/mod.rs +0 -56
- package/rust/core/audio/engine/notes/dsp.rs +0 -85
- package/rust/core/audio/engine/notes/mod.rs +0 -44
- package/rust/core/audio/engine/notes/params.rs +0 -294
- package/rust/core/audio/engine/sample/insert.rs +0 -199
- package/rust/core/audio/engine/sample/mod.rs +0 -40
- package/rust/core/audio/engine/sample/padding.rs +0 -170
- package/rust/core/audio/evaluator/condition.rs +0 -61
- package/rust/core/audio/evaluator/mod.rs +0 -9
- package/rust/core/audio/evaluator/numeric.rs +0 -152
- package/rust/core/audio/evaluator/rhs.rs +0 -16
- package/rust/core/audio/evaluator/string_expr.rs +0 -94
- package/rust/core/audio/interpreter/driver.rs +0 -574
- package/rust/core/audio/interpreter/mod.rs +0 -2
- package/rust/core/audio/interpreter/statements/arrow_call/interprete.rs +0 -175
- package/rust/core/audio/interpreter/statements/arrow_call/methods/chord.rs +0 -384
- package/rust/core/audio/interpreter/statements/arrow_call/methods/mod.rs +0 -2
- package/rust/core/audio/interpreter/statements/arrow_call/methods/note.rs +0 -316
- package/rust/core/audio/interpreter/statements/arrow_call/mod.rs +0 -3
- package/rust/core/audio/interpreter/statements/arrow_call/types/arp.rs +0 -192
- package/rust/core/audio/interpreter/statements/arrow_call/types/mod.rs +0 -24
- package/rust/core/audio/interpreter/statements/arrow_call/types/pad.rs +0 -116
- package/rust/core/audio/interpreter/statements/arrow_call/types/pluck.rs +0 -97
- package/rust/core/audio/interpreter/statements/arrow_call/types/sub.rs +0 -100
- package/rust/core/audio/interpreter/statements/automate.rs +0 -16
- package/rust/core/audio/interpreter/statements/call.rs +0 -295
- package/rust/core/audio/interpreter/statements/condition.rs +0 -72
- package/rust/core/audio/interpreter/statements/function.rs +0 -24
- package/rust/core/audio/interpreter/statements/let_.rs +0 -36
- package/rust/core/audio/interpreter/statements/load.rs +0 -17
- package/rust/core/audio/interpreter/statements/loop_.rs +0 -115
- package/rust/core/audio/interpreter/statements/mod.rs +0 -12
- package/rust/core/audio/interpreter/statements/sleep.rs +0 -28
- package/rust/core/audio/interpreter/statements/spawn.rs +0 -253
- package/rust/core/audio/interpreter/statements/tempo.rs +0 -40
- package/rust/core/audio/interpreter/statements/trigger.rs +0 -239
- package/rust/core/audio/loader/mod.rs +0 -1
- package/rust/core/audio/loader/trigger.rs +0 -98
- package/rust/core/audio/mod.rs +0 -6
- package/rust/core/audio/player.rs +0 -70
- package/rust/core/audio/special/easing.rs +0 -189
- package/rust/core/audio/special/env.rs +0 -45
- package/rust/core/audio/special/math.rs +0 -134
- package/rust/core/audio/special/mod.rs +0 -9
- package/rust/core/audio/special/modulator.rs +0 -143
- package/rust/core/builder/mod.rs +0 -129
- package/rust/core/debugger/lexer.rs +0 -27
- package/rust/core/debugger/logs.rs +0 -52
- package/rust/core/debugger/mod.rs +0 -30
- package/rust/core/debugger/preprocessor.rs +0 -27
- package/rust/core/debugger/store.rs +0 -38
- package/rust/core/error/mod.rs +0 -269
- package/rust/core/lexer/driver.rs +0 -59
- package/rust/core/lexer/handler/arrow.rs +0 -82
- package/rust/core/lexer/handler/at.rs +0 -21
- package/rust/core/lexer/handler/brace.rs +0 -41
- package/rust/core/lexer/handler/colon.rs +0 -21
- package/rust/core/lexer/handler/comment.rs +0 -30
- package/rust/core/lexer/handler/dot.rs +0 -21
- package/rust/core/lexer/handler/driver.rs +0 -337
- package/rust/core/lexer/handler/identifier.rs +0 -47
- package/rust/core/lexer/handler/indent.rs +0 -66
- package/rust/core/lexer/handler/mod.rs +0 -15
- package/rust/core/lexer/handler/newline.rs +0 -23
- package/rust/core/lexer/handler/number.rs +0 -31
- package/rust/core/lexer/handler/operator.rs +0 -46
- package/rust/core/lexer/handler/parenthesis.rs +0 -41
- package/rust/core/lexer/handler/slash.rs +0 -21
- package/rust/core/lexer/handler/string.rs +0 -63
- package/rust/core/lexer/mod.rs +0 -3
- package/rust/core/lexer/token.rs +0 -91
- package/rust/core/mod.rs +0 -9
- package/rust/core/parser/driver/block.rs +0 -111
- package/rust/core/parser/driver/cursor.rs +0 -82
- package/rust/core/parser/driver/driver_impl.rs +0 -139
- package/rust/core/parser/driver/mod.rs +0 -6
- package/rust/core/parser/driver/parse_array.rs +0 -120
- package/rust/core/parser/driver/parse_map.rs +0 -223
- package/rust/core/parser/driver/parser.rs +0 -160
- package/rust/core/parser/handler/arrow_call.rs +0 -277
- package/rust/core/parser/handler/at.rs +0 -279
- package/rust/core/parser/handler/bank.rs +0 -104
- package/rust/core/parser/handler/condition.rs +0 -83
- package/rust/core/parser/handler/dot.rs +0 -148
- package/rust/core/parser/handler/identifier/automate.rs +0 -254
- package/rust/core/parser/handler/identifier/call.rs +0 -91
- package/rust/core/parser/handler/identifier/emit.rs +0 -70
- package/rust/core/parser/handler/identifier/function.rs +0 -113
- package/rust/core/parser/handler/identifier/group.rs +0 -89
- package/rust/core/parser/handler/identifier/let_.rs +0 -173
- package/rust/core/parser/handler/identifier/mod.rs +0 -55
- package/rust/core/parser/handler/identifier/on.rs +0 -107
- package/rust/core/parser/handler/identifier/print.rs +0 -49
- package/rust/core/parser/handler/identifier/sleep.rs +0 -96
- package/rust/core/parser/handler/identifier/spawn.rs +0 -91
- package/rust/core/parser/handler/identifier/synth.rs +0 -135
- package/rust/core/parser/handler/loop_.rs +0 -194
- package/rust/core/parser/handler/mod.rs +0 -9
- package/rust/core/parser/handler/pattern.rs +0 -74
- package/rust/core/parser/handler/tempo.rs +0 -105
- package/rust/core/parser/mod.rs +0 -3
- package/rust/core/parser/statement.rs +0 -10
- package/rust/core/plugin/loader.rs +0 -137
- package/rust/core/plugin/mod.rs +0 -2
- package/rust/core/plugin/runner/mod.rs +0 -11
- package/rust/core/plugin/runner/non_wasm.rs +0 -297
- package/rust/core/plugin/runner/wasm32.rs +0 -43
- package/rust/core/preprocessor/loader/inject.rs +0 -278
- package/rust/core/preprocessor/loader/loader_helpers.rs +0 -110
- package/rust/core/preprocessor/loader/mod.rs +0 -235
- package/rust/core/preprocessor/mod.rs +0 -4
- package/rust/core/preprocessor/module.rs +0 -55
- package/rust/core/preprocessor/processor/handlers.rs +0 -107
- package/rust/core/preprocessor/processor/mod.rs +0 -1
- package/rust/core/preprocessor/resolver/bank.rs +0 -49
- package/rust/core/preprocessor/resolver/call.rs +0 -124
- package/rust/core/preprocessor/resolver/condition.rs +0 -95
- package/rust/core/preprocessor/resolver/driver.rs +0 -324
- package/rust/core/preprocessor/resolver/function.rs +0 -69
- package/rust/core/preprocessor/resolver/group.rs +0 -122
- package/rust/core/preprocessor/resolver/let_.rs +0 -32
- package/rust/core/preprocessor/resolver/loop_.rs +0 -318
- package/rust/core/preprocessor/resolver/mod.rs +0 -16
- package/rust/core/preprocessor/resolver/pattern.rs +0 -83
- package/rust/core/preprocessor/resolver/spawn.rs +0 -99
- package/rust/core/preprocessor/resolver/synth.rs +0 -54
- package/rust/core/preprocessor/resolver/tempo.rs +0 -48
- package/rust/core/preprocessor/resolver/trigger.rs +0 -116
- package/rust/core/preprocessor/resolver/value.rs +0 -176
- package/rust/core/store/global.rs +0 -57
- package/rust/core/store/mod.rs +0 -1
- package/rust/lib.rs +0 -323
- package/rust/main.rs +0 -388
- package/rust/types/Cargo.toml +0 -11
- package/rust/types/src/addons.rs +0 -55
- package/rust/types/src/ast.rs +0 -202
- package/rust/types/src/config.rs +0 -84
- package/rust/types/src/lib.rs +0 -15
- package/rust/types/src/plugin.rs +0 -20
- package/rust/types/src/store.rs +0 -139
- package/rust/types/src/telemetry.rs +0 -85
- package/rust/utils/Cargo.toml +0 -26
- package/rust/utils/src/error.rs +0 -186
- package/rust/utils/src/file.rs +0 -94
- package/rust/utils/src/first_usage.rs +0 -97
- package/rust/utils/src/lib.rs +0 -9
- package/rust/utils/src/logger.rs +0 -200
- package/rust/utils/src/path.rs +0 -129
- package/rust/utils/src/signature.rs +0 -41
- package/rust/utils/src/spinner.rs +0 -20
- package/rust/utils/src/version.rs +0 -27
- package/rust/utils/src/watcher.rs +0 -46
- package/rust/web/api.rs +0 -5
- package/rust/web/cdn.rs +0 -34
- package/rust/web/mod.rs +0 -3
- package/rust/web/sso.rs +0 -5
- package/templates/minimal/.devalang +0 -5
- package/templates/minimal/README.md +0 -218
- package/templates/minimal/src/index.deva +0 -2
- package/templates/welcome/.devalang +0 -5
- package/templates/welcome/README.md +0 -218
- package/templates/welcome/samples/kick-808.wav +0 -0
- package/templates/welcome/src/index.deva +0 -61
- package/templates/welcome/src/variables.deva +0 -3
- package/tests/integration.rs +0 -21
- package/tests/rust/cli_check_build.rs +0 -21
- package/tests/rust/cli_help.rs +0 -12
- package/tests/rust/cli_template_list.rs +0 -10
- package/tests/rust/cli_version.rs +0 -11
- package/tests/typescript/index.spec.ts +0 -136
- package/tests/typescript/playhead.spec.ts +0 -36
- package/tests/typescript/render_e2e.spec.ts +0 -77
- package/tsconfig.json +0 -115
- package/typescript/bin/index.ts +0 -28
- package/typescript/core/functions/index.ts +0 -94
- package/typescript/core/index.ts +0 -6
- package/typescript/core/types/index.ts +0 -4
- package/typescript/core/types/plugin.ts +0 -19
- package/typescript/core/types/result.ts +0 -29
- package/typescript/core/types/statement.ts +0 -47
- package/typescript/core/types/value.ts +0 -29
- package/typescript/index.ts +0 -8
- package/typescript/pkg/devalang_core.d.ts +0 -4
- package/typescript/pkg/devalang_core.ts +0 -65
- package/typescript/scripts/copy-wasm-dts.ts +0 -41
- package/typescript/scripts/postinstall.ts +0 -85
- package/typescript/scripts/version/bump.ts +0 -44
- package/typescript/scripts/version/fetch.ts +0 -18
- package/typescript/scripts/version/index.ts +0 -25
- package/typescript/scripts/version/sync.ts +0 -24
|
@@ -1,294 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,199 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,170 +0,0 @@
|
|
|
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
|
-
}
|