tesselode / kira

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

Playing a `StreamingSoundData` intermittently plays a scratchy, sped-up version of the sound #36

Open PatchMixolydic opened 1 year ago

PatchMixolydic commented 1 year ago

I tried playing this audio file as a StreamingSoundData with a loop point of 7.0. Occasionally, this would work, but sometimes, it would come out as a scratchy mess:

https://user-images.githubusercontent.com/8634700/206922679-c6a48323-92aa-4923-8758-9b918ca80a4d.mp4

Upon closer inspection, this seems to be a sped up version of the track. Sometimes this issue would correct itself at the loop point, but occasionally, it continues forever.

Thus far, I've only been able to reproduce this issue in debug builds (with optimized dependencies). Release builds seem to be unaffected.

tesselode commented 1 year ago

I'm not able to reproduce the issue. What operating system is this on? Does the problem still occur if you set opt-level = 3 for the main crate? What if you use StaticSoundData (without opt-level = 3)?

That "scratchiness" sounds to me like Kira can't fill the OS's audio buffer fast enough, which would make sense for an unoptimized build, but that doesn't explain why the music is playing back faster than normal. If anything, I'd expect it to play back slower. I think there might be some part of Kira that isn't getting optimized even though you have optimization turned on for dependencies because of some generic functions that get compiled as part of the main crate, but that's pure speculation on my part.

PatchMixolydic commented 1 year ago

This is on Debian Sid (Linux 5.19.0-2-amd64 #1 SMP PREEMPT_DYNAMIC Debian 5.19.11-1 (2022-09-24)) running PulseAudio. I wasn't able to reproduce this in a fully optimized build. Using StaticSoundData avoided the issue, but resulted in long load times in unoptimized builds (which could probably be alleviated by reencoding the music at a lower quality).

That "scratchiness" sounds to me like Kira can't fill the OS's audio buffer fast enough

That's about what I expected, but I figured I might as well ask here just in case.

there might be some part of Kira that isn't getting optimized ... because of some generic functions that get compiled as part of the main crate

This might be part of it. I don't believe there are any generic functions between the main crate and Kira (at least in the state shown in the video), but I am using a trait object to store game states.

tesselode commented 1 year ago

Hi again,

I could not reproduce the issue on Debian Bullseye. When playing a hundred or so copies of the streaming sound, I got underruns, but I didn't observe the sped-up playback you did. I just got normal playback, but with regular pauses.

I'm guessing the Linux kernel version isn't relevant here, but I could be wrong. Not sure what else to try to reproduce the issue.