From 284f5615cc5244e8798fbf21826ae5b9001ef8ec Mon Sep 17 00:00:00 2001 From: Jade Rowland Date: Tue, 29 Aug 2023 18:14:02 -0400 Subject: [PATCH 1/6] bridge functionality for midi --- packages/midi/midibridge.mjs | 58 +++++++++ src-tauri/Cargo.lock | 244 ++++++++++++++++++++++++++++++++++- src-tauri/Cargo.toml | 2 + src-tauri/src/main.rs | 17 ++- src-tauri/src/midibridge.rs | 154 ++++++++++++++++++++++ 5 files changed, 467 insertions(+), 8 deletions(-) create mode 100644 packages/midi/midibridge.mjs create mode 100644 src-tauri/src/midibridge.rs diff --git a/packages/midi/midibridge.mjs b/packages/midi/midibridge.mjs new file mode 100644 index 00000000..803259a1 --- /dev/null +++ b/packages/midi/midibridge.mjs @@ -0,0 +1,58 @@ +import { invoke } from '@tauri-apps/api/tauri'; +import { noteToMidi } from '@strudel.cycles/core'; + +const ON_MESSAGE = 0x90; +const OFF_MESSAGE = 0x80; + +export function playNote(hap, offset, output) { + const { note, nrpnn, nrpv, ccn, ccv } = hap.value; + const velocity = Math.floor((hap.context?.velocity ?? 0.9) * 100); // TODO: refactor velocity + const duration = Math.floor(hap.duration.valueOf() * 1000 - 10); + const roundedOffset = Math.round(offset); + const midichan = (hap.value.midichan ?? 1) - 1; + const requestedport = output ?? 'IAC'; + const messagesfromjs = []; + if (note != null) { + const midiNumber = typeof note === 'number' ? note : noteToMidi(note); + + messagesfromjs.push({ + requestedport, + message: [ON_MESSAGE + midichan, midiNumber, velocity], + offset: roundedOffset, + }); + + messagesfromjs.push({ + requestedport, + message: [OFF_MESSAGE + midichan, midiNumber, velocity], + offset: roundedOffset + duration, + }); + } + + if (ccv && ccn) { + if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) { + throw new Error('expected ccv to be a number between 0 and 1'); + } + if (!['string', 'number'].includes(typeof ccn)) { + throw new Error('expected ccn to be a number or a string'); + } + const scaled = Math.round(ccv * 127); + messagesfromjs.push({ + requestedport, + message: [ON_MESSAGE + midichan, ccn, scaled], + offset: roundedOffset, + }); + + messagesfromjs.push({ + requestedport, + message: [OFF_MESSAGE + midichan, ccn, scaled], + offset: roundedOffset + duration, + }); + } + + // invoke is temporarily blocking, run in an async process + if (messagesfromjs.length) { + setTimeout(() => { + invoke('sendmidi', { messagesfromjs }); + }); + } +} diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 37c7f0a6..90767fec 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + [[package]] name = "adler" version = "1.0.2" @@ -41,6 +50,28 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "alsa" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2562ad8dcf0f789f65c6fdaad8a8a9708ed6b488e649da28c01656ad66b8b47" +dependencies = [ + "alsa-sys", + "bitflags", + "libc", + "nix", +] + +[[package]] +name = "alsa-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db8fee663d06c4e303404ef5f40488a53e062f89ba8bfed81f42325aafad1527" +dependencies = [ + "libc", + "pkg-config", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -66,10 +97,12 @@ checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" name = "app" version = "0.1.0" dependencies = [ + "midir", "serde", "serde_json", "tauri", "tauri-build", + "tokio", ] [[package]] @@ -102,6 +135,21 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "backtrace" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "base64" version = "0.13.1" @@ -378,6 +426,26 @@ dependencies = [ "libc", ] +[[package]] +name = "coremidi" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a7847ca018a67204508b77cb9e6de670125075f7464fff5f673023378fa34f5" +dependencies = [ + "core-foundation", + "core-foundation-sys", + "coremidi-sys", +] + +[[package]] +name = "coremidi-sys" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79a6deed0c97b2d40abbab77e4c97f81d71e162600423382c277dd640019116c" +dependencies = [ + "core-foundation-sys", +] + [[package]] name = "cpufeatures" version = "0.2.8" @@ -910,6 +978,12 @@ dependencies = [ "wasi 0.11.0+wasi-snapshot-preview1", ] +[[package]] +name = "gimli" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" + [[package]] name = "gio" version = "0.15.12" @@ -1467,6 +1541,22 @@ dependencies = [ "autocfg", ] +[[package]] +name = "midir" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a456444d83e7ead06ae6a5c0a215ed70282947ff3897fb45fcb052b757284731" +dependencies = [ + "alsa", + "bitflags", + "coremidi", + "js-sys", + "libc", + "wasm-bindgen", + "web-sys", + "windows 0.43.0", +] + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -1477,6 +1567,17 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "mio" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" +dependencies = [ + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys", +] + [[package]] name = "ndk" version = "0.6.0" @@ -1511,6 +1612,17 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" +[[package]] +name = "nix" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" +dependencies = [ + "bitflags", + "cfg-if", + "libc", +] + [[package]] name = "nodrop" version = "0.1.14" @@ -1616,6 +1728,15 @@ dependencies = [ "objc", ] +[[package]] +name = "object" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ac5bbd07aea88c60a577a1ce218075ffd59208b2d7ca97adf9bfc5aeb21ebe" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.18.0" @@ -1782,9 +1903,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.9" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "pin-utils" @@ -2052,6 +2173,12 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + [[package]] name = "rustc_version" version = "0.4.0" @@ -2274,6 +2401,15 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "simd-adler32" version = "0.3.5" @@ -2301,6 +2437,16 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +[[package]] +name = "socket2" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2538b18701741680e0322a2302176d3253a35388e2e62f172f64f4f16605f877" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "soup2" version = "0.2.1" @@ -2785,17 +2931,34 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.28.2" +version = "1.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94d7b1cfd2aa4011f2de74c2c4c63665e27a71006b0a192dcd2710272e73dfa2" +checksum = "17ed6077ed6cd6c74735e21f37eb16dc3935f96878b1fe961074089cc80893f9" dependencies = [ - "autocfg", + "backtrace", "bytes", + "libc", + "mio", "num_cpus", + "parking_lot", "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", "windows-sys", ] +[[package]] +name = "tokio-macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.18", +] + [[package]] name = "toml" version = "0.5.11" @@ -3090,6 +3253,16 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" +[[package]] +name = "web-sys" +version = "0.3.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "webkit2gtk" version = "0.18.2" @@ -3220,6 +3393,21 @@ dependencies = [ "windows_x86_64_msvc 0.39.0", ] +[[package]] +name = "windows" +version = "0.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04662ed0e3e5630dfa9b26e4cb823b817f1a9addda855d973a9458c236556244" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + [[package]] name = "windows" version = "0.48.0" @@ -3270,12 +3458,12 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" dependencies = [ - "windows_aarch64_gnullvm", + "windows_aarch64_gnullvm 0.48.0", "windows_aarch64_msvc 0.48.0", "windows_i686_gnu 0.48.0", "windows_i686_msvc 0.48.0", "windows_x86_64_gnu 0.48.0", - "windows_x86_64_gnullvm", + "windows_x86_64_gnullvm 0.48.0", "windows_x86_64_msvc 0.48.0", ] @@ -3285,6 +3473,12 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f838de2fe15fe6bac988e74b798f26499a8b21a9d97edec321e79b28d1d7f597" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.48.0" @@ -3297,6 +3491,12 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec7711666096bd4096ffa835238905bb33fb87267910e154b18b44eaabb340f2" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.48.0" @@ -3309,6 +3509,12 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "763fc57100a5f7042e3057e7e8d9bdd7860d330070251a73d003563a3bb49e1b" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.48.0" @@ -3321,6 +3527,12 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bc7cbfe58828921e10a9f446fcaaf649204dcfe6c1ddd712c5eebae6bda1106" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.48.0" @@ -3333,12 +3545,24 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6868c165637d653ae1e8dc4d82c25d4f97dd6605eaa8d784b5c6e0ab2a252b65" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.0" @@ -3351,6 +3575,12 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e4d40883ae9cae962787ca76ba76390ffa29214667a111db9e0a1ad8377e809" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.48.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index b7401f85..22170cac 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -18,6 +18,8 @@ tauri-build = { version = "1.4.0", features = [] } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } tauri = { version = "1.4.0", features = ["fs-all"] } +midir = "0.9.1" +tokio = { version = "1.29.0", features = ["full"] } [features] # this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled. diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index f5c5be23..0643cefd 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,8 +1,23 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +mod midibridge; +use tokio::sync::mpsc; +use tokio::sync::Mutex; + fn main() { - tauri::Builder::default() + let (async_input_transmitter, async_input_receiver) = mpsc::channel(1); + let (async_output_transmitter, async_output_receiver) = mpsc::channel(1); + tauri::Builder + ::default() + .manage(midibridge::AsyncInputTransmit { + inner: Mutex::new(async_input_transmitter), + }) + .invoke_handler(tauri::generate_handler![midibridge::sendmidi]) + .setup(|_app| { + midibridge::init(async_input_receiver, async_output_receiver, async_output_transmitter); + Ok(()) + }) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/src-tauri/src/midibridge.rs b/src-tauri/src/midibridge.rs new file mode 100644 index 00000000..5dc7d0c6 --- /dev/null +++ b/src-tauri/src/midibridge.rs @@ -0,0 +1,154 @@ +use std::collections::HashMap; +use std::sync::Arc; +use midir::MidiOutput; +use tokio::sync::mpsc; +use tokio::sync::Mutex; +use tokio::time::Instant; +use serde::Deserialize; + +pub struct MidiMessage { + pub message: Vec, + pub instant: Instant, + pub offset: u64, + pub requestedport: String, +} + +pub struct AsyncInputTransmit { + pub inner: Mutex>>, +} + +pub fn init( + async_input_receiver: mpsc::Receiver>, + mut async_output_receiver: mpsc::Receiver>, + async_output_transmitter: mpsc::Sender> +) { + tauri::async_runtime::spawn(async move { async_process_model(async_input_receiver, async_output_transmitter).await }); + let message_queue: Arc>> = Arc::new(Mutex::new(Vec::new())); + + /* ........................................................... + Listen For incoming messages and add to queue + ............................................................*/ + let message_queue_clone = Arc::clone(&message_queue); + tauri::async_runtime::spawn(async move { + loop { + if let Some(package) = async_output_receiver.recv().await { + let mut message_queue = message_queue_clone.lock().await; + let messages = package; + for message in messages { + (*message_queue).push(message); + } + } + } + }); + + let message_queue_clone = Arc::clone(&message_queue); + tauri::async_runtime::spawn(async move { + /* ........................................................... + Open Midi Ports + ............................................................*/ + let midiout = MidiOutput::new("strudel").unwrap(); + let out_ports = midiout.ports(); + let mut port_names = Vec::new(); + //TODO: Send these print messages to the UI logger instead of the rust console so the user can see them + if out_ports.len() == 0 { + println!(" No MIDI devices found. Connect a device or enable IAC Driver."); + return; + } + println!("Found {} midi devices!", out_ports.len()); + + // the user could reference any port at anytime during runtime, + // so let's go ahead and open them all (same behavior as web app) + let mut output_connections = HashMap::new(); + for i in 0..=out_ports.len().saturating_sub(1) { + let midiout = MidiOutput::new("strudel").unwrap(); + let ports = midiout.ports(); + let port = ports.get(i).unwrap(); + let port_name = midiout.port_name(port).unwrap(); + println!("{}", port_name); + let out_con = midiout.connect(port, &port_name).unwrap(); + port_names.insert(i, port_name.clone()); + output_connections.insert(port_name, out_con); + } + /* ........................................................... + Process queued messages + ............................................................*/ + loop { + let mut message_queue = message_queue_clone.lock().await; + + for i in 0..=message_queue.len().saturating_sub(1) { + let m = message_queue.get(i); + if m.is_none() { + continue; + } + let message = m.unwrap(); + + // dont play the message if its offset time has not elapsed + if message.instant.elapsed().as_millis() < message.offset.into() { + continue; + } + let mut out_con = output_connections.get_mut(&message.requestedport); + + // WebMidi supports getting a connection by part of its name + // ex: 'bus 1' instead of 'IAC Driver bus 1' so let's emulate that behavior + if out_con.is_none() { + let key = port_names.iter().find(|port_name| { + return port_name.contains(&message.requestedport); + }); + if key.is_some() { + out_con = output_connections.get_mut(key.unwrap()); + } + } + + if out_con.is_some() { + // process the message + if let Err(err) = (&mut out_con.unwrap()).send(&message.message) { + println!("Midi message send error: {}", err); + } + } else { + println!("failed to find midi device: {}", message.requestedport); + } + + // the message has been processed, so remove it from the queue + message_queue.remove(i); + } + } + }); +} + +pub async fn async_process_model( + mut input_reciever: mpsc::Receiver>, + output_transmitter: mpsc::Sender> +) -> Result<(), Box> { + while let Some(input) = input_reciever.recv().await { + let output = input; + output_transmitter.send(output).await?; + } + Ok(()) +} +#[derive(Deserialize)] +pub struct MessageFromJS { + message: Vec, + offset: u64, + requestedport: String, +} +// Called from JS +#[tauri::command] +pub async fn sendmidi( + messagesfromjs: Vec, + state: tauri::State<'_, AsyncInputTransmit> +) -> Result<(), String> { + let async_proc_input_tx = state.inner.lock().await; + let mut messages_to_process: Vec = Vec::new(); + + for m in messagesfromjs { + let message_to_process = MidiMessage { + instant: Instant::now(), + message: m.message, + offset: m.offset, + requestedport: m.requestedport, + }; + messages_to_process.push(message_to_process); + } + + async_proc_input_tx.send(messages_to_process).await.map_err(|e| e.to_string()) +} From c29c4c0a8bbd6568849e8377851e476c1dba9735 Mon Sep 17 00:00:00 2001 From: Jade Rowland Date: Wed, 30 Aug 2023 08:24:02 -0400 Subject: [PATCH 2/6] configuring package, wiring everything together --- index.mjs | 1 + packages/desktopbridge/README.md | 3 + packages/desktopbridge/index.mjs | 8 ++ packages/desktopbridge/midibridge.mjs | 57 +++++++++++ packages/desktopbridge/package.json | 28 ++++++ packages/desktopbridge/utils.mjs | 4 + packages/midi/midi.mjs | 138 +------------------------- packages/midi/midibridge.mjs | 58 ----------- packages/midi/webmidihandler.mjs | 133 +++++++++++++++++++++++++ pnpm-lock.yaml | 19 ++++ 10 files changed, 258 insertions(+), 191 deletions(-) create mode 100644 packages/desktopbridge/README.md create mode 100644 packages/desktopbridge/index.mjs create mode 100644 packages/desktopbridge/midibridge.mjs create mode 100644 packages/desktopbridge/package.json create mode 100644 packages/desktopbridge/utils.mjs delete mode 100644 packages/midi/midibridge.mjs create mode 100644 packages/midi/webmidihandler.mjs diff --git a/index.mjs b/index.mjs index aeb5ff1b..941d6411 100644 --- a/index.mjs +++ b/index.mjs @@ -2,6 +2,7 @@ export * from './packages/core/index.mjs'; export * from './packages/csound/index.mjs'; export * from './packages/embed/index.mjs'; +export * from './packages/desktopbridge/index.mjs'; export * from './packages/midi/index.mjs'; export * from './packages/mini/index.mjs'; export * from './packages/osc/index.mjs'; diff --git a/packages/desktopbridge/README.md b/packages/desktopbridge/README.md new file mode 100644 index 00000000..6cf21bba --- /dev/null +++ b/packages/desktopbridge/README.md @@ -0,0 +1,3 @@ +# @strudel/desktopbridge + +This package contains utilities used to communicate with the Tauri backend \ No newline at end of file diff --git a/packages/desktopbridge/index.mjs b/packages/desktopbridge/index.mjs new file mode 100644 index 00000000..2cc9db3d --- /dev/null +++ b/packages/desktopbridge/index.mjs @@ -0,0 +1,8 @@ +/* +index.mjs - +Copyright (C) 2022 Strudel contributors - see +This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . +*/ + +export * from './midibridge.mjs'; +export * from './utils.mjs'; diff --git a/packages/desktopbridge/midibridge.mjs b/packages/desktopbridge/midibridge.mjs new file mode 100644 index 00000000..16ddd606 --- /dev/null +++ b/packages/desktopbridge/midibridge.mjs @@ -0,0 +1,57 @@ +import { Invoke } from './utils.mjs'; +import { noteToMidi } from '@strudel.cycles/core'; + +const ON_MESSAGE = 0x90; +const OFF_MESSAGE = 0x80; + +export function processMidi(output) { + return this.onTrigger((time, hap, currentTime) => { + console.log('here'); + const { note, nrpnn, nrpv, ccn, ccv } = hap.value; + const offset = (time - currentTime) * 1000; + const velocity = Math.floor((hap.context?.velocity ?? 0.9) * 100); // TODO: refactor velocity + const duration = Math.floor(hap.duration.valueOf() * 1000 - 10); + const roundedOffset = Math.round(offset); + const midichan = (hap.value.midichan ?? 1) - 1; + const requestedport = output ?? 'IAC'; + const messagesfromjs = []; + if (note != null) { + const midiNumber = typeof note === 'number' ? note : noteToMidi(note); + messagesfromjs.push({ + requestedport, + message: [ON_MESSAGE + midichan, midiNumber, velocity], + offset: roundedOffset, + }); + messagesfromjs.push({ + requestedport, + message: [OFF_MESSAGE + midichan, midiNumber, velocity], + offset: roundedOffset + duration, + }); + } + if (ccv && ccn) { + if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) { + throw new Error('expected ccv to be a number between 0 and 1'); + } + if (!['string', 'number'].includes(typeof ccn)) { + throw new Error('expected ccn to be a number or a string'); + } + const scaled = Math.round(ccv * 127); + messagesfromjs.push({ + requestedport, + message: [ON_MESSAGE + midichan, ccn, scaled], + offset: roundedOffset, + }); + messagesfromjs.push({ + requestedport, + message: [OFF_MESSAGE + midichan, ccn, scaled], + offset: roundedOffset + duration, + }); + } + // invoke is temporarily blocking, run in an async process + if (messagesfromjs.length) { + setTimeout(() => { + Invoke('sendmidi', { messagesfromjs }); + }); + } + }); +} diff --git a/packages/desktopbridge/package.json b/packages/desktopbridge/package.json new file mode 100644 index 00000000..bdeda2d9 --- /dev/null +++ b/packages/desktopbridge/package.json @@ -0,0 +1,28 @@ +{ + "name": "@strudel/desktopbridge", + "version": "0.1.0", + "description": "send midi messages between the JS and Tauri (Rust) sides of the Studel desktop app", + "main": "index.mjs", + "type": "module", + "repository": { + "type": "git", + "url": "git+https://github.com/tidalcycles/strudel.git" + }, + "keywords": [ + "tidalcycles", + "strudel", + "pattern", + "livecoding", + "algorave" + ], + "author": "Jade Rowland ", + "license": "AGPL-3.0-or-later", + "bugs": { + "url": "https://github.com/tidalcycles/strudel/issues" + }, + "dependencies": { + "@strudel.cycles/core": "workspace:*", + "@tauri-apps/api": "^1.4.0" + }, + "homepage": "https://github.com/tidalcycles/strudel#readme" + } \ No newline at end of file diff --git a/packages/desktopbridge/utils.mjs b/packages/desktopbridge/utils.mjs new file mode 100644 index 00000000..c4c69af5 --- /dev/null +++ b/packages/desktopbridge/utils.mjs @@ -0,0 +1,4 @@ +import { invoke } from '@tauri-apps/api/tauri'; + +export const Invoke = invoke; +export const isTauri = () => window.__TAURI_IPC__ != null; diff --git a/packages/midi/midi.mjs b/packages/midi/midi.mjs index fa86f629..5606539a 100644 --- a/packages/midi/midi.mjs +++ b/packages/midi/midi.mjs @@ -4,137 +4,9 @@ Copyright (C) 2022 Strudel contributors - see . */ -import * as _WebMidi from 'webmidi'; -import { Pattern, isPattern, logger } from '@strudel.cycles/core'; -import { noteToMidi } from '@strudel.cycles/core'; -import { Note } from 'webmidi'; -// if you use WebMidi from outside of this package, make sure to import that instance: -export const { WebMidi } = _WebMidi; +import { Pattern } from '@strudel.cycles/core'; +import * as webmidihandler from './webmidihandler.mjs'; +import * as midibridge from '../desktopbridge/midibridge.mjs'; +import { isTauri } from '../desktopbridge/utils.mjs'; -function supportsMidi() { - return typeof navigator.requestMIDIAccess === 'function'; -} - -function getMidiDeviceNamesString(outputs) { - return outputs.map((o) => `'${o.name}'`).join(' | '); -} - -export function enableWebMidi(options = {}) { - const { onReady, onConnected, onDisconnected, onEnabled } = options; - if (WebMidi.enabled) { - return; - } - if (!supportsMidi()) { - throw new Error('Your Browser does not support WebMIDI.'); - } - WebMidi.addListener('connected', () => { - onConnected?.(WebMidi); - }); - WebMidi.addListener('enabled', () => { - onEnabled?.(WebMidi); - }); - // Reacting when a device becomes unavailable - WebMidi.addListener('disconnected', (e) => { - onDisconnected?.(WebMidi, e); - }); - return new Promise((resolve, reject) => { - if (WebMidi.enabled) { - // if already enabled, just resolve WebMidi - resolve(WebMidi); - return; - } - WebMidi.enable((err) => { - if (err) { - reject(err); - } - onReady?.(WebMidi); - resolve(WebMidi); - }); - }); -} -// const outputByName = (name: string) => WebMidi.getOutputByName(name); -const outputByName = (name) => WebMidi.getOutputByName(name); - -// output?: string | number, outputs: typeof WebMidi.outputs -function getDevice(output, outputs) { - if (!outputs.length) { - throw new Error(`🔌 No MIDI devices found. Connect a device or enable IAC Driver.`); - } - if (typeof output === 'number') { - return outputs[output]; - } - if (typeof output === 'string') { - return outputByName(output); - } - // attempt to default to first IAC device if none is specified - const IACOutput = outputs.find((output) => output.name.includes('IAC')); - const device = IACOutput ?? outputs[0]; - if (!device) { - throw new Error( - `🔌 MIDI device '${output ? output : ''}' not found. Use one of ${getMidiDeviceNamesString(WebMidi.outputs)}`, - ); - } - - return IACOutput ?? outputs[0]; -} - -// Pattern.prototype.midi = function (output: string | number, channel = 1) { -Pattern.prototype.midi = function (output) { - if (isPattern(output)) { - throw new Error( - `.midi does not accept Pattern input. Make sure to pass device name with single quotes. Example: .midi('${ - WebMidi.outputs?.[0]?.name || 'IAC Driver Bus 1' - }')`, - ); - } - - enableWebMidi({ - onEnabled: ({ outputs }) => { - const device = getDevice(output, outputs); - const otherOutputs = outputs.filter((o) => o.name !== device.name); - logger( - `Midi enabled! Using "${device.name}". ${ - otherOutputs?.length ? `Also available: ${getMidiDeviceNamesString(otherOutputs)}` : '' - }`, - ); - }, - onDisconnected: ({ outputs }) => - logger(`Midi device disconnected! Available: ${getMidiDeviceNamesString(outputs)}`), - }); - - return this.onTrigger((time, hap, currentTime, cps) => { - if (!WebMidi.enabled) { - return; - } - const device = getDevice(output, WebMidi.outputs); - hap.ensureObjectValue(); - - const offset = (time - currentTime) * 1000; - // passing a string with a +num into the webmidi api adds an offset to the current time https://webmidijs.org/api/classes/Output - const timeOffsetString = `+${offset}`; - - // destructure value - const { note, nrpnn, nrpv, ccn, ccv, midichan = 1 } = hap.value; - const velocity = hap.context?.velocity ?? 0.9; // TODO: refactor velocity - - // note off messages will often a few ms arrive late, try to prevent glitching by subtracting from the duration length - const duration = Math.floor(hap.duration.valueOf() * 1000 - 10); - if (note != null) { - const midiNumber = typeof note === 'number' ? note : noteToMidi(note); - const midiNote = new Note(midiNumber, { attack: velocity, duration }); - device.playNote(midiNote, midichan, { - time: timeOffsetString, - }); - } - if (ccv && ccn) { - if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) { - throw new Error('expected ccv to be a number between 0 and 1'); - } - if (!['string', 'number'].includes(typeof ccn)) { - throw new Error('expected ccn to be a number or a string'); - } - const scaled = Math.round(ccv * 127); - device.sendControlChange(ccn, scaled, midichan, { time: timeOffsetString }); - } - }); -}; +Pattern.prototype.midi = isTauri() ? midibridge.processMidi : webmidihandler.processMidi; diff --git a/packages/midi/midibridge.mjs b/packages/midi/midibridge.mjs deleted file mode 100644 index 803259a1..00000000 --- a/packages/midi/midibridge.mjs +++ /dev/null @@ -1,58 +0,0 @@ -import { invoke } from '@tauri-apps/api/tauri'; -import { noteToMidi } from '@strudel.cycles/core'; - -const ON_MESSAGE = 0x90; -const OFF_MESSAGE = 0x80; - -export function playNote(hap, offset, output) { - const { note, nrpnn, nrpv, ccn, ccv } = hap.value; - const velocity = Math.floor((hap.context?.velocity ?? 0.9) * 100); // TODO: refactor velocity - const duration = Math.floor(hap.duration.valueOf() * 1000 - 10); - const roundedOffset = Math.round(offset); - const midichan = (hap.value.midichan ?? 1) - 1; - const requestedport = output ?? 'IAC'; - const messagesfromjs = []; - if (note != null) { - const midiNumber = typeof note === 'number' ? note : noteToMidi(note); - - messagesfromjs.push({ - requestedport, - message: [ON_MESSAGE + midichan, midiNumber, velocity], - offset: roundedOffset, - }); - - messagesfromjs.push({ - requestedport, - message: [OFF_MESSAGE + midichan, midiNumber, velocity], - offset: roundedOffset + duration, - }); - } - - if (ccv && ccn) { - if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) { - throw new Error('expected ccv to be a number between 0 and 1'); - } - if (!['string', 'number'].includes(typeof ccn)) { - throw new Error('expected ccn to be a number or a string'); - } - const scaled = Math.round(ccv * 127); - messagesfromjs.push({ - requestedport, - message: [ON_MESSAGE + midichan, ccn, scaled], - offset: roundedOffset, - }); - - messagesfromjs.push({ - requestedport, - message: [OFF_MESSAGE + midichan, ccn, scaled], - offset: roundedOffset + duration, - }); - } - - // invoke is temporarily blocking, run in an async process - if (messagesfromjs.length) { - setTimeout(() => { - invoke('sendmidi', { messagesfromjs }); - }); - } -} diff --git a/packages/midi/webmidihandler.mjs b/packages/midi/webmidihandler.mjs new file mode 100644 index 00000000..52933190 --- /dev/null +++ b/packages/midi/webmidihandler.mjs @@ -0,0 +1,133 @@ +import * as _WebMidi from 'webmidi'; +import { isPattern, logger } from '@strudel.cycles/core'; +import { noteToMidi } from '@strudel.cycles/core'; +import { Note } from 'webmidi'; +// if you use WebMidi from outside of this package, make sure to import that instance: +export const { WebMidi } = _WebMidi; + +function supportsMidi() { + return typeof navigator.requestMIDIAccess === 'function'; +} + +function getMidiDeviceNamesString(outputs) { + return outputs.map((o) => `'${o.name}'`).join(' | '); +} + +function enableWebMidi(options = {}) { + const { onReady, onConnected, onDisconnected, onEnabled } = options; + if (WebMidi.enabled) { + return; + } + if (!supportsMidi()) { + throw new Error('Your Browser does not support WebMIDI.'); + } + WebMidi.addListener('connected', () => { + onConnected?.(WebMidi); + }); + WebMidi.addListener('enabled', () => { + onEnabled?.(WebMidi); + }); + // Reacting when a device becomes unavailable + WebMidi.addListener('disconnected', (e) => { + onDisconnected?.(WebMidi, e); + }); + return new Promise((resolve, reject) => { + if (WebMidi.enabled) { + // if already enabled, just resolve WebMidi + resolve(WebMidi); + return; + } + WebMidi.enable((err) => { + if (err) { + reject(err); + } + onReady?.(WebMidi); + resolve(WebMidi); + }); + }); +} +// const outputByName = (name: string) => WebMidi.getOutputByName(name); +const outputByName = (name) => WebMidi.getOutputByName(name); + +// output?: string | number, outputs: typeof WebMidi.outputs +function getDevice(output, outputs) { + if (!outputs.length) { + throw new Error(`🔌 No MIDI devices found. Connect a device or enable IAC Driver.`); + } + if (typeof output === 'number') { + return outputs[output]; + } + if (typeof output === 'string') { + return outputByName(output); + } + // attempt to default to first IAC device if none is specified + const IACOutput = outputs.find((output) => output.name.includes('IAC')); + const device = IACOutput ?? outputs[0]; + if (!device) { + throw new Error( + `🔌 MIDI device '${output ? output : ''}' not found. Use one of ${getMidiDeviceNamesString(WebMidi.outputs)}`, + ); + } + + return IACOutput ?? outputs[0]; +} + +export function processMidi(output) { + if (isPattern(output)) { + throw new Error( + `.midi does not accept Pattern input. Make sure to pass device name with single quotes. Example: .midi('${ + WebMidi.outputs?.[0]?.name || 'IAC Driver Bus 1' + }')`, + ); + } + + enableWebMidi({ + onEnabled: ({ outputs }) => { + const device = getDevice(output, outputs); + const otherOutputs = outputs.filter((o) => o.name !== device.name); + logger( + `Midi enabled! Using "${device.name}". ${ + otherOutputs?.length ? `Also available: ${getMidiDeviceNamesString(otherOutputs)}` : '' + }`, + ); + }, + onDisconnected: ({ outputs }) => + logger(`Midi device disconnected! Available: ${getMidiDeviceNamesString(outputs)}`), + }); + + return this.onTrigger((time, hap, currentTime, cps) => { + if (!WebMidi.enabled) { + return; + } + const device = getDevice(output, WebMidi.outputs); + hap.ensureObjectValue(); + + const offset = (time - currentTime) * 1000; + // passing a string with a +num into the webmidi api adds an offset to the current time https://webmidijs.org/api/classes/Output + const timeOffsetString = `+${offset}`; + + // destructure value + const { note, nrpnn, nrpv, ccn, ccv, midichan = 1 } = hap.value; + const velocity = hap.context?.velocity ?? 0.9; // TODO: refactor velocity + + // note off messages will often a few ms arrive late, try to prevent glitching by subtracting from the duration length + const duration = Math.floor(hap.duration.valueOf() * 1000 - 10); + if (note != null) { + const midiNumber = typeof note === 'number' ? note : noteToMidi(note); + const midiNote = new Note(midiNumber, { attack: velocity, duration }); + device.playNote(midiNote, midichan, { + time: timeOffsetString, + }); + } + if (ccv && ccn) { + if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) { + throw new Error('expected ccv to be a number between 0 and 1'); + } + if (!['string', 'number'].includes(typeof ccn)) { + throw new Error('expected ccn to be a number or a string'); + } + const scaled = Math.round(ccv * 127); + device.sendControlChange(ccn, scaled, midichan, { time: timeOffsetString }); + } + }); +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 643a4766..391c6681 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,5 +1,9 @@ lockfileVersion: '6.0' +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + importers: .: @@ -176,6 +180,15 @@ importers: specifier: ^4.3.3 version: 4.3.3(@types/node@18.16.3) + packages/desktopbridge: + dependencies: + '@strudel.cycles/core': + specifier: workspace:* + version: link:../core + '@tauri-apps/api': + specifier: ^1.4.0 + version: 1.4.0 + packages/embed: {} packages/midi: @@ -3944,6 +3957,11 @@ packages: tailwindcss: 3.3.2 dev: false + /@tauri-apps/api@1.4.0: + resolution: {integrity: sha512-Jd6HPoTM1PZSFIzq7FB8VmMu3qSSyo/3lSwLpoapW+lQ41CL5Dow2KryLg+gyazA/58DRWI9vu/XpEeHK4uMdw==} + engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'} + dev: false + /@tauri-apps/cli-darwin-arm64@1.4.0: resolution: {integrity: sha512-nA/ml0SfUt6/CYLVbHmT500Y+ijqsuv5+s9EBnVXYSLVg9kbPUZJJHluEYK+xKuOj6xzyuT/+rZFMRapmJD3jQ==} engines: {node: '>= 10'} @@ -8025,6 +8043,7 @@ packages: /iconv-lite@0.6.3: resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==} engines: {node: '>=0.10.0'} + requiresBuild: true dependencies: safer-buffer: 2.1.2 dev: true From ad230a8dc7d42b4f82763e9d62f11f6d812377a5 Mon Sep 17 00:00:00 2001 From: Jade Rowland Date: Wed, 30 Aug 2023 08:32:13 -0400 Subject: [PATCH 3/6] remove console statement --- packages/desktopbridge/midibridge.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/desktopbridge/midibridge.mjs b/packages/desktopbridge/midibridge.mjs index 16ddd606..8c38cb25 100644 --- a/packages/desktopbridge/midibridge.mjs +++ b/packages/desktopbridge/midibridge.mjs @@ -6,7 +6,6 @@ const OFF_MESSAGE = 0x80; export function processMidi(output) { return this.onTrigger((time, hap, currentTime) => { - console.log('here'); const { note, nrpnn, nrpv, ccn, ccv } = hap.value; const offset = (time - currentTime) * 1000; const velocity = Math.floor((hap.context?.velocity ?? 0.9) * 100); // TODO: refactor velocity From 282f36c47cb32bfacb547ab01c29b94e1bc1776e Mon Sep 17 00:00:00 2001 From: Jade Rowland Date: Wed, 30 Aug 2023 09:17:12 -0400 Subject: [PATCH 4/6] fix lock file --- pnpm-lock.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 391c6681..65232669 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,9 +1,5 @@ lockfileVersion: '6.0' -settings: - autoInstallPeers: true - excludeLinksFromLockfile: false - importers: .: From 328af5f560d2d3e0292c7d6510cc3bceeaed1130 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 31 Aug 2023 05:10:17 +0200 Subject: [PATCH 5/6] fix: move desktopbridge dependency to website --- packages/desktopbridge/midibridge.mjs | 6 +- packages/midi/midi.mjs | 136 +++++++++++++++++++++++++- packages/midi/webmidihandler.mjs | 133 ------------------------- pnpm-lock.yaml | 6 ++ website/package.json | 2 + website/src/repl/Repl.jsx | 3 +- website/src/tauri.mjs | 4 + 7 files changed, 149 insertions(+), 141 deletions(-) delete mode 100644 packages/midi/webmidihandler.mjs create mode 100644 website/src/tauri.mjs diff --git a/packages/desktopbridge/midibridge.mjs b/packages/desktopbridge/midibridge.mjs index 8c38cb25..7e9528e7 100644 --- a/packages/desktopbridge/midibridge.mjs +++ b/packages/desktopbridge/midibridge.mjs @@ -1,10 +1,10 @@ import { Invoke } from './utils.mjs'; -import { noteToMidi } from '@strudel.cycles/core'; +import { Pattern, noteToMidi } from '@strudel.cycles/core'; const ON_MESSAGE = 0x90; const OFF_MESSAGE = 0x80; -export function processMidi(output) { +Pattern.prototype.midi = function (output) { return this.onTrigger((time, hap, currentTime) => { const { note, nrpnn, nrpv, ccn, ccv } = hap.value; const offset = (time - currentTime) * 1000; @@ -53,4 +53,4 @@ export function processMidi(output) { }); } }); -} +}; diff --git a/packages/midi/midi.mjs b/packages/midi/midi.mjs index 5606539a..201840cc 100644 --- a/packages/midi/midi.mjs +++ b/packages/midi/midi.mjs @@ -5,8 +5,136 @@ This program is free software: you can redistribute it and/or modify it under th */ import { Pattern } from '@strudel.cycles/core'; -import * as webmidihandler from './webmidihandler.mjs'; -import * as midibridge from '../desktopbridge/midibridge.mjs'; -import { isTauri } from '../desktopbridge/utils.mjs'; +import * as _WebMidi from 'webmidi'; +import { isPattern, logger } from '@strudel.cycles/core'; +import { noteToMidi } from '@strudel.cycles/core'; +import { Note } from 'webmidi'; +// if you use WebMidi from outside of this package, make sure to import that instance: +export const { WebMidi } = _WebMidi; -Pattern.prototype.midi = isTauri() ? midibridge.processMidi : webmidihandler.processMidi; +function supportsMidi() { + return typeof navigator.requestMIDIAccess === 'function'; +} + +function getMidiDeviceNamesString(outputs) { + return outputs.map((o) => `'${o.name}'`).join(' | '); +} + +function enableWebMidi(options = {}) { + const { onReady, onConnected, onDisconnected, onEnabled } = options; + if (WebMidi.enabled) { + return; + } + if (!supportsMidi()) { + throw new Error('Your Browser does not support WebMIDI.'); + } + WebMidi.addListener('connected', () => { + onConnected?.(WebMidi); + }); + WebMidi.addListener('enabled', () => { + onEnabled?.(WebMidi); + }); + // Reacting when a device becomes unavailable + WebMidi.addListener('disconnected', (e) => { + onDisconnected?.(WebMidi, e); + }); + return new Promise((resolve, reject) => { + if (WebMidi.enabled) { + // if already enabled, just resolve WebMidi + resolve(WebMidi); + return; + } + WebMidi.enable((err) => { + if (err) { + reject(err); + } + onReady?.(WebMidi); + resolve(WebMidi); + }); + }); +} +// const outputByName = (name: string) => WebMidi.getOutputByName(name); +const outputByName = (name) => WebMidi.getOutputByName(name); + +// output?: string | number, outputs: typeof WebMidi.outputs +function getDevice(output, outputs) { + if (!outputs.length) { + throw new Error(`🔌 No MIDI devices found. Connect a device or enable IAC Driver.`); + } + if (typeof output === 'number') { + return outputs[output]; + } + if (typeof output === 'string') { + return outputByName(output); + } + // attempt to default to first IAC device if none is specified + const IACOutput = outputs.find((output) => output.name.includes('IAC')); + const device = IACOutput ?? outputs[0]; + if (!device) { + throw new Error( + `🔌 MIDI device '${output ? output : ''}' not found. Use one of ${getMidiDeviceNamesString(WebMidi.outputs)}`, + ); + } + + return IACOutput ?? outputs[0]; +} + +Pattern.prototype.midi = function (output) { + if (isPattern(output)) { + throw new Error( + `.midi does not accept Pattern input. Make sure to pass device name with single quotes. Example: .midi('${ + WebMidi.outputs?.[0]?.name || 'IAC Driver Bus 1' + }')`, + ); + } + + enableWebMidi({ + onEnabled: ({ outputs }) => { + const device = getDevice(output, outputs); + const otherOutputs = outputs.filter((o) => o.name !== device.name); + logger( + `Midi enabled! Using "${device.name}". ${ + otherOutputs?.length ? `Also available: ${getMidiDeviceNamesString(otherOutputs)}` : '' + }`, + ); + }, + onDisconnected: ({ outputs }) => + logger(`Midi device disconnected! Available: ${getMidiDeviceNamesString(outputs)}`), + }); + + return this.onTrigger((time, hap, currentTime, cps) => { + if (!WebMidi.enabled) { + return; + } + const device = getDevice(output, WebMidi.outputs); + hap.ensureObjectValue(); + + const offset = (time - currentTime) * 1000; + // passing a string with a +num into the webmidi api adds an offset to the current time https://webmidijs.org/api/classes/Output + const timeOffsetString = `+${offset}`; + + // destructure value + const { note, nrpnn, nrpv, ccn, ccv, midichan = 1 } = hap.value; + const velocity = hap.context?.velocity ?? 0.9; // TODO: refactor velocity + + // note off messages will often a few ms arrive late, try to prevent glitching by subtracting from the duration length + const duration = Math.floor(hap.duration.valueOf() * 1000 - 10); + if (note != null) { + const midiNumber = typeof note === 'number' ? note : noteToMidi(note); + const midiNote = new Note(midiNumber, { attack: velocity, duration }); + device.playNote(midiNote, midichan, { + time: timeOffsetString, + }); + } + if (ccv && ccn) { + if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) { + throw new Error('expected ccv to be a number between 0 and 1'); + } + if (!['string', 'number'].includes(typeof ccn)) { + throw new Error('expected ccn to be a number or a string'); + } + const scaled = Math.round(ccv * 127); + device.sendControlChange(ccn, scaled, midichan, { time: timeOffsetString }); + } + }); +}; diff --git a/packages/midi/webmidihandler.mjs b/packages/midi/webmidihandler.mjs deleted file mode 100644 index 52933190..00000000 --- a/packages/midi/webmidihandler.mjs +++ /dev/null @@ -1,133 +0,0 @@ -import * as _WebMidi from 'webmidi'; -import { isPattern, logger } from '@strudel.cycles/core'; -import { noteToMidi } from '@strudel.cycles/core'; -import { Note } from 'webmidi'; -// if you use WebMidi from outside of this package, make sure to import that instance: -export const { WebMidi } = _WebMidi; - -function supportsMidi() { - return typeof navigator.requestMIDIAccess === 'function'; -} - -function getMidiDeviceNamesString(outputs) { - return outputs.map((o) => `'${o.name}'`).join(' | '); -} - -function enableWebMidi(options = {}) { - const { onReady, onConnected, onDisconnected, onEnabled } = options; - if (WebMidi.enabled) { - return; - } - if (!supportsMidi()) { - throw new Error('Your Browser does not support WebMIDI.'); - } - WebMidi.addListener('connected', () => { - onConnected?.(WebMidi); - }); - WebMidi.addListener('enabled', () => { - onEnabled?.(WebMidi); - }); - // Reacting when a device becomes unavailable - WebMidi.addListener('disconnected', (e) => { - onDisconnected?.(WebMidi, e); - }); - return new Promise((resolve, reject) => { - if (WebMidi.enabled) { - // if already enabled, just resolve WebMidi - resolve(WebMidi); - return; - } - WebMidi.enable((err) => { - if (err) { - reject(err); - } - onReady?.(WebMidi); - resolve(WebMidi); - }); - }); -} -// const outputByName = (name: string) => WebMidi.getOutputByName(name); -const outputByName = (name) => WebMidi.getOutputByName(name); - -// output?: string | number, outputs: typeof WebMidi.outputs -function getDevice(output, outputs) { - if (!outputs.length) { - throw new Error(`🔌 No MIDI devices found. Connect a device or enable IAC Driver.`); - } - if (typeof output === 'number') { - return outputs[output]; - } - if (typeof output === 'string') { - return outputByName(output); - } - // attempt to default to first IAC device if none is specified - const IACOutput = outputs.find((output) => output.name.includes('IAC')); - const device = IACOutput ?? outputs[0]; - if (!device) { - throw new Error( - `🔌 MIDI device '${output ? output : ''}' not found. Use one of ${getMidiDeviceNamesString(WebMidi.outputs)}`, - ); - } - - return IACOutput ?? outputs[0]; -} - -export function processMidi(output) { - if (isPattern(output)) { - throw new Error( - `.midi does not accept Pattern input. Make sure to pass device name with single quotes. Example: .midi('${ - WebMidi.outputs?.[0]?.name || 'IAC Driver Bus 1' - }')`, - ); - } - - enableWebMidi({ - onEnabled: ({ outputs }) => { - const device = getDevice(output, outputs); - const otherOutputs = outputs.filter((o) => o.name !== device.name); - logger( - `Midi enabled! Using "${device.name}". ${ - otherOutputs?.length ? `Also available: ${getMidiDeviceNamesString(otherOutputs)}` : '' - }`, - ); - }, - onDisconnected: ({ outputs }) => - logger(`Midi device disconnected! Available: ${getMidiDeviceNamesString(outputs)}`), - }); - - return this.onTrigger((time, hap, currentTime, cps) => { - if (!WebMidi.enabled) { - return; - } - const device = getDevice(output, WebMidi.outputs); - hap.ensureObjectValue(); - - const offset = (time - currentTime) * 1000; - // passing a string with a +num into the webmidi api adds an offset to the current time https://webmidijs.org/api/classes/Output - const timeOffsetString = `+${offset}`; - - // destructure value - const { note, nrpnn, nrpv, ccn, ccv, midichan = 1 } = hap.value; - const velocity = hap.context?.velocity ?? 0.9; // TODO: refactor velocity - - // note off messages will often a few ms arrive late, try to prevent glitching by subtracting from the duration length - const duration = Math.floor(hap.duration.valueOf() * 1000 - 10); - if (note != null) { - const midiNumber = typeof note === 'number' ? note : noteToMidi(note); - const midiNote = new Note(midiNumber, { attack: velocity, duration }); - device.playNote(midiNote, midichan, { - time: timeOffsetString, - }); - } - if (ccv && ccn) { - if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) { - throw new Error('expected ccv to be a number between 0 and 1'); - } - if (!['string', 'number'].includes(typeof ccn)) { - throw new Error('expected ccn to be a number or a string'); - } - const scaled = Math.round(ccv * 127); - device.sendControlChange(ccn, scaled, midichan, { time: timeOffsetString }); - } - }); -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 65232669..8b48a93f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -588,6 +588,9 @@ importers: '@strudel.cycles/xen': specifier: workspace:* version: link:../packages/xen + '@strudel/desktopbridge': + specifier: workspace:* + version: link:../packages/desktopbridge '@supabase/supabase-js': specifier: ^2.21.0 version: 2.21.0 @@ -597,6 +600,9 @@ importers: '@tailwindcss/typography': specifier: ^0.5.8 version: 0.5.9(tailwindcss@3.3.2) + '@tauri-apps/api': + specifier: ^1.4.0 + version: 1.4.0 '@types/node': specifier: ^18.16.3 version: 18.16.3 diff --git a/website/package.json b/website/package.json index c595f5a6..6ac799d3 100644 --- a/website/package.json +++ b/website/package.json @@ -34,9 +34,11 @@ "@strudel.cycles/transpiler": "workspace:*", "@strudel.cycles/webaudio": "workspace:*", "@strudel.cycles/xen": "workspace:*", + "@strudel/desktopbridge": "workspace:*", "@supabase/supabase-js": "^2.21.0", "@tailwindcss/forms": "^0.5.3", "@tailwindcss/typography": "^0.5.8", + "@tauri-apps/api": "^1.4.0", "@types/node": "^18.16.3", "@types/react": "^18.2.0", "@types/react-dom": "^18.2.1", diff --git a/website/src/repl/Repl.jsx b/website/src/repl/Repl.jsx index 8dafe8a4..4434ed7a 100644 --- a/website/src/repl/Repl.jsx +++ b/website/src/repl/Repl.jsx @@ -21,6 +21,7 @@ import { settingsMap, useSettings, setLatestCode } from '../settings.mjs'; import Loader from './Loader'; import { settingPatterns } from '../settings.mjs'; import { code2hash, hash2code } from './helpers.mjs'; +import { isTauri } from '../tauri.mjs'; const { latestCode } = settingsMap.get(); @@ -36,7 +37,7 @@ const modules = [ import('@strudel.cycles/core'), import('@strudel.cycles/tonal'), import('@strudel.cycles/mini'), - import('@strudel.cycles/midi'), + isTauri() ? import('@strudel/desktopbridge') : import('@strudel.cycles/midi'), import('@strudel.cycles/xen'), import('@strudel.cycles/webaudio'), import('@strudel.cycles/osc'), diff --git a/website/src/tauri.mjs b/website/src/tauri.mjs new file mode 100644 index 00000000..c4c69af5 --- /dev/null +++ b/website/src/tauri.mjs @@ -0,0 +1,4 @@ +import { invoke } from '@tauri-apps/api/tauri'; + +export const Invoke = invoke; +export const isTauri = () => window.__TAURI_IPC__ != null; From 749b11a5ea61145bfd322cec964653d582bf3970 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 31 Aug 2023 05:12:33 +0200 Subject: [PATCH 6/6] fix: history --- packages/midi/midi.mjs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/midi/midi.mjs b/packages/midi/midi.mjs index 201840cc..98509b46 100644 --- a/packages/midi/midi.mjs +++ b/packages/midi/midi.mjs @@ -4,9 +4,8 @@ Copyright (C) 2022 Strudel contributors - see . */ -import { Pattern } from '@strudel.cycles/core'; import * as _WebMidi from 'webmidi'; -import { isPattern, logger } from '@strudel.cycles/core'; +import { Pattern, isPattern, logger } from '@strudel.cycles/core'; import { noteToMidi } from '@strudel.cycles/core'; import { Note } from 'webmidi'; // if you use WebMidi from outside of this package, make sure to import that instance: @@ -20,7 +19,7 @@ function getMidiDeviceNamesString(outputs) { return outputs.map((o) => `'${o.name}'`).join(' | '); } -function enableWebMidi(options = {}) { +export function enableWebMidi(options = {}) { const { onReady, onConnected, onDisconnected, onEnabled } = options; if (WebMidi.enabled) { return;