kira icon indicating copy to clipboard operation
kira copied to clipboard

Pop sound on init under linux

Open hasenbanck opened this issue 9 months ago • 4 comments

Some of our users report, that on linux (current fedora linux in their case), the sound system has a "pop" sound playing when the on init. I attached a screen recording that contains the "pop" sound. The "pop" sound seems to have been occuring for them since we updated from kira 0.9 to kira 0.10.

It only manifests on linux systems (they use alsa). On windows / mac we can't reproduce the issue. Do you have any idea, how this "pop" sound could be generated? The BGM that plays at the start is a sound stream.

https://github.com/user-attachments/assets/3fef52d6-ce6e-4f97-8bc1-177345a45126

hasenbanck avatar Feb 07 '25 07:02 hasenbanck

I'm not able to replicate this using a simple example:

use std::{error::Error, io::stdin};

use kira::{
	sound::{static_sound::StaticSoundData, streaming::StreamingSoundData},
	AudioManager, AudioManagerSettings, DefaultBackend,
};

fn main() -> Result<(), Box<dyn Error>> {
	let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
	manager.play(StreamingSoundData::from_file("01.mp3")?)?;

	stdin().read_line(&mut "".into())?;

	Ok(())
}

I'm using the same mp3 that I'm hearing in the video. I'm on Arch using PulseAudio (I think).

tesselode avatar Feb 09 '25 09:02 tesselode

It seems to be related again to the spatial sounds we have on our map. When we defer the loading of our spatial sound files (not playng! just loadin!), to when the window is visible, the "pop" sound dissappears (as referenced in the draft PR we have open in our project). Very odd cause and effect relationship of the bug.

hasenbanck avatar Feb 09 '25 15:02 hasenbanck

When are spatial sounds loaded and played otherwise?

I wasn't able to reproduce the pop by playing the music in a spatial sub track.

tesselode avatar Feb 10 '25 01:02 tesselode

  1. On programm start: Init the audio engine, adds all tracks, add a spatial listener to (0.0, 0.0, 0.0) and sets the music track to 10% of maximal output volume. new()
  2. After Init: Load default map and then register then loads all spatial sound of the map as static sounds (sounds are loaded async in a loader thread) and finally built the KD-tree of all ambient sound locations registered. load() and add_ambient_sound()
  3. Queue the main menu BGM for playback (The name of the function is missleading, since the playback will not start until the first call to "AudioEngine::update()"). play_background_music_track()

Program now waits for the graphics engine init and window to become visible and renders the first frame. For each frame then:

  1. Update the spatial listener to the position of the camera (moves from the origin to somewhere on the map). set_spatial_listener()
  • This queries the KD-tree that contains all ambient sounds of a map and queues the playback for all ambient sounds in range.
  • The new position and orientation of the spatial listener is tweened over 50 ms.
  1. Calls the "AudioEngine::update()" function update()
  • Resolves all async loads (static sound effects are added to the cache)
  • Starts the playback of the BGM if queued.
  • Starts the playback fo all queued static non-spatial sound effects, spatial sound effects, and ambient map sounds.
  • Restarts sound effects if they are marked as "cycling"

When we defer the loading of the ambient sounds, so that while the first frame is rendered the ambient sound files are still loading in the async thread, then the pop disappears. The first frame would only start the playback of the BGM track and at later frames, when the ambient sounds have been loaded, would start the playback of the ambient , spatial sounds (two ambient sounds of birds are in range of the camera location of the main menu).

On the other hand, if we remove the defered loading and make sure the ambient sound haves been fully loaded, then the ambient, spatial sounds of the birds and the BGM will start to play at the same frame. In this case the "pop" can be heard on linux systems.

(I can provide you with a test setup of our client if you want. Please write me a PM over discord if you want to have the needed files.)

hasenbanck avatar Feb 10 '25 07:02 hasenbanck

Fixed with bca3fde52d552d2a22736cb3fd0c694cbfa897b5

hasenbanck avatar Jun 08 '25 17:06 hasenbanck