Skip to main content

realize_volume

Function realize_volume 

Source
pub fn realize_volume(
    stream: &mut Stream,
    context: &DynamicContext,
    use_velocity: bool,
)
Expand description

Sets how loud every note and chord of a stream sounds, from the dynamics in force: music21’s realizeVolume with setAbsoluteVelocity.

Each note’s volume is realized against the dynamic it stands under and the answer is written back as its velocity, no longer relative, so that a stream played from its velocities alone sounds as marked. Without use_velocity a note’s own velocity is left out of the realizing and the dynamic alone decides.

music21 without setAbsoluteVelocity only fills each volume’s cache of its realized value. There is no such cache here, so this is always the call that writes. The crate carries no articulations, so none shifts a note; Volume::realized_with takes that shift as an argument for a caller that has them.

use music21_rs::{volume::{realize_volume, DynamicContext}, Dynamic, Note, Stream};

let mut stream = Stream::new();
stream.insert(0.0, Dynamic::new("pp"));
stream.insert(0.0, Note::from_name("G3")?);
stream.insert(1.0, Dynamic::new("ff"));
stream.insert(1.0, Note::from_name("G3")?);
realize_volume(&mut stream, &DynamicContext::FromStream, true);
let velocities: Vec<_> = stream
    .notes()
    .iter()
    .map(|(_, note)| match note {
        music21_rs::StreamElement::Note(note) => note.volume().velocity(),
        _ => None,
    })
    .collect();
assert_eq!(velocities, [Some(45), Some(127)]);