Skip to content

Commit ae0e215

Browse files
committed
Rename TimeSig::beats_in_a_bar to beats_per_bar. Add TimeSig::ticks_per_bar. Publish 0.12.
1 parent d1c5115 commit ae0e215

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "time_calc"
33
description = "A library for music/DSP time conversions! Provides functions and methods for converting between ticks, ms, samples, bars, beats and measures."
4-
version = "0.11.1"
4+
version = "0.12.0"
55
authors = ["[email protected]"]
66
readme = "README.md"
77
keywords = ["time", "dsp", "audio", "music", "conversion"]

examples/test.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const SAMPLE_HZ: SampleHz = 44_100.0;
2525
const PPQN: Ppqn = 19_200;
2626

2727
fn main() {
28-
2928
println!("time_calc demonstration!");
3029

3130
// Out `Bars` type is a simplified version of a Measure.
@@ -48,12 +47,12 @@ fn main() {
4847

4948
// We also need to know the "time signature" if we are to convert from "Bars".
5049
// This is because different time signatures can have a different duration in "Beats".
51-
let beats_in_a_bar = TimeSig { top: 4, bottom: 4 }.beats_in_a_bar();
52-
println!("Duration of a bar in `Beats` with a 4/4 Time Signature: {}", beats_in_a_bar);
53-
let beats_in_a_bar = TimeSig { top: 3, bottom: 4 }.beats_in_a_bar();
54-
println!("Duration of a bar in `Beats` with a 3/4 Time Signature: {}", beats_in_a_bar);
55-
let beats_in_a_bar = TimeSig { top: 7, bottom: 8 }.beats_in_a_bar();
56-
println!("Duration of a bar in `Beats` with a 7/8 Time Signature: {}", beats_in_a_bar);
50+
let beats_per_bar = TimeSig { top: 4, bottom: 4 }.beats_per_bar();
51+
println!("Duration of a bar in `Beats` with a 4/4 Time Signature: {}", beats_per_bar);
52+
let beats_per_bar = TimeSig { top: 3, bottom: 4 }.beats_per_bar();
53+
println!("Duration of a bar in `Beats` with a 3/4 Time Signature: {}", beats_per_bar);
54+
let beats_per_bar = TimeSig { top: 7, bottom: 8 }.beats_per_bar();
55+
println!("Duration of a bar in `Beats` with a 7/8 Time Signature: {}", beats_per_bar);
5756
let time_sig = TimeSig { top: 4, bottom: 4 };
5857
println!("Duration of a bar at 120bpm, 44_100 sample_hz and 4/4 Time Sig in samples: {}",
5958
Bars(1).samples(bpm, time_sig, SAMPLE_HZ));
@@ -66,5 +65,4 @@ fn main() {
6665
Samples(176_400).bars(bpm, time_sig, SAMPLE_HZ));
6766

6867
println!("Great Success!");
69-
7068
}

src/calc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub const SECOND_IN_MS: Ms = 1_000.0;
2828
/// Calculate and return the duration of a bar in milliseconds.
2929
#[inline]
3030
pub fn bar_in_ms(bpm: Bpm, ts: TimeSig) -> Ms {
31-
beat_in_ms(bpm) * ts.beats_in_a_bar()
31+
beat_in_ms(bpm) * ts.beats_per_bar()
3232
}
3333

3434
/// Calculate and return the duration of a beat in milliseconds.

src/division.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Division {
4141
pub fn beats(&self, ts: TimeSig) -> f64 {
4242
use num::Float;
4343
match *self {
44-
Division::Bar => ts.beats_in_a_bar(),
44+
Division::Bar => ts.beats_per_bar(),
4545
_ => 2.0.powi(Division::Beat as i32 - *self as i32),
4646
}
4747
}
@@ -50,7 +50,7 @@ impl Division {
5050
pub fn bars(&self, ts: TimeSig) -> f64 {
5151
match *self {
5252
Division::Bar => 1.0,
53-
_ => self.beats(ts) / ts.beats_in_a_bar(),
53+
_ => self.beats(ts) / ts.beats_per_bar(),
5454
}
5555
}
5656

src/time_sig.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use {Bars, Ppqn, Ticks};
2+
13
pub type Top = u16;
24
pub type Bottom = u16;
35

@@ -9,9 +11,17 @@ pub struct TimeSig {
911
}
1012

1113
impl TimeSig {
14+
1215
/// Return how many beats there are in a bar under this time signature.
1316
#[inline]
14-
pub fn beats_in_a_bar(&self) -> f64 {
17+
pub fn beats_per_bar(&self) -> f64 {
1518
4.0 * self.top as f64 / self.bottom as f64
1619
}
20+
21+
/// The number of `Ticks` in a single `Bar` with this `TimeSig`.
22+
#[inline]
23+
pub fn ticks_per_bar(&self, ppqn: Ppqn) -> Ticks {
24+
Bars(1).to_ticks(*self, ppqn)
25+
}
26+
1727
}

0 commit comments

Comments
 (0)