Skip to content

Commit

Permalink
round tap tempo to nearest 100th
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdinosaur committed May 26, 2018
1 parent 93051e5 commit 533544e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::time::{Duration, Instant};
use std::thread::{sleep, spawn};
use std::sync::mpsc::{channel, Sender, TryRecvError};
use num::rational::Ratio;
use num::integer::Integer;

use metronome;

Expand Down Expand Up @@ -348,7 +349,7 @@ impl Clock {
let tap_beats_per_nanos = Ratio::from_integer(1) / tap_nanos;
let tap_beats_per_seconds = tap_beats_per_nanos * Ratio::from_integer(NANOS_PER_SECOND);
let beats_per_minute = tap_beats_per_seconds * Ratio::from_integer(SECONDS_PER_MINUTE);
next_tempo = Some(beats_per_minute);
next_tempo = Some(round_to_nearest(beats_per_minute, 100));
}
}

Expand All @@ -361,3 +362,8 @@ impl Clock {
fn duration_to_nanos (duration: Duration) -> i64 {
duration.as_secs() as i64 * 1_000_000_000 + duration.subsec_nanos() as i64
}

fn round_to_nearest<T: Clone + Copy + Integer> (value: Ratio<T>, quantum: T) -> Ratio<T> {
let quantum_rat = Ratio::from_integer(quantum);
(value * quantum_rat).round() / quantum_rat
}

0 comments on commit 533544e

Please sign in to comment.