How to make a simple FM synthesizer

The final product

This is the simple FM synthesizer I made in javascript. Sure it lacks all the modern features commercial FM synthesizers have, but it's enough for a showcase. The source code belongs to public domain, in case you need it.

The math behind the formulas

The maths I used are taken from this paper. All the variables are explained thoughly in the paper except for time t which is range [0..1]. We will work with the final formula: finalformula.png

This formula works perfectly for cases of a single modulator:algorithm1.png
// Modulator M1 modulates C1    
return formulaValues.A1*Math.sin( 2*Math.PI*formulaValues.C1*(i/sampleRate) + formulaValues.D1*Math.sin(2*Math.PI*formulaValues.M1*(i/sampleRate)) );
But we also need the frequency formula for a given time t to use it to create nested modulation: freqchformula.png algorithm2.png This case is solved in the following way:
// Modulator M2 modulates modulator M1 that modulates C1    
let M1Freq=formulaValues.M1+(formulaValues.D2*Math.sin(2*Math.PI*formulaValues.M2*(i/sampleRate)));
return formulaValues.A1*Math.sin( 2*Math.PI*formulaValues.C1*(i/sampleRate) + formulaValues.D1*Math.sin(2*Math.PI*M1Freq*(i/sampleRate)) );
That is, we first find the frequency of a given time t for modulator M1 that is being modulated by M2, and then apply that frequency to the final formula.