Yea, it really is great for quickly testing ideas and ironing out algorithms - I've been playing around with generating harmonics to a given signal today. Do you know anything about the math involved in that? I've never really messed with it before and I'm using algorithms that basically just call sin(sampleValue + Pi/2 (or Pi/4)). It is working and I'm getting some cool results, but I can't wrap my head around what this is doing or why this is all it takes to generate harmonics like this. I have a solid base in trig and vector and matrix maths that make heavy use of trig for orientations and the like - but why exactly is trigonometry involved in harmonics like this? I'm kinda confused...
I just noticed your post after re-reading the thread.
The algorithm I use for sin waves is
splX = sin(2*$pi*freq*i);
Where splX is the sample you want to set, i increases by 1/srate each sample, and freq is the frequency of the tone I'm generating (e.g. 440 Hz for A)
The basic math of it is, that if you graph sin(x), it draws a standard sin wave. As x increases, your amplitude fluctuates at a consistent frequency. In fact, if you use 440 for freq in the above example, it will go from 0 to 1 to 0 to -1 back to 0 440 times in a second, generating an A tone.
You can also do some fun stuff to shape the wave.
To turn your sin wave into a square wave, you just need to only allow 1 and -1 as your sample values.
i.e.
splX = sin(2*$pi*freq*i);
(splX > 0 ) ?
(
splX = 1;
) : (
splX = -1;
);
To make a sawtooth wave, start your sample at 1 and decrease it until you hit 0:
prev =
get the previous sample somehow
splX = prev - freq/srate;
splX <= 0 ? splX = 1.0;
To make a triangle wave, take your sawtooth wave, and let it go down to -1 (but take the absolute value!)
prev =
get the previous sample somehow
spl = prev - 2*freq/srate;
spl < -1 ? spl = 1.0;
spl >= 0 ? splX = spl : splX = -1 * spl;
(also, BOO! why doesn't the forum support