nice (and working) port connect api

This commit is contained in:
🪞👃🪞 2024-07-05 00:49:24 +03:00
parent f928b026ed
commit 3ed9ebddd4
7 changed files with 209 additions and 190 deletions

View file

@ -44,7 +44,7 @@ impl super::Plugin {
pub fn lv2 (name: &str, path: &str) -> Usually<JackDevice> {
let plugin = LV2Plugin::new(path)?;
Jack::new(name)?
.ports_from_lv2(&plugin.plugin)?
.ports_from_lv2(&plugin.plugin)
.run(|ports|Box::new(Self {
name: name.into(),
path: Some(String::from(path)),

View file

@ -1,57 +1,5 @@
use crate::core::*;
pub struct Voice {
pub sample: Arc<Sample>,
pub after: usize,
pub position: usize,
}
pub struct Sample {
pub name: String,
pub start: usize,
pub end: usize,
pub channels: Vec<Vec<f32>>,
}
impl Voice {
pub fn chunk (&mut self, mut frames: usize) -> Option<Vec<Vec<f32>>> {
// Create output buffer for each channel
let mut chunk = vec![vec![];self.sample.channels.len()];
// If it's not time to play yet, count down
if self.after >= frames {
self.after = self.after - frames;
return Some(chunk)
}
// If the voice will start playing within the current buffer,
// subtract the remaining number of wait frames.
if self.after > 0 && self.after < frames {
chunk = vec![vec![0.0;self.after];self.sample.channels.len()];
frames = frames - self.after;
self.after = 0;
}
if self.position < self.sample.end {
let start = self.position.min(self.sample.end);
let end = (self.position + frames).min(self.sample.end);
for (i, channel) in self.sample.channels.iter().enumerate() {
chunk[i].extend_from_slice(&channel[start..end]);
};
self.position = self.position + frames;
Some(chunk)
} else {
None
}
}
}
impl Sample {
pub fn new (name: &str, start: usize, end: usize, channels: Vec<Vec<f32>>) -> Arc<Self> {
Arc::new(Self { name: name.to_string(), start, end, channels })
}
pub fn play (self: &Arc<Self>, after: usize) -> Voice {
Voice { sample: self.clone(), after, position: self.start }
}
}
pub struct Sampler {
pub name: String,
pub cursor: (usize, usize),
@ -90,11 +38,11 @@ impl Sampler {
name: &str, samples: Option<BTreeMap<u7, Arc<Sample>>>,
) -> Usually<JackDevice> {
Jack::new(name)?
.register_midi_in("midi")?
.register_audio_in("recL")?
.register_audio_in("recR")?
.register_audio_out("outL")?
.register_audio_out("outR")?
.midi_in("midi")
.audio_in("recL")
.audio_in("recR")
.audio_out("outL")
.audio_out("outR")
.run(|ports|Box::new(Self {
name: name.into(),
cursor: (0, 0),
@ -168,3 +116,55 @@ impl Sampler {
}
};
}
pub struct Sample {
pub name: String,
pub start: usize,
pub end: usize,
pub channels: Vec<Vec<f32>>,
}
impl Sample {
pub fn new (name: &str, start: usize, end: usize, channels: Vec<Vec<f32>>) -> Arc<Self> {
Arc::new(Self { name: name.to_string(), start, end, channels })
}
pub fn play (self: &Arc<Self>, after: usize) -> Voice {
Voice { sample: self.clone(), after, position: self.start }
}
}
pub struct Voice {
pub sample: Arc<Sample>,
pub after: usize,
pub position: usize,
}
impl Voice {
pub fn chunk (&mut self, mut frames: usize) -> Option<Vec<Vec<f32>>> {
// Create output buffer for each channel
let mut chunk = vec![vec![];self.sample.channels.len()];
// If it's not time to play yet, count down
if self.after >= frames {
self.after = self.after - frames;
return Some(chunk)
}
// If the voice will start playing within the current buffer,
// subtract the remaining number of wait frames.
if self.after > 0 && self.after < frames {
chunk = vec![vec![0.0;self.after];self.sample.channels.len()];
frames = frames - self.after;
self.after = 0;
}
if self.position < self.sample.end {
let start = self.position.min(self.sample.end);
let end = (self.position + frames).min(self.sample.end);
for (i, channel) in self.sample.channels.iter().enumerate() {
chunk[i].extend_from_slice(&channel[start..end]);
};
self.position = self.position + frames;
Some(chunk)
} else {
None
}
}
}