use crate::*; /// Contains state for viewing and editing a clip. /// /// ``` /// use std::sync::{Arc, RwLock}; /// let clip = tek::MidiClip::stop_all(); /// let mut editor = tek::MidiEditor { /// mode: tek::PianoHorizontal::new(Some(&Arc::new(RwLock::new(clip)))), /// size: Default::default(), /// //keys: Default::default(), /// }; /// let _ = editor.put_note(true); /// let _ = editor.put_note(false); /// let _ = editor.clip_status(); /// let _ = editor.edit_status(); /// ``` pub struct MidiEditor { /// Size of editor on screen pub size: Size, /// View mode and state of editor pub mode: PianoHorizontal, } impl_default!(MidiEditor: Self { size: [0, 0].into(), mode: PianoHorizontal::new(None) }); impl MidiEditor { /// Put note at current position pub fn put_note (&mut self, advance: bool) { let mut redraw = false; if let Some(clip) = self.clip() { let mut clip = clip.write().unwrap(); let note_start = self.get_time_pos(); let note_pos = self.get_note_pos(); let note_len = self.get_note_len(); let note_end = note_start + (note_len.saturating_sub(1)); let key: u7 = u7::from(note_pos as u8); let vel: u7 = 100.into(); let length = clip.length; let note_end = note_end % length; let note_on = MidiMessage::NoteOn { key, vel }; if !clip.notes[note_start].iter().any(|msg|*msg == note_on) { clip.notes[note_start].push(note_on); } let note_off = MidiMessage::NoteOff { key, vel }; if !clip.notes[note_end].iter().any(|msg|*msg == note_off) { clip.notes[note_end].push(note_off); } if advance { self.set_time_pos((note_end + 1) % clip.length); } redraw = true; } if redraw { self.mode.redraw(); } } fn _todo_opt_clip_stub (&self) -> Option>> { todo!() } fn clip_length (&self) -> usize { self.clip().as_ref().map(|p|p.read().unwrap().length).unwrap_or(1) } fn note_length (&self) -> usize { self.get_note_len() } fn note_pos (&self) -> usize { self.get_note_pos() } fn note_pos_next (&self) -> usize { self.get_note_pos() + 1 } fn note_pos_next_octave (&self) -> usize { self.get_note_pos() + 12 } fn note_pos_prev (&self) -> usize { self.get_note_pos().saturating_sub(1) } fn note_pos_prev_octave (&self) -> usize { self.get_note_pos().saturating_sub(12) } fn note_len (&self) -> usize { self.get_note_len() } fn note_len_next (&self) -> usize { self.get_note_len() + 1 } fn note_len_prev (&self) -> usize { self.get_note_len().saturating_sub(1) } fn note_range (&self) -> usize { self.get_note_axis() } fn note_range_next (&self) -> usize { self.get_note_axis() + 1 } fn note_range_prev (&self) -> usize { self.get_note_axis().saturating_sub(1) } fn time_zoom (&self) -> usize { self.get_time_zoom() } fn time_zoom_next (&self) -> usize { self.get_time_zoom() + 1 } fn time_zoom_next_fine (&self) -> usize { self.get_time_zoom() + 1 } fn time_zoom_prev (&self) -> usize { self.get_time_zoom().saturating_sub(1).max(1) } fn time_zoom_prev_fine (&self) -> usize { self.get_time_zoom().saturating_sub(1).max(1) } fn time_lock (&self) -> bool { self.get_time_lock() } fn time_lock_toggled (&self) -> bool { !self.get_time_lock() } fn time_pos (&self) -> usize { self.get_time_pos() } fn time_pos_next (&self) -> usize { (self.get_time_pos() + self.get_note_len()) % self.clip_length() } fn time_pos_next_fine (&self) -> usize { (self.get_time_pos() + 1) % self.clip_length() } fn time_pos_prev (&self) -> usize { let step = self.get_note_len(); self.get_time_pos().overflowing_sub(step) .0.min(self.clip_length().saturating_sub(step)) } fn time_pos_prev_fine (&self) -> usize { self.get_time_pos().overflowing_sub(1) .0.min(self.clip_length().saturating_sub(1)) } pub fn clip_status (&self) -> impl Draw + '_ { let (_color, name, length, looped) = if let Some(clip) = self.clip().as_ref().map(|p|p.read().unwrap()) { (clip.color, clip.name.clone(), clip.length, clip.looped) } else { (ItemTheme::G[64], String::new().into(), 0, false) }; w_exact(20, south!( w_full(origin_w(east( button_2("f2", "name ", false), w_full(origin_e(fg(Rgb(255, 255, 255), format!("{name} "))))))), w_full(origin_w(east( button_2("l", "ength ", false), w_full(origin_e(fg(Rgb(255, 255, 255), format!("{length} "))))))), w_full(origin_w(east( button_2("r", "epeat ", false), w_full(origin_e(fg(Rgb(255, 255, 255), format!("{looped} "))))))), )) } pub fn edit_status (&self) -> impl Draw + '_ { let (_color, length) = if let Some(clip) = self.clip().as_ref().map(|p|p.read().unwrap()) { (clip.color, clip.length) } else { (ItemTheme::G[64], 0) }; let time_pos = self.get_time_pos(); let time_zoom = self.get_time_zoom(); let time_lock = if self.get_time_lock() { "[lock]" } else { " " }; let note_pos = self.get_note_pos(); let note_name = format!("{:4}", note_pitch_to_name(note_pos)); let note_pos = format!("{:>3}", note_pos); let note_len = format!("{:>4}", self.get_note_len()); w_exact(20, south!( w_full(origin_w(east( button_2("t", "ime ", false), w_full(origin_e(fg(Rgb(255, 255, 255), format!("{length} /{time_zoom} +{time_pos} "))))))), w_full(origin_w(east( button_2("z", "lock ", false), w_full(origin_e(fg(Rgb(255, 255, 255), format!("{time_lock}"))))))), w_full(origin_w(east( button_2("x", "note ", false), w_full(origin_e(fg(Rgb(255, 255, 255), format!("{note_name} {note_pos} {note_len}"))))))), )) } } /// ``` /// use tek::{*, tengri::*}; /// /// struct Test(Option); /// impl_as_ref_opt!(MidiEditor: |self: Test|self.0.as_ref()); /// impl_as_mut_opt!(MidiEditor: |self: Test|self.0.as_mut()); /// /// let mut host = Test(Some(MidiEditor::default())); /// let _ = host.editor(); /// let _ = host.editor_mut(); /// let _ = host.is_editing(); /// let _ = host.editor_w(); /// let _ = host.editor_h(); /// ``` pub trait HasEditor: AsRefOpt + AsMutOpt { fn editor (&self) -> Option<&MidiEditor> { self.as_ref_opt() } fn editor_mut (&mut self) -> Option<&mut MidiEditor> { self.as_mut_opt() } fn is_editing (&self) -> bool { self.editor().is_some() } fn editor_w (&self) -> usize { self.editor().map(|e|e.size.w()).unwrap_or(0) as usize } fn editor_h (&self) -> usize { self.editor().map(|e|e.size.h()).unwrap_or(0) as usize } } impl MidiPoint for T {} impl MidiRange for T {} impl +AsMutOpt> HasEditor for T {} def_command!(MidiEditCommand: |editor: MidiEditor| { Show { clip: Option>> } => { editor.set_clip(clip.as_ref()); editor.redraw(); Ok(None) }, DeleteNote => { editor.redraw(); todo!() }, AppendNote { advance: bool } => { editor.put_note(*advance); editor.redraw(); Ok(None) }, SetNotePos { pos: usize } => { editor.set_note_pos((*pos).min(127)); editor.redraw(); Ok(None) }, SetNoteLen { len: usize } => { editor.set_note_len(*len); editor.redraw(); Ok(None) }, SetNoteScroll { scroll: usize } => { editor.set_note_lo((*scroll).min(127)); editor.redraw(); Ok(None) }, SetTimePos { pos: usize } => { editor.set_time_pos(*pos); editor.redraw(); Ok(None) }, SetTimeScroll { scroll: usize } => { editor.set_time_start(*scroll); editor.redraw(); Ok(None) }, SetTimeZoom { zoom: usize } => { editor.set_time_zoom(*zoom); editor.redraw(); Ok(None) }, SetTimeLock { lock: bool } => { editor.set_time_lock(*lock); editor.redraw(); Ok(None) }, // TODO: 1-9 seek markers that by default start every 8th of the clip }); pub trait MidiViewer: MidiRange + MidiPoint + Debug + Send + Sync { fn buffer_size (&self, clip: &MidiClip) -> (usize, usize); fn redraw (&self); fn clip (&self) -> &Option>>; fn clip_mut (&mut self) -> &mut Option>>; fn set_clip (&mut self, clip: Option<&Arc>>) { *self.clip_mut() = clip.cloned(); self.redraw(); } /// Make sure cursor is within note range fn autoscroll (&self) { let note_pos = self.get_note_pos().min(127); let note_lo = self.get_note_lo(); let note_hi = self.get_note_hi(); if note_pos < note_lo { self.note_lo().store(note_pos, Relaxed); } else if note_pos > note_hi { self.note_lo().store((note_lo + note_pos).saturating_sub(note_hi), Relaxed); } } /// Make sure time range is within display fn autozoom (&self) { if self.time_lock().load(Relaxed) { let time_len = self.get_time_len(); let time_axis = self.get_time_axis(); let time_zoom = self.get_time_zoom(); loop { let time_zoom = self.time_zoom().load(Relaxed); let time_area = time_axis * time_zoom; if time_area > time_len { let next_time_zoom = note_duration_prev(time_zoom); if next_time_zoom <= 1 { break } let next_time_area = time_axis * next_time_zoom; if next_time_area >= time_len { self.time_zoom().store(next_time_zoom, Relaxed); } else { break } } else if time_area < time_len { let prev_time_zoom = note_duration_next(time_zoom); if prev_time_zoom > 384 { break } let prev_time_area = time_axis * prev_time_zoom; if prev_time_area <= time_len { self.time_zoom().store(prev_time_zoom, Relaxed); } else { break } } } if time_zoom != self.time_zoom().load(Relaxed) { self.redraw() } } //while time_len.div_ceil(time_zoom) > time_axis { //println!("\r{time_len} {time_zoom} {time_axis}"); //time_zoom = Note::next(time_zoom); //} //self.time_zoom().set(time_zoom); } } impl_has!(Size: |self: MidiEditor| self.size); impl Draw for MidiEditor { fn draw (self, to: &mut Tui) -> Usually> { self.mode.tui().draw(to) } } impl std::fmt::Debug for MidiEditor { fn fmt (&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { f.debug_struct("MidiEditor").field("mode", &self.mode).finish() } } impl_from!(MidiEditor: |clip: &Arc>| { let model = Self::from(Some(clip.clone())); model.redraw(); model }); impl_from!(MidiEditor: |clip: Option>>| { let mut model = Self::default(); *model.clip_mut() = clip; model.redraw(); model }); impl NotePoint for MidiEditor { fn note_len (&self) -> &AtomicUsize { self.mode.note_len() } fn note_pos (&self) -> &AtomicUsize { self.mode.note_pos() } } impl TimePoint for MidiEditor { fn time_pos (&self) -> &AtomicUsize { self.mode.time_pos() } } impl MidiViewer for MidiEditor { fn buffer_size (&self, clip: &MidiClip) -> (usize, usize) { self.mode.buffer_size(clip) } fn redraw (&self) { self.mode.redraw() } fn clip (&self) -> &Option>> { self.mode.clip() } fn clip_mut (&mut self) -> &mut Option>> { self.mode.clip_mut() } fn set_clip (&mut self, p: Option<&Arc>>) { self.mode.set_clip(p) } } impl View for MidiEditor { fn view (&self) -> impl Draw { self.autoscroll(); /*self.autozoom();*/ self.size.of(&self.mode) } } mod octave; pub use self::octave::*; mod piano; pub use self::piano::*; mod point; pub use self::point::*; mod range; pub use self::range::*;