Struct musical_note::Note
source · pub struct Note { /* private fields */ }Expand description
Not yet resolved Note, constructed by MIDI.
Implementations§
source§impl Note
impl Note
sourcepub fn from_midi(midi: u8, accidental: Option<Accidental>) -> Self
pub fn from_midi(midi: u8, accidental: Option<Accidental>) -> Self
Examples found in repository?
src/lib.rs (line 234)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
pub fn midi_to_note(midi: u8, key: Key, accidental: Option<Accidental>) -> ResolvedNote {
Note::from_midi(midi, accidental).resolve(key)
}
/// Not yet resolved Note, constructed by MIDI.
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Serialize, Deserialize)]
pub struct Note {
midi: u8,
accidental: Option<Accidental>,
octave: Octave,
}
impl Note {
pub fn from_midi(midi: u8, accidental: Option<Accidental>) -> Self {
Self {
midi,
accidental,
octave: Octave::from_midi(midi),
}
}
pub fn resolve(&self, key: Key) -> ResolvedNote {
let (midi_note, octave) = Octave::split_midi(self.midi);
// let midi_note = self.midi % 12;
let notes_map = NotesMap::get();
let res = self.resolve_by_accident(¬es_map, midi_note, octave);
if res.is_some() {
return res.unwrap();
}
let scale = key.resolve_scale(¬es_map);
scale.resolve_pitch(notes_map, midi_note, octave)
}
fn resolve_by_accident(
&self,
notes_map: &NotesMap,
midi_note: u8,
octave: Octave,
) -> Option<ResolvedNote> {
if self.accidental.is_some() {
let acc = self.accidental.as_ref().unwrap();
let note = notes_map.get_by_midi(&midi_note).get(&acc);
if note.is_some() {
return Some(ResolvedNote::new(
note.unwrap().clone(),
acc.clone(),
octave,
octave.apply_to_midi_note(midi_note),
));
}
}
None
}
pub fn midi(&self) -> u8 {
self.midi
}
pub fn set_midi(&mut self, midi: u8) {
self.midi = midi;
}
pub fn accidental(&self) -> Option<Accidental> {
self.accidental.clone()
}
pub fn set_accidental(&mut self, accidental: Option<Accidental>) {
self.accidental = accidental;
}
}
impl From<u8> for Note {
/// from midi, but without boilerplate.
fn from(midi: u8) -> Self {
Self::from_midi(midi, None)
}sourcepub fn resolve(&self, key: Key) -> ResolvedNote
pub fn resolve(&self, key: Key) -> ResolvedNote
pub fn midi(&self) -> u8
pub fn set_midi(&mut self, midi: u8)
pub fn accidental(&self) -> Option<Accidental>
pub fn set_accidental(&mut self, accidental: Option<Accidental>)
Trait Implementations§
source§impl<'de> Deserialize<'de> for Note
impl<'de> Deserialize<'de> for Note
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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<ResolvedNote> for Note
impl From<ResolvedNote> for Note
source§fn from(value: ResolvedNote) -> Self
fn from(value: ResolvedNote) -> Self
Converts to this type from the input type.
source§impl PartialEq<Note> for Note
impl PartialEq<Note> for Note
source§impl PartialOrd<Note> for Note
impl PartialOrd<Note> for Note
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self and other) and is used by the <=
operator. Read more