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

Add fade in/out function. #23

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
3 changes: 2 additions & 1 deletion src/SoundMaker/Sounds/SoundChannels/SquareSoundChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public override ushort[] GenerateWave()
var result = new List<ushort>();
foreach (var soundComponent in SoundComponents)
{
result.AddRange(soundComponent.GenerateWave(Format, Tempo, new SquareWave(Ratio)));
var wave = soundComponent.GenerateWave(Format, Tempo, new SquareWave(Ratio));
result.AddRange(wave);
}
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
Loading