add getter/setters to note cursor traits

This commit is contained in:
🪞👃🪞 2025-05-08 03:19:47 +03:00
parent a6100ab1d6
commit a8be2e9dad
8 changed files with 214 additions and 188 deletions

View file

@ -20,53 +20,60 @@ impl Default for MidiPointModel {
}
}
pub trait NotePoint {
/// Get the current length of the note cursor.
fn note_len (&self) -> usize;
/// Set the length of the note cursor, returning the previous value.
fn set_note_len (&self, x: usize) -> usize;
/// Get the current pitch of the note cursor.
fn note_pos (&self) -> usize;
/// Set the current pitch fo the note cursor, returning the previous value.
fn set_note_pos (&self, x: usize) -> usize;
}
pub trait TimePoint {
/// Get the current time position of the note cursor.
fn time_pos (&self) -> usize;
/// Set the current time position of the note cursor, returning the previous value.
fn set_time_pos (&self, x: usize) -> usize;
}
pub trait MidiPoint: NotePoint + TimePoint {
/// Get the current end of the note cursor.
fn note_end (&self) -> usize {
self.time_pos() + self.note_len()
}
}
impl<T: NotePoint + TimePoint> MidiPoint for T {}
impl NotePoint for MidiPointModel {
fn note_len (&self) -> usize {
self.note_len.load(Relaxed)
fn note_len (&self) -> &AtomicUsize {
&self.note_len
}
fn set_note_len (&self, x: usize) -> usize {
self.note_len.swap(x, Relaxed)
}
fn note_pos (&self) -> usize {
self.note_pos.load(Relaxed).min(127)
}
fn set_note_pos (&self, x: usize) -> usize {
self.note_pos.swap(x.min(127), Relaxed)
fn note_pos (&self) -> &AtomicUsize {
&self.note_pos
}
}
impl TimePoint for MidiPointModel {
fn time_pos (&self) -> usize {
self.time_pos.load(Relaxed)
}
fn set_time_pos (&self, x: usize) -> usize {
self.time_pos.swap(x, Relaxed)
fn time_pos (&self) -> &AtomicUsize {
self.time_pos.as_ref()
}
}
pub trait NotePoint {
fn note_len (&self) -> &AtomicUsize;
/// Get the current length of the note cursor.
fn get_note_len (&self) -> usize {
self.note_len().load(Relaxed)
}
/// Set the length of the note cursor, returning the previous value.
fn set_note_len (&self, x: usize) -> usize {
self.note_len().swap(x, Relaxed)
}
fn note_pos (&self) -> &AtomicUsize;
/// Get the current pitch of the note cursor.
fn get_note_pos (&self) -> usize {
self.note_pos().load(Relaxed).min(127)
}
/// Set the current pitch fo the note cursor, returning the previous value.
fn set_note_pos (&self, x: usize) -> usize {
self.note_pos().swap(x.min(127), Relaxed)
}
}
pub trait TimePoint {
fn time_pos (&self) -> &AtomicUsize;
/// Get the current time position of the note cursor.
fn get_time_pos (&self) -> usize {
self.time_pos().load(Relaxed)
}
/// Set the current time position of the note cursor, returning the previous value.
fn set_time_pos (&self, x: usize) -> usize {
self.time_pos().swap(x, Relaxed)
}
}
pub trait MidiPoint: NotePoint + TimePoint {
/// Get the current end of the note cursor.
fn get_note_end (&self) -> usize {
self.get_time_pos() + self.get_note_len()
}
}
impl<T: NotePoint + TimePoint> MidiPoint for T {}

View file

@ -1,4 +1,5 @@
use crate::*;
use std::sync::atomic::Ordering;
#[derive(Debug, Clone)]
pub struct MidiRangeModel {
@ -28,20 +29,44 @@ from!(|data:(usize, bool)|MidiRangeModel = Self {
});
pub trait TimeRange {
fn time_len (&self) -> &AtomicUsize;
fn time_zoom (&self) -> &AtomicUsize;
fn time_lock (&self) -> &AtomicBool;
fn time_len (&self) -> &AtomicUsize;
fn get_time_len (&self) -> usize {
self.time_len().load(Ordering::Relaxed)
}
fn time_zoom (&self) -> &AtomicUsize;
fn get_time_zoom (&self) -> usize {
self.time_zoom().load(Ordering::Relaxed)
}
fn time_lock (&self) -> &AtomicBool;
fn get_time_lock (&self) -> bool {
self.time_lock().load(Ordering::Relaxed)
}
fn time_start (&self) -> &AtomicUsize;
fn time_axis (&self) -> &AtomicUsize;
fn time_end (&self) -> usize {
fn get_time_start (&self) -> usize {
self.time_start().load(Ordering::Relaxed)
}
fn time_axis (&self) -> &AtomicUsize;
fn get_time_axis (&self) -> usize {
self.time_axis().load(Ordering::Relaxed)
}
fn get_time_end (&self) -> usize {
self.time_start().get() + self.time_axis().get() * self.time_zoom().get()
}
}
pub trait NoteRange {
fn note_lo (&self) -> &AtomicUsize;
fn note_axis (&self) -> &AtomicUsize;
fn note_hi (&self) -> usize {
fn note_lo (&self) -> &AtomicUsize;
fn get_note_lo (&self) -> usize {
self.note_lo().load(Ordering::Relaxed)
}
fn set_note_lo (&self, x: usize) -> usize {
self.note_lo().swap(x, Ordering::Relaxed)
}
fn note_axis (&self) -> &AtomicUsize;
fn get_note_axis (&self) -> usize {
self.note_axis().load(Ordering::Relaxed)
}
fn get_note_hi (&self) -> usize {
(self.note_lo().get() + self.note_axis().get().saturating_sub(1)).min(127)
}
}