rust_music_theory/note/note.rs
1use crate::note::Pitch;
2use std::fmt;
3use std::fmt::Formatter;
4
5/// A note.
6#[derive(Debug, Clone)]
7pub struct Note {
8 /// The pitch of the note (A, B, C#, etc).
9 pub pitch: Pitch,
10 /// The octave of the note in standard notation.
11 pub octave: u8,
12}
13
14impl Note {
15 /// Create a new note.
16 pub fn new(pitch: Pitch, octave: u8) -> Self {
17 Note {
18 pitch,
19 octave,
20 }
21 }
22}
23
24impl fmt::Display for Note {
25 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
26 write!(f, "{}", self.pitch)
27 }
28}
29
30/// A type that can produce a sequence of notes.
31pub trait Notes {
32 /// Get the sequence of notes.
33 fn notes(&self) -> Vec<Note>;
34
35 /// Print the sequence of notes.
36 ///
37 /// By default this function will print out each notes' index and its pitch class. For example,
38 /// printing out C major would look like:
39 /// ```text
40 /// Notes:
41 /// 1: C
42 /// 2: E
43 /// 3: G
44 /// ```
45 fn print_notes(&self) {
46 let notes = self.notes();
47
48 println!("Notes:");
49 for (i, note) in notes.iter().enumerate() {
50 println!(" {}: {}", i + 1, note.pitch)
51 }
52 }
53}