Skip to main content

sim_lib_audio_dsp/
runtime.rs

1use sim_kernel::{Cx, Lib, LibManifest, Linker, LoadCx, Result, Symbol};
2use sim_lib_core::{SurfaceField, SurfacePackLib, SurfacePackSpec, SurfaceValueSpec, install_once};
3
4const AUDIO_DSP_LIB_ID: &str = "audio-dsp";
5
6/// Host-registered lib exporting the reusable DSP processor cards, built on the
7/// shared [`SurfacePackLib`] substrate.
8pub struct AudioDspLib;
9
10impl Lib for AudioDspLib {
11    fn manifest(&self) -> LibManifest {
12        audio_dsp_pack().manifest()
13    }
14
15    fn load(&self, cx: &mut LoadCx, linker: &mut Linker<'_>) -> Result<()> {
16        audio_dsp_pack().load(cx, linker)
17    }
18}
19
20/// Installs [`AudioDspLib`] into the context once, idempotently.
21pub fn install_audio_dsp_lib(cx: &mut Cx) -> Result<()> {
22    install_once(cx, &AudioDspLib)?;
23    Ok(())
24}
25
26/// Returns the namespaced symbols for every DSP processor card this lib exports.
27pub fn audio_dsp_symbols() -> Vec<Symbol> {
28    [
29        "SmoothValue",
30        "SmoothedGain",
31        "Gain",
32        "Pan",
33        "DcBlocker",
34        "OnePoleFilter",
35        "BiquadFilter",
36        "StateVariableFilter",
37        "DelayProcessor",
38        "FractionalDelay",
39        "CombFilter",
40        "AllPassFilter",
41        "Chorus",
42        "Flanger",
43        "Vibrato",
44        "Waveshaper",
45        "SoftClipper",
46        "Compressor",
47        "Limiter",
48        "Gate",
49        "OversamplingWrapper",
50    ]
51    .into_iter()
52    .map(|name| Symbol::qualified(AUDIO_DSP_LIB_ID, name))
53    .collect()
54}
55
56fn audio_dsp_pack() -> SurfacePackLib {
57    SurfacePackLib {
58        spec: SurfacePackSpec {
59            lib_id: Symbol::new(AUDIO_DSP_LIB_ID),
60            values: audio_dsp_symbols()
61                .into_iter()
62                .map(|symbol| SurfaceValueSpec {
63                    symbol: symbol.clone(),
64                    fields: vec![
65                        (Symbol::new("symbol"), SurfaceField::Symbol(symbol)),
66                        (
67                            Symbol::new("layer"),
68                            SurfaceField::Str(AUDIO_DSP_LIB_ID.to_owned()),
69                        ),
70                        (
71                            Symbol::new("kind"),
72                            SurfaceField::Str("audio-graph-processor".to_owned()),
73                        ),
74                        (
75                            Symbol::new("role"),
76                            SurfaceField::Str(
77                                "reusable pure Rust DSP processor descriptor".to_owned(),
78                            ),
79                        ),
80                        (
81                            Symbol::new("contract"),
82                            SurfaceField::Str("Processor".to_owned()),
83                        ),
84                    ],
85                })
86                .collect(),
87        },
88    }
89}