mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
wip: refactor command definitions
This commit is contained in:
parent
1b926b0338
commit
9f9de3fafb
9 changed files with 489 additions and 343 deletions
|
|
@ -1,72 +1,165 @@
|
|||
use crate::*;
|
||||
|
||||
expose!([self: Sampler]
|
||||
([Arc<str>])
|
||||
([MaybeSample])
|
||||
([PathBuf])
|
||||
([f32])
|
||||
([u7]
|
||||
(":pitch" (self.note_pos() as u8).into()) // TODO
|
||||
(":sample" (self.note_pos() as u8).into()))
|
||||
([usize]
|
||||
(":sample-up" self.note_pos().min(119) + 8)
|
||||
(":sample-down" self.note_pos().max(8) - 8)
|
||||
(":sample-left" self.note_pos().min(126) + 1)
|
||||
(":sample-right" self.note_pos().max(1) - 1)));
|
||||
|
||||
impose!([state: Sampler]
|
||||
(FileBrowserCommand:
|
||||
("begin" [] Some(Self::Begin))
|
||||
("cancel" [] Some(Self::Cancel))
|
||||
("confirm" [] Some(Self::Confirm))
|
||||
("select" [i: usize] Some(Self::Select(i.expect("no index"))))
|
||||
("chdir" [p: PathBuf] Some(Self::Chdir(p.expect("no path"))))
|
||||
("filter" [f: Arc<str>] Some(Self::Filter(f.expect("no filter")))))
|
||||
(SamplerCommand:
|
||||
("import" [,..a]
|
||||
FileBrowserCommand::try_from_expr(state, a).map(Self::Import))
|
||||
("select" [i: usize]
|
||||
Some(Self::Select(i.expect("no index"))))
|
||||
("record/begin" [i: u7]
|
||||
Some(Self::RecordBegin(i.expect("no index"))))
|
||||
("record/cancel" []
|
||||
Some(Self::RecordCancel))
|
||||
("record/finish" []
|
||||
Some(Self::RecordFinish))
|
||||
("set/sample" [i: u7, s: MaybeSample]
|
||||
Some(Self::SetSample(i.expect("no index"), s.expect("no sampler"))))
|
||||
("set/start" [i: u7, s: usize]
|
||||
Some(Self::SetStart(i.expect("no index"), s.expect("no start"))))
|
||||
("set/gain" [i: u7, g: f32]
|
||||
Some(Self::SetGain(i.expect("no index"), g.expect("no gain"))))
|
||||
("note/on" [p: u7, v: u7]
|
||||
Some(Self::NoteOn(p.expect("no pitch"), v.expect("no velocity"))))
|
||||
("note/off" [p: u7]
|
||||
Some(Self::NoteOff(p.expect("no pitch"))))));
|
||||
|
||||
macro_rules! cmd { ($cmd:expr) => {{ $cmd; None }}; }
|
||||
macro_rules! cmd_todo { ($msg:literal) => {{ println!($msg); None }}; }
|
||||
|
||||
defcom!([self, state: Sampler]
|
||||
(SamplerCommand
|
||||
(Select [i: usize] Some(Self::Select(state.set_note_pos(i))))
|
||||
(RecordBegin [p: u7] cmd!(state.begin_recording(p.as_int() as usize)))
|
||||
(RecordCancel [] cmd!(state.cancel_recording()))
|
||||
(RecordFinish [] cmd!(state.finish_recording()))
|
||||
(SetStart [p: u7, frame: usize] cmd_todo!("\n\rtodo: {self:?}"))
|
||||
(SetGain [p: u7, gain: f32] cmd_todo!("\n\rtodo: {self:?}"))
|
||||
(NoteOn [p: u7, velocity: u7] cmd_todo!("\n\rtodo: {self:?}"))
|
||||
(NoteOff [p: u7] cmd_todo!("\n\rtodo: {self:?}"))
|
||||
(SetSample [p: u7, s: MaybeSample] Some(Self::SetSample(p, state.set_sample(p, s))))
|
||||
(Import [c: FileBrowserCommand] match c {
|
||||
FileBrowserCommand::Begin => {
|
||||
//let voices = &state.state.voices;
|
||||
//let sample = Arc::new(RwLock::new(Sample::new("", 0, 0, vec![])));
|
||||
state.mode = Some(SamplerMode::Import(0, FileBrowser::new(None)?));
|
||||
None
|
||||
},
|
||||
_ => {
|
||||
println!("\n\rtodo: import: filebrowser: {c:?}");
|
||||
None
|
||||
}
|
||||
})));
|
||||
#[tengri_proc::expose]
|
||||
impl Sampler {
|
||||
//fn file_browser_filter (&self) -> Arc<str> {
|
||||
//todo!()
|
||||
//}
|
||||
//fn file_browser_path (&self) -> PathBuf {
|
||||
//todo!();
|
||||
//}
|
||||
///// Immutable reference to sample at cursor.
|
||||
//fn sample_selected (&self) -> MaybeSample {
|
||||
//for (i, sample) in self.mapped.iter().enumerate() {
|
||||
//if i == self.cursor().0 {
|
||||
//return sample.as_ref()
|
||||
//}
|
||||
//}
|
||||
//for (i, sample) in self.unmapped.iter().enumerate() {
|
||||
//if i + self.mapped.len() == self.cursor().0 {
|
||||
//return Some(sample)
|
||||
//}
|
||||
//}
|
||||
//None
|
||||
//}
|
||||
//fn sample_gain (&self) -> f32 {
|
||||
//todo!()
|
||||
//}
|
||||
//fn sample_above () -> usize {
|
||||
//self.note_pos().min(119) + 8
|
||||
//}
|
||||
//fn sample_below () -> usize {
|
||||
//self.note_pos().max(8) - 8
|
||||
//}
|
||||
//fn sample_to_left () -> usize {
|
||||
//self.note_pos().min(126) + 1
|
||||
//}
|
||||
//fn sample_to_right () -> usize {
|
||||
//self.note_pos().max(1) - 1
|
||||
//}
|
||||
//fn selected_pitch () -> u7 {
|
||||
//(self.note_pos() as u8).into() // TODO
|
||||
//}
|
||||
//fn selected_sample () -> u7 { // TODO
|
||||
//(self.note_pos() as u8).into()
|
||||
//}
|
||||
}
|
||||
|
||||
#[tengri_proc::command(Sampler)]
|
||||
impl SamplerCommand {
|
||||
//fn select (&self, state: &mut Sampler, i: usize) -> Option<Self> {
|
||||
//Self::Select(state.set_note_pos(i))
|
||||
//}
|
||||
///// Assign sample to pitch
|
||||
//fn set (&self, pitch: u7, sample: MaybeSample) -> Option<Self> {
|
||||
//let i = pitch.as_int() as usize;
|
||||
//let old = self.mapped[i].clone();
|
||||
//self.mapped[i] = sample;
|
||||
//Some(Self::Set(old))
|
||||
//}
|
||||
//fn record_begin (&self, state: &mut Sampler, pitch: u7) -> Option<Self> {
|
||||
//self.recording = Some((
|
||||
//pitch.as_int() as usize,
|
||||
//Arc::new(RwLock::new(Sample::new("Sample", 0, 0, vec![vec![];self.audio_ins.len()])))
|
||||
//));
|
||||
//None
|
||||
//}
|
||||
//fn record_cancel (&self, state: &mut Sampler) -> Option<Self> {
|
||||
//self.recording = None;
|
||||
//None
|
||||
//}
|
||||
//fn record_finish (&self, state: &mut Sampler) -> Option<Self> {
|
||||
//let recording = self.recording.take();
|
||||
//let _sample = if let Some((index, sample)) = recording {
|
||||
//let old = self.mapped[index].clone();
|
||||
//self.mapped[index] = Some(sample);
|
||||
//old
|
||||
//} else {
|
||||
//None
|
||||
//};
|
||||
//None
|
||||
//}
|
||||
//fn set_start (&self, state: &mut Sampler, pitch: u7, frame: usize) -> Option<Self> {
|
||||
//todo!()
|
||||
//}
|
||||
//fn set_gain (&self, state: &mut Sampler, pitch: u7, g: f32) -> Option<Self> {
|
||||
//todo!()
|
||||
//}
|
||||
//fn note_on (&self, state: &mut Sampler, pitch: u7, v: u7) -> Option<Self> {
|
||||
//todo!()
|
||||
//}
|
||||
//fn note_off (&self, state: &mut Sampler, pitch: u7) -> Option<Self> {
|
||||
//todo!()
|
||||
//}
|
||||
//fn set_sample (&self, state: &mut Sampler, pitch: u7, s: MaybeSample) -> Option<Self> {
|
||||
//Some(Self::SetSample(p, state.set_sample(p, s)))
|
||||
//}
|
||||
//fn import (&self, state: &mut Sampler, c: FileBrowserCommand) -> Option<Self> {
|
||||
//match c {
|
||||
//FileBrowserCommand::Begin => {
|
||||
////let voices = &state.state.voices;
|
||||
////let sample = Arc::new(RwLock::new(Sample::new("", 0, 0, vec![])));
|
||||
//state.mode = Some(SamplerMode::Import(0, FileBrowser::new(None)?));
|
||||
//None
|
||||
//},
|
||||
//_ => {
|
||||
//println!("\n\rtodo: import: filebrowser: {c:?}");
|
||||
//None
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
////(Select [i: usize] Some(Self::Select(state.set_note_pos(i))))
|
||||
////(RecordBegin [p: u7] cmd!(state.begin_recording(p.as_int() as usize)))
|
||||
////(RecordCancel [] cmd!(state.cancel_recording()))
|
||||
////(RecordFinish [] cmd!(state.finish_recording()))
|
||||
////(SetStart [p: u7, frame: usize] cmd_todo!("\n\rtodo: {self:?}"))
|
||||
////(SetGain [p: u7, gain: f32] cmd_todo!("\n\rtodo: {self:?}"))
|
||||
////(NoteOn [p: u7, velocity: u7] cmd_todo!("\n\rtodo: {self:?}"))
|
||||
////(NoteOff [p: u7] cmd_todo!("\n\rtodo: {self:?}"))
|
||||
////(SetSample [p: u7, s: MaybeSample] Some(Self::SetSample(p, state.set_sample(p, s))))
|
||||
////(Import [c: FileBrowserCommand] match c {
|
||||
////FileBrowserCommand::Begin => {
|
||||
//////let voices = &state.state.voices;
|
||||
//////let sample = Arc::new(RwLock::new(Sample::new("", 0, 0, vec![])));
|
||||
////state.mode = Some(SamplerMode::Import(0, FileBrowser::new(None)?));
|
||||
////None
|
||||
////},
|
||||
////_ => {
|
||||
////println!("\n\rtodo: import: filebrowser: {c:?}");
|
||||
////None
|
||||
////}
|
||||
////})));
|
||||
////("import" [,..a]
|
||||
////FileBrowserCommand::try_from_expr(state, a).map(Self::Import))
|
||||
////("select" [i: usize]
|
||||
////Some(Self::Select(i.expect("no index"))))
|
||||
////("record/begin" [i: u7]
|
||||
////Some(Self::RecordBegin(i.expect("no index"))))
|
||||
////("record/cancel" []
|
||||
////Some(Self::RecordCancel))
|
||||
////("record/finish" []
|
||||
////Some(Self::RecordFinish))
|
||||
////("set/sample" [i: u7, s: MaybeSample]
|
||||
////Some(Self::SetSample(i.expect("no index"), s.expect("no sampler"))))
|
||||
////("set/start" [i: u7, s: usize]
|
||||
////Some(Self::SetStart(i.expect("no index"), s.expect("no start"))))
|
||||
////("set/gain" [i: u7, g: f32]
|
||||
////Some(Self::SetGain(i.expect("no index"), g.expect("no gain"))))
|
||||
////("note/on" [p: u7, v: u7]
|
||||
////Some(Self::NoteOn(p.expect("no pitch"), v.expect("no velocity"))))
|
||||
////("note/off" [p: u7]
|
||||
////Some(Self::NoteOff(p.expect("no pitch"))))));
|
||||
}
|
||||
|
||||
#[tengri_proc::command(Sampler)]
|
||||
impl FileBrowserCommand {
|
||||
//("begin" [] Some(Self::Begin))
|
||||
//("cancel" [] Some(Self::Cancel))
|
||||
//("confirm" [] Some(Self::Confirm))
|
||||
//("select" [i: usize] Some(Self::Select(i.expect("no index"))))
|
||||
//("chdir" [p: PathBuf] Some(Self::Chdir(p.expect("no path"))))
|
||||
//("filter" [f: Arc<str>] Some(Self::Filter(f.expect("no filter")))))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,50 +77,10 @@ impl Sampler {
|
|||
..Default::default()
|
||||
})
|
||||
}
|
||||
pub fn cancel_recording (&mut self) {
|
||||
self.recording = None;
|
||||
}
|
||||
pub fn begin_recording (&mut self, index: usize) {
|
||||
self.recording = Some((
|
||||
index,
|
||||
Arc::new(RwLock::new(Sample::new("Sample", 0, 0, vec![vec![];self.audio_ins.len()])))
|
||||
));
|
||||
}
|
||||
pub fn finish_recording (&mut self) -> MaybeSample {
|
||||
let recording = self.recording.take();
|
||||
if let Some((index, sample)) = recording {
|
||||
let old = self.mapped[index].clone();
|
||||
self.mapped[index] = Some(sample);
|
||||
old
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
/// Immutable reference to sample at cursor.
|
||||
pub fn sample (&self) -> Option<&Arc<RwLock<Sample>>> {
|
||||
for (i, sample) in self.mapped.iter().enumerate() {
|
||||
if i == self.cursor().0 {
|
||||
return sample.as_ref()
|
||||
}
|
||||
}
|
||||
for (i, sample) in self.unmapped.iter().enumerate() {
|
||||
if i + self.mapped.len() == self.cursor().0 {
|
||||
return Some(sample)
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
/// Value of cursor
|
||||
pub fn cursor (&self) -> (usize, usize) {
|
||||
(self.cursor.0.load(Relaxed), self.cursor.1.load(Relaxed))
|
||||
}
|
||||
/// Assign sample to pitch
|
||||
pub fn set_sample (&mut self, pitch: u7, sample: MaybeSample) -> MaybeSample {
|
||||
let i = pitch.as_int() as usize;
|
||||
let old = self.mapped[i].clone();
|
||||
self.mapped[i] = sample;
|
||||
old
|
||||
}
|
||||
}
|
||||
|
||||
impl NoteRange for Sampler {
|
||||
|
|
@ -189,4 +149,3 @@ pub enum SamplerMode {
|
|||
// Load sample from path
|
||||
Import(usize, FileBrowser),
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -80,6 +80,7 @@ impl Sampler {
|
|||
Tui::fg_bg(fg, bg, format!("{note:3} {}", self.view_list_item(note, compact)))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn view_list_item (&self, note: usize, compact: bool) -> String {
|
||||
if compact {
|
||||
String::default()
|
||||
|
|
@ -87,6 +88,7 @@ impl Sampler {
|
|||
draw_list_item(&self.mapped[note])
|
||||
}
|
||||
}
|
||||
|
||||
pub fn view_sample (&self, note_pt: usize) -> impl Content<TuiOut> + use<'_> {
|
||||
Outer(true, Style::default().fg(Tui::g(96))).enclose(draw_viewer(if let Some((_, sample)) = &self.recording {
|
||||
Some(sample)
|
||||
|
|
@ -96,6 +98,7 @@ impl Sampler {
|
|||
None
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn status (&self, index: usize) -> impl Content<TuiOut> {
|
||||
draw_status(self.mapped[index].as_ref())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue