mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
big ass refactor (rip client)
This commit is contained in:
parent
94c1f83ef2
commit
8c3cf53c67
56 changed files with 2232 additions and 1891 deletions
175
src/model/sampler.rs
Normal file
175
src/model/sampler.rs
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
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>>,
|
||||
}
|
||||
|
||||
pub struct Sampler {
|
||||
pub name: String,
|
||||
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
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Sampler {
|
||||
pub fn new (
|
||||
name: &str,
|
||||
samples: Option<BTreeMap<u7, Arc<Sample>>>,
|
||||
) -> Result<DynamicDevice<Self>, Box<dyn Error>> {
|
||||
let (client, _) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
||||
DynamicDevice::new(
|
||||
crate::view::sampler::render,
|
||||
crate::control::sampler::handle,
|
||||
Self::process,
|
||||
Self {
|
||||
name: name.into(),
|
||||
cursor: (0, 0),
|
||||
samples: samples.unwrap_or(BTreeMap::new()),
|
||||
voices: vec![],
|
||||
midi_in: client.register_port("midi", ::jack::MidiIn::default())?,
|
||||
audio_ins: vec![
|
||||
client.register_port("recL", ::jack::AudioIn::default())?,
|
||||
client.register_port("recR", ::jack::AudioIn::default())?,
|
||||
],
|
||||
audio_outs: vec![
|
||||
client.register_port("outL", ::jack::AudioOut::default())?,
|
||||
client.register_port("outR", ::jack::AudioOut::default())?,
|
||||
],
|
||||
}).activate(client)
|
||||
}
|
||||
|
||||
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 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) {
|
||||
if let LiveEvent::Midi { message, .. } = LiveEvent::parse(bytes).unwrap() {
|
||||
if let MidiMessage::NoteOn { ref key, .. } = message {
|
||||
if let Some(sample) = self.samples.get(key) {
|
||||
self.voices.push(sample.play(time as usize));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Emit next chunk of each currently playing voice,
|
||||
// dropping voices that have reached their ends.
|
||||
let mut voices = vec![];
|
||||
std::mem::swap(&mut voices, &mut self.voices);
|
||||
loop {
|
||||
if voices.len() < 1 {
|
||||
break
|
||||
}
|
||||
let mut voice = voices.swap_remove(0);
|
||||
if let Some(chunk) = voice.chunk(scope.n_frames() as usize) {
|
||||
for (i, channel) in chunk.iter().enumerate() {
|
||||
let buffer = &mut mixed[i % channel_count];
|
||||
for (i, sample) in channel.iter().enumerate() {
|
||||
buffer[i] += sample;
|
||||
}
|
||||
}
|
||||
self.voices.push(voice);
|
||||
}
|
||||
}
|
||||
// Write output buffer to output ports.
|
||||
for (i, port) in self.audio_outs.iter_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);
|
||||
}
|
||||
}
|
||||
Control::Continue
|
||||
}
|
||||
|
||||
fn load_sample (&mut self, _path: &str) {}
|
||||
}
|
||||
|
||||
impl PortList for Sampler {
|
||||
fn midi_ins (&self) -> Usually<Vec<String>> {
|
||||
Ok(vec![self.midi_in.name()?])
|
||||
}
|
||||
fn audio_ins (&self) -> Usually<Vec<String>> {
|
||||
let mut ports = vec![];
|
||||
for port in self.audio_ins.iter() {
|
||||
ports.push(port.name()?);
|
||||
}
|
||||
Ok(ports)
|
||||
}
|
||||
fn audio_outs (&self) -> Usually<Vec<String>> {
|
||||
let mut ports = vec![];
|
||||
for port in self.audio_outs.iter() {
|
||||
ports.push(port.name()?);
|
||||
}
|
||||
Ok(ports)
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! sample {
|
||||
($note:expr, $name:expr, $src:expr) => {
|
||||
{
|
||||
let mut channels: Vec<wavers::Samples<f32>> = vec![];
|
||||
for channel in wavers::Wav::from_path($src)?.channels() {
|
||||
channels.push(channel);
|
||||
}
|
||||
let mut end = 0;
|
||||
let mut data: Vec<Vec<f32>> = vec![];
|
||||
for samples in channels.iter() {
|
||||
let channel = Vec::from(samples.as_ref());
|
||||
end = end.max(channel.len());
|
||||
data.push(channel);
|
||||
}
|
||||
(u7::from_int_lossy($note).into(), Sample::new($name, 0, end, data).into())
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue