mirror of
https://github.com/Ale32bit/Capy64.git
synced 2025-01-18 18:46:43 +00:00
Better square waveform
This commit is contained in:
parent
e24192fb26
commit
8e08c80d91
1 changed files with 16 additions and 3 deletions
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue