Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#22 Changed to implementation using short type #27

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/SoundMaker/Sounds/MonauralMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public MonauralMixer(IReadOnlyList<ISoundChannel> channels) : base(channels)
/// <returns>the mixed wave of monaural. モノラルの波形データ : MonauralWave</returns>
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();
lock (LockObject)
{
for (var i = 0; i < waveNumericData.Length; i++)
{
result[i] += (ushort)(waveNumericData[i] / Channels.Count);
result[i] += (short)(waveNumericData[i] / Channels.Count);
}
}
});
Expand Down
24 changes: 12 additions & 12 deletions src/SoundMaker/Sounds/MonauralWave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ public class MonauralWave : IWave
/// constructor. コンストラクタ
/// </summary>
/// <param name="wave">the collection of wave data. 波形データを表す配列</param>
public MonauralWave(IReadOnlyCollection<ushort> wave)
public MonauralWave(IReadOnlyCollection<short> 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;

Expand All @@ -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));
}
}

Expand All @@ -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);
}

Expand All @@ -59,7 +59,7 @@ public byte[] GetBytes(BitRateType bitRate)
var result = new List<byte>(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]);
}
Expand All @@ -70,7 +70,7 @@ public byte[] GetBytes(BitRateType bitRate)
var result = new List<byte>(Wave.Length);
foreach (var value in Wave)
{
result.Add((byte)(value / 256));
result.Add((byte)(value / 256 + 128));
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32767 / 256 + 128 = 255
-32768 / 256 + 128 = 0

}
return result.ToArray();
}
Expand All @@ -79,10 +79,10 @@ public byte[] GetBytes(BitRateType bitRate)
/// <summary>
/// get the wave. 音の波形データを取得するメソッド。
/// </summary>
/// <returns>the wave. 波形データ : unsigned short[]</returns>
public ushort[] GetWave()
/// <returns>the wave. 波形データ : short[]</returns>
public short[] GetWave()
{
var result = new ushort[Wave.Length];
var result = new short[Wave.Length];
Array.Copy(Wave, result, Wave.Length);
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/SoundMaker/Sounds/Score/BasicSoundComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public BasicSoundComponentBase(LengthType length, bool isDotted)
/// </summary>
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)
{
Expand Down
8 changes: 4 additions & 4 deletions src/SoundMaker/Sounds/Score/ISoundComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public interface ISoundComponent
/// <param name="tempo">quarter note/rest per minute. 一分間の四分音符・休符の数</param>
/// <param name="length">length of the array. 配列の長さ</param>
/// <param name="waveType">type of wave.波形の種類</param>
/// <returns>data of wave. 波形データ : unsigned short[]</returns>
/// <returns>data of wave. 波形データ : short[]</returns>
/// <exception cref="ArgumentOutOfRangeException">Tempo must be non-negative and greater than 0.</exception>
/// <exception cref="ArgumentOutOfRangeException">Length must be non-negative.</exception>
ushort[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType);
short[] GenerateWave(SoundFormat format, int tempo, int length, WaveTypeBase waveType);

/// <summary>
/// generate the wave of wave type. 波形の種類に基づいて波形データの配列を生成するメソッド。
/// </summary>
/// <param name="format">format of the sound.音のフォーマット</param>
/// <param name="tempo">quarter note/rest per minute. 一分間の四分音符・休符の数</param>
/// <param name="waveType">type of wave.波形の種類</param>
/// <returns>data of wave. 波形データ : unsigned short[]</returns>
/// <returns>data of wave. 波形データ : short[]</returns>
/// <exception cref="ArgumentOutOfRangeException">Tempo must be non-negative and greater than 0.</exception>
ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType);
short[] GenerateWave(SoundFormat format, int tempo, WaveTypeBase waveType);
}
4 changes: 2 additions & 2 deletions src/SoundMaker/Sounds/Score/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/SoundMaker/Sounds/Score/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -29,8 +29,8 @@ public override ushort[] GenerateWave(SoundFormat format, int tempo, WaveTypeBas
/// </summary>
/// <param name="format">format of the sound.音のフォーマット</param>
/// <param name="tempo">一分間の休符の数</param>
/// <returns>0埋めされた配列 : unsigned short[]</returns>
private ushort[] GetWave(SoundFormat format, int tempo)
/// <returns>0埋めされた配列 : short[]</returns>
private short[] GetWave(SoundFormat format, int tempo)
{
var length = GetWaveArrayLength(format, tempo);
return GetWave(format, tempo, length);
Expand All @@ -42,9 +42,9 @@ private ushort[] GetWave(SoundFormat format, int tempo)
/// <param name="format">format of the sound.音のフォーマット</param>
/// <param name="tempo">一分間の休符の数</param>
/// <param name="length">length of the array. 配列の長さ</param>
/// <returns>0埋めされた配列 : unsigned short[]</returns>
private ushort[] GetWave(SoundFormat format, int tempo, int length)
/// <returns>0埋めされた配列 : short[]</returns>
private short[] GetWave(SoundFormat format, int tempo, int length)
{
return Enumerable.Repeat<ushort>(0, length).ToArray();
return Enumerable.Repeat<short>(0, length).ToArray();
}
}
4 changes: 2 additions & 2 deletions src/SoundMaker/Sounds/Score/Tie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public Tie(Note baseNote, IReadOnlyCollection<Note> additionalNotes)
/// </summary>
public IReadOnlyCollection<Note> 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);
Expand Down
6 changes: 3 additions & 3 deletions src/SoundMaker/Sounds/Score/Tuplet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ushort>(length);
var result = new List<short>(length);
// 一個あたりの配列の長さを算出
var count = GetLengthPerOneComponent();
int i;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/SoundMaker/Sounds/SoundChannels/ISoundChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public interface ISoundChannel : IEnumerable<ISoundComponent>
/// <summary>
/// generate wave data. 音の波形データを生成するメソッド。
/// </summary>
/// <returns>the array of wave data.音の波形データの配列 : unsigned short[]</returns>
ushort[] GenerateWave();
/// <returns>the array of wave data.音の波形データの配列 : short[]</returns>
short[] GenerateWave();

/// <summary>
/// add sound component to this. サウンドコンポーネントを追加するメソッド。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public LowBitNoiseSoundChannel(int tempo, SoundFormat format, PanType panType) :
{
}

public override ushort[] GenerateWave()
public override short[] GenerateWave()
{
var result = new List<ushort>();
var result = new List<short>();
foreach (var soundComponent in SoundComponents)
{
result.AddRange(soundComponent.GenerateWave(Format, Tempo, new LowBitNoiseWave()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public PseudoTriangleSoundChannel(int tempo, SoundFormat format, PanType panType
{
}

public override ushort[] GenerateWave()
public override short[] GenerateWave()
{
var result = new List<ushort>();
var result = new List<short>();
foreach (var soundComponent in SoundComponents)
{
var wave = soundComponent.GenerateWave(Format, Tempo, new PseudoTriangleWave());
Expand Down
11 changes: 6 additions & 5 deletions src/SoundMaker/Sounds/SoundChannels/SoundChannelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,19 @@ public void Import(IEnumerable<ISoundComponent> 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]);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/SoundMaker/Sounds/SoundChannels/SquareSoundChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public SquareSoundChannel(int tempo, SoundFormat format, SquareWaveRatio ratio,
/// </summary>
private SquareWaveRatio Ratio { get; }

public override ushort[] GenerateWave()
public override short[] GenerateWave()
{
var result = new List<ushort>();
var result = new List<short>();
foreach (var soundComponent in SoundComponents)
{
var wave = soundComponent.GenerateWave(Format, Tempo, new SquareWave(Ratio));
Expand Down
4 changes: 2 additions & 2 deletions src/SoundMaker/Sounds/SoundChannels/TriangleSoundChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public TriangleSoundChannel(int tempo, SoundFormat format, PanType panType, int
/// <exception cref="ArgumentOutOfRangeException">Tempo must be non-negative and greater than 0.</exception>
public TriangleSoundChannel(int tempo, SoundFormat format, PanType panType) : base(tempo, format, panType) { }

public override ushort[] GenerateWave()
public override short[] GenerateWave()
{
var result = new List<ushort>();
var result = new List<short>();
foreach (var soundComponent in SoundComponents)
{
var wave = soundComponent.GenerateWave(Format, Tempo, new TriangleWave());
Expand Down
14 changes: 7 additions & 7 deletions src/SoundMaker/Sounds/StereoMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand Down
Loading
Loading