mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
refactor: merge plugin, sampler -> mixer; transport -> sequencer; time -> core
This commit is contained in:
parent
6206a43b4a
commit
a659062dbc
46 changed files with 128 additions and 198 deletions
226
crates/tek_mixer/src/sampler.rs
Normal file
226
crates/tek_mixer/src/sampler.rs
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
use crate::*;
|
||||
|
||||
/// The sampler plugin plays sounds.
|
||||
pub struct Sampler {
|
||||
pub name: String,
|
||||
pub cursor: (usize, usize),
|
||||
pub editing: Option<Arc<RwLock<Sample>>>,
|
||||
pub mapped: BTreeMap<u7, Arc<RwLock<Sample>>>,
|
||||
pub unmapped: Vec<Arc<RwLock<Sample>>>,
|
||||
pub voices: Arc<RwLock<Vec<Voice>>>,
|
||||
pub ports: JackPorts,
|
||||
pub buffer: Vec<Vec<f32>>,
|
||||
pub modal: Arc<Mutex<Option<Box<dyn Exit + Send>>>>,
|
||||
pub output_gain: f32
|
||||
}
|
||||
|
||||
process!(Sampler = Sampler::process);
|
||||
handle!(Sampler |self, event| handle_keymap(self, event, KEYMAP_SAMPLER));
|
||||
render!(Sampler |self, buf, area| {
|
||||
let Rect { x, y, height, .. } = area;
|
||||
let style = Style::default().gray();
|
||||
let title = format!(" {} ({})", self.name, self.voices.read().unwrap().len());
|
||||
title.blit(buf, x+1, y, Some(style.white().bold().not_dim()))?;
|
||||
let mut width = title.len() + 2;
|
||||
let mut y1 = 1;
|
||||
let mut j = 0;
|
||||
for (note, sample) in self.mapped.iter()
|
||||
.map(|(note, sample)|(Some(note), sample))
|
||||
.chain(self.unmapped.iter().map(|sample|(None, sample)))
|
||||
{
|
||||
if y1 >= height {
|
||||
break
|
||||
}
|
||||
let active = j == self.cursor.0;
|
||||
width = width.max(draw_sample(buf, x, y + y1, note, &*sample.read().unwrap(), active)?);
|
||||
y1 = y1 + 1;
|
||||
j = j + 1;
|
||||
}
|
||||
let height = ((2 + y1) as u16).min(height);
|
||||
Ok(Rect { x, y, width: (width as u16).min(area.width), height })
|
||||
});
|
||||
|
||||
fn draw_sample (
|
||||
buf: &mut Buffer, x: u16, y: u16, note: Option<&u7>, sample: &Sample, focus: bool
|
||||
) -> Usually<usize> {
|
||||
let style = if focus { Style::default().green() } else { Style::default() };
|
||||
if focus {
|
||||
"🬴".blit(buf, x+1, y, Some(style.bold()))?;
|
||||
}
|
||||
let label1 = format!("{:3} {:12}",
|
||||
note.map(|n|n.to_string()).unwrap_or(String::default()),
|
||||
sample.name);
|
||||
let label2 = format!("{:>6} {:>6} +0.0",
|
||||
sample.start,
|
||||
sample.end);
|
||||
label1.blit(buf, x+2, y, Some(style.bold()))?;
|
||||
label2.blit(buf, x+3+label1.len()as u16, y, Some(style))?;
|
||||
Ok(label1.len() + label2.len() + 4)
|
||||
}
|
||||
|
||||
/// Key bindings for sampler device.
|
||||
pub const KEYMAP_SAMPLER: &'static [KeyBinding<Sampler>] = keymap!(Sampler {
|
||||
[Up, NONE, "/sampler/cursor/up", "move cursor up", |state: &mut Sampler| {
|
||||
state.cursor.0 = if state.cursor.0 == 0 {
|
||||
state.mapped.len() + state.unmapped.len() - 1
|
||||
} else {
|
||||
state.cursor.0 - 1
|
||||
};
|
||||
Ok(true)
|
||||
}],
|
||||
[Down, NONE, "/sampler/cursor/down", "move cursor down", |state: &mut Sampler| {
|
||||
state.cursor.0 = (state.cursor.0 + 1) % (state.mapped.len() + state.unmapped.len());
|
||||
Ok(true)
|
||||
}],
|
||||
[Char('p'), NONE, "/sampler/play", "play current sample", |state: &mut Sampler| {
|
||||
if let Some(sample) = state.sample() {
|
||||
state.voices.write().unwrap().push(Sample::play(sample, 0, &100.into()));
|
||||
}
|
||||
Ok(true)
|
||||
}],
|
||||
[Char('a'), NONE, "/sampler/add", "add a new sample", |state: &mut Sampler| {
|
||||
let sample = Arc::new(RwLock::new(Sample::new("", 0, 0, vec![])));
|
||||
*state.modal.lock().unwrap() = Some(Exit::boxed(AddSampleModal::new(&sample, &state.voices)?));
|
||||
state.unmapped.push(sample);
|
||||
Ok(true)
|
||||
}],
|
||||
[Char('r'), NONE, "/sampler/replace", "replace selected sample", |state: &mut Sampler| {
|
||||
if let Some(sample) = state.sample() {
|
||||
*state.modal.lock().unwrap() = Some(Exit::boxed(AddSampleModal::new(&sample, &state.voices)?));
|
||||
}
|
||||
Ok(true)
|
||||
}],
|
||||
[Enter, NONE, "/sampler/edit", "edit selected sample", |state: &mut Sampler| {
|
||||
if let Some(sample) = state.sample() {
|
||||
state.editing = Some(sample.clone());
|
||||
}
|
||||
Ok(true)
|
||||
}],
|
||||
});
|
||||
|
||||
impl Sampler {
|
||||
pub fn from_edn <'e> (args: &[Edn<'e>]) -> Usually<JackDevice> {
|
||||
let mut name = String::new();
|
||||
let mut dir = String::new();
|
||||
let mut samples = BTreeMap::new();
|
||||
edn!(edn in args {
|
||||
Edn::Map(map) => {
|
||||
if let Some(Edn::Str(n)) = map.get(&Edn::Key(":name")) {
|
||||
name = String::from(*n);
|
||||
}
|
||||
if let Some(Edn::Str(n)) = map.get(&Edn::Key(":dir")) {
|
||||
dir = String::from(*n);
|
||||
}
|
||||
},
|
||||
Edn::List(args) => match args.get(0) {
|
||||
Some(Edn::Symbol("sample")) => {
|
||||
let (midi, sample) = Sample::from_edn(&dir, &args[1..])?;
|
||||
if let Some(midi) = midi {
|
||||
samples.insert(midi, sample);
|
||||
} else {
|
||||
panic!("sample without midi binding: {}", sample.read().unwrap().name);
|
||||
}
|
||||
},
|
||||
_ => panic!("unexpected in sampler {name}: {args:?}")
|
||||
},
|
||||
_ => panic!("unexpected in sampler {name}: {edn:?}")
|
||||
});
|
||||
Self::new(&name, Some(samples))
|
||||
}
|
||||
|
||||
pub fn new (name: &str, mapped: Option<BTreeMap<u7, Arc<RwLock<Sample>>>>) -> Usually<JackDevice> {
|
||||
Jack::new(name)?
|
||||
.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),
|
||||
editing: None,
|
||||
mapped: mapped.unwrap_or_else(||BTreeMap::new()),
|
||||
unmapped: vec![],
|
||||
voices: Arc::new(RwLock::new(vec![])),
|
||||
ports,
|
||||
buffer: vec![vec![0.0;16384];2],
|
||||
output_gain: 0.5,
|
||||
modal: Default::default()
|
||||
}))
|
||||
}
|
||||
|
||||
/// Immutable reference to sample at cursor.
|
||||
pub fn sample (&self) -> Option<&Arc<RwLock<Sample>>> {
|
||||
for (i, sample) in self.mapped.values().enumerate() {
|
||||
if i == self.cursor.0 {
|
||||
return Some(sample)
|
||||
}
|
||||
}
|
||||
for (i, sample) in self.unmapped.iter().enumerate() {
|
||||
if i + self.mapped.len() == self.cursor.0 {
|
||||
return Some(sample)
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
|
||||
self.process_midi_in(scope);
|
||||
self.clear_output_buffer();
|
||||
self.process_audio_out(scope);
|
||||
self.write_output_buffer(scope);
|
||||
Control::Continue
|
||||
}
|
||||
|
||||
/// Create [Voice]s from [Sample]s in response to MIDI input.
|
||||
fn process_midi_in (&mut self, scope: &ProcessScope) {
|
||||
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, ref vel } = message {
|
||||
if let Some(sample) = self.mapped.get(key) {
|
||||
self.voices.write().unwrap().push(Sample::play(sample, time as usize, vel));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero the output buffer.
|
||||
fn clear_output_buffer (&mut self) {
|
||||
for buffer in self.buffer.iter_mut() {
|
||||
buffer.fill(0.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Mix all currently playing samples into the output.
|
||||
fn process_audio_out (&mut self, scope: &ProcessScope) {
|
||||
let channel_count = self.buffer.len();
|
||||
self.voices.write().unwrap().retain_mut(|voice|{
|
||||
for index in 0..scope.n_frames() as usize {
|
||||
if let Some(frame) = voice.next() {
|
||||
for (channel, sample) in frame.iter().enumerate() {
|
||||
// Averaging mixer:
|
||||
//self.buffer[channel % channel_count][index] = (
|
||||
//(self.buffer[channel % channel_count][index] + sample * self.output_gain) / 2.0
|
||||
//);
|
||||
self.buffer[channel % channel_count][index] +=
|
||||
sample * self.output_gain;
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
});
|
||||
}
|
||||
|
||||
/// Write output buffer to output ports.
|
||||
fn write_output_buffer (&mut self, scope: &ProcessScope) {
|
||||
for (i, port) in self.ports.audio_outs.values_mut().enumerate() {
|
||||
let buffer = &self.buffer[i];
|
||||
for (i, value) in port.as_mut_slice(scope).iter_mut().enumerate() {
|
||||
*value = *buffer.get(i).unwrap_or(&0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue