From b283a3dcc206778f43da6c812ab7dae38a0260ed Mon Sep 17 00:00:00 2001 From: AutumnSky1010 <66455966+AutumnSky1010@users.noreply.github.com> Date: Sat, 7 Dec 2024 22:17:25 +0900 Subject: [PATCH] :+1: Add a validation check --- .../Sounds/WaveTypes/PseudoTriangleWave.cs | 20 +++++++++++++++++-- src/SoundMaker/Sounds/WaveTypes/SquareWave.cs | 19 ++++++++++++++---- .../Sounds/WaveTypes/TriangleWave.cs | 20 +++++++++++++++++-- .../Sounds/WaveTypes/WaveTypeBase.cs | 6 ++++++ 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs index e9d5d8f..b915086 100644 --- a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs @@ -26,7 +26,7 @@ public override short[] GenerateWave(SoundFormat format, int length, int volume, } var result = new List(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); @@ -43,7 +43,23 @@ internal override WaveTypeBase Clone() return new PseudoTriangleWave(); } - public static short[] GenerateUnitWave(SoundFormat format, int volume, double hertz) + /// + /// Generates one cycle of a sound waveform at the specified frequency.
+ /// 指定した周波数の音声波形1周期分を生成する。 + ///
+ /// Format of the sound.
音のフォーマット + /// Volume
音量(0 ~ 100) + /// Hertz of the sound.
音の周波数 + /// The array of wave data. + /// Hertz must be non-negative and greater than 0. + /// Volume must be below 100 and above 0. + 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); diff --git a/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs b/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs index ae0c48a..6c2dc0d 100644 --- a/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs @@ -30,7 +30,7 @@ public SquareWave(SquareWaveRatio squareWaveRatio) public override short[] GenerateWave(SoundFormat format, int length, int volume, double hertz) { var result = new List(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); @@ -47,12 +47,23 @@ internal override WaveTypeBase Clone() return new SquareWave(SquareWaveRatio); } - private short[] GenerateUnitWave(SoundFormat format, int volume, double hertz) + /// + /// Generates one cycle of a sound waveform at the specified frequency.
+ /// 指定した周波数の音声波形1周期分を生成する。 + ///
+ /// Format of the sound.
音のフォーマット + /// Volume
音量(0 ~ 100) + /// Hertz of the sound.
音の周波数 + /// The array of wave data. + /// Hertz must be non-negative and greater than 0. + /// Volume must be below 100 and above 0. + 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); diff --git a/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs b/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs index 54ea211..fbef7c9 100644 --- a/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs @@ -8,7 +8,7 @@ public override short[] GenerateWave(SoundFormat format, int length, int volume, { CheckGenerateWaveArgs(length, volume, hertz); var result = new List(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); @@ -25,7 +25,23 @@ internal override WaveTypeBase Clone() return new TriangleWave(); } - private static short[] GenerateUnitWave(SoundFormat format, int volume, double hertz) + /// + /// Generates one cycle of a sound waveform at the specified frequency.
+ /// 指定した周波数の音声波形1周期分を生成する。 + ///
+ /// Format of the sound.
音のフォーマット + /// Volume
音量(0 ~ 100) + /// Hertz of the sound.
音の周波数 + /// The array of wave data. + /// Hertz must be non-negative and greater than 0. + /// Volume must be below 100 and above 0. + 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); diff --git a/src/SoundMaker/Sounds/WaveTypes/WaveTypeBase.cs b/src/SoundMaker/Sounds/WaveTypes/WaveTypeBase.cs index a7323d3..0ce732f 100644 --- a/src/SoundMaker/Sounds/WaveTypes/WaveTypeBase.cs +++ b/src/SoundMaker/Sounds/WaveTypes/WaveTypeBase.cs @@ -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.");