aboutsummaryrefslogtreecommitdiff
path: root/src/note.rs
blob: 053ce15606120882ad11f1af716164c21382031f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This file is part of Guitar <https://github.com/Fuwn/guitar>.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

use crate::{pitch::Pitch, unit::Octave};

/// A note which only keeps track of its frequency, octave, and name; enables
/// for a more flexible note system; including conversions, enharmonics, and
/// more.
///
/// # Examples
///
/// ```rust
/// let _ = guitar::Note::new("A", 4); 
/// ```
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Note {
  pitch: Pitch,
}

impl Note {
  /// Creates a new, specific [`Note`] given a note's name, octave, and
  /// duration.
  ///
  /// # Examples
  ///
  /// ```rust
  /// let _ = guitar::Note::new("A", 4); 
  /// ```
  #[must_use]
  pub fn new(pitch: &str, octave: Octave) -> Self {
    let pitch = Pitch::new(pitch, octave);

    Self::new_from_pitch(pitch)
  }

  /// Creates a new, specific [`Note`] given a frequency and a [`Pitch`].
  ///
  /// This is an internal command which is made obsolete by the other,
  /// public-acing `new_from_*` methods.
  #[must_use]
  pub const fn new_from_pitch(pitch: Pitch) -> Self { Self { pitch } }

  /// Returns the [`Pitch`] of the [`Note`] for further manipulation.
  pub fn pitch(&mut self) -> &Pitch { &self.pitch }

  /// Returns the [`Pitch`] of the [`Note`] for further manipulation.
  pub fn pitch_mut(&mut self) -> &mut Pitch { &mut self.pitch }
}