1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4
5use crate::prelude::{Octave, Pitch, Syllable, Semitones};
6
7#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Debug)]
8pub struct Note {
9 pub octave: Octave,
10 pub pitch: Pitch,
11 pub syllable: Syllable,
12}
13
14impl Display for Note {
15 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 write!(f, "{} {} {}", self.octave, self.pitch, self.syllable)
17 }
18}
19
20impl Note {
21 pub fn new(octave: Octave, pitch: Pitch, syllable: Syllable) -> Self {
22 Self { octave, pitch, syllable }
23 }
24}
25
26impl From<(Octave, Pitch, Syllable)> for Note {
27 fn from(v: (Octave, Pitch, Syllable)) -> Self {
28 Self::new(v.0, v.1, v.2)
29 }
30}
31
32impl From<Note> for Semitones {
33 fn from(v: Note) -> Self {
34 let octave_val = Semitones::from(v.octave).0;
35 let pitch_val = Semitones::from(v.pitch).0;
36 Self(octave_val + pitch_val)
37 }
38}