AGS Awards nominations close at 13:59 GMT on Saturday 10 February 2018. You haven't yet nominated, so you've got 18 days and 10 hours left to play the games and decide which to nominate!

Author Topic: Prevent alt+tab audio glitch  (Read 365 times)

Prevent alt+tab audio glitch
« on: 10 Dec 2017, 20:40 »
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

  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    •  
    • I can help with translating
    •  
Re: Prevent alt+tab audio glitch
« Reply #1 on: 10 Dec 2017, 20:57 »
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

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: Prevent alt+tab audio glitch
« Reply #2 on: 10 Dec 2017, 21: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: Adventure Game Studio
  1. SetMultitaskingMode(1); // this way the game will run in background
  2.  

Warning, dumb example:
Code: Adventure Game Studio
  1. function repeatedly_execute()
  2. {
  3.   if (!System.HasInputFocus && IsGamePaused() == 0) {
  4.     PauseGame();
  5.   } else if (System.HasInputFocus && IsGamePaused() == 1) {
  6.     UnPauseGame();
  7.   }
  8. }
  9.  
« Last Edit: 10 Dec 2017, 21:07 by Crimson Wizard »

Re: Prevent alt+tab audio glitch
« Reply #3 on: 11 Dec 2017, 00:07 »
Thanks Crimson Wizard!!!! :-D It actually works! (nod)

My code is like this:

AltTabSoundStutterFix.asc
Code: Adventure Game Studio
  1. int previousSystemVolume;
  2.  
  3. void game_start(){
  4.   SetMultitaskingMode(1);
  5.   previousSystemVolume = System.Volume;
  6. }
  7.  
  8. function repeatedly_execute()
  9. {
  10.   if (!System.HasInputFocus && IsGamePaused() == 0) {
  11.     previousSystemVolume = System.Volume;
  12.     System.Volume=0;
  13.     PauseGame();
  14.   } else if (System.HasInputFocus && IsGamePaused() == 1) {
  15.     System.Volume = previousSystemVolume;
  16.     UnPauseGame();
  17.   }
  18. }
  19.  

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 ?
« Last Edit: 11 Dec 2017, 00:22 by eri0o »

selmiak

  • ǝsıɔɹǝxǝ ʞɔǝu puɐ uıɐɹq
    • I can help with play testing
    •  
    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with web design
    •  
    • selmiak worked on a game that was nominated for an AGS Award!
Re: Prevent alt+tab audio glitch
« Reply #4 on: 13 Dec 2017, 14:41 »
This is a nice script, but imho this should be the default behaviour of the engine :P

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: Prevent alt+tab audio glitch
« Reply #5 on: 06 Jan 2018, 20:08 »
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.
« Last Edit: 06 Jan 2018, 20:51 by Crimson Wizard »

Re: Prevent alt+tab audio glitch
« Reply #6 on: 07 Jan 2018, 12:54 »
Both window and fullscreen, but the above code does work. I will actually improve it to pause and proceed with the music later.

selmiak

  • ǝsıɔɹǝxǝ ʞɔǝu puɐ uıɐɹq
    • I can help with play testing
    •  
    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with web design
    •  
    • selmiak worked on a game that was nominated for an AGS Award!
Re: Prevent alt+tab audio glitch
« Reply #7 on: 07 Jan 2018, 17:38 »
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

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: Prevent alt+tab audio glitch
« Reply #8 on: 07 Jan 2018, 17:56 »
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

  • ǝsıɔɹǝxǝ ʞɔǝu puɐ uıɐɹq
    • I can help with play testing
    •  
    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with web design
    •  
    • selmiak worked on a game that was nominated for an AGS Award!
Re: Prevent alt+tab audio glitch
« Reply #9 on: 07 Jan 2018, 18:05 »
no idea, but it does happen :)

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: Prevent alt+tab audio glitch
« Reply #10 on: 07 Jan 2018, 18:15 »
no idea, but it does happen :)
Does it happen in any game, or particular games?

selmiak

  • ǝsıɔɹǝxǝ ʞɔǝu puɐ uıɐɹq
    • I can help with play testing
    •  
    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with web design
    •  
    • selmiak worked on a game that was nominated for an AGS Award!
Re: Prevent alt+tab audio glitch
« Reply #11 on: 07 Jan 2018, 18:51 »
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 ;)