-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing with ADSR envelopes, additive voice signal generator
- Loading branch information
1 parent
7c8d144
commit 80877cd
Showing
7 changed files
with
692 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
ComputerSystems/Commodore64/Sid/NAudioImpl/SidAdsrSampleProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
using NAudio.Wave; | ||
using System; | ||
|
||
namespace Commodore64.Sid.NAudioImpl | ||
{ | ||
public class SidAdsrSampleProvider : ISampleProvider | ||
{ | ||
private readonly ISampleProvider source; | ||
private int position; | ||
private readonly int sampleRate; | ||
private bool gate; | ||
private float lastLevel; | ||
|
||
private int attackSamples, decaySamples, releaseSamples; | ||
private float sustainLevel; | ||
|
||
public float AttackSeconds | ||
{ | ||
get => attackSamples / (float)sampleRate; | ||
set => attackSamples = (int)(sampleRate * value); | ||
} | ||
|
||
public float DecaySeconds | ||
{ | ||
get => decaySamples / (float)sampleRate; | ||
set => decaySamples = (int)(sampleRate * value); | ||
} | ||
|
||
public float SustainLevel | ||
{ | ||
get => sustainLevel; | ||
set => sustainLevel = value; | ||
} | ||
|
||
public float ReleaseSeconds | ||
{ | ||
get => releaseSamples / (float)sampleRate; | ||
set => releaseSamples = (int)(sampleRate * value); | ||
} | ||
|
||
|
||
public SidAdsrSampleProvider(ISampleProvider source) | ||
{ | ||
this.source = source; | ||
sampleRate = source.WaveFormat.SampleRate; | ||
} | ||
|
||
public void Gate(bool isOpen) | ||
{ | ||
gate = isOpen; | ||
if (!gate) // If gate is closed, it's the start of the Release phase | ||
{ | ||
position = 0; | ||
} | ||
} | ||
|
||
public WaveFormat WaveFormat => source.WaveFormat; | ||
|
||
public int Read(float[] buffer, int offset, int count) | ||
{ | ||
int sourceSamplesRead = source.Read(buffer, offset, count); | ||
|
||
for (int n = 0; n < sourceSamplesRead; n++) | ||
{ | ||
float envMultiplier = GetEnvelopeMultiplier(position++); | ||
buffer[offset + n] *= envMultiplier; | ||
} | ||
|
||
return sourceSamplesRead; | ||
} | ||
|
||
private float GetEnvelopeMultiplier(int position) | ||
{ | ||
if (gate) // Attack and Decay | ||
{ | ||
if (position < attackSamples) | ||
{ | ||
return lastLevel = (float)position / attackSamples; // Linear Attack | ||
} | ||
|
||
int decayPosition = position - attackSamples; | ||
if (decayPosition < decaySamples) | ||
{ | ||
float decayRate = (sustainLevel - lastLevel) / decaySamples; // Calculate the rate of decay | ||
return lastLevel += decayRate; // Linear Decay | ||
} | ||
|
||
return lastLevel = sustainLevel; // Sustain | ||
} | ||
else // Release | ||
{ | ||
if (position < releaseSamples) | ||
{ | ||
float releaseRate = lastLevel / releaseSamples; // Calculate the rate of release | ||
return lastLevel -= releaseRate; // Linear Release | ||
} | ||
return 0f; | ||
} | ||
} | ||
|
||
private float GetEnvelopeMultiplierExponential(int position) | ||
{ | ||
if (gate) // Attack and Decay | ||
{ | ||
if (position < attackSamples) | ||
{ | ||
return lastLevel = (float)Math.Pow(position / (double)attackSamples, 0.3f); // Exponential Attack | ||
} | ||
|
||
int decayPosition = position - attackSamples; | ||
if (decayPosition < decaySamples) | ||
{ | ||
return lastLevel *= (float)Math.Pow(sustainLevel / lastLevel, 1.0f / decaySamples); // Exponential Decay | ||
} | ||
|
||
return lastLevel = sustainLevel; // Sustain | ||
} | ||
else // Release | ||
{ | ||
if (position < releaseSamples) | ||
{ | ||
float releaseFactor = 1.0f - (position / (float)releaseSamples); | ||
return lastLevel *= (float)Math.Pow(releaseFactor, 0.03f); // Exponential Release | ||
} | ||
return 0f; | ||
} | ||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
ComputerSystems/Commodore64/Sid/NAudioImpl/SidAdsrSampleProvider2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using NAudio.Wave; | ||
using System; | ||
|
||
namespace Commodore64.Sid.NAudioImpl | ||
{ | ||
public class SidAdsrSampleProvider2 : ISampleProvider | ||
{ | ||
private readonly ISampleProvider source; | ||
|
||
private readonly SidEnvelopeGenerator adsr; | ||
|
||
private float attackSeconds; | ||
private float decaySeconds; | ||
private float sustainLevel; | ||
private float releaseSeconds; | ||
|
||
|
||
public float AttackSeconds | ||
{ | ||
get | ||
{ | ||
return attackSeconds; | ||
} | ||
set | ||
{ | ||
attackSeconds = value; | ||
adsr.AttackRate = attackSeconds * WaveFormat.SampleRate; | ||
} | ||
} | ||
|
||
public float DecaySeconds | ||
{ | ||
get | ||
{ | ||
return decaySeconds; | ||
} | ||
set | ||
{ | ||
decaySeconds = value; | ||
adsr.DecayRate = decaySeconds * WaveFormat.SampleRate; | ||
} | ||
} | ||
|
||
public float SustainLevel | ||
{ | ||
get | ||
{ | ||
return sustainLevel; | ||
} | ||
set | ||
{ | ||
sustainLevel = value; | ||
adsr.SustainLevel = sustainLevel; | ||
} | ||
} | ||
|
||
public float ReleaseSeconds | ||
{ | ||
get | ||
{ | ||
return releaseSeconds; | ||
} | ||
set | ||
{ | ||
releaseSeconds = value; | ||
adsr.ReleaseRate = releaseSeconds * WaveFormat.SampleRate; | ||
} | ||
} | ||
|
||
public WaveFormat WaveFormat => source.WaveFormat; | ||
|
||
public SidAdsrSampleProvider2(ISampleProvider source) | ||
{ | ||
if (source.WaveFormat.Channels > 1) | ||
{ | ||
throw new ArgumentException("Currently only supports mono inputs"); | ||
} | ||
|
||
this.source = source; | ||
adsr = new SidEnvelopeGenerator(); | ||
AttackSeconds = 0.01f; | ||
adsr.SustainLevel = 1f; | ||
adsr.DecayRate = 0f * WaveFormat.SampleRate; | ||
ReleaseSeconds = 0.3f; | ||
adsr.Gate(gate: true); | ||
} | ||
|
||
public int Read(float[] buffer, int offset, int count) | ||
{ | ||
if (adsr.State == SidEnvelopeGenerator.EnvelopeState.Idle) | ||
{ | ||
return 0; | ||
} | ||
|
||
int num = source.Read(buffer, offset, count); | ||
for (int i = 0; i < num; i++) | ||
{ | ||
buffer[offset++] *= adsr.Process(); | ||
} | ||
|
||
return num; | ||
} | ||
|
||
public void Gate(bool gate) | ||
{ | ||
adsr.Gate(gate); | ||
} | ||
} | ||
} |
Oops, something went wrong.