Skip to main content

melodic_line_positions

Function melodic_line_positions 

Source
pub fn melodic_line_positions(stream: &Stream) -> Vec<Vec<usize>>
Expand description

The same lines as positions in Stream::leaves, which is the by-position twin melodic_lines reads its own answer off: the note a count was taken from is the note the stream holds, so a caller handing back objects can hand back its own.

use music21_rs::analysis::melodic_line_positions;
use music21_rs::stream::StreamElement;
use music21_rs::tinynotation::from_tiny_notation;

let line = from_tiny_notation("4/4 c4 r d e")?;
let leaves = line.leaves();
// The clef, the meter and the rest are leaves too; only the notes
// make a line.
let names: Vec<Vec<String>> = melodic_line_positions(&line)
    .into_iter()
    .map(|line| {
        line.into_iter()
            .map(|position| match leaves[position].1 {
                StreamElement::Note(note) => note.pitch().name(),
                _ => unreachable!(),
            })
            .collect()
    })
    .collect();
assert_eq!(names, [vec!["C", "D", "E"]]);