Skip to content

Commit

Permalink
Merge pull request #130 from LostLuma/pause-soundmanager
Browse files Browse the repository at this point in the history
Pause soundmanager instead of stopping all sounds for zero volumes
  • Loading branch information
LostLuma authored Sep 29, 2023
2 parents 9c9985c + ca12692 commit 7707890
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/main/java/dynamic_fps/impl/DynamicFPSMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public static int targetFrameRate() {
return config.frameRateTarget();
}

public static float volumeMultiplier() {
return config.volumeMultiplier();
}

public static boolean shouldShowToasts() {
return config.showToasts();
}
Expand Down Expand Up @@ -200,23 +204,27 @@ private static void checkForStateChanges() {
}
}

private static boolean willPauseSounds() {
return !minecraft.isWindowActive() && minecraft.options.pauseOnLostFocus && minecraft.screen == null;
}

private static void setVolumeMultiplier(float multiplier) {
// setting the volume to 0 stops all sounds (including music), which we want to
// avoid if possible.
// if the client would pause anyway, we don't need to do anything because that
// will already pause all sounds.
if (multiplier == 0 && willPauseSounds())
return;
// Set the sound engine to a new volume multiplier,
// Or instead pause it when the multiplier is zero.

var baseVolume = minecraft.options.getSoundSourceVolume(SoundSource.MASTER);
minecraft.getSoundManager().updateSourceVolume(
SoundSource.MASTER,
baseVolume * multiplier
);
// We can not set the sound engine to a zero volume
// Because it stops all actively playing sounds and
// Makes for a rather jarring experience when music
// Is stopped. Also fixes now-playing compatibility

var manager = minecraft.getSoundManager();

if (multiplier == 0) {
manager.pause();
} else {
manager.resume();

manager.updateSourceVolume(
SoundSource.MASTER,
minecraft.options.getSoundSourceVolume(SoundSource.MASTER) * multiplier
);
}
}

private static boolean checkForRender(long timeSinceLastRender) {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/dynamic_fps/impl/mixin/SoundEngineMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dynamic_fps.impl.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import dynamic_fps.impl.DynamicFPSMod;
import net.minecraft.client.sounds.SoundEngine;

@Mixin(SoundEngine.class)
public class SoundEngineMixin {
/**
* Cancels playing sounds while we are overwriting the volume to be off.
*
* This is done in favor of actually setting the volume to zero, because it
* Allows pausing and resuming the sound engine without cancelling active sounds.
*/
@Inject(method = { "play", "playDelayed" }, at = @At("HEAD"), cancellable = true)
private void play(CallbackInfo callbackInfo) {
if (DynamicFPSMod.volumeMultiplier() == 0.0f) {
callbackInfo.cancel();
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/dynamic_fps.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"LoadingOverlayMixin",
"MinecraftMixin",
"ScreenMixin",
"SoundEngineMixin",
"StatsScreenMixin",
"ToastComponentMixin",
"WindowMixin",
Expand Down

0 comments on commit 7707890

Please sign in to comment.