Module kira::clock

source ·
Expand description

Precise timing for audio events.

Clocks can be used to set the start times of sounds and tweens. To create a clock, use AudioManager::add_clock.

use kira::{
	manager::{
		AudioManager, AudioManagerSettings,
		backend::DefaultBackend,
	},
	clock::ClockSpeed,
};

let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
let mut clock = manager.add_clock(ClockSpeed::SecondsPerTick(1.0))?;
clock.start();

You can specify the speed of the clock as seconds per tick, ticks per second, or ticks per minute.

Clocks are stopped when you first create them, so be sure to explicitly call ClockHandle::start when you want the clock to start ticking.

§Starting sounds on clock ticks

Sounds can be set to only start playing when a clock has ticked a certain number of times. You can configure this using StaticSoundSettings::start_time or StreamingSoundSettings::start_time.

use kira::{
	clock::{ClockTime, ClockSpeed},
	manager::{
		AudioManager, AudioManagerSettings,
		backend::DefaultBackend,
	},
	sound::static_sound::{StaticSoundData, StaticSoundSettings},
	StartTime,
};

let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
let mut clock = manager.add_clock(ClockSpeed::SecondsPerTick(1.0))?;
manager.play(
	StaticSoundData::from_file("sound.ogg")?
		.start_time(StartTime::ClockTime(ClockTime {
			clock: clock.id(),
			ticks: 4,
			fraction: 0.0,
		}))
)?;
clock.start();

As a shorthand, you can pass the ClockTime directly into the start_time function.

manager.play(
	StaticSoundData::from_file("sound.ogg")?
		.start_time(ClockTime {
			clock: clock.id(),
			ticks: 4,
			fraction: 0.0,
		})
)?;

As an even shorter hand, you can use ClockHandle::time to get the clock’s current ClockTime, and then add to it to get a time in the future:

use kira::{
	manager::{
		AudioManager, AudioManagerSettings,
		backend::DefaultBackend,
	},
	sound::static_sound::{StaticSoundData, StaticSoundSettings},
	clock::ClockSpeed,
};

manager.play(
	StaticSoundData::from_file("sound.ogg")?
		.start_time(clock.time() + 4)
)?;

§Starting tweens on clock ticks

You can also use clocks to set the start time of tweens. In this example, we set the playback rate of a sound to start tweening when a clock reaches its third tick.

use std::time::Duration;

use kira::{
	manager::{
		AudioManager, AudioManagerSettings,
		backend::DefaultBackend,
	},
	sound::static_sound::{StaticSoundData, StaticSoundSettings},
	tween::Tween,
	clock::ClockSpeed,
	StartTime,
};

let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
let mut clock = manager.add_clock(ClockSpeed::SecondsPerTick(1.0))?;
let mut sound = manager.play(StaticSoundData::from_file("sound.ogg")?)?;
sound.set_playback_rate(
	0.5,
	Tween {
		start_time: StartTime::ClockTime(clock.time() + 3),
		duration: Duration::from_secs(2),
		..Default::default()
	},
);
clock.start();

Modules§

  • Contains types for reporting information about clocks.

Structs§

Enums§