From a4a2f645b13e4749c02324339b707d8381904e39 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 25 Jun 2024 22:50:30 +0300 Subject: [PATCH] lv2 plugin plays! --- src/device/plugin.rs | 100 ++++++++++++++++++++++++-------------- src/device/plugin/lv2.rs | 20 ++++++-- src/device/plugin/vst2.rs | 4 +- src/device/sequencer.rs | 89 +++++++++++++++++---------------- 4 files changed, 125 insertions(+), 88 deletions(-) diff --git a/src/device/plugin.rs b/src/device/plugin.rs index 6a5c40df..d96965d9 100644 --- a/src/device/plugin.rs +++ b/src/device/plugin.rs @@ -6,6 +6,8 @@ mod vst3; pub struct Plugin { name: String, + input: ::jack::Port<::jack::MidiIn>, + outputs: [::jack::Port<::jack::AudioOut>;2], path: String, plugin: Option, offset: usize, @@ -14,34 +16,81 @@ pub struct Plugin { } enum PluginKind { - VST2(::vst::host::PluginInstance), + LV2 { + world: ::livi::World, + features: Arc<::livi::Features>, + input: ::livi::event::LV2AtomSequence, + portList: Vec<::livi::Port>, + instance: ::livi::Instance, + outputs: [Vec;2], + }, + VST2 { + instance: ::vst::host::PluginInstance + }, VST3, - LV2(Vec<::livi::Port>, ::livi::Instance), } const HELM: &'static str = "file:///nix/store/ij3sz7nqg5l7v2dygdvzy3w6cj62bd6r-helm-0.9.0/lib/lv2/helm.lv2"; impl Plugin { pub fn new (name: &str) -> Result, Box> { - let device = DynamicDevice::new(render, handle, process, Self { + let (client, _status) = Client::new(name, ClientOptions::NO_START_SERVER)?; + DynamicDevice::new(render, handle, Self::process, Self { name: name.into(), + input: client.register_port("in", MidiIn::default())?, + outputs: [ + client.register_port("outL", AudioOut::default())?, + client.register_port("outR", AudioOut::default())?, + ], path: HELM.into(), - plugin: None, + plugin: Some(self::lv2::plug_in(HELM)?), offset: 0, selected: 0, midi_map_enable: false - }); - device.state.lock().unwrap().plugin = Some(self::lv2::plug_in(HELM)?); - Ok(device) + }).activate(client) + } + + pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control { + match self.plugin.as_mut() { + Some(PluginKind::LV2 { + features, input, ref mut instance, .. + }) => { + let mut input = ::livi::event::LV2AtomSequence::new( + &features, scope.n_frames() as usize + ); + let urid = features.midi_urid(); + for event in self.input.iter(scope) { + match event.bytes.len() { + 3 => input.push_midi_event::<3>( + event.time as i64, + urid, + &event.bytes[0..3] + ).unwrap(), + _ => {} + } + } + let ports = ::livi::EmptyPortConnections::new() + .with_atom_sequence_inputs( + std::iter::once(&input) + ) + .with_audio_outputs( + self.outputs.iter_mut().map(|o|o.as_mut_slice(scope)), + ); + unsafe { + instance.run( + scope.n_frames() as usize, + ports + ).unwrap() + }; + }, + _ => {} + } + Control::Continue } } impl ::vst::host::Host for Plugin {} -pub fn process (_: &mut Plugin, _: &Client, _: &ProcessScope) -> Control { - Control::Continue -} - pub fn render (state: &Plugin, buf: &mut Buffer, area: Rect) -> Usually { @@ -50,43 +99,20 @@ pub fn render (state: &Plugin, buf: &mut Buffer, area: Rect) let style = Style::default().gray(); let mut width = 40u16; match &state.plugin { - Some(PluginKind::LV2(ports, instance)) => { + Some(PluginKind::LV2 { portList, instance, .. }) => { for i in 0..height - 4 { - let port = &ports[i as usize]; + let port = &portList[i as usize]; let label = &format!("C·· M·· {:25} = {:03}", port.name, port.default_value); width = width.max(label.len() as u16); label.blit(buf, x + 2, y + 3 + i as u16, None); } draw_box(buf, Rect { x, y, width, height }); - - //let mut height = 3; - //for (i, port) in ports - //.iter() - //.filter(|port|port.port_type == ::livi::PortType::ControlInput) - //.skip(state.offset) - //.enumerate() - //{ - //if i >= 20 { - //break - //} - //buf.set_string(x + 2, y + 3 + i as u16, &format!("C·· M·· {:25} = {:03}", - //port.name, - //port.default_value - //), if i + state.offset == state.selected { - //Style::default().white().bold() - //} else { - //Style::default().not_dim() - //}); - //height = height + 1; - //} - //Ok(draw_box(buf, rect { x, y, width, height })) }, _ => { buf.set_string(x + 1, y + 3, &format!(" Parameter 1 0.0"), style); buf.set_string(x + 1, y + 4, &format!(" Parameter 2 0.0"), style); buf.set_string(x + 1, y + 5, &format!(" Parameter 3 0.0"), style); buf.set_string(x + 1, y + 6, &format!(" Parameter 4 0.0"), style); - //Ok(draw_box(buf, Rect { x, y, width: 40, height: 7 })) } }; draw_header(state, buf, area.x, area.y, width); @@ -135,6 +161,6 @@ impl DevicePorts for Plugin { Ok(vec!["in".into()]) } fn audio_outs (&self) -> Usually> { - Ok(vec!["out L".into(), "out R".into()]) + Ok(vec!["outL".into(), "outR".into()]) } } diff --git a/src/device/plugin/lv2.rs b/src/device/plugin/lv2.rs index 413334e3..1f229acf 100644 --- a/src/device/plugin/lv2.rs +++ b/src/device/plugin/lv2.rs @@ -13,11 +13,21 @@ pub fn plug_in (uri: &str) -> Usually { break } let plugin = plugin.unwrap(); - let mut ports = vec![]; + let mut portList = vec![]; for port in plugin.ports() { - ports.push(port); + portList.push(port); } - let instance = unsafe { plugin.instantiate(features.clone(), 48000.0).expect("boop") }; - let result = PluginKind::LV2(ports, instance); - Ok(result) + Ok(PluginKind::LV2 { + input: ::livi::event::LV2AtomSequence::new(&features, 1024), + outputs: [ + vec![0.0; features.max_block_length()], + vec![0.0; features.max_block_length()], + ], + instance: unsafe { + plugin.instantiate(features.clone(), 48000.0).expect("boop") + }, + portList, + features, + world, + }) } diff --git a/src/device/plugin/vst2.rs b/src/device/plugin/vst2.rs index ea21520b..b9e71c4c 100644 --- a/src/device/plugin/vst2.rs +++ b/src/device/plugin/vst2.rs @@ -6,6 +6,8 @@ fn set_vst_plugin (host: &Arc>, path: &str) -> Usually &std::path::Path::new("/nix/store/ij3sz7nqg5l7v2dygdvzy3w6cj62bd6r-helm-0.9.0/lib/lxvst/helm.so"), host.clone() )?; - Ok(PluginKind::VST2(loader.instance()?)) + Ok(PluginKind::VST2 { + instance: loader.instance()? + }) } //"file:///nix/store/ij3sz7nqg5l7v2dygdvzy3w6cj62bd6r-helm-0.9.0/lib/lxvst/helm.so" diff --git a/src/device/sequencer.rs b/src/device/sequencer.rs index 32a1433a..db9ff714 100644 --- a/src/device/sequencer.rs +++ b/src/device/sequencer.rs @@ -222,6 +222,15 @@ impl Sequencer { } } +impl DevicePorts for Sequencer { + fn midi_ins (&self) -> Usually> { + Ok(vec!["in".into()]) + } + fn midi_outs (&self) -> Usually> { + Ok(vec!["out".into()]) + } +} + fn render (s: &Sequencer, buf: &mut Buffer, mut area: Rect) -> Usually { let Rect { x, y, width, .. } = area; let (time0, time1) = s.time_axis; @@ -266,59 +275,62 @@ fn draw_header (s: &Sequencer, buf: &mut Buffer, area: Rect, beat: usize) -> Usu let Rect { x, y, width, .. } = area; let style = Style::default().gray(); let timer = format!("{rep}.{step:02} / {reps}.{steps}"); - buf.set_string(x + width - 2 - timer.len() as u16, y + 1, &timer, style.bold().not_dim()); - buf.set_string(x + 2, y + 1, &match s.playing { - TransportState::Rolling => format!("▶ PLAYING"), - TransportState::Starting => format!("READY ..."), - TransportState::Stopped => format!("⏹ STOPPED") - }, match s.playing { + timer.blit(buf, x + width - 2 - timer.len() as u16, y + 1, Some(style.bold().not_dim())); + match s.playing { + TransportState::Rolling => "▶ PLAYING", + TransportState::Starting => "READY ...", + TransportState::Stopped => "⏹ STOPPED", + }.blit(buf, x + 2, y + 1, Some(match s.playing { TransportState::Stopped => style.dim().bold(), TransportState::Starting => style.not_dim().bold(), TransportState::Rolling => style.not_dim().white().bold() - }); - buf.set_string(x, y + 2, format!("├{}┤", "-".repeat((area.width - 2).into())), style.dim()); - //buf.set_string(x + 2, y + 2, - //&format!("▶ PLAY"), if s.playing { - //Style::default().green() - //} else { - //Style::default().dim() - //}); - buf.set_string(x + 13, y + 1, &format!("⏺ REC"), if s.recording { - Style::default().bold().red() - } else { - Style::default().bold().dim() - }); - buf.set_string(x + 20, y + 1, &format!("⏺ DUB"), if s.overdub { - Style::default().bold().yellow() - } else { - Style::default().bold().dim() - }); - buf.set_string(x + 27, y + 1, &format!("⏺ MON"), if s.monitoring { - Style::default().bold().green() - } else { - Style::default().bold().dim() - }); + })); + let separator = format!("├{}┤", "-".repeat((area.width - 2).into())); + separator.blit(buf, x, y + 2, Some(style.dim())); + draw_rec(buf, x + 13, y + 1, s.recording); + draw_dub(buf, x + 20, y + 1, s.overdub); + draw_mon(buf, x + 27, y + 1, s.monitoring); let clips = draw_clips(s, buf, area)?; Ok(Rect { x, y, width: area.width, height: 3 }) } +fn draw_rec (buf: &mut Buffer, x: u16, y: u16, on: bool) { + "⏺ REC".blit(buf, x, y, Some(if on { + Style::default().bold().red() + } else { + Style::default().bold().dim() + })) +} +fn draw_dub (buf: &mut Buffer, x: u16, y: u16, on: bool) { + "⏺ DUB".blit(buf, x + 20, y + 1, Some(if on { + Style::default().bold().yellow() + } else { + Style::default().bold().dim() + })) +} +fn draw_mon (buf: &mut Buffer, x: u16, y: u16, on: bool) { + "⏺ MON".blit(buf, x + 27, y + 1, Some(if on { + Style::default().bold().green() + } else { + Style::default().bold().dim() + })) +} fn draw_clips (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually { let Rect { x, y, .. } = area; let style = Style::default().gray(); for (i, sequence) in s.sequences.iter().enumerate() { let label = format!("▶ {}", &sequence.name); - buf.set_string(x + 2, y + 3 + (i as u16)*2, &label, if i == s.sequence { + label.blit(buf, x + 2, y + 3 + (i as u16)*2, Some(if i == s.sequence { match s.playing { TransportState::Rolling => style.white().bold(), _ => style.not_dim().bold() } } else { style.dim() - }); + })); } Ok(Rect { x, y, width: 14, height: 14 }) } - pub fn contains_note_on (sequence: &Sequence, k: ::midly::num::u7, start: u32, end: u32) -> bool { for (_, (_, events)) in sequence.notes.range(start..end).enumerate() { for event in events.iter() { @@ -334,11 +346,9 @@ pub fn contains_note_on (sequence: &Sequence, k: ::midly::num::u7, start: u32, e } return false } - pub fn handle (s: &mut Sequencer, event: &AppEvent) -> Usually { handle_keymap(s, event, KEYMAP) } - pub const KEYMAP: &'static [KeyBinding] = keymap!(Sequencer { [Up, NONE, "cursor_up", "move cursor up", cursor_up], [Down, NONE, "cursor_down", "move cursor down", cursor_down], @@ -373,14 +383,12 @@ pub const KEYMAP: &'static [KeyBinding] = keymap!(Sequencer { [Char('7'), NONE, "seq_7", "Sequence 7", focus_seq(6)], [Char('8'), NONE, "seq_8", "Sequence 8", focus_seq(7)], }); - const fn focus_seq (i: usize) -> impl Fn(&mut Sequencer)->Usually { move |s: &mut Sequencer| { s.sequence = i; Ok(true) } } - fn nop (_: &mut Sequencer) -> Usually { Ok(false) } @@ -561,12 +569,3 @@ fn quantize_prev (s: &mut Sequencer) -> Usually { Ok(()) } } - -impl DevicePorts for Sequencer { - fn midi_ins (&self) -> Usually> { - Ok(vec!["in".into()]) - } - fn midi_outs (&self) -> Usually> { - Ok(vec!["out".into()]) - } -}