wavegraph_core/lib.rs
1#![no_std]
2#![forbid(unsafe_code)]
3#![doc = include_str!("../README.md")]
4
5mod sine;
6mod tuning;
7
8pub mod bytecode;
9pub mod midi;
10pub mod polyphony;
11pub use bytecode::Op;
12pub use sine::sine_q15;
13pub use tuning::Tuning;
14
15/// A target can implement these operations with its own DSP kernels. Each
16/// slice has the same frame count and the output is distinct from the inputs.
17/// Calls must finish within the audio deadline and fully overwrite the output.
18pub trait Dsp<S> {
19 /// Scale by `level / 128` and saturate to the sample format's nominal range.
20 /// For fixed-point PCM, round toward negative infinity.
21 fn gain(&mut self, input: &[S], level: u8, output: &mut [S]);
22
23 /// Add two signals and saturate to the nominal sample range.
24 fn mix(&mut self, left: &[S], right: &[S], output: &mut [S]);
25}
26
27/// A patch object. The patch selects a common message type `M` and signal
28/// sample type `S`. Each object owns its state between calls.
29pub trait Process<M = (), S = f32> {
30 /// Inlets in left-to-right patch order. The layout stays fixed while connected.
31 fn inlets(&self) -> &[Port];
32
33 /// Outlets in left-to-right patch order. The layout stays fixed while connected.
34 fn outlets(&self) -> &[Port];
35
36 /// Process the supplied signals and messages. On the audio thread this
37 /// work must be bounded, allocation-free and nonblocking.
38 fn process(&mut self, cx: &mut ProcessContext<'_, '_, M, S>);
39}
40
41/// Kind of connection exposed by an inlet or outlet.
42#[derive(Clone, Copy, Debug, Eq, PartialEq)]
43pub enum Port {
44 /// Carries discrete messages such as a bang, note or parameter change.
45 Message,
46 /// Carries a block of samples. A signal inlet can also receive messages
47 /// when the object defines a meaning for them.
48 Signal,
49}
50
51/// A control message at a frame offset within a processing call.
52#[derive(Clone, Copy, Debug, Eq, PartialEq)]
53pub struct Event<M> {
54 /// Apply the message immediately before this zero-based frame.
55 pub frame: usize,
56 /// Local inlet number on delivery, or local outlet number on emission.
57 pub port: usize,
58 /// The patch's message payload.
59 pub message: M,
60}
61
62/// Borrowed signals and messages for one call. The output table can be
63/// reborrowed independently of its sample storage.
64#[allow(
65 missing_debug_implementations,
66 reason = "The event sink is an opaque callback."
67)]
68pub struct ProcessContext<'call, 'output, M = (), S = f32> {
69 /// Number of frames in every signal buffer. A zero-frame call delivers
70 /// messages at offset zero without advancing audio time.
71 pub frames: usize,
72 /// One shared buffer per signal inlet, in inlet order. For inlets
73 /// `[Message, Signal]`, buffer zero belongs to inlet one.
74 pub inputs: &'call [&'call [S]],
75 /// One exclusive buffer per signal outlet, in outlet order. The processor
76 /// overwrites every frame. An additive renderer clears its output first.
77 pub outputs: &'call mut [&'output mut [S]],
78 /// Messages in nondecreasing frame order. Apply ties in slice order.
79 /// In a nonempty block, offsets are less than `frames`. An event at the
80 /// end belongs to the next block.
81 pub events: &'call [Event<M>],
82 /// Emit in frame order on a message outlet. The host maps the outlet to
83 /// downstream inlets and preserves the frame offset and emission order.
84 /// Return the original event if the host cannot accept it.
85 pub emit: &'call mut dyn FnMut(Event<M>) -> Result<(), Event<M>>,
86}