Initial commit of a toy analogue audio generator
Key components: - no_std core so it can be used bare metal on embedded systems - default implementation for RPi 2350, with midi input and I2s output using PIO - Visualiser for the web to play with on a dev system
This commit is contained in:
40
crates/synth-core/src/patch.rs
Normal file
40
crates/synth-core/src/patch.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
//! Signal routing — connects module outputs to inputs.
|
||||
//!
|
||||
//! A `Patch` is a fixed-size connection graph. Each cable routes
|
||||
//! the output buffer of one slot into the CV or audio input of another.
|
||||
//! Const generics keep everything on the stack.
|
||||
|
||||
/// A single cable: from slot `src` output to slot `dst` input.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Cable {
|
||||
pub src: usize,
|
||||
pub dst: usize,
|
||||
}
|
||||
|
||||
/// A fixed-capacity patch with up to `MAX_CABLES` connections.
|
||||
pub struct Patch<const MAX_CABLES: usize> {
|
||||
cables: heapless::Vec<Cable, MAX_CABLES>,
|
||||
}
|
||||
|
||||
impl<const MAX_CABLES: usize> Patch<MAX_CABLES> {
|
||||
pub fn new() -> Self {
|
||||
Self { cables: heapless::Vec::new() }
|
||||
}
|
||||
|
||||
/// Add a cable; returns `Err(cable)` if the patch is full.
|
||||
pub fn connect(&mut self, src: usize, dst: usize) -> Result<(), Cable> {
|
||||
self.cables.push(Cable { src, dst }).map_err(|_| Cable { src, dst })
|
||||
}
|
||||
|
||||
pub fn disconnect(&mut self, src: usize, dst: usize) {
|
||||
self.cables.retain(|c| !(c.src == src && c.dst == dst));
|
||||
}
|
||||
|
||||
pub fn cables(&self) -> &[Cable] {
|
||||
&self.cables
|
||||
}
|
||||
}
|
||||
|
||||
impl<const MAX_CABLES: usize> Default for Patch<MAX_CABLES> {
|
||||
fn default() -> Self { Self::new() }
|
||||
}
|
||||
Reference in New Issue
Block a user