From c15f16a4cbba6dc9091dadb4870c7d771bb3a6a3 Mon Sep 17 00:00:00 2001 From: AutumnSky1010 <66455966+AutumnSky1010@users.noreply.github.com> Date: Fri, 20 Dec 2024 11:14:20 +0900 Subject: [PATCH] :recycle: Modified type and algorithm for higher speed monaural wave --- src/SoundMaker/Sounds/TrackBaseSound.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/SoundMaker/Sounds/TrackBaseSound.cs b/src/SoundMaker/Sounds/TrackBaseSound.cs index b6a434b..d4f3ced 100644 --- a/src/SoundMaker/Sounds/TrackBaseSound.cs +++ b/src/SoundMaker/Sounds/TrackBaseSound.cs @@ -219,7 +219,8 @@ public MonauralWave GenerateMonauralWave() .Where(track => track.Count != 0) .Max(track => track.EndIndex); - var wave = new double[maxEndIndex + 1]; + var wave = new long[maxEndIndex + 1]; + long maxAmplitude = 0; foreach (var (_, tracks) in _tracksTimeMap) { @@ -234,12 +235,14 @@ public MonauralWave GenerateMonauralWave() for (int i = track.StartIndex; i <= track.EndIndex; i++) { wave[i] += trackWave[i - track.StartIndex]; + var amplitude = Math.Abs(wave[i]); + maxAmplitude = maxAmplitude < amplitude ? amplitude : maxAmplitude; } } } - var normalizedRight = NormalizeAndClamp(wave); - return new(normalizedRight); + var normalized = NormalizeAndClamp(wave, maxAmplitude); + return new(normalized); } /// @@ -284,13 +287,11 @@ public StereoWave GenerateStereoWave() var amplitudeLeft = Math.Abs(left[i]); var amplitudeRight = Math.Abs(right[i]); maxAmplitudeRight = maxAmplitudeRight < amplitudeRight ? amplitudeRight : maxAmplitudeRight; - maxAmplitudeRight = maxAmplitudeLeft < amplitudeLeft ? amplitudeLeft : maxAmplitudeLeft; + maxAmplitudeLeft = maxAmplitudeLeft < amplitudeLeft ? amplitudeLeft : maxAmplitudeLeft; } } } - var normalizedRight = NormalizeAndClamp(right); - var normalizedLeft = NormalizeAndClamp(left); var normalizedRight = NormalizeAndClamp(right, maxAmplitudeRight); var normalizedLeft = NormalizeAndClamp(left, maxAmplitudeLeft); return new(normalizedRight, normalizedLeft);