3 releases (breaking)
Uses old Rust 2015
0.3.0 | Jun 12, 2016 |
---|---|
0.2.0 | Feb 5, 2016 |
0.1.0 | Feb 4, 2016 |
#19 in #fps
Used in mel
10KB
100 lines
hertz
status: working. tested. api still in flux.
useful functions for working with frame-rates, sample-rates, other rates, time durations, frequencies, etc and for keeping a constant framerate. written in rust.
to use add hertz = "*"
to the [dependencies]
section of your Cargo.toml
and call extern crate hertz;
in your code.
read the documentation for an example and more !
contributing
licensed under either of apache-2.0 (tl;dr) or MIT (tl;dr) at your option
lib.rs
:
useful functions for working with frame-rates, sample-rates, other rates, time durations, frequencies, etc and for keeping a constant framerate.
rate
, sample rate
, frame rate
, fps
, frequency
, etc
express the same concept and are therefore used interchangeably.
you can use hertz to compute the time resolution and frequency range that can be meaningfully analyzed by a short-time fourier transform of a signal:
extern crate hertz;
fn main() {
let sample_rate = 44100;
let window_size = 4096;
let step_size = 512;
// 11 hertz is the maximum frequency that can be meaningfully analyzed
assert_eq!(
11.,
hertz::rayleigh(sample_rate as f64, window_size as f64).round());
// 22050 hertz is the maximum frequency that can be meaningfully analyzed
assert_eq!(
22050.,
hertz::nyquist(sample_rate as f64).round());
// 12 ms is the time resolution we get when analyzing a 44100 hertz
// signal with a step size of 512
assert_eq!(
12.,
hertz::s_to_ms(hertz::cycles_per_second_to_seconds_per_cycle(
sample_rate as f64,
step_size as f64)).round());
}
you can use hertz to keep a constant framerate in a game or other computer graphics application:
fn main() {
let frames_per_second: usize = 60;
loop {
let instant_at_frame_start = std::time::Instant::now();
// here's where logic and rendering would go.
// this is never called more than frames_per_second
// times per second.
hertz::sleep_for_constant_rate(
frames_per_second, instant_at_frame_start);
}
}