Add setting to disable audio dampening if losing window focus
authorWuzzy <Wuzzy2@mail.ru>
Tue, 07 Aug 2018 11:37:37 +0200 (2018-08-07)
changeset 13628 d5e029b84e16
parent 13627 605767bbd022
child 13629 c9a49d06f1a2
Add setting to disable audio dampening if losing window focus
ChangeLog.txt
QTfrontend/game.cpp
QTfrontend/gameuiconfig.cpp
QTfrontend/gameuiconfig.h
QTfrontend/hwform.cpp
QTfrontend/ui/page/pageoptions.cpp
QTfrontend/ui/page/pageoptions.h
hedgewars/ArgParsers.pas
hedgewars/uSound.pas
--- a/ChangeLog.txt	Tue Aug 07 10:29:01 2018 +0200
+++ b/ChangeLog.txt	Tue Aug 07 11:37:37 2018 +0200
@@ -28,6 +28,7 @@
  * Limit hedgehog health to 268435455 to prevent some bugs
 
 Frontend:
+ + Add setting to disable audio dampening when losing window focus
  * Controllers are detected again
  * No longer allow having schemes with equal names (case-insensitive)
  * Lobby/room: No longer allow opening context menu if no player selected
--- a/QTfrontend/game.cpp	Tue Aug 07 10:29:01 2018 +0200
+++ b/QTfrontend/game.cpp	Tue Aug 07 11:37:37 2018 +0200
@@ -414,6 +414,8 @@
         arguments << "--nosound";
     if (!config->isMusicEnabled())
         arguments << "--nomusic";
+    if (!config->isAudioDampenEnabled())
+        arguments << "--nodampen";
     if (!nick.isEmpty()) {
         arguments << "--nick";
         arguments << nick;
--- a/QTfrontend/gameuiconfig.cpp	Tue Aug 07 10:29:01 2018 +0200
+++ b/QTfrontend/gameuiconfig.cpp	Tue Aug 07 11:37:37 2018 +0200
@@ -111,6 +111,7 @@
     Form->ui.pageOptions->CBFrontendSound->setChecked(value("frontend/sound", true).toBool());
     Form->ui.pageOptions->CBMusic->setChecked(value("audio/music", true).toBool());
     Form->ui.pageOptions->CBFrontendMusic->setChecked(value("frontend/music", true).toBool());
+    Form->ui.pageOptions->CBDampenAudio->setChecked(value("audio/dampen", true).toBool());
     Form->ui.pageOptions->SLVolume->setValue(value("audio/volume", 100).toUInt());
 
     QString netNick = value("net/nick", tr("Guest")+QString("%1").arg(rand())).toString();
@@ -268,6 +269,7 @@
     setValue("audio/music", isMusicEnabled());
     setValue("frontend/music", isFrontendMusicEnabled());
     setValue("audio/volume", Form->ui.pageOptions->SLVolume->value());
+    setValue("audio/dampen", isAudioDampenEnabled());
 
     setValue("net/nick", netNick());
     if (netPasswordIsValid() && Form->ui.pageOptions->CBSavePassword->isChecked()) {
@@ -456,6 +458,10 @@
 {
     return Form->ui.pageOptions->CBFrontendMusic->isChecked();
 }
+bool GameUIConfig::isAudioDampenEnabled()
+{
+    return Form->ui.pageOptions->CBDampenAudio->isChecked();
+}
 
 bool GameUIConfig::isShowFPSEnabled()
 {
--- a/QTfrontend/gameuiconfig.h	Tue Aug 07 10:29:01 2018 +0200
+++ b/QTfrontend/gameuiconfig.h	Tue Aug 07 11:37:37 2018 +0200
@@ -47,6 +47,7 @@
         QString language();
         bool isMusicEnabled();
         bool isFrontendMusicEnabled();
+        bool isAudioDampenEnabled();
         bool isShowFPSEnabled();
         bool isAltDamageEnabled();
         bool appendDateTimeToRecordName();
--- a/QTfrontend/hwform.cpp	Tue Aug 07 10:29:01 2018 +0200
+++ b/QTfrontend/hwform.cpp	Tue Aug 07 11:37:37 2018 +0200
@@ -2101,6 +2101,7 @@
                    + " --volume " + QString::number(config->volume())
                    + (config->isMusicEnabled() ? "" : " --nomusic")
                    + (config->isSoundEnabled() ? "" : " --nosound")
+                   + (config->isAudioDampenEnabled() ? "" : " --nodampen")
                    + " --locale " + config->language() + ".txt"
                    + (config->vid_Fullscreen() ? " --fullscreen" : "")
                    + (config->isShowFPSEnabled() ? " --showfps" : "")
--- a/QTfrontend/ui/page/pageoptions.cpp	Tue Aug 07 10:29:01 2018 +0200
+++ b/QTfrontend/ui/page/pageoptions.cpp	Tue Aug 07 11:37:37 2018 +0200
@@ -508,6 +508,13 @@
             CBMusic->setText(QCheckBox::tr("Music"));
             CBMusic->setWhatsThis(QCheckBox::tr("In-game music"));
             groupGame->layout()->addWidget(CBMusic, 1, 2, 1, 2, Qt::AlignLeft);
+
+            // Dampen
+
+            CBDampenAudio = new QCheckBox(groupGame);
+            CBDampenAudio->setText(QCheckBox::tr("Dampen when losing focus"));
+            CBDampenAudio->setWhatsThis(QCheckBox::tr("Reduce the game audio volume if the game window has lost its focus"));
+            groupGame->layout()->addWidget(CBDampenAudio, 2, 1, 1, 3, Qt::AlignLeft);
         }
 
         { // group: frontend
--- a/QTfrontend/ui/page/pageoptions.h	Tue Aug 07 10:29:01 2018 +0200
+++ b/QTfrontend/ui/page/pageoptions.h	Tue Aug 07 11:37:37 2018 +0200
@@ -84,6 +84,7 @@
         QCheckBox *CBFrontendMusic;
         QCheckBox *CBSound;
         QCheckBox *CBMusic;
+        QCheckBox *CBDampenAudio;
         QCheckBox *CBFullscreen;
         QCheckBox *CBFrontendFullscreen;
         QCheckBox *CBShowFPS;
--- a/hedgewars/ArgParsers.pas	Tue Aug 07 10:29:01 2018 +0200
+++ b/hedgewars/ArgParsers.pas	Tue Aug 07 11:37:37 2018 +0200
@@ -90,6 +90,7 @@
     WriteLn(stdout, ' --low-quality');
     WriteLn(stdout, ' --nomusic');
     WriteLn(stdout, ' --nosound');
+    WriteLn(stdout, ' --nodampen');
     WriteLn(stdout, ' --fullscreen');
     WriteLn(stdout, ' --showfps');
     WriteLn(stdout, ' --altdmg');
@@ -216,10 +217,10 @@
       audioarray: array [0..2] of string = ('--volume','--nomusic','--nosound');
       otherarray: array [0..2] of string = ('--locale','--fullscreen','--showfps');
       mediaarray: array [0..9] of string = ('--fullscreen-width', '--fullscreen-height', '--width', '--height', '--depth', '--volume','--nomusic','--nosound','--locale','--fullscreen');
-      allarray: array [0..17] of string = ('--fullscreen-width','--fullscreen-height', '--width', '--height', '--depth','--volume','--nomusic','--nosound','--locale','--fullscreen','--showfps','--altdmg','--frame-interval','--low-quality','--no-teamtag','--no-hogtag','--no-healthtag','--translucent-tags');
-      reallyAll: array[0..35] of shortstring = (
+      allarray: array [0..18] of string = ('--fullscreen-width','--fullscreen-height', '--width', '--height', '--depth','--volume','--nomusic','--nosound','--nodampen','--locale','--fullscreen','--showfps','--altdmg','--frame-interval','--low-quality','--no-teamtag','--no-hogtag','--no-healthtag','--translucent-tags');
+      reallyAll: array[0..36] of shortstring = (
                 '--prefix', '--user-prefix', '--locale', '--fullscreen-width', '--fullscreen-height', '--width',
-                '--height', '--frame-interval', '--volume','--nomusic', '--nosound',
+                '--height', '--frame-interval', '--volume','--nomusic', '--nosound', '--nodampen',
                 '--fullscreen', '--showfps', '--altdmg', '--low-quality', '--raw-quality', '--stereo', '--nick',
   {deprecated}  '--depth', '--set-video', '--set-audio', '--set-other', '--set-multimedia', '--set-everything',
   {internal}    '--internal', '--port', '--recorder', '--landpreview',
@@ -245,34 +246,35 @@
         {--volume}               8 : SetVolume          ( max(getLongIntParameter(arg, paramIndex, parseParameter), 0) );
         {--nomusic}              9 : SetMusic           ( false );
         {--nosound}             10 : SetSound           ( false );
-        {--fullscreen}          11 : cFullScreen       := true;
-        {--showfps}             12 : cShowFPS          := true;
-        {--altdmg}              13 : cAltDamage        := true;
-        {--low-quality}         14 : cReducedQuality   := $FFFFFFFF xor rqLowRes;
-        {--raw-quality}         15 : cReducedQuality   := getLongIntParameter(arg, paramIndex, parseParameter);
-        {--stereo}              16 : setStereoMode      ( getLongIntParameter(arg, paramIndex, parseParameter) );
-        {--nick}                17 : UserNick          := parseNick( getstringParameter(arg, paramIndex, parseParameter) );
+        {--nodampen}            11 : SetAudioDampen     ( false );
+        {--fullscreen}          12 : cFullScreen       := true;
+        {--showfps}             13 : cShowFPS          := true;
+        {--altdmg}              14 : cAltDamage        := true;
+        {--low-quality}         15 : cReducedQuality   := $FFFFFFFF xor rqLowRes;
+        {--raw-quality}         16 : cReducedQuality   := getLongIntParameter(arg, paramIndex, parseParameter);
+        {--stereo}              17 : setStereoMode      ( getLongIntParameter(arg, paramIndex, parseParameter) );
+        {--nick}                18 : UserNick          := parseNick( getstringParameter(arg, paramIndex, parseParameter) );
         {deprecated options}
-        {--depth}               18 : setDepth(paramIndex);
-        {--set-video}           19 : parseClassicParameter(videoarray,5,paramIndex);
-        {--set-audio}           20 : parseClassicParameter(audioarray,3,paramIndex);
-        {--set-other}           21 : parseClassicParameter(otherarray,3,paramIndex);
-        {--set-multimedia}      22 : parseClassicParameter(mediaarray,10,paramIndex);
-        {--set-everything}      23 : parseClassicParameter(allarray,14,paramIndex);
+        {--depth}               19 : setDepth(paramIndex);
+        {--set-video}           20 : parseClassicParameter(videoarray,5,paramIndex);
+        {--set-audio}           21 : parseClassicParameter(audioarray,3,paramIndex);
+        {--set-other}           22 : parseClassicParameter(otherarray,3,paramIndex);
+        {--set-multimedia}      23 : parseClassicParameter(mediaarray,10,paramIndex);
+        {--set-everything}      24 : parseClassicParameter(allarray,14,paramIndex);
         {"internal" options}
-        {--internal}            24 : {$IFDEF HWLIBRARY}isInternal:= true{$ENDIF};
-        {--port}                25 : setIpcPort( getLongIntParameter(arg, paramIndex, parseParameter), parseParameter );
-        {--recorder}            26 : startVideoRecording(paramIndex);
-        {--landpreview}         27 : GameType := gmtLandPreview;
+        {--internal}            25 : {$IFDEF HWLIBRARY}isInternal:= true{$ENDIF};
+        {--port}                26 : setIpcPort( getLongIntParameter(arg, paramIndex, parseParameter), parseParameter );
+        {--recorder}            27 : startVideoRecording(paramIndex);
+        {--landpreview}         28 : GameType := gmtLandPreview;
         {anything else}
-        {--stats-only}          28 : statsOnlyGame();
-        {--gci}                 29 : GciEasterEgg();
-        {--help}                30 : DisplayUsage();
-        {--no-teamtag}          31 : cTagsMask := cTagsMask and (not htTeamName);
-        {--no-hogtag}           32 : cTagsMask := cTagsMask and (not htName);
-        {--no-healthtag}        33 : cTagsMask := cTagsMask and (not htHealth);
-        {--translucent-tags}    34 : cTagsMask := cTagsMask or htTransparent;
-        {--lua-test}            35 : begin cTestLua := true; SetSound(false); cScriptName := getstringParameter(arg, paramIndex, parseParameter); WriteLn(stdout, 'Lua test file specified: ' + cScriptName);end;
+        {--stats-only}          29 : statsOnlyGame();
+        {--gci}                 30 : GciEasterEgg();
+        {--help}                31 : DisplayUsage();
+        {--no-teamtag}          32 : cTagsMask := cTagsMask and (not htTeamName);
+        {--no-hogtag}           33 : cTagsMask := cTagsMask and (not htName);
+        {--no-healthtag}        34 : cTagsMask := cTagsMask and (not htHealth);
+        {--translucent-tags}    35 : cTagsMask := cTagsMask or htTransparent;
+        {--lua-test}            36 : begin cTestLua := true; SetSound(false); cScriptName := getstringParameter(arg, paramIndex, parseParameter); WriteLn(stdout, 'Lua test file specified: ' + cScriptName);end;
     else
         begin
         //Assume the first "non parameter" is the replay file, anything else is invalid
@@ -308,10 +310,10 @@
         isValid:= (cmd<>'--depth');
 
         // check if the parameter is a boolean one
-        isBool:= (cmd = '--nomusic') or (cmd = '--nosound') or (cmd = '--fullscreen') or (cmd = '--showfps') or (cmd = '--altdmg') or (cmd = '--no-teamtag') or (cmd = '--no-hogtag') or (cmd = '--no-healthtag') or (cmd = '--translucent-tags');
+        isBool:= (cmd = '--nomusic') or (cmd = '--nosound') or (cmd = '--nodampen') or (cmd = '--fullscreen') or (cmd = '--showfps') or (cmd = '--altdmg') or (cmd = '--no-teamtag') or (cmd = '--no-hogtag') or (cmd = '--no-healthtag') or (cmd = '--translucent-tags');
         if isBool and (arg='0') then
             isValid:= false;
-        if (cmd='--nomusic') or (cmd='--nosound') then
+        if (cmd='--nomusic') or (cmd='--nosound') or (cmd='--nodampen') then
             isValid:= not isValid;
 
         if isValid then
--- a/hedgewars/uSound.pas	Tue Aug 07 10:29:01 2018 +0200
+++ b/hedgewars/uSound.pas	Tue Aug 07 11:37:37 2018 +0200
@@ -43,6 +43,7 @@
 procedure ReleaseSound(complete: boolean);      // Releases sound-system and used resources.
 procedure ResetSound;                           // Reset sound state to the previous state.
 procedure SetSound(enabled: boolean);           // Enable/disable sound-system and backup status.
+procedure SetAudioDampen(enabled: boolean);     // Enable/disable automatic dampening if losing window focus.
 
 // MUSIC
 
@@ -129,6 +130,7 @@
     Mus: PMixMusic; // music pointer
     isMusicEnabled: boolean;
     isSoundEnabled: boolean;
+    isAutoDampening: boolean;
     isSEBackup: boolean;
     VoiceList : array[0..7] of TVoice =  (
                     ( snd: sndNone; voicepack: nil),
@@ -388,6 +390,11 @@
     isSoundEnabled:= enabled;
 end;
 
+procedure SetAudioDampen(enabled: boolean);
+begin
+    isAutoDampening:= enabled;
+end;
+
 // when complete is false, this procedure just releases some of the chucks on inactive channels
 // in this way music is not stopped, nor are chucks currently being played
 procedure ReleaseSound(complete: boolean);
@@ -752,7 +759,7 @@
 
 procedure DampenAudio;
 begin
-    if (isAudioMuted) then
+    if (isAudioMuted or (not isAutoDampening)) then
         exit;
     previousVolume:= Volume;
     ChangeVolume(-Volume * 7 div 9);
@@ -760,7 +767,7 @@
 
 procedure UndampenAudio;
 begin
-     if (isAudioMuted) then
+    if (isAudioMuted or (not isAutoDampening)) then
         exit;
     ChangeVolume(previousVolume - Volume);
 end;
@@ -849,6 +856,7 @@
 begin
     isMusicEnabled:= true;
     isSoundEnabled:= true;
+    isAutoDampening:= true;
     cInitVolume:= 100;
 end;