Skip to main content

Key

Struct Key 

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

A tonal key with a tonic pitch and mode.

Implementations§

Source§

impl Key

Source

pub fn color(&self) -> Option<&str>

The colour the key’s signature is drawn in, if one was said.

Source

pub fn set_color(&mut self, color: Option<String>)

Sets the colour the key’s signature is drawn in.

Source

pub fn from_tonic_mode<'a, M>(tonic: &str, mode: M) -> Result<Self>
where M: Into<Option<&'a str>>,

Builds a key from a tonic and mode.

Pass a mode string such as "major", "minor", "dorian", or None::<&str> to infer major/minor from tonic case. Builds a key from a tonic name and a mode. Without a mode the name decides, as music21’s Key reads it: a trailing m is minor and a trailing M major (F#m, EM), otherwise a lower-case name is minor and an upper-case one major.

Source

pub fn from_tonic(tonic: &str) -> Result<Self>

Builds a key and infers major/minor from tonic case.

Source

pub fn tonic(&self) -> Pitch

Returns a cloned tonic pitch.

Source

pub fn tonic_pitch(&self) -> &Pitch

Returns a borrowed tonic pitch.

Source

pub fn mode(&self) -> &str

Returns the key mode.

Source

pub fn transpose(&self, interval: &Interval) -> Result<Self>

Returns this key moved by the interval, keeping its mode and the colour it is drawn in.

Source

pub fn scale(&self) -> Scale

The scale this key is written in: its tonic, and the diatonic mode that tonic stands on within the key’s own signature.

The mode is read off the signature rather than the mode’s name, so every key has one. The church modes come out as themselves, D dorian as ScaleType::Dorian on D, and a mode music21 keeps no signature for takes the major signature on its tonic, as music21 gives it, and so reads as major.

music21’s own Key.getScale answers the relative major for a church mode, a C major scale for D dorian. The crate answers the mode.

Source

pub fn tonic_pitch_name_with_case(&self) -> String

Returns the tonic name in music21’s case convention: upper case for major, lower case for minor, unchanged for other modes.

Source

pub fn sharps(&self) -> IntegerType

Returns the number of sharps in the key signature.

Source

pub fn key_signature(&self) -> KeySignature

Returns the matching key signature.

Source

pub fn pitch_from_degree(&self, degree: usize) -> Result<Pitch>

The pitch on a one-based scale degree, counting on up past the octave: in C major the ninth degree is D5, which is what stacking thirds on the upper degrees needs. A tonic given with no octave is read in octave 4.

Scale::pitch_at_degree is music21’s pitchFromDegree, which reads the eighth degree as the tonic again.

Source

pub fn pitches(&self) -> Result<Vec<Pitch>>

The scale’s pitches from the tonic up to its octave, eight in all.

Source

pub fn triad_from_degree(&self, degree: usize) -> Result<Chord>

The diatonic triad on a one-based degree.

Source

pub fn seventh_chord_from_degree(&self, degree: usize) -> Result<Chord>

The diatonic seventh chord on a one-based degree.

Source

pub fn harmonized_triads(&self) -> Result<Vec<Chord>>

Returns all seven diatonic triads.

Source

pub fn harmonized_sevenths(&self) -> Result<Vec<Chord>>

Returns all seven diatonic seventh chords.

Source

pub fn derive_by_degree(&self, degree: usize, pitch: &Pitch) -> Result<Self>

Returns the relative major or minor key when applicable. Returns the key of the same mode in which pitch is the given scale degree: music21’s deriveByDegree, so C major with E as degree 5 is A major and A minor with C as degree 3 is A minor again.

Source

pub fn derive_by_degree_of( &self, scale_type: ScaleType, degree: usize, pitch: &Pitch, ) -> Result<Self>

The same, reading the degree off a scale pattern the caller chooses rather than the key’s own.

music21 spells this as a settable .abstract on the key: the seventh degree of a minor key is a whole tone below the tonic in the natural form and a semitone below it in the harmonic form, so the key that has E as its seventh is F-sharp minor by one reading and F minor by the other. The key that comes back keeps this one’s mode either way.

Source

pub fn relative(&self) -> Result<Self>

Returns the relative major or minor key – the one sharing this key’s signature. A mode that is neither answers with itself.

Source

pub fn parallel(&self) -> Result<Self>

Returns the parallel major or minor key when applicable.

Trait Implementations§

Source§

impl Clone for Key

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 Key

Source§

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

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

impl<'de> Deserialize<'de> for Key

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 From<Key> for StreamElement

Source§

fn from(value: Key) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Key

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(value: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
Source§

impl Serialize for Key

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<&str> for Key

Source§

type Error = Error

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

fn try_from(value: &str) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<String> for Key

Source§

type Error = Error

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

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

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Key

§

impl RefUnwindSafe for Key

§

impl Send for Key

§

impl Sync for Key

§

impl Unpin for Key

§

impl UnsafeUnpin for Key

§

impl UnwindSafe for Key

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.