Skip to content

Commit

Permalink
Add MtsClient for MTS-ESP client interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
greatest-ape committed Mar 18, 2024
1 parent 149da82 commit a55b17e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ allow = [
"Unicode-DFS-2016",
"zlib-acknowledgement",
"MIT-0",
"CC0-1.0"
"CC0-1.0",
"0BSD"
# "FTL",
#"Apache-2.0 WITH LLVM-exception",
]
Expand Down
2 changes: 2 additions & 0 deletions octasine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ indexmap = { version = "2", features = ["serde"] }
log = { version = "0.4", default-features = false }
log-panics = "2"
memchr = "2"
mts-esp-client-sys = { git = "https://github.com/greatest-ape/mts-esp-client-sys.git", rev = "da259c3" }
once_cell = "1"
os_info = "3"
ringbuf = "0.3"
Expand All @@ -60,6 +61,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
simplelog = { version = "0.12", default-features = false, features = ["local-offset"] }
sleef-trig = "0.1.0"
tune = "0.34"

# vst2

Expand Down
5 changes: 5 additions & 0 deletions octasine/src/plugin/clap/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::{
audio::{gen::process_f32_runtime_select, AudioState},
common::{BeatsPerMinute, EventToHost, NoteEvent, NoteEventInner, SampleRate},
parameters::ParameterKey,
plugin::common::MtsClient,
sync::SyncState,
utils::{init_logging, update_audio_parameters},
};
Expand All @@ -55,6 +56,7 @@ pub struct OctaSine {
pub gui_parent: Mutex<Option<ParentWindow>>,
pub gui_window_handle: Mutex<Option<WindowHandle<crate::gui::Message>>>,
pub clap_plugin: AtomicRefCell<clap_plugin>,
pub mts_client: MtsClient,
}

impl OctaSine {
Expand All @@ -68,6 +70,8 @@ impl OctaSine {
host,
};

let mts_client = unsafe { MtsClient::new() };

let plugin = Self {
host,
audio: Default::default(),
Expand All @@ -89,6 +93,7 @@ impl OctaSine {
get_extension: Some(Self::get_extension),
on_main_thread: Some(Self::on_main_thread),
}),
mts_client,
};

let plugin = Arc::new(plugin);
Expand Down
47 changes: 47 additions & 0 deletions octasine/src/plugin/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use std::os::raw::c_char;

use mts_esp_client_sys::{
MTSClient, MTS_DeregisterClient, MTS_NoteToFrequency, MTS_RegisterClient,
};

pub const PLUGIN_UNIQUE_VST2_ID: i32 = 1_438_048_626;
pub const PLUGIN_SEMVER_NAME: &str = "OctaSine v0.9";

Expand All @@ -7,6 +13,47 @@ pub fn crate_version_to_vst2_format(crate_version: &str) -> i32 {
.expect("convert crate version to i32")
}

pub struct MtsClient(*mut MTSClient);

impl MtsClient {
/// Safety: only call once per instance
pub unsafe fn new() -> Self {
Self(MTS_RegisterClient())
}

/// Safety: only call from one thread
pub unsafe fn note_to_frequency(&self, midi_note: u8, midi_channel: Option<u8>) -> f64 {
let midi_note = c_char::from_ne_bytes(midi_note.min(127).to_ne_bytes());
let midi_channel = midi_channel
.map(|midi_channel| c_char::from_ne_bytes(midi_channel.min(15).to_ne_bytes()))
.unwrap_or_else(|| {
// Try to account for the fact that ARM c_char is unsigned,
// while the API wants to be passed -1 to indicate "no
// particular channel"
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
{
u8::from_ne_bytes((-1i8).to_ne_bytes())
}
#[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))]
{
-1i8
}
});

MTS_NoteToFrequency(self.0, midi_note, midi_channel)
}
}

impl Drop for MtsClient {
fn drop(&mut self) {
unsafe {
MTS_DeregisterClient(self.0);
}
}
}

unsafe impl Send for MtsClient {}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 7 additions & 1 deletion octasine/src/plugin/vst2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ use crate::sync::SyncState;
use crate::utils::{init_logging, update_audio_parameters};
use crate::{common::*, crate_version};

use super::common::{crate_version_to_vst2_format, PLUGIN_SEMVER_NAME, PLUGIN_UNIQUE_VST2_ID};
use super::common::{
crate_version_to_vst2_format, MtsClient, PLUGIN_SEMVER_NAME, PLUGIN_UNIQUE_VST2_ID,
};

pub struct OctaSine {
pub audio: Box<AudioState>,
pub sync: Arc<SyncState<vst::plugin::HostCallback>>,
pub mts_client: MtsClient,
#[cfg(feature = "gui")]
editor: Option<editor::Editor<Arc<SyncState<vst::plugin::HostCallback>>>>,
}
Expand All @@ -39,12 +42,15 @@ impl OctaSine {

let sync = Arc::new(SyncState::new(host));

let mts_client = unsafe { MtsClient::new() };

#[cfg(feature = "gui")]
let editor = editor::Editor::new(sync.clone());

Self {
audio: Default::default(),
sync,
mts_client,
#[cfg(feature = "gui")]
editor: Some(editor),
}
Expand Down

0 comments on commit a55b17e

Please sign in to comment.