Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native effects #30

Merged
merged 10 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions effects/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
10 changes: 10 additions & 0 deletions effects/rust/raindrop/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
11 changes: 11 additions & 0 deletions effects/rust/raindrop/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "raindrop"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
rand = "0.8.5"
turbo_plugin = { path = "../../../turbo_plugin" }
26 changes: 26 additions & 0 deletions effects/rust/raindrop/Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[tasks.mkdir]
command = "mkdir"
args = ["-p", "../../bin"]

[tasks.rm]
command = "rm"
args = ["-f", "../../bin/libraindrop.so"]
dependencies = ["build-release", "mkdir"]

[tasks.build-debug]
command = "cargo"
args = ["build"]

[tasks.install-debug]
command = "cp"
args = ["target/debug/libraindrop.so", "../../bin"]
dependencies = ["build-debug", "mkdir", "rm"]

[tasks.build-release]
command = "cargo"
args = ["build", "--release"]

[tasks.install-release]
command = "cp"
args = ["target/release/libraindrop.so", "../../bin"]
dependencies = ["build-release", "mkdir", "rm"]
179 changes: 179 additions & 0 deletions effects/rust/raindrop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
use rand::Rng;
use std::sync::Mutex;
use turbo_plugin::{Color, Plugin, VTable};

#[derive(Clone, Copy, Debug)]
pub struct RaindropSettings {
pub rain_speed: i32,
pub drop_rate: f64,
}

#[derive(Clone, Copy, Debug)]
pub enum RipleDirection {
Left,
Right,
}

#[derive(Debug, Default)]
pub struct RaindropState {
riples: Vec<(usize, Color, RipleDirection)>,
}

struct Soin {
state: Mutex<RaindropState>,
}

impl Soin {
pub fn new() -> Self {
println!("New!!");
Self {
state: Default::default(),
}
}
}

impl Plugin for Soin {
fn name(&self) -> *const std::ffi::c_char {
static NAME: &[u8] = b"Soin\0";
static CSTR_NAME: &std::ffi::CStr =
unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(NAME) };
CSTR_NAME.as_ptr()
}

fn tick(&self, leds: &mut [Color]) {
let mut state = self.state.lock().unwrap();
leds.fill(Color { r: 0, g: 0, b: 0 });
let color_size = leds.len();
let mut next_riples: Vec<(usize, Color, RipleDirection)> = vec![];
const SHIFT: usize = 1;
for (current_position, color, direction) in &state.riples {
let next_position = match direction {
RipleDirection::Left => {
if current_position < &SHIFT {
continue;
}
current_position - SHIFT
}
RipleDirection::Right => {
if current_position + SHIFT >= color_size {
continue;
}
current_position + SHIFT
}
};
const NUMERATOR: u8 = 3;
const DENOMINATOR: u8 = 4;
let next_color = Color {
r: color.r / DENOMINATOR * NUMERATOR,
g: color.g / DENOMINATOR * NUMERATOR,
b: color.b / DENOMINATOR * NUMERATOR,
};
if let Some(led) = leds.get_mut(next_position) {
led.r += next_color.r;
led.g += next_color.g;
led.b += next_color.b;
next_riples.push((next_position, next_color, *direction));
}
}

if !rand::thread_rng().gen_bool(0.5) {
state.riples = next_riples;
return;
}

for _ in 0..1 {
let new_position = rand::thread_rng().gen_range(0..color_size);
let next_color = match rand::thread_rng().gen_range(0..4) {
0 => Color {
r: 255,
g: 0,
b: 255,
},
1 => Color {
r: 255,
g: 255,
b: 0,
},
2 => Color {
r: 0,
g: 255,
b: 255,
},
3 => Color {
r: 255,
g: 255,
b: 255,
},
_ => unreachable!(),
};
*leds.get_mut(new_position).expect("Rng lib failed.") = next_color;
next_riples.push((new_position, next_color, RipleDirection::Left));
next_riples.push((new_position, next_color, RipleDirection::Right));
}

state.riples = next_riples;
}

fn load() {
println!("Loading shared library");
}

fn unload() {
println!("Unloading shared library");
}
}

impl Drop for Soin {
fn drop(&mut self) {
println!("Dropping plugin instance");
}
}

#[no_mangle]
extern "C" fn _plugin_vtable() -> *const std::ffi::c_void {
extern "C" fn plugin_create() -> *mut std::ffi::c_void {
let plugin = Box::new(Soin::new());
Box::into_raw(plugin) as *mut _
}

extern "C" fn plugin_destroy(plugin: *mut std::ffi::c_void) {
unsafe {
drop(Box::from_raw(plugin as *mut Soin));
}
}

extern "C" fn name(plugin: *const std::ffi::c_void) -> *const std::ffi::c_char {
let plugin = unsafe { &*(plugin as *const Soin) };
plugin.name()
}

extern "C" fn tick(
plugin: *const std::ffi::c_void,
colors: *mut Color,
len: std::ffi::c_ulong,
) {
let plugin = unsafe { &*(plugin as *const Soin) };
let slice = unsafe { std::slice::from_raw_parts_mut(colors, len as _) };
plugin.tick(slice);
}

extern "C" fn load(audio_api: turbo_plugin::AudioApi) {
turbo_plugin::on_load(audio_api);
Soin::load();
}

extern "C" fn unload() {
Soin::unload();
}

static VTABLE: VTable = VTable {
plugin_create,
plugin_destroy,
name,
tick,
load,
unload,
};

&VTABLE as *const VTable as *const _
}
3 changes: 3 additions & 0 deletions turbo_audio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dasp_signal = "0.11.0"
dasp_window = { version = "0.11.0", features = ["hanning"]}
env_logger = "0.10.0"
jsonschema = "0.16.1"
libloading = "0.8.1"
log = "0.4.17"
mlua = { version = "0.9.2", features = ["luajit52", "vendored", "async", "send", "serialize", "send"] }
notify-debouncer-mini = { version = "0.4.1" }
Expand All @@ -28,3 +29,5 @@ ringbuf = "0.3.3"
rustfft = "6.1.0"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
thiserror = "1.0.50"
turbo_plugin = { path = "../turbo_plugin" }
10 changes: 3 additions & 7 deletions turbo_audio/Settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
],
"effect_settings": [
{
"setting": {
"Lua": {
"settings": {}
}
},
"setting": "Native",
"id": 1
}
],
Expand All @@ -35,15 +31,15 @@
"effect_id": 1,
"settings_id": 1,
"effect": {
"Lua": "sketchers.lua"
"Native": "../effects/bin/libraindrop.so"
}
}
],
"devices": [
{
"type": "Tcp",
"connection": {
"Tcp": "192.168.0.164:1234"
"Tcp": "127.0.0.1:42069"
},
"id": 1
}
Expand Down
7 changes: 2 additions & 5 deletions turbo_audio/src/config_parser.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
use std::path::PathBuf;

use crate::audio::pipewire_listener::StreamConnections;
use crate::resources::color::Color;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub enum EffectConfigType {
Lua(String),
Moody(),
Raindrop(),
Native(String),
}

#[derive(Debug, Serialize, Deserialize)]
pub enum SettingsConfigType {
Native,
Lua(serde_json::Value),
Moody(Color),
Raindrop(),
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
Loading
Loading