Skip to content

Commit

Permalink
♻️ Modified type and algorithm for higher speed monaural wave
Browse files Browse the repository at this point in the history
  • Loading branch information
AutumnSky1010 committed Dec 20, 2024
1 parent 8e77a93 commit c15f16a
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/SoundMaker/Sounds/TrackBaseSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c15f16a

Please sign in to comment.