A Wikibookian believes this page should be split into smaller pages with a narrower subtopic.You can help by splitting this big page into smaller ones. Please make sure to follow the naming policy. Dividing books into smaller sections can provide more focus and allow each one to do one thing well, which benefits everyone. |

This algorithm generates very beautiful sounds, even if its idea is much simpler than that of other algorithms. It generates a perfectly looped wavetable sample which can be used in instruments. It easily generates sounds of ensembles, choirs, metallic sounds (bells) and many others.

This algorithm is public domain. This page includes some public domain C/C++ implementation sources that can be used in your projects or products.

This algorithm is implemented in ZynAddSubFX in the *PADsynth* module, which you can download to hear for yourself what beautiful sounds it is capable of generating. Before proceeding with reading this document, I recommend that you listen to all sound examples in the next category.

## Sound examples of instruments that use this algorithmEdit

These sound examples are generated by ZynAddSubFX. All instruments' wavetables are generated by this algorithm. These examples are grouped into two categories:

### With EffectsEdit

In this category, some effects are used. These effects can be reverberation, phaser, etc. Mostly, the only effect was the reverberation.

### Without EffectsEdit

All instruments in this category the instruments are "dry". No reverberation, no other effects.

- Bells, Strings
- Organ and Choir
- Saw Piano (this example contains the same notes played many times, but with different bandwidth and harmonic profiles)

## Description of the algorithmEdit

In order to understand how this algorithm works, you need to be familiar with how the author of this text thinks about the musical instruments. Please read an introduction for the description of the meaning and the importance of Bandwidth of each harmonic and Randomness.

### General descriptionEdit

This algorithm generates some large wavetables that can played at different speeds to get the desired sound. This algorithm describes only how these wavetables are generated. The result is a perfectly looped wavetable. Unlike other synthesis methods which use Inverse Fast Fourier Transform, this one does not use overlap/add methods and there is only one IFFT for the whole sample.

The basic steps are:

- Make a very large array that represents the amplitude spectrum of the sound (default all values are zero)
- Generates the distribution of each harmonic in frequency and add it to the array
- Put random phases to each frequency of the spectrum
- Do a single Inverse Fourier Transform of the whole spectrum. There is no need of any overlapping windows, because there is only one single IFFT for the whole sample.

The ouput is the sample which can be used as a wavetable. In the next image, the steps are represented graphically:

#### The bandwidth of each harmonicEdit

The author of this considers one harmonic(overtone) as being composed of many frequencies. These sine components of one harmonic are spread over a certain band of frequencies. Higher harmonics has bigger bandwidth. In natural choirs/ensembles the bandwidth is proportional to the harmonic's frequency. Here is an example of a spectrum of an instrument generated by this algorithm:

Spectrum | Close-up of the spectrum |

The harmonics becomes wider and wider, until at a certain frequency, where they may merge to a noise band (as in the full spectrum image from above). This is a normal thing and I recomand to not avoid this by limiting the bandwidth of the harmonics.

#### The frequency distribution of one harmonic/overtone (or the harmonic profile)Edit

This describes the function of the spread of the harmonic. Here are some examples of how they can be spread:

- A special case is where there is only a single sine component inside the hamonic. In this case, the harmonic and the "sine component" are the same thing.

Audio example: Single harmonic Complex sound

- Detuned. In this case there are two sine components which are detuned.

Audio example: Single harmonic Complex sound

- Evenly spread inside the harmonic (all components has the same amplitude)

Audio example: Single harmonic Complex sound

- Normal (Gaussian) distribution. The sine components amplitude are bell shaped. The largest amplitude is in the center of the band. This distribution gives the most natural sounds (it simulates a very, very large enssemble)

Audio example: Single harmonic Complex sound

Of course, you can use many others harmonic's profile functions. ZynAddSubFX's implementetation of the PADsynth algorithm offers many ways to generate the harmonic profile. Also, it's very important that the harmonic must have the same amplitude, regardless of the profile functions/parameters and the bandwidth.

#### The phases of the sine components of the harmonicsEdit

This algorithm considers the phases of the sine components of each harmonics as random.

### Steps, input and output of the algorithmEdit

#### Steps of the basic algorithmEdit

**Input:**

N- wavetable size. It's recomanded to be a power of 2. This is, usually, a big number (like 262144)samplerate- the samplerate (eg. 44100)f- frequency of the the fundamental note (eg. 440)bw- bandwidth of first harmonic in cents (eg. 50 cents) must be greater than zeronumber_harmonics- the number of harmonics. Of course, number_harmonics<(samplerate/f)A[1..number_harmonics]- amplitude of the harmonics

**Output**

smp[0..N-1]- the generated wavetable

**Internal variables**

freq_amp[0..N/2-1]= {0,0,0,0,...,0}freq_phase[0..N/2-1]etc...

**Functions**

RND()returns a random value between 0 and 1IFFT()is the inverse fourier transformnormalize_sample()normalizes samplesprofile(fi,bwi){ x=fi/bwi; return exp(-x*x)/bwi; };

**Steps**

FOR nh = 1 to number_harmonics bw_Hz=(pow(2,bw/1200)-1.0)*f*nh; bwi=bw_Hz/(2.0*samplerate); fi=f*nh/samplerate; FOR i=0 to N/2-1 hprofile=profile((i/N)-fi,bwi); freq_amp[i]=freq_amp[i]+hprofile*A[nh]; ENDFOR ENDFOR FOR i=0 to N/2-1 freq_phase[i]=RND()*2*PI; ENDFOR smp=IFFT(N,freq_amp,freq_phase); normalize_sample(N,smp); OUTPUT smp

#### The extended algorithmEdit

The differences between the extended algorithm and the basic algorithm are minor: There is an additional parameter:

**bwscale**: that specify how much the bandwidth of the harmonic increase according to it's frequency.- Also, there is defined a function called
**relF(N)**who returns the relative frequency of the N'th overtone. It allows to generate detuned harmonics or even metallic sounds (like bells).

The difference between the basic algorithm is at the computation of *bw_Hz* and *fi*:

bw_Hz=(pow(2.0,bw/1200.0)-1.0)*f*pow(relF(nh),bwscale); fi=f*relF(nh)/samplerate;

If the *relF(N)* function returns N and the bwscale is equal to 1, this algorithm will be equivalent to the basic algorithm.

#### Example Graph of *freq_amp* arrayEdit

Graphs of the (basic algorithm) *freq_amp* array for N=262144, f=500 Hz, bw=100 cents, samplerate=44.1 Khz, and A[] where A[n]=1.0/sqrt(n)

Whole array | Close-up of the array |

#### Audio example of the output of this algorithmEdit

Media:Padsynth_c_basic_sample.ogg - this is the resulting **smp** array converted to ogg vorbis

### Public domain C/C++ code that shows a simple implementationEdit

I wrote some C/C++ implementations of the basic algorithm and the extended algorithm. The *"c_basic"* directory contains the basic algorithm, *"c_extended"* contains the extended algorithm and the *"c_simple_choir"* is the implementation of the basic algorithm to make a simple choir. These implementations are wrote to be easy to understood and they are not optimised for speed. You can test on Linux by running the *./compile.sh* scripts. It's recomanded to have snd installed, to make possible to hear the results as wav file. Of course, you can import the results into your instruments, because the waves are perfectly looped (set the first loop point to 0 and the second to the end of the wav). I put the source code under public domain, but it depends on [FFTW3_library], so, if you want to use into your products, you must use your IFFT routines to avoid licensing issues of the FFTW library.

### Public domain C++ class that implements the algorithm (ready to use)Edit

To be easy to use this algorithm into your projects or products, I made a ready-to-use C++ class. Only thing that you have to do, is to provide it an IFFT routine. Please read the header file for details.

The source code of C/C++ implementation is available here

Also, you can look at the C++ class here: CPP Implementation

## Tips and suggestionsEdit

- Keep in mind that the resulting wavetables are perfectly looped
- When using the wavetables into instruments, on each NoteOn, start from a random position and not from the start. This avoids hearing the same sound on each keystroke
- You can use the same wavetable for generating stereo sounds, by playing the same wavetable at different positions for left and right. The best is to make the difference between left right as N/2
- Generate different wavetables for different pitches and use the one who is closest to the desired pitch
- Upsample or downsample the harmonics's amplitude array before running the algorithm, according to the fundamental frequency. In this case we need to set a parameter
*"base_frequency"*which represents the frequency where the array is left unchanged.`Example: we have`*A_orig[]*={1,2,1,3,0,0,1,0} and*base_frequency*is equal to 440 Hz

Here are some cases:

- A[] for 440 Hz: is the same as A_orig[]

- A[] for 220 Hz: is the A_orig[] upsampled by factor of 2

so: A[]={1,**1**, 1.5,**2**, 1.5,**1**, 2,**3**, 1.5,**0**, 0,**0**, 0.5,**1**, 0.5,**0**}

(the original A_orig amplitudes are shown as bold)

- A[] for 880 Hz: the A_orig[] is downsampled by a factor of 2

so: A[]={1.5, 2, 0, 0.5}

- A[] for*F*Hz: the A_orig[] is scaled by a factor of 440/F.

Even if this idea is very simple, the resulting sounds are very natural, because it keeps the spectrum constant according to the harmonic's frequency and not to harmonic's number. This folows the point 4 from the document where I described some principles regarding synthesis.

## ConclusionsEdit

I hope that this algorithm will be implemented in many software/hardware synthesizers. Use it, spread it, write about it, make beautiful instruments with it. If your synthesizer uses plenty of samples, you can use this algorithm to generate many ready-to-use samples.

Source text The original source text and this page (including images,sounds,etc) are released under Public Domain.