Struct tune::note::Note

source · []
pub struct Note { /* private fields */ }
Expand description

A musical note encapsulating a clearly defined pitch.

The pitch can be derived using the Pitched impl on the Note type itself, assuming standard 440 Hz tuning, or on NoteAtConcertPitch, given a specific concert pitch.

Implementations

Creates a Note instance from the given MIDI number.

Creates a Note instance given a NoteLetter and an octave.

Examples
let a4 = Note::from_midi_number(69);
assert_eq!(Note::from_letter_and_octave(NoteLetter::A, 4), a4);
assert_eq!(Note::from_letter_and_octave(NoteLetter::A, Octave::from_octave_number(4)), a4);
assert_eq!(Note::from_letter_and_octave(NoteLetter::A, HelmholtzOctave::OneLined), a4);

Creates a Note instance from a PianoKey assuming standard 12-EDO tuning.

Returns the MIDI number of this Note.

Returns the MIDI number of this Note if it is in the valid MIDI range [0..128).

Examples
assert_eq!(Note::from_midi_number(-1).checked_midi_number(), None);
assert_eq!(Note::from_midi_number(0).checked_midi_number(), Some(0));
assert_eq!(Note::from_midi_number(64).checked_midi_number(), Some(64));
assert_eq!(Note::from_midi_number(127).checked_midi_number(), Some(127));
assert_eq!(Note::from_midi_number(128).checked_midi_number(), None);

Splits the current note into a NoteLetter and an Octave part.

Examples
let a4 = Note::from_midi_number(69);
assert_eq!(a4.letter_and_octave(), (NoteLetter::A, Octave::from_octave_number(4)));

let midi_root = Note::from_midi_number(0);
assert_eq!(midi_root.letter_and_octave(), (NoteLetter::C, Octave::from_octave_number(-1)));

Retrieves the associated PianoKey assuming standard 12-EDO tuning.

Creates a NoteAtConcertPitch instance with self sounding at a different pitch.

Examples
use tune::pitch::Pitched;

let c4_at_260_hz = NoteLetter::C.in_octave(4).at_pitch(Pitch::from_hz(260.0));
assert_approx_eq!(c4_at_260_hz.pitch().as_hz(), 260.0);

let (_note, concert_pitch) = c4_at_260_hz;
assert_approx_eq!(concert_pitch.a4_pitch().as_hz(), 437.266136);

Convenience function creating a NoteAtConcertPitch instance.

Examples
let a4 = NoteLetter::A.in_octave(4);
let concert_pitch = ConcertPitch::from_a4_pitch(Pitch::from_hz(435.0));
assert_eq!(a4.at_concert_pitch(concert_pitch), (a4, concert_pitch));

Iterates over all Notes in the range [self..upper_bound).

Examples
let midi_note_62 = Note::from_midi_number(62);
let midi_note_67 = Note::from_midi_number(67);

assert_eq!(
    midi_note_62.notes_before(midi_note_67).collect::<Vec<_>>(),
    (62..67).map(Note::from_midi_number).collect::<Vec<_>>()
);
assert!(midi_note_67.notes_before(midi_note_62).collect::<Vec<_>>().is_empty());

Counts the number of semitones [left inclusive, right exclusive) between self and other.

Examples
let midi_note_62 = Note::from_midi_number(62);
let midi_note_67 = Note::from_midi_number(67);

assert_eq!(midi_note_62.num_semitones_before(midi_note_67), 5);
assert_eq!(midi_note_67.num_semitones_before(midi_note_62), -5);

Returns the note num_semitones semitones above self.

Examples
let midi_note_62 = Note::from_midi_number(62);
let midi_note_67 = Note::from_midi_number(67);

assert_eq!(midi_note_62.plus_semitones(5), midi_note_67);
assert_eq!(midi_note_67.plus_semitones(-5), midi_note_62);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Notes are rendered in scientific pitch notation.

Examples

assert_eq!(Note::from_midi_number(0).to_string(), "C -1");
assert_eq!(Note::from_midi_number(69).to_string(), "A 4");
assert_eq!(Note::from_midi_number(70).to_string(), "A#/Bb 4");
assert_eq!(Note::from_midi_number(71).to_string(), "B 4");
assert_eq!(Note::from_midi_number(72).to_string(), "C 5");
assert_eq!(Note::from_midi_number(127).to_string(), "G 9");

// Format flags
assert_eq!(format!("{:+}", Note::from_midi_number(70)), "A# 4");
assert_eq!(format!("{:-}", Note::from_midi_number(70)), "Bb 4");
assert_eq!(format!("{:10}", Note::from_midi_number(70)), "A#/Bb 4   ");
assert_eq!(format!("{:<10}", Note::from_midi_number(70)), "A#/Bb 4   ");
assert_eq!(format!("{:>10}", Note::from_midi_number(70)), "   A#/Bb 4");

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

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

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Retrieves the Pitch of the Pitched object. Read more

Finds a key or note for any Pitched object in the given tuning. Read more

Retrieves the Note part of self. Read more

Returns a new PitchedNote with the same Note part but a Pitch altered by delta. Read more

A ConcertPitch maps Notes to Pitches and is considered a Tuning.

Examples

use tune::tuning::Tuning;

let c4 = NoteLetter::C.in_octave(4);
let a4 = NoteLetter::A.in_octave(4);

let standard_tuning = ConcertPitch::default();
assert_approx_eq!(standard_tuning.pitch_of(c4).as_hz(), 261.625565);
assert_approx_eq!(standard_tuning.pitch_of(a4).as_hz(), 440.0);

let healing_tuning = ConcertPitch::from_a4_pitch(Pitch::from_hz(432.0));
assert_approx_eq!(healing_tuning.pitch_of(c4).as_hz(), 256.868737);
assert_approx_eq!(healing_tuning.pitch_of(a4).as_hz(), 432.0);

Returns the Pitch of the given key or note K in the current Tuning. Read more

Finds a closest key or note Approximation for the given Pitch in the current Tuning. Read more

Wraps self in a type adapter s.t. it can be used in functions that are generic over KeyboardMapping<K>.

Convenience implementation enabling to write () instead of ConcertPitch::default().

Examples

use tune::pitch::Pitched;

assert_eq!(Pitch::from_hz(880.0).find_in_tuning(()).approx_value, Note::from_midi_number(81));

Returns the Pitch of the given key or note K in the current Tuning. Read more

Finds a closest key or note Approximation for the given Pitch in the current Tuning. Read more

Wraps self in a type adapter s.t. it can be used in functions that are generic over KeyboardMapping<K>.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.