Skip to content

Commit

Permalink
Merge pull request #165 from LostLuma/fix-music-pause-behavior
Browse files Browse the repository at this point in the history
Fix sounds / music pausing and resuming at the wrong times
  • Loading branch information
LostLuma committed Feb 17, 2024
2 parents 8c1baef + aa3186b commit 6c11f4e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
Expand Up @@ -205,9 +205,7 @@ public static void handleStateChange(PowerState previous, PowerState current) {
}

for (SoundSource source : SoundSource.values()) {
if (before.volumeMultiplier(source) != config.volumeMultiplier(source)) {
((DuckSoundEngine) minecraft.getSoundManager().soundEngine).dynamic_fps$updateVolume(source);
}
((DuckSoundEngine) minecraft.getSoundManager().soundEngine).dynamic_fps$updateVolume(before, source);
}

if (before.graphicsState() != config.graphicsState()) {
Expand Down
Expand Up @@ -33,6 +33,14 @@ public void setFrameRateTarget(int value) {
}

public float volumeMultiplier(SoundSource source) {
if (this.getVolumeMultiplier(SoundSource.MASTER) == 0.0f) {
return 0.0f;
} else {
return this.getVolumeMultiplier(source);
}
}

private float getVolumeMultiplier(SoundSource source) {
return this.volumeMultipliers.getOrDefault(source, 1.0f);
}

Expand Down
Expand Up @@ -2,10 +2,13 @@

import java.util.Map;

import dynamic_fps.impl.config.Config;
import net.minecraft.client.Minecraft;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand Down Expand Up @@ -46,7 +49,10 @@ private float calculateVolume(SoundInstance instance) {
};
*/

public void dynamic_fps$updateVolume(SoundSource source) {
@Unique
private static final Minecraft dynamic_fps$minecraft = Minecraft.getInstance();

public void dynamic_fps$updateVolume(Config before, SoundSource source) {
SoundEngine self = (SoundEngine)(Object) this;

if (!self.loaded) {
Expand All @@ -65,18 +71,25 @@ private float calculateVolume(SoundInstance instance) {
boolean isMusic = source.equals(SoundSource.MUSIC) || source.equals(SoundSource.RECORDS);

self.instanceToChannel.forEach((instance, handle) -> {
float volume = self.calculateVolume(instance);

if (instance.getSource().equals(source)) {
float volume = self.calculateVolume(instance);

handle.execute(channel -> {
if (volume <= 0.0f) {
if (!isMusic) {
channel.stop();
} else {
// Pause music unconditionally when volume is zero
// Other sounds get paused by vanilla if the game is also paused, else we cancel them
if (isMusic) {
channel.pause();
} else if (!dynamic_fps$minecraft.isPaused()) {
channel.stop();
}
} else {
channel.unpause();
// Resume music if Minecraft is active *and* the previous volume was zero
// Prevents us from resuming music when the user returns to a paused game
// Or the game was just paused and the user is focusing to another window
if (!dynamic_fps$minecraft.isPaused() && isMusic && before.volumeMultiplier(source) == 0.0f) {
channel.unpause();
}
channel.setVolume(volume);
}
});
Expand All @@ -87,8 +100,8 @@ private float calculateVolume(SoundInstance instance) {
/**
* 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.
* This is done in favor of actually setting the volume to zero because it
* Allows pausing and resuming the sound engine without cancelling all active sounds.
*/
@Inject(method = { "play", "playDelayed" }, at = @At("HEAD"), cancellable = true)
private void play(SoundInstance instance, CallbackInfo callbackInfo) {
Expand Down
@@ -1,9 +1,10 @@
package dynamic_fps.impl.util.duck;

import dynamic_fps.impl.config.Config;
import net.minecraft.sounds.SoundSource;

public interface DuckSoundEngine {
public default void dynamic_fps$updateVolume(SoundSource source) {
public default void dynamic_fps$updateVolume(Config before, SoundSource source) {
throw new RuntimeException("No implementation for dynamic_fps$updateVolume was found.");
}
}

0 comments on commit 6c11f4e

Please sign in to comment.