Struct musical_note::Note

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

Not yet resolved Note, constructed by MIDI.

Implementations§

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(&notes_map, midi_note, octave);
        if res.is_some() {
            return res.unwrap();
        }
        let scale = key.resolve_scale(&notes_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)
    }
Examples found in repository?
src/lib.rs (line 234)
233
234
235
pub fn midi_to_note(midi: u8, key: Key, accidental: Option<Accidental>) -> ResolvedNote {
    Note::from_midi(midi, accidental).resolve(key)
}

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
Deserialize this value from the given Serde deserializer. Read more
Converts to this type from the input type.

from midi, but without boilerplate.

This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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
Serialize this value into the given Serde serializer. Read more

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
Uses borrowed data to replace owned data, usually by cloning. 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.