From 4078a2dcba6cc76f6cfef418ccb1024907fe0697 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 1 Jan 2023 13:59:58 +0000 Subject: [PATCH] Clamp speaker volume again Looks like this was removed in b048b6666d652dafc4614b086782f956de58d377. --- .../shared/peripheral/speaker/DfpwmState.java | 4 ++-- .../shared/peripheral/speaker/SpeakerPeripheral.java | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/speaker/DfpwmState.java b/src/main/java/dan200/computercraft/shared/peripheral/speaker/DfpwmState.java index 8c5d0ed1a..437c56a46 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/speaker/DfpwmState.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/speaker/DfpwmState.java @@ -8,7 +8,6 @@ package dan200.computercraft.shared.peripheral.speaker; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaTable; import dan200.computercraft.shared.util.PauseAwareTimer; -import net.minecraft.util.math.MathHelper; import javax.annotation.Nonnull; import java.nio.ByteBuffer; @@ -16,6 +15,7 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; import static dan200.computercraft.shared.peripheral.speaker.SpeakerPeripheral.SAMPLE_RATE; +import static dan200.computercraft.shared.peripheral.speaker.SpeakerPeripheral.clampVolume; /** * Internal state of the DFPWM decoder and the state of playback. @@ -87,7 +87,7 @@ class DfpwmState buffer.flip(); pendingAudio = buffer; - pendingVolume = MathHelper.clamp( volume.orElse( (double) pendingVolume ).floatValue(), 0.0f, 3.0f ); + pendingVolume = (float) clampVolume( volume.orElse( (double) pendingVolume ) ); return true; } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java index cb3534168..951c09b49 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java @@ -25,6 +25,7 @@ import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocationException; import net.minecraft.util.SoundCategory; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.vector.Vector3d; import net.minecraft.world.World; @@ -222,7 +223,7 @@ public abstract class SpeakerPeripheral implements IPeripheral @LuaFunction public final boolean playNote( ILuaContext context, String instrumentA, Optional volumeA, Optional pitchA ) throws LuaException { - float volume = (float) checkFinite( 1, volumeA.orElse( 1.0 ) ); + float volume = (float) clampVolume( checkFinite( 1, volumeA.orElse( 1.0 ) ) ); float pitch = (float) checkFinite( 2, pitchA.orElse( 1.0 ) ); NoteBlockInstrument instrument = null; @@ -271,7 +272,7 @@ public abstract class SpeakerPeripheral implements IPeripheral @LuaFunction public final boolean playSound( ILuaContext context, String name, Optional volumeA, Optional pitchA ) throws LuaException { - float volume = (float) checkFinite( 1, volumeA.orElse( 1.0 ) ); + float volume = (float) clampVolume( checkFinite( 1, volumeA.orElse( 1.0 ) ) ); float pitch = (float) checkFinite( 2, pitchA.orElse( 1.0 ) ); ResourceLocation identifier; @@ -396,6 +397,11 @@ public abstract class SpeakerPeripheral implements IPeripheral } } + static double clampVolume( double volume ) + { + return MathHelper.clamp( volume, 0, 3 ); + } + private static final class PendingSound { final ResourceLocation location;