Skip to main content

dynamic_context

Function dynamic_context 

Source
pub fn dynamic_context(stream: &Stream, position: usize) -> Option<usize>
Expand description

The dynamic a note’s volume is read against: music21’s getDynamicContext, which asks the note for the nearest dynamic at or before it.

position is the note’s place in Stream::leaves, and the answer is the dynamic’s place there, or None where no dynamic comes before it. The search starts in the stream holding the note and works outwards, as music21’s getContextByClass does: a measure, a voice or a plain stream is searched first among its own elements and then through the streams it holds, a part only through them, and a score or an opus only among its own elements – so a dynamic in another part is never the one, while a dynamic nearer the note in its own voice wins over a later one in another voice. A dynamic at the note’s own offset is before it.

use music21_rs::{volume::dynamic_context, Dynamic, Note, Stream, StreamKind};

let mut measure = Stream::with_kind(StreamKind::Measure);
measure.insert(0.0, Dynamic::new("ff"));
measure.insert(0.0, Note::from_name("C4")?);
measure.insert(1.0, Note::from_name("D4")?);
assert_eq!(dynamic_context(&measure, 2), Some(0));
assert_eq!(dynamic_context(&Stream::new(), 0), None);