Skip to content

Commit

Permalink
👍 Add a validation check
Browse files Browse the repository at this point in the history
  • Loading branch information
AutumnSky1010 committed Dec 7, 2024
1 parent db83eac commit b283a3d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
20 changes: 18 additions & 2 deletions src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override short[] GenerateWave(SoundFormat format, int length, int volume,
}

var result = new List<short>(length);
var unitWave = GenerateUnitWave(format, volume, hertz);
var unitWave = GenerateUnitWaveInternal(format, volume, hertz);
for (var i = 0; i < length / unitWave.Length; i++)
{
result.AddRange(unitWave);
Expand All @@ -43,7 +43,23 @@ internal override WaveTypeBase Clone()
return new PseudoTriangleWave();
}

public static short[] GenerateUnitWave(SoundFormat format, int volume, double hertz)
/// <summary>
/// Generates one cycle of a sound waveform at the specified frequency.<br/>
/// 指定した周波数の音声波形1周期分を生成する。
/// </summary>
/// <param name="format">Format of the sound. <br/>音のフォーマット</param>
/// <param name="volume">Volume <br/>音量(0 ~ 100)</param>
/// <param name="hertz">Hertz of the sound. <br/>音の周波数</param>
/// <returns>The array of wave data.</returns>
/// <exception cref="ArgumentOutOfRangeException">Hertz must be non-negative and greater than 0.</exception>
/// <exception cref="ArgumentOutOfRangeException">Volume must be below 100 and above 0.</exception>
public short[] GenerateUnitWave(SoundFormat format, int volume, double hertz)
{
GenerateUnitWaveInternal(format, volume, hertz);
return GenerateUnitWave(format, volume, hertz);
}

private static short[] GenerateUnitWaveInternal(SoundFormat format, int volume, double hertz)
{
var repeatNumber = (int)((int)format.SamplingFrequency / hertz);

Expand Down
19 changes: 15 additions & 4 deletions src/SoundMaker/Sounds/WaveTypes/SquareWave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SquareWave(SquareWaveRatio squareWaveRatio)
public override short[] GenerateWave(SoundFormat format, int length, int volume, double hertz)
{
var result = new List<short>(length);
var unitWave = GenerateUnitWave(format, volume, hertz);
var unitWave = GenerateUnitWaveInternal(format, volume, hertz, SquareWaveRatio);
for (var i = 0; i < length / unitWave.Length; i++)
{
result.AddRange(unitWave);
Expand All @@ -47,12 +47,23 @@ internal override WaveTypeBase Clone()
return new SquareWave(SquareWaveRatio);
}

private short[] GenerateUnitWave(SoundFormat format, int volume, double hertz)
/// <summary>
/// Generates one cycle of a sound waveform at the specified frequency.<br/>
/// 指定した周波数の音声波形1周期分を生成する。
/// </summary>
/// <param name="format">Format of the sound. <br/>音のフォーマット</param>
/// <param name="volume">Volume <br/>音量(0 ~ 100)</param>
/// <param name="hertz">Hertz of the sound. <br/>音の周波数</param>
/// <returns>The array of wave data.</returns>
/// <exception cref="ArgumentOutOfRangeException">Hertz must be non-negative and greater than 0.</exception>
/// <exception cref="ArgumentOutOfRangeException">Volume must be below 100 and above 0.</exception>
public short[] GenerateUnitWave(SoundFormat format, int volume, double hertz)
{
return GenerateUnitWave(format, volume, hertz, SquareWaveRatio);
CheckGenerateUnitWaveArgs(volume, hertz);
return GenerateUnitWaveInternal(format, volume, hertz, SquareWaveRatio);
}

public static short[] GenerateUnitWave(SoundFormat format, int volume, double hertz, SquareWaveRatio squareWaveRatio)
private static short[] GenerateUnitWaveInternal(SoundFormat format, int volume, double hertz, SquareWaveRatio squareWaveRatio)
{
var ratioIndex = (int)squareWaveRatio;
var allRepeatTimes = (int)((int)format.SamplingFrequency / hertz);
Expand Down
20 changes: 18 additions & 2 deletions src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public override short[] GenerateWave(SoundFormat format, int length, int volume,
{
CheckGenerateWaveArgs(length, volume, hertz);
var result = new List<short>(length);
var unitWave = GenerateUnitWave(format, volume, hertz);
var unitWave = GenerateUnitWaveInternal(format, volume, hertz);
for (var i = 0; i < length / unitWave.Length; i++)
{
result.AddRange(unitWave);
Expand All @@ -25,7 +25,23 @@ internal override WaveTypeBase Clone()
return new TriangleWave();
}

private static short[] GenerateUnitWave(SoundFormat format, int volume, double hertz)
/// <summary>
/// Generates one cycle of a sound waveform at the specified frequency.<br/>
/// 指定した周波数の音声波形1周期分を生成する。
/// </summary>
/// <param name="format">Format of the sound. <br/>音のフォーマット</param>
/// <param name="volume">Volume <br/>音量(0 ~ 100)</param>
/// <param name="hertz">Hertz of the sound. <br/>音の周波数</param>
/// <returns>The array of wave data.</returns>
/// <exception cref="ArgumentOutOfRangeException">Hertz must be non-negative and greater than 0.</exception>
/// <exception cref="ArgumentOutOfRangeException">Volume must be below 100 and above 0.</exception>
public short[] GenerateUnitWave(SoundFormat format, int volume, double hertz)
{
CheckGenerateUnitWaveArgs(volume, hertz);
return GenerateUnitWaveInternal(format, volume, hertz);
}

private static short[] GenerateUnitWaveInternal(SoundFormat format, int volume, double hertz)
{
var repeatNumber = (int)((int)format.SamplingFrequency / hertz);

Expand Down
6 changes: 6 additions & 0 deletions src/SoundMaker/Sounds/WaveTypes/WaveTypeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ protected void CheckGenerateWaveArgs(int length, int volume, double hertz)
{
throw new ArgumentOutOfRangeException(nameof(length), "'length' must be non-negative.");
}

CheckGenerateUnitWaveArgs(volume, hertz);
}

protected static void CheckGenerateUnitWaveArgs(int volume, double hertz)
{
if (volume is > 100 or < 0)
{
throw new ArgumentOutOfRangeException(nameof(volume), "'volume must be below than 100 and more than 0.");
Expand Down

0 comments on commit b283a3d

Please sign in to comment.