Skip to content

Commit 7f641ab

Browse files
authored
Merge pull request #36 from ajmwagar/feature/precompute-bin
Precomputed scattering matrix binary format (.pksc)
2 parents d30f3de + bf0d3b1 commit 7f641ab

File tree

6 files changed

+860
-0
lines changed

6 files changed

+860
-0
lines changed

pedalkernel/src/compiler/compiled.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,3 +2586,64 @@ impl PedalProcessor for CompiledPedal {
25862586
out
25872587
}
25882588
}
2589+
2590+
// ═══════════════════════════════════════════════════════════════════════════
2591+
// Precompute extraction — used by crate::precompute
2592+
// ═══════════════════════════════════════════════════════════════════════════
2593+
2594+
/// Extract precomputed scattering data from a compiled pedal.
2595+
///
2596+
/// Called by [`crate::precompute::extract_precomputed`]; lives here so it
2597+
/// has access to the `pub(super)` fields of [`CompiledPedal`] and
2598+
/// [`MultiNlStage`].
2599+
pub(crate) fn extract_precomputed_from_compiled(
2600+
compiled: &CompiledPedal,
2601+
pedal_source: &str,
2602+
sample_rate: f64,
2603+
) -> crate::precompute::PrecomputedScattering {
2604+
use crate::precompute::{
2605+
pedal_hash, PrecomputedInterpTable, PrecomputedScattering, PrecomputedStage,
2606+
};
2607+
2608+
let hash = pedal_hash(pedal_source);
2609+
let mut stages = Vec::new();
2610+
let mut interp_tables = Vec::new();
2611+
2612+
for (stage_idx, mnl) in compiled.multi_nl_stages.iter().enumerate() {
2613+
let adaptor = mnl.adaptor();
2614+
let n = adaptor.num_ports();
2615+
let scattering = adaptor.power_scattering().to_vec();
2616+
let port_resistances = adaptor.port_resistances();
2617+
let vs_injection = mnl.vs_injection().map(ToOwned::to_owned);
2618+
let extract_coeffs = mnl.extract_coeffs().map(ToOwned::to_owned);
2619+
let extract_vs = mnl.extract_vs();
2620+
2621+
stages.push(PrecomputedStage {
2622+
stage_type: 1, // MultiNL
2623+
n_ports: n,
2624+
scattering,
2625+
vs_injection,
2626+
port_resistances,
2627+
extract_coeffs,
2628+
extract_vs,
2629+
});
2630+
2631+
if let Some(table) = mnl.interp_table() {
2632+
interp_tables.push(PrecomputedInterpTable {
2633+
stage_index: stage_idx,
2634+
n_ports: n,
2635+
has_vs_injection: table.has_vs_injection(),
2636+
resistances: table.resistances().to_vec(),
2637+
matrices: table.matrices().to_vec(),
2638+
injections: table.injections().to_vec(),
2639+
});
2640+
}
2641+
}
2642+
2643+
PrecomputedScattering {
2644+
pedal_hash: hash,
2645+
sample_rate,
2646+
stages,
2647+
interp_tables,
2648+
}
2649+
}

pedalkernel/src/compiler/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod warnings;
2929
pub(crate) mod wdf_leaf;
3030

3131
pub use compile::{compile_pedal, compile_pedal_with_options, CompileOptions};
32+
pub(crate) use compiled::extract_precomputed_from_compiled;
3233
pub use compiled::CompiledPedal;
3334
pub use component::{Component, PinDirection};
3435
pub use split::{compile_split_pedal, SplitCompiledPedal};

pedalkernel/src/compiler/stage.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3737,6 +3737,35 @@ impl MultiNlStage {
37373737
}
37383738
}
37393739
}
3740+
3741+
// ── Precompute accessors ─────────────────────────────────────────────
3742+
// Minimal read-only accessors for `precompute::extract_precomputed`.
3743+
3744+
/// The R-type adaptor containing the scattering matrix and port data.
3745+
pub(crate) fn adaptor(&self) -> &RTypeAdaptor {
3746+
&self.adaptor
3747+
}
3748+
3749+
/// VS injection vector (present when driven by an ideal voltage source).
3750+
pub(crate) fn vs_injection(&self) -> Option<&Vec<f64>> {
3751+
self.vs_injection.as_ref()
3752+
}
3753+
3754+
/// Node-voltage extraction coefficients (present when output is read from
3755+
/// MNA node voltages directly rather than WDF port waves).
3756+
pub(crate) fn extract_coeffs(&self) -> Option<&Vec<f64>> {
3757+
self.extract_coeffs.as_ref()
3758+
}
3759+
3760+
/// VS component of the extraction formula.
3761+
pub(crate) fn extract_vs(&self) -> f64 {
3762+
self.extract_vs
3763+
}
3764+
3765+
/// Pot interpolation table (present for single-pot stages).
3766+
pub(crate) fn interp_table(&self) -> Option<&crate::tree::ScatteringInterpolationTable> {
3767+
self.interp_table.as_ref()
3768+
}
37403769
}
37413770

37423771
// ═══════════════════════════════════════════════════════════════════════════

pedalkernel/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub mod models;
3232
pub mod oversampling;
3333
pub mod pedalboard;
3434
pub mod pedals;
35+
pub mod precompute;
3536
#[cfg(feature = "runtime-warnings")]
3637
pub mod runtime_warnings;
3738
pub mod thermal;

0 commit comments

Comments
 (0)