

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I need to create a specific custom waveform for an oscillator for use with Web Audio API.

I have a Javascript function to output the desired waveform (calculating a y between -1 and 1 for any given x), and the plotted result looks like this:

Custom wave shape

However the Web Audio API documentation only lets you create custom wavetables based on harmonic tables via the createPeriodicWave function, which can then be used to configure a custom oscillator via setPeriodicWave. Is there a generic technique that can be used to compute the harmonic tables based on my waveform function?

share|improve this question
up vote 5 down vote accepted

A DFT (or FFT) with a length of exactly one period of your custom waveform will produce a harmonic table. Just low-pass filter and sample your waveform 2^N times, and feed that to a generic library FFT. (Choose a large enough 2^N to be at least more than 2X the low-pass filter's or your waveform's intrinsic highest frequency content). The magnitudes of the FFT's resulting complex bins will be your harmonic power levels.

share|improve this answer
Thank you! I found dsp.js but I have two problems: 1) I need to provide a sample rate with my input, but I'm building a wave table for an oscillator and I don't have a sample rate, it's not like I am processing signal from recorded input, and 2) it outputs a single table yet the Web Audio createPeriodicWave function takes two arrays (real and imag), not one! – Clafou Jul 14 '14 at 21:27
The sample rate is the number of samples divided by the period of your waveform, but unneeded since you will be using the raw FFT bin output, not the frequency values represented by each bin. To use a complex input FFT of real data, just set the imaginary components or array to zeros. – hotpaw2 Jul 14 '14 at 22:01
Great, thank you! – Clafou Jul 15 '14 at 0:59
By the way, the 2nd point in my previous comment was incorrect, dsp.js does output the two (real and imag) arrays. – Clafou Jul 15 '14 at 10:13

Your Answer


