Struct Key

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

Musical key signature represented as a root PitchClass, Scale (e.g. Major/Minor), and Mode.

use redact_composer_musical::{Key, Mode, NoteName::*, Scale::Major, Note, PitchClassCollection};
let c_major = Key::from((C, Major));
assert_eq!(c_major.pitch_classes(), vec![C, D, E, F, G, A, B]);

Implementations§

Source§

impl Key

Source

pub fn root_name(&self) -> NoteName

Returns the NoteName of the key’s tonic pitch class.

Source

pub fn note_names(&self) -> Vec<NoteName>

Returns the ordered list of NoteName’s associated with the key’s pitch classes.

use redact_composer_musical::{Key, NoteName::*, Scale::Major};
let key = Key::from((D, Major));
assert_eq!(key.note_names(), [D, E, Fs, G, A, B, Cs])
Source§

impl Key

Source

pub fn new(root: PitchClass, scale: Scale, mode: Mode) -> Key

Creates a Key from a PitchClass, Scale, and Mode. Alternatively, several From implementations are supported:

use redact_composer_musical::{Key, Mode::Ionian, NoteName::C, PitchClass, Scale::Major};

// All the ways to define C Major (Ionian)
let first = Key::new(PitchClass(0), Major, Ionian);
let second = Key::from((PitchClass(0), Major));
let third = Key::from((PitchClass(0), Major, Ionian));
let fourth = Key::from((C, Major));
let fifth = Key::from((C, Major, Ionian));

assert!([second, third, fourth, fifth].into_iter().all(|k| k == first));
Source

pub fn root(&self) -> PitchClass

Returns the key’s root PitchClass.

Source

pub fn scale(&self) -> Scale

Returns the key’s Scale.

Source

pub fn mode(&self) -> Mode

Returns the key’s Mode.

Source

pub fn chords(&self) -> Vec<Chord>

Returns the chords that use notes exclusively from this key.

Source

pub fn chords_with_shape(&self, shape: Vec<ChordShape>) -> Vec<Chord>

Returns chords of the given shapes which use notes exclusively from this key.

use redact_composer_musical::{Key, NoteName::*, Chord, ChordShape, ChordShape::{dim, maj, min}, Scale, Mode};
use redact_composer_musical::Scale::Major;

let key = Key::from((C, Major));
assert_eq!(
    key.chords_with_shape(ChordShape::triad()),
    vec![
        Chord::from((C, maj)),
        Chord::from((D, min)),
        Chord::from((E, min)),
        Chord::from((F, maj)),
        Chord::from((G, maj)),
        Chord::from((A, min)),
        Chord::from((B, dim)),
    ]
)
Source

pub fn contains<P>(&self, pitches: &P) -> bool

Checks if all PitchClasss from a collection (for example, Chord) belong to this key.

use redact_composer_musical::{Chord, ChordShape::{maj, min}, Key, Mode::Ionian, NoteName::*, Scale::Major};

assert!(Key::from((C, Major)).contains(&Chord::from((C, maj))));
assert!(!Key::from((C, Major)).contains(&Chord::from((C, min))));
Source

pub fn relative_pitch<D>(&self, degree: D) -> PitchClass
where D: Into<Degree>,

Returns the pitch class for a given degree of this scale.

use redact_composer_musical::{Degree, Key, Scale::Major, Mode::Locrian, NoteName::{B, D}};

let key = Key::from((B, Major, Locrian));
assert_eq!(key.relative_pitch(Degree::III), D);

Trait Implementations§

Source§

impl Clone for Key

Source§

fn clone(&self) -> Key

Returns a duplicate of the value. Read more
1.0.0 · 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<(), Error>

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

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

Source§

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

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

impl Element for Key

Source§

fn wrapped_element(&self) -> Option<&(dyn Element + 'static)>

None.
Source§

impl From<(NoteName, Scale)> for Key

Source§

fn from(value: (NoteName, Scale)) -> Key

Converts to this type from the input type.
Source§

impl From<(NoteName, Scale, Mode)> for Key

Source§

fn from(value: (NoteName, Scale, Mode)) -> Key

Converts to this type from the input type.
Source§

impl From<(PitchClass, Scale)> for Key

Source§

fn from(value: (PitchClass, Scale)) -> Key

Converts to this type from the input type.
Source§

impl From<(PitchClass, Scale, Mode)> for Key

Source§

fn from(value: (PitchClass, Scale, Mode)) -> Key

Converts to this type from the input type.
Source§

impl Hash for Key

Source§

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

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 IntervalStepSequence for Key

Source§

fn interval_steps(&self) -> Vec<Interval>

Provides a sequence of intervals, representing the interval deltas from one to the next.
Source§

impl NoteIterator for Key

Source§

fn iter_notes_in_range<R>(&self, note_range: R) -> NoteIter<R>
where R: RangeBounds<Note>,

Returns a note iterator (NoteIter) for notes of an interval pattern within the given note range.
Source§

fn notes_in_range<R>(&self, note_range: R) -> Vec<Note>
where R: RangeBounds<Note>,

Returns all notes matching an interval pattern within the given note range.
Source§

impl PartialEq for Key

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PitchClassCollection for Key

Source§

fn pitch_classes(&self) -> Vec<PitchClass>

Returns this type’s pitches.
Source§

impl Serialize for Key

Source§

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

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

impl Copy for Key

Source§

impl Eq for Key

Auto Trait Implementations§

§

impl Freeze for Key

§

impl RefUnwindSafe for Key

§

impl Send for Key

§

impl Sync for Key

§

impl Unpin 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> AsAny for T
where T: Element,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts this to a [&dyn Any].
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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> IntervalCollection for T

Source§

fn intervals(&self) -> Vec<Interval>

Provides a collection of intervals, each as an absolute interval from a relative pitch.
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> IntoSegment for T
where T: Element,

Source§

fn into_segment(self, timing: impl Into<Timing>) -> Segment

Creates a Segment from this element, spanning the given time range.

Source§

fn over(self, timing: impl Into<Timing>) -> Segment

Creates a Segment from this element, spanning the given time range.

Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

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