Skip to main content

Duration

Struct Duration 

Source
pub struct Duration { /* private fields */ }
Expand description

Rhythmic duration measured in quarter lengths.

A quarter note has a quarter length of 1.0; an eighth note is 0.5; a whole note is 4.0.

Implementations§

Source§

impl Duration

Source

pub fn new(quarter_length: FloatType) -> Result<Self>

Creates a duration from a quarter-length value.

Source

pub fn grace(&self) -> Option<&Grace>

How this grace note is written, or nothing where this is not the duration of a grace note: music21’s isGrace and what its GraceDuration says.

Source

pub fn is_grace(&self) -> bool

Whether this is the duration of a grace note: music21’s isGrace.

Source

pub fn set_grace(&mut self, grace: Option<Grace>)

Says how this grace note is written. A duration is made a grace note’s by Duration::grace_duration, which gives it no length.

Source

pub fn quarter() -> Self

Returns a quarter-note duration.

Source

pub fn half() -> Self

Returns a half-note duration.

Source

pub fn whole() -> Self

Returns a whole-note duration.

Source

pub fn eighth() -> Self

Returns an eighth-note duration.

Source

pub fn from_type(duration_type: DurationType) -> Self

Creates a duration from a note-value type.

Source

pub fn from_type_with_dots(duration_type: DurationType, dots: u32) -> Self

Creates a duration from a note-value type carrying augmentation dots.

Source

pub fn type_and_dots(&self) -> Option<(DurationType, u32)>

Returns the note-value type and dot count that together make exactly this length, such as a half note with one dot for 3.0. None for a length no single dotted note value has, such as a tuplet or a tie.

Where the written values have been said, it is the one value there is, whatever the length: an unlinked duration written as a half is a half however long it sounds.

Source

pub fn dots(&self) -> u32

Returns the augmentation dots on this duration, or 0 when it is not a single dotted note value.

Source

pub fn set_dots(&mut self, dots: u32) -> Result<()>

Writes every value of the duration with so many dots, each keeping its note value and the whole its tuplets: music21’s dots setter. The length becomes what the values then come to, and a duration still worked out from its length is then taken as written so.

§Errors

A value written as no note value at all, which cannot take dots.

Source

pub fn ordinal(&self) -> Option<usize>

music21’s ordinal for the duration’s type, or None where music21 says complex or the duration is zero: a duration whose quarter length is not a single dotted note value.

Source

pub fn augment_or_diminish(&self, factor: FloatType) -> Result<Duration>

Returns the duration scaled by a positive factor: music21’s augmentOrDiminish, so a quarter by two is a half.

An unlinked duration stays unlinked and keeps its written values; only its length is scaled, and it is not snapped to a fraction.

Source

pub fn tuplet(&self) -> Option<Tuplet>

The tuplet this length is written as, if any: the first of quarter_length_to_tuplet.

A length that is already a plain written value is that value rather than a tuplet of some other one, so a quarter answers None: music21 tries the exact match before it tries any ratio, and a plain quarter is a quarter even though it is also two thirds of a dotted quarter in a triplet.

Source

pub fn tuplets(&self) -> Vec<Tuplet>

The tuplets this length is written inside: the ones a caller set, or the one read off the length when nobody has.

This is music21’s tuplets, which is likewise inferred until it is assigned. Setting it to nothing is not the same as never setting it: two thirds of a quarter reads as a quarter triplet on its own, and stays two thirds of a quarter written as no tuplet once told so.

Source

pub fn aggregate_tuplet_multiplier(&self) -> FractionType

What the written values are multiplied by to give the sounding length: music21’s aggregateTupletMultiplier, every tuplet’s ratio multiplied together, so a triplet inside a quintuplet is 8/15.

Source

pub fn quarter_length_no_tuplets(&self) -> FloatType

The total of the written values, before any tuplet shortens them: music21’s quarterLengthNoTuplets.

Source

pub fn set_tuplets(&mut self, tuplets: Vec<Tuplet>)

Says what tuplets this length is written inside, keeping the written values and changing the sounding length to match: music21’s tuplets setter.

Source

pub fn append_tuplet(&mut self, tuplet: Tuplet)

Writes this length inside one more tuplet, shortening it by that tuplet’s ratio: music21’s appendTuplet.

Source

pub fn components(&self) -> Vec<(DurationType, u32)>

The written note values this length is made of, tied together: music21’s components.

This is the first half of quarter_conversion, read over the written length. A length whose tuplets a caller has set is read as the tie those tuplets leave, without looking for a tuplet of its own.

Where the written values have been said these are they, less any with no type; Duration::written_values keeps those too.

Source

pub fn written_values(&self) -> Vec<DurationTuple>

The written values with the length of each: music21’s components as the DurationTuples they are there.

A length no tie of note values reaches is one value with no type, as it is upstream, and nought is no values at all.

Source

pub fn consolidate(&mut self)

One written value for the whole length, losing how it had been written: music21’s consolidate.

The value is the one the written total comes to, before any tuplet, and has no type where no single note value does. A duration already written as one value is left alone.

use music21_rs::{Duration, DurationType};

let mut duration = Duration::quarter();
duration.add_duration_tuple(DurationType::Half, 0);
duration.add_duration_tuple(DurationType::Quarter, 0);
assert_eq!(duration.components().len(), 3);
duration.consolidate();
assert_eq!(duration.components(), [(DurationType::Whole, 0)]);
Source

pub fn slice_component_at_position(&mut self, position: FloatType) -> Result<()>

Cuts the written value sounding at a position in two, leaving the whole length as it was: music21’s sliceComponentAtPosition.

The position is counted in the written values. One that falls on the join between two values, or at either end, cuts nothing and is an error.

Source

pub fn set_dot_groups(&mut self, groups: Vec<u32>)

Writes the duration with dots above its dots: music21’s dotGroups setter. Each group lengthens the value as dots do, so a half under [1, 1] lasts four and a half quarters. The plain dots come off the written values, since the groups now say how the note is dotted.

Source

pub fn split_dot_groups(&self) -> Result<Duration>

The same length written as a tie of singly dotted values, for a reader with no dot groups: music21’s splitDotGroups.

Each group after the first ties on every value so far at the next note value down, so a half under [1, 1] is a dotted half tied to a dotted quarter. It is an error for a duration that is not one written value.

Source

pub fn linked(&self) -> bool

Whether the written values and the sounding length move together: music21’s linked, true unless a caller has said otherwise.

Source

pub fn set_linked(&mut self, linked: bool)

Links or unlinks the written values and the length: music21’s linked setter.

Unlinking keeps the values the duration is written as today, and Duration::set_quarter_length then changes how long it sounds without touching them. Linking again keeps the length and reads the values off it afresh.

Source

pub fn grace_duration(&self) -> Duration

The same written values sounding for no time, unlinked from their length: the duration of a grace note, music21’s getGraceDuration.

A duration with no written values is written as an eighth, since a grace note has to be written as something. The tuplets are dropped, as they are upstream.

use music21_rs::{Duration, DurationType};

let grace = Duration::new(1.25)?.grace_duration();
assert_eq!(grace.quarter_length(), 0.0);
assert!(!grace.linked());
assert_eq!(
    grace.components(),
    [(DurationType::Quarter, 0), (DurationType::Sixteenth, 0)]
);
Source

pub fn expression_is_inferred(&self) -> bool

Whether the way this length is written was worked out from the length rather than said: music21’s expressionIsInferred.

A duration made from a length is inferred, and one made from a note value is not. Making notation may write an inferred length another way, cutting it to finish a tuplet or joining it with its neighbours; one that was said is left as it was written.

use music21_rs::{Duration, DurationType};

assert!(Duration::new(0.5)?.expression_is_inferred());
assert!(!Duration::from_type(DurationType::Eighth).expression_is_inferred());
Source

pub fn set_expression_is_inferred(&mut self, inferred: bool)

Says whether the way this length is written may be worked out again from the length.

Source

pub fn is_complex(&self) -> bool

Whether the length needs more than one written value, tied: music21’s isComplex.

Source

pub fn dot_groups(&self) -> Vec<u32>

The dots on the written value, as the one dot group the crate keeps: music21’s dotGroups, which can also write one length twice over where a mensural notation asks for it.

Source

pub fn clear(&mut self)

Takes the length away: music21’s clear, which empties the written values so the duration sounds for no time. The tuplets it was told stay.

Source

pub fn add_duration_tuple(&mut self, duration_type: DurationType, dots: u32)

Ties one more written value onto the end: music21’s addDurationTuple, which lengthens the duration by that value inside whatever tuplets it is written in.

The values are kept as they were added from then on, so a quarter with a half and a quarter tied on is three values and not a whole until it is consolidated.

Source

pub fn component_index_at_qtr_position( &self, position: FloatType, ) -> Result<usize>

Which written value is sounding at a position within the duration: music21’s componentIndexAtQtrPosition. The positions are counted in the written values, before any tuplet scales them, and the start and the very end answer the first and the last value.

Source

pub fn component_start_time(&self, index: usize) -> Result<FloatType>

Where a written value starts within the duration, counted in the written values: music21’s componentStartTime. An index past the values is an error.

Source

pub fn full_name(&self) -> String

Returns music21’s fullName for a single written note value, such as "Dotted Quarter", "Double Dotted Half", "Imperfect Longa" or "Quarter Triplet (2/3 QL)".

A length that has to be written as a tie names each of its values and joins them, "Quarter tied to 16th (1 1/4 total QL)". A length no note value reaches is "Inexpressible" and a zero one is "Zero Duration (0 total QL)", both as music21 names them.

Source

pub fn duration_type(&self) -> Option<DurationType>

Returns the note-value type whose undotted length this duration is.

Returns None for a length that is not a plain note value, such as a dotted or tuplet duration.

Source

pub fn quarter_length(&self) -> FloatType

Returns the duration in quarter lengths.

Source

pub fn set_quarter_length(&mut self, quarter_length: FloatType) -> Result<()>

Updates the duration in quarter lengths.

The written values are read off the new length, unless the duration is unlinked and keeps the ones it has.

Trait Implementations§

Source§

impl Clone for Duration

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Duration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Duration

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Duration

Available on crate feature serde only.
Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Duration

Source§

impl From<Duration> for Rest

Source§

fn from(value: Duration) -> Self

Converts to this type from the input type.
Source§

impl Hash for Duration

Hashes what equality compares, the quarter length.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Duration

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Duration

Available on crate feature serde only.
Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<f64> for Duration

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: FloatType) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<i32> for Duration

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: IntegerType) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.