Better square waveform

This commit is contained in:
Alessandro Proto 2023-02-14 15:04:48 +01:00
parent e24192fb26
commit 8e08c80d91

View file

@ -46,18 +46,31 @@ public class Audio : IDisposable
return buffer; return buffer;
} }
private static double GetSquarePoint(double frequency, double x)
{
double v = 0;
for (int n = 0; n <= 25; n++)
{
v += Math.Sin(Math.PI * (2 * n) * x * frequency)
/ (2 * n + 1)
/ 2 + 0.5;
}
return v;
}
public static byte[] GenerateSquareWave(double frequency, double time, double volume = 1d) public static byte[] GenerateSquareWave(double frequency, double time, double volume = 1d)
{ {
var amplitude = 128 * volume; var amplitude = 128 * volume;
var timeStep = 1d / SampleRate; var timeStep = 1d / SampleRate;
frequency /= 2;
var buffer = new byte[(int)(SampleRate * time)]; var buffer = new byte[(int)(SampleRate * time)];
var ctime = 0d; var ctime = 0d;
for (int i = 0; i < buffer.Length; i++) for (int i = 0; i < buffer.Length; i++)
{ {
double angle = (Math.PI * frequency) * ctime; var v = GetSquarePoint(frequency, ctime);
double factor = Math.Sin(angle); buffer[i] = (byte)(v * amplitude);
buffer[i] = (byte)(factor >= 0 ? amplitude : 0);
ctime += timeStep; ctime += timeStep;
} }
return buffer; return buffer;