`TrackPosition::get_pos` drifts after `try_seek(Duration::ZERO)`: f32 accumulation per span
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 67/100
Hướng nghiên cứu
Start with src/source/position.rs, especially TrackPosition::next() and its offset_duration accumulation, then inspect how get_pos() reports the position. Compare the default and 64bit behavior using the issue's silent reproducer in examples/position_drift.rs and the stated Cargo commands. Done means position error no longer accumulates over long playback after try_seek(Duration::ZERO).
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
After try_seek(Duration::ZERO), TrackPosition (and so Player::get_pos) adds each finished span's duration to an f32 offset_duration (src/source/position.rs, in next()). The rounding error of each addition piles up. Without that seek the tracker stays in seek mode, never takes this path, and the reproducer below prints 0.000 ms at every length. With --features 64bit it stays within 0.0005 ms (prints 0.000) at every length I tried.
I noticed this while reviewing #934. It is on master (685e057) and does not depend on that PR.
Reproducer
Silent, no audio device. Stereo 44.1 kHz with 2304-sample spans (1152 frames, MP3-sized). Put it in examples/position_drift.rs and run cargo run --release --no-default-features --example position_drift, then again with --features 64bit.
use std::{num::NonZero, time::Duration};
use rodio::{source::SeekError, Source};
struct Silence;
impl Iterator for Silence {
type Item = rodio::Sample;
fn next(&mut self) -> Option<Self::Item> { Some(0.0) }
}
impl Source for Silence {
fn current_span_len(&self) -> Option<usize> { Some(2304) }
fn channels(&self) -> rodio::ChannelCount { NonZero::new(2).unwrap() }
fn sample_rate(&self) -> rodio::SampleRate { NonZero::new(44_100).unwrap() }
fn total_duration(&self) -> Option<Duration> { None }
fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> { Ok(()) }
}
fn main() {
let mut src = Silence.track_position();
src.try_seek(Duration::ZERO).unwrap(); // remove this line and the error is 0.000 ms
let mut played: u64 = 0; // samples
for minutes in [1u64, 10, 30, 60, 90, 120] {
while played < minutes * 60 * 44_100 * 2 {
src.next();
played += 1;
}
let exact = played as f64 / 2.0 / 44_100.0;
let err_ms = (src.get_pos().as_secs_f64() - exact) * 1000.0;
println!("{minutes:>3} min: get_pos - exact = {err_ms:+.3} ms");
}
}
Results
get_pos() minus the exact position. The numbers are the same in every run and in debug and release builds.
| Played | f32 (default) | 64bit |
|---|---|---|
| 1 min | +0.908 ms | 0.000 ms |
| 10 min | +13.245 ms | 0.000 ms |
| 30 min | +40.649 ms | 0.000 ms |
| 60 min | +81.787 ms | 0.000 ms |
| 90 min | -12.06 s | 0.000 ms |
| 120 min | -28.85 s | 0.000 ms |
Cause
Each span adds 1152 / 44100 = 26.122 ms. An f32 sum can only hold multiples of its ulp, and the ulp doubles each time the sum passes a power of two. Rounding is deterministic, so every addition in the same power-of-two range rounds the same way. The error grows linearly, not like a random walk:
- Up to 4096 s the step is a little under a whole number of ulps (106.998 ulp between 2048 s and 4096 s, for example). Each addition rounds up by a tiny fraction of an ulp, and that adds up to the tens of milliseconds above.
- From 4096 s to 8192 s the ulp is 2^-11 s = 0.488 ms, and the step is 53.499 ulp. Every addition rounds down to 53 ulp and loses 0.244 ms, about 0.93 % of each step.
get_posfalls behind by about 9.3 ms per second played: about 12 s after another 1300 s (90 min) and about 29 s after another 3100 s (120 min).
A plain f32 simulation of acc += step gives the same numbers to within 0.15 ms. Other span lengths and sample rates land on different fractions, so the size of the error depends on the format. The cliff past 4096 s can be worse or milder than here.
Possible fixes
- Count the played frames as an integer (for example
u64frames, or aDurationper span) and convert to seconds only inget_pos. That is exact for a stable sample rate. When the rate changes at a span boundary, it would need one running total per rate, or a conversion toDurationat each boundary. - Keep the accumulator in f64 whatever
Floatis. That is the smallest change. At these lengths it matches the64bitresults, but it is still a float sum.
- Ngôn ngữ chính
- Rust
- Star
- 2.5k
- Fork
- 338
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của RustAudio/rodio
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 73/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 46/100
Tất cả issue của RustAudio/rodio
Issue tương tự
-
bug nvim
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
chip:esp32c6 peripheral:dma
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
esp-rs/esp-hal#7037 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
BrettMayson/HEMTT#1445 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Choochmeque/tauri-plugin-webdriver#134 ·
Maintainer thường phản hồi trong vòng 17 ngày
-
bug(c-sdk): boxlite_box_exec does not check runtime liveness before dispatching streaming execĐang mởbug rust SDK & CLI & REST API
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
boxlite-ai/boxlite#3292 ·
Maintainer thường phản hồi trong vòng 1 ngày