Skip to content

Commit

Permalink
Move PowfGen
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikNatanael committed Oct 7, 2024
1 parent c164041 commit f390772
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 50 deletions.
81 changes: 80 additions & 1 deletion knyst/src/gen/basic_gens.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//! Includes basic `Gen`s such as `Mul` and `Range`

use crate::{self as knyst, prelude::Seconds, SampleRate};
use crate::{
self as knyst,
graph::NodeId,
handles::{Handle, HandleData, Input, NodeIdIter, SinkChannelIter, SourceChannelIter},
prelude::{KnystCommands, Seconds},
SampleRate,
};
use knyst_macro::impl_gen;

use crate::{
Expand Down Expand Up @@ -114,6 +120,79 @@ impl Gen for PowfGen {
"PowfGen"
}
}
/// base to the power of exponent, where base and exponent can be constants or handles
pub fn powf_gen(base: impl Into<Input>, exponent: impl Into<Input>) -> Handle<PowfHandle> {
let base = base.into();
let (node_id, num_channels) = match base {
Input::Constant(c) => {
let node_id = knyst::knyst_commands().push_without_inputs(PowfGen(1));
knyst::knyst_commands().connect(
knyst::graph::connection::constant(c)
.to(node_id)
.to_channel(1),
);
(node_id, 1)
}
Input::Handle { output_channels } => {
let connecting_channels: Vec<_> = output_channels.collect();
let num_channels = connecting_channels.len();
let node_id =
knyst::knyst_commands().push_without_inputs(PowfGen(connecting_channels.len()));
for (i, (source, chan)) in connecting_channels.into_iter().enumerate() {
knyst::knyst_commands()
.connect(source.to(node_id).from_channel(chan).to_channel(i + 1));
}

(node_id, num_channels)
}
};
Handle::new(PowfHandle {
node_id,
num_channels,
})
.exponent(exponent)
}
/// Handle to a PowfGen
#[derive(Copy, Clone, Debug)]
pub struct PowfHandle {
pub(crate) node_id: NodeId,
pub(crate) num_channels: usize,
}
impl PowfHandle {
/// Set the exponent
pub fn exponent(self, exponent: impl Into<Input>) -> Handle<Self> {
let inp = exponent.into();
match inp {
Input::Constant(v) => {
knyst::knyst_commands().connect(
crate::graph::connection::constant(v)
.to(self.node_id)
.to_channel(0),
);
}
Input::Handle { output_channels } => {
for (node_id, chan) in output_channels {
crate::modal_interface::knyst_commands()
.connect(node_id.to(self.node_id).from_channel(chan).to_channel(0));
}
}
}
Handle::new(self)
}
}
impl HandleData for PowfHandle {
fn out_channels(&self) -> SourceChannelIter {
SourceChannelIter::single_node_id(self.node_id, self.num_channels)
}

fn in_channels(&self) -> SinkChannelIter {
SinkChannelIter::single_node_id(self.node_id, self.num_channels + 1)
}

fn node_ids(&self) -> NodeIdIter {
NodeIdIter::Single(self.node_id)
}
}

/// Mul(num out channels)
///
Expand Down
12 changes: 6 additions & 6 deletions knyst/src/gen/filter/svf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum SvfFilterType {
/// A versatile EQ filter implementation
///
/// Implemented based on [a technical paper by Andrew Simper, Cytomic, 2013](https://cytomic.com/files/dsp/SvfLinearTrapOptimised2.pdf) also available at <https://cytomic.com/technical-papers/>
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct SvfFilter {
ty: SvfFilterType,
cutoff_freq: Sample,
Expand Down Expand Up @@ -277,15 +277,15 @@ impl SvfDynamic {
) -> GenState {
if cutoff_freq[0] != self.last_cutoff || gain[0] != self.last_gain || q[0] != self.last_q {
// A q value of 0.0 will result in NaN
let mut q = q[0];
if q <= 0.0 {
q = f32::MIN;
let mut safe_q = q[0];
if safe_q <= 0.0 {
safe_q = f32::MIN;
}
self.svf
.set_coeffs(cutoff_freq[0], q, gain[0], *sample_rate);
.set_coeffs(cutoff_freq[0], safe_q, gain[0], *sample_rate);
self.last_cutoff = cutoff_freq[0];
self.last_gain = gain[0];
self.last_q = q;
self.last_q = q[0];
}
self.svf.process(input, output);
GenState::Continue
Expand Down
44 changes: 1 addition & 43 deletions knyst/src/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use std::{

use crate::{
graph::{Change, Connection, GraphId, ParameterChange, SimultaneousChanges},
prelude::{PowfGen, SubGen},
prelude::{PowfGen, PowfHandle, SubGen},
Sample,
};

Expand Down Expand Up @@ -216,48 +216,6 @@ impl<H: HandleData + Copy> HandleData for Handle<H> {
}
}

/// Handle to a PowfGen
#[derive(Copy, Clone, Debug)]
pub struct PowfHandle {
node_id: NodeId,
num_channels: usize,
}
impl PowfHandle {
/// Set the exponent
pub fn exponent(self, exponent: impl Into<Input>) -> Handle<Self> {
let inp = exponent.into();
match inp {
Input::Constant(v) => {
knyst_commands().connect(
crate::graph::connection::constant(v)
.to(self.node_id)
.to_channel(0),
);
}
Input::Handle { output_channels } => {
for (node_id, chan) in output_channels {
crate::modal_interface::knyst_commands()
.connect(node_id.to(self.node_id).from_channel(chan).to_channel(0));
}
}
}
Handle::new(self)
}
}
impl HandleData for PowfHandle {
fn out_channels(&self) -> SourceChannelIter {
SourceChannelIter::single_node_id(self.node_id, self.num_channels)
}

fn in_channels(&self) -> SinkChannelIter {
SinkChannelIter::single_node_id(self.node_id, self.num_channels + 1)
}

fn node_ids(&self) -> NodeIdIter {
NodeIdIter::Single(self.node_id)
}
}

/// Handle to a single output channel from a node.
#[derive(Copy, Clone, Debug)]
pub struct OutputChannelHandle {
Expand Down

0 comments on commit f390772

Please sign in to comment.