Skip to content

Commit

Permalink
Extract the consts and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
veminovici committed Mar 17, 2024
1 parent 0a4ccd9 commit cee5044
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
89 changes: 77 additions & 12 deletions src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ use super::{Tone, OCTAVE};
pub struct Note(i8);

impl Note {
pub fn base(&self) -> Self {
const M: i8 = 3;
let o = u8::from(OCTAVE) as i8;
const M: i8 = 3;
const O: i8 = OCTAVE.inner() as i8;

let mut note = self.0 + M;
/// Returns the base note (the one in the C4 octave)
pub fn base(&self) -> Self {
let mut note = self.0 + Self::M;
if note < 0 {
let p = note / o;
note += (p.abs() + 1) * o;
let p = note / Self::O;
note += (p.abs() + 1) * Self::O;
}

note %= o;
note -= M;
note %= Self::O;
note -= Self::M;

let b: Self = note.into();
debug_assert_eq!(
Expand All @@ -35,44 +36,108 @@ impl Note {
b
}

/// Returns the octave for the given note (eg. C4)
pub fn octave(&self) -> i8 {
let octave = (self.0 + 3) / u8::from(OCTAVE) as i8;
let rest = (self.0 + 3) % u8::from(OCTAVE) as i8;
let octave = (self.0 + Self::M) / Self::O;
let rest = (self.0 + Self::M) % Self::O;
if rest >= 0 {
octave + 4
octave + Self::M + 1
} else {
octave + 3
octave + Self::M
}
}

//
// Functions which build chords
//

/// Builds a diminished7 chord with the root in the current note.
///
/// # Example
/// ```
/// use musika_rs::*;
///
/// let chord = C.diminished7();
/// println!("{:X}", chord);
/// ```
pub fn diminished7(self) -> chords::Diminished7 {
chords::Diminished7::from(self)
}

/// Builds a dominant7 choard with the root in the current note.
///
/// # Example
/// ```
/// use musika_rs::*;
///
/// let chord = C.dominant7();
/// println!("{:X}", chord);
/// ```
pub fn dominant7(self) -> chords::Dominant7 {
chords::Dominant7::from(self)
}

/// Builds a major chord with the root in the current note.
///
/// # Example
/// ```
/// use musika_rs::*;
///
/// let chord = C.major();
/// println!("{:X}", chord);
/// ```
pub fn major(self) -> chords::Major {
chords::Major::from(self)
}

/// Builds a major7 chord with the root in the current note.
///
/// # Example
/// ```
/// use musika_rs::*;
///
/// let chord = C.major7();
/// println!("{:X}", chord);
/// ```
pub fn major7(self) -> chords::Major7 {
chords::Major7::from(self)
}

/// Builds a minor chord with the root in the current note.
///
/// # Example
/// ```
/// use musika_rs::*;
///
/// let chord = C.minor();
/// println!("{:X}", chord);
/// ```
pub fn minor(self) -> chords::Minor {
chords::Minor::from(self)
}

/// Builds a minor7 chord with the root in the current note.
///
/// # Example
/// ```
/// use musika_rs::*;
///
/// let chord = C.minor7();
/// println!("{:X}", chord);
/// ```
pub fn minor7(self) -> chords::Minor7 {
chords::Minor7::from(self)
}

/// Builds a minor7b5 chord with the root in the current note.
///
/// # Example
/// ```
/// use musika_rs::*;
///
/// let chord = C.minor7b5();
/// println!("{:X}", chord);
/// ```
pub fn minor7b5(self) -> chords::Minor7b5 {
chords::Minor7b5::from(self)
}
Expand Down
7 changes: 7 additions & 0 deletions src/tone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use std::fmt::{Debug, Display};
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct Tone(u8);

impl Tone {
/// Returns the inner value of the tone.
pub(crate) const fn inner(&self) -> u8 {
self.0
}
}

pub const SEMI_TONE: Tone = Tone(1);
pub const TONE: Tone = Tone(2);
pub const OCTAVE: Tone = Tone(12);
Expand Down

0 comments on commit cee5044

Please sign in to comment.