There is a Impulse wave shape made from adding sine-waves together, now I have made the basic Square, Triangle, and Saw functions, those are easy:
Code: Select all (#)
float getSquare(float phase, float resolution)
{
resolution *= 2.f;
float yr = sinf(phase);
for(float h = 3.f; h < resolution; h += 2.f)
yr += sinf(phase*h)/h;
return yr;
}
float getSawtooth(float phase, float resolution)
{
float yr = sinf(phase);
for(float h = 2.f; h <= resolution; h += 1.f)
yr += sinf(phase*h)/h;
return yr;
}
float getTriangle(float phase, float resolution)
{
resolution *= 2.f;
float yr = sinf(phase);
float sign = -1.f;
for(float h = 3.f; h <= resolution; h += 2.f)
{
yr += (sinf(phase*h) / (h*h)) * sign;
sign *= -1.f;
}
return yr;
}
This is for a FOSS tone generator I am working on, details I can provide in DM, if anyone is good with this kind of thing I would appreciate your help in this matter greatly, and would love to credit you in my FOSS project as such.
Technically each shape only needs to produce up to 30 additions, so the solution provided could just be a table/array of the 30 different harmonics that need to be added together, it doesn't have to be 100% generated by math like I am doing above. Execution speed is priority.
Here is an Impulse reconstructed from the website above to 10 harmonics: (realistically all it needs)
Code: Select all (#)
float getImpulse(float phase, float resolution)
{
float yr = 0.f;
if(resolution >= 0){yr += sinf(phase * Hz(100.f));}
if(resolution >= 1){yr -= cosf(phase * Hz(200.f)) * 0.9f;}
if(resolution >= 2){yr -= sinf(phase * Hz(300.f)) * 0.8f;}
if(resolution >= 3){yr += cosf(phase * Hz(400.f)) * 0.7f;}
if(resolution >= 4){yr += sinf(phase * Hz(500.f)) * 0.6f;}
if(resolution >= 5){yr -= cosf(phase * Hz(600.f)) * 0.5f;}
if(resolution >= 6){yr -= sinf(phase * Hz(700.f)) * 0.4f;}
if(resolution >= 7){yr += cosf(phase * Hz(800.f)) * 0.3f;}
if(resolution >= 8){yr += sinf(phase * Hz(900.f)) * 0.2f;}
if(resolution >= 9){yr -= cosf(phase * Hz(1000.f))* 0.1f;}
return yr;
}