From 0a4ccd96c8bd8eff70c4fb72c818fb9ec567a3a3 Mon Sep 17 00:00:00 2001 From: VLAD EMINOVICI Date: Sun, 17 Mar 2024 21:43:41 +0200 Subject: [PATCH] Add a better way to create chords --- README.md | 15 +++++++++------ examples/practices.rs | 10 +++++----- src/note.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 70a4e2d..8993b5f 100644 --- a/README.md +++ b/README.md @@ -32,17 +32,20 @@ println!("Dom7 C: {chord:X}"); You can find more examples in the [examples][examples_folder] folder. ## Piano Exercises -You can find all piano exercises implemented in the [practices][practices_file] file. +You can find all piano exercises implemented in the [practices][practices_file] file. You can see the practices by running: +```bsh +cargo run --example practices +``` ### Exercise 1 (C - Am - F - G) ```rust use musika_rs::*; -println!("Exercise 1: (C - Am - F - G"); -println!("{:X}", chords::Major::from(C)); // C -println!("{:X}", chords::Minor::from(A)); // Amin -println!("{:X}", chords::Major::from(F)); // F -println!("{:X}", chords::Major::from(G)); // G +println!("Exercise 1: (C - Am - F - G)"); +println!("{:X}", C.major()); // C [C, E, G] +println!("{:X}", A.minor()); // Am [A, C, E] +println!("{:X}", F.major()); // F [F, A, C] +println!("{:X}", G.major()); // G [G, B, D] ``` ## Resources diff --git a/examples/practices.rs b/examples/practices.rs index 131a4bf..a8fe72b 100644 --- a/examples/practices.rs +++ b/examples/practices.rs @@ -1,11 +1,11 @@ use musika_rs::*; fn exercise() { - println!("Exercise 1: (C - Am - F - G"); - println!("{:X}", chords::Major::from(C)); // C - println!("{:X}", chords::Minor::from(A)); // Amin - println!("{:X}", chords::Major::from(F)); // F - println!("{:X}", chords::Major::from(G)); // G + println!("Exercise 1: (C - Am - F - G)"); + println!("{:X}", C.major()); // C [C, E, G] + println!("{:X}", A.minor()); // Am [A, C, E] + println!("{:X}", F.major()); // F [F, A, C] + println!("{:X}", G.major()); // G [G, B, D] } fn main() { diff --git a/src/note.rs b/src/note.rs index a2042a7..e277e18 100644 --- a/src/note.rs +++ b/src/note.rs @@ -3,6 +3,8 @@ use std::{ ops::{Add, Sub}, }; +use crate::chords; + use super::{Tone, OCTAVE}; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] @@ -42,6 +44,38 @@ impl Note { octave + 3 } } + + // + // Functions which build chords + // + + pub fn diminished7(self) -> chords::Diminished7 { + chords::Diminished7::from(self) + } + + pub fn dominant7(self) -> chords::Dominant7 { + chords::Dominant7::from(self) + } + + pub fn major(self) -> chords::Major { + chords::Major::from(self) + } + + pub fn major7(self) -> chords::Major7 { + chords::Major7::from(self) + } + + pub fn minor(self) -> chords::Minor { + chords::Minor::from(self) + } + + pub fn minor7(self) -> chords::Minor7 { + chords::Minor7::from(self) + } + + pub fn minor7b5(self) -> chords::Minor7b5 { + chords::Minor7b5::from(self) + } } impl Display for Note {