mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
wip: refactor frames_to_ticks, will become Iterator
This commit is contained in:
parent
4204ac4462
commit
238d307817
4 changed files with 86 additions and 46 deletions
38
Cargo.lock
generated
38
Cargo.lock
generated
|
|
@ -111,6 +111,12 @@ dependencies = [
|
|||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
||||
|
||||
[[package]]
|
||||
name = "better-panic"
|
||||
version = "0.3.0"
|
||||
|
|
@ -237,6 +243,12 @@ dependencies = [
|
|||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const-default"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b396d1f76d455557e1218ec8066ae14bba60b4b36ecd55577ba979f5db7ecaa"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.5"
|
||||
|
|
@ -814,6 +826,18 @@ dependencies = [
|
|||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rlsf"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "222fb240c3286247ecdee6fa5341e7cdad0ffdf8e7e401d9937f2d58482a20bf"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"const-default",
|
||||
"libc",
|
||||
"svgbobdoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.24"
|
||||
|
|
@ -947,6 +971,19 @@ dependencies = [
|
|||
"syn 2.0.60",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "svgbobdoc"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2c04b93fc15d79b39c63218f15e3fdffaa4c227830686e3b7c5f41244eb3e50"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
|
|
@ -985,6 +1022,7 @@ dependencies = [
|
|||
"midly",
|
||||
"music-math",
|
||||
"ratatui",
|
||||
"rlsf",
|
||||
"toml",
|
||||
"vst",
|
||||
"wavers",
|
||||
|
|
|
|||
|
|
@ -21,3 +21,4 @@ wavers = "1.4.3"
|
|||
music-math = "0.1.1"
|
||||
atomic_float = "1.0.0"
|
||||
fraction = "0.15.3"
|
||||
rlsf = "0.2.1"
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@
|
|||
//#![feature(unboxed_closures)]
|
||||
#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]
|
||||
|
||||
//#[global_allocator]
|
||||
//static A: rlsf::SmallGlobalTlsf = rlsf::SmallGlobalTlsf::new();
|
||||
|
||||
extern crate clap;
|
||||
extern crate jack as _jack;
|
||||
extern crate crossterm;
|
||||
|
|
|
|||
|
|
@ -46,13 +46,13 @@ impl Phrase {
|
|||
timebase: &Arc<Timebase>,
|
||||
(frame0, frames, _): (usize, usize, f64),
|
||||
) {
|
||||
let start = frame0;
|
||||
let end = frame0 + frames;
|
||||
let repeat = timebase.pulses_frames(self.length as f64);
|
||||
let ticks = timebase.frames_to_ticks(start as f64, end as f64, repeat);
|
||||
//panic!("{start} {end} {repeat} {ticks:?}");
|
||||
for (time, tick) in ticks.iter() {
|
||||
if let Some(events) = self.notes.get(&(*tick as usize)) {
|
||||
for (time, tick) in frames_to_ticks(
|
||||
timebase.pulse_frame(),
|
||||
timebase.pulses_frames(self.length as f64),
|
||||
frame0 as f64,
|
||||
(frame0 + frames) as f64,
|
||||
) {
|
||||
if let Some(events) = self.notes.get(&(tick as usize)) {
|
||||
for message in events.iter() {
|
||||
let mut buf = vec![];
|
||||
let channel = 0.into();
|
||||
|
|
@ -63,59 +63,57 @@ impl Phrase {
|
|||
_ => {}
|
||||
}
|
||||
LiveEvent::Midi { channel, message }.write(&mut buf).unwrap();
|
||||
let t = *time as usize; // FIXME miscalculated, skipping notes
|
||||
output[t].push(buf);
|
||||
output[time as usize].push(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Timebase {
|
||||
pub fn frames_to_ticks (
|
||||
&self,
|
||||
start: f64,
|
||||
end: f64,
|
||||
repeat: f64,
|
||||
) -> Vec<(usize, usize)> {
|
||||
let start_frame = start % repeat;
|
||||
let end_frame = end % repeat;
|
||||
let fpt = self.pulse_frame();
|
||||
//panic!("{start_frame} {end_frame} {fpt}");
|
||||
let mut ticks = vec![];
|
||||
let mut add_frame = |frame: f64|{
|
||||
let jitter = frame.rem_euclid(fpt); // ramps
|
||||
let next_jitter = (frame + 1.0).rem_euclid(fpt);
|
||||
if jitter > next_jitter { // at head of ramp crossing
|
||||
ticks.push((
|
||||
frame as usize % (end as usize-start as usize),
|
||||
(frame / fpt) as usize)
|
||||
);
|
||||
};
|
||||
fn frames_to_ticks (fpt: f64, repeat: f64, start: f64, end: f64) -> Box<dyn Iterator<Item=(usize, usize)>> {
|
||||
let mut ticks = vec![];
|
||||
let mut add_frame = |frame: f64|{
|
||||
let jitter = frame.rem_euclid(fpt); // ramps
|
||||
let next_jitter = (frame + 1.0).rem_euclid(fpt);
|
||||
if jitter > next_jitter { // at head of ramp crossing
|
||||
ticks.push((
|
||||
frame as usize % (end as usize-start as usize),
|
||||
(frame / fpt) as usize)
|
||||
);
|
||||
};
|
||||
if start_frame < end_frame {
|
||||
for frame in start_frame as usize..end_frame as usize {
|
||||
add_frame(frame as f64);
|
||||
}
|
||||
} else {
|
||||
let mut frame = start_frame as usize;
|
||||
};
|
||||
let start_frame = start % repeat;
|
||||
let end_frame = end % repeat;
|
||||
if start_frame < end_frame {
|
||||
frames_to_ticks_1(&mut add_frame, start % repeat, end % repeat);
|
||||
} else {
|
||||
frames_to_ticks_2(&mut add_frame, start % repeat, end % repeat, repeat);
|
||||
}
|
||||
Box::new(ticks.into_iter())
|
||||
}
|
||||
|
||||
fn frames_to_ticks_1 (add_frame: &mut impl FnMut(f64), start_frame: f64, end_frame: f64) {
|
||||
for frame in start_frame as usize..end_frame as usize {
|
||||
add_frame(frame as f64);
|
||||
}
|
||||
}
|
||||
|
||||
fn frames_to_ticks_2 (add_frame: &mut impl FnMut(f64), start_frame: f64, end_frame: f64, repeat: f64) {
|
||||
let mut frame = start_frame as usize;
|
||||
loop {
|
||||
add_frame(frame as f64);
|
||||
frame = frame + 1;
|
||||
if frame >= repeat as usize {
|
||||
frame = 0;
|
||||
loop {
|
||||
add_frame(frame as f64);
|
||||
frame = frame + 1;
|
||||
if frame >= repeat as usize {
|
||||
frame = 0;
|
||||
loop {
|
||||
add_frame(frame as f64);
|
||||
frame = frame + 1;
|
||||
if frame >= (end_frame as usize).saturating_sub(1) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if frame >= (end_frame as usize).saturating_sub(1) {
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
ticks
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue