Sign in to follow this  
Relfos

Audio wavetable synthesis

Recommended Posts

Relfos    349
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[i]=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 this post


Link to post
Share on other sites
Relfos    349
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[Note] / 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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this