Skip to content

Commit

Permalink
everything sucks but kinda works
Browse files Browse the repository at this point in the history
  • Loading branch information
dvub committed Dec 18, 2024
1 parent 98c43e9 commit 697056a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 67 deletions.
44 changes: 33 additions & 11 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "circle_of_5ths"
name = "midiometry"
version = "0.1.0"
edition = "2021"
authors = ["dvub <[email protected]>"]
Expand All @@ -14,14 +14,17 @@ members = ["xtask"]
crate-type = ["cdylib"]

[dependencies]
include_dir = "0.7.4"
mime_guess = "2.0.5"
nih_plug = { git = "https://github.com/robbert-vdh/nih-plug.git" }
# Remove the `assert_process_allocs` feature to allow allocations on the audio
# thread in debug builds.
# Uncomment the below line to disable the on-by-default VST3 feature to remove
# the GPL compatibility requirement
# nih_plug = { git = "https://github.com/robbert-vdh/nih-plug.git", default-features = false, features = ["assert_process_allocs"] }

nih_plug_webview = { path = "../dvub-nih-plug-webview" }
nih_plug_webview = { git = "https://github.com/dvub/nih-plug-webview.git" }
# nih_plug_webview = { path = "../dvub-nih-plug-webview" }
rtrb = "0.3.1"
serde_json = "1.0.133"
triple_buffer = "8.0.0"
Expand Down
4 changes: 2 additions & 2 deletions bundler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# [package_name]
# name = "Human Readable Plugin Name" # defaults to <package_name>

[circle_of_5ths]
name = "Circle Of 5ths"
[midiometry]
name = "midiometry"
48 changes: 44 additions & 4 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::{
sync::{Arc, Mutex},
};

use include_dir::include_dir;
use nih_plug::midi::{MidiResult, NoteEvent};
use nih_plug_webview::{
editors::add_asset_dir_protocol, DropData, DropEffect, EventStatus, HTMLSource, Key,
MouseEvent, WebViewEditor,
http::Response, DropData, DropEffect, EventStatus, HTMLSource, Key, MouseEvent, WebViewEditor,
};
use rtrb::Consumer;
use serde_json::json;
Expand All @@ -16,11 +16,51 @@ pub fn create_editor(buffer: Consumer<NoteEvent<()>>) -> WebViewEditor {

let size = (400, 400);

#[cfg(debug_assertions)]
let src = HTMLSource::URL("http://localhost:3000".to_owned());

let mut editor = WebViewEditor::new(src, size);

#[cfg(not(debug_assertions))]
{
editor = {
let protocol_name = "assets";

#[cfg(target_os = "windows")]
let url_scheme = format!("http://{}.localhost", protocol_name);

#[cfg(not(target_os = "windows"))]
let url_scheme = format!("{}://localhost", protocol_name);

let src = HTMLSource::URL(url_scheme);
let mut editor = WebViewEditor::new(src, size);

editor = editor.with_custom_protocol(protocol_name.to_string(), move |req| {
let path = req.uri().path();
let file = if path == "/" {
"index.html"
} else {
&path[1..]
};

let dir = include_dir!("$CARGO_MANIFEST_DIR/target/bundled/dist");

// mime guess is awesome!
let mime_type =
mime_guess::from_ext(Path::new(file).extension().unwrap().to_str().unwrap())
.first_or_text_plain() // TODO: fix _or_...
.to_string();
if let Some(result_file) = dir.get_file(file) {
return Response::builder()
.header("content-type", mime_type)
.header("Access-Control-Allow-Origin", "*")
.body(result_file.contents().into())
.map_err(Into::into);
}
panic!("Web asset not found.")
});
editor
};
}

editor = editor
.with_developer_mode(true)
.with_keyboard_handler(move |event| {
Expand Down
58 changes: 10 additions & 48 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use editor::create_editor;
use nih_plug::prelude::*;
use rtrb::{Producer, RingBuffer};
use std::sync::Arc;
use triple_buffer::triple_buffer;

// This is a shortened version of the gain example with most comments removed, check out
// https://github.com/robbert-vdh/nih-plug/blob/master/plugins/examples/gain/src/lib.rs to get
Expand All @@ -15,15 +14,8 @@ struct PluginStruct {
input_buffer: Option<Producer<NoteEvent<()>>>,
}

#[derive(Params)]
struct PluginStructParams {
/// The parameter's ID is used to identify the parameter in the wrappred plugin API. As long as
/// these IDs remain constant, you can rename and reorder these fields as you wish. The
/// parameters are exposed to the host in the same order they were defined. In this case, this
/// gain parameter is stored as linear gain while the values are displayed in decibels.
#[id = "gain"]
pub gain: FloatParam,
}
#[derive(Params, Default)]
struct PluginStructParams {}

impl Default for PluginStruct {
fn default() -> Self {
Expand All @@ -34,38 +26,8 @@ impl Default for PluginStruct {
}
}

impl Default for PluginStructParams {
fn default() -> Self {
Self {
// This gain is stored as linear gain. NIH-plug comes with useful conversion functions
// to treat these kinds of parameters as if we were dealing with decibels. Storing this
// as decibels is easier to work with, but requires a conversion for every sample.
gain: FloatParam::new(
"Gain",
util::db_to_gain(0.0),
FloatRange::Skewed {
min: util::db_to_gain(-30.0),
max: util::db_to_gain(30.0),
// This makes the range appear as if it was linear when displaying the values as
// decibels
factor: FloatRange::gain_skew_factor(-30.0, 30.0),
},
)
// Because the gain parameter is stored as linear gain instead of storing the value as
// decibels, we need logarithmic smoothing
.with_smoother(SmoothingStyle::Logarithmic(50.0))
.with_unit(" dB")
// There are many predefined formatters we can use here. If the gain was stored as
// decibels instead of as a linear gain value, we could have also used the
// `.with_step_size(0.1)` function to get internal rounding.
.with_value_to_string(formatters::v2s_f32_gain_to_db(2))
.with_string_to_value(formatters::s2v_f32_gain_to_db()),
}
}
}

impl Plugin for PluginStruct {
const NAME: &'static str = "Circle Of 5ths";
const NAME: &'static str = "MIDIOMETRY";
const VENDOR: &'static str = "dvub";
const URL: &'static str = env!("CARGO_PKG_HOMEPAGE");
const EMAIL: &'static str = "[email protected]";
Expand Down Expand Up @@ -129,18 +91,18 @@ impl Plugin for PluginStruct {
context: &mut impl ProcessContext<Self>,
) -> ProcessStatus {
for channel_samples in buffer.iter_samples() {
// Smoothing is optionally built into the parameters themselves
let gain = self.params.gain.smoothed.next();

// NOT SURE IF THIS IS NEEDED...
for sample in channel_samples {
*sample *= gain;
*sample *= 1.0;
}

// TODO:
// does it matter where this goes?
if let Some(event) = context.next_event() {
nih_log!("Sending an event to the GUI Thread...");
if let Some(buffer) = &mut self.input_buffer {
buffer.push(event).unwrap();
}
// im fairly sure this is necessary
context.send_event(event);
}
}

Expand All @@ -161,7 +123,7 @@ impl Plugin for PluginStruct {
}

impl ClapPlugin for PluginStruct {
const CLAP_ID: &'static str = "com.your-domain.circle-of-5ths";
const CLAP_ID: &'static str = "com.your-domain.midiometry";
const CLAP_DESCRIPTION: Option<&'static str> = Some("A cool plugin for vis midi input");
const CLAP_MANUAL_URL: Option<&'static str> = Some(Self::URL);
const CLAP_SUPPORT_URL: Option<&'static str> = None;
Expand Down

0 comments on commit 697056a

Please sign in to comment.