mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56: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 {}
|
||||
|
||||
/// 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 {
|
||||
Box::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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:
|
||||
pub use crate::{
|
||||
|
|
|
|||
109
src/core/jack.rs
109
src/core/jack.rs
|
|
@ -1,5 +1,114 @@
|
|||
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>
|
||||
where T: Handle + Process + Send + 'static
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@ use crate::core::*;
|
|||
|
||||
/// Trait for things that may expose JACK 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![])
|
||||
}
|
||||
fn audio_outs (&self) -> Usually<Vec<&Port<AudioOut>>> {
|
||||
fn audio_outs <'a> (&'a self) -> Usually<Vec<&'a Port<AudioOut>>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
fn midi_ins <'a> (&'a self) -> Usually<Vec<&'a Port<MidiIn>>> {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,19 @@
|
|||
use crate::core::*;
|
||||
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.
|
||||
pub trait Render: Send {
|
||||
// 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 {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
||||
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.scenes = vec![
|
||||
Scene::new("Intro", vec![None, Some(0), None, None]),
|
||||
Scene::new("Hook", vec![Some(0), 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("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 ppq = timebase.ppq() as usize;
|
||||
let ppq = timebase.ppq() as usize;
|
||||
state.tracks = 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! {
|
||||
00 * 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() },
|
||||
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![
|
||||
Plugin::lv2("Odin2", "file:///home/user/.lv2/Odin2.lv2")?.boxed(),
|
||||
]), Some(vec![
|
||||
Phrase::new("Offbeat", ppq * 4, Some(phrase! {
|
||||
00 * ppq/4 => MidiMessage::NoteOff { 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() },
|
||||
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.scene_cursor = 1;
|
||||
state.note_start = 12;
|
||||
let client = jack.as_client();
|
||||
//for track in state.tracks.iter() {
|
||||
//if let Some(port) = track.midi_ins()?.get(0) {
|
||||
//client.connect_ports(&track.midi_out, port)?;
|
||||
|
|
|
|||
|
|
@ -7,28 +7,24 @@ pub mod vst3;
|
|||
use self::lv2::*;
|
||||
|
||||
pub struct Plugin {
|
||||
pub name: String,
|
||||
pub path: Option<String>,
|
||||
pub plugin: Option<PluginKind>,
|
||||
pub selected: usize,
|
||||
pub mapping: bool,
|
||||
pub midi_ins: Vec<Port<MidiIn>>,
|
||||
pub midi_outs: Vec<Port<MidiOut>>,
|
||||
pub audio_ins: Vec<Port<AudioIn>>,
|
||||
pub audio_outs: Vec<Port<AudioOut>>,
|
||||
pub jack: Client,
|
||||
pub name: String,
|
||||
pub path: Option<String>,
|
||||
pub plugin: Option<PluginKind>,
|
||||
pub selected: usize,
|
||||
pub mapping: bool,
|
||||
pub ports: JackPorts,
|
||||
}
|
||||
render!(Plugin = crate::view::plugin::render);
|
||||
handle!(Plugin = crate::control::plugin::handle);
|
||||
process!(Plugin = Plugin::process);
|
||||
ports!(Plugin {
|
||||
audio: {
|
||||
ins: |track|Ok(track.audio_ins.iter().collect()),
|
||||
outs: |track|Ok(track.audio_outs.iter().collect()),
|
||||
ins: |p|Ok(p.ports.audio_ins.values().collect()),
|
||||
outs: |p|Ok(p.ports.audio_outs.values().collect()),
|
||||
}
|
||||
midi: {
|
||||
ins: |track|Ok(track.midi_ins.iter().collect()),
|
||||
outs: |track|Ok(track.midi_outs.iter().collect()),
|
||||
ins: |p|Ok(p.ports.midi_ins.values().collect()),
|
||||
outs: |p|Ok(p.ports.midi_outs.values().collect()),
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -41,20 +37,15 @@ pub enum PluginKind {
|
|||
}
|
||||
|
||||
impl Plugin {
|
||||
/// Load a LV2 plugin.
|
||||
/// Create a plugin host device.
|
||||
pub fn new (name: &str) -> Usually<Self> {
|
||||
let (jack, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
||||
Ok(Self {
|
||||
name: name.into(),
|
||||
path: None,
|
||||
plugin: None,
|
||||
selected: 0,
|
||||
mapping: false,
|
||||
midi_ins: vec![],
|
||||
midi_outs: vec![],
|
||||
audio_ins: vec![],
|
||||
audio_outs: vec![],
|
||||
jack,
|
||||
ports: JackPorts::default()
|
||||
})
|
||||
}
|
||||
pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
||||
|
|
@ -62,7 +53,7 @@ impl Plugin {
|
|||
Some(PluginKind::LV2(LV2Plugin { features, ref mut instance, .. })) => {
|
||||
let urid = features.midi_urid();
|
||||
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(
|
||||
&features,
|
||||
scope.n_frames() as usize
|
||||
|
|
@ -80,7 +71,7 @@ impl Plugin {
|
|||
inputs.push(atom);
|
||||
}
|
||||
let mut outputs = vec![];
|
||||
for _ in self.midi_outs.iter() {
|
||||
for _ in self.ports.midi_outs.iter() {
|
||||
outputs.push(::livi::event::LV2AtomSequence::new(
|
||||
&features,
|
||||
scope.n_frames() as usize
|
||||
|
|
@ -94,10 +85,10 @@ impl Plugin {
|
|||
outputs.iter_mut()
|
||||
)
|
||||
.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(
|
||||
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 {
|
||||
instance.run(scope.n_frames() as usize, ports).unwrap()
|
||||
|
|
|
|||
|
|
@ -41,46 +41,17 @@ impl LV2Plugin {
|
|||
}
|
||||
|
||||
impl super::Plugin {
|
||||
pub fn lv2 (name: &str, path: &str) -> Usually<Self> {
|
||||
let mut host = Self::new(name)?;
|
||||
pub fn lv2 (name: &str, path: &str) -> Usually<Arc<Mutex<Box<dyn Device>>>> {
|
||||
let plugin = LV2Plugin::new(path)?;
|
||||
let client = &host.jack;
|
||||
let (midi_ins, midi_outs, audio_ins, audio_outs) = (
|
||||
plugin.plugin.port_counts().atom_sequence_inputs,
|
||||
plugin.plugin.port_counts().atom_sequence_outputs,
|
||||
plugin.plugin.port_counts().audio_inputs,
|
||||
plugin.plugin.port_counts().audio_outputs,
|
||||
);
|
||||
host.midi_ins = {
|
||||
let mut ports = vec![];
|
||||
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)
|
||||
Jack::new(name)?
|
||||
.ports_from_lv2(&plugin.plugin)?
|
||||
.run(|ports|Box::new(Self {
|
||||
name: name.into(),
|
||||
path: Some(String::from(path)),
|
||||
plugin: Some(super::PluginKind::LV2(plugin)),
|
||||
selected: 0,
|
||||
mapping: false,
|
||||
ports
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,17 +13,6 @@ pub struct Sample {
|
|||
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 {
|
||||
pub fn chunk (&mut self, mut frames: usize) -> Option<Vec<Vec<f32>>> {
|
||||
// Create output buffer for each channel
|
||||
|
|
@ -62,46 +51,65 @@ impl Sample {
|
|||
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);
|
||||
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);
|
||||
ports!(Sampler {
|
||||
audio: {
|
||||
ins: |s|Ok(s.audio_ins.iter().collect()),
|
||||
outs: |s|Ok(s.audio_outs.iter().collect()),
|
||||
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()),
|
||||
}
|
||||
midi: { ins: |s|Ok(vec![&s.midi_in]), }
|
||||
});
|
||||
|
||||
impl Sampler {
|
||||
pub fn new (
|
||||
name: &str, samples: Option<BTreeMap<u7, Arc<Sample>>>,
|
||||
) -> Usually<Self> {
|
||||
let (jack, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
||||
Ok(Self {
|
||||
name: name.into(),
|
||||
cursor: (0, 0),
|
||||
samples: samples.unwrap_or(BTreeMap::new()),
|
||||
voices: vec![],
|
||||
midi_in: jack.register_port("midi", MidiIn::default())?,
|
||||
audio_ins: vec![
|
||||
jack.register_port("recL", AudioIn::default())?,
|
||||
jack.register_port("recR", AudioIn::default())?,
|
||||
],
|
||||
audio_outs: vec![
|
||||
jack.register_port("outL", AudioOut::default())?,
|
||||
jack.register_port("outR", AudioOut::default())?,
|
||||
],
|
||||
jack
|
||||
})
|
||||
) -> Usually<Arc<Mutex<Box<dyn Device>>>> {
|
||||
Jack::new(name)?
|
||||
.register_midi_in("midi")?
|
||||
.register_audio_in("recL")?
|
||||
.register_audio_in("recR")?
|
||||
.register_audio_out("outL")?
|
||||
.register_audio_out("outR")?
|
||||
.run(|ports|Box::new(Self {
|
||||
name: name.into(),
|
||||
cursor: (0, 0),
|
||||
samples: samples.unwrap_or(BTreeMap::new()),
|
||||
voices: vec![],
|
||||
ports
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
||||
// 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];
|
||||
// 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 MidiMessage::NoteOn { ref key, .. } = message {
|
||||
if let Some(sample) = self.samples.get(key) {
|
||||
|
|
@ -130,7 +138,7 @@ impl Sampler {
|
|||
}
|
||||
}
|
||||
// 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];
|
||||
for (i, value) in port.as_mut_slice(scope).iter_mut().enumerate() {
|
||||
*value = *buffer.get(i).unwrap_or(&0.0);
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ pub struct Track {
|
|||
/// Red keys on piano roll.
|
||||
pub notes_on: Vec<bool>,
|
||||
/// Device chain
|
||||
pub devices: Vec<Box<dyn Device>>,
|
||||
pub devices: Vec<Arc<Mutex<Box<dyn Device>>>>,
|
||||
/// Device selector
|
||||
pub device: usize,
|
||||
}
|
||||
|
|
@ -27,8 +27,8 @@ impl Track {
|
|||
pub fn new (
|
||||
name: &str,
|
||||
jack: &Client,
|
||||
devices: Option<Vec<Box<dyn Device>>>,
|
||||
phrases: Option<Vec<Phrase>>,
|
||||
devices: Option<Vec<Arc<Mutex<Box<dyn Device>>>>>,
|
||||
) -> Usually<Self> {
|
||||
Ok(Self {
|
||||
name: name.to_string(),
|
||||
|
|
@ -43,17 +43,17 @@ impl Track {
|
|||
device: 0,
|
||||
})
|
||||
}
|
||||
pub fn device (&self) -> Option<&Box<dyn Device>> {
|
||||
self.devices.get(self.device)
|
||||
pub fn device (&self, i: usize) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||
self.devices.get(i).map(|d|d.lock().unwrap())
|
||||
}
|
||||
pub fn device_mut (&mut self) -> Option<&mut Box<dyn Device>> {
|
||||
self.devices.get_mut(self.device)
|
||||
pub fn active_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||
self.device(self.device)
|
||||
}
|
||||
pub fn first_device (&self) -> Option<&Box<dyn Device>> {
|
||||
self.devices.get(0)
|
||||
pub fn first_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||
self.device(0)
|
||||
}
|
||||
pub fn last_device (&self) -> Option<&Box<dyn Device>> {
|
||||
self.devices.get(self.devices.len().saturating_sub(1))
|
||||
pub fn last_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||
self.device(self.devices.len().saturating_sub(1))
|
||||
}
|
||||
pub fn phrase (&self) -> Option<&Phrase> {
|
||||
if let Some(phrase) = self.sequence {
|
||||
|
|
|
|||
|
|
@ -145,6 +145,7 @@ pub fn draw_as_column (
|
|||
let mut w = 0u16;
|
||||
let mut frames = vec![];
|
||||
for device in state.devices.iter() {
|
||||
let device = device.lock().unwrap();
|
||||
let style_midi = Style::default().black().bold().on_green();
|
||||
let style_audio = Style::default().black().bold().on_red();
|
||||
let midi_ins = device.midi_ins()?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue