midi: return old values from cursor traits

This commit is contained in:
🪞👃🪞 2025-04-27 17:07:09 +03:00
parent 397e71edee
commit e9c825e865
6 changed files with 76 additions and 92 deletions

View file

@ -160,14 +160,14 @@ impl NoteRange for MidiEditor {
impl NotePoint for MidiEditor {
fn note_len (&self) -> usize { self.mode.note_len() }
fn set_note_len (&self, x: usize) { self.mode.set_note_len(x) }
fn set_note_len (&self, x: usize) -> usize { self.mode.set_note_len(x) }
fn note_pos (&self) -> usize { self.mode.note_pos() }
fn set_note_pos (&self, x: usize) { self.mode.set_note_pos(x) }
fn set_note_pos (&self, x: usize) -> usize { self.mode.set_note_pos(x) }
}
impl TimePoint for MidiEditor {
fn time_pos (&self) -> usize { self.mode.time_pos() }
fn set_time_pos (&self, x: usize) { self.mode.set_time_pos(x) }
fn set_time_pos (&self, x: usize) -> usize { self.mode.set_time_pos(x) }
}
impl MidiViewer for MidiEditor {

View file

@ -21,30 +21,52 @@ impl Default for MidiPointModel {
}
pub trait NotePoint {
/// Get the current length of the note cursor.
fn note_len (&self) -> usize;
fn set_note_len (&self, x: 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;
fn set_note_pos (&self, x: usize);
fn note_end (&self) -> usize { self.note_pos() + self.note_len() }
/// 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;
fn set_time_pos (&self, x: 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 {}
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 set_note_len (&self, x: usize) { self.note_len.store(x, Relaxed) }
fn note_pos (&self) -> usize { self.note_pos.load(Relaxed).min(127) }
fn set_note_pos (&self, x: usize) { self.note_pos.store(x.min(127), Relaxed) }
fn note_len (&self) -> usize {
self.note_len.load(Relaxed)
}
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)
}
}
impl TimePoint for MidiPointModel {
fn time_pos (&self) -> usize { self.time_pos.load(Relaxed) }
fn set_time_pos (&self, x: usize) { self.time_pos.store(x, Relaxed) }
fn time_pos (&self) -> usize {
self.time_pos.load(Relaxed)
}
fn set_time_pos (&self, x: usize) -> usize {
self.time_pos.swap(x, Relaxed)
}
}

View file

@ -242,14 +242,14 @@ impl NoteRange for PianoHorizontal {
impl NotePoint for PianoHorizontal {
fn note_len (&self) -> usize { self.point.note_len() }
fn set_note_len (&self, x: usize) { self.point.set_note_len(x) }
fn set_note_len (&self, x: usize) -> usize { self.point.set_note_len(x) }
fn note_pos (&self) -> usize { self.point.note_pos() }
fn set_note_pos (&self, x: usize) { self.point.set_note_pos(x) }
fn set_note_pos (&self, x: usize) -> usize { self.point.set_note_pos(x) }
}
impl TimePoint for PianoHorizontal {
fn time_pos (&self) -> usize { self.point.time_pos() }
fn set_time_pos (&self, x: usize) { self.point.set_time_pos(x) }
fn set_time_pos (&self, x: usize) -> usize { self.point.set_time_pos(x) }
}
impl MidiViewer for PianoHorizontal {

View file

@ -42,10 +42,17 @@ impose!([state: Sampler]
("note/off" [p: u7]
Some(Self::NoteOff(p.expect("no pitch"))))));
defcom! { |self, state: Sampler|
SamplerCommand {
Import(cmd: FileBrowserCommand) => match cmd {
defcom!([self, state: Sampler]
(SamplerCommand
(Select [i: usize] Some(Self::Select(state.set_note_pos(i))))
(RecordBegin [p: u7] { state.begin_recording(p.as_int() as usize); None })
(RecordCancel [] { state.cancel_recording(); None })
(RecordFinish [] { state.finish_recording(); None })
(SetStart [pitch: u7, frame: usize] { println!("\n\rtodo: {self:?}"); None })
(SetGain [pitch: u7, gain: f32] { println!("\n\rtodo: {self:?}"); None })
(NoteOn [pitch: u7, velocity: u7] { println!("\n\rtodo: {self:?}"); None })
(NoteOff [pitch: u7] { println!("\n\rtodo: {self:?}"); None })
(Import [cmd: FileBrowserCommand] match cmd {
FileBrowserCommand::Begin => {
//let voices = &state.state.voices;
//let sample = Arc::new(RwLock::new(Sample::new("", 0, 0, vec![])));
@ -56,56 +63,10 @@ defcom! { |self, state: Sampler|
println!("\n\rtodo: import: filebrowser: {cmd:?}");
None
}
}
Select(index: usize) => {
let old = state.note_pos();
state.set_note_pos(index);
Some(Self::Select(old))
}
RecordBegin(pitch: u7) => {
state.begin_recording(pitch.as_int() as usize);
None
}
RecordCancel => {
state.cancel_recording();
None
}
RecordFinish => {
state.finish_recording();
None
}
SetSample(pitch: u7, sample: Option<Arc<RwLock<Sample>>>) => {
})
(SetSample [pitch: u7, sample: Option<Arc<RwLock<Sample>>>] {
let i = pitch.as_int() as usize;
let old = state.mapped[i].clone();
state.mapped[i] = sample;
Some(Self::SetSample(pitch, old))
}
SetStart(pitch: u7, frame: usize) => {
println!("\n\rtodo: {self:?}");
None
}
SetGain(pitch: u7, gain: f32) => {
println!("\n\rtodo: {self:?}");
None
}
NoteOn(pitch: u7, velocity: u7) => {
println!("\n\rtodo: {self:?}");
None
}
NoteOff(pitch: u7) => {
println!("\n\rtodo: {self:?}");
None
}
}
}
})));

View file

@ -124,14 +124,15 @@ impl NoteRange for Sampler {
impl NotePoint for Sampler {
fn note_len (&self) -> usize { 0/*TODO*/ }
fn set_note_len (&self, x: usize) {}
fn set_note_len (&self, x: usize) -> usize { 0 /*TODO*/ }
fn note_pos (&self) -> usize {
self.note_pt.load(Relaxed)
}
fn set_note_pos (&self, x: usize) {
self.note_pt.store(x, Relaxed);
fn set_note_pos (&self, x: usize) -> usize {
let old = self.note_pt.swap(x, Relaxed);
self.cursor.0.store(x % 8, Relaxed);
self.cursor.1.store(x / 8, Relaxed);
old
}
}