Skip to content

Commit

Permalink
Merge pull request #43 from AutumnSky1010/feature/#42
Browse files Browse the repository at this point in the history
Accelerating TrackBaseSound
  • Loading branch information
AutumnSky1010 authored Dec 20, 2024
2 parents e4179b4 + c15f16a commit 8dcd96e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/SoundMaker/Sounds/Score/BasicSoundComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public BasicSoundComponentBase(LengthType length, bool isDotted)

public int GetWaveArrayLength(SoundFormat format, int tempo)
{
return SoundWaveLengthCalclator.Calclate(format, tempo, Length, IsDotted);
return SoundWaveLengthCalculator.Calculate(format, tempo, Length, IsDotted);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// <summary>
/// 音の配列の長さを計算するクラス
/// </summary>
internal static class SoundWaveLengthCalclator
internal static class SoundWaveLengthCalculator
{
/// <summary>
/// メソッド。
Expand All @@ -13,7 +13,7 @@ internal static class SoundWaveLengthCalclator
/// <param name="isDotted">付点の場合はTrueに設定する</param>
/// <returns>音の配列の長さ : int</returns>
/// <exception cref="ArgumentOutOfRangeException">Tempo must be non-negative and greater than 0.</exception>
public static int Calclate(SoundFormat format, int tempo, LengthType length, bool isDotted)
public static int Calculate(SoundFormat format, int tempo, LengthType length, bool isDotted)
{
if (tempo <= 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SoundMaker/Sounds/Score/Tie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int GetWaveArrayLength(SoundFormat format, int tempo)
var length = BaseNote.GetWaveArrayLength(format, tempo);
foreach (var note in AdditionalNotes)
{
length += SoundWaveLengthCalclator.Calclate(format, tempo, note.Length, note.IsDotted);
length += SoundWaveLengthCalculator.Calculate(format, tempo, note.Length, note.IsDotted);
}
return length;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SoundMaker/Sounds/Score/Tuplet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Tuplet(IReadOnlyList<ISoundComponent> tupletComponents, LengthType length

public int GetWaveArrayLength(SoundFormat format, int tempo)
{
return SoundWaveLengthCalclator.Calclate(format, tempo, Length, IsDotted);
return SoundWaveLengthCalculator.Calculate(format, tempo, Length, IsDotted);
}

public short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType)
Expand Down
47 changes: 28 additions & 19 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 All @@ -260,9 +263,11 @@ public StereoWave GenerateStereoWave()
.Where(track => track.Count != 0)
.Max(track => track.EndIndex);

var right = new double[maxEndIndex + 1];
var left = new double[maxEndIndex + 1];
var right = new long[maxEndIndex + 1];
var left = new long[maxEndIndex + 1];

long maxAmplitudeRight = 0;
long maxAmplitudeLeft = 0;
foreach (var (_, tracks) in _tracksTimeMap)
{
foreach (var track in tracks)
Expand All @@ -276,14 +281,19 @@ public StereoWave GenerateStereoWave()
var pan = (track.Pan + 1) / 2.0f;
for (int i = track.StartIndex; i <= track.EndIndex; i++)
{
left[i] += trackWave[i - track.StartIndex] * pan;
right[i] += trackWave[i - track.StartIndex] * (1 - pan);
left[i] += (long)(trackWave[i - track.StartIndex] * pan);
right[i] += (long)(trackWave[i - track.StartIndex] * (1 - pan));

var amplitudeLeft = Math.Abs(left[i]);
var amplitudeRight = Math.Abs(right[i]);
maxAmplitudeRight = maxAmplitudeRight < amplitudeRight ? amplitudeRight : maxAmplitudeRight;
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 All @@ -293,26 +303,25 @@ public StereoWave GenerateStereoWave()
/// </summary>
/// <param name="wave">The wave data. <br/> 波形データ。</param>
/// <returns>The normalized and clamped wave data. <br/> 正規化およびクランプされた波形データ。</returns>
private static short[] NormalizeAndClamp(double[] wave)
private static short[] NormalizeAndClamp(long[] wave, long maxAmplitude)
{
const int MaxValue = short.MaxValue;
const int MinValue = short.MinValue;

var maxAmplitude = wave.Max(Math.Abs);
var scaleFactor = 1.0;
var scaleFactor = maxAmplitude > MaxValue ? (double)MaxValue / maxAmplitude : 1.0;

var normalizedWave = new short[wave.Length];

if (maxAmplitude > MaxValue)
for (int i = 0; i < wave.Length; i++)
{
scaleFactor = MaxValue / maxAmplitude;
var scaledSample = wave[i] * scaleFactor;
normalizedWave[i] = (short)Math.Clamp(scaledSample, MinValue, MaxValue);
}

return wave.Select(sample =>
{
var scaledSample = sample * scaleFactor;
return (short)Math.Clamp(scaledSample, MinValue, MaxValue);
}).ToArray();
return normalizedWave;
}


/// <summary>
/// Imports tracks into the internal map based on their start times. <br/>
/// トラックを開始時間に基づいて内部のマップにインポートするメソッド。
Expand Down

0 comments on commit 8dcd96e

Please sign in to comment.