1mod bindings;
4mod cache;
5mod cast;
6mod error;
7pub mod flops;
8pub mod instantiate;
9mod loops;
10mod lower;
11mod offset;
12pub mod overload;
13pub mod query;
14mod refs;
15pub mod render;
16mod schedule;
17mod trace;
18mod typing;
19mod vocabulary;
20
21pub use bindings::Binding;
22pub use cache::{
23 Cache, DiskCache, Entry, Expected, Hash, IO_NANOS_PER_BYTE, MemoryCache, Payload, PayloadKind,
24 buffer_key, symbolic_key,
25};
26pub use cast::Cast;
27pub use error::{BindingFault, Diagnostic, EngineError, Located, REGISTRY};
28pub use flops::{Row as FlopRow, Tree as FlopTree};
29pub use loops::{Delay, Shift};
30pub use offset::Offset;
31pub use query::{Answer, Ask, DEFAULT_FRAME_SECS, Output, Representation};
32pub use refs::{Read, identity, nodes_in, resolve, spectral_sum_of, symbolic_hash};
33pub use render::{Render, RenderConfig, answer, answer_buffer, render, sketch_atom};
34pub use schedule::{Order, Schedule, schedule_from};
35pub use sva_formula::{C64, Codomain, Held, Line, NodeId, SpectralSum, Ty, Var};
36pub use sva_samples::{
37 Alias, AliasBand, BAND_COUNT, BandCrest, BandTrack, Bands, Buffer, Cost, Crest, Detail,
38 EnvelopeFrame, FormantFrame, Frames, Horizon, Label, LedgerEntry, Loudness, LoudnessFrame,
39 MAX_PINNED_FRAME, PSYCHOACOUSTIC_V1, PitchFrame, Profile, Rule, SignalKind, Source, Spectrum,
40 StereoFrame, StereoImage, measure_alias, pinned_frame,
41};
42pub use trace::{Traced, Up, trace};
43pub use typing::{Typing, Value};
44pub use vocabulary::{BUILTINS, MAX_WIDTH, is_builtin, recognized_named};
45
46use sva_ast::Graph;
47
48pub const DEFAULT_SAMPLE_RATE: u32 = 44100;
49
50pub fn check_structure(graph: &Graph, root: &str) -> Result<(), EngineError> {
52 types(graph, root).map(|_| ())
53}
54
55pub fn types(graph: &Graph, root: &str) -> Result<Typing, EngineError> {
57 let instances = instantiate::instantiate(graph, root)?;
58 let held = instances.instance_of(root)?;
60 let order = schedule::schedule_from(&instances, std::slice::from_ref(&held))?;
61 typing::infer_all(&instances, &order)
62}
63
64pub fn rate_dependent(graph: &Graph, root: &str) -> Result<Vec<String>, EngineError> {
66 let instances = instantiate::instantiate(graph, root)?;
67 Ok(instances
68 .paths()
69 .filter(|p| instances.at(p).is_some_and(|(e, _)| reads_rate(e)))
70 .map(str::to_string)
71 .collect())
72}
73
74fn reads_rate(e: &sva_ast::Expr) -> bool {
75 match e {
76 sva_ast::Expr::Lit(sva_ast::Literal::Samples(_)) => true,
77 sva_ast::Expr::Lit(_) => false,
78 sva_ast::Expr::Var(_) => false,
79 sva_ast::Expr::Bin(_, a, b) => reads_rate(a) || reads_rate(b),
80 sva_ast::Expr::SelfRef { .. } => true,
81 sva_ast::Expr::Ref { arg, binds, .. } => {
82 reads_rate(arg) || binds.iter().any(|(_, v)| reads_rate(v))
83 }
84 sva_ast::Expr::Call { name, args, .. } => {
85 let drawn = name == "rand"
86 && !matches!(
87 args.first(),
88 Some(sva_ast::Arg::Pos(sva_ast::Expr::Lit(_))) | None
89 );
90 drawn
91 || args.iter().any(|a| match a {
92 sva_ast::Arg::Pos(v) | sva_ast::Arg::Named(_, v) => reads_rate(v),
93 })
94 }
95 }
96}