pub struct Note {
pub base_start_tick: u32,
pub pitch: Pitch,
pub duration: Duration,
pub tie: bool,
pub tied: bool,
pub base_velocity: Velocity,
pub start_tick_trimmer: Trimmer,
pub duration_trimmer: RateTrimmer,
pub velocity_trimmer: Trimmer,
pub channel: Channel,
}Expand description
Represents a musical note with timing, pitch, duration, and velocity information.
A Note is the fundamental building block of musical composition in this library.
It contains all the information needed to play a single note, including:
- Timing information (start tick)
- Pitch (musical note and octave)
- Duration (note length)
- Velocity (how hard the note is played)
- Tie information (for connecting notes)
- Trimmers for fine-tuning timing, duration, and velocity
§Examples
use klavier_core::note::{Note, NoteBuilder};
use klavier_core::pitch::Pitch;
use klavier_core::solfa::Solfa;
use klavier_core::octave::Octave;
use klavier_core::sharp_flat::SharpFlat;
use klavier_core::duration::{Duration, Numerator, Denominator, Dots};
use klavier_core::velocity::Velocity;
// Create a note using the builder pattern
let note = NoteBuilder::default()
.base_start_tick(0)
.pitch(Pitch::new(Solfa::C, Octave::Oct4, SharpFlat::Null))
.duration(Duration::new(Numerator::Whole, Denominator::from_value(4).unwrap(), Dots::ZERO))
.base_velocity(Velocity::new(100))
.build()
.unwrap();Fields§
§base_start_tick: u32The base start tick position of the note (before any trimmer adjustments).
pitch: PitchThe pitch of the note (musical note, octave, and accidental).
duration: DurationThe duration of the note (note length).
tie: boolWhether this note is tied to the next note (tie start).
tied: boolWhether this note is tied from the previous note (tie end).
base_velocity: VelocityThe base velocity of the note (before any trimmer adjustments).
start_tick_trimmer: TrimmerTrimmer for adjusting the start tick position.
duration_trimmer: RateTrimmerTrimmer for adjusting the duration as a rate multiplier.
velocity_trimmer: TrimmerTrimmer for adjusting the velocity.
channel: ChannelThe MIDI channel for this note.
Implementations§
Source§impl Note
impl Note
Sourcepub fn start_tick(&self) -> u32
pub fn start_tick(&self) -> u32
Returns the actual start tick of the note after applying the start tick trimmer.
This method calculates the final start tick by adding the base start tick and the trimmer adjustment. If the result would be negative, it returns 0.
§Returns
The actual start tick position (always >= 0).
Sourcepub fn tick_len(&self) -> u32
pub fn tick_len(&self) -> u32
Returns the actual tick length of the note after applying the duration trimmer.
This method calculates the final duration by applying the rate trimmer to the base duration’s tick length.
§Returns
The actual tick length of the note.
Sourcepub fn up_score_offset(&self) -> Result<Self, PitchError>
pub fn up_score_offset(&self) -> Result<Self, PitchError>
Creates a new note with the pitch raised by one semitone.
§Returns
Ok(Note)- A new note with the pitch raised by one semitone.Err(PitchError)- If the pitch is already at the maximum value.
§Examples
let note = Note {
pitch: Pitch::new(Solfa::C, Octave::Oct4, SharpFlat::Null),
..Default::default()
};
let higher_note = note.up_score_offset().unwrap();
assert_eq!(higher_note.pitch, Pitch::new(Solfa::D, Octave::Oct4, SharpFlat::Null));Sourcepub fn down_score_offset(&self) -> Result<Self, PitchError>
pub fn down_score_offset(&self) -> Result<Self, PitchError>
Creates a new note with the pitch lowered by one semitone.
§Returns
Ok(Note)- A new note with the pitch lowered by one semitone.Err(PitchError)- If the pitch is already at the minimum value.
Sourcepub fn with_duration(&self, d: Duration) -> Self
pub fn with_duration(&self, d: Duration) -> Self
Sourcepub fn with_duration_numerator(&self, numerator: Numerator) -> Self
pub fn with_duration_numerator(&self, numerator: Numerator) -> Self
Creates a new note with the specified duration numerator.
This method only updates the duration if the numerator is different from the current one, optimizing for cases where no change is needed.
§Arguments
numerator- The new numerator for the note’s duration.
§Returns
A new note with the specified duration numerator.
Sourcepub fn with_tick_added(
&self,
tick_delta: i32,
is_trim: bool,
) -> Result<Self, TickError>
pub fn with_tick_added( &self, tick_delta: i32, is_trim: bool, ) -> Result<Self, TickError>
Creates a new note with the start tick adjusted by the specified delta.
§Arguments
tick_delta- The amount to add to the start tick (can be negative).is_trim- Iftrue, adjusts the trimmer; iffalse, adjusts the base start tick.
§Returns
Ok(Note)- A new note with the adjusted start tick.Err(TickError::Minus)- If the resulting tick would be negative.
§Examples
let note = Note {
base_start_tick: 100,
..Default::default()
};
let later_note = note.with_tick_added(50, false).unwrap();
assert_eq!(later_note.base_start_tick, 150);Sourcepub fn drag(&self, tick_delta: i32, score_offset_delta: i32) -> Self
pub fn drag(&self, tick_delta: i32, score_offset_delta: i32) -> Self
Creates a new note with both timing and pitch adjusted (for drag operations).
This method is typically used for interactive editing where a note is being dragged both horizontally (time) and vertically (pitch).
§Arguments
tick_delta- The amount to add to the start tick.score_offset_delta- The amount to adjust the pitch (in semitones).
§Returns
A new note with adjusted timing and pitch.
§Panics
Panics if the pitch adjustment would result in an invalid pitch.
Sourcepub fn add_dots(&self, dots_to_add: i32) -> Result<Self, InvalidDot>
pub fn add_dots(&self, dots_to_add: i32) -> Result<Self, InvalidDot>
Creates a new note with additional dots added to the duration.
Dots extend the duration of a note. Each dot adds half the value of the previous duration component.
§Arguments
dots_to_add- The number of dots to add (can be negative to remove dots).
§Returns
Ok(Note)- A new note with the adjusted dot count.Err(InvalidDot)- If the resulting dot count is outside the valid range.
§Examples
let note = Note {
duration: Duration::new(Numerator::Whole, Denominator::from_value(4).unwrap(), Dots::ZERO),
..Default::default()
};
let dotted_note = note.add_dots(1).unwrap();
assert_eq!(dotted_note.duration.dots, Dots::ONE);Sourcepub fn toggle_sharp(&self) -> Result<Self, PitchError>
pub fn toggle_sharp(&self) -> Result<Self, PitchError>
Creates a new note with the sharp accidental toggled.
If the note has no accidental, it becomes sharp. If the note is already sharp, it becomes natural.
§Returns
Ok(Note)- A new note with the sharp toggled.Err(PitchError)- If the operation would result in an invalid pitch.
Sourcepub fn toggle_flat(&self) -> Result<Self, PitchError>
pub fn toggle_flat(&self) -> Result<Self, PitchError>
Creates a new note with the flat accidental toggled.
If the note has no accidental, it becomes flat. If the note is already flat, it becomes natural.
§Returns
Ok(Note)- A new note with the flat toggled.Err(PitchError)- If the operation would result in an invalid pitch.
Sourcepub fn toggle_natural(&self) -> Result<Self, PitchError>
pub fn toggle_natural(&self) -> Result<Self, PitchError>
Creates a new note with the natural accidental toggled.
If the note has an accidental (sharp or flat), it becomes natural. If the note is already natural, this may have no effect depending on the key signature.
§Returns
Ok(Note)- A new note with the natural toggled.Err(PitchError)- If the operation would result in an invalid pitch.
Sourcepub fn toggle_tie(&self) -> Note
pub fn toggle_tie(&self) -> Note
Creates a new note with the tie state toggled through its cycle.
The tie state cycles through four states:
- No tie:
tie=false, tied=false - Tie start:
tie=true, tied=false - Tie end:
tie=false, tied=true - Tie middle:
tie=true, tied=true
§Returns
A new note with the tie state advanced to the next state in the cycle.
Sourcepub fn base_velocity(&self) -> Velocity
pub fn base_velocity(&self) -> Velocity
Returns the base velocity of the note (before trimmer adjustments).
§Returns
The base velocity value.
Source§impl Note
impl Note
Sourcepub const MAX_SCORE_OFFSET: i32 = 76i32
pub const MAX_SCORE_OFFSET: i32 = 76i32
The maximum score offset value (76 semitones, covering the full MIDI range).
Sourcepub const TICK_CLIPPER: Clipper<i32>
pub const TICK_CLIPPER: Clipper<i32>
Clipper for tick values, ensuring they stay within valid range.
Sourcepub const VELOCITY_CLIPPER: Clipper<i16>
pub const VELOCITY_CLIPPER: Clipper<i16>
Clipper for velocity values, ensuring they stay within MIDI range (0-127).
Sourcepub const LONGEST_TICK_LEN: Lazy<u32>
pub const LONGEST_TICK_LEN: Lazy<u32>
The longest possible tick length for a note.
This is calculated as a whole note with 7 dots at the slowest denominator, multiplied by the maximum duration trimmer rate.