Skip to content

Commit

Permalink
Conserve the structure of scale items in custom scales
Browse files Browse the repository at this point in the history
  • Loading branch information
Woyten committed Jun 21, 2020
1 parent c140f80 commit 2ffd5e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
32 changes: 28 additions & 4 deletions tune-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tune::key::PianoKey;
use tune::key_map::KeyMap;
use tune::mts::{DeviceId, SingleNoteTuningChange, SingleNoteTuningChangeMessage};
use tune::pitch::{Pitch, ReferencePitch};
use tune::ratio::Ratio;
use tune::ratio::{Ratio, RatioExpression, RatioExpressionVariant};
use tune::scale;
use tune::scale::Scale;
use tune::tuning::{ConcertPitch, Tuning};
Expand Down Expand Up @@ -158,7 +158,7 @@ enum ScaleCommand {
#[structopt(name = "cust")]
Custom {
/// Items of the scale
items: Vec<Ratio>,
items: Vec<RatioExpression>,

/// Name of the scale
#[structopt(short = "n")]
Expand Down Expand Up @@ -481,14 +481,38 @@ fn create_scale(command: ScaleCommand) -> Scale {
}
}

fn create_custom_scale(items: Vec<Ratio>, name: String) -> Scale {
fn create_custom_scale(items: Vec<RatioExpression>, name: String) -> Scale {
let mut scale = Scale::with_name(name);
for item in items {
scale.push_ratio(item);
match item.variant() {
RatioExpressionVariant::Float { float_value } => {
if let Some(float_value) = as_int(float_value) {
scale.push_fraction(float_value, 1);
continue;
}
}
RatioExpressionVariant::Fraction { numer, denom } => {
if let (Some(numer), Some(denom)) = (as_int(numer), as_int(denom)) {
scale.push_fraction(numer, denom);
continue;
}
}
_ => {}
}
scale.push_ratio(item.ratio());
}
scale.build()
}

fn as_int(float: f64) -> Option<u32> {
let rounded = float.round();
if (float - rounded).abs() < 1e-6 {
Some(rounded as u32)
} else {
None
}
}

fn create_key_map(key_map_params: KeyMapParams) -> KeyMap {
KeyMap {
ref_pitch: key_map_params.ref_pitch,
Expand Down
4 changes: 2 additions & 2 deletions tune-cli/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ fn crate_custom_scale() {
"-n",
"Just intonation",
"9/8",
"5/4",
"1.25",
"4/3",
"3/2",
"1.5",
"5/3",
"15/8",
"2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Just intonation
7
203.910
9/8
386.314
498.045
4/3
701.955
884.359
1088.269
1200.000
5/3
15/8
2/1

0 comments on commit 2ffd5e8

Please sign in to comment.