mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
181 lines
5.8 KiB
Rust
181 lines
5.8 KiB
Rust
use crate::*;
|
|
|
|
pub struct AddSampleModal {
|
|
exited: bool,
|
|
dir: PathBuf,
|
|
subdirs: Vec<OsString>,
|
|
files: Vec<OsString>,
|
|
cursor: usize,
|
|
offset: usize,
|
|
sample: Arc<RwLock<Sample>>,
|
|
voices: Arc<RwLock<Vec<Voice>>>,
|
|
_search: Option<String>,
|
|
}
|
|
|
|
impl AddSampleModal {
|
|
fn exited (&self) -> bool {
|
|
self.exited
|
|
}
|
|
fn exit (&mut self) {
|
|
self.exited = true
|
|
}
|
|
}
|
|
|
|
impl AddSampleModal {
|
|
pub fn new (
|
|
sample: &Arc<RwLock<Sample>>,
|
|
voices: &Arc<RwLock<Vec<Voice>>>
|
|
) -> Usually<Self> {
|
|
let dir = std::env::current_dir()?;
|
|
let (subdirs, files) = scan(&dir)?;
|
|
Ok(Self {
|
|
exited: false,
|
|
dir,
|
|
subdirs,
|
|
files,
|
|
cursor: 0,
|
|
offset: 0,
|
|
sample: sample.clone(),
|
|
voices: voices.clone(),
|
|
_search: None
|
|
})
|
|
}
|
|
fn rescan (&mut self) -> Usually<()> {
|
|
scan(&self.dir).map(|(subdirs, files)|{
|
|
self.subdirs = subdirs;
|
|
self.files = files;
|
|
})
|
|
}
|
|
fn prev (&mut self) {
|
|
self.cursor = self.cursor.saturating_sub(1);
|
|
}
|
|
fn next (&mut self) {
|
|
self.cursor = self.cursor + 1;
|
|
}
|
|
fn try_preview (&mut self) -> Usually<()> {
|
|
if let Some(path) = self.cursor_file() {
|
|
if let Ok(sample) = Sample::from_file(&path) {
|
|
*self.sample.write().unwrap() = sample;
|
|
self.voices.write().unwrap().push(
|
|
Sample::play(&self.sample, 0, &u7::from(100u8))
|
|
);
|
|
}
|
|
//load_sample(&path)?;
|
|
//let src = std::fs::File::open(&path)?;
|
|
//let mss = MediaSourceStream::new(Box::new(src), Default::default());
|
|
//let mut hint = Hint::new();
|
|
//if let Some(ext) = path.extension() {
|
|
//hint.with_extension(&ext.to_string_lossy());
|
|
//}
|
|
//let meta_opts: MetadataOptions = Default::default();
|
|
//let fmt_opts: FormatOptions = Default::default();
|
|
//if let Ok(mut probed) = symphonia::default::get_probe()
|
|
//.format(&hint, mss, &fmt_opts, &meta_opts)
|
|
//{
|
|
//panic!("{:?}", probed.format.metadata());
|
|
//};
|
|
}
|
|
Ok(())
|
|
}
|
|
fn cursor_dir (&self) -> Option<PathBuf> {
|
|
if self.cursor < self.subdirs.len() {
|
|
Some(self.dir.join(&self.subdirs[self.cursor]))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
fn cursor_file (&self) -> Option<PathBuf> {
|
|
if self.cursor < self.subdirs.len() {
|
|
return None
|
|
}
|
|
let index = self.cursor.saturating_sub(self.subdirs.len());
|
|
if index < self.files.len() {
|
|
Some(self.dir.join(&self.files[index]))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
fn pick (&mut self) -> Usually<bool> {
|
|
if self.cursor == 0 {
|
|
if let Some(parent) = self.dir.parent() {
|
|
self.dir = parent.into();
|
|
self.rescan()?;
|
|
self.cursor = 0;
|
|
return Ok(false)
|
|
}
|
|
}
|
|
if let Some(dir) = self.cursor_dir() {
|
|
self.dir = dir;
|
|
self.rescan()?;
|
|
self.cursor = 0;
|
|
return Ok(false)
|
|
}
|
|
if let Some(path) = self.cursor_file() {
|
|
let (end, channels) = read_sample_data(&path.to_string_lossy())?;
|
|
let mut sample = self.sample.write().unwrap();
|
|
sample.name = path.file_name().unwrap().to_string_lossy().into();
|
|
sample.end = end;
|
|
sample.channels = channels;
|
|
return Ok(true)
|
|
}
|
|
return Ok(false)
|
|
}
|
|
}
|
|
|
|
fn read_sample_data (_: &str) -> Usually<(usize, Vec<Vec<f32>>)> {
|
|
todo!();
|
|
}
|
|
|
|
fn scan (dir: &PathBuf) -> Usually<(Vec<OsString>, Vec<OsString>)> {
|
|
let (mut subdirs, mut files) = std::fs::read_dir(dir)?
|
|
.fold((vec!["..".into()], vec![]), |(mut subdirs, mut files), entry|{
|
|
let entry = entry.expect("failed to read drectory entry");
|
|
let meta = entry.metadata().expect("failed to read entry metadata");
|
|
if meta.is_file() {
|
|
files.push(entry.file_name());
|
|
} else if meta.is_dir() {
|
|
subdirs.push(entry.file_name());
|
|
}
|
|
(subdirs, files)
|
|
});
|
|
subdirs.sort();
|
|
files.sort();
|
|
Ok((subdirs, files))
|
|
}
|
|
|
|
impl Content<TuiOut> for AddSampleModal {
|
|
fn render (&self, to: &mut TuiOut) {
|
|
todo!()
|
|
//let area = to.area();
|
|
//to.make_dim();
|
|
//let area = center_box(
|
|
//area,
|
|
//64.max(area.w().saturating_sub(8)),
|
|
//20.max(area.w().saturating_sub(8)),
|
|
//);
|
|
//to.fill_fg(area, Color::Reset);
|
|
//to.fill_bg(area, Nord::bg_lo(true, true));
|
|
//to.fill_char(area, ' ');
|
|
//to.blit(&format!("{}", &self.dir.to_string_lossy()), area.x()+2, area.y()+1, Some(Style::default().bold()))?;
|
|
//to.blit(&"Select sample:", area.x()+2, area.y()+2, Some(Style::default().bold()))?;
|
|
//for (i, (is_dir, name)) in self.subdirs.iter()
|
|
//.map(|path|(true, path))
|
|
//.chain(self.files.iter().map(|path|(false, path)))
|
|
//.enumerate()
|
|
//.skip(self.offset)
|
|
//{
|
|
//if i >= area.h() as usize - 4 {
|
|
//break
|
|
//}
|
|
//let t = if is_dir { "" } else { "" };
|
|
//let line = format!("{t} {}", name.to_string_lossy());
|
|
//let line = &line[..line.len().min(area.w() as usize - 4)];
|
|
//to.blit(&line, area.x() + 2, area.y() + 3 + i as u16, Some(if i == self.cursor {
|
|
//Style::default().green()
|
|
//} else {
|
|
//Style::default().white()
|
|
//}))?;
|
|
//}
|
|
//Lozenge(Style::default()).draw(to)
|
|
}
|
|
}
|