Skip to main content

segments_list

Function segments_list 

Source
pub fn segments_list(stream: &Stream, remove_empty: bool) -> Vec<Vec<usize>>
Expand description

The runs of notes between rests and clefs, through every stream nested in this one, each note as its position in Stream::leaves: music21’s Segmenter.getSegmentsList.

Only single notes count: a chord or an unpitched stroke neither joins a run nor ends one. Parts are read one after another with nothing between them, so a part that opens without a clef or a rest carries on the last run of the part before it. With remove_empty a rest or clef following another gives no empty run.

use music21_rs::analysis::segment_by_rests::segments_list;
use music21_rs::stream::StreamElement;
use music21_rs::tinynotation::from_tiny_notation;

let part = from_tiny_notation("C4 r D E r r F r G r A B r c")?;
let leaves = part.leaves();
let names: Vec<Vec<String>> = segments_list(&part, true)
    .into_iter()
    .map(|run| {
        run.into_iter()
            .map(|position| match leaves[position].1 {
                StreamElement::Note(note) => note.pitch().name(),
                _ => unreachable!(),
            })
            .collect()
    })
    .collect();
assert_eq!(names, [vec!["C"], vec!["D", "E"], vec!["F"], vec!["G"], vec!["A", "B"], vec!["C"]]);