pub fn partition_by_instrument(stream: &Stream) -> StreamExpand description
Splits a score into a part for each instrument playing in it, each part
gathering what is played on that instrument from every part of the
score: music21’s partitionByInstrument.
Each part of the score is flattened, or the stream itself where it holds none. An instrument is in force from where it stands until the next one in its part, the last until the part ends, and whatever starts while it is in force goes to the part for its name; instruments with one name share a part, the first of them standing at its start, and a part is named by its instrument’s name. What a part plays under two instruments of one name is gathered once. A stream with no instrument in it comes back as a score of its parts flattened.
The parts come out as music21’s do, with no measures; spanners are not carried across, as the places they name move.
use music21_rs::{instrument::partition_by_instrument, Instrument, Note, Stream};
let mut part = Stream::new();
part.insert(0.0, Instrument::of_kind("Piccolo")?);
part.insert(0.0, Note::from_name("C4")?);
part.insert(1.0, Instrument::of_kind("Trombone")?);
part.insert(1.0, Note::from_name("D4")?);
part.insert(2.0, Note::from_name("E4")?);
let score = partition_by_instrument(&part);
let names: Vec<Option<&str>> = score.parts().iter().map(|part| part.id()).collect();
assert_eq!(names, [Some("Piccolo"), Some("Trombone")]);
assert_eq!(score.parts()[1].notes().len(), 2);