use crate::*; pub mod lv2; pub(crate) use lv2::*; pub use self::lv2::LV2Plugin; /// A plugin device. #[derive(Debug)] pub struct Plugin { /// JACK client handle (needs to not be dropped for standalone mode to work). pub jack: Arc>, pub name: String, pub path: Option, pub plugin: Option, pub selected: usize, pub mapping: bool, pub midi_ins: Vec>, pub midi_outs: Vec>, pub audio_ins: Vec>, pub audio_outs: Vec>, } /// Supported plugin formats. #[derive(Default)] pub enum PluginKind { #[default] None, LV2(LV2Plugin), VST2 { instance: () /*::vst::host::PluginInstance*/ }, VST3, } impl Debug for PluginKind { fn fmt (&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { write!(f, "{}", match self { Self::None => "(none)", Self::LV2(_) => "LV2", Self::VST2{..} => "VST2", Self::VST3 => "VST3", }) } } impl Plugin { pub fn new_lv2 ( jack: &Arc>, name: &str, path: &str, ) -> Usually { Ok(Self { jack: jack.clone(), name: name.into(), path: Some(String::from(path)), plugin: Some(PluginKind::LV2(LV2Plugin::new(path)?)), selected: 0, mapping: false, midi_ins: vec![], midi_outs: vec![], audio_ins: vec![], audio_outs: vec![], }) } } pub struct PluginAudio(Arc>); from!(|model: &Arc>| PluginAudio = Self(model.clone())); audio!(|self: PluginAudio, client, scope|{ let state = &mut*self.0.write().unwrap(); match state.plugin.as_mut() { Some(PluginKind::LV2(LV2Plugin { features, ref mut instance, ref mut input_buffer, .. })) => { let urid = features.midi_urid(); input_buffer.clear(); for port in state.midi_ins.iter() { let mut atom = ::livi::event::LV2AtomSequence::new( &features, scope.n_frames() as usize ); for event in port.iter(scope) { match event.bytes.len() { 3 => atom.push_midi_event::<3>( event.time as i64, urid, &event.bytes[0..3] ).unwrap(), _ => {} } } input_buffer.push(atom); } let mut outputs = vec![]; for _ in state.midi_outs.iter() { outputs.push(::livi::event::LV2AtomSequence::new( features, scope.n_frames() as usize )); } let ports = ::livi::EmptyPortConnections::new() .with_atom_sequence_inputs(input_buffer.iter()) .with_atom_sequence_outputs(outputs.iter_mut()) .with_audio_inputs(state.audio_ins.iter().map(|o|o.as_slice(scope))) .with_audio_outputs(state.audio_outs.iter_mut().map(|o|o.as_mut_slice(scope))); unsafe { instance.run(scope.n_frames() as usize, ports).unwrap() }; }, _ => todo!("only lv2 is supported") } Control::Continue }); //fn jack_from_lv2 (name: &str, plugin: &::livi::Plugin) -> Usually { //let counts = plugin.port_counts(); //let mut jack = Jack::new(name)?; //for i in 0..counts.atom_sequence_inputs { //jack = jack.midi_in(&format!("midi-in-{i}")) //} //for i in 0..counts.atom_sequence_outputs { //jack = jack.midi_out(&format!("midi-out-{i}")); //} //for i in 0..counts.audio_inputs { //jack = jack.audio_in(&format!("audio-in-{i}")); //} //for i in 0..counts.audio_outputs { //jack = jack.audio_out(&format!("audio-out-{i}")); //} //Ok(jack) //} impl Plugin { /// Create a plugin host device. pub fn new ( jack: &Arc>, name: &str, ) -> Usually { Ok(Self { //_engine: Default::default(), jack: jack.clone(), name: name.into(), path: None, plugin: None, selected: 0, mapping: false, audio_ins: vec![], audio_outs: vec![], midi_ins: vec![], midi_outs: vec![], //ports: JackPorts::default() }) } } impl Content for Plugin { fn render (&self, to: &mut TuiOutput) { let area = to.area(); let [x, y, _, height] = area; let mut width = 20u16; match &self.plugin { Some(PluginKind::LV2(LV2Plugin { port_list, instance, .. })) => { let start = self.selected.saturating_sub((height as usize / 2).saturating_sub(1)); let end = start + height as usize - 2; //draw_box(buf, Rect { x, y, width, height }); for i in start..end { if let Some(port) = port_list.get(i) { let value = if let Some(value) = instance.control_input(port.index) { value } else { port.default_value }; //let label = &format!("C·· M·· {:25} = {value:.03}", port.name); let label = &format!("{:25} = {value:.03}", port.name); width = width.max(label.len() as u16 + 4); let style = if i == self.selected { Some(Style::default().green()) } else { None } ; to.blit(&label, x + 2, y + 1 + i as u16 - start as u16, style); } else { break } } }, _ => {} }; draw_header(self, to, x, y, width); } } fn draw_header (state: &Plugin, to: &mut TuiOutput, x: u16, y: u16, w: u16) { let style = Style::default().gray(); let label1 = format!(" {}", state.name); to.blit(&label1, x + 1, y, Some(style.white().bold())); if let Some(ref path) = state.path { let label2 = format!("{}…", &path[..((w as usize - 10).min(path.len()))]); to.blit(&label2, x + 2 + label1.len() as u16, y, Some(style.not_dim())); } //Ok(Rect { x, y, width: w, height: 1 }) } handle!(|self:Plugin, from|{ match from.event() { key_pat!(KeyCode::Up) => { self.selected = self.selected.saturating_sub(1); Ok(Some(true)) }, key_pat!(KeyCode::Down) => { self.selected = (self.selected + 1).min(match &self.plugin { Some(PluginKind::LV2(LV2Plugin { port_list, .. })) => port_list.len() - 1, _ => unimplemented!() }); Ok(Some(true)) }, key_pat!(KeyCode::PageUp) => { self.selected = self.selected.saturating_sub(8); Ok(Some(true)) }, key_pat!(KeyCode::PageDown) => { self.selected = (self.selected + 10).min(match &self.plugin { Some(PluginKind::LV2(LV2Plugin { port_list, .. })) => port_list.len() - 1, _ => unimplemented!() }); Ok(Some(true)) }, key_pat!(KeyCode::Char(',')) => { match self.plugin.as_mut() { Some(PluginKind::LV2(LV2Plugin { port_list, ref mut instance, .. })) => { let index = port_list[self.selected].index; if let Some(value) = instance.control_input(index) { instance.set_control_input(index, value - 0.01); } }, _ => {} } Ok(Some(true)) }, key_pat!(KeyCode::Char('.')) => { match self.plugin.as_mut() { Some(PluginKind::LV2(LV2Plugin { port_list, ref mut instance, .. })) => { let index = port_list[self.selected].index; if let Some(value) = instance.control_input(index) { instance.set_control_input(index, value + 0.01); } }, _ => {} } Ok(Some(true)) }, key_pat!(KeyCode::Char('g')) => { match self.plugin { //Some(PluginKind::LV2(ref mut plugin)) => { //plugin.ui_thread = Some(run_lv2_ui(LV2PluginUI::new()?)?); //}, Some(_) => unreachable!(), None => {} } Ok(Some(true)) }, _ => Ok(None) } });