mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-08-28 21:06:56 +02:00
This commit is contained in:
parent
ae496987c8
commit
7fcab73b04
12 changed files with 811 additions and 559 deletions
|
|
@ -11,6 +11,30 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
#[tek_proc::commands(BrowseCommand = "browse")]
|
||||
impl Browse {
|
||||
/// Toggle visibility of browser
|
||||
#[command(Show = "show")]
|
||||
fn show (&mut self) -> Perhaps<BrowseCommand> {
|
||||
todo!()
|
||||
}
|
||||
/// Set current directory
|
||||
#[command(SetPath = "set-path")]
|
||||
fn set_path (&mut self, _path: PathBuf) -> Perhaps<BrowseCommand> {
|
||||
todo!()
|
||||
}
|
||||
/// Set current filter
|
||||
#[command(SetSearch = "set-search")]
|
||||
fn set_search (&mut self, _filter: Arc<str>) -> Perhaps<BrowseCommand> {
|
||||
todo!()
|
||||
}
|
||||
/// Set selected item
|
||||
#[command(SetCursor = "set-cursor")]
|
||||
fn set_cursor (&mut self, _index: usize) -> Perhaps<BrowseCommand> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
def_command!(FileBrowserCommand: |_browse: Browse|{
|
||||
//("begin" [] Some(Self::Begin))
|
||||
//("cancel" [] Some(Self::Cancel))
|
||||
|
|
@ -52,40 +76,6 @@ pub(crate) struct EntriesIterator<'a, S: Screen> {
|
|||
ExportClip(Arc<RwLock<Option<MidiClip>>>),
|
||||
}
|
||||
|
||||
/// A clip pool.
|
||||
///
|
||||
/// ```
|
||||
/// let pool = tek::Pool::default();
|
||||
/// ```
|
||||
#[derive(Debug)] pub struct Pool {
|
||||
pub visible: bool,
|
||||
/// Selected clip
|
||||
pub clip: AtomicUsize,
|
||||
/// Mode switch
|
||||
pub mode: Option<PoolMode>,
|
||||
/// Embedded file browse
|
||||
#[cfg(feature = "browse")] pub browse: Option<Browse>,
|
||||
/// Collection of MIDI clips.
|
||||
#[cfg(feature = "clip")] pub clips: Arc<RwLock<Vec<Arc<RwLock<MidiClip>>>>>,
|
||||
/// Collection of sound samples.
|
||||
#[cfg(feature = "sampler")] pub samples: Arc<RwLock<Vec<Arc<RwLock<Sample>>>>>,
|
||||
}
|
||||
|
||||
/// Displays and edits clip length.
|
||||
#[derive(Clone, Debug, Default)] pub struct ClipLength {
|
||||
/// Pulses per beat (quaver)
|
||||
pub ppq: usize,
|
||||
/// Beats per bar
|
||||
pub bpb: usize,
|
||||
/// Length of clip in pulses
|
||||
pub pulses: usize,
|
||||
/// Selected subdivision
|
||||
pub focus: Option<ClipLengthFocus>,
|
||||
}
|
||||
|
||||
/// Some sort of wrapper again?
|
||||
pub struct PoolView<'a>(pub &'a Pool);
|
||||
|
||||
// Commands supported by [Browse]
|
||||
//#[derive(Debug, Clone, PartialEq)]
|
||||
//pub enum BrowseCommand {
|
||||
|
|
@ -96,198 +86,6 @@ pub struct PoolView<'a>(pub &'a Pool);
|
|||
//Chdir(PathBuf),
|
||||
//Filter(Arc<str>),
|
||||
//}
|
||||
|
||||
/// Modes for clip pool
|
||||
#[derive(Debug, Clone)] pub enum PoolMode {
|
||||
/// Renaming a pattern
|
||||
Rename(usize, Arc<str>),
|
||||
/// Editing the length of a pattern
|
||||
Length(usize, usize, ClipLengthFocus),
|
||||
/// Load clip from disk
|
||||
Import(usize, Browse),
|
||||
/// Save clip to disk
|
||||
Export(usize, Browse),
|
||||
}
|
||||
|
||||
/// Focused field of `ClipLength`
|
||||
#[derive(Copy, Clone, Debug)] pub enum ClipLengthFocus {
|
||||
/// Editing the number of bars
|
||||
Bar,
|
||||
/// Editing the number of beats
|
||||
Beat,
|
||||
/// 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
|
||||
}
|
||||
}
|
||||
|
||||
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(||g(32).into());
|
||||
//let on_bg = |x|x;//below(Repeat(" "), bg(color.darkest.term, x));
|
||||
//let border = |x|x;//Outer(Style::default().fg(color.dark.term).bg(color.darkest.term)).enclose(x);
|
||||
//let height = pool.clips.read().unwrap().len() as u16;
|
||||
iter(
|
||||
||pool.clips().clone().into_iter(),
|
||||
move|clip: Arc<RwLock<MidiClip>>, i: usize|{
|
||||
let MidiClip { ref name, color, length, .. } = *clip.read().unwrap();
|
||||
let item_height = 1;
|
||||
let _item_offset = i as u16 * item_height;
|
||||
let selected = i == pool.clip_index();
|
||||
let b = if selected { color.light.term } else { color.base.term };
|
||||
let f = color.lightest.term;
|
||||
let name = if false { format!(" {i:>3}") } else { format!(" {i:>3} {name}") };
|
||||
let length = if false { String::default() } else { format!("{length} ") };
|
||||
bg(b, below!(
|
||||
fg(f, bold(selected, name)).origin_w().full_w(),
|
||||
fg(f, bold(selected, length)).origin_e().full_w(),
|
||||
when(selected, bold(true, fg(g(255), "▶"))).origin_w().full_w(),
|
||||
when(selected, bold(true, fg(g(255), "◀"))).origin_e().full_w(),
|
||||
)).exact_h(1)
|
||||
}).origin_n().full_h().exact_w(20)
|
||||
}
|
||||
}
|
||||
|
||||
impl ClipLength {
|
||||
fn tui (&self) -> impl Draw<Tui> {
|
||||
use ClipLengthFocus::*;
|
||||
let bars = format!("{}", self.bars());
|
||||
let beats = format!("{}", self.beats());
|
||||
let ticks = format!("{:>02}", self.ticks());
|
||||
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()? };
|
||||
|
|
@ -322,9 +120,6 @@ impl Browse {
|
|||
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_fixed(1, ||self.tui_entries(), |entry, _index|entry.origin_w().full_w())
|
||||
}
|
||||
|
|
@ -369,188 +164,6 @@ impl PartialEq for BrowseTarget {
|
|||
}
|
||||
}
|
||||
|
||||
def_command!(BrowseCommand: |browse: Browse| {
|
||||
SetVisible => Ok(None),
|
||||
SetPath { address: PathBuf } => Ok(None),
|
||||
SetSearch { filter: Arc<str> } => Ok(None),
|
||||
SetCursor { cursor: usize } => Ok(None),
|
||||
});
|
||||
|
||||
def_command!(PoolCommand: |pool: Pool| {
|
||||
// Toggle visibility of pool
|
||||
Show { visible: bool } => {
|
||||
pool.visible = *visible;
|
||||
Ok(Some(Self::Show { visible: !visible }))
|
||||
},
|
||||
// Select a clip from the clip pool
|
||||
Select { index: usize } => {
|
||||
pool.set_clip_index(*index);
|
||||
Ok(None)
|
||||
},
|
||||
// Update the contents of the clip pool
|
||||
Clip { command: PoolClipCommand } => Ok(
|
||||
command.act(pool)?.map(|command|Self::Clip{command})
|
||||
),
|
||||
// Rename a clip
|
||||
Rename { command: RenameCommand } => Ok(
|
||||
command.act(pool)?.map(|command|Self::Rename{command})
|
||||
),
|
||||
// Change the length of a clip
|
||||
Length { command: CropCommand } => Ok(
|
||||
command.act(pool)?.map(|command|Self::Length{command})
|
||||
),
|
||||
// Import from file
|
||||
Import { command: BrowseCommand } => Ok(if let Some(browse) = pool.browse.as_mut() {
|
||||
command.act(browse)?.map(|command|Self::Import{command})
|
||||
} else {
|
||||
None
|
||||
}),
|
||||
// Export to file
|
||||
Export { command: BrowseCommand } => Ok(if let Some(browse) = pool.browse.as_mut() {
|
||||
command.act(browse)?.map(|command|Self::Export{command})
|
||||
} else {
|
||||
None
|
||||
}),
|
||||
});
|
||||
|
||||
def_command!(PoolClipCommand: |pool: Pool| {
|
||||
Delete { index: usize } => {
|
||||
let index = *index;
|
||||
let clip = pool.clips_mut().remove(index).read().unwrap().clone();
|
||||
Ok(Some(Self::Add { index, clip }))
|
||||
},
|
||||
Swap { index: usize, other: usize } => {
|
||||
let index = *index;
|
||||
let other = *other;
|
||||
pool.clips_mut().swap(index, other);
|
||||
Ok(Some(Self::Swap { index, other }))
|
||||
},
|
||||
Export { index: usize, path: PathBuf } => {
|
||||
todo!("export clip to midi file");
|
||||
},
|
||||
Add { index: usize, clip: MidiClip } => {
|
||||
let index = *index;
|
||||
let mut index = index;
|
||||
let clip = Arc::new(RwLock::new(clip.clone()));
|
||||
let mut clips = pool.clips_mut();
|
||||
if index >= clips.len() {
|
||||
index = clips.len();
|
||||
clips.push(clip)
|
||||
} else {
|
||||
clips.insert(index, clip);
|
||||
}
|
||||
Ok(Some(Self::Delete { index }))
|
||||
},
|
||||
Import { index: usize, path: PathBuf } => {
|
||||
let index = *index;
|
||||
let bytes = std::fs::read(&path)?;
|
||||
let smf = Smf::parse(bytes.as_slice())?;
|
||||
let mut t = 0u32;
|
||||
let mut events = vec![];
|
||||
for track in smf.tracks.iter() {
|
||||
for event in track.iter() {
|
||||
t += event.delta.as_int();
|
||||
if let TrackEventKind::Midi { channel, message } = event.kind {
|
||||
events.push((t, channel.as_int(), message));
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut clip = MidiClip::new("imported", true, t as usize + 1, None, None);
|
||||
for event in events.iter() {
|
||||
clip.notes[event.0 as usize].push(event.2);
|
||||
}
|
||||
Ok(Self::Add { index, clip }.act(pool)?)
|
||||
},
|
||||
SetName { index: usize, name: Arc<str> } => {
|
||||
let index = *index;
|
||||
let clip = &mut pool.clips_mut()[index];
|
||||
let old_name = clip.read().unwrap().name.clone();
|
||||
clip.write().unwrap().name = name.clone();
|
||||
Ok(Some(Self::SetName { index, name: old_name }))
|
||||
},
|
||||
SetLength { index: usize, length: usize } => {
|
||||
let index = *index;
|
||||
let clip = &mut pool.clips_mut()[index];
|
||||
let old_len = clip.read().unwrap().length;
|
||||
clip.write().unwrap().length = *length;
|
||||
Ok(Some(Self::SetLength { index, length: old_len }))
|
||||
},
|
||||
SetColor { index: usize, color: ItemColor } => {
|
||||
let index = *index;
|
||||
let mut color = ItemTheme::from(*color);
|
||||
std::mem::swap(&mut color, &mut pool.clips()[index].write().unwrap().color);
|
||||
Ok(Some(Self::SetColor { index, color: color.base }))
|
||||
},
|
||||
});
|
||||
|
||||
def_command!(RenameCommand: |pool: Pool| {
|
||||
Begin => unreachable!(),
|
||||
Cancel => {
|
||||
if let Some(PoolMode::Rename(clip, ref mut old_name)) = pool.mode_mut().clone() {
|
||||
pool.clips()[clip].write().unwrap().name = old_name.clone().into();
|
||||
}
|
||||
Ok(None)
|
||||
},
|
||||
Confirm => {
|
||||
if let Some(PoolMode::Rename(_clip, ref mut old_name)) = pool.mode_mut().clone() {
|
||||
let old_name = old_name.clone(); *pool.mode_mut() = None; return Ok(Some(Self::Set { value: old_name }))
|
||||
}
|
||||
Ok(None)
|
||||
},
|
||||
Set { value: Arc<str> } => {
|
||||
if let Some(PoolMode::Rename(clip, ref mut _old_name)) = pool.mode_mut().clone() {
|
||||
pool.clips()[clip].write().unwrap().name = value.clone();
|
||||
}
|
||||
Ok(None)
|
||||
},
|
||||
});
|
||||
|
||||
def_command!(CropCommand: |pool: Pool| {
|
||||
Begin => unreachable!(),
|
||||
Cancel => { if let Some(PoolMode::Length(..)) = pool.mode_mut().clone() { *pool.mode_mut() = None; } Ok(None) },
|
||||
Set { length: usize } => {
|
||||
if let Some(PoolMode::Length(clip, ref mut length, ref mut _focus))
|
||||
= pool.mode_mut().clone()
|
||||
{
|
||||
let old_length;
|
||||
{
|
||||
let clip = pool.clips()[clip].clone();//.write().unwrap();
|
||||
old_length = Some(clip.read().unwrap().length);
|
||||
clip.write().unwrap().length = *length;
|
||||
}
|
||||
*pool.mode_mut() = None;
|
||||
return Ok(old_length.map(|length|Self::Set { length }))
|
||||
}
|
||||
Ok(None)
|
||||
},
|
||||
Next => {
|
||||
if let Some(PoolMode::Length(_clip, ref mut _length, ref mut focus)) = pool.mode_mut().clone() { focus.next() }; Ok(None)
|
||||
},
|
||||
Prev => {
|
||||
if let Some(PoolMode::Length(_clip, ref mut _length, ref mut focus)) = pool.mode_mut().clone() { focus.prev() }; Ok(None)
|
||||
},
|
||||
Inc => {
|
||||
if let Some(PoolMode::Length(_clip, ref mut length, ref mut focus)) = pool.mode_mut().clone() {
|
||||
match focus {
|
||||
ClipLengthFocus::Bar => { *length += 4 * PPQ },
|
||||
ClipLengthFocus::Beat => { *length += PPQ },
|
||||
ClipLengthFocus::Tick => { *length += 1 },
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
},
|
||||
Dec => {
|
||||
if let Some(PoolMode::Length(_clip, ref mut length, ref mut focus)) = pool.mode_mut().clone() {
|
||||
match focus {
|
||||
ClipLengthFocus::Bar => { *length = length.saturating_sub(4 * PPQ) },
|
||||
ClipLengthFocus::Beat => { *length = length.saturating_sub(PPQ) },
|
||||
ClipLengthFocus::Tick => { *length = length.saturating_sub(1) },
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
});
|
||||
|
||||
pub 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|{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue