tesselode / kira

Library for expressive game audio.
https://crates.io/crates/kira
Apache License 2.0
836 stars 42 forks source link

`set_volume` does not apply immediately #65

Closed zeozeozeo closed 7 months ago

zeozeozeo commented 8 months ago

For example, if I write something like this:

if let Ok(mut sound) = manager.play(sound_buf) {
    sound.set_volume(0.01, Tween {
        start_time: StartTime::Immediate,
        duration: Duration::from_secs(0),
        easing: Easing::Linear,
    });
}

the volume doesn't get applied instantly, for a very short time the volume is still at 1.0

lukecarr commented 8 months ago

Admittedly, I'm not too familiar with the internals of Kira, but to me, it would make more sense for set_volume to take an Option<Tween> (instead of a Tween).

A None tween value would indicate that the volume change should occur immediately without constructing a "hacky" tween like the one in @zeozeozeo's example.

Unless there's a specific requirement for always having a tween associated with a set volume command?

tesselode commented 8 months ago

@zeozeozeo How short of a time are we talking? If it's around the 10-20 millisecond range, that's to be expected. No command from the gameplay thread will reach the audio thread immediately, because the audio thread only does its processing every 15ms or so (depending on the buffer size your audio system is using).

@lukecarr You always want volume changes to happen with a tween. If you change volumes instantly, you'll hear an unpleasant popping sound because of the discontinuity the volume change creates in the output waveform. The default tween duration is short enough to feel instant without creating a popping sound.

zeozeozeo commented 8 months ago

@tesselode thanks for the explanation, so there's currently no way of immediately setting the volume of the sound?

tesselode commented 8 months ago

@zeozeozeo No, and there won't ever be one. That's inherent to how operating systems handle audio.