Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: eri0o on Sun 10/12/2017 20:40:56

Title: Prevent alt+tab audio glitch
Post by: eri0o on Sun 10/12/2017 20:40:56
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.
Title: Re: Prevent alt+tab audio glitch
Post by: Snarky on Sun 10/12/2017 20:57:11
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.
Title: Re: Prevent alt+tab audio glitch
Post by: Crimson Wizard on Sun 10/12/2017 21:06:06
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) Select

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


Warning, dumb example:
Code (ags) Select

function repeatedly_execute()
{
  if (!System.HasInputFocus && IsGamePaused() == 0) {
    PauseGame();
  } else if (System.HasInputFocus && IsGamePaused() == 1) {
    UnPauseGame();
  }
}
Title: Re: Prevent alt+tab audio glitch
Post by: eri0o on Mon 11/12/2017 00:07:27
Thanks Crimson Wizard!!!! :-D It actually works! (nod)

My code is like this:

AltTabSoundStutterFix.asc
Code (ags) Select

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 ?
Title: Re: Prevent alt+tab audio glitch
Post by: selmiak on Wed 13/12/2017 14:41:36
This is a nice script, but imho this should be the default behaviour of the engine :P
Title: Re: Prevent alt+tab audio glitch
Post by: Crimson Wizard on Sat 06/01/2018 20:08:04
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.
Title: Re: Prevent alt+tab audio glitch
Post by: eri0o on Sun 07/01/2018 12:54:02
Both window and fullscreen, but the above code does work. I will actually improve it to pause and proceed with the music later.
Title: Re: Prevent alt+tab audio glitch
Post by: selmiak on Sun 07/01/2018 17:38:20
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.
Title: Re: Prevent alt+tab audio glitch
Post by: Crimson Wizard on Sun 07/01/2018 17:56:11
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.
Title: Re: Prevent alt+tab audio glitch
Post by: selmiak on Sun 07/01/2018 18:05:08
no idea, but it does happen :)
Title: Re: Prevent alt+tab audio glitch
Post by: Crimson Wizard on Sun 07/01/2018 18:15:26
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?
Title: Re: Prevent alt+tab audio glitch
Post by: selmiak on Sun 07/01/2018 18:51:31
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 ;)
Title: Re: Prevent alt+tab audio glitch
Post by: Cookie_Wood on Wed 04/03/2020 11:38:09
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 !
Title: Re: Prevent alt+tab audio glitch
Post by: eri0o on Wed 04/03/2020 12:13:57
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?
Title: Re: Prevent alt+tab audio glitch
Post by: Cookie_Wood on Wed 04/03/2020 12:48:00
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 :/