Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute voice leading for a sequence of chords #61

Merged
merged 25 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ categories = ["command-line-utilities"]
[dependencies]
itertools = "0.10"
lazy_static = "1.4"
petgraph = "0.5"
structopt = "0.3"

[dev-dependencies]
Expand Down
34 changes: 34 additions & 0 deletions src/chord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ impl Chord {
.filter(|voicing| voicing.spells_out(self) && voicing.get_span() <= config.max_span)
.sorted()
}

pub fn transpose(&self, semitones: i8) -> Chord {
match semitones {
s if s < 0 => *self - semitones.abs() as Semitones,
_ => *self + semitones as Semitones,
}
}
}

impl fmt::Display for Chord {
Expand Down Expand Up @@ -747,4 +754,31 @@ mod tests {
let c = Chord::from_str(chord).unwrap();
assert_eq!(c - n, Chord::from_str(result).unwrap());
}

#[rstest(
chord,
n,
result,
case("C", 0, "C"),
case("C#", 0, "C#"),
case("Db", 0, "Db"),
case("Cm", 1, "C#m"),
case("Cmaj7", 2, "Dmaj7"),
case("Cdim", 4, "Edim"),
case("C#", 2, "D#"),
case("A#m", 3, "C#m"),
case("A", 12, "A"),
case("A#", 12, "A#"),
case("Ab", 12, "Ab"),
case("Cm", -1, "Bm"),
case("Cmaj7", -2, "Bbmaj7"),
case("Adim", -3, "Gbdim"),
case("A", -12, "A"),
case("A#", -12, "A#"),
case("Ab", -12, "Ab")
)]
fn test_transpose(chord: &str, n: i8, result: &str) {
let c = Chord::from_str(chord).unwrap();
assert_eq!(c.transpose(n), Chord::from_str(result).unwrap());
}
}
5 changes: 4 additions & 1 deletion src/chord_chart.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cmp::max;
use std::fmt;

use crate::{FretID, Semitones, UkeString, Voicing};
use crate::{FretID, Semitones, UkeString, Voicing, MIN_CHART_WIDTH};

pub struct ChordChart {
voicing: Voicing,
Expand All @@ -10,6 +11,8 @@ pub struct ChordChart {

impl ChordChart {
pub fn new(voicing: Voicing, width: Semitones) -> Self {
let width = max(width, MIN_CHART_WIDTH);

assert!(voicing.get_span() <= width);

Self { voicing, width }
Expand Down
77 changes: 77 additions & 0 deletions src/chord_sequence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::slice::Iter;
use std::str::FromStr;

use crate::Chord;

#[derive(Debug, PartialEq)]
pub struct ChordSequence {
chords: Vec<Chord>,
}

impl ChordSequence {
pub fn chords(&self) -> Iter<'_, Chord> {
self.chords.iter()
}

pub fn transpose(&self, semitones: i8) -> ChordSequence {
let chords = self.chords().map(|c| c.transpose(semitones)).collect();
Self { chords }
}
}

impl FromStr for ChordSequence {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let res: Result<Vec<_>, _> = s.split_whitespace().map(|s| Chord::from_str(s)).collect();

if let Ok(chords) = res {
return Ok(Self { chords });
}

Err("Could not parse chord sequence")
}
}

#[cfg(test)]
mod tests {
use rstest::rstest;

use super::*;

#[rstest(
chord_seq,
chords,
case("", &[]),
case("C", &["C"]),
case("C F G", &["C", "F", "G"]),
case("Dsus2 Am7 C#", &["Dsus2", "Am7", "C#"]),
)]
fn test_from_str(chord_seq: &str, chords: &[&str]) {
let cs = ChordSequence::from_str(chord_seq).unwrap();
let chords1: Vec<Chord> = cs.chords().cloned().collect();
let chords2: Vec<Chord> = chords.iter().map(|c| Chord::from_str(c).unwrap()).collect();
assert_eq!(chords1, chords2);
}

#[rstest(chord_seq, case("Z"), case("A Z"))]
fn test_from_str_fail(chord_seq: &str) {
assert!(ChordSequence::from_str(chord_seq).is_err())
}

#[rstest(
chord_seq1,
semitones,
chord_seq2,
case("", 0, ""),
case("C F G", 0, "C F G"),
case("C F G", 1, "C# F# G#"),
case("C F G", -1, "B E Gb"),
case("C F G", 12, "C F G"),
)]
fn test_transpose(chord_seq1: &str, semitones: i8, chord_seq2: &str) {
let cs1 = ChordSequence::from_str(chord_seq1).unwrap();
let cs2 = ChordSequence::from_str(chord_seq2).unwrap();
assert_eq!(cs1.transpose(semitones), cs2);
}
}
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub mod chord;
pub mod chord_chart;
pub mod chord_sequence;
pub mod chord_type;
pub mod fret_pattern;
pub mod interval;
Expand All @@ -10,9 +11,11 @@ pub mod pitch_class;
pub mod staff_position;
pub mod tuning;
pub mod voicing;
pub mod voicing_graph;

pub use chord::Chord;
pub use chord_chart::ChordChart;
pub use chord_sequence::ChordSequence;
pub use chord_type::ChordType;
pub use fret_pattern::FretPattern;
pub use interval::Interval;
Expand All @@ -21,13 +24,17 @@ pub use pitch_class::PitchClass;
pub use staff_position::StaffPosition;
pub use tuning::Tuning;
pub use voicing::Voicing;
pub use voicing_graph::VoicingGraph;

/// Number of strings on our string instrument.
pub const STRING_COUNT: usize = 4;

/// Number of fingers on our left hand to be used for pressing down strings.
pub const FINGER_COUNT: usize = 4;

/// Minimal number of frets to be shown in a chord chart.
pub const MIN_CHART_WIDTH: Semitones = 4;

/// The ID of a fret on the fretboard. 0 corresponds to the nut,
/// 1 corresponds to the first fret, 2 to the second etc.
pub type FretID = u8;
Expand Down
Loading