Skip to content

Commit

Permalink
Add monome grid chop.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Feb 4, 2024
1 parent bf9d2ca commit 6cf07c3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ members = [
"plugins/chop/euro-filter",
"plugins/chop/filter",
"plugins/chop/generator",
"plugins/chop/monome-grid",
"plugins/chop/python",
"plugins/chop/wasm",
"plugins/dat/filter",
Expand Down
16 changes: 16 additions & 0 deletions plugins/chop/monome-grid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "monome-grid"
version = "0.1.0"
edition = "2021"

[package.metadata.td-rs]
type = "chop"

[lib]
name = "monome_grid"
crate-type = ["staticlib"]

[dependencies]
td-rs-chop = { path = "../../../td-rs-chop" }
td-rs-derive = { path = "../../../td-rs-derive" }
monome-rs = "1.1.3"
118 changes: 118 additions & 0 deletions plugins/chop/monome-grid/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use monome::{KeyDirection, Monome, MonomeDevice, MonomeEvent};
use td_rs_chop::*;
use td_rs_derive::{Param, Params};

#[derive(Param, Default, Clone, Debug)]
enum Operation {
#[default]
Add,
Multiply,
Power,
}

#[derive(Params, Default, Clone, Debug, Eq, PartialEq)]
struct MonomeGridParams {
#[param(label = "Prefix", page = "Grid", default = "/touchdesigner")]
prefix: String,
#[param(label = "Hold", page = "Grid")]
hold: bool,
}

/// Struct representing our CHOP's state
#[derive(Debug)]
pub struct MonomeGrid {
params: MonomeGridParams,
prev_params: MonomeGridParams,
device: Option<Monome>,
grid: [bool; 128],
}

/// Impl block providing default constructor for plugin
impl OpNew for MonomeGrid {
fn new(_info: NodeInfo) -> Self {
Self {
params: MonomeGridParams {
prefix: "/touchdesigner".to_string(),
hold: false,
},
prev_params: Default::default(),
device: None,
grid: [false; 128],
}
}
}

impl OpInfo for MonomeGrid {
const OPERATOR_LABEL: &'static str = "Monome Grid";
const OPERATOR_TYPE: &'static str = "Monomegrid";
}

impl Op for MonomeGrid {
fn params_mut(&mut self) -> Option<Box<&mut dyn OperatorParams>> {
Some(Box::new(&mut self.params))
}
}

impl Chop for MonomeGrid {
fn execute(&mut self, output: &mut ChopOutput, inputs: &OperatorInputs<ChopInput>) {
if self.params != self.prev_params || self.device.is_none() {
self.prev_params = self.params.clone();
let device = match Monome::new(&self.params.prefix) {
Ok(device) => device,
Err(err) => {
self.set_error(&format!("Error connecting to monome: {}", err));
return;
}
};
self.device = Some(device);
}

if let Some(ref mut device) = &mut self.device {
while let Some(event) = device.poll() {
match event {
MonomeEvent::GridKey { x, y, direction, } => {
let index = (y * 16 + x) as usize;
if self.params.hold {
if matches!(direction, KeyDirection::Down) {
self.grid[index] = !self.grid[index];
}
} else {
self.grid[index] = !self.grid[index];
}
}
_ => {}
}
}

device.set_all(&self.grid);
}

for i in 0..output.num_channels() {
output[i][0] = if self.grid[i] { 1.0 } else { 0.0 };
}
}

fn general_info(&self, _inputs: &OperatorInputs<ChopInput>) -> ChopGeneralInfo {
ChopGeneralInfo {
cook_every_frame: true,
cook_every_frame_if_asked: true,
timeslice: false,
input_match_index: 0,
}
}

fn channel_name(&self, index: usize, _inputs: &OperatorInputs<ChopInput>) -> String {
format!("grid{}", index)
}

fn output_info(&self, _inputs: &OperatorInputs<ChopInput>) -> Option<ChopOutputInfo> {
Some(ChopOutputInfo {
num_channels: 128,
num_samples: 1,
start_index: 0,
..Default::default()
})
}
}

chop_plugin!(MonomeGrid);

0 comments on commit 6cf07c3

Please sign in to comment.