# Audio wavetable synthesis

This topic is 2656 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to understand how wavetable synthesis work.
Let's say I have a wavetable with 5000 samples of a piano, and that samples represent a note, let's say, middle C.

Now, I want to synthetize another note, for example, an A above middle C, and write the result to a sound buffer.
If I understand correctly, I need to resample the original samples at a different rate.

Frequency of middle C: 261.63
Frequency of A above it: 440
440 / 261.63 = 1.68176432

This means the A needs to be resampled 1.68176432 times faster than C.
So I would for example, do something like

float sourcePos = 0;
for (int i=0; i<5000; i++)
{
dest=samples[(int)sourcePos];
sourcePos += 1.68176432;
if ((int)sourcePos>5000) sourcePos -= 5000; // wrap around
}

Is my logic right until now?
(Of course, if this correct, then the algoritm should be improved with some kind of interpolation)

Also I didn't find a audio programming subforum here, so I hope this is the right place for this topic.

##### Share on other sites
Well, I just implemented the algoritm I described and it sounds horribly, even just one semitone up or down...

This code works fine when Dx = 1.0, every other value outputs strange sound data, for example, I as using an ocarina sample of a A6 note, and when trying an A#6 (one semitone up), instead of sounding like an ocarina, sounds like something between a motor and a very big bell.

  float Dx = Frequencies / Frequencies[_BaseNote];  float Pos = 0.0;  while (samples>0)   {    uint16 N = _sample[(int)(Pos)];    *Dest = N; Dest++; // left channel    *Dest = N; Dest++; // right channel    Pos += Dx;    if (Pos>_LoopEnd)       Pos -= (_LoopEnd-_LoopStart);    Samples--;  }

(Ignore any sintax error in the code, I just converted it into C++ now, I'm using a different language)

EDIT: Fixed, it was actually a bug in my implementation, I was not sampling correctly when the position wrapped around.

[Edited by - Relfos on November 12, 2010 6:06:31 PM]