Daily DSP tip :-)
![]()
So, I played a bit with software synthesizers in the past and last week I took the project up for revision. Doing so, I came up with the following easy waveform generators:
- Sine: double wave_sine(double x) { return sin(x); }
- Square: double wave_square(double x) { return (abs(x)-floor(abs(x)))<0.5?-1.0:1.0; }
- Saw tooth: double wave_saw(double x) { return (abs(x)-floor(abs(x))); }
- Triangle: double wave_triangle(double x) { return abs(abs(x)-floor(abs(x)))*2.0-1.0); }
Having the basic generators, we move on to making them “easy” controlable by Hz rather than [0;1] or [0;
] when it comes to sine. I use these generators in relation to sound - and most sound is generated at 44k1Hz, i.e. 44100 samples per second per channel. With this in mind, we get that each period should be transformed from/to [0;44100]. Let i be the current sample index in the stream; we’ll observe that output[i]=wave_sine((Hz*
/44100.0)*x) if we set Hz=440.0 we get a nice sine A-note. In addition, a square/saw/triangle A-note can be output[i]=wave_triangle((Hz/44100.0)*x).
I hope this is useful to some one else than me :-)
