Skip to content

Commit

Permalink
See if play can be tested in GA, and fix b5.
Browse files Browse the repository at this point in the history
  • Loading branch information
twitchax committed Dec 21, 2022
1 parent 8f9032c commit 9c249df
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use rodio::{OutputStream, Sink, source::SineWave, Source};
#[command(author, version, about, long_about = None)]
struct Args {
#[command(subcommand)]
command: Option<Commands>,
command: Option<Command>,
}

#[derive(Subcommand, Debug)]
enum Commands {
enum Command {
/// Describes a chord
Describe {
/// Chord symbol to parse
Expand Down Expand Up @@ -46,13 +46,19 @@ enum Commands {
fn main() -> Void {
let args = Args::parse();

start(args)?;

Ok(())
}

fn start(args: Args) -> Void {
match args.command {
Some(Commands::Describe { symbol, octave }) => {
Some(Command::Describe { symbol, octave }) => {
let chord = Chord::parse(&symbol)?.with_octave(Octave::Zero + octave);

describe(&chord);
}
Some(Commands::Play { symbol, octave, delay, length }) => {
Some(Command::Play { symbol, octave, delay, length }) => {
let chord = Chord::parse(&symbol)?.with_octave(Octave::Zero + octave);

play(&chord, delay, length);
Expand Down Expand Up @@ -89,4 +95,33 @@ fn play(chord: &Chord, delay: f32, length: f32) {
}

std::thread::sleep(Duration::from_secs_f32(length));
}

// Tests.

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

#[test]
fn test_describe() {
start(Args {
command: Some(Command::Describe {
symbol: "Cmaj7".to_string(),
octave: 4,
}),
}).unwrap();
}

#[test]
fn test_play() {
start(Args {
command: Some(Command::Play {
symbol: "Cmaj7".to_string(),
octave: 3,
delay: 0.01,
length: 0.1,
}),
}).unwrap();
}
}
9 changes: 9 additions & 0 deletions src/chord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ impl HasName for Chord {

// Add special modifiers that are true modifiers when not part of their "special case".

if self.modifiers.contains(&Modifier::Flat5) && !known_name.contains("(♭5)") {
name.push_str("(♭5)");
}

if self.modifiers.contains(&Modifier::Flat9) && !known_name.contains("(♭9)") {
name.push_str("(♭9)");
}
Expand Down Expand Up @@ -669,6 +673,11 @@ impl HasRelativeChord for Chord {

// Special modifiers that can also be extensions.

if modifiers.contains(&Modifier::Flat5) {
result.remove(2);
result.push(Interval::DiminishedFifth);
}

if modifiers.contains(&Modifier::Flat9) {
result.push(Interval::MinorNinth);
}
Expand Down

0 comments on commit 9c249df

Please sign in to comment.