Skip to content

Commit

Permalink
WIP: workaround for infinite instruments split
Browse files Browse the repository at this point in the history
  • Loading branch information
tsionyx committed Aug 24, 2024
1 parent 8e89fe6 commit d3217c6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
7 changes: 2 additions & 5 deletions examples/hsom-exercises/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
use ux2::u7;

use musik::{p, AbsPitch, Dur, Interval, Music, Performable as _, Performance, Pitch, Volume};
use musik::{p, AbsPitch, Dur, Interval, Music, Performable as _, Pitch, Volume};

mod ch1;
mod ch2;
Expand Down Expand Up @@ -97,10 +97,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let perf = m.perform();
if cli.mode.play {
// TODO: make the whole flow lazy (test on `ch6 shepard-desc`):
// - introduce lazy `midly::Smf`, then check that it starts real sound almost instantly without any `take`;
let perf = Performance::with_events(perf.iter().take(10_000));
perf.play()?;
perf.clone().play()?;
}

if let Some(path) = cli.mode.save_into {
Expand Down
10 changes: 9 additions & 1 deletion src/output/midi/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ impl Performance {
(stream, Timing::Metrical(DEFAULT_TIME_DIV))
}

// after one hour stop trying to find new instruments in the Performance
const FIND_NEW_INSTRUMENTS_IN: Option<Duration> = Some(Duration::from_secs(3_600));

fn split_by_instruments(self) -> impl Iterator<Item = (InstrumentName, Self)> {
let mut stream = {
let x: LazyList<_> = self.into_iter();
Expand All @@ -98,7 +101,12 @@ impl Performance {
let instrument = head.instrument.clone();
let i = instrument.clone();

let (this_instrument, other) = partition(current_stream, move |e| e.instrument == i);
let trying_fn = Self::FIND_NEW_INSTRUMENTS_IN.map(|max_dur| {
move |ev: &Event| u64::from(ev.start_time.to_integer()) < max_dur.as_secs()
});

let (this_instrument, other) =
partition(current_stream, move |e| e.instrument == i, trying_fn);
stream = Some(LazyList(Box::new(other)).peekable());
Some((instrument, Self::with_events(this_instrument)))
})
Expand Down
12 changes: 8 additions & 4 deletions src/utils/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,23 @@ where
}
}

pub fn partition<I, T, F>(
pub fn partition<I, T, F, TakeF>(
iter: I,
predicate: F,
take_only: Option<TakeF>,
) -> (
impl CloneableIterator<Item = T>,
impl CloneableIterator<Item = T>,
)
where
I: Iterator<Item = T> + Clone,
F: Fn(&T) -> bool + Clone + 'static,
TakeF: Fn(&T) -> bool + Clone + 'static,
{
let f = predicate.clone();
let left = iter.clone().filter(move |x| f(x));
let right = iter.filter(move |x| !predicate(x));
let take = move |x: &T| take_only.as_ref().map_or(true, |take_only| take_only(x));
let filter = move |x: &T| predicate(x);

let left = iter.clone().take_while(take.clone()).filter(filter.clone());
let right = iter.take_while(take).filter(move |x| !filter(x));
(left, right)
}

0 comments on commit d3217c6

Please sign in to comment.