Skip to main content

unbundle_instruments

Function unbundle_instruments 

Source
pub fn unbundle_instruments(stream: &mut Stream)
Expand description

Puts the instrument each note, chord or stroke of a stream keeps as its own into the stream beside it, at its offset: music21’s unbundleInstruments.

Only what the stream holds itself is looked at, not what a measure or a voice in it holds. The notes keep their instruments too.

use music21_rs::{instrument::unbundle_instruments, Instrument, Stream, StreamElement, Unpitched};

let mut stream = Stream::new();
for (offset, kind) in [(0.0, "BassDrum"), (1.0, "Cowbell")] {
    let mut stroke = Unpitched::new();
    stroke.written_mut().set_stored_instrument(Some(Instrument::of_kind(kind)?));
    stream.insert(offset, stroke);
}
unbundle_instruments(&mut stream);
let said: Vec<String> = stream
    .events()
    .iter()
    .map(|event| match event.element() {
        StreamElement::Instrument(instrument) => format!("{} {}", event.offset(), instrument.kind()),
        _ => format!("{} stroke", event.offset()),
    })
    .collect();
assert_eq!(said, ["0 BassDrum", "0 stroke", "1 Cowbell", "1 stroke"]);