mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
wip: connect devices
This commit is contained in:
parent
7f3425fe04
commit
394355331d
10 changed files with 235 additions and 145 deletions
|
|
@ -24,14 +24,14 @@ pub trait Component: Render + Handle {}
|
||||||
impl<T: Render + Handle> Component for T {}
|
impl<T: Render + Handle> Component for T {}
|
||||||
|
|
||||||
/// A UI component that may have presence on the JACK grap.
|
/// A UI component that may have presence on the JACK grap.
|
||||||
pub trait Device: Render + Handle + Ports + Send + Sync {
|
pub trait Device: Render + Handle + Process + Ports + Send + Sync {
|
||||||
fn boxed (self) -> Box<dyn Device> where Self: Sized + 'static {
|
fn boxed (self) -> Box<dyn Device> where Self: Sized + 'static {
|
||||||
Box::new(self)
|
Box::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// All things that implement the required traits can be treated as `Device`.
|
/// All things that implement the required traits can be treated as `Device`.
|
||||||
impl<T: Render + Handle + Ports + Send + Sync> Device for T {}
|
impl<T: Render + Handle + Process + Ports + Send + Sync> Device for T {}
|
||||||
|
|
||||||
// Reexport macros:
|
// Reexport macros:
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
|
|
|
||||||
109
src/core/jack.rs
109
src/core/jack.rs
|
|
@ -1,5 +1,114 @@
|
||||||
use crate::core::*;
|
use crate::core::*;
|
||||||
|
|
||||||
|
pub struct Jack {
|
||||||
|
pub client: JackClient,
|
||||||
|
pub ports: JackPorts,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum JackClient {
|
||||||
|
Active(DynamicAsyncClient),
|
||||||
|
Inactive(Client),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct JackPorts {
|
||||||
|
pub audio_ins: BTreeMap<String, Port<AudioIn>>,
|
||||||
|
pub midi_ins: BTreeMap<String, Port<MidiIn>>,
|
||||||
|
pub audio_outs: BTreeMap<String, Port<AudioOut>>,
|
||||||
|
pub midi_outs: BTreeMap<String, Port<MidiOut>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
ports!(Jack {
|
||||||
|
audio: {
|
||||||
|
ins: |s|Ok(s.ports.audio_ins.values().collect()),
|
||||||
|
outs: |s|Ok(s.ports.audio_outs.values().collect()),
|
||||||
|
}
|
||||||
|
midi: {
|
||||||
|
ins: |s|Ok(s.ports.midi_ins.values().collect()),
|
||||||
|
outs: |s|Ok(s.ports.midi_outs.values().collect()),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
impl Jack {
|
||||||
|
pub fn new (name: &str) -> Usually<Self> {
|
||||||
|
let (client, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
||||||
|
Ok(Self { client: JackClient::Inactive(client), ports: JackPorts::default(), })
|
||||||
|
}
|
||||||
|
pub fn ports_from_lv2 (self, plugin: &::livi::Plugin) -> Usually<Self> {
|
||||||
|
let counts = plugin.port_counts();
|
||||||
|
let mut jack = self;
|
||||||
|
for i in 0..counts.atom_sequence_inputs {
|
||||||
|
jack = jack.register_midi_in(&format!("midi-in-{i}"))?
|
||||||
|
}
|
||||||
|
for i in 0..counts.atom_sequence_outputs {
|
||||||
|
jack = jack.register_midi_out(&format!("midi-out-{i}"))?;
|
||||||
|
}
|
||||||
|
for i in 0..counts.audio_inputs {
|
||||||
|
jack = jack.register_audio_in(&format!("audio-in-{i}"))?
|
||||||
|
}
|
||||||
|
for i in 0..counts.audio_outputs {
|
||||||
|
jack = jack.register_audio_out(&format!("audio-out-{i}"))?;
|
||||||
|
}
|
||||||
|
Ok(jack)
|
||||||
|
}
|
||||||
|
pub fn register_midi_out (mut self, name: &str) -> Usually<Self> {
|
||||||
|
self.ports.midi_outs.insert(name.to_string(), match &self.client {
|
||||||
|
JackClient::Inactive(c) => c.register_port(name, MidiOut::default())?,
|
||||||
|
JackClient::Active(c) => c.as_client().register_port(name, MidiOut::default())?,
|
||||||
|
});
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
pub fn register_midi_in (mut self, name: &str) -> Usually<Self> {
|
||||||
|
self.ports.midi_ins.insert(name.to_string(), match &self.client {
|
||||||
|
JackClient::Inactive(c) => c.register_port(name, MidiIn::default())?,
|
||||||
|
JackClient::Active(c) => c.as_client().register_port(name, MidiIn::default())?,
|
||||||
|
});
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
pub fn register_audio_out (mut self, name: &str) -> Usually<Self> {
|
||||||
|
self.ports.audio_outs.insert(name.to_string(), match &self.client {
|
||||||
|
JackClient::Inactive(c) => c.register_port(name, AudioOut::default())?,
|
||||||
|
JackClient::Active(c) => c.as_client().register_port(name, AudioOut::default())?,
|
||||||
|
});
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
pub fn register_audio_in (mut self, name: &str) -> Usually<Self> {
|
||||||
|
self.ports.audio_ins.insert(name.to_string(), match &self.client {
|
||||||
|
JackClient::Inactive(c) => c.register_port(name, AudioIn::default())?,
|
||||||
|
JackClient::Active(c) => c.as_client().register_port(name, AudioIn::default())?,
|
||||||
|
});
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
pub fn run <T> (
|
||||||
|
mut self, state: impl FnOnce(JackPorts)->Box<T>
|
||||||
|
) -> Usually<Arc<Mutex<Box<dyn Device>>>> where
|
||||||
|
T: Device + Process + Sized + 'static
|
||||||
|
{
|
||||||
|
let mut ports = JackPorts::default();
|
||||||
|
std::mem::swap(&mut self.ports, &mut ports);
|
||||||
|
let state = Arc::new(Mutex::new(state(ports) as Box<dyn Device>));
|
||||||
|
self.client = match self.client {
|
||||||
|
JackClient::Active(_) => panic!("already running"),
|
||||||
|
JackClient::Inactive(client) => JackClient::Active(client.activate_async(
|
||||||
|
Notifications(Box::new({
|
||||||
|
let _state = state.clone();
|
||||||
|
move|_event|{
|
||||||
|
// FIXME: this deadlocks
|
||||||
|
//state.lock().unwrap().handle(&event).unwrap();
|
||||||
|
}
|
||||||
|
}) as Box<dyn Fn(AppEvent) + Send + Sync>),
|
||||||
|
ClosureProcessHandler::new(Box::new({
|
||||||
|
let state = state.clone();
|
||||||
|
move|c: &Client, s: &ProcessScope|{
|
||||||
|
state.lock().unwrap().process(c, s)
|
||||||
|
}
|
||||||
|
}) as BoxedProcessHandler)
|
||||||
|
)?)
|
||||||
|
};
|
||||||
|
Ok(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn jack_run <T> (name: &str, app: &Arc<Mutex<T>>) -> Usually<DynamicAsyncClient>
|
pub fn jack_run <T> (name: &str, app: &Arc<Mutex<T>>) -> Usually<DynamicAsyncClient>
|
||||||
where T: Handle + Process + Send + 'static
|
where T: Handle + Process + Send + 'static
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ use crate::core::*;
|
||||||
|
|
||||||
/// Trait for things that may expose JACK ports.
|
/// Trait for things that may expose JACK ports.
|
||||||
pub trait Ports {
|
pub trait Ports {
|
||||||
fn audio_ins (&self) -> Usually<Vec<&Port<AudioIn>>> {
|
fn audio_ins <'a> (&'a self) -> Usually<Vec<&'a Port<AudioIn>>> {
|
||||||
Ok(vec![])
|
Ok(vec![])
|
||||||
}
|
}
|
||||||
fn audio_outs (&self) -> Usually<Vec<&Port<AudioOut>>> {
|
fn audio_outs <'a> (&'a self) -> Usually<Vec<&'a Port<AudioOut>>> {
|
||||||
Ok(vec![])
|
Ok(vec![])
|
||||||
}
|
}
|
||||||
fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<MidiIn>>> {
|
fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<MidiIn>>> {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,19 @@
|
||||||
use crate::core::*;
|
use crate::core::*;
|
||||||
use ratatui::widgets::WidgetRef;
|
use ratatui::widgets::WidgetRef;
|
||||||
|
|
||||||
|
pub trait Blit {
|
||||||
|
// Render something to X, Y coordinates in a buffer, ignoring width/height.
|
||||||
|
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: AsRef<str>> Blit for T {
|
||||||
|
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) {
|
||||||
|
if x < buf.area.width && y < buf.area.height {
|
||||||
|
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Trait for things that render to the display.
|
/// Trait for things that render to the display.
|
||||||
pub trait Render: Send {
|
pub trait Render: Send {
|
||||||
// Render something to an area of the buffer.
|
// Render something to an area of the buffer.
|
||||||
|
|
@ -43,6 +56,12 @@ impl Render for Box<dyn Device> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: Render> Render for Arc<Mutex<T>> {
|
||||||
|
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
|
||||||
|
self.lock().unwrap().render(b, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl WidgetRef for &dyn Render {
|
impl WidgetRef for &dyn Render {
|
||||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||||
Render::render(*self, buf, area).expect("Failed to render device.");
|
Render::render(*self, buf, area).expect("Failed to render device.");
|
||||||
|
|
@ -54,16 +73,3 @@ impl WidgetRef for dyn Render {
|
||||||
Render::render(self, buf, area).expect("Failed to render device.");
|
Render::render(self, buf, area).expect("Failed to render device.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Blit {
|
|
||||||
// Render something to X, Y coordinates in a buffer, ignoring width/height.
|
|
||||||
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: AsRef<str>> Blit for T {
|
|
||||||
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) {
|
|
||||||
if x < buf.area.width && y < buf.area.height {
|
|
||||||
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
40
src/main.rs
40
src/main.rs
|
|
@ -24,36 +24,39 @@ pub fn main () -> Usually<()> {
|
||||||
state.modal = Some(Box::new(crate::config::SetupModal(Some(xdg.clone()))));
|
state.modal = Some(Box::new(crate::config::SetupModal(Some(xdg.clone()))));
|
||||||
}
|
}
|
||||||
state.scenes = vec![
|
state.scenes = vec![
|
||||||
Scene::new("Intro", vec![None, Some(0), None, None]),
|
Scene::new("Intro", vec![None, Some(0), None, None]),
|
||||||
Scene::new("Hook", vec![Some(0), Some(0), None, None]),
|
Scene::new("Hook", vec![Some(0), Some(0), None, None]),
|
||||||
|
Scene::new("Verse", vec![None, Some(0), None, None]),
|
||||||
|
Scene::new("Chorus", vec![None, Some(0), None, None]),
|
||||||
|
Scene::new("Bridge", vec![None, Some(0), None, None]),
|
||||||
|
Scene::new("Outro", vec![None, Some(0), None, None]),
|
||||||
];
|
];
|
||||||
let jack = jack_run("tek", &app)?;
|
let jack = jack_run("tek", &app)?;
|
||||||
|
let client = jack.as_client();
|
||||||
let timebase = &state.timebase;
|
let timebase = &state.timebase;
|
||||||
let ppq = timebase.ppq() as usize;
|
let ppq = timebase.ppq() as usize;
|
||||||
state.tracks = vec![
|
state.tracks = vec![
|
||||||
|
|
||||||
Track::new("Drums", &jack.as_client(), Some(vec![
|
Track::new("Drums", &jack.as_client(), Some(vec![
|
||||||
Sampler::new("Sampler", Some(BTreeMap::from([
|
|
||||||
sample!(36, "Kick", "/home/user/Lab/Music/pak/kik.wav"),
|
|
||||||
sample!(40, "Snare", "/home/user/Lab/Music/pak/sna.wav"),
|
|
||||||
sample!(44, "Hihat", "/home/user/Lab/Music/pak/chh.wav"),
|
|
||||||
])))?.boxed(),
|
|
||||||
Plugin::lv2(
|
|
||||||
"Panagement",
|
|
||||||
"file:///home/user/.lv2/Auburn Sounds Panagement 2.lv2"
|
|
||||||
)?.boxed(),
|
|
||||||
]), Some(vec![
|
|
||||||
Phrase::new("4 kicks", ppq * 4, Some(phrase! {
|
Phrase::new("4 kicks", ppq * 4, Some(phrase! {
|
||||||
00 * ppq/4 => MidiMessage::NoteOn { key: 36.into(), vel: 100.into() },
|
00 * ppq/4 => MidiMessage::NoteOn { key: 36.into(), vel: 100.into() },
|
||||||
04 * ppq/4 => MidiMessage::NoteOn { key: 36.into(), vel: 100.into() },
|
04 * ppq/4 => MidiMessage::NoteOn { key: 36.into(), vel: 100.into() },
|
||||||
08 * ppq/4 => MidiMessage::NoteOn { key: 36.into(), vel: 100.into() },
|
08 * ppq/4 => MidiMessage::NoteOn { key: 36.into(), vel: 100.into() },
|
||||||
12 * ppq/4 => MidiMessage::NoteOn { key: 36.into(), vel: 100.into() },
|
12 * ppq/4 => MidiMessage::NoteOn { key: 36.into(), vel: 100.into() },
|
||||||
})),
|
})),
|
||||||
]))?,
|
]), Some(vec![
|
||||||
|
Sampler::new("Sampler", Some(BTreeMap::from([
|
||||||
|
sample!(36, "Kick", "/home/user/Lab/Music/pak/kik.wav"),
|
||||||
|
sample!(40, "Snare", "/home/user/Lab/Music/pak/sna.wav"),
|
||||||
|
sample!(44, "Hihat", "/home/user/Lab/Music/pak/chh.wav"),
|
||||||
|
])))?,
|
||||||
|
Plugin::lv2(
|
||||||
|
"Panagement",
|
||||||
|
"file:///home/user/.lv2/Auburn Sounds Panagement 2.lv2"
|
||||||
|
)?,
|
||||||
|
]),)?,
|
||||||
|
|
||||||
Track::new("Bass", &jack.as_client(), Some(vec![
|
Track::new("Bass", &jack.as_client(), Some(vec![
|
||||||
Plugin::lv2("Odin2", "file:///home/user/.lv2/Odin2.lv2")?.boxed(),
|
|
||||||
]), Some(vec![
|
|
||||||
Phrase::new("Offbeat", ppq * 4, Some(phrase! {
|
Phrase::new("Offbeat", ppq * 4, Some(phrase! {
|
||||||
00 * ppq/4 => MidiMessage::NoteOff { key: 40.into(), vel: 100.into() },
|
00 * ppq/4 => MidiMessage::NoteOff { key: 40.into(), vel: 100.into() },
|
||||||
02 * ppq/4 => MidiMessage::NoteOn { key: 40.into(), vel: 100.into() },
|
02 * ppq/4 => MidiMessage::NoteOn { key: 40.into(), vel: 100.into() },
|
||||||
|
|
@ -64,6 +67,8 @@ pub fn main () -> Usually<()> {
|
||||||
12 * ppq/4 => MidiMessage::NoteOff { key: 40.into(), vel: 100.into() },
|
12 * ppq/4 => MidiMessage::NoteOff { key: 40.into(), vel: 100.into() },
|
||||||
14 * ppq/4 => MidiMessage::NoteOn { key: 40.into(), vel: 100.into() },
|
14 * ppq/4 => MidiMessage::NoteOn { key: 40.into(), vel: 100.into() },
|
||||||
})),
|
})),
|
||||||
|
]), Some(vec![
|
||||||
|
Plugin::lv2("Odin2", "file:///home/user/.lv2/Odin2.lv2")?,
|
||||||
]))?,
|
]))?,
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
@ -72,7 +77,6 @@ pub fn main () -> Usually<()> {
|
||||||
state.track_cursor = 1;
|
state.track_cursor = 1;
|
||||||
state.scene_cursor = 1;
|
state.scene_cursor = 1;
|
||||||
state.note_start = 12;
|
state.note_start = 12;
|
||||||
let client = jack.as_client();
|
|
||||||
//for track in state.tracks.iter() {
|
//for track in state.tracks.iter() {
|
||||||
//if let Some(port) = track.midi_ins()?.get(0) {
|
//if let Some(port) = track.midi_ins()?.get(0) {
|
||||||
//client.connect_ports(&track.midi_out, port)?;
|
//client.connect_ports(&track.midi_out, port)?;
|
||||||
|
|
|
||||||
|
|
@ -7,28 +7,24 @@ pub mod vst3;
|
||||||
use self::lv2::*;
|
use self::lv2::*;
|
||||||
|
|
||||||
pub struct Plugin {
|
pub struct Plugin {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub path: Option<String>,
|
pub path: Option<String>,
|
||||||
pub plugin: Option<PluginKind>,
|
pub plugin: Option<PluginKind>,
|
||||||
pub selected: usize,
|
pub selected: usize,
|
||||||
pub mapping: bool,
|
pub mapping: bool,
|
||||||
pub midi_ins: Vec<Port<MidiIn>>,
|
pub ports: JackPorts,
|
||||||
pub midi_outs: Vec<Port<MidiOut>>,
|
|
||||||
pub audio_ins: Vec<Port<AudioIn>>,
|
|
||||||
pub audio_outs: Vec<Port<AudioOut>>,
|
|
||||||
pub jack: Client,
|
|
||||||
}
|
}
|
||||||
render!(Plugin = crate::view::plugin::render);
|
render!(Plugin = crate::view::plugin::render);
|
||||||
handle!(Plugin = crate::control::plugin::handle);
|
handle!(Plugin = crate::control::plugin::handle);
|
||||||
process!(Plugin = Plugin::process);
|
process!(Plugin = Plugin::process);
|
||||||
ports!(Plugin {
|
ports!(Plugin {
|
||||||
audio: {
|
audio: {
|
||||||
ins: |track|Ok(track.audio_ins.iter().collect()),
|
ins: |p|Ok(p.ports.audio_ins.values().collect()),
|
||||||
outs: |track|Ok(track.audio_outs.iter().collect()),
|
outs: |p|Ok(p.ports.audio_outs.values().collect()),
|
||||||
}
|
}
|
||||||
midi: {
|
midi: {
|
||||||
ins: |track|Ok(track.midi_ins.iter().collect()),
|
ins: |p|Ok(p.ports.midi_ins.values().collect()),
|
||||||
outs: |track|Ok(track.midi_outs.iter().collect()),
|
outs: |p|Ok(p.ports.midi_outs.values().collect()),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -41,20 +37,15 @@ pub enum PluginKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Plugin {
|
impl Plugin {
|
||||||
/// Load a LV2 plugin.
|
/// Create a plugin host device.
|
||||||
pub fn new (name: &str) -> Usually<Self> {
|
pub fn new (name: &str) -> Usually<Self> {
|
||||||
let (jack, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
path: None,
|
path: None,
|
||||||
plugin: None,
|
plugin: None,
|
||||||
selected: 0,
|
selected: 0,
|
||||||
mapping: false,
|
mapping: false,
|
||||||
midi_ins: vec![],
|
ports: JackPorts::default()
|
||||||
midi_outs: vec![],
|
|
||||||
audio_ins: vec![],
|
|
||||||
audio_outs: vec![],
|
|
||||||
jack,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
||||||
|
|
@ -62,7 +53,7 @@ impl Plugin {
|
||||||
Some(PluginKind::LV2(LV2Plugin { features, ref mut instance, .. })) => {
|
Some(PluginKind::LV2(LV2Plugin { features, ref mut instance, .. })) => {
|
||||||
let urid = features.midi_urid();
|
let urid = features.midi_urid();
|
||||||
let mut inputs = vec![];
|
let mut inputs = vec![];
|
||||||
for port in self.midi_ins.iter() {
|
for port in self.ports.midi_ins.values() {
|
||||||
let mut atom = ::livi::event::LV2AtomSequence::new(
|
let mut atom = ::livi::event::LV2AtomSequence::new(
|
||||||
&features,
|
&features,
|
||||||
scope.n_frames() as usize
|
scope.n_frames() as usize
|
||||||
|
|
@ -80,7 +71,7 @@ impl Plugin {
|
||||||
inputs.push(atom);
|
inputs.push(atom);
|
||||||
}
|
}
|
||||||
let mut outputs = vec![];
|
let mut outputs = vec![];
|
||||||
for _ in self.midi_outs.iter() {
|
for _ in self.ports.midi_outs.iter() {
|
||||||
outputs.push(::livi::event::LV2AtomSequence::new(
|
outputs.push(::livi::event::LV2AtomSequence::new(
|
||||||
&features,
|
&features,
|
||||||
scope.n_frames() as usize
|
scope.n_frames() as usize
|
||||||
|
|
@ -94,10 +85,10 @@ impl Plugin {
|
||||||
outputs.iter_mut()
|
outputs.iter_mut()
|
||||||
)
|
)
|
||||||
.with_audio_inputs(
|
.with_audio_inputs(
|
||||||
self.audio_ins.iter().map(|o|o.as_slice(scope))
|
self.ports.audio_ins.values().map(|o|o.as_slice(scope))
|
||||||
)
|
)
|
||||||
.with_audio_outputs(
|
.with_audio_outputs(
|
||||||
self.audio_outs.iter_mut().map(|o|o.as_mut_slice(scope))
|
self.ports.audio_outs.values_mut().map(|o|o.as_mut_slice(scope))
|
||||||
);
|
);
|
||||||
unsafe {
|
unsafe {
|
||||||
instance.run(scope.n_frames() as usize, ports).unwrap()
|
instance.run(scope.n_frames() as usize, ports).unwrap()
|
||||||
|
|
|
||||||
|
|
@ -41,46 +41,17 @@ impl LV2Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl super::Plugin {
|
impl super::Plugin {
|
||||||
pub fn lv2 (name: &str, path: &str) -> Usually<Self> {
|
pub fn lv2 (name: &str, path: &str) -> Usually<Arc<Mutex<Box<dyn Device>>>> {
|
||||||
let mut host = Self::new(name)?;
|
|
||||||
let plugin = LV2Plugin::new(path)?;
|
let plugin = LV2Plugin::new(path)?;
|
||||||
let client = &host.jack;
|
Jack::new(name)?
|
||||||
let (midi_ins, midi_outs, audio_ins, audio_outs) = (
|
.ports_from_lv2(&plugin.plugin)?
|
||||||
plugin.plugin.port_counts().atom_sequence_inputs,
|
.run(|ports|Box::new(Self {
|
||||||
plugin.plugin.port_counts().atom_sequence_outputs,
|
name: name.into(),
|
||||||
plugin.plugin.port_counts().audio_inputs,
|
path: Some(String::from(path)),
|
||||||
plugin.plugin.port_counts().audio_outputs,
|
plugin: Some(super::PluginKind::LV2(plugin)),
|
||||||
);
|
selected: 0,
|
||||||
host.midi_ins = {
|
mapping: false,
|
||||||
let mut ports = vec![];
|
ports
|
||||||
for i in 0..midi_ins {
|
}))
|
||||||
ports.push(client.register_port(&format!("midi-in-{i}"), MidiIn::default())?)
|
|
||||||
}
|
|
||||||
ports
|
|
||||||
};
|
|
||||||
host.midi_outs = {
|
|
||||||
let mut ports = vec![];
|
|
||||||
for i in 0..midi_outs {
|
|
||||||
ports.push(client.register_port(&format!("midi-out-{i}"), MidiOut::default())?)
|
|
||||||
}
|
|
||||||
ports
|
|
||||||
};
|
|
||||||
host.audio_ins = {
|
|
||||||
let mut ports = vec![];
|
|
||||||
for i in 0..audio_ins {
|
|
||||||
ports.push(client.register_port(&format!("audio-in-{i}"), AudioIn::default())?)
|
|
||||||
}
|
|
||||||
ports
|
|
||||||
};
|
|
||||||
host.audio_outs = {
|
|
||||||
let mut ports = vec![];
|
|
||||||
for i in 0..audio_outs {
|
|
||||||
ports.push(client.register_port(&format!("audio-out-{i}"), AudioOut::default())?)
|
|
||||||
}
|
|
||||||
ports
|
|
||||||
};
|
|
||||||
host.plugin = Some(super::PluginKind::LV2(plugin));
|
|
||||||
host.path = Some(String::from(path));
|
|
||||||
Ok(host)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,17 +13,6 @@ pub struct Sample {
|
||||||
pub channels: Vec<Vec<f32>>,
|
pub channels: Vec<Vec<f32>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Sampler {
|
|
||||||
pub name: String,
|
|
||||||
pub jack: Client,
|
|
||||||
pub cursor: (usize, usize),
|
|
||||||
pub samples: BTreeMap<u7, Arc<Sample>>,
|
|
||||||
pub voices: Vec<Voice>,
|
|
||||||
pub midi_in: Port<MidiIn>,
|
|
||||||
pub audio_ins: Vec<Port<AudioIn>>,
|
|
||||||
pub audio_outs: Vec<Port<AudioOut>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Voice {
|
impl Voice {
|
||||||
pub fn chunk (&mut self, mut frames: usize) -> Option<Vec<Vec<f32>>> {
|
pub fn chunk (&mut self, mut frames: usize) -> Option<Vec<Vec<f32>>> {
|
||||||
// Create output buffer for each channel
|
// Create output buffer for each channel
|
||||||
|
|
@ -62,46 +51,65 @@ impl Sample {
|
||||||
Voice { sample: self.clone(), after, position: self.start }
|
Voice { sample: self.clone(), after, position: self.start }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Sampler {
|
||||||
|
pub name: String,
|
||||||
|
pub cursor: (usize, usize),
|
||||||
|
pub samples: BTreeMap<u7, Arc<Sample>>,
|
||||||
|
pub voices: Vec<Voice>,
|
||||||
|
pub ports: JackPorts,
|
||||||
|
}
|
||||||
|
|
||||||
render!(Sampler = crate::view::sampler::render);
|
render!(Sampler = crate::view::sampler::render);
|
||||||
handle!(Sampler = crate::control::sampler::handle);
|
handle!(Sampler = crate::control::sampler::handle);
|
||||||
|
//jack!(Sampler {
|
||||||
|
//process = Sampler::process,
|
||||||
|
//audio = {
|
||||||
|
//ins = |s|Ok(s.jack.audio_ins.values().collect()),
|
||||||
|
//outs = |s|Ok(s.jack.audio_outs.values().collect()),
|
||||||
|
//}
|
||||||
|
//midi = {
|
||||||
|
//ins = |s|Ok(s.jack.midi_ins.values().collect()),
|
||||||
|
//outs = |s|Ok(s.jack.midi_outs.values().collect()),
|
||||||
|
//}
|
||||||
|
//});
|
||||||
process!(Sampler = Sampler::process);
|
process!(Sampler = Sampler::process);
|
||||||
ports!(Sampler {
|
ports!(Sampler {
|
||||||
audio: {
|
audio: {
|
||||||
ins: |s|Ok(s.audio_ins.iter().collect()),
|
ins: |s|Ok(s.ports.audio_ins.values().collect()),
|
||||||
outs: |s|Ok(s.audio_outs.iter().collect()),
|
outs: |s|Ok(s.ports.audio_outs.values().collect()),
|
||||||
|
}
|
||||||
|
midi: {
|
||||||
|
ins: |s|Ok(s.ports.midi_ins.values().collect()),
|
||||||
|
outs: |s|Ok(s.ports.midi_outs.values().collect()),
|
||||||
}
|
}
|
||||||
midi: { ins: |s|Ok(vec![&s.midi_in]), }
|
|
||||||
});
|
});
|
||||||
|
|
||||||
impl Sampler {
|
impl Sampler {
|
||||||
pub fn new (
|
pub fn new (
|
||||||
name: &str, samples: Option<BTreeMap<u7, Arc<Sample>>>,
|
name: &str, samples: Option<BTreeMap<u7, Arc<Sample>>>,
|
||||||
) -> Usually<Self> {
|
) -> Usually<Arc<Mutex<Box<dyn Device>>>> {
|
||||||
let (jack, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
Jack::new(name)?
|
||||||
Ok(Self {
|
.register_midi_in("midi")?
|
||||||
name: name.into(),
|
.register_audio_in("recL")?
|
||||||
cursor: (0, 0),
|
.register_audio_in("recR")?
|
||||||
samples: samples.unwrap_or(BTreeMap::new()),
|
.register_audio_out("outL")?
|
||||||
voices: vec![],
|
.register_audio_out("outR")?
|
||||||
midi_in: jack.register_port("midi", MidiIn::default())?,
|
.run(|ports|Box::new(Self {
|
||||||
audio_ins: vec![
|
name: name.into(),
|
||||||
jack.register_port("recL", AudioIn::default())?,
|
cursor: (0, 0),
|
||||||
jack.register_port("recR", AudioIn::default())?,
|
samples: samples.unwrap_or(BTreeMap::new()),
|
||||||
],
|
voices: vec![],
|
||||||
audio_outs: vec![
|
ports
|
||||||
jack.register_port("outL", AudioOut::default())?,
|
}))
|
||||||
jack.register_port("outR", AudioOut::default())?,
|
|
||||||
],
|
|
||||||
jack
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
||||||
// Output buffer: this will be copied to the audio outs.
|
// Output buffer: this will be copied to the audio outs.
|
||||||
let channel_count = self.audio_outs.len();
|
let channel_count = self.ports.audio_outs.len();
|
||||||
let mut mixed = vec![vec![0.0;scope.n_frames() as usize];channel_count];
|
let mut mixed = vec![vec![0.0;scope.n_frames() as usize];channel_count];
|
||||||
// Process MIDI input to add new voices.
|
// Process MIDI input to add new voices.
|
||||||
for RawMidi { time, bytes } in self.midi_in.iter(scope) {
|
for RawMidi { time, bytes } in self.ports.midi_ins.get("midi").unwrap().iter(scope) {
|
||||||
if let LiveEvent::Midi { message, .. } = LiveEvent::parse(bytes).unwrap() {
|
if let LiveEvent::Midi { message, .. } = LiveEvent::parse(bytes).unwrap() {
|
||||||
if let MidiMessage::NoteOn { ref key, .. } = message {
|
if let MidiMessage::NoteOn { ref key, .. } = message {
|
||||||
if let Some(sample) = self.samples.get(key) {
|
if let Some(sample) = self.samples.get(key) {
|
||||||
|
|
@ -130,7 +138,7 @@ impl Sampler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Write output buffer to output ports.
|
// Write output buffer to output ports.
|
||||||
for (i, port) in self.audio_outs.iter_mut().enumerate() {
|
for (i, port) in self.ports.audio_outs.values_mut().enumerate() {
|
||||||
let buffer = &mixed[i];
|
let buffer = &mixed[i];
|
||||||
for (i, value) in port.as_mut_slice(scope).iter_mut().enumerate() {
|
for (i, value) in port.as_mut_slice(scope).iter_mut().enumerate() {
|
||||||
*value = *buffer.get(i).unwrap_or(&0.0);
|
*value = *buffer.get(i).unwrap_or(&0.0);
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ pub struct Track {
|
||||||
/// Red keys on piano roll.
|
/// Red keys on piano roll.
|
||||||
pub notes_on: Vec<bool>,
|
pub notes_on: Vec<bool>,
|
||||||
/// Device chain
|
/// Device chain
|
||||||
pub devices: Vec<Box<dyn Device>>,
|
pub devices: Vec<Arc<Mutex<Box<dyn Device>>>>,
|
||||||
/// Device selector
|
/// Device selector
|
||||||
pub device: usize,
|
pub device: usize,
|
||||||
}
|
}
|
||||||
|
|
@ -27,8 +27,8 @@ impl Track {
|
||||||
pub fn new (
|
pub fn new (
|
||||||
name: &str,
|
name: &str,
|
||||||
jack: &Client,
|
jack: &Client,
|
||||||
devices: Option<Vec<Box<dyn Device>>>,
|
|
||||||
phrases: Option<Vec<Phrase>>,
|
phrases: Option<Vec<Phrase>>,
|
||||||
|
devices: Option<Vec<Arc<Mutex<Box<dyn Device>>>>>,
|
||||||
) -> Usually<Self> {
|
) -> Usually<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
|
|
@ -43,17 +43,17 @@ impl Track {
|
||||||
device: 0,
|
device: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn device (&self) -> Option<&Box<dyn Device>> {
|
pub fn device (&self, i: usize) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||||
self.devices.get(self.device)
|
self.devices.get(i).map(|d|d.lock().unwrap())
|
||||||
}
|
}
|
||||||
pub fn device_mut (&mut self) -> Option<&mut Box<dyn Device>> {
|
pub fn active_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||||
self.devices.get_mut(self.device)
|
self.device(self.device)
|
||||||
}
|
}
|
||||||
pub fn first_device (&self) -> Option<&Box<dyn Device>> {
|
pub fn first_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||||
self.devices.get(0)
|
self.device(0)
|
||||||
}
|
}
|
||||||
pub fn last_device (&self) -> Option<&Box<dyn Device>> {
|
pub fn last_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||||
self.devices.get(self.devices.len().saturating_sub(1))
|
self.device(self.devices.len().saturating_sub(1))
|
||||||
}
|
}
|
||||||
pub fn phrase (&self) -> Option<&Phrase> {
|
pub fn phrase (&self) -> Option<&Phrase> {
|
||||||
if let Some(phrase) = self.sequence {
|
if let Some(phrase) = self.sequence {
|
||||||
|
|
|
||||||
|
|
@ -145,6 +145,7 @@ pub fn draw_as_column (
|
||||||
let mut w = 0u16;
|
let mut w = 0u16;
|
||||||
let mut frames = vec![];
|
let mut frames = vec![];
|
||||||
for device in state.devices.iter() {
|
for device in state.devices.iter() {
|
||||||
|
let device = device.lock().unwrap();
|
||||||
let style_midi = Style::default().black().bold().on_green();
|
let style_midi = Style::default().black().bold().on_green();
|
||||||
let style_audio = Style::default().black().bold().on_red();
|
let style_audio = Style::default().black().bold().on_red();
|
||||||
let midi_ins = device.midi_ins()?;
|
let midi_ins = device.midi_ins()?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue