rodio_scheduler

Crates.iorodio_scheduler
lib.rsrodio_scheduler
version0.3.1
created_at2025-07-04 15:20:44.240988+00
updated_at2025-08-04 15:58:17.324836+00
descriptionA library for sample-perfect audio scheduling with rodio.
homepage
repositoryhttps://github.com/LinoBigatti/coop-editor/tree/master/rodio_scheduler
max_upload_size
id1738139
size397,102
Lino (LinoBigatti)

documentation

README

rodio_scheduler: Sample-perfect scheduling for Rodio Sources.

Crates.io Docs.rs License: MIT

A crate that provides a rodio source capable of sample-pefect scheduling of other sources.

This is useful for applications that need to schedule multiple audio playback events before starting playback, such as Rhythm Games or DAWs. For synchronizing visuals or other external events, see the rodio_playback_position crate.

Important

This crate requires the nightly Rust compiler because it uses the portable-simd feature, which has not yet been stabilized.

Usage

Add this to your Cargo.toml:

[dependencies]
rodio_scheduler = "0.3.0"

Here is an example of how to use the library to schedule a sound and keep track of the sample counter. For more information, see the Docs.

use std::fs::File;

use rodio::{Source, Decoder, OutputStreamBuilder};
use rodio_scheduler::{Scheduler, PlaybackEvent};

fn main() {
   // Get an output stream handle to the default physical sound device.
   let stream = OutputStreamBuilder::open_default_stream().unwrap();

   // Load a sound from a file.
   let metronome = File::open("assets/metronome.wav").unwrap();
   let metronome_decoder_source = Decoder::new(metronome).unwrap();

   // Create a scheduler.
   let mut scheduler = Scheduler::new(metronome_decoder_source, 48000, 2);

   // Load another sound to be scheduled.
   let note_hit = File::open("assets/note_hit.wav").unwrap();
   let note_hit_decoder_source = Decoder::new(note_hit).unwrap();

   // Add the sound to the scheduler, with a list of playback events to schedule.
   let note_hit_id = scheduler.add_source(note_hit_decoder_source);

   // Schedule the sound to be played at a specific timestamp.
   let event = PlaybackEvent {
       source_id: note_hit_id,
       timestamp: scheduler.sample_rate() as u64 * 2, // 2 seconds in
       repeat: None,
   };
   scheduler.get_scheduler(note_hit_id).unwrap().schedule_event(event);

   // Play the scheduled sounds.
   let _ = stream.mixer().add(scheduler);

   // The sound plays in a separate audio thread,
   // so we need to keep the main thread alive while it's playing.
   std::thread::sleep(std::time::Duration::from_secs(5));
}

Features

  • simd: Enables SIMD optimizations for audio processing.
  • profiler: Enables profiling with time-graph. Beware that this has a big impact on real-time performance.

License

This project is licensed under the MIT license.

Commit count: 0

cargo fmt