Prevent alt+tab audio glitch

Started by eri0o, Sun 10/12/2017 20:40:56

Previous topic - Next topic

eri0o

Hey! When I alt+tab an AGS game, the audio likely rolls a dice, and on a odd number, it glitches, producing looping sound that sounds like the previous 200ms of sound. Is there a way to prevent this? If I could detect from in game the loss of focus, I could try to lower the volume.

Snarky

Audio stuttering is standard AGS behavior when some operation takes too long: the engine doesn't get around to filling the audio buffer in time, and so it replays the last few milliseconds. When you Alt-Tab, Windows probably has to give CPU time to a lot of other processes, so AGS doesn't get enough to run smoothly.

There's basically no way to prevent it, unless you want to try the build that has audio processing in a separate thread.

Crimson Wizard

#2
I think one of possible solutions to prevent audio locking in a loop is to set multitasking mode on, but then you better pause your game when its alt+tabbed.
Note, that you cannot detect alt+tab unless you set multitasking mode, because game script is literally suspended otherwise.

Code: ags

SetMultitaskingMode(1); // this way the game will run in background


Warning, dumb example:
Code: ags

function repeatedly_execute()
{
  if (!System.HasInputFocus && IsGamePaused() == 0) {
    PauseGame();
  } else if (System.HasInputFocus && IsGamePaused() == 1) {
    UnPauseGame();
  }
}

eri0o

#3
Thanks Crimson Wizard!!!! :-D It actually works! (nod)

My code is like this:

AltTabSoundStutterFix.asc
Code: ags

int previousSystemVolume;

void game_start(){
  SetMultitaskingMode(1);
  previousSystemVolume = System.Volume;
}

function repeatedly_execute()
{
  if (!System.HasInputFocus && IsGamePaused() == 0) {
    previousSystemVolume = System.Volume;
    System.Volume=0;
    PauseGame();
  } else if (System.HasInputFocus && IsGamePaused() == 1) {
    System.Volume = previousSystemVolume;
    UnPauseGame();
  }
}


I am considering switching the repeatedly_execute for a repeatedly_execute_always to make it work even in the middle of animations, otherwise it has to wait it happen before. I also noticed that it doesn't pause background animations (the 5 frames in the bg).

@Snarky! Can you move this thread to Advanced Technical Forum ?

selmiak

This is a nice script, but imho this should be the default behaviour of the engine :P

Crimson Wizard

#5
I found that engine already tries to suspend all channels when you alt+tab, and there is comment in code saying "// stop the sound stuttering". Guess that does not work for some reason.

E: BTW, does the stutter happen only in fullscreen mode or both? I only experience it when game is run in fullscreen.

eri0o

Both window and fullscreen, but the above code does work. I will actually improve it to pause and proceed with the music later.

selmiak

I also only have this in fullscreen as you can click away the windowed game and the sound of the game still keeps on running.

Crimson Wizard

Quote from: selmiak on Sun 07/01/2018 17:38:20
as you can click away the windowed game and the sound of the game still keeps on running.
That is not supposed to be happening normally, unless the game runs in "Multitasking" mode.

selmiak


Crimson Wizard

Quote from: selmiak on Sun 07/01/2018 18:05:08
no idea, but it does happen :)
Does it happen in any game, or particular games?

selmiak

So multitask mode is set by the game author, not in winsetup, so then yes, I was refering to I want Out and NicolaGs assured me that it runs in multitask mode... so AGS is not that broke ;)

Cookie_Wood

#12
Hi eri0o, Hi everyone :)

I'm struggeling with this problem since a while and I was hoping your module will help me. I'm still a newbie but i do understand what it is supposed to do and don't understand why it is not working on my game.

You was talking about maybe improve it on your previous post, did you bring some changes since 2017 ? Any advices, informations that can help me ? Actually your module works like a charm in windowed mode but in fullscreen the sounds glitches persist (on windows).

Also, in the manual, they say that SetMultiTaskingMode was sometime dealing with some problems depending of the user graphic card. Does your module supposed to override that limitation ?

Other things : I'm using AGS 3.4.3, there's noting related to music on my rep_execute, I just created an .asc with your module.

Thanks you in advance !

eri0o

Did you use repeatedly execute always instead of just repeatedly execute? Otherwise it won't work during blocking actions. I haven't used 3.4.3 in a while, and haven't had time to test it yet. CW had the problem only in fullscreen, which maybe means it only happens in this case?

Cookie_Wood

Thank you for your reply :D

Yes, I tested in rep_execute_always since you were mentioning it in your previous posts, but didn't change anything :/

SMF spam blocked by CleanTalk