wip: adding samples

This commit is contained in:
🪞👃🪞 2024-07-12 18:41:00 +03:00
parent 45021bc77a
commit c81de2123f
2 changed files with 46 additions and 53 deletions

View file

@ -5,15 +5,16 @@ pub fn handle (state: &mut Sampler, event: &AppEvent) -> Usually<bool> {
} }
pub const KEYMAP: &'static [KeyBinding<Sampler>] = keymap!(Sampler { pub const KEYMAP: &'static [KeyBinding<Sampler>] = keymap!(Sampler {
[Up, NONE, "cursor_up", "move cursor up", cursor_up], [Up, NONE, "cursor_up", "move cursor up", cursor_up],
[Down, NONE, "cursor_down", "move cursor down", cursor_down], [Down, NONE, "cursor_down", "move cursor down", cursor_down],
[Char('t'), NONE, "trigger", "play current sample", trigger], [Char('t'), NONE, "sample_play", "play current sample", trigger],
[Enter, NONE, "select", "select item under cursor", select], [Char('a'), NONE, "sample_add", "add a new sample", add_sample],
[Enter, NONE, "sample_edit", "edit selected sample", edit_sample],
}); });
fn cursor_up (state: &mut Sampler) -> Usually<bool> { fn cursor_up (state: &mut Sampler) -> Usually<bool> {
state.cursor.0 = if state.cursor.0 == 0 { state.cursor.0 = if state.cursor.0 == 0 {
state.samples.len() - 1 state.mapped.len() - 1
} else { } else {
state.cursor.0 - 1 state.cursor.0 - 1
}; };
@ -21,24 +22,25 @@ fn cursor_up (state: &mut Sampler) -> Usually<bool> {
} }
fn cursor_down (state: &mut Sampler) -> Usually<bool> { fn cursor_down (state: &mut Sampler) -> Usually<bool> {
state.cursor.0 = (state.cursor.0 + 1) % state.samples.len(); state.cursor.0 = (state.cursor.0 + 1) % state.mapped.len();
Ok(true) Ok(true)
} }
fn trigger (state: &mut Sampler) -> Usually<bool> { fn trigger (state: &mut Sampler) -> Usually<bool> {
for (i, sample) in state.samples.values().enumerate() { if let Some(sample) = state.sample() {
if i == state.cursor.0 { state.voices.push(sample.play(0, &100.into()))
state.voices.push(sample.play(0, &100.into()))
}
} }
Ok(true) Ok(true)
} }
fn select (state: &mut Sampler) -> Usually<bool> { fn add_sample (state: &mut Sampler) -> Usually<bool> {
for (i, _sample) in state.samples.values().enumerate() { state.unmapped.push(Sample::new("", 0, 0, vec![]));
if i == state.cursor.0 { Ok(true)
//state.voices.push(sample.play(0)) }
}
fn edit_sample (state: &mut Sampler) -> Usually<bool> {
if let Some(sample) = state.sample() {
state.editing = Some(sample.clone());
} }
Ok(true) Ok(true)
} }

View file

@ -1,12 +1,14 @@
use crate::core::*; use crate::core::*;
pub struct Sampler { pub struct Sampler {
pub name: String, pub name: String,
pub cursor: (usize, usize), pub cursor: (usize, usize),
pub samples: BTreeMap<u7, Arc<Sample>>, pub editing: Option<Arc<Sample>>,
pub voices: Vec<Voice>, pub mapped: BTreeMap<u7, Arc<Sample>>,
pub ports: JackPorts, pub unmapped: Vec<Arc<Sample>>,
pub buffer: Vec<Vec<f32>>, pub voices: Vec<Voice>,
pub ports: JackPorts,
pub buffer: Vec<Vec<f32>>,
pub output_gain: f32 pub output_gain: f32
} }
render!(Sampler |self, buf, area| { render!(Sampler |self, buf, area| {
@ -15,7 +17,7 @@ render!(Sampler |self, buf, area| {
let title = format!(" {} ({})", self.name, self.voices.len()); let title = format!(" {} ({})", self.name, self.voices.len());
title.blit(buf, x+1, y, Some(style.white().bold().not_dim()))?; title.blit(buf, x+1, y, Some(style.white().bold().not_dim()))?;
let mut width = title.len() + 2; let mut width = title.len() + 2;
for (i, (note, sample)) in self.samples.iter().enumerate() { for (i, (note, sample)) in self.mapped.iter().enumerate() {
let style = if i == self.cursor.0 { let style = if i == self.cursor.0 {
Style::default().green() Style::default().green()
} else { } else {
@ -35,38 +37,15 @@ render!(Sampler |self, buf, area| {
label2.blit(buf, x+3+label1.len()as u16, y1, Some(style))?; label2.blit(buf, x+3+label1.len()as u16, y1, Some(style))?;
width = width.max(label1.len() + label2.len() + 4); width = width.max(label1.len() + label2.len() + 4);
} }
let height = ((2 + self.samples.len()) as u16).min(height); let height = ((2 + self.mapped.len()) as u16).min(height);
Ok(Rect { x, y, width: (width as u16).min(area.width), height }) Ok(Rect { x, y, width: (width as u16).min(area.width), height })
}); });
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 {
//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 Sampler { impl Sampler {
pub fn new ( pub fn new (name: &str, mapped: Option<BTreeMap<u7, Arc<Sample>>>) -> Usually<JackDevice> {
name: &str, samples: Option<BTreeMap<u7, Arc<Sample>>>,
) -> Usually<JackDevice> {
Jack::new(name)? Jack::new(name)?
.midi_in("midi") .midi_in("midi")
.audio_in("recL") .audio_in("recL")
@ -74,16 +53,28 @@ impl Sampler {
.audio_out("outL") .audio_out("outL")
.audio_out("outR") .audio_out("outR")
.run(|ports|Box::new(Self { .run(|ports|Box::new(Self {
name: name.into(), name: name.into(),
cursor: (0, 0), cursor: (0, 0),
samples: samples.unwrap_or(BTreeMap::new()), editing: None,
voices: vec![], mapped: mapped.unwrap_or_else(||BTreeMap::new()),
unmapped: vec![],
voices: vec![],
ports, ports,
buffer: vec![vec![0.0;16384];2], buffer: vec![vec![0.0;16384];2],
output_gain: 0.5, output_gain: 0.5,
})) }))
} }
/// Immutable reference to sample at cursor.
pub fn sample (&self) -> Option<&Arc<Sample>> {
for (i, sample) in self.mapped.values().enumerate() {
if i == self.cursor.0 {
return Some(sample)
}
}
None
}
pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control { pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
self.process_midi_in(scope); self.process_midi_in(scope);
self.clear_output_buffer(); self.clear_output_buffer();
@ -96,7 +87,7 @@ impl Sampler {
for RawMidi { time, bytes } in self.ports.midi_ins.get("midi").unwrap().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, ref vel } = message { if let MidiMessage::NoteOn { ref key, ref vel } = message {
if let Some(sample) = self.samples.get(key) { if let Some(sample) = self.mapped.get(key) {
self.voices.push(sample.play(time as usize, vel)); self.voices.push(sample.play(time as usize, vel));
} }
} }