wip: nermalize

This commit is contained in:
okay stopped screaming 2026-03-21 23:12:30 +02:00
parent 915e13aec8
commit 35197fb826
12 changed files with 4649 additions and 4718 deletions

View file

@ -1,3 +1,7 @@
use ::std::sync::{Arc, RwLock, atomic::{AtomicUsize, Ordering::*}};
use crate::sequence::MidiClip;
use crate::sample::Sample;
/// Browses for files to load/save.
///
/// ```
@ -95,3 +99,253 @@ pub struct PoolView<'a>(pub &'a Pool);
/// Editing the number of ticks
Tick,
}
has_clip!(|self: Pool|self.clips().get(self.clip_index()).map(|c|c.clone()));
impl_has_clips!(|self: Pool|self.clips);
impl_from!(Pool: |clip:&Arc<RwLock<MidiClip>>|{
let model = Self::default();
model.clips.write().unwrap().push(clip.clone());
model.clip.store(1, Relaxed);
model
});
impl_default!(Pool: Self {
browse: None,
clip: 0.into(),
clips: Arc::from(RwLock::from(vec![])),
mode: None,
samples: Arc::from(RwLock::from(vec![])),
visible: true,
});
impl Pool {
pub fn clip_index (&self) -> usize {
self.clip.load(Relaxed)
}
pub fn set_clip_index (&self, value: usize) {
self.clip.store(value, Relaxed);
}
pub fn mode (&self) -> &Option<PoolMode> {
&self.mode
}
pub fn mode_mut (&mut self) -> &mut Option<PoolMode> {
&mut self.mode
}
pub fn begin_clip_length (&mut self) {
let length = self.clips()[self.clip_index()].read().unwrap().length;
*self.mode_mut() = Some(PoolMode::Length(
self.clip_index(),
length,
ClipLengthFocus::Bar
));
}
pub fn begin_clip_rename (&mut self) {
let name = self.clips()[self.clip_index()].read().unwrap().name.clone();
*self.mode_mut() = Some(PoolMode::Rename(
self.clip_index(),
name
));
}
pub fn begin_import (&mut self) -> Usually<()> {
*self.mode_mut() = Some(PoolMode::Import(
self.clip_index(),
Browse::new(None)?
));
Ok(())
}
pub fn begin_export (&mut self) -> Usually<()> {
*self.mode_mut() = Some(PoolMode::Export(
self.clip_index(),
Browse::new(None)?
));
Ok(())
}
pub fn new_clip (&self) -> MidiClip {
MidiClip::new("Clip", true, 4 * PPQ, None, Some(ItemTheme::random()))
}
pub fn cloned_clip (&self) -> MidiClip {
let index = self.clip_index();
let mut clip = self.clips()[index].read().unwrap().duplicate();
clip.color = ItemTheme::random_near(clip.color, 0.25);
clip
}
pub fn add_new_clip (&self) -> (usize, Arc<RwLock<MidiClip>>) {
let clip = Arc::new(RwLock::new(self.new_clip()));
let index = {
let mut clips = self.clips.write().unwrap();
clips.push(clip.clone());
clips.len().saturating_sub(1)
};
self.clip.store(index, Relaxed);
(index, clip)
}
pub fn delete_clip (&mut self, clip: &MidiClip) -> bool {
let index = self.clips.read().unwrap().iter().position(|x|*x.read().unwrap()==*clip);
if let Some(index) = index {
self.clips.write().unwrap().remove(index);
return true
}
false
}
}
impl ClipLengthFocus {
pub fn next (&mut self) {
use ClipLengthFocus::*;
*self = match self { Bar => Beat, Beat => Tick, Tick => Bar, }
}
pub fn prev (&mut self) {
use ClipLengthFocus::*;
*self = match self { Bar => Tick, Beat => Bar, Tick => Beat, }
}
}
impl ClipLength {
pub fn _new (pulses: usize, focus: Option<ClipLengthFocus>) -> Self {
Self { ppq: PPQ, bpb: 4, pulses, focus }
}
pub fn bars (&self) -> usize {
self.pulses / (self.bpb * self.ppq)
}
pub fn beats (&self) -> usize {
(self.pulses % (self.bpb * self.ppq)) / self.ppq
}
pub fn ticks (&self) -> usize {
self.pulses % self.ppq
}
pub fn bars_string (&self) -> Arc<str> {
format!("{}", self.bars()).into()
}
pub fn beats_string (&self) -> Arc<str> {
format!("{}", self.beats()).into()
}
pub fn ticks_string (&self) -> Arc<str> {
format!("{:>02}", self.ticks()).into()
}
}
impl Pool {
fn _todo_usize_ (&self) -> usize { todo!() }
fn _todo_bool_ (&self) -> bool { todo!() }
fn _todo_clip_ (&self) -> MidiClip { todo!() }
fn _todo_path_ (&self) -> PathBuf { todo!() }
fn _todo_color_ (&self) -> ItemColor { todo!() }
fn _todo_str_ (&self) -> Arc<str> { todo!() }
fn _clip_new (&self) -> MidiClip { self.new_clip() }
fn _clip_cloned (&self) -> MidiClip { self.cloned_clip() }
fn _clip_index_current (&self) -> usize { 0 }
fn _clip_index_after (&self) -> usize { 0 }
fn _clip_index_previous (&self) -> usize { 0 }
fn _clip_index_next (&self) -> usize { 0 }
fn _color_random (&self) -> ItemColor { ItemColor::random() }
}
impl<'a> PoolView<'a> {
fn tui (&self) -> impl Draw<Tui> {
let Self(pool) = self;
//let color = self.1.clip().map(|c|c.read().unwrap().color).unwrap_or_else(||Tui::g(32).into());
//let on_bg = |x|x;//below(Repeat(" "), Tui::bg(color.darkest.rgb, x));
//let border = |x|x;//Outer(Style::default().fg(color.dark.rgb).bg(color.darkest.rgb)).enclose(x);
//let height = pool.clips.read().unwrap().len() as u16;
w_exact(20, h_full(origin_n(iter(
||pool.clips().clone().into_iter(),
move|clip: Arc<RwLock<MidiClip>>, i: usize|{
let item_height = 1;
let item_offset = i as u16 * item_height;
let selected = i == pool.clip_index();
let MidiClip { ref name, color, length, .. } = *clip.read().unwrap();
let bg = if selected { color.light.rgb } else { color.base.rgb };
let fg = color.lightest.rgb;
let name = if false { format!(" {i:>3}") } else { format!(" {i:>3} {name}") };
let length = if false { String::default() } else { format!("{length} ") };
h_exact(1, iter_south(item_offset, item_height, Tui::bg(bg, below!(
w_full(origin_w(Tui::fg(fg, Tui::bold(selected, name)))),
w_full(origin_e(Tui::fg(fg, Tui::bold(selected, length)))),
w_full(origin_w(When::new(selected, Tui::bold(true, Tui::fg(Tui::g(255), ""))))),
w_full(origin_e(When::new(selected, Tui::bold(true, Tui::fg(Tui::g(255), ""))))),
))))
}))))
}
}
impl ClipLength {
fn tui (&self) -> impl Draw<Tui> {
use ClipLengthFocus::*;
let bars = ||self.bars_string();
let beats = ||self.beats_string();
let ticks = ||self.ticks_string();
match self.focus {
None => east!(" ", bars(), ".", beats(), ".", ticks()),
Some(Bar) => east!("[", bars(), "]", beats(), ".", ticks()),
Some(Beat) => east!(" ", bars(), "[", beats(), "]", ticks()),
Some(Tick) => east!(" ", bars(), ".", beats(), "[", ticks()),
}
}
}
impl Browse {
pub fn new (cwd: Option<PathBuf>) -> Usually<Self> {
let cwd = if let Some(cwd) = cwd { cwd } else { std::env::current_dir()? };
let mut dirs = vec![];
let mut files = vec![];
for entry in std::fs::read_dir(&cwd)? {
let entry = entry?;
let name = entry.file_name();
let decoded = name.clone().into_string().unwrap_or_else(|_|"<unreadable>".to_string());
let meta = entry.metadata()?;
if meta.is_dir() {
dirs.push((name, format!("📁 {decoded}")));
} else if meta.is_file() {
files.push((name, format!("📄 {decoded}")));
}
}
Ok(Self { cwd, dirs, files, ..Default::default() })
}
pub fn chdir (&self) -> Usually<Self> { Self::new(Some(self.path())) }
pub fn len (&self) -> usize { self.dirs.len() + self.files.len() }
pub fn is_dir (&self) -> bool { self.index < self.dirs.len() }
pub fn is_file (&self) -> bool { self.index >= self.dirs.len() }
pub fn path (&self) -> PathBuf {
self.cwd.join(if self.is_dir() {
&self.dirs[self.index].0
} else if self.is_file() {
&self.files[self.index - self.dirs.len()].0
} else {
unreachable!()
})
}
fn _todo_stub_path_buf (&self) -> PathBuf { todo!() }
fn _todo_stub_usize (&self) -> usize { todo!() }
fn _todo_stub_arc_str (&self) -> Arc<str> { todo!() }
}
impl Browse {
fn tui (&self) -> impl Draw<Tui> {
iter_south(1, ||EntriesIterator {
offset: 0,
index: 0,
length: self.dirs.len() + self.files.len(),
browser: self,
}, |entry, _index|w_full(origin_w(entry)))
}
}
impl<'a> Iterator for EntriesIterator<'a> {
type Item = Modify<&'a str>;
fn next (&mut self) -> Option<Self::Item> {
let dirs = self.browser.dirs.len();
let files = self.browser.files.len();
let index = self.index;
if self.index < dirs {
self.index += 1;
Some(Tui::bold(true, self.browser.dirs[index].1.as_str()))
} else if self.index < dirs + files {
self.index += 1;
Some(Tui::bold(false, self.browser.files[index - dirs].1.as_str()))
} else {
None
}
}
}
impl PartialEq for BrowseTarget {
fn eq (&self, other: &Self) -> bool {
match self {
Self::ImportSample(_) => false,
Self::ExportSample(_) => false,
Self::ImportClip(_) => false,
Self::ExportClip(_) => false,
#[allow(unused)] t => matches!(other, t)
}
}
}