user1132760
user1132760

Reputation:

Calculating triangle wave table for a VST

I'm creating a VST for a project, and trying to create an oscillator produces sawtooth, pulse, sin and triangle wave forms. I've looked everywhere and cannot seem to find anything useful (for me), essentially because I'm struggling to understand it.

so far I have...

 for (i=0;i<KWaveSize;i++)
 {
      sawtooth[i] = (float)(-1. + (2. * ((double)i / (double)kWaveSize)));
      pulse[i] = (i < wh) ? -1.f : 1.f;
      sine [i] (float)sin(twoPi * ((float)i /kWaveSize));
      triangle[i] = ....
 }

I'm just struggling on how to create the correct waveform using this.

I tried doing:

 triangle[i] = (float)(1 -((twoPi / KWaveSize) * i));

this was an educated guess based on the book I was following, but produces a distorted sound.

If anyone has done anything like this that can help, or point me to a newbie tutorial for all things sound synthesis where wave table generation etc is covered it would be greatly appreciated.

Thanks.

Upvotes: 1

Views: 828

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308391

You need a wave that switches direction - it goes positive from 0 to kWaveSize/2 and negative from there to kWaveSize.

triangle[i] = i < kWaveSize/2 ? -1.0 + 2.0 * i / (kWaveSize*0.5) : 1.0 - 2.0 * i / (kWaveSize*0.5);

Upvotes: 1

Related Questions