Expand description
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);
}
}Functionsยง
- cycles_
per_ second_ to_ seconds_ per_ cycle - window duration in seconds sample_rate in hertz
- fps_
to_ ns_ per_ frame - when given frames per second (or sample rate) returns the duration of a single frame
- hertz_
range - frequency range that can be modeled when taking the short
time fourier transform of a signal with
sample_ratewith a sliding window ofwindow_sizew. equivalent torayleigh(sample_rate, window_size)..nyquist(sample_rate)increase the window size to increase the lower frequency increase the sample rate to increase the upper frequency - ms_
to_ ns - milliseconds to nanoseconds
- ms_to_s
- milliseconds to seconds
- ns_
to_ ms - nanoseconds to milliseconds
- ns_to_s
- nanoseconds to seconds
- nyquist
- maximum frequency in hertz that can be meaningfully analyzed with a given sample rate https://en.wikipedia.org/wiki/Short-time_Fourier_transform#Explanation
- rayleigh
- minimum frequency in hertz that can be meaningfully analyzed with a given sample rate and window size https://en.wikipedia.org/wiki/Short-time_Fourier_transform#Rayleigh_frequency
- s_to_ms
- seconds to milliseconds
- s_to_ns
- seconds to nanoseconds
- sleep_
for_ constant_ rate - useful for keeping a constant framerate