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
impl Key
Sourcepub fn note_names(&self) -> Vec<NoteName>
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
impl Key
Sourcepub fn new(root: PitchClass, scale: Scale, mode: Mode) -> Key
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));
Sourcepub fn root(&self) -> PitchClass
pub fn root(&self) -> PitchClass
Returns the key’s root PitchClass
.
Sourcepub fn chords(&self) -> Vec<Chord>
pub fn chords(&self) -> Vec<Chord>
Returns the chords that use notes exclusively from this key.
Sourcepub fn chords_with_shape(&self, shape: Vec<ChordShape>) -> Vec<Chord>
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)),
]
)
Sourcepub fn contains<P>(&self, pitches: &P) -> boolwhere
P: PitchClassCollection,
pub fn contains<P>(&self, pitches: &P) -> boolwhere
P: PitchClassCollection,
Checks if all PitchClass
s 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))));
Sourcepub fn relative_pitch<D>(&self, degree: D) -> PitchClass
pub fn relative_pitch<D>(&self, degree: D) -> PitchClass
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<'de> Deserialize<'de> for Key
impl<'de> Deserialize<'de> for Key
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Key, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
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
impl Element for Key
Source§fn wrapped_element(&self) -> Option<&(dyn Element + 'static)>
fn wrapped_element(&self) -> Option<&(dyn Element + 'static)>
None.
Source§impl IntervalStepSequence for Key
impl IntervalStepSequence for Key
Source§fn interval_steps(&self) -> Vec<Interval>
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
impl NoteIterator for Key
Source§fn iter_notes_in_range<R>(&self, note_range: R) -> NoteIter<R> ⓘwhere
R: RangeBounds<Note>,
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>,
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 PitchClassCollection for Key
impl PitchClassCollection for Key
Source§fn pitch_classes(&self) -> Vec<PitchClass>
fn pitch_classes(&self) -> Vec<PitchClass>
Returns this type’s pitches.
Source§impl Serialize for Key
impl Serialize for Key
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
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
impl Copy for Key
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more