diff --git a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs index c413a97..59477c8 100644 --- a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs @@ -26,12 +26,12 @@ 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++) + var unitWave = GenerateUnitWaveInternal(format, volume, hertz); + 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,23 @@ internal override WaveTypeBase Clone() return new PseudoTriangleWave(); } - private List 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); @@ -77,6 +93,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..6c2dc0d 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++) + var unitWave = GenerateUnitWaveInternal(format, volume, hertz, SquareWaveRatio); + 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,25 @@ internal override WaveTypeBase Clone() return new SquareWave(SquareWaveRatio); } - private List 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) { - var ratioIndex = (int)SquareWaveRatio; + CheckGenerateUnitWaveArgs(volume, hertz); + return GenerateUnitWaveInternal(format, volume, hertz, SquareWaveRatio); + } + + private static short[] GenerateUnitWaveInternal(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 +84,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 a145d90..423e011 100644 --- a/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs @@ -8,12 +8,12 @@ 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++) + var unitWave = GenerateUnitWaveInternal(format, volume, hertz); + 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,23 @@ internal override WaveTypeBase Clone() return new TriangleWave(); } - private List 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); @@ -58,6 +74,6 @@ private List GenerateUnitWave(SoundFormat format, int volume, double hert result[halfCount + quarterCount + midOffsetFromHalfCount] = (short)(short.MinValue * volumeMagnification); } - return result.ToList(); + return result; } } 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.");