Skip to content

Commit

Permalink
👍 Add fade in/out function.
Browse files Browse the repository at this point in the history
  • Loading branch information
AutumnSky1010 committed Jun 15, 2024
1 parent e00bed1 commit b82e931
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public override ushort[] GenerateWave()
var result = new List<ushort>();
foreach (var soundComponent in SoundComponents)
{
result.AddRange(soundComponent.GenerateWave(Format, Tempo, new PseudoTriangleWave()));
var wave = soundComponent.GenerateWave(Format, Tempo, new PseudoTriangleWave());
FadeInOut(wave);
result.AddRange(wave);
}
return result.ToArray();
}
Expand Down
14 changes: 14 additions & 0 deletions src/SoundMaker/Sounds/SoundChannels/SoundChannelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ public void Import(IEnumerable<ISoundComponent> components)

public abstract ushort[] GenerateWave();

protected void FadeInOut(ushort[] 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);
}
}
}

public IEnumerator<ISoundComponent> GetEnumerator()
{
return SoundComponents.GetEnumerator();
Expand Down
2 changes: 2 additions & 0 deletions src/SoundMaker/Sounds/SoundChannels/SquareSoundChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public override ushort[] GenerateWave()
var result = new List<ushort>();
foreach (var soundComponent in SoundComponents)
{
var wave = soundComponent.GenerateWave(Format, Tempo, new SquareWave(Ratio));
FadeInOut(wave);
result.AddRange(soundComponent.GenerateWave(Format, Tempo, new SquareWave(Ratio)));
}
return result.ToArray();
Expand Down
4 changes: 3 additions & 1 deletion src/SoundMaker/Sounds/SoundChannels/TriangleSoundChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public override ushort[] GenerateWave()
var result = new List<ushort>();
foreach (var soundComponent in SoundComponents)
{
result.AddRange(soundComponent.GenerateWave(Format, Tempo, new TriangleWave()));
var wave = soundComponent.GenerateWave(Format, Tempo, new TriangleWave());
FadeInOut(wave);
result.AddRange(wave);
}
return result.ToArray();
}
Expand Down

0 comments on commit b82e931

Please sign in to comment.