From c78b2dc9dea6e75dc33c4b7937f02b585c57030e Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 10 May 2025 18:47:58 +0300 Subject: [PATCH] device: add DeviceAudio dispatcher --- crates/app/src/audio.rs | 30 ++++++++++++++------------ crates/device/src/device.rs | 43 +++++++++++++++++++++++++++++++++++++ crates/device/src/lib.rs | 23 +++----------------- 3 files changed, 62 insertions(+), 34 deletions(-) create mode 100644 crates/device/src/device.rs diff --git a/crates/app/src/audio.rs b/crates/app/src/audio.rs index 5bdf1ffc..b6db6a44 100644 --- a/crates/app/src/audio.rs +++ b/crates/app/src/audio.rs @@ -2,26 +2,20 @@ use crate::*; impl HasJack for App { fn jack (&self) -> &Jack { &self.jack } } audio!( |self: App, client, scope|{ + // Start profiling cycle let t0 = self.perf.get_t0(); + // Update transport clock self.clock().update_from_scope(scope).unwrap(); - // Collect MIDI input (TODO preallocate) + + // Collect MIDI input (TODO preallocate large buffers) let midi_in = self.midi_ins.iter() .map(|port|port.port().iter(scope) .map(|RawMidi { time, bytes }|(time, LiveEvent::parse(bytes))) .collect::>()) .collect::>(); - // Update standalone MIDI sequencer - //if let Some(player) = self.player.as_mut() { - //if Control::Quit == PlayerAudio( - //player, - //&mut self.note_buf, - //&mut self.midi_buf, - //).process(client, scope) { - //return Control::Quit - //} - //} + // Update standalone sampler //if let Some(sampler) = self.sampler.as_mut() { //if Control::Quit == SamplerAudio(sampler).process(client, scope) { @@ -35,6 +29,7 @@ audio!( //} //} //} + // TODO move these to editor and sampler?: //for port in midi_in.iter() { //for event in port.iter() { @@ -58,14 +53,21 @@ audio!( //} //} //} - // Update track sequencers + + // Update track sequencers and devices for track in self.tracks.iter_mut() { - if PlayerAudio( + if Control::Quit == PlayerAudio( track.player_mut(), &mut self.note_buf, &mut self.midi_buf - ).process(client, scope) == Control::Quit { + ).process(client, scope) { return Control::Quit } + for device in track.devices.iter_mut() { + if Control::Quit == DeviceAudio(device).process(client, scope) { + return Control::Quit + } + } } + // End profiling cycle self.perf.update_from_jack_scope(t0, scope); Control::Continue diff --git a/crates/device/src/device.rs b/crates/device/src/device.rs new file mode 100644 index 00000000..74d76488 --- /dev/null +++ b/crates/device/src/device.rs @@ -0,0 +1,43 @@ +use crate::*; + +#[derive(Debug)] +pub enum Device { + #[cfg(feature = "sequencer")] Sequencer(MidiPlayer), + #[cfg(feature = "sampler")] Sampler(Sampler), + #[cfg(feature = "lv2")] Lv2(Lv2), // TODO + #[cfg(feature = "vst2")] Vst2, // TODO + #[cfg(feature = "vst3")] Vst3, // TODO + #[cfg(feature = "clap")] Clap, // TODO + #[cfg(feature = "sf2")] Sf2, // TODO +} + +impl Device { + pub fn name (&self) -> &str { + match self { + Self::Sampler(sampler) => sampler.name.as_ref(), + _ => todo!(), + } + } +} + +pub struct DeviceAudio<'a>(pub &'a mut Device); + +audio!(|self: DeviceAudio<'a>, client, scope|{ + use Device::*; + match self.0 { + #[cfg(feature = "sequencer")] Sequencer(sequencer) => + { Control::Continue /* TODO */ }, + #[cfg(feature = "sampler")] Sampler(sampler) => + SamplerAudio(sampler).process(client, scope), + #[cfg(feature = "lv2")] Lv2(lv2) => + { todo!() }, // TODO + #[cfg(feature = "vst2")] Vst2 => + { todo!() }, // TODO + #[cfg(feature = "vst3")] Vst3 => + { todo!() }, // TODO + #[cfg(feature = "clap")] Clap => + { todo!() }, // TODO + #[cfg(feature = "sf2")] Sf2 => + { todo!() }, // TODO + } +}); diff --git a/crates/device/src/lib.rs b/crates/device/src/lib.rs index 64d99c31..16f34bf2 100644 --- a/crates/device/src/lib.rs +++ b/crates/device/src/lib.rs @@ -15,6 +15,9 @@ pub(crate) use ::tek_engine::midi::{u7, LiveEvent, MidiMessage}; pub(crate) use ::tek_engine::jack::{Control, ProcessScope, MidiWriter, RawMidi}; pub(crate) use ratatui::{prelude::Rect, widgets::{Widget, canvas::{Canvas, Line}}}; +mod device; +pub use self::device::*; + #[cfg(feature = "clock")] mod clock; #[cfg(feature = "clock")] pub use self::clock::*; @@ -38,23 +41,3 @@ pub(crate) use ratatui::{prelude::Rect, widgets::{Widget, canvas::{Canvas, Line} #[cfg(feature = "clap")] mod clap; #[cfg(feature = "clap")] pub use self::clap::*; - -#[derive(Debug)] -pub enum Device { - #[cfg(feature = "sequencer")] Sequencer(MidiPlayer), - #[cfg(feature = "sampler")] Sampler(Sampler), - #[cfg(feature = "lv2")] Lv2(Lv2), // TODO - #[cfg(feature = "vst2")] Vst2, // TODO - #[cfg(feature = "vst3")] Vst3, // TODO - #[cfg(feature = "clap")] Clap, // TODO - #[cfg(feature = "sf2")] Sf2, // TODO -} - -impl Device { - pub fn name (&self) -> &str { - match self { - Self::Sampler(sampler) => sampler.name.as_ref(), - _ => todo!(), - } - } -}