Skip to content

Commit

Permalink
Refactor the chords
Browse files Browse the repository at this point in the history
  • Loading branch information
veminovici committed Mar 19, 2024
1 parent 9cd3303 commit 6934ed6
Show file tree
Hide file tree
Showing 27 changed files with 455 additions and 1,712 deletions.
40 changes: 13 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@
A Rust crate for musical basic elements.

```rust
use musika_rs::{
chords::{self},
C, C_SHARP,
};
use musika_rs::C;

let chord = chords::Major::from(C);
println!("Major C: {chord:X}");
let chord = C.maj();
println!("{chord:X}");

let chord = chords::Major7::from(C);
println!("Major7 C: {chord:X}");
let chord = C.maj7();
println!("{chord:X}");

let chord = chords::Major7::from(C_SHARP);
println!("Major7 C#: {chord:X}");
let chord = C.min();
println!("{chord:X}");

let chord = chords::Dominant7::from(C);
println!("Dom7 C: {chord:X}");
let chord = C.dom7();
println!("{chord:X}");
```

You can find more examples in the [examples][examples_folder] folder.
Expand All @@ -44,33 +41,21 @@ The create allows you to build the following chords:

You can find all the chords in the [chords][chords_folder] folder.

```rust
use musika_rs::*

let cmaj = C.major();
println!("{:X}", cmaj);

let amin = A.minor();
println!("{:X}", amin);

let cmaj7 = C.major7();
println!("{:X}", cmaj7);

```

## Piano Exercises
You can find all piano exercises implemented in the exercises examples. You can see the practices by running:
```bsh
cargo run --example exercise1
cargo run --example exercise2
cargo run --example exercise3
cargo run --example exercise4
cargo run --example exercise5
```

- [Exercise 1][exercise1_file] | C - Am | F - G |
- [Exercise 2][exercise2_file] | C - G | Am - F | C - G | F - Em - Dm - C |
- [Exercise 3][exercise3_file] | Cx4 | Gx4 | Gx4 | Cx4 | Fx4 | Cx4 | Gx4 | Cx4 |
- [Exercise 4][exercise4_file] | C | F | Bdim | Em | Am | Dm | G | C |
- [Exercise 4][exercise4_file] | C | F | Bdim | Em | Am | Dm | G | C |

## Resources
- [Piano Chords][piano_chords_url]
Expand Down Expand Up @@ -102,4 +87,5 @@ cargo run --example exercise4
[exercise1_file]: ./examples/exercise1.rs
[exercise2_file]: ./examples/exercise2.rs
[exercise3_file]: ./examples/exercise3.rs
[exercise4_file]: ./examples/exercise4.rs
[exercise4_file]: ./examples/exercise4.rs
[exercise5_file]: ./examples/exercise5.rs
21 changes: 9 additions & 12 deletions examples/chords.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use musika_rs::{
chords::{self},
C, C_SHARP,
};
use musika_rs::C;

fn main() {
let chord = chords::Major::from(C);
println!("Major C: {chord:X}");
let chord = C.maj();
println!("{chord:X}");

let chord = chords::Major7::from(C);
println!("Major7 C: {chord:X}");
let chord = C.maj7();
println!("{chord:X}");

let chord = chords::Major7::from(C_SHARP);
println!("Major7 C#: {chord:X}");
let chord = C.min();
println!("{chord:X}");

let chord = chords::Dominant7::from(C);
println!("Dom7 C: {chord:X}");
let chord = C.dom7();
println!("{chord:X}");
}
8 changes: 4 additions & 4 deletions examples/exercise1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use musika_rs::*;
fn main() {
println!();
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]
println!("{:X}", C.maj()); // C [C, E, G]
println!("{:X}", A.min()); // Am [A, C, E]
println!("{:X}", F.maj()); // F [F, A, C]
println!("{:X}", G.maj()); // G [G, B, D]
}
20 changes: 10 additions & 10 deletions examples/exercise2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use musika_rs::*;
fn main() {
println!();
println!("Exercise 2: | C - G | Am - F | C - G | F - Em - Dm - C |");
println!("{:X}", C.major()); // C [C, E, G]
println!("{:X}", G.major()); // G [G, B, D]
println!("{:X}", A.minor()); // Am [A, C, E]
println!("{:X}", F.major()); // F [F, A, C]
println!("{:X}", C.major()); // C [C, E, G]
println!("{:X}", G.major()); // G [G, B, D]
println!("{:X}", F.major()); // F [F, A, C]
println!("{:X}", E.minor()); // Em [E, G, B]
println!("{:X}", D.minor()); // Dm [D, F, A]
println!("{:X}", C.major()); // C [C, E, G]
println!("{:X}", C.maj()); // C [C, E, G]
println!("{:X}", G.maj()); // G [G, B, D]
println!("{:X}", A.min()); // Am [A, C, E]
println!("{:X}", F.maj()); // F [F, A, C]
println!("{:X}", C.maj()); // C [C, E, G]
println!("{:X}", G.maj()); // G [G, B, D]
println!("{:X}", F.maj()); // F [F, A, C]
println!("{:X}", E.min()); // Em [E, G, B]
println!("{:X}", D.min()); // Dm [D, F, A]
println!("{:X}", C.maj()); // C [C, E, G]
}
6 changes: 3 additions & 3 deletions examples/exercise3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ fn main() {
println!();
println!("Exercise 3: | Cx4 | Gx4 | Gx4 | Cx4 | Fx4 | Cx4 | Gx4 | Cx4 |");

let cmaj = C.major();
let gmaj = G.major();
let fmaj = F.major();
let cmaj = C.maj();
let gmaj = G.maj();
let fmaj = F.maj();

// bar 1
for _ in 0..4 {
Expand Down
16 changes: 8 additions & 8 deletions examples/exercise4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ fn main() {
println!();
println!("Exercise 4: | C | F | Bdim | Em | Am | Dm | G | C |");

println!("{:X}", C.major());
println!("{:X}", F.major());
println!("{:X}", B.diminished());
println!("{:X}", E.minor());
println!("{:X}", A.minor());
println!("{:X}", D.minor());
println!("{:X}", G.major());
println!("{:X}", C.major());
println!("{:X}", C.maj());
println!("{:X}", F.maj());
println!("{:X}", B.dim());
println!("{:X}", E.min());
println!("{:X}", A.min());
println!("{:X}", D.min());
println!("{:X}", G.maj());
println!("{:X}", C.maj());
}
16 changes: 8 additions & 8 deletions examples/exercise5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ fn main() {
println!();
println!("Exercise 5: | Dm9x2 | G13x2 | Cmaj9x2 | Fmaj13X2 | A7 | Dm | G | C |");

println!("{:X}", D.minor9()); // D - F - A - C - E
println!("{:X}", D.minor9()); // D - F - A - C - E
println!("{:X}", G.dominant9()); // G - F - A - B - E
println!("{:X}", C.major9()); // C - E - G - B - D
// TODO: Find the Fmaj13 chord (4:48)
println!("{:X}", F.major9()); // F - E - G - A - D
// TODO: Find the A7 b5 b13
println!("{:X}", A.dominant9()); // G - F - A - B - E
println!("{:X}", D.min9()); // D - F - A - C - E
println!("{:X}", D.min9()); // D - F - A - C - E
println!("{:X}", G.dom9()); // G - F - A - B - E
println!("{:X}", C.maj9()); // C - E - G - B - D
// TODO: Find the Fmaj13 chord (4:48)
println!("{:X}", F.maj9()); // F - E - G - A - D
// TODO: Find the A7 b5 b13
println!("{:X}", A.dom9()); // G - F - A - B - E
}
47 changes: 13 additions & 34 deletions src/bar.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
use crate::chords::Chords;
use std::fmt::Display;

use crate::{chords::Chord, Note};

pub enum BarElement<C>
where
C: Chord<IntoIter = <Vec<Note> as IntoIterator>::IntoIter>,
{
pub enum BarElement {
Silence(u8),
Chord(C, u8),
Chord(Chords, u8),
}

impl<C> Display for BarElement<C>
where
C: Display + Chord<IntoIter = <Vec<Note> as IntoIterator>::IntoIter>,
{
impl Display for BarElement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BarElement::Silence(_) => write!(f, "_"),
Expand All @@ -22,19 +15,14 @@ where
}
}

pub struct Bar<C>(Vec<BarElement<C>>)
where
C: Chord<IntoIter = <Vec<Note> as IntoIterator>::IntoIter>;
pub struct Bar(Vec<BarElement>);

impl<C> Bar<C>
where
C: Chord<IntoIter = <Vec<Note> as IntoIterator>::IntoIter>,
{
impl Bar {
pub fn new() -> Self {
Self(vec![])
}

pub fn with_chord(self, chord: C, fraction: u8) -> Self {
pub fn with_chord(self, chord: Chords, fraction: u8) -> Self {
let mut items = self.0;
items.push(BarElement::Chord(chord, fraction));
Self(items)
Expand All @@ -47,19 +35,13 @@ where
}
}

impl<C> Default for Bar<C>
where
C: Chord<IntoIter = <Vec<Note> as IntoIterator>::IntoIter>,
{
impl Default for Bar {
fn default() -> Self {
Self::new()
}
}

impl<C> Display for Bar<C>
where
C: Display + Chord<IntoIter = <Vec<Note> as IntoIterator>::IntoIter>,
{
impl Display for Bar {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = self
.0
Expand All @@ -71,10 +53,7 @@ where
}
}

pub fn show<C>(bars: impl Iterator<Item = Bar<C>>) -> String
where
C: Display + Chord<IntoIter = <Vec<Note> as IntoIterator>::IntoIter>,
{
pub fn show(bars: impl Iterator<Item = Bar>) -> String {
let s = bars
.map(|b| b.to_string())
.collect::<Vec<String>>()
Expand All @@ -90,14 +69,14 @@ mod tests {

#[test]
fn display() {
let bar = Bar::new().with_chord(C.major(), 2).with_chord(G.major(), 2);
let bar = Bar::new().with_chord(C.maj(), 2).with_chord(G.maj(), 2);
assert_eq!(bar.to_string(), "C - G");
}

#[test]
fn displa_bars() {
let bar1 = Bar::new().with_chord(C.major(), 2).with_chord(G.major(), 2);
let bar2 = Bar::new().with_chord(C.major(), 2).with_chord(G.major(), 2);
let bar1 = Bar::new().with_chord(C.maj(), 2).with_chord(G.maj(), 2);
let bar2 = Bar::new().with_chord(C.maj(), 2).with_chord(G.maj(), 2);
let bars = [bar1, bar2];

let s = show(bars.into_iter());
Expand Down
Loading

0 comments on commit 6934ed6

Please sign in to comment.