diff --git a/src/SoundMaker/Sounds/MonauralMixer.cs b/src/SoundMaker/Sounds/MonauralMixer.cs index 32c3149..528c9a2 100644 --- a/src/SoundMaker/Sounds/MonauralMixer.cs +++ b/src/SoundMaker/Sounds/MonauralMixer.cs @@ -22,7 +22,7 @@ public MonauralMixer(IReadOnlyList channels) : base(channels) /// the mixed wave of monaural. モノラルの波形データ : MonauralWave public MonauralWave Mix() { - var result = Enumerable.Repeat((ushort)0, GetMaxWaveLength()).ToArray(); + var result = Enumerable.Repeat((short)0, GetMaxWaveLength()).ToArray(); _ = Parallel.ForEach(Channels, channel => { var waveNumericData = channel.GenerateWave(); @@ -30,7 +30,7 @@ public MonauralWave Mix() { for (var i = 0; i < waveNumericData.Length; i++) { - result[i] += (ushort)(waveNumericData[i] / Channels.Count); + result[i] += (short)(waveNumericData[i] / Channels.Count); } } }); diff --git a/src/SoundMaker/Sounds/MonauralWave.cs b/src/SoundMaker/Sounds/MonauralWave.cs index ffd1860..ad3c2f4 100644 --- a/src/SoundMaker/Sounds/MonauralWave.cs +++ b/src/SoundMaker/Sounds/MonauralWave.cs @@ -8,18 +8,18 @@ public class MonauralWave : IWave /// constructor. コンストラクタ /// /// the collection of wave data. 波形データを表す配列 - public MonauralWave(IReadOnlyCollection wave) + public MonauralWave(IReadOnlyCollection wave) { var argumentIntegers = wave.ToArray(); - OriginalVolumeWave = new ushort[wave.Count]; - Wave = new ushort[wave.Count]; + OriginalVolumeWave = new short[wave.Count]; + Wave = new short[wave.Count]; Array.Copy(argumentIntegers, OriginalVolumeWave, argumentIntegers.Length); Array.Copy(argumentIntegers, Wave, argumentIntegers.Length); } - private ushort[] OriginalVolumeWave { get; set; } + private short[] OriginalVolumeWave { get; set; } - private ushort[] Wave { get; set; } + private short[] Wave { get; set; } public int Volume { get; private set; } = 100; @@ -37,7 +37,7 @@ public void ChangeVolume(int volume) Volume = volume; for (var i = 0; i < Wave.Length; i++) { - Wave[i] = (ushort)(Wave[i] * (volume / 100d)); + Wave[i] = (short)(Wave[i] * (volume / 100d)); } } @@ -48,7 +48,7 @@ public void ChangeVolume(int volume) public void Append(MonauralWave wave) { Wave = Wave.Concat(wave.GetWave()).ToArray(); - OriginalVolumeWave = new ushort[Wave.Length]; + OriginalVolumeWave = new short[Wave.Length]; Array.Copy(Wave, OriginalVolumeWave, Wave.Length); } @@ -59,7 +59,7 @@ public byte[] GetBytes(BitRateType bitRate) var result = new List(Wave.Length * 2); foreach (var value in Wave) { - var bytes = BitConverter.GetBytes((short)((value - short.MaxValue) / 2)); + var bytes = BitConverter.GetBytes(value); result.Add(bytes[0]); result.Add(bytes[1]); } @@ -70,7 +70,7 @@ public byte[] GetBytes(BitRateType bitRate) var result = new List(Wave.Length); foreach (var value in Wave) { - result.Add((byte)(value / 256)); + result.Add((byte)(value / 256 + 128)); } return result.ToArray(); } @@ -79,10 +79,10 @@ public byte[] GetBytes(BitRateType bitRate) /// /// get the wave. 音の波形データを取得するメソッド。 /// - /// the wave. 波形データ : unsigned short[] - public ushort[] GetWave() + /// the wave. 波形データ : short[] + public short[] GetWave() { - var result = new ushort[Wave.Length]; + var result = new short[Wave.Length]; Array.Copy(Wave, result, Wave.Length); return result; } diff --git a/src/SoundMaker/Sounds/Score/BasicSoundComponentBase.cs b/src/SoundMaker/Sounds/Score/BasicSoundComponentBase.cs index 9fbbb32..785645e 100644 --- a/src/SoundMaker/Sounds/Score/BasicSoundComponentBase.cs +++ b/src/SoundMaker/Sounds/Score/BasicSoundComponentBase.cs @@ -27,9 +27,9 @@ public BasicSoundComponentBase(LengthType length, bool isDotted) /// public bool IsDotted { get; } - public abstract ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType); + public abstract short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType); - public abstract ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType); + public abstract short[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType); public int GetWaveArrayLength(SoundFormat format, int tempo) { diff --git a/src/SoundMaker/Sounds/Score/ISoundComponent.cs b/src/SoundMaker/Sounds/Score/ISoundComponent.cs index c4789d1..bb576bd 100644 --- a/src/SoundMaker/Sounds/Score/ISoundComponent.cs +++ b/src/SoundMaker/Sounds/Score/ISoundComponent.cs @@ -23,10 +23,10 @@ public interface ISoundComponent /// quarter note/rest per minute. 一分間の四分音符・休符の数 /// length of the array. 配列の長さ /// type of wave.波形の種類 - /// data of wave. 波形データ : unsigned short[] + /// data of wave. 波形データ : short[] /// Tempo must be non-negative and greater than 0. /// Length must be non-negative. - ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType); + short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType); /// /// generate the wave of wave type. 波形の種類に基づいて波形データの配列を生成するメソッド。 @@ -34,7 +34,7 @@ public interface ISoundComponent /// format of the sound.音のフォーマット /// quarter note/rest per minute. 一分間の四分音符・休符の数 /// type of wave.波形の種類 - /// data of wave. 波形データ : unsigned short[] + /// data of wave. 波形データ : short[] /// Tempo must be non-negative and greater than 0. - ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType); + short[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType); } diff --git a/src/SoundMaker/Sounds/Score/Note.cs b/src/SoundMaker/Sounds/Score/Note.cs index 4313a22..aa9815c 100644 --- a/src/SoundMaker/Sounds/Score/Note.cs +++ b/src/SoundMaker/Sounds/Score/Note.cs @@ -82,12 +82,12 @@ private static void CheckArgument(Scale scale, int scaleNumber) } } - public override ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) + public override short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) { return waveType.GenerateWave(format, length, Volume, Hertz); } - public override ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) + public override short[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) { var length = GetWaveArrayLength(format, tempo); return GenerateWave(format, tempo, length, waveType); diff --git a/src/SoundMaker/Sounds/Score/Rest.cs b/src/SoundMaker/Sounds/Score/Rest.cs index 60d4bca..3af7480 100644 --- a/src/SoundMaker/Sounds/Score/Rest.cs +++ b/src/SoundMaker/Sounds/Score/Rest.cs @@ -14,12 +14,12 @@ public class Rest : BasicSoundComponentBase public Rest(LengthType length, bool isDotted = false) : base(length, isDotted) { } - public override ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) + public override short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) { return GetWave(format, tempo, length); } - public override ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) + public override short[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) { return GetWave(format, tempo); } @@ -29,8 +29,8 @@ public override ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBas /// /// format of the sound.音のフォーマット /// 一分間の休符の数 - /// 0埋めされた配列 : unsigned short[] - private ushort[] GetWave(SoundFormat format, int tempo) + /// 0埋めされた配列 : short[] + private short[] GetWave(SoundFormat format, int tempo) { var length = GetWaveArrayLength(format, tempo); return GetWave(format, tempo, length); @@ -42,9 +42,9 @@ private ushort[] GetWave(SoundFormat format, int tempo) /// format of the sound.音のフォーマット /// 一分間の休符の数 /// length of the array. 配列の長さ - /// 0埋めされた配列 : unsigned short[] - private ushort[] GetWave(SoundFormat format, int tempo, int length) + /// 0埋めされた配列 : short[] + private short[] GetWave(SoundFormat format, int tempo, int length) { - return Enumerable.Repeat(0, length).ToArray(); + return Enumerable.Repeat(0, length).ToArray(); } } diff --git a/src/SoundMaker/Sounds/Score/Tie.cs b/src/SoundMaker/Sounds/Score/Tie.cs index 467629e..9d3d7e4 100644 --- a/src/SoundMaker/Sounds/Score/Tie.cs +++ b/src/SoundMaker/Sounds/Score/Tie.cs @@ -47,12 +47,12 @@ public Tie(Note baseNote, IReadOnlyCollection additionalNotes) /// public IReadOnlyCollection AdditionalNotes { get; } - public ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) + public short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) { return BaseNote.GenerateWave(format, tempo, length, waveType); } - public ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) + public short[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) { var length = GetWaveArrayLength(format, tempo); return GenerateWave(format, tempo, length, waveType); diff --git a/src/SoundMaker/Sounds/Score/Tuplet.cs b/src/SoundMaker/Sounds/Score/Tuplet.cs index 7dd1b18..d7e45b5 100644 --- a/src/SoundMaker/Sounds/Score/Tuplet.cs +++ b/src/SoundMaker/Sounds/Score/Tuplet.cs @@ -54,9 +54,9 @@ public int GetWaveArrayLength(SoundFormat format, int tempo) return SoundWaveLengthCalclator.Calclate(format, tempo, Length, IsDotted); } - public ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) + public short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) { - var result = new List(length); + var result = new List(length); // 一個あたりの配列の長さを算出 var count = GetLengthPerOneComponent(); int i; @@ -74,7 +74,7 @@ public ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveType return result.ToArray(); } - public ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) + public short[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) { var length = GetWaveArrayLength(format, tempo); return GenerateWave(format, tempo, length, waveType); diff --git a/src/SoundMaker/Sounds/SoundChannels/ISoundChannel.cs b/src/SoundMaker/Sounds/SoundChannels/ISoundChannel.cs index 1275b08..5d1a85d 100644 --- a/src/SoundMaker/Sounds/SoundChannels/ISoundChannel.cs +++ b/src/SoundMaker/Sounds/SoundChannels/ISoundChannel.cs @@ -43,8 +43,8 @@ public interface ISoundChannel : IEnumerable /// /// generate wave data. 音の波形データを生成するメソッド。 /// - /// the array of wave data.音の波形データの配列 : unsigned short[] - ushort[] GenerateWave(); + /// the array of wave data.音の波形データの配列 : short[] + short[] GenerateWave(); /// /// add sound component to this. サウンドコンポーネントを追加するメソッド。 diff --git a/src/SoundMaker/Sounds/SoundChannels/LowBitNoiseSoundChannel.cs b/src/SoundMaker/Sounds/SoundChannels/LowBitNoiseSoundChannel.cs index 88de65f..5952578 100644 --- a/src/SoundMaker/Sounds/SoundChannels/LowBitNoiseSoundChannel.cs +++ b/src/SoundMaker/Sounds/SoundChannels/LowBitNoiseSoundChannel.cs @@ -29,9 +29,9 @@ public LowBitNoiseSoundChannel(int tempo, SoundFormat format, PanType panType) : { } - public override ushort[] GenerateWave() + public override short[] GenerateWave() { - var result = new List(); + var result = new List(); foreach (var soundComponent in SoundComponents) { result.AddRange(soundComponent.GenerateWave(Format, Tempo, new LowBitNoiseWave())); diff --git a/src/SoundMaker/Sounds/SoundChannels/PseudoTriangleSoundChannel.cs b/src/SoundMaker/Sounds/SoundChannels/PseudoTriangleSoundChannel.cs index 9a1c925..d42589b 100644 --- a/src/SoundMaker/Sounds/SoundChannels/PseudoTriangleSoundChannel.cs +++ b/src/SoundMaker/Sounds/SoundChannels/PseudoTriangleSoundChannel.cs @@ -27,9 +27,9 @@ public PseudoTriangleSoundChannel(int tempo, SoundFormat format, PanType panType { } - public override ushort[] GenerateWave() + public override short[] GenerateWave() { - var result = new List(); + var result = new List(); foreach (var soundComponent in SoundComponents) { var wave = soundComponent.GenerateWave(Format, Tempo, new PseudoTriangleWave()); diff --git a/src/SoundMaker/Sounds/SoundChannels/SoundChannelBase.cs b/src/SoundMaker/Sounds/SoundChannels/SoundChannelBase.cs index 7d11e4d..0dbbd8b 100644 --- a/src/SoundMaker/Sounds/SoundChannels/SoundChannelBase.cs +++ b/src/SoundMaker/Sounds/SoundChannels/SoundChannelBase.cs @@ -117,18 +117,19 @@ public void Import(IEnumerable components) WaveArrayLength = components.Sum(component => component.GetWaveArrayLength(Format, Tempo)); } - public abstract ushort[] GenerateWave(); + public abstract short[] GenerateWave(); - protected void FadeInOut(ushort[] wave) + protected void FadeInOut(short[] wave) { var tenMSCount = (int)((double)Format.SamplingFrequency * 0.01); if (wave.Length > tenMSCount << 1) { for (int i = 0; i < tenMSCount; i++) { - double magnification = (1.0 / tenMSCount) * (i + 1); - wave[i] = (ushort)(((short)(wave[i] - short.MaxValue - 1) * magnification) + short.MaxValue + 1); - wave[wave.Length - i - 1] = (ushort)(((short)(wave[wave.Length - i - 1] - short.MaxValue - 1) * magnification) + short.MaxValue + 1); + double fadeMagnification = (1.0 / tenMSCount) * (i + 1); + + wave[i] = (short)(fadeMagnification * wave[i]); + wave[wave.Length - i - 1] = (short)(fadeMagnification * wave[wave.Length - i - 1]); } } } diff --git a/src/SoundMaker/Sounds/SoundChannels/SquareSoundChannel.cs b/src/SoundMaker/Sounds/SoundChannels/SquareSoundChannel.cs index c53b89d..ee185a3 100644 --- a/src/SoundMaker/Sounds/SoundChannels/SquareSoundChannel.cs +++ b/src/SoundMaker/Sounds/SoundChannels/SquareSoundChannel.cs @@ -40,9 +40,9 @@ public SquareSoundChannel(int tempo, SoundFormat format, SquareWaveRatio ratio, /// private SquareWaveRatio Ratio { get; } - public override ushort[] GenerateWave() + public override short[] GenerateWave() { - var result = new List(); + var result = new List(); foreach (var soundComponent in SoundComponents) { var wave = soundComponent.GenerateWave(Format, Tempo, new SquareWave(Ratio)); diff --git a/src/SoundMaker/Sounds/SoundChannels/TriangleSoundChannel.cs b/src/SoundMaker/Sounds/SoundChannels/TriangleSoundChannel.cs index 9fa068a..44a07c0 100644 --- a/src/SoundMaker/Sounds/SoundChannels/TriangleSoundChannel.cs +++ b/src/SoundMaker/Sounds/SoundChannels/TriangleSoundChannel.cs @@ -26,9 +26,9 @@ public TriangleSoundChannel(int tempo, SoundFormat format, PanType panType, int /// Tempo must be non-negative and greater than 0. public TriangleSoundChannel(int tempo, SoundFormat format, PanType panType) : base(tempo, format, panType) { } - public override ushort[] GenerateWave() + public override short[] GenerateWave() { - var result = new List(); + var result = new List(); foreach (var soundComponent in SoundComponents) { var wave = soundComponent.GenerateWave(Format, Tempo, new TriangleWave()); diff --git a/src/SoundMaker/Sounds/StereoMixer.cs b/src/SoundMaker/Sounds/StereoMixer.cs index 15d8446..5603b53 100644 --- a/src/SoundMaker/Sounds/StereoMixer.cs +++ b/src/SoundMaker/Sounds/StereoMixer.cs @@ -26,15 +26,15 @@ public StereoWave Mix() { var max = GetMaxWaveLength(); var channelCount = GetChannelCount(); - var rightResult = Enumerable.Repeat((ushort)0, max).ToArray(); - var leftResult = Enumerable.Repeat((ushort)0, max).ToArray(); + var rightResult = Enumerable.Repeat((short)0, max).ToArray(); + var leftResult = Enumerable.Repeat((short)0, max).ToArray(); _ = Parallel.ForEach(Channels, channel => { Merge(leftResult, rightResult, channel, channelCount); }); return new StereoWave(rightResult, leftResult); } - private void Merge(ushort[] left, ushort[] right, ISoundChannel channel, ChannelCount channelCount) + private void Merge(short[] left, short[] right, ISoundChannel channel, ChannelCount channelCount) { var waveNumericData = channel.GenerateWave(); if (channel.PanType is PanType.Left) @@ -43,7 +43,7 @@ private void Merge(ushort[] left, ushort[] right, ISoundChannel channel, Channel { for (var i = 0; i < waveNumericData.Length; i++) { - left[i] += (ushort)(waveNumericData[i] / channelCount.Left); + left[i] += (short)(waveNumericData[i] / channelCount.Left); } } } @@ -53,7 +53,7 @@ private void Merge(ushort[] left, ushort[] right, ISoundChannel channel, Channel { for (var i = 0; i < waveNumericData.Length; i++) { - right[i] += (ushort)(waveNumericData[i] / channelCount.Right); + right[i] += (short)(waveNumericData[i] / channelCount.Right); } } } @@ -66,8 +66,8 @@ private void Merge(ushort[] left, ushort[] right, ISoundChannel channel, Channel { for (var i = 0; i < waveNumericData.Length; i++) { - right[i] += (ushort)(waveNumericData[i] / channelCount.Right); - left[i] += (ushort)(waveNumericData[i] / channelCount.Left); + right[i] += (short)(waveNumericData[i] / channelCount.Right); + left[i] += (short)(waveNumericData[i] / channelCount.Left); } } } diff --git a/src/SoundMaker/Sounds/StereoWave.cs b/src/SoundMaker/Sounds/StereoWave.cs index e7b46b0..9c1ce5b 100644 --- a/src/SoundMaker/Sounds/StereoWave.cs +++ b/src/SoundMaker/Sounds/StereoWave.cs @@ -9,29 +9,29 @@ public class StereoWave : IWave /// /// the wave of right. 右の波形データ /// the wave of left. 左の波形データ - public StereoWave(IReadOnlyCollection rightWave, IReadOnlyCollection leftWave) + public StereoWave(IReadOnlyCollection rightWave, IReadOnlyCollection leftWave) { var rightArgument = rightWave.ToArray(); var leftArgument = leftWave.ToArray(); - RightOriginalVolumeWave = new ushort[rightWave.Count]; - LeftOriginalVolumeWave = new ushort[leftWave.Count]; + RightOriginalVolumeWave = new short[rightWave.Count]; + LeftOriginalVolumeWave = new short[leftWave.Count]; Array.Copy(rightArgument, RightOriginalVolumeWave, rightArgument.Length); Array.Copy(leftArgument, LeftOriginalVolumeWave, leftArgument.Length); - RightWave = new ushort[rightWave.Count]; - LeftWave = new ushort[leftWave.Count]; + RightWave = new short[rightWave.Count]; + LeftWave = new short[leftWave.Count]; Array.Copy(rightArgument, RightWave, rightArgument.Length); Array.Copy(leftArgument, LeftWave, leftArgument.Length); } - private ushort[] RightOriginalVolumeWave { get; set; } + private short[] RightOriginalVolumeWave { get; set; } - private ushort[] LeftOriginalVolumeWave { get; set; } + private short[] LeftOriginalVolumeWave { get; set; } - private ushort[] RightWave { get; set; } + private short[] RightWave { get; set; } - private ushort[] LeftWave { get; set; } + private short[] LeftWave { get; set; } /// /// volume of the wave. 波形データの音量 @@ -60,14 +60,14 @@ public void ChangeVolume(int volume, SoundDirectionType channelType) case SoundDirectionType.Left: for (var i = 0; i < LeftWave.Length; i++) { - LeftWave[i] = (ushort)(LeftOriginalVolumeWave[i] * (volume / 100d)); + LeftWave[i] = (short)(LeftOriginalVolumeWave[i] * (volume / 100d)); } LeftVolume = volume; break; case SoundDirectionType.Right: for (var i = 0; i < RightWave.Length; i++) { - RightWave[i] = (ushort)(RightOriginalVolumeWave[i] * (volume / 100d)); + RightWave[i] = (short)(RightOriginalVolumeWave[i] * (volume / 100d)); } RightVolume = volume; break; @@ -75,8 +75,8 @@ public void ChangeVolume(int volume, SoundDirectionType channelType) var maxAndMinLength = GetMaxAndMinWaveLength(); for (var i = 0; i < maxAndMinLength.Min; i++) { - RightWave[i] = (ushort)(RightOriginalVolumeWave[i] * (volume / 100d)); - LeftWave[i] = (ushort)(LeftOriginalVolumeWave[i] * (volume / 100d)); + RightWave[i] = (short)(RightOriginalVolumeWave[i] * (volume / 100d)); + LeftWave[i] = (short)(LeftOriginalVolumeWave[i] * (volume / 100d)); } // 残りを処理する。 @@ -85,7 +85,7 @@ public void ChangeVolume(int volume, SoundDirectionType channelType) RightOriginalVolumeWave : LeftOriginalVolumeWave; for (var i = maxAndMinLength.Min; i < maxAndMinLength.Max; i++) { - wave[i] = (ushort)(originalWave[i] * (volume / 100d)); + wave[i] = (short)(originalWave[i] * (volume / 100d)); } RightVolume = volume; LeftVolume = volume; @@ -114,15 +114,15 @@ private List Get8BitBytes() for (var i = 0; i < maxAndMinLength.Min; i++) { // Point : ステレオの波は左右左右左右左右・・・・・・左右 - resultWave.Add((byte)(LeftWave[i] / 256)); - resultWave.Add((byte)(RightWave[i] / 256)); + resultWave.Add((byte)(LeftWave[i] / 256 + 128)); + resultWave.Add((byte)(RightWave[i] / 256 + 128)); } // 追加しきれていない波形データを追加する if (LeftWave.Length == maxAndMinLength.Max) { for (var i = maxAndMinLength.Min; i < maxAndMinLength.Max; i++) { - resultWave.Add((byte)(LeftWave[i] / 256)); + resultWave.Add((byte)(LeftWave[i] / 256 + 128)); resultWave.Add(0); } } @@ -131,7 +131,7 @@ private List Get8BitBytes() for (var i = maxAndMinLength.Min; i < maxAndMinLength.Max; i++) { resultWave.Add(0); - resultWave.Add((byte)(RightWave[i] / 256)); + resultWave.Add((byte)(RightWave[i] / 256 + 128)); } } return resultWave; @@ -147,10 +147,8 @@ private List Get16BitBytes() var resultWave = new List(maxAndMinLength.Max * 4); for (var i = 0; i < maxAndMinLength.Min; i++) { - var left = LeftWave[i] == ushort.MaxValue ? - (short)(LeftWave[i] - short.MaxValue - 1) : (short)(LeftWave[i] - short.MaxValue); - var right = RightWave[i] == ushort.MaxValue ? - (short)(RightWave[i] - short.MaxValue - 1) : (short)(RightWave[i] - short.MaxValue); + var left = LeftWave[i]; + var right = RightWave[i]; var leftBytes = BitConverter.GetBytes(left); var rightBytes = BitConverter.GetBytes(right); // Point : ステレオの波は左右左右左右左右・・・・・・左右 @@ -164,8 +162,7 @@ private List Get16BitBytes() { for (var i = maxAndMinLength.Min; i < maxAndMinLength.Max; i++) { - var left = LeftWave[i] == ushort.MaxValue ? - (short)(LeftWave[i] - short.MaxValue - 1) : (short)(LeftWave[i] - short.MaxValue); + var left = LeftWave[i]; var leftBytes = BitConverter.GetBytes(left); resultWave.Add(leftBytes[0]); resultWave.Add(leftBytes[1]); @@ -177,8 +174,7 @@ private List Get16BitBytes() { for (var i = maxAndMinLength.Min; i < maxAndMinLength.Max; i += 2) { - var right = RightWave[i] == ushort.MaxValue ? - (short)(RightWave[i] - short.MaxValue - 1) : (short)(RightWave[i] - short.MaxValue); + var right = RightWave[i]; var rightBytes = BitConverter.GetBytes(right); resultWave.Add(0); resultWave.Add(0); @@ -192,23 +188,23 @@ private List Get16BitBytes() /// /// get the wave on the right. 右側のチャンネルの音の波形データを取得するメソッド。 /// - /// the wave on the right. 右側のチャンネルの音の波形データ : unsigned short[] - public ushort[] GetRightWave() + /// the wave on the right. 右側のチャンネルの音の波形データ : short[] + public short[] GetRightWave() { - var resultushorts = new ushort[RightWave.Length]; - Array.Copy(RightWave, resultushorts, RightWave.Length); - return resultushorts; + var resultshorts = new short[RightWave.Length]; + Array.Copy(RightWave, resultshorts, RightWave.Length); + return resultshorts; } /// /// get the wave on the left. 左側のチャンネルの音の波形データを取得するメソッド。 /// - /// the wave on the left. 左側のチャンネルの音の波形データ : unsigned short[] - public ushort[] GetLeftWave() + /// the wave on the left. 左側のチャンネルの音の波形データ : short[] + public short[] GetLeftWave() { - var resultushorts = new ushort[LeftWave.Length]; - Array.Copy(LeftWave, resultushorts, LeftWave.Length); - return resultushorts; + var resultshorts = new short[LeftWave.Length]; + Array.Copy(LeftWave, resultshorts, LeftWave.Length); + return resultshorts; } /// @@ -218,11 +214,11 @@ public ushort[] GetLeftWave() public void Append(StereoWave wave) { RightWave = RightWave.Concat(wave.GetRightWave()).ToArray(); - RightOriginalVolumeWave = new ushort[RightWave.Length]; + RightOriginalVolumeWave = new short[RightWave.Length]; Array.Copy(RightWave, RightOriginalVolumeWave, RightWave.Length); LeftWave = LeftWave.Concat(wave.GetLeftWave()).ToArray(); - LeftOriginalVolumeWave = new ushort[LeftWave.Length]; + LeftOriginalVolumeWave = new short[LeftWave.Length]; Array.Copy(LeftWave, LeftOriginalVolumeWave, LeftWave.Length); } diff --git a/src/SoundMaker/Sounds/WaveTypes/LowBitNoiseWave.cs b/src/SoundMaker/Sounds/WaveTypes/LowBitNoiseWave.cs index 6e5719a..76905a7 100644 --- a/src/SoundMaker/Sounds/WaveTypes/LowBitNoiseWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/LowBitNoiseWave.cs @@ -5,31 +5,31 @@ public class LowBitNoiseWave : WaveTypeBase { [Obsolete("Use 'GenerateWave(SoundFormat format, int length, int volume, double hertz)'")] - public override ushort[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz) + public override short[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz) { CheckGenerateWaveArgs(tempo, length, volume, hertz); return GenerateWave(format, length, volume, hertz); } - public override ushort[] GenerateWave(SoundFormat format, int length, int volume, double hertz) + public override short[] GenerateWave(SoundFormat format, int length, int volume, double hertz) { - var result = new List(length); + var result = new List(length); var mode = false; var count = 1; while (count <= length) { var allRepeatTimes = (int)((int)format.SamplingFrequency / hertz); var firstRepeatTimes = (int)(allRepeatTimes * 0.5); - var height = (ushort)new Random().Next(0, ushort.MaxValue + 1); + var height = (short)new Random().Next(short.MinValue, short.MaxValue + 1); for (var i = 1; i <= firstRepeatTimes && mode && count <= length; i++, count++) { - var sound = (ushort)(height * volume / 100); + var sound = (short)(height * volume / 100); result.Add(sound); } - height = (ushort)new Random().Next(0, ushort.MaxValue + 1); + height = (short)new Random().Next(short.MinValue, short.MaxValue + 1); for (var i = 1; i <= allRepeatTimes - firstRepeatTimes && !mode && count <= length; i++, count++) { - var sound = (ushort)(height * volume / 100); + var sound = (short)(height * volume / 100); result.Add(sound); } mode = !mode; diff --git a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs index 0bcd3d3..b3ed276 100644 --- a/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/PseudoTriangleWave.cs @@ -4,14 +4,23 @@ /// public class PseudoTriangleWave : WaveTypeBase { + private static readonly short[] _leftHeights = new short[] + { + 0, -4096, -8192, -12288, -16384, -20480, -24576, -28672, -28672, -24576, -20480, -16384, -12288, -8192, -4096, 0, + }; + private static readonly short[] _rightHeights = new short[] + { + 4095, 8191, 12287, 16383, 20479, 24575, 28671, short.MaxValue, short.MaxValue, 28671, 24575, 20479, 16383, 12287, 8191, 4095 + }; + [Obsolete("Use 'GenerateWave(SoundFormat format, int length, int volume, double hertz)'")] - public override ushort[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz) + public override short[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz) { CheckGenerateWaveArgs(tempo, length, volume, hertz); return GenerateWave(format, length, volume, hertz); } - public override ushort[] GenerateWave(SoundFormat format, int length, int volume, double hertz) + public override short[] GenerateWave(SoundFormat format, int length, int volume, double hertz) { CheckGenerateWaveArgs(length, volume, hertz); @@ -23,7 +32,7 @@ public override ushort[] GenerateWave(SoundFormat format, int length, int volume return new TriangleWave().GenerateWave(format, length, volume, hertz); } - var result = new List(length); + var result = new List(length); var unitWave = GenerateUnitWave(format, volume, hertz); for (var i = 0; i < length / unitWave.Count; i++) { @@ -36,53 +45,40 @@ public override ushort[] GenerateWave(SoundFormat format, int length, int volume return result.ToArray(); } - private List GenerateUnitWave(SoundFormat format, int volume, double hertz) + private List GenerateUnitWave(SoundFormat format, int volume, double hertz) { var repeatNumber = (int)((int)format.SamplingFrequency / hertz); - // なぜか配列よりリストの方が早い - var result = new List(repeatNumber); + + var result = new short[repeatNumber]; + var steps = 32; // 音量の倍率(1.00 ~ 0.00) var volumeMagnification = volume / 100d; // 階段の幅 - var stairsWidth = repeatNumber / 32; + var stairsWidth = repeatNumber / steps; // 幅の余り - var r = repeatNumber % 32; + var r = repeatNumber % steps; - // 上り波形 - for (var i = 0; i < 16; i++) - { - var sound = (ushort)(ushort.MaxValue * (i / 15d)); - sound = (ushort)(sound * volumeMagnification); - for (var j = 0; j < stairsWidth; j++) - { - result.Add(sound); - } - // 余りがある場合は足す。波形の後ろ側の部分に足す - // ex. r = 31の場合、i = 1 ~ 15の時足す。 - if (i <= r / 2 && i != 0) - { - result.Add(sound); - } - } - // 下り波形 - // 下り波形では上り波形より数値を減らす - var negativeDiff = (ushort)(ushort.MaxValue * (1 / 30d)); - for (var i = 15; i >= 0; i--) + int position = 0; + for (int i = 0; i < _leftHeights.Length; i++) { - var sound = (ushort)(ushort.MaxValue * (i / 15d)); - sound = sound != 0 ? (ushort)(sound - negativeDiff) : sound; - sound = (ushort)(sound * volumeMagnification); - for (var j = 0; j < stairsWidth; j++) + var leftHeight = (short)(_leftHeights[i] * volumeMagnification); + var rightHeight = (short)(_rightHeights[i] * volumeMagnification); + + for (int j = 0; j < stairsWidth; j++) { - result.Add(sound); + result[position] = leftHeight; + result[result.Length - 1 - position] = rightHeight; + position++; } - // 余りがある場合は足す。波形の後ろ側の部分に足す - // ex. r = 31の場合、i = 15 ~ 0の時足す。 - if (i < (r / 2) + (r % 2)) + if (r > 0) { - result.Add(sound); + result[position] = leftHeight; + result[result.Length - 1 - position] = rightHeight; + r -= 2; + position++; } } - return result; + + return result.ToList(); } } diff --git a/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs b/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs index 1bf0b9d..bf0f10c 100644 --- a/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/SquareWave.cs @@ -28,15 +28,15 @@ public SquareWave(SquareWaveRatio squareWaveRatio) }; [Obsolete("Use 'GenerateWave(SoundFormat format, int length, int volume, double hertz)'")] - public override ushort[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz) + public override short[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz) { CheckGenerateWaveArgs(tempo, length, volume, hertz); return GenerateWave(format, length, volume, hertz); } - public override ushort[] GenerateWave(SoundFormat format, int length, int volume, double hertz) + public override short[] GenerateWave(SoundFormat format, int length, int volume, double hertz) { - var result = new List(length); + var result = new List(length); var unitWave = GenerateUnitWave(format, volume, hertz); for (var i = 0; i < length / unitWave.Count; i++) { @@ -49,24 +49,26 @@ public override ushort[] GenerateWave(SoundFormat format, int length, int volume return result.ToArray(); } - private List GenerateUnitWave(SoundFormat format, int volume, double hertz) + private List GenerateUnitWave(SoundFormat format, int volume, double hertz) { var ratioIndex = (int)SquareWaveRatio; var allRepeatTimes = (int)((int)format.SamplingFrequency / hertz); var firstRepeatTimes = (int)(allRepeatTimes * Ratio[ratioIndex].Item1); // なぜか配列よりリストの方が早い - var result = new List(allRepeatTimes); + var result = new List(allRepeatTimes); // 音量の倍率(1.00 ~ 0.00) var volumeMagnification = volume / 100d; + var top = (short)(short.MaxValue * volumeMagnification); + var bottom = (short)(-top); + for (var i = 0; i < firstRepeatTimes; i++) { - result.Add(0); + result.Add(top); } for (var i = 0; i < allRepeatTimes - firstRepeatTimes; i++) { - var sound = (ushort)(ushort.MaxValue * volumeMagnification); - result.Add(sound); + result.Add(bottom); } return result; } diff --git a/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs b/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs index 898b0e7..38e72c3 100644 --- a/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs +++ b/src/SoundMaker/Sounds/WaveTypes/TriangleWave.cs @@ -5,16 +5,16 @@ public class TriangleWave : WaveTypeBase { [Obsolete("Use 'GenerateWave(SoundFormat format, int length, int volume, double hertz)'")] - public override ushort[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz) + public override short[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz) { CheckGenerateWaveArgs(tempo, length, volume, hertz); return GenerateWave(format, length, volume, hertz); } - public override ushort[] GenerateWave(SoundFormat format, int length, int volume, double hertz) + public override short[] GenerateWave(SoundFormat format, int length, int volume, double hertz) { CheckGenerateWaveArgs(length, volume, hertz); - var result = new List(length); + var result = new List(length); var unitWave = GenerateUnitWave(format, volume, hertz); for (var i = 0; i < length / unitWave.Count; i++) { @@ -27,31 +27,39 @@ public override ushort[] GenerateWave(SoundFormat format, int length, int volume return result.ToArray(); } - private List GenerateUnitWave(SoundFormat format, int volume, double hertz) + private List GenerateUnitWave(SoundFormat format, int volume, double hertz) { - var repeatNumber = (int)format.SamplingFrequency / hertz; - // なぜか配列よりリストの方が早い - var result = new List((int)repeatNumber); - // 直線の方程式の傾きを求める。 - var slope = ushort.MaxValue / (repeatNumber / 2); - // 音量の倍率(1.00 ~ 0.00) - var volumeMagnification = volume / 100d; + var repeatNumber = (int)((int)format.SamplingFrequency / hertz); + + var result = new short[repeatNumber]; + + + var halfCount = repeatNumber >> 1; + var quarterCount = halfCount >> 1; + + var slope = ushort.MaxValue / halfCount; - // 傾き正波形 - for (var i = 1; i <= repeatNumber / 2; i++) + var volumeMagnification = volume / 100d; + // halfCount基準で中央のオフセット(lengthが奇数の場合1) + var midOffsetFromHalfCount = repeatNumber % 2; + for (int i = 0; i < quarterCount; i++) { - var sound = (ushort)(slope * i); - sound = (ushort)(sound * volumeMagnification); - result.Add(sound); + var sound = (short)(0 + slope * (i + 1)); + sound = (short)(sound * volumeMagnification); + result[i] = sound; + result[halfCount - i - 1] = sound; + + var negativeSound = (short)(-sound); + result[result.Length - i - 1] = negativeSound; + result[halfCount + i + midOffsetFromHalfCount] = negativeSound; } - // 傾き負波形 - slope = -slope; - for (var i = (int)((repeatNumber / 2) + 1); i <= repeatNumber; i++) + + if (halfCount % 2 != 0) { - var sound = (ushort)(ushort.MaxValue + (slope * i)); - sound = (ushort)(sound * volumeMagnification); - result.Add(sound); + result[quarterCount] = short.MaxValue; + result[halfCount + quarterCount + midOffsetFromHalfCount] = short.MinValue; } - return result; + + return result.ToList(); } } diff --git a/src/SoundMaker/Sounds/WaveTypes/WaveTypeBase.cs b/src/SoundMaker/Sounds/WaveTypes/WaveTypeBase.cs index aa62bc0..e168dd3 100644 --- a/src/SoundMaker/Sounds/WaveTypes/WaveTypeBase.cs +++ b/src/SoundMaker/Sounds/WaveTypes/WaveTypeBase.cs @@ -12,13 +12,13 @@ public abstract class WaveTypeBase /// length of the array. 配列の長さ /// volume 音量(0 ~ 100) /// hertz of the sound. 音の周波数 - /// the array of wave data. : unsigned short[] + /// the array of wave data. : short[] /// Tempo must be non-negative and greater than 0. /// Length must be non-negative. /// Hertz must be non-negative and greater than 0. /// Volume must be below than 100 and more than 0. [Obsolete("Use 'GenerateWave(SoundFormat format, int length, int volume, double hertz)'")] - public abstract ushort[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz); + public abstract short[] GenerateWave(SoundFormat format, int tempo, int length, int volume, double hertz); /// /// generate array of wave data. 波形データの配列を生成する。 @@ -27,11 +27,11 @@ public abstract class WaveTypeBase /// length of the array. 配列の長さ /// volume 音量(0 ~ 100) /// hertz of the sound. 音の周波数 - /// the array of wave data. : unsigned short[] + /// the array of wave data. : short[] /// Length must be non-negative. /// Hertz must be non-negative and greater than 0. /// Volume must be below than 100 and more than 0. - public abstract ushort[] GenerateWave(SoundFormat format, int length, int volume, double hertz); + public abstract short[] GenerateWave(SoundFormat format, int length, int volume, double hertz); [Obsolete("Use 'CheckGenerateWaveArgs(int length, int volume, double hertz)'")] protected void CheckGenerateWaveArgs(int tempo, int length, int volume, double hertz) diff --git a/test/UnitTests/Sounds/MonauralWaveTest.cs b/test/UnitTests/Sounds/MonauralWaveTest.cs index 1f0a0fa..ad4dd14 100644 --- a/test/UnitTests/Sounds/MonauralWaveTest.cs +++ b/test/UnitTests/Sounds/MonauralWaveTest.cs @@ -14,7 +14,7 @@ public MonauralWaveTest(ITestOutputHelper output) [Fact(DisplayName = "ボリュームの大きさが正しく反映されているかのテスト。ボリュームは0から100の間の整数値")] public void TestReflectedVolume() { - var wave = GetTestMonauralWave(defaultWaveValue: ushort.MinValue, waveLength: 100); + var wave = GetTestMonauralWave(defaultWaveValue: short.MinValue, waveLength: 100); wave.ChangeVolume(-1); // -1になってたらやばい Assert.Equal(0, wave.Volume); @@ -30,7 +30,7 @@ public void TestReflectedVolume() [Fact(DisplayName = "波形のボリュームが変更されるかのテスト")] public void TestChangeVolume() { - var defaultWaveValue = ushort.MaxValue; + var defaultWaveValue = short.MaxValue; var wave = GetTestMonauralWave(defaultWaveValue: defaultWaveValue, waveLength: 100); // 音量を二分の一にする。波形データの値も半分になるはず。 var volume = 50; @@ -39,7 +39,7 @@ public void TestChangeVolume() Output.WriteLine("ボリューム: 50"); Output.WriteLine("波形をテストする。"); - WaveAssert.Equal((ushort)(defaultWaveValue * volumeMultiplier), wave.GetWave()); + WaveAssert.Equal((short)(defaultWaveValue * volumeMultiplier), wave.GetWave()); } [Fact(DisplayName = "生成したバイト列の長さを正しく取得できるかをテストする。")] @@ -57,8 +57,8 @@ public void TestGetLengthOfBytes() [Fact(DisplayName = "二つの同じ長さの波形が正しく追加されているかをテストする。")] public void TestAppendWave() { - var monauralWave1 = GetTestMonauralWave(defaultWaveValue: ushort.MinValue, waveLength: 100); - var monauralWave2 = GetTestMonauralWave(defaultWaveValue: ushort.MaxValue, waveLength: 100); + var monauralWave1 = GetTestMonauralWave(defaultWaveValue: short.MinValue, waveLength: 100); + var monauralWave2 = GetTestMonauralWave(defaultWaveValue: short.MaxValue, waveLength: 100); // 16bitの場合 var bitRate = BitRateType.SixteenBit; // 期待する長さを計算する。 @@ -73,12 +73,12 @@ public void TestAppendWave() Assert.Equal(monauralWave1.GetLengthOfBytes(BitRateType.SixteenBit), lengthOf16bit); Assert.Equal(monauralWave1.GetLengthOfBytes(BitRateType.EightBit), lengthOf8bit); - // 末尾に追加されていることを確認する。先頭の値はusort.MinValue、末尾の値はushort.MaxValueになっているはず。 + // 末尾に追加されていることを確認する。先頭の値はusort.MinValue、末尾の値はshort.MaxValueになっているはず。 var wave = monauralWave1.GetWave(); - Assert.True(wave[0] == ushort.MinValue && wave[^1] == ushort.MaxValue); + Assert.True(wave[0] == short.MinValue && wave[^1] == short.MaxValue); } - private MonauralWave GetTestMonauralWave(ushort defaultWaveValue, int waveLength) + private MonauralWave GetTestMonauralWave(short defaultWaveValue, int waveLength) { var rightWave = Enumerable.Repeat(defaultWaveValue, waveLength).ToArray(); return new MonauralWave(rightWave); diff --git a/test/UnitTests/Sounds/Score/TestTuplet.cs b/test/UnitTests/Sounds/Score/TestTuplet.cs index c01bffd..7a85042 100644 --- a/test/UnitTests/Sounds/Score/TestTuplet.cs +++ b/test/UnitTests/Sounds/Score/TestTuplet.cs @@ -7,14 +7,14 @@ public class TestTuplet { private class SoundComponent : ISoundComponent { - public ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) + public short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) { - return new ushort[0]; + return new short[0]; } - public ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) + public short[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) { - return new ushort[0]; + return new short[0]; } public int GetWaveArrayLength(SoundFormat format, int tempo) diff --git a/test/UnitTests/Sounds/SoundChannels/TestSoundChannelBase.cs b/test/UnitTests/Sounds/SoundChannels/TestSoundChannelBase.cs index 6140772..af1c42f 100644 --- a/test/UnitTests/Sounds/SoundChannels/TestSoundChannelBase.cs +++ b/test/UnitTests/Sounds/SoundChannels/TestSoundChannelBase.cs @@ -13,22 +13,22 @@ public SoundChannelBaseDerived(int tempo, SoundFormat format, PanType panType, i { } - public override ushort[] GenerateWave() + public override short[] GenerateWave() { - return new ushort[0]; + return new short[0]; } } private class SoundComponent : ISoundComponent { - public ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) + public short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType) { - return new ushort[0]; + return new short[0]; } - public ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) + public short[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType) { - return new ushort[0]; + return new short[0]; } public int GetWaveArrayLength(SoundFormat format, int tempo) diff --git a/test/UnitTests/Sounds/StereoWaveTest.cs b/test/UnitTests/Sounds/StereoWaveTest.cs index 1dabfd5..8385bbd 100644 --- a/test/UnitTests/Sounds/StereoWaveTest.cs +++ b/test/UnitTests/Sounds/StereoWaveTest.cs @@ -14,7 +14,7 @@ public StereoWaveTest(ITestOutputHelper output) [Fact(DisplayName = "ボリュームの大きさが正しく反映されているかのテスト。ボリュームは0から100の間の整数値")] public void TestReflectedVolume() { - var wave = GetTestStereoWave(defaultWaveValue: ushort.MinValue, leftWaveLength: 1, rightWaveLength: 100); + var wave = GetTestStereoWave(defaultWaveValue: short.MinValue, leftWaveLength: 1, rightWaveLength: 100); wave.ChangeVolume(-1, SoundDirectionType.Both); // 両方-1になってたらやばい Assert.Equal(0, wave.RightVolume); @@ -34,7 +34,7 @@ public void TestReflectedVolume() [Fact(DisplayName = "ステレオ波形の左右両方のボリュームが変更されるかのテスト")] public void TestChangeVolumeBoth() { - var defaultWaveValue = ushort.MaxValue; + var defaultWaveValue = short.MaxValue; var wave = GetTestStereoWave(defaultWaveValue, 100, 100); // 音量を二分の一にする。波形データの値も半分になるはず。 var volume = 50; @@ -42,17 +42,17 @@ public void TestChangeVolumeBoth() wave.ChangeVolume(volume, SoundDirectionType.Both); Output.WriteLine("ボリューム: 50, 方向: 両方でテストする。"); Output.WriteLine("右の波形をテストする。"); - WaveAssert.Equal((ushort)(defaultWaveValue * volumeMultiplier), wave.GetRightWave()); + WaveAssert.Equal((short)(defaultWaveValue * volumeMultiplier), wave.GetRightWave()); Output.WriteLine("ボリューム: 50, 方向: 両方でテストする。"); Output.WriteLine("左の波形をテストする。"); - WaveAssert.Equal((ushort)(defaultWaveValue * volumeMultiplier), wave.GetLeftWave()); + WaveAssert.Equal((short)(defaultWaveValue * volumeMultiplier), wave.GetLeftWave()); } [Fact(DisplayName = "ステレオ波形の右のボリュームが変更されるかのテスト")] public void TestChangeVolumeRight() { - var defaultWaveValue = ushort.MaxValue; + var defaultWaveValue = short.MaxValue; var wave = GetTestStereoWave(defaultWaveValue, 100, 100); // 音量を二分の一にする。波形データの値も半分になるはず。 var volume = 50; @@ -60,7 +60,7 @@ public void TestChangeVolumeRight() wave.ChangeVolume(volume, SoundDirectionType.Right); Output.WriteLine("ボリューム: 50, 方向: 右でテストする。"); Output.WriteLine("右の波形をテストする。"); - WaveAssert.Equal((ushort)(defaultWaveValue * volumeMultiplier), wave.GetRightWave()); + WaveAssert.Equal((short)(defaultWaveValue * volumeMultiplier), wave.GetRightWave()); Output.WriteLine("ボリューム: 50, 方向: 右でテストする。"); Output.WriteLine("左の波形をテストする。"); @@ -70,7 +70,7 @@ public void TestChangeVolumeRight() [Fact(DisplayName = "ステレオ波形の右のボリュームが変更されるかのテスト")] public void TestChangeVolumeLeft() { - var defaultWaveValue = ushort.MaxValue; + var defaultWaveValue = short.MaxValue; var wave = GetTestStereoWave(defaultWaveValue, 100, 100); // 音量を二分の一にする。波形データの値も半分になるはず。 var volume = 50; @@ -82,7 +82,7 @@ public void TestChangeVolumeLeft() Output.WriteLine("ボリューム: 50, 方向: 左でテストする。"); Output.WriteLine("左の波形をテストする。"); - WaveAssert.Equal((ushort)(defaultWaveValue * volumeMultiplier), wave.GetLeftWave()); + WaveAssert.Equal((short)(defaultWaveValue * volumeMultiplier), wave.GetLeftWave()); } [Fact(DisplayName = "生成したバイト列の長さを正しく取得できるかをテストする。")] @@ -100,8 +100,8 @@ public void TestGetLengthOfBytes() [Fact(DisplayName = "二つの同じ長さの波形が正しく追加されているかをテストする。")] public void TestAppendWave() { - var stereoWave1 = GetTestStereoWave(defaultWaveValue: ushort.MinValue, leftWaveLength: 1, rightWaveLength: 100); - var stereoWave2 = GetTestStereoWave(defaultWaveValue: ushort.MaxValue, leftWaveLength: 1, rightWaveLength: 100); + var stereoWave1 = GetTestStereoWave(defaultWaveValue: short.MinValue, leftWaveLength: 1, rightWaveLength: 100); + var stereoWave2 = GetTestStereoWave(defaultWaveValue: short.MaxValue, leftWaveLength: 1, rightWaveLength: 100); // 16bitの場合 var bitRate = BitRateType.SixteenBit; // 期待する長さを計算する。 @@ -116,17 +116,17 @@ public void TestAppendWave() Assert.Equal(stereoWave1.GetLengthOfBytes(BitRateType.SixteenBit), lengthOf16bit); Assert.Equal(stereoWave1.GetLengthOfBytes(BitRateType.EightBit), lengthOf8bit); - // 末尾に追加されていることを確認する。先頭の値はusort.MinValue、末尾の値はushort.MaxValueになっているはず。 + // 末尾に追加されていることを確認する。先頭の値はusort.MinValue、末尾の値はshort.MaxValueになっているはず。 var wave = stereoWave1.GetLeftWave(); - Assert.True(wave[0] == ushort.MinValue && wave[^1] == ushort.MaxValue); + Assert.True(wave[0] == short.MinValue && wave[^1] == short.MaxValue); wave = stereoWave1.GetRightWave(); - Assert.True(wave[0] == ushort.MinValue && wave[^1] == ushort.MaxValue); + Assert.True(wave[0] == short.MinValue && wave[^1] == short.MaxValue); } - private StereoWave GetTestStereoWave(ushort defaultWaveValue, int leftWaveLength, int rightWaveLength) + private StereoWave GetTestStereoWave(short defaultWaveValue, int leftWaveLength, int rightWaveLength) { - var rightWave = Enumerable.Repeat(defaultWaveValue, rightWaveLength).ToArray(); - var leftWave = Enumerable.Repeat(defaultWaveValue, leftWaveLength).ToArray(); + var rightWave = Enumerable.Repeat(defaultWaveValue, rightWaveLength).ToArray(); + var leftWave = Enumerable.Repeat(defaultWaveValue, leftWaveLength).ToArray(); return new StereoWave(rightWave, leftWave); } } diff --git a/test/UnitTests/Sounds/WaveAssert.cs b/test/UnitTests/Sounds/WaveAssert.cs index b587903..b9f1b46 100644 --- a/test/UnitTests/Sounds/WaveAssert.cs +++ b/test/UnitTests/Sounds/WaveAssert.cs @@ -1,7 +1,7 @@ namespace SoundMakerTests.UnitTests.Sounds; internal static class WaveAssert { - public static void Equal(ushort waveValue, ushort[] targetWave) + public static void Equal(short waveValue, short[] targetWave) { foreach (var targetWaveValue in targetWave) { diff --git a/test/UseCaseTests/CreateWaveFileTest.cs b/test/UseCaseTests/CreateWaveFileTest.cs index 01828ac..5b40d02 100644 --- a/test/UseCaseTests/CreateWaveFileTest.cs +++ b/test/UseCaseTests/CreateWaveFileTest.cs @@ -127,7 +127,7 @@ public void ChangeVolume() GetStereo(SetAllScale(new PseudoTriangleSoundChannel(tempo, waveCase.SoundFormat, panType))), GetStereo(SetAllScale(new LowBitNoiseSoundChannel(tempo, waveCase.SoundFormat, panType))) }; - var result = new StereoWave(new ushort[0], new ushort[0]); + var result = new StereoWave(new short[0], new short[0]); foreach (var wave in waves) { wave.ChangeVolume(1, SoundDirectionType.Both);