From db83eac11bbef9663c668f6b9d994a810f6d188d Mon Sep 17 00:00:00 2001 From: AutumnSky1010 <66455966+AutumnSky1010@users.noreply.github.com> Date: Sat, 7 Dec 2024 15:22:30 +0900 Subject: [PATCH 1/3] :arrow_double_down: Add GenerateUnitWave() --- .../Sounds/WaveTypes/PseudoTriangleWave.cs | 8 +++---- src/SoundMaker/Sounds/WaveTypes/SquareWave.cs | 21 ++++++++++++------- .../Sounds/WaveTypes/TriangleWave.cs | 8 +++---- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs index c413a97..e9d5d8f 100644 --- a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs @@ -27,11 +27,11 @@ public override short[] GenerateWave(SoundFormat format, int length, int volume, var result = new List(length); var unitWave = GenerateUnitWave(format, volume, hertz); - for (var i = 0; i < length / unitWave.Count; i++) + for (var i = 0; i < length / unitWave.Length; i++) { result.AddRange(unitWave); } - for (var i = 0; i < length % unitWave.Count; i++) + for (var i = 0; i < length % unitWave.Length; i++) { result.Add(0); } @@ -43,7 +43,7 @@ internal override WaveTypeBase Clone() return new PseudoTriangleWave(); } - private List GenerateUnitWave(SoundFormat format, int volume, double hertz) + public static short[] GenerateUnitWave(SoundFormat format, int volume, double hertz) { var repeatNumber = (int)((int)format.SamplingFrequency / hertz); @@ -77,6 +77,6 @@ private List GenerateUnitWave(SoundFormat format, int volume, double hert } } - return result.ToList(); + return result; } } diff --git a/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs b/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs index c576ee3..ae0c48a 100644 --- a/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs @@ -20,22 +20,22 @@ public SquareWave(SquareWaveRatio squareWaveRatio) /// /// デューティ比を基に繰り返し回数を求める為の値 /// - private List<(double, double)> Ratio { get; } = new List<(double, double)> - { + private static List<(double, double)> Ratio { get; } = + [ (0.875, 0.125), (0.75, 0.25), (0.5, 0.5), - }; + ]; public override short[] GenerateWave(SoundFormat format, int length, int volume, double hertz) { var result = new List(length); var unitWave = GenerateUnitWave(format, volume, hertz); - for (var i = 0; i < length / unitWave.Count; i++) + for (var i = 0; i < length / unitWave.Length; i++) { result.AddRange(unitWave); } - for (var i = 0; i < length % unitWave.Count; i++) + for (var i = 0; i < length % unitWave.Length; i++) { result.Add(0); } @@ -47,9 +47,14 @@ internal override WaveTypeBase Clone() return new SquareWave(SquareWaveRatio); } - private List GenerateUnitWave(SoundFormat format, int volume, double hertz) + private short[] GenerateUnitWave(SoundFormat format, int volume, double hertz) { - var ratioIndex = (int)SquareWaveRatio; + return GenerateUnitWave(format, volume, hertz, SquareWaveRatio); + } + + public static short[] GenerateUnitWave(SoundFormat format, int volume, double hertz, SquareWaveRatio squareWaveRatio) + { + var ratioIndex = (int)squareWaveRatio; var allRepeatTimes = (int)((int)format.SamplingFrequency / hertz); var firstRepeatTimes = (int)(allRepeatTimes * Ratio[ratioIndex].Item1); // なぜか配列よりリストの方が早い @@ -68,6 +73,6 @@ private List GenerateUnitWave(SoundFormat format, int volume, double hert { result.Add(bottom); } - return result; + return result.ToArray(); } } diff --git a/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs b/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs index cded92f..54ea211 100644 --- a/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs @@ -9,11 +9,11 @@ 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); - for (var i = 0; i < length / unitWave.Count; i++) + for (var i = 0; i < length / unitWave.Length; i++) { result.AddRange(unitWave); } - for (var i = 0; i < length % unitWave.Count; i++) + for (var i = 0; i < length % unitWave.Length; i++) { result.Add(0); } @@ -25,7 +25,7 @@ internal override WaveTypeBase Clone() return new TriangleWave(); } - private List GenerateUnitWave(SoundFormat format, int volume, double hertz) + private static short[] GenerateUnitWave(SoundFormat format, int volume, double hertz) { var repeatNumber = (int)((int)format.SamplingFrequency / hertz); @@ -58,6 +58,6 @@ private List GenerateUnitWave(SoundFormat format, int volume, double hert result[halfCount + quarterCount + midOffsetFromHalfCount] = short.MinValue; } - return result.ToList(); + return result; } } 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 2/3] :+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."); From 316c826aa6ad12dee75c1dfe8c1806ca26f2eb3d Mon Sep 17 00:00:00 2001 From: AutumnSky1010 <66455966+AutumnSky1010@users.noreply.github.com> Date: Sat, 7 Dec 2024 22:42:48 +0900 Subject: [PATCH 3/3] :bug: Fix a bug of method call --- src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs index b915086..59477c8 100644 --- a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs @@ -55,8 +55,8 @@ internal override WaveTypeBase Clone() /// 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); + CheckGenerateUnitWaveArgs(volume, hertz); + return GenerateUnitWaveInternal(format, volume, hertz); } private static short[] GenerateUnitWaveInternal(SoundFormat format, int volume, double hertz)