1 unstable release
0.1.0 | Jul 17, 2021 |
---|
#780 in Audio
Used in midi-reader-writer
16KB
189 lines
Timestamp stretcher
Stretch integer time stamps by a fractional factor that may change over time. This library takes care to avoid time-drifting and keep the errors small.
Contributing
See CONTRIBUTING.md for more information.
License
timestamp-stretcher
is licensed under the Apache License, Version 2.0
or the MIT license, at your option.
For the application of the MIT license, the examples included in the doc comments are not considered "substantial portions of this Software".
lib.rs
:
TimeStretcher
Stretch integer (u64
) time stamps by a fractional factor that may change over time.
Examples
The following example illustrates the usage of the library.
use timestamp_stretcher::TimestampStretcher;
use std::num::NonZeroU64;
// Create a new TimeStretcher with an initial factor of 2/3.
let mut stretcher = TimestampStretcher::new(2, NonZeroU64::new(3).unwrap());
// Suppose we have five events: "0", "1", "2", "3" and "4".
// Each event happens at a certain time stamp:
// +--- Time stamp of event "1" is 3
// v
// 0 . . 1 . . 2 . . . . 3 . . . . 4
// If we stretch the time stamps up to event 2 with a factor 2/3,
// and the subsequent time stamps with a factor 7/5, we get the following:
// 0 . 1 . 2 . . . . . . 3 . . . . . . 4
let input_time_stamps = vec![
(0, None), // Event at time 0, no change to conversion factor.
(3, None), // Event at time 3, no change to conversion factor.
(6, Some((7, NonZeroU64::new(5).unwrap()))), // Event at time 6, change conversion factor to 7/8.
// The new conversion factor will be applied to the time differences, not to the absolute times.
(11, None), // Event at time 11.
(16, None),
];
let mut observed_output_time_stamps = Vec::new();
for input in input_time_stamps.into_iter() {
observed_output_time_stamps.push(stretcher.stretch(input.0, input.1));
}
let expected_output_time_stamps = vec![0, 2, 4, 11, 18];
assert_eq!(expected_output_time_stamps, observed_output_time_stamps);
Dependencies
~12KB