From 13f37ec260666ff87d5388cf15043d8582345289 Mon Sep 17 00:00:00 2001 From: VaDiM Date: Wed, 16 Apr 2025 12:47:33 +0300 Subject: [PATCH] Fix audioClip converting --- AssetStudio/Math/Clamp.cs | 18 ++++++++++++++++++ AssetStudioGUI/AssetStudioGUIForm.cs | 6 +----- AssetStudioUtility/Audio/AudioClipConverter.cs | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 AssetStudio/Math/Clamp.cs diff --git a/AssetStudio/Math/Clamp.cs b/AssetStudio/Math/Clamp.cs new file mode 100644 index 0000000..e2dc083 --- /dev/null +++ b/AssetStudio/Math/Clamp.cs @@ -0,0 +1,18 @@ +using System; +using System.Runtime.CompilerServices; + +namespace AssetStudio +{ + public static partial class MathHelper + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Clamp(float value, float minValue, float maxValue) + { +#if NETFRAMEWORK + return Math.Max(minValue, Math.Min(value, maxValue)); +#else + return Math.Clamp(value, minValue, maxValue); +#endif + } + } +} diff --git a/AssetStudioGUI/AssetStudioGUIForm.cs b/AssetStudioGUI/AssetStudioGUIForm.cs index 830b9a4..9fafa10 100644 --- a/AssetStudioGUI/AssetStudioGUIForm.cs +++ b/AssetStudioGUI/AssetStudioGUIForm.cs @@ -2866,11 +2866,7 @@ namespace AssetStudioGUI } FMODtimerLabel.Text = $"{ms / 1000 / 60:00}:{ms / 1000 % 60:00}.{ms / 10 % 100:00} / {FMODlenms / 1000 / 60:00}:{FMODlenms / 1000 % 60:00}.{FMODlenms / 10 % 100:00}"; -#if NETFRAMEWORK - FMODprogressBar.Value = (int)Math.Max(0, Math.Min(ms * 1000f / FMODlenms, 1000)); -#else - FMODprogressBar.Value = (int)Math.Clamp(ms * 1000f / FMODlenms, 0, 1000); -#endif + FMODprogressBar.Value = (int)AssetStudio.MathHelper.Clamp(ms * 1000f / FMODlenms, 0, 1000); FMODstatusLabel.Text = paused ? "Paused " : playing ? "Playing" : "Stopped"; if (system.hasHandle() && channel.hasHandle()) diff --git a/AssetStudioUtility/Audio/AudioClipConverter.cs b/AssetStudioUtility/Audio/AudioClipConverter.cs index d166078..7c5044c 100644 --- a/AssetStudioUtility/Audio/AudioClipConverter.cs +++ b/AssetStudioUtility/Audio/AudioClipConverter.cs @@ -148,7 +148,7 @@ namespace AssetStudio { pcmFloatSample[j] = Marshal.ReadByte(srcPtr, i + j); } - var pcm16Sample = (short)(BitConverter.ToSingle(pcmFloatSample, 0) * 32767); + var pcm16Sample = (short)MathHelper.Clamp(BitConverter.ToSingle(pcmFloatSample, 0) * short.MaxValue, short.MinValue, short.MaxValue); destBuffer[offset] = (byte)(pcm16Sample & 255); destBuffer[offset + 1] = (byte)(pcm16Sample >> 8); offset += 2;