Search code examples
fftweb-audio-apiifft

How to make a wavetable with Inverse FFT in web-audio api or any other tool


I would like to know how one could generate a wavetable out of a wav file for example.

I know a wavetable can be used in web audio api with setPerdiodic wave and I know how to use it.

But what do I need to do to create my own wavetables? I read about inverse FFT, but I did find nearly nothing. I don't need any code just an idea or a formula of how to get the wavetable from an wav file to a Buffer.


Solution

  • There are a few constraints here and I'm not sure how good the result will be.

    • Your wav file source can't be too long; the PeriodicWave object only supports arrays up to size 8192 or so.
    • I'm going to assume your waveform is intended to be periodic. If the last sample and the first aren't reasonably close to each other, there will be a hard-to-reproduce jump.
    • The waveform must have zero mean, so if it doesn't you should remove the mean.

    With that taken care of, select a power of two greater than the length of your wave file (not strictly needed, but most FFTs expect powers of two). Zero-pad the wave file if the length is not a power of two. Then compute the the FFT. You'll either get an array of complex numbers or two arrays. Separate these out to real and imaginary arrays and use them for contructing the PeriodicWave.