rodio_playback_position: Track the Playback Position of a Rodio Source at runtime.

A crate that provides a small playback backend for rodio sources,
with support for a high-precision interpolated playback position counter.
This is useful for applications that need to sync visuals or other events with audio playback at runtime,
such as rhythm games or music players. For scheduling and synchronizing the playback of multiple sources
simultaneously, see the rodio_scheduler crate.
Usage
Add this to your Cargo.toml:
[dependencies]
rodio_playback_position = "0.1.0"
rodio = "0.21.1"
cpal = "0.16.0"
Here is an example of how to use the library to start playback and keep track of the current playback position.
For more information, see the Docs, as well as the cpal Docs.
use std::time::Duration;
use cpal::traits::{HostTrait, DeviceTrait};
use rodio::source::SineWave;
use rodio::Source;
use rodio_playback_position::{OutputStreamConfig, stream};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let host = cpal::default_host();
let device = host.default_output_device().expect("no output device available");
println!("Using output device: {}", device.name()?);
let config = OutputStreamConfig::from(device.default_output_config().unwrap());
let source = SineWave::new(440.0).take_duration(Duration::from_secs(5));
println!("Opening stream...");
let mut stream_handle = stream::open(
&device,
&config,
source,
|err| eprintln!("stream error: {}", err),
)?;
println!("Stream opened.");
for _ in 0..50 {
println!("Sample count: {}", stream_handle.sample_count());
std::thread::sleep(Duration::from_millis(100));
}
Ok(())
}